blob: f8584bdc599dbf9cc28bfb69de0093ca9ade440a [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
18import com.google.common.testing.EqualsTester;
19import io.netty.buffer.ByteBuf;
20import io.netty.buffer.Unpooled;
21import org.junit.Before;
22import org.junit.Test;
23import 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.LispNatLcafAddress.NatAddressBuilder;
28import org.onosproject.lisp.msg.types.LispNatLcafAddress.NatLcafAddressReader;
29import org.onosproject.lisp.msg.types.LispNatLcafAddress.NatLcafAddressWriter;
30
31import static org.hamcrest.MatcherAssert.assertThat;
32import static org.hamcrest.Matchers.is;
33
34/**
35 * Unit tests for LispNatLcafAddress class.
36 */
37public class LispNatLcafAddressTest {
38
39 private LispNatLcafAddress address1;
40 private LispNatLcafAddress sameAsAddress1;
41 private LispNatLcafAddress address2;
42
43 @Before
44 public void setup() {
45
46 NatAddressBuilder builder1 = new NatAddressBuilder();
47
48 short msUdpPortNumber1 = 80;
49 short etrUdpPortNumber1 = 100;
50 LispIpv4Address globalEtrRlocAddress1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.1"));
51 LispIpv4Address msRlocAddress1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.2"));
52 LispIpv4Address privateEtrRlocAddress1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.3"));
53
54 address1 = builder1
55 .withLength((short) 0)
56 .withMsUdpPortNumber(msUdpPortNumber1)
57 .withEtrUdpPortNumber(etrUdpPortNumber1)
58 .withGlobalEtrRlocAddress(globalEtrRlocAddress1)
59 .withMsRlocAddress(msRlocAddress1)
60 .withPrivateEtrRlocAddress(privateEtrRlocAddress1)
61 .build();
62
63 NatAddressBuilder builder2 = new NatAddressBuilder();
64
65 sameAsAddress1 = builder2
66 .withLength((short) 0)
67 .withMsUdpPortNumber(msUdpPortNumber1)
68 .withEtrUdpPortNumber(etrUdpPortNumber1)
69 .withGlobalEtrRlocAddress(globalEtrRlocAddress1)
70 .withMsRlocAddress(msRlocAddress1)
71 .withPrivateEtrRlocAddress(privateEtrRlocAddress1)
72 .build();
73
74 NatAddressBuilder builder3 = new NatAddressBuilder();
75
76 short msUdpPortNumber2 = 81;
77 short etrUdpPortNumber2 = 101;
78 LispIpv4Address globalEtrRlocAddress2 = new LispIpv4Address(IpAddress.valueOf("192.168.2.1"));
79 LispIpv4Address msRlocAddress2 = new LispIpv4Address(IpAddress.valueOf("192.168.2.2"));
80 LispIpv4Address privateEtrRlocAddress2 = new LispIpv4Address(IpAddress.valueOf("192.168.2.3"));
81
82 address2 = builder3
83 .withLength((short) 0)
84 .withMsUdpPortNumber(msUdpPortNumber2)
85 .withEtrUdpPortNumber(etrUdpPortNumber2)
86 .withGlobalEtrRlocAddress(globalEtrRlocAddress2)
87 .withMsRlocAddress(msRlocAddress2)
88 .withPrivateEtrRlocAddress(privateEtrRlocAddress2)
89 .build();
90 }
91
92 @Test
93 public void testEquality() {
94 new EqualsTester()
95 .addEqualityGroup(address1, sameAsAddress1)
96 .addEqualityGroup(address2).testEquals();
97 }
98
99 @Test
100 public void testConstruction() {
101 LispNatLcafAddress natLcafAddress = address1;
102
103 LispIpv4Address globalEtrRlocAddress1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.1"));
104 LispIpv4Address msRlocAddress1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.2"));
105 LispIpv4Address privateEtrRlocAddress1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.3"));
106
107 assertThat(natLcafAddress.getLength(), is((short) 0));
108 assertThat(natLcafAddress.getMsUdpPortNumber(), is((short) 80));
109 assertThat(natLcafAddress.getEtrUdpPortNumber(), is((short) 100));
110 assertThat(natLcafAddress.getGlobalEtrRlocAddress(), is(globalEtrRlocAddress1));
111 assertThat(natLcafAddress.getMsRlocAddress(), is(msRlocAddress1));
112 assertThat(natLcafAddress.getPrivateEtrRlocAddress(), is(privateEtrRlocAddress1));
113 }
114
115 @Test
116 public void testSerialization() throws LispWriterException, LispParseError, LispReaderException {
117 ByteBuf byteBuf = Unpooled.buffer();
118
119 NatLcafAddressWriter writer = new NatLcafAddressWriter();
120 writer.writeTo(byteBuf, address1);
121
122 NatLcafAddressReader reader = new NatLcafAddressReader();
123 LispNatLcafAddress deserialized = reader.readFrom(byteBuf);
124
125 new EqualsTester()
126 .addEqualityGroup(address1, deserialized).testEquals();
127 }
128}