blob: 0410178cab8e44050f0a1bedb6b7a8d2fa14f79f [file] [log] [blame]
Jian Li47671902016-08-11 01:18:18 +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 Li47671902016-08-11 01:18:18 +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.DefaultLispLocatorRecord.DefaultLocatorRecordBuilder;
28import org.onosproject.lisp.msg.protocols.LispLocatorRecord.LocatorRecordBuilder;
29import org.onosproject.lisp.msg.protocols.DefaultLispLocatorRecord.LocatorRecordWriter;
30import org.onosproject.lisp.msg.protocols.DefaultLispLocatorRecord.LocatorRecordReader;
Jian Lie4ba2a42016-08-29 20:24:15 +090031import org.onosproject.lisp.msg.types.LispIpv4Address;
Jian Li47671902016-08-11 01:18:18 +090032
33import static org.hamcrest.MatcherAssert.assertThat;
34import static org.hamcrest.Matchers.is;
35
36/**
37 * Unit tests for DefaultLispLocatorRecord class.
38 */
39public final class DefaultLispLocatorRecordTest {
40
41 private LispLocatorRecord record1;
42 private LispLocatorRecord sameAsRecord1;
43 private LispLocatorRecord record2;
44
45 @Before
46 public void setup() {
47
Jian Li84b75822016-10-04 23:10:35 +090048 LocatorRecordBuilder builder1 = new DefaultLocatorRecordBuilder();
Jian Lie4ba2a42016-08-29 20:24:15 +090049
50 LispIpv4Address ipv4Locator1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.1"));
Jian Li47671902016-08-11 01:18:18 +090051
52 record1 = builder1
53 .withPriority((byte) 0x01)
54 .withWeight((byte) 0x01)
55 .withMulticastPriority((byte) 0x01)
56 .withMulticastWeight((byte) 0x01)
57 .withLocalLocator(true)
58 .withRlocProbed(false)
59 .withRouted(true)
Jian Lie4ba2a42016-08-29 20:24:15 +090060 .withLocatorAfi(ipv4Locator1)
Jian Li47671902016-08-11 01:18:18 +090061 .build();
62
Jian Li84b75822016-10-04 23:10:35 +090063 LocatorRecordBuilder builder2 = new DefaultLocatorRecordBuilder();
Jian Li47671902016-08-11 01:18:18 +090064
65 sameAsRecord1 = builder2
66 .withPriority((byte) 0x01)
67 .withWeight((byte) 0x01)
68 .withMulticastPriority((byte) 0x01)
69 .withMulticastWeight((byte) 0x01)
70 .withLocalLocator(true)
71 .withRlocProbed(false)
72 .withRouted(true)
Jian Lie4ba2a42016-08-29 20:24:15 +090073 .withLocatorAfi(ipv4Locator1)
Jian Li47671902016-08-11 01:18:18 +090074 .build();
75
Jian Li84b75822016-10-04 23:10:35 +090076 LocatorRecordBuilder builder3 = new DefaultLocatorRecordBuilder();
Jian Lie4ba2a42016-08-29 20:24:15 +090077
78 LispIpv4Address ipv4Locator2 = new LispIpv4Address(IpAddress.valueOf("192.168.1.2"));
Jian Li47671902016-08-11 01:18:18 +090079
80 record2 = builder3
81 .withPriority((byte) 0x02)
82 .withWeight((byte) 0x02)
83 .withMulticastPriority((byte) 0x02)
84 .withMulticastWeight((byte) 0x02)
85 .withLocalLocator(false)
86 .withRlocProbed(true)
87 .withRouted(false)
Jian Lie4ba2a42016-08-29 20:24:15 +090088 .withLocatorAfi(ipv4Locator2)
Jian Li47671902016-08-11 01:18:18 +090089 .build();
90 }
91
92 @Test
93 public void testEquality() {
94 new EqualsTester()
95 .addEqualityGroup(record1, sameAsRecord1)
96 .addEqualityGroup(record2).testEquals();
97 }
98
99 @Test
100 public void testConstruction() {
101 DefaultLispLocatorRecord record = (DefaultLispLocatorRecord) record1;
102
Jian Lie4ba2a42016-08-29 20:24:15 +0900103 LispIpv4Address ipv4Locator = new LispIpv4Address(IpAddress.valueOf("192.168.1.1"));
104
Jian Li47671902016-08-11 01:18:18 +0900105 assertThat(record.getPriority(), is((byte) 0x01));
106 assertThat(record.getWeight(), is((byte) 0x01));
107 assertThat(record.getMulticastPriority(), is((byte) 0x01));
108 assertThat(record.getMulticastWeight(), is((byte) 0x01));
109 assertThat(record.isLocalLocator(), is(true));
110 assertThat(record.isRlocProbed(), is(false));
111 assertThat(record.isRouted(), is(true));
Jian Lie4ba2a42016-08-29 20:24:15 +0900112 assertThat(record.getLocatorAfi(), is(ipv4Locator));
113 }
114
115 @Test
116 public void testSerialization() throws LispReaderException, LispWriterException, LispParseError {
117 ByteBuf byteBuf = Unpooled.buffer();
118
119 LocatorRecordWriter writer = new LocatorRecordWriter();
120 writer.writeTo(byteBuf, record1);
121
122 LocatorRecordReader reader = new LocatorRecordReader();
123 LispLocatorRecord deserialized = reader.readFrom(byteBuf);
124
125 new EqualsTester()
126 .addEqualityGroup(record1, deserialized).testEquals();
Jian Li47671902016-08-11 01:18:18 +0900127 }
128}