Jian Li | d55111b | 2017-02-07 10:36:40 +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.onlab.packet.IpAddress; |
| 25 | import org.onosproject.lisp.msg.exceptions.LispParseError; |
| 26 | import org.onosproject.lisp.msg.exceptions.LispReaderException; |
| 27 | import org.onosproject.lisp.msg.exceptions.LispWriterException; |
| 28 | import org.onosproject.lisp.msg.protocols.DefaultLispReferralRecord.DefaultReferralRecordBuilder; |
| 29 | import org.onosproject.lisp.msg.protocols.DefaultLispReferralRecord.ReferralRecordReader; |
| 30 | import org.onosproject.lisp.msg.protocols.DefaultLispReferralRecord.ReferralRecordWriter; |
| 31 | import org.onosproject.lisp.msg.protocols.LispReferralRecord.ReferralRecordBuilder; |
| 32 | import org.onosproject.lisp.msg.types.LispIpv4Address; |
| 33 | |
| 34 | import static org.hamcrest.MatcherAssert.assertThat; |
| 35 | import static org.hamcrest.Matchers.is; |
| 36 | |
| 37 | /** |
| 38 | * Unit tests for DefaultLispReferralRecord class. |
| 39 | */ |
| 40 | public final class DefaultLispReferralRecordTest { |
| 41 | |
| 42 | private static final String IP_ADDRESS1 = "192.168.1.1"; |
| 43 | private static final String IP_ADDRESS2 = "192.168.1.2"; |
| 44 | |
| 45 | private LispReferralRecord record1; |
| 46 | private LispReferralRecord sameAsRecord1; |
| 47 | private LispReferralRecord record2; |
| 48 | |
| 49 | @Before |
| 50 | public void setup() { |
| 51 | |
| 52 | ReferralRecordBuilder builder1 = new DefaultReferralRecordBuilder(); |
| 53 | |
| 54 | LispIpv4Address ipv4Address1 = |
| 55 | new LispIpv4Address(IpAddress.valueOf(IP_ADDRESS1)); |
| 56 | |
| 57 | record1 = builder1 |
| 58 | .withRecordTtl(100) |
| 59 | .withIsAuthoritative(true) |
| 60 | .withIsIncomplete(false) |
| 61 | .withMapVersionNumber((short) 1) |
| 62 | .withMaskLength((byte) 0x01) |
| 63 | .withAction(LispMapReplyAction.NativelyForward) |
| 64 | .withEidPrefixAfi(ipv4Address1) |
| 65 | .build(); |
| 66 | |
| 67 | ReferralRecordBuilder builder2 = new DefaultReferralRecordBuilder(); |
| 68 | |
| 69 | sameAsRecord1 = builder2 |
| 70 | .withRecordTtl(100) |
| 71 | .withIsAuthoritative(true) |
| 72 | .withIsIncomplete(false) |
| 73 | .withMapVersionNumber((short) 1) |
| 74 | .withMaskLength((byte) 0x01) |
| 75 | .withAction(LispMapReplyAction.NativelyForward) |
| 76 | .withEidPrefixAfi(ipv4Address1) |
| 77 | .build(); |
| 78 | |
| 79 | ReferralRecordBuilder builder3 = new DefaultReferralRecordBuilder(); |
| 80 | |
| 81 | LispIpv4Address ipv4Address2 = |
| 82 | new LispIpv4Address(IpAddress.valueOf(IP_ADDRESS2)); |
| 83 | |
| 84 | record2 = builder3 |
| 85 | .withRecordTtl(200) |
| 86 | .withIsAuthoritative(false) |
| 87 | .withIsIncomplete(true) |
| 88 | .withMapVersionNumber((short) 2) |
| 89 | .withMaskLength((byte) 0x02) |
| 90 | .withAction(LispMapReplyAction.Drop) |
| 91 | .withEidPrefixAfi(ipv4Address2) |
| 92 | .build(); |
| 93 | } |
| 94 | |
| 95 | @Test |
| 96 | public void testEquality() { |
| 97 | new EqualsTester() |
| 98 | .addEqualityGroup(record1, sameAsRecord1) |
| 99 | .addEqualityGroup(record2).testEquals(); |
| 100 | } |
| 101 | |
| 102 | @Test |
| 103 | public void testConstruction() { |
| 104 | LispReferralRecord record = record1; |
| 105 | |
| 106 | LispIpv4Address ipv4Address1 = |
| 107 | new LispIpv4Address(IpAddress.valueOf(IP_ADDRESS1)); |
| 108 | |
| 109 | assertThat(record.getRecordTtl(), is(100)); |
| 110 | assertThat(record.isAuthoritative(), is(true)); |
| 111 | assertThat(record.isIncomplete(), is(false)); |
| 112 | assertThat(record.getMapVersionNumber(), is((short) 1)); |
| 113 | assertThat(record.getMaskLength(), is((byte) 0x01)); |
| 114 | assertThat(record.getAction(), is(LispMapReplyAction.NativelyForward)); |
| 115 | assertThat(record.getEidPrefixAfi(), is(ipv4Address1)); |
| 116 | } |
| 117 | |
| 118 | @Test |
| 119 | public void testSerialization() throws LispReaderException, LispWriterException, |
| 120 | LispParseError, DeserializationException { |
| 121 | ByteBuf byteBuf = Unpooled.buffer(); |
| 122 | |
| 123 | ReferralRecordWriter writer = new ReferralRecordWriter(); |
| 124 | writer.writeTo(byteBuf, record1); |
| 125 | |
| 126 | ReferralRecordReader reader = new ReferralRecordReader(); |
| 127 | LispReferralRecord deserialized = reader.readFrom(byteBuf); |
| 128 | |
| 129 | new EqualsTester() |
| 130 | .addEqualityGroup(record1, deserialized).testEquals(); |
| 131 | } |
| 132 | } |