blob: 2fb6b259c95bf2f53fe151268392b1e127a2ab97 [file] [log] [blame]
Jian Li52015762016-10-04 17:51:16 +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
Jian Lief0f7232016-11-15 19:55:46 +090018import com.google.common.collect.ImmutableList;
Jian Li52015762016-10-04 17:51:16 +090019import com.google.common.testing.EqualsTester;
20import io.netty.buffer.ByteBuf;
21import io.netty.buffer.Unpooled;
22import org.junit.Before;
23import org.junit.Test;
24import org.onlab.packet.IpAddress;
25import org.onosproject.lisp.msg.exceptions.LispParseError;
26import org.onosproject.lisp.msg.exceptions.LispReaderException;
27import org.onosproject.lisp.msg.exceptions.LispWriterException;
28import org.onosproject.lisp.msg.types.LispNatLcafAddress.NatAddressBuilder;
29import org.onosproject.lisp.msg.types.LispNatLcafAddress.NatLcafAddressReader;
30import org.onosproject.lisp.msg.types.LispNatLcafAddress.NatLcafAddressWriter;
31
Jian Lief0f7232016-11-15 19:55:46 +090032import java.util.List;
33
Jian Li52015762016-10-04 17:51:16 +090034import static org.hamcrest.MatcherAssert.assertThat;
35import static org.hamcrest.Matchers.is;
36
37/**
38 * Unit tests for LispNatLcafAddress class.
39 */
40public class LispNatLcafAddressTest {
41
42 private LispNatLcafAddress address1;
43 private LispNatLcafAddress sameAsAddress1;
44 private LispNatLcafAddress address2;
45
46 @Before
47 public void setup() {
48
49 NatAddressBuilder builder1 = new NatAddressBuilder();
50
51 short msUdpPortNumber1 = 80;
52 short etrUdpPortNumber1 = 100;
53 LispIpv4Address globalEtrRlocAddress1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.1"));
54 LispIpv4Address msRlocAddress1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.2"));
55 LispIpv4Address privateEtrRlocAddress1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.3"));
56
Jian Lief0f7232016-11-15 19:55:46 +090057 LispIpv4Address rtrRloc11 = new LispIpv4Address(IpAddress.valueOf("10.1.1.1"));
58 LispIpv4Address rtrRloc12 = new LispIpv4Address(IpAddress.valueOf("10.1.1.2"));
59 List<LispAfiAddress> rtrRlocs1 = ImmutableList.of(rtrRloc11, rtrRloc12);
60
Jian Li52015762016-10-04 17:51:16 +090061 address1 = builder1
62 .withLength((short) 0)
63 .withMsUdpPortNumber(msUdpPortNumber1)
64 .withEtrUdpPortNumber(etrUdpPortNumber1)
65 .withGlobalEtrRlocAddress(globalEtrRlocAddress1)
66 .withMsRlocAddress(msRlocAddress1)
67 .withPrivateEtrRlocAddress(privateEtrRlocAddress1)
Jian Lief0f7232016-11-15 19:55:46 +090068 .withRtrRlocAddresses(rtrRlocs1)
Jian Li52015762016-10-04 17:51:16 +090069 .build();
70
71 NatAddressBuilder builder2 = new NatAddressBuilder();
72
73 sameAsAddress1 = builder2
74 .withLength((short) 0)
75 .withMsUdpPortNumber(msUdpPortNumber1)
76 .withEtrUdpPortNumber(etrUdpPortNumber1)
77 .withGlobalEtrRlocAddress(globalEtrRlocAddress1)
78 .withMsRlocAddress(msRlocAddress1)
79 .withPrivateEtrRlocAddress(privateEtrRlocAddress1)
Jian Lief0f7232016-11-15 19:55:46 +090080 .withRtrRlocAddresses(rtrRlocs1)
Jian Li52015762016-10-04 17:51:16 +090081 .build();
82
83 NatAddressBuilder builder3 = new NatAddressBuilder();
84
85 short msUdpPortNumber2 = 81;
86 short etrUdpPortNumber2 = 101;
87 LispIpv4Address globalEtrRlocAddress2 = new LispIpv4Address(IpAddress.valueOf("192.168.2.1"));
88 LispIpv4Address msRlocAddress2 = new LispIpv4Address(IpAddress.valueOf("192.168.2.2"));
89 LispIpv4Address privateEtrRlocAddress2 = new LispIpv4Address(IpAddress.valueOf("192.168.2.3"));
90
Jian Lief0f7232016-11-15 19:55:46 +090091 LispIpv4Address rtrRloc21 = new LispIpv4Address(IpAddress.valueOf("10.1.2.1"));
92 LispIpv4Address rtrRloc22 = new LispIpv4Address(IpAddress.valueOf("10.1.2.2"));
93 List<LispAfiAddress> rtrRlocs2 = ImmutableList.of(rtrRloc21, rtrRloc22);
94
Jian Li52015762016-10-04 17:51:16 +090095 address2 = builder3
96 .withLength((short) 0)
97 .withMsUdpPortNumber(msUdpPortNumber2)
98 .withEtrUdpPortNumber(etrUdpPortNumber2)
99 .withGlobalEtrRlocAddress(globalEtrRlocAddress2)
100 .withMsRlocAddress(msRlocAddress2)
101 .withPrivateEtrRlocAddress(privateEtrRlocAddress2)
Jian Lief0f7232016-11-15 19:55:46 +0900102 .withRtrRlocAddresses(rtrRlocs2)
Jian Li52015762016-10-04 17:51:16 +0900103 .build();
104 }
105
106 @Test
107 public void testEquality() {
108 new EqualsTester()
109 .addEqualityGroup(address1, sameAsAddress1)
110 .addEqualityGroup(address2).testEquals();
111 }
112
113 @Test
114 public void testConstruction() {
115 LispNatLcafAddress natLcafAddress = address1;
116
117 LispIpv4Address globalEtrRlocAddress1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.1"));
118 LispIpv4Address msRlocAddress1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.2"));
119 LispIpv4Address privateEtrRlocAddress1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.3"));
120
121 assertThat(natLcafAddress.getLength(), is((short) 0));
122 assertThat(natLcafAddress.getMsUdpPortNumber(), is((short) 80));
123 assertThat(natLcafAddress.getEtrUdpPortNumber(), is((short) 100));
124 assertThat(natLcafAddress.getGlobalEtrRlocAddress(), is(globalEtrRlocAddress1));
125 assertThat(natLcafAddress.getMsRlocAddress(), is(msRlocAddress1));
126 assertThat(natLcafAddress.getPrivateEtrRlocAddress(), is(privateEtrRlocAddress1));
127 }
128
129 @Test
130 public void testSerialization() throws LispWriterException, LispParseError, LispReaderException {
131 ByteBuf byteBuf = Unpooled.buffer();
132
133 NatLcafAddressWriter writer = new NatLcafAddressWriter();
134 writer.writeTo(byteBuf, address1);
135
136 NatLcafAddressReader reader = new NatLcafAddressReader();
137 LispNatLcafAddress deserialized = reader.readFrom(byteBuf);
138
139 new EqualsTester()
140 .addEqualityGroup(address1, deserialized).testEquals();
141 }
142}