blob: 338dfb655892c0f473e52ed036b345c4f3851562 [file] [log] [blame]
Jian Lie9af3b42016-08-08 15:50:01 +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.types;
17
18import com.google.common.testing.EqualsTester;
Jian Li76ea0572016-08-29 12:41:16 +090019import io.netty.buffer.ByteBuf;
20import io.netty.buffer.Unpooled;
Jian Lie9af3b42016-08-08 15:50:01 +090021import org.junit.Before;
22import org.junit.Test;
23import org.onlab.packet.IpAddress;
Jian Li76ea0572016-08-29 12:41:16 +090024import 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.types.LispAppDataLcafAddress.AppDataAddressBuilder;
Jian Li76ea0572016-08-29 12:41:16 +090028import org.onosproject.lisp.msg.types.LispAppDataLcafAddress.AppDataLcafAddressWriter;
Jian Li5e505c62016-12-05 02:44:24 +090029import org.onosproject.lisp.msg.types.LispAppDataLcafAddress.AppDataLcafAddressReader;
Jian Lie9af3b42016-08-08 15:50:01 +090030
31import static org.hamcrest.MatcherAssert.assertThat;
32import static org.hamcrest.Matchers.is;
33
34/**
35 * Unit tests for LispAppDataLcafAddress class.
36 */
37public class LispAppDataLcafAddressTest {
38
39 private LispAppDataLcafAddress address1;
40 private LispAppDataLcafAddress sameAsAddress1;
41 private LispAppDataLcafAddress address2;
42
43 @Before
44 public void setup() {
45
Jian Li84b75822016-10-04 23:10:35 +090046 AppDataAddressBuilder builder1 = new AppDataAddressBuilder();
Jian Lie9af3b42016-08-08 15:50:01 +090047
Jian Li115d8602016-08-15 20:21:53 +090048 LispAfiAddress ipv4Address1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.1"));
49
50 address1 = builder1
51 .withProtocol((byte) 0x01)
52 .withIpTos((short) 10)
53 .withLocalPortLow((short) 1)
54 .withLocalPortHigh((short) 255)
55 .withRemotePortLow((short) 2)
56 .withRemotePortHigh((short) 254)
57 .withAddress(ipv4Address1)
58 .build();
59
Jian Li84b75822016-10-04 23:10:35 +090060 AppDataAddressBuilder builder2 = new AppDataAddressBuilder();
Jian Li115d8602016-08-15 20:21:53 +090061
62 sameAsAddress1 = builder2
63 .withProtocol((byte) 0x01)
64 .withIpTos((short) 10)
65 .withLocalPortLow((short) 1)
66 .withLocalPortHigh((short) 255)
67 .withRemotePortLow((short) 2)
68 .withRemotePortHigh((short) 254)
69 .withAddress(ipv4Address1)
70 .build();
71
Jian Li84b75822016-10-04 23:10:35 +090072 AppDataAddressBuilder builder3 = new AppDataAddressBuilder();
Jian Lie9af3b42016-08-08 15:50:01 +090073
74 LispAfiAddress ipv4Address2 = new LispIpv4Address(IpAddress.valueOf("192.168.2.1"));
Jian Li115d8602016-08-15 20:21:53 +090075
76 address2 = builder3
77 .withProtocol((byte) 0x02)
78 .withIpTos((short) 20)
79 .withLocalPortLow((short) 1)
80 .withLocalPortHigh((short) 255)
81 .withRemotePortLow((short) 2)
82 .withRemotePortHigh((short) 254)
83 .withAddress(ipv4Address2)
84 .build();
Jian Lie9af3b42016-08-08 15:50:01 +090085 }
86
87 @Test
88 public void testEquality() {
89 new EqualsTester()
90 .addEqualityGroup(address1, sameAsAddress1)
91 .addEqualityGroup(address2).testEquals();
92 }
93
94 @Test
95 public void testConstruction() {
96 LispAppDataLcafAddress appDataLcafAddress = address1;
97
98 LispAfiAddress ipv4Address = new LispIpv4Address(IpAddress.valueOf("192.168.1.1"));
99
100 assertThat(appDataLcafAddress.getProtocol(), is((byte) 0x01));
Jian Li115d8602016-08-15 20:21:53 +0900101 assertThat(appDataLcafAddress.getIpTos(), is(10));
102 assertThat(appDataLcafAddress.getLocalPortLow(), is((short) 1));
103 assertThat(appDataLcafAddress.getLocalPortHigh(), is((short) 255));
104 assertThat(appDataLcafAddress.getRemotePortLow(), is((short) 2));
105 assertThat(appDataLcafAddress.getRemotePortHigh(), is((short) 254));
Jian Lie9af3b42016-08-08 15:50:01 +0900106 assertThat(appDataLcafAddress.getAddress(), is(ipv4Address));
107 }
Jian Li76ea0572016-08-29 12:41:16 +0900108
109 @Test
110 public void testSerialization() throws LispWriterException, LispParseError, LispReaderException {
111 ByteBuf byteBuf = Unpooled.buffer();
112
113 AppDataLcafAddressWriter writer = new AppDataLcafAddressWriter();
114 writer.writeTo(byteBuf, address1);
115
116 AppDataLcafAddressReader reader = new AppDataLcafAddressReader();
117 LispAppDataLcafAddress deserialized = reader.readFrom(byteBuf);
118
Jian Li5e505c62016-12-05 02:44:24 +0900119 new EqualsTester().addEqualityGroup(address1, deserialized).testEquals();
Jian Li76ea0572016-08-29 12:41:16 +0900120 }
Jian Lie9af3b42016-08-08 15:50:01 +0900121}