blob: 4c266e55b4082d745604d27379b20e14e8129e83 [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
40 private LispMapRecord record1;
41 private LispMapRecord sameAsRecord1;
42 private LispMapRecord record2;
43
44 @Before
45 public void setup() {
46
Jian Lib26d3502016-08-31 01:43:24 +090047 MapRecordBuilder builder1 = new DefaultMapRecordBuilder();
Jian Lie4ba2a42016-08-29 20:24:15 +090048
49 LispIpv4Address ipv4Locator1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.1"));
Jian Li18f3bce2016-08-04 17:36:41 +090050
51 record1 = builder1
52 .withRecordTtl(100)
53 .withAuthoritative(true)
Jian Li18f3bce2016-08-04 17:36:41 +090054 .withMapVersionNumber((short) 1)
55 .withMaskLength((byte) 0x01)
Jian Lie4ba2a42016-08-29 20:24:15 +090056 .withAction(LispMapReplyAction.NativelyForward)
57 .withEidPrefixAfi(ipv4Locator1)
Jian Li18f3bce2016-08-04 17:36:41 +090058 .build();
59
Jian Lib26d3502016-08-31 01:43:24 +090060 MapRecordBuilder builder2 = new DefaultMapRecordBuilder();
Jian Li18f3bce2016-08-04 17:36:41 +090061
62 sameAsRecord1 = builder2
63 .withRecordTtl(100)
64 .withAuthoritative(true)
Jian Li18f3bce2016-08-04 17:36:41 +090065 .withMapVersionNumber((short) 1)
66 .withMaskLength((byte) 0x01)
Jian Lie4ba2a42016-08-29 20:24:15 +090067 .withAction(LispMapReplyAction.NativelyForward)
68 .withEidPrefixAfi(ipv4Locator1)
Jian Li18f3bce2016-08-04 17:36:41 +090069 .build();
70
Jian Lib26d3502016-08-31 01:43:24 +090071 MapRecordBuilder builder3 = new DefaultMapRecordBuilder();
Jian Lie4ba2a42016-08-29 20:24:15 +090072
73 LispIpv4Address ipv4Locator2 = new LispIpv4Address(IpAddress.valueOf("192.168.1.2"));
Jian Li18f3bce2016-08-04 17:36:41 +090074
75 record2 = builder3
76 .withRecordTtl(200)
77 .withAuthoritative(false)
Jian Li18f3bce2016-08-04 17:36:41 +090078 .withMapVersionNumber((short) 2)
79 .withMaskLength((byte) 0x02)
Jian Lie4ba2a42016-08-29 20:24:15 +090080 .withAction(LispMapReplyAction.Drop)
81 .withEidPrefixAfi(ipv4Locator2)
Jian Li18f3bce2016-08-04 17:36:41 +090082 .build();
83 }
84
85 @Test
86 public void testEquality() {
87 new EqualsTester()
88 .addEqualityGroup(record1, sameAsRecord1)
89 .addEqualityGroup(record2).testEquals();
90 }
91
92 @Test
93 public void testConstruction() {
94 DefaultLispMapRecord record = (DefaultLispMapRecord) record1;
95
Jian Lie4ba2a42016-08-29 20:24:15 +090096 LispIpv4Address ipv4Locator = new LispIpv4Address(IpAddress.valueOf("192.168.1.1"));
97
Jian Li18f3bce2016-08-04 17:36:41 +090098 assertThat(record.getRecordTtl(), is(100));
99 assertThat(record.isAuthoritative(), is(true));
Jian Li18f3bce2016-08-04 17:36:41 +0900100 assertThat(record.getMapVersionNumber(), is((short) 1));
101 assertThat(record.getMaskLength(), is((byte) 0x01));
Jian Lie4ba2a42016-08-29 20:24:15 +0900102 assertThat(record.getAction(), is(LispMapReplyAction.NativelyForward));
103 assertThat(record.getEidPrefixAfi(), is(ipv4Locator));
104 }
105
106 @Test
107 public void testSerialization() throws LispReaderException, LispWriterException, LispParseError {
108 ByteBuf byteBuf = Unpooled.buffer();
109
110 MapRecordWriter writer = new MapRecordWriter();
111 writer.writeTo(byteBuf, record1);
112
113 MapRecordReader reader = new MapRecordReader();
114 LispMapRecord deserialized = reader.readFrom(byteBuf);
115
116 new EqualsTester()
117 .addEqualityGroup(record1, deserialized).testEquals();
Jian Li18f3bce2016-08-04 17:36:41 +0900118 }
119}