blob: ec45980b9ab74dfdd070a1a2b3e2cca99cda832c [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
Jian Li84b75822016-10-04 23:10:35 +090045 LocatorRecordBuilder builder1 = new DefaultLocatorRecordBuilder();
Jian Lie4ba2a42016-08-29 20:24:15 +090046
47 LispIpv4Address ipv4Locator1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.1"));
Jian Li47671902016-08-11 01:18:18 +090048
49 record1 = builder1
50 .withPriority((byte) 0x01)
51 .withWeight((byte) 0x01)
52 .withMulticastPriority((byte) 0x01)
53 .withMulticastWeight((byte) 0x01)
54 .withLocalLocator(true)
55 .withRlocProbed(false)
56 .withRouted(true)
Jian Lie4ba2a42016-08-29 20:24:15 +090057 .withLocatorAfi(ipv4Locator1)
Jian Li47671902016-08-11 01:18:18 +090058 .build();
59
Jian Li84b75822016-10-04 23:10:35 +090060 LocatorRecordBuilder builder2 = new DefaultLocatorRecordBuilder();
Jian Li47671902016-08-11 01:18:18 +090061
62 sameAsRecord1 = builder2
63 .withPriority((byte) 0x01)
64 .withWeight((byte) 0x01)
65 .withMulticastPriority((byte) 0x01)
66 .withMulticastWeight((byte) 0x01)
67 .withLocalLocator(true)
68 .withRlocProbed(false)
69 .withRouted(true)
Jian Lie4ba2a42016-08-29 20:24:15 +090070 .withLocatorAfi(ipv4Locator1)
Jian Li47671902016-08-11 01:18:18 +090071 .build();
72
Jian Li84b75822016-10-04 23:10:35 +090073 LocatorRecordBuilder builder3 = new DefaultLocatorRecordBuilder();
Jian Lie4ba2a42016-08-29 20:24:15 +090074
75 LispIpv4Address ipv4Locator2 = new LispIpv4Address(IpAddress.valueOf("192.168.1.2"));
Jian Li47671902016-08-11 01:18:18 +090076
77 record2 = builder3
78 .withPriority((byte) 0x02)
79 .withWeight((byte) 0x02)
80 .withMulticastPriority((byte) 0x02)
81 .withMulticastWeight((byte) 0x02)
82 .withLocalLocator(false)
83 .withRlocProbed(true)
84 .withRouted(false)
Jian Lie4ba2a42016-08-29 20:24:15 +090085 .withLocatorAfi(ipv4Locator2)
Jian Li47671902016-08-11 01:18:18 +090086 .build();
87 }
88
89 @Test
90 public void testEquality() {
91 new EqualsTester()
92 .addEqualityGroup(record1, sameAsRecord1)
93 .addEqualityGroup(record2).testEquals();
94 }
95
96 @Test
97 public void testConstruction() {
98 DefaultLispLocatorRecord record = (DefaultLispLocatorRecord) record1;
99
Jian Lie4ba2a42016-08-29 20:24:15 +0900100 LispIpv4Address ipv4Locator = new LispIpv4Address(IpAddress.valueOf("192.168.1.1"));
101
Jian Li47671902016-08-11 01:18:18 +0900102 assertThat(record.getPriority(), is((byte) 0x01));
103 assertThat(record.getWeight(), is((byte) 0x01));
104 assertThat(record.getMulticastPriority(), is((byte) 0x01));
105 assertThat(record.getMulticastWeight(), is((byte) 0x01));
106 assertThat(record.isLocalLocator(), is(true));
107 assertThat(record.isRlocProbed(), is(false));
108 assertThat(record.isRouted(), is(true));
Jian Lie4ba2a42016-08-29 20:24:15 +0900109 assertThat(record.getLocatorAfi(), is(ipv4Locator));
110 }
111
112 @Test
113 public void testSerialization() throws LispReaderException, LispWriterException, LispParseError {
114 ByteBuf byteBuf = Unpooled.buffer();
115
116 LocatorRecordWriter writer = new LocatorRecordWriter();
117 writer.writeTo(byteBuf, record1);
118
119 LocatorRecordReader reader = new LocatorRecordReader();
120 LispLocatorRecord deserialized = reader.readFrom(byteBuf);
121
122 new EqualsTester()
123 .addEqualityGroup(record1, deserialized).testEquals();
Jian Li47671902016-08-11 01:18:18 +0900124 }
125}