blob: 7e3849a29928b34aabd5b6e10f9b82dd57dfd7c2 [file] [log] [blame]
Jian Li27759352016-10-04 20:14:42 +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;
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.protocols.DefaultLispInfoReply.DefaultInfoReplyBuilder;
28import org.onosproject.lisp.msg.protocols.DefaultLispInfoReply.InfoReplyReader;
29import org.onosproject.lisp.msg.protocols.DefaultLispInfoReply.InfoReplyWriter;
30import org.onosproject.lisp.msg.protocols.LispInfoReply.InfoReplyBuilder;
31import org.onosproject.lisp.msg.types.LispIpv4Address;
32import org.onosproject.lisp.msg.types.LispNatLcafAddress;
Jian Lief0f7232016-11-15 19:55:46 +090033import org.onosproject.lisp.msg.types.LispNatLcafAddress.NatAddressBuilder;
Jian Li27759352016-10-04 20:14:42 +090034
35import static org.hamcrest.MatcherAssert.assertThat;
36import static org.hamcrest.Matchers.is;
37
38/**
39 * Unit tests for DefaultLispInfoReply class.
40 */
41public final class DefaultLispInfoReplyTest {
42
43 private LispInfoReply reply1;
44 private LispInfoReply sameAsReply1;
45 private LispInfoReply reply2;
46
Jian Lid1a109e2016-11-12 09:00:42 +090047 private static final String AUTH_KEY = "onos";
48
Jian Li27759352016-10-04 20:14:42 +090049 @Before
50 public void setup() {
51
52 InfoReplyBuilder builder1 = new DefaultInfoReplyBuilder();
53
54 short msUdpPortNumber1 = 80;
55 short etrUdpPortNumber1 = 100;
56 LispIpv4Address globalEtrRlocAddress1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.1"));
57 LispIpv4Address msRlocAddress1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.2"));
58 LispIpv4Address privateEtrRlocAddress1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.3"));
59
60 LispIpv4Address address1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.4"));
61
Jian Lief0f7232016-11-15 19:55:46 +090062 LispNatLcafAddress natLcafAddress1 = new NatAddressBuilder()
63 .withLength((short) 0)
64 .withMsUdpPortNumber(msUdpPortNumber1)
65 .withEtrUdpPortNumber(etrUdpPortNumber1)
66 .withGlobalEtrRlocAddress(globalEtrRlocAddress1)
67 .withMsRlocAddress(msRlocAddress1)
68 .withPrivateEtrRlocAddress(privateEtrRlocAddress1)
69 .build();
Jian Li27759352016-10-04 20:14:42 +090070
71 reply1 = builder1
72 .withNonce(1L)
73 .withKeyId((short) 1)
Jian Lid1a109e2016-11-12 09:00:42 +090074 .withAuthKey(AUTH_KEY)
Jian Li27759352016-10-04 20:14:42 +090075 .withInfoReply(false)
76 .withMaskLength((byte) 1)
77 .withEidPrefix(address1)
78 .withNatLcafAddress(natLcafAddress1).build();
79
80 InfoReplyBuilder builder2 = new DefaultInfoReplyBuilder();
81
82 sameAsReply1 = builder2
83 .withNonce(1L)
84 .withKeyId((short) 1)
Jian Lid1a109e2016-11-12 09:00:42 +090085 .withAuthKey(AUTH_KEY)
Jian Li27759352016-10-04 20:14:42 +090086 .withInfoReply(false)
87 .withMaskLength((byte) 1)
88 .withEidPrefix(address1)
89 .withNatLcafAddress(natLcafAddress1).build();
90
91 InfoReplyBuilder builder3 = new DefaultInfoReplyBuilder();
92
93 short msUdpPortNumber2 = 81;
94 short etrUdpPortNumber2 = 101;
95 LispIpv4Address globalEtrRlocAddress2 = new LispIpv4Address(IpAddress.valueOf("192.168.2.1"));
96 LispIpv4Address msRlocAddress2 = new LispIpv4Address(IpAddress.valueOf("192.168.2.2"));
97 LispIpv4Address privateEtrRlocAddress2 = new LispIpv4Address(IpAddress.valueOf("192.168.2.3"));
98
99 LispIpv4Address address2 = new LispIpv4Address(IpAddress.valueOf("192.168.2.4"));
100
Jian Lief0f7232016-11-15 19:55:46 +0900101 LispNatLcafAddress natLcafAddress2 = new NatAddressBuilder()
102 .withLength((short) 0)
103 .withMsUdpPortNumber(msUdpPortNumber2)
104 .withEtrUdpPortNumber(etrUdpPortNumber2)
105 .withGlobalEtrRlocAddress(globalEtrRlocAddress2)
106 .withMsRlocAddress(msRlocAddress2)
107 .withPrivateEtrRlocAddress(privateEtrRlocAddress2)
108 .build();
Jian Li27759352016-10-04 20:14:42 +0900109
110 reply2 = builder3
111 .withNonce(2L)
112 .withKeyId((short) 2)
Jian Lid1a109e2016-11-12 09:00:42 +0900113 .withAuthKey(AUTH_KEY)
Jian Li27759352016-10-04 20:14:42 +0900114 .withInfoReply(true)
115 .withMaskLength((byte) 1)
116 .withEidPrefix(address2)
117 .withNatLcafAddress(natLcafAddress2).build();
118 }
119
120 @Test
121 public void testEquality() {
122 new EqualsTester()
123 .addEqualityGroup(reply1, sameAsReply1)
124 .addEqualityGroup(reply2).testEquals();
125 }
126
127 @Test
128 public void testConstruction() {
129 DefaultLispInfoReply reply = (DefaultLispInfoReply) reply1;
130
131 LispIpv4Address address = new LispIpv4Address(IpAddress.valueOf("192.168.1.4"));
132
133 short msUdpPortNumber1 = 80;
134 short etrUdpPortNumber1 = 100;
135 LispIpv4Address globalEtrRlocAddress1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.1"));
136 LispIpv4Address msRlocAddress1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.2"));
137 LispIpv4Address privateEtrRlocAddress1 = new LispIpv4Address(IpAddress.valueOf("192.168.1.3"));
138
Jian Lief0f7232016-11-15 19:55:46 +0900139 LispNatLcafAddress natLcafAddress = new NatAddressBuilder()
Jian Li27759352016-10-04 20:14:42 +0900140 .withLength((short) 0)
141 .withMsUdpPortNumber(msUdpPortNumber1)
142 .withEtrUdpPortNumber(etrUdpPortNumber1)
143 .withGlobalEtrRlocAddress(globalEtrRlocAddress1)
144 .withMsRlocAddress(msRlocAddress1)
145 .withPrivateEtrRlocAddress(privateEtrRlocAddress1)
146 .build();
147
Jian Lid1a109e2016-11-12 09:00:42 +0900148 assertThat(reply.isInfoReply(), is(false));
Jian Li27759352016-10-04 20:14:42 +0900149 assertThat(reply.getNonce(), is(1L));
150 assertThat(reply.getKeyId(), is((short) 1));
151 assertThat(reply.getMaskLength(), is((byte) 1));
152 assertThat(reply.getPrefix(), is(address));
153 assertThat(reply.getNatLcafAddress(), is(natLcafAddress));
154 }
155
156 @Test
157 public void testSerialization() throws LispReaderException, LispWriterException, LispParseError {
158 ByteBuf byteBuf = Unpooled.buffer();
159
160 InfoReplyWriter writer = new InfoReplyWriter();
161 writer.writeTo(byteBuf, reply1);
162
163 InfoReplyReader reader = new InfoReplyReader();
164 LispInfoReply deserialized = reader.readFrom(byteBuf);
165
166 new EqualsTester()
167 .addEqualityGroup(reply1, deserialized).testEquals();
168 }
169}