blob: 99dcfcfb3abf72dace9ae8ab07194932f327ff5b [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;
27import org.onosproject.lisp.msg.types.LispIpv4Address;
Jian Li47671902016-08-11 01:18:18 +090028
29import static org.hamcrest.MatcherAssert.assertThat;
30import static org.hamcrest.Matchers.is;
Jian Lie4ba2a42016-08-29 20:24:15 +090031import static org.onosproject.lisp.msg.protocols.DefaultLispLocatorRecord.*;
Jian Li47671902016-08-11 01:18:18 +090032
33/**
34 * Unit tests for DefaultLispLocatorRecord class.
35 */
36public final class DefaultLispLocatorRecordTest {
37
38 private LispLocatorRecord record1;
39 private LispLocatorRecord sameAsRecord1;
40 private LispLocatorRecord record2;
41
42 @Before
43 public void setup() {
44
45 LispLocatorRecord.LocatorRecordBuilder builder1 =
Jian Lie4ba2a42016-08-29 20:24:15 +090046 new DefaultLocatorRecordBuilder();
47
48 LispIpv4Address ipv4Locator1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.1"));
Jian Li47671902016-08-11 01:18:18 +090049
50 record1 = builder1
51 .withPriority((byte) 0x01)
52 .withWeight((byte) 0x01)
53 .withMulticastPriority((byte) 0x01)
54 .withMulticastWeight((byte) 0x01)
55 .withLocalLocator(true)
56 .withRlocProbed(false)
57 .withRouted(true)
Jian Lie4ba2a42016-08-29 20:24:15 +090058 .withLocatorAfi(ipv4Locator1)
Jian Li47671902016-08-11 01:18:18 +090059 .build();
60
61 LispLocatorRecord.LocatorRecordBuilder builder2 =
Jian Lie4ba2a42016-08-29 20:24:15 +090062 new DefaultLocatorRecordBuilder();
Jian Li47671902016-08-11 01:18:18 +090063
64 sameAsRecord1 = builder2
65 .withPriority((byte) 0x01)
66 .withWeight((byte) 0x01)
67 .withMulticastPriority((byte) 0x01)
68 .withMulticastWeight((byte) 0x01)
69 .withLocalLocator(true)
70 .withRlocProbed(false)
71 .withRouted(true)
Jian Lie4ba2a42016-08-29 20:24:15 +090072 .withLocatorAfi(ipv4Locator1)
Jian Li47671902016-08-11 01:18:18 +090073 .build();
74
75 LispLocatorRecord.LocatorRecordBuilder builder3 =
Jian Lie4ba2a42016-08-29 20:24:15 +090076 new DefaultLocatorRecordBuilder();
77
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}