blob: 69bf4956c13ecb098a6ad0d9f23265259f59329b [file] [log] [blame]
Jian Li18f3bce2016-08-04 17:36:41 +09001/*
2 * Copyright 2016-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 */
16package org.onosproject.lisp.msg.protocols;
17
18import com.google.common.testing.EqualsTester;
Jian Lie4ba2a42016-08-29 20:24:15 +090019import io.netty.buffer.ByteBuf;
20import io.netty.buffer.Unpooled;
Jian Li18f3bce2016-08-04 17:36:41 +090021import org.junit.Before;
22import org.junit.Test;
Jian Lie4ba2a42016-08-29 20:24:15 +090023import org.onlab.packet.IpAddress;
24import org.onosproject.lisp.msg.exceptions.LispParseError;
25import org.onosproject.lisp.msg.exceptions.LispReaderException;
26import org.onosproject.lisp.msg.exceptions.LispWriterException;
Jian Li5e505c62016-12-05 02:44:24 +090027import org.onosproject.lisp.msg.protocols.DefaultLispMapRecord.DefaultMapRecordBuilder;
28import org.onosproject.lisp.msg.protocols.LispMapRecord.MapRecordBuilder;
Jian Lie4ba2a42016-08-29 20:24:15 +090029import org.onosproject.lisp.msg.types.LispIpv4Address;
Jian Li18f3bce2016-08-04 17:36:41 +090030
31import static org.hamcrest.MatcherAssert.assertThat;
32import static org.hamcrest.Matchers.is;
Jian Lie4ba2a42016-08-29 20:24:15 +090033import static org.onosproject.lisp.msg.protocols.DefaultLispMapRecord.*;
Jian Li18f3bce2016-08-04 17:36:41 +090034
35/**
36 * Unit tests for DefaultLispMapRecord class.
37 */
Jian Li47671902016-08-11 01:18:18 +090038public final class DefaultLispMapRecordTest {
Jian Li18f3bce2016-08-04 17:36:41 +090039
Jian Li672ebda2017-02-06 20:21:04 +090040 private static final String IP_ADDRESS_1 = "192.168.1.1";
41 private static final String IP_ADDRESS_2 = "192.168.1.2";
42
Jian Li18f3bce2016-08-04 17:36:41 +090043 private LispMapRecord record1;
44 private LispMapRecord sameAsRecord1;
45 private LispMapRecord record2;
46
47 @Before
48 public void setup() {
49
Jian Lib26d3502016-08-31 01:43:24 +090050 MapRecordBuilder builder1 = new DefaultMapRecordBuilder();
Jian Lie4ba2a42016-08-29 20:24:15 +090051
Jian Li672ebda2017-02-06 20:21:04 +090052 LispIpv4Address ipv4Locator1 = new LispIpv4Address(IpAddress.valueOf(IP_ADDRESS_1));
Jian Li18f3bce2016-08-04 17:36:41 +090053
54 record1 = builder1
55 .withRecordTtl(100)
Jian Li672ebda2017-02-06 20:21:04 +090056 .withIsAuthoritative(true)
Jian Li18f3bce2016-08-04 17:36:41 +090057 .withMapVersionNumber((short) 1)
58 .withMaskLength((byte) 0x01)
Jian Lie4ba2a42016-08-29 20:24:15 +090059 .withAction(LispMapReplyAction.NativelyForward)
60 .withEidPrefixAfi(ipv4Locator1)
Jian Li18f3bce2016-08-04 17:36:41 +090061 .build();
62
Jian Lib26d3502016-08-31 01:43:24 +090063 MapRecordBuilder builder2 = new DefaultMapRecordBuilder();
Jian Li18f3bce2016-08-04 17:36:41 +090064
65 sameAsRecord1 = builder2
66 .withRecordTtl(100)
Jian Li672ebda2017-02-06 20:21:04 +090067 .withIsAuthoritative(true)
Jian Li18f3bce2016-08-04 17:36:41 +090068 .withMapVersionNumber((short) 1)
69 .withMaskLength((byte) 0x01)
Jian Lie4ba2a42016-08-29 20:24:15 +090070 .withAction(LispMapReplyAction.NativelyForward)
71 .withEidPrefixAfi(ipv4Locator1)
Jian Li18f3bce2016-08-04 17:36:41 +090072 .build();
73
Jian Lib26d3502016-08-31 01:43:24 +090074 MapRecordBuilder builder3 = new DefaultMapRecordBuilder();
Jian Lie4ba2a42016-08-29 20:24:15 +090075
Jian Li672ebda2017-02-06 20:21:04 +090076 LispIpv4Address ipv4Locator2 = new LispIpv4Address(IpAddress.valueOf(IP_ADDRESS_2));
Jian Li18f3bce2016-08-04 17:36:41 +090077
78 record2 = builder3
79 .withRecordTtl(200)
Jian Li672ebda2017-02-06 20:21:04 +090080 .withIsAuthoritative(false)
Jian Li18f3bce2016-08-04 17:36:41 +090081 .withMapVersionNumber((short) 2)
82 .withMaskLength((byte) 0x02)
Jian Lie4ba2a42016-08-29 20:24:15 +090083 .withAction(LispMapReplyAction.Drop)
84 .withEidPrefixAfi(ipv4Locator2)
Jian Li18f3bce2016-08-04 17:36:41 +090085 .build();
86 }
87
88 @Test
89 public void testEquality() {
90 new EqualsTester()
91 .addEqualityGroup(record1, sameAsRecord1)
92 .addEqualityGroup(record2).testEquals();
93 }
94
95 @Test
96 public void testConstruction() {
97 DefaultLispMapRecord record = (DefaultLispMapRecord) record1;
98
Jian Li672ebda2017-02-06 20:21:04 +090099 LispIpv4Address ipv4Locator = new LispIpv4Address(IpAddress.valueOf(IP_ADDRESS_1));
Jian Lie4ba2a42016-08-29 20:24:15 +0900100
Jian Li18f3bce2016-08-04 17:36:41 +0900101 assertThat(record.getRecordTtl(), is(100));
102 assertThat(record.isAuthoritative(), is(true));
Jian Li18f3bce2016-08-04 17:36:41 +0900103 assertThat(record.getMapVersionNumber(), is((short) 1));
104 assertThat(record.getMaskLength(), is((byte) 0x01));
Jian Lie4ba2a42016-08-29 20:24:15 +0900105 assertThat(record.getAction(), is(LispMapReplyAction.NativelyForward));
106 assertThat(record.getEidPrefixAfi(), is(ipv4Locator));
107 }
108
109 @Test
110 public void testSerialization() throws LispReaderException, LispWriterException, LispParseError {
111 ByteBuf byteBuf = Unpooled.buffer();
112
113 MapRecordWriter writer = new MapRecordWriter();
114 writer.writeTo(byteBuf, record1);
115
116 MapRecordReader reader = new MapRecordReader();
117 LispMapRecord deserialized = reader.readFrom(byteBuf);
118
119 new EqualsTester()
120 .addEqualityGroup(record1, deserialized).testEquals();
Jian Li18f3bce2016-08-04 17:36:41 +0900121 }
122}