blob: 053b1e942a90379d9ae2f4da6f90028c932ba56e [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 Li672ebda2017-02-06 20:21:04 +090027import org.onosproject.lisp.msg.protocols.DefaultLispLocator.DefaultLocatorBuilder;
28import org.onosproject.lisp.msg.protocols.DefaultLispLocator.LocatorReader;
29import org.onosproject.lisp.msg.protocols.DefaultLispLocator.LocatorWriter;
30import org.onosproject.lisp.msg.protocols.LispLocator.LocatorBuilder;
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/**
Jian Li672ebda2017-02-06 20:21:04 +090037 * Unit tests for DefaultLispLocator class.
Jian Li47671902016-08-11 01:18:18 +090038 */
Jian Li672ebda2017-02-06 20:21:04 +090039public final class DefaultLispLocatorTest {
Jian Li47671902016-08-11 01:18:18 +090040
Jian Li672ebda2017-02-06 20:21:04 +090041 private static final String IP_ADDRESS_1 = "192.168.1.1";
42 private static final String IP_ADDRESS_2 = "192.168.1.2";
43
44 private LispLocator record1;
45 private LispLocator sameAsRecord1;
46 private LispLocator record2;
Jian Li47671902016-08-11 01:18:18 +090047
48 @Before
49 public void setup() {
50
Jian Li672ebda2017-02-06 20:21:04 +090051 LispLocator.LocatorBuilder builder1 = new DefaultLocatorBuilder();
Jian Lie4ba2a42016-08-29 20:24:15 +090052
Jian Li672ebda2017-02-06 20:21:04 +090053 LispIpv4Address ipv4Locator1 = new LispIpv4Address(IpAddress.valueOf(IP_ADDRESS_1));
Jian Li47671902016-08-11 01:18:18 +090054
55 record1 = builder1
56 .withPriority((byte) 0x01)
57 .withWeight((byte) 0x01)
58 .withMulticastPriority((byte) 0x01)
59 .withMulticastWeight((byte) 0x01)
60 .withLocalLocator(true)
61 .withRlocProbed(false)
62 .withRouted(true)
Jian Lie4ba2a42016-08-29 20:24:15 +090063 .withLocatorAfi(ipv4Locator1)
Jian Li47671902016-08-11 01:18:18 +090064 .build();
65
Jian Li672ebda2017-02-06 20:21:04 +090066 LocatorBuilder builder2 = new DefaultLocatorBuilder();
Jian Li47671902016-08-11 01:18:18 +090067
68 sameAsRecord1 = builder2
69 .withPriority((byte) 0x01)
70 .withWeight((byte) 0x01)
71 .withMulticastPriority((byte) 0x01)
72 .withMulticastWeight((byte) 0x01)
73 .withLocalLocator(true)
74 .withRlocProbed(false)
75 .withRouted(true)
Jian Lie4ba2a42016-08-29 20:24:15 +090076 .withLocatorAfi(ipv4Locator1)
Jian Li47671902016-08-11 01:18:18 +090077 .build();
78
Jian Li672ebda2017-02-06 20:21:04 +090079 LispLocator.LocatorBuilder builder3 = new DefaultLocatorBuilder();
Jian Lie4ba2a42016-08-29 20:24:15 +090080
Jian Li672ebda2017-02-06 20:21:04 +090081 LispIpv4Address ipv4Locator2 = new LispIpv4Address(IpAddress.valueOf(IP_ADDRESS_2));
Jian Li47671902016-08-11 01:18:18 +090082
83 record2 = builder3
84 .withPriority((byte) 0x02)
85 .withWeight((byte) 0x02)
86 .withMulticastPriority((byte) 0x02)
87 .withMulticastWeight((byte) 0x02)
88 .withLocalLocator(false)
89 .withRlocProbed(true)
90 .withRouted(false)
Jian Lie4ba2a42016-08-29 20:24:15 +090091 .withLocatorAfi(ipv4Locator2)
Jian Li47671902016-08-11 01:18:18 +090092 .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() {
Jian Li672ebda2017-02-06 20:21:04 +0900104 DefaultLispLocator record = (DefaultLispLocator) record1;
Jian Li47671902016-08-11 01:18:18 +0900105
Jian Li672ebda2017-02-06 20:21:04 +0900106 LispIpv4Address ipv4Locator = new LispIpv4Address(IpAddress.valueOf(IP_ADDRESS_1));
Jian Lie4ba2a42016-08-29 20:24:15 +0900107
Jian Li47671902016-08-11 01:18:18 +0900108 assertThat(record.getPriority(), is((byte) 0x01));
109 assertThat(record.getWeight(), is((byte) 0x01));
110 assertThat(record.getMulticastPriority(), is((byte) 0x01));
111 assertThat(record.getMulticastWeight(), is((byte) 0x01));
112 assertThat(record.isLocalLocator(), is(true));
113 assertThat(record.isRlocProbed(), is(false));
114 assertThat(record.isRouted(), is(true));
Jian Lie4ba2a42016-08-29 20:24:15 +0900115 assertThat(record.getLocatorAfi(), is(ipv4Locator));
116 }
117
118 @Test
119 public void testSerialization() throws LispReaderException, LispWriterException, LispParseError {
120 ByteBuf byteBuf = Unpooled.buffer();
121
Jian Li672ebda2017-02-06 20:21:04 +0900122 LocatorWriter writer = new LocatorWriter();
Jian Lie4ba2a42016-08-29 20:24:15 +0900123 writer.writeTo(byteBuf, record1);
124
Jian Li672ebda2017-02-06 20:21:04 +0900125 LocatorReader reader = new LocatorReader();
126 LispLocator deserialized = reader.readFrom(byteBuf);
Jian Lie4ba2a42016-08-29 20:24:15 +0900127
128 new EqualsTester()
129 .addEqualityGroup(record1, deserialized).testEquals();
Jian Li47671902016-08-11 01:18:18 +0900130 }
131}