blob: f9a00ee6dc4276d0aa1e98e8411e7f1acfca224e [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;
27import org.onosproject.lisp.msg.types.LispAppDataLcafAddress.AppDataLcafAddressWriter;
Jian Lie9af3b42016-08-08 15:50:01 +090028
29import static org.hamcrest.MatcherAssert.assertThat;
30import static org.hamcrest.Matchers.is;
Jian Li76ea0572016-08-29 12:41:16 +090031import static org.onosproject.lisp.msg.types.LispAppDataLcafAddress.AppDataAddressBuilder;
32import static org.onosproject.lisp.msg.types.LispAppDataLcafAddress.AppDataLcafAddressReader;
Jian Lie9af3b42016-08-08 15:50:01 +090033
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 Li76ea0572016-08-29 12:41:16 +090046 AppDataAddressBuilder builder1 =
47 new AppDataAddressBuilder();
Jian Lie9af3b42016-08-08 15:50:01 +090048
Jian Li115d8602016-08-15 20:21:53 +090049 LispAfiAddress ipv4Address1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.1"));
50
51 address1 = builder1
52 .withProtocol((byte) 0x01)
53 .withIpTos((short) 10)
54 .withLocalPortLow((short) 1)
55 .withLocalPortHigh((short) 255)
56 .withRemotePortLow((short) 2)
57 .withRemotePortHigh((short) 254)
58 .withAddress(ipv4Address1)
59 .build();
60
Jian Li76ea0572016-08-29 12:41:16 +090061 AppDataAddressBuilder builder2 =
62 new AppDataAddressBuilder();
Jian Li115d8602016-08-15 20:21:53 +090063
64 sameAsAddress1 = builder2
65 .withProtocol((byte) 0x01)
66 .withIpTos((short) 10)
67 .withLocalPortLow((short) 1)
68 .withLocalPortHigh((short) 255)
69 .withRemotePortLow((short) 2)
70 .withRemotePortHigh((short) 254)
71 .withAddress(ipv4Address1)
72 .build();
73
Jian Li76ea0572016-08-29 12:41:16 +090074 AppDataAddressBuilder builder3 =
75 new AppDataAddressBuilder();
Jian Lie9af3b42016-08-08 15:50:01 +090076
77 LispAfiAddress ipv4Address2 = new LispIpv4Address(IpAddress.valueOf("192.168.2.1"));
Jian Li115d8602016-08-15 20:21:53 +090078
79 address2 = builder3
80 .withProtocol((byte) 0x02)
81 .withIpTos((short) 20)
82 .withLocalPortLow((short) 1)
83 .withLocalPortHigh((short) 255)
84 .withRemotePortLow((short) 2)
85 .withRemotePortHigh((short) 254)
86 .withAddress(ipv4Address2)
87 .build();
Jian Lie9af3b42016-08-08 15:50:01 +090088 }
89
90 @Test
91 public void testEquality() {
92 new EqualsTester()
93 .addEqualityGroup(address1, sameAsAddress1)
94 .addEqualityGroup(address2).testEquals();
95 }
96
97 @Test
98 public void testConstruction() {
99 LispAppDataLcafAddress appDataLcafAddress = address1;
100
101 LispAfiAddress ipv4Address = new LispIpv4Address(IpAddress.valueOf("192.168.1.1"));
102
103 assertThat(appDataLcafAddress.getProtocol(), is((byte) 0x01));
Jian Li115d8602016-08-15 20:21:53 +0900104 assertThat(appDataLcafAddress.getIpTos(), is(10));
105 assertThat(appDataLcafAddress.getLocalPortLow(), is((short) 1));
106 assertThat(appDataLcafAddress.getLocalPortHigh(), is((short) 255));
107 assertThat(appDataLcafAddress.getRemotePortLow(), is((short) 2));
108 assertThat(appDataLcafAddress.getRemotePortHigh(), is((short) 254));
Jian Lie9af3b42016-08-08 15:50:01 +0900109 assertThat(appDataLcafAddress.getAddress(), is(ipv4Address));
110 }
Jian Li76ea0572016-08-29 12:41:16 +0900111
112 @Test
113 public void testSerialization() throws LispWriterException, LispParseError, LispReaderException {
114 ByteBuf byteBuf = Unpooled.buffer();
115
116 AppDataLcafAddressWriter writer = new AppDataLcafAddressWriter();
117 writer.writeTo(byteBuf, address1);
118
119 AppDataLcafAddressReader reader = new AppDataLcafAddressReader();
120 LispAppDataLcafAddress deserialized = reader.readFrom(byteBuf);
121
122 new EqualsTester()
123 .addEqualityGroup(address1, deserialized).testEquals();
124 }
Jian Lie9af3b42016-08-08 15:50:01 +0900125}