Jian Li | 4fc55e3 | 2017-02-07 06:25:50 +0900 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.lisp.msg.protocols; |
| 17 | |
| 18 | import com.google.common.testing.EqualsTester; |
| 19 | import io.netty.buffer.ByteBuf; |
| 20 | import io.netty.buffer.Unpooled; |
| 21 | import org.junit.Before; |
| 22 | import org.junit.Test; |
| 23 | import org.onlab.packet.DeserializationException; |
| 24 | import org.onosproject.lisp.msg.exceptions.LispParseError; |
| 25 | import org.onosproject.lisp.msg.exceptions.LispReaderException; |
| 26 | import org.onosproject.lisp.msg.exceptions.LispWriterException; |
| 27 | import org.onosproject.lisp.msg.protocols.DefaultLispSignature.DefaultSignatureBuilder; |
| 28 | import org.onosproject.lisp.msg.protocols.DefaultLispSignature.SignatureReader; |
| 29 | import org.onosproject.lisp.msg.protocols.DefaultLispSignature.SignatureWriter; |
| 30 | import org.onosproject.lisp.msg.protocols.LispSignature.SignatureBuilder; |
| 31 | |
| 32 | import static org.hamcrest.MatcherAssert.assertThat; |
| 33 | import static org.hamcrest.Matchers.is; |
| 34 | |
| 35 | /** |
| 36 | * Unit tests for DefaultLispSignature class. |
| 37 | */ |
| 38 | public final class DefaultLispSignatureTest { |
| 39 | |
| 40 | private static final int SIG_UNIQUE_VALUE_1 = 100; |
| 41 | private static final int SIG_UNIQUE_VALUE_2 = 200; |
| 42 | |
| 43 | private LispSignature signature1; |
| 44 | private LispSignature sameAsSignature1; |
| 45 | private LispSignature signature2; |
| 46 | |
| 47 | @Before |
| 48 | public void setup() { |
| 49 | |
| 50 | SignatureBuilder builder1 = new DefaultSignatureBuilder(); |
| 51 | |
| 52 | signature1 = builder1 |
| 53 | .withRecordTtl(SIG_UNIQUE_VALUE_1) |
| 54 | .withSigExpiration(SIG_UNIQUE_VALUE_1) |
| 55 | .withSigInception(SIG_UNIQUE_VALUE_1) |
| 56 | .withKeyTag((short) SIG_UNIQUE_VALUE_1) |
| 57 | .withSigLength((short) SIG_UNIQUE_VALUE_1) |
| 58 | .withSigAlgorithm((byte) 1) |
| 59 | .withSignature(SIG_UNIQUE_VALUE_1) |
| 60 | .build(); |
| 61 | |
| 62 | SignatureBuilder builder2 = new DefaultSignatureBuilder(); |
| 63 | |
| 64 | sameAsSignature1 = builder2 |
| 65 | .withRecordTtl(SIG_UNIQUE_VALUE_1) |
| 66 | .withSigExpiration(SIG_UNIQUE_VALUE_1) |
| 67 | .withSigInception(SIG_UNIQUE_VALUE_1) |
| 68 | .withKeyTag((short) SIG_UNIQUE_VALUE_1) |
| 69 | .withSigLength((short) SIG_UNIQUE_VALUE_1) |
| 70 | .withSigAlgorithm((byte) 1) |
| 71 | .withSignature(SIG_UNIQUE_VALUE_1) |
| 72 | .build(); |
| 73 | |
| 74 | SignatureBuilder builder3 = new DefaultSignatureBuilder(); |
| 75 | |
| 76 | signature2 = builder3 |
| 77 | .withRecordTtl(SIG_UNIQUE_VALUE_2) |
| 78 | .withSigExpiration(SIG_UNIQUE_VALUE_2) |
| 79 | .withSigInception(SIG_UNIQUE_VALUE_2) |
| 80 | .withKeyTag((short) SIG_UNIQUE_VALUE_2) |
| 81 | .withSigLength((short) SIG_UNIQUE_VALUE_2) |
| 82 | .withSigAlgorithm((byte) 2) |
| 83 | .withSignature(SIG_UNIQUE_VALUE_2) |
| 84 | .build(); |
| 85 | } |
| 86 | |
| 87 | @Test |
| 88 | public void testEquality() { |
| 89 | new EqualsTester() |
| 90 | .addEqualityGroup(signature1, sameAsSignature1) |
| 91 | .addEqualityGroup(signature2).testEquals(); |
| 92 | } |
| 93 | |
| 94 | @Test |
| 95 | public void testConstruction() { |
| 96 | LispSignature signature = signature1; |
| 97 | |
| 98 | assertThat(signature.getRecordTtl(), is(SIG_UNIQUE_VALUE_1)); |
| 99 | assertThat(signature.getSigExpiration(), is(SIG_UNIQUE_VALUE_1)); |
| 100 | assertThat(signature.getSigInception(), is(SIG_UNIQUE_VALUE_1)); |
| 101 | assertThat(signature.getKeyTag(), is((short) SIG_UNIQUE_VALUE_1)); |
| 102 | assertThat(signature.getSigLength(), is((short) SIG_UNIQUE_VALUE_1)); |
| 103 | assertThat(signature.getSigAlgorithm(), is((byte) 1)); |
| 104 | assertThat(signature.getSignature(), is(SIG_UNIQUE_VALUE_1)); |
| 105 | } |
| 106 | |
| 107 | @Test |
| 108 | public void testSerialization() throws LispReaderException, DeserializationException, |
| 109 | LispWriterException, LispParseError { |
| 110 | ByteBuf byteBuf = Unpooled.buffer(); |
| 111 | |
| 112 | SignatureWriter writer = new SignatureWriter(); |
| 113 | writer.writeTo(byteBuf, signature1); |
| 114 | |
| 115 | SignatureReader reader = new SignatureReader(); |
| 116 | LispSignature deserialized = reader.readFrom(byteBuf); |
| 117 | |
| 118 | new EqualsTester() |
| 119 | .addEqualityGroup(signature1, deserialized).testEquals(); |
| 120 | } |
| 121 | } |