blob: 06cf2a179471a5382246be20bb4c986daca41e73 [file] [log] [blame]
Yoonseon Hanca814bf2016-09-12 11:37:48 -07001/*
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 */
16
17package org.onosproject.lisp.msg.protocols;
18
19import com.google.common.collect.ImmutableList;
20import com.google.common.testing.EqualsTester;
21import io.netty.buffer.ByteBuf;
22import io.netty.buffer.Unpooled;
23import org.junit.Before;
24import org.junit.Test;
25import org.onlab.packet.DeserializationException;
26import org.onlab.packet.IP;
27import org.onlab.packet.IPv4;
28import org.onlab.packet.IpAddress;
29import org.onlab.packet.UDP;
30import org.onosproject.lisp.msg.exceptions.LispParseError;
31import org.onosproject.lisp.msg.exceptions.LispReaderException;
32import org.onosproject.lisp.msg.exceptions.LispWriterException;
33import org.onosproject.lisp.msg.types.LispIpv4Address;
34
35import java.util.List;
36
37import static org.hamcrest.MatcherAssert.assertThat;
38import static org.hamcrest.Matchers.is;
39
40/**
41 * Unit tests for DefaultLispEncapsulatedControl class.
42 */
43public final class DefaultLispEncapsulatedControlTest {
44
45 private static final String ECM1_SRC_IP = "192.168.1.1";
46 private static final String ECM1_DST_IP = "192.168.1.2";
47 private static final String ECM2_SRC_IP = "192.168.2.1";
48 private static final String ECM2_DST_IP = "192.168.2.2";
49 private static final String RECORD_EID = "1.1.1.1";
50
Jian Lid1a109e2016-11-12 09:00:42 +090051 private static final String AUTH_KEY = "onos";
52
Yoonseon Hanca814bf2016-09-12 11:37:48 -070053 private LispEncapsulatedControl ecm1;
54 private LispEncapsulatedControl sameAsEcm1;
55 private LispEncapsulatedControl ecm2;
56
57 @Before
58 public void setup() {
59
60 //Creates ecm1
61 LispEncapsulatedControl.EcmBuilder builder1 =
62 new DefaultLispEncapsulatedControl.DefaultEcmBuilder();
63
64 IP innerIp1 = new IPv4().setSourceAddress(ECM1_SRC_IP)
65 .setDestinationAddress(ECM1_DST_IP)
66 .setProtocol(IPv4.PROTOCOL_UDP).setVersion((byte) (4 & 0xf));
67 UDP innerUdp1 = new UDP().setSourcePort(1)
68 .setDestinationPort(2);
69
70 LispMapRegister.RegisterBuilder msgBuilder = new
71 DefaultLispMapRegister.DefaultRegisterBuilder();
72
73 List<LispMapRecord> records1 = ImmutableList.of(getMapRecord(),
74 getMapRecord());
75
76 LispMapRegister innerMsg1 = msgBuilder.withIsProxyMapReply(true)
77 .withIsWantMapNotify(false)
78 .withKeyId((short) 1)
Jian Lid1a109e2016-11-12 09:00:42 +090079 .withAuthKey(AUTH_KEY)
Yoonseon Hanca814bf2016-09-12 11:37:48 -070080 .withNonce(1L)
81 .withMapRecords(records1)
82 .build();
83
84 ecm1 = builder1.isSecurity(false)
85 .innerIpHeader(innerIp1)
86 .innerUdpHeader(innerUdp1)
87 .innerLispMessage(innerMsg1)
88 .build();
89
90 //Creates sameAsEcm1
91 LispEncapsulatedControl.EcmBuilder builder2 =
92 new DefaultLispEncapsulatedControl.DefaultEcmBuilder();
93
94 IP innerIp2 = new IPv4().setSourceAddress(ECM1_SRC_IP)
95 .setDestinationAddress(ECM1_DST_IP)
96 .setProtocol(IPv4.PROTOCOL_UDP);
97 UDP innerUdp2 = new UDP().setSourcePort(1)
98 .setDestinationPort(2);
99
100 LispMapRegister.RegisterBuilder msgBuilder2 =
101 new DefaultLispMapRegister.DefaultRegisterBuilder();
102
103 List<LispMapRecord> records2 = ImmutableList.of(getMapRecord(),
104 getMapRecord());
105
106 LispMapRegister innerMsg2 = msgBuilder2.withIsProxyMapReply(true)
107 .withIsWantMapNotify(false)
108 .withKeyId((short) 1)
Jian Lid1a109e2016-11-12 09:00:42 +0900109 .withAuthKey(AUTH_KEY)
Yoonseon Hanca814bf2016-09-12 11:37:48 -0700110 .withNonce(1L)
111 .withMapRecords(records2)
112 .build();
113
114 sameAsEcm1 = builder2.isSecurity(false)
115 .innerIpHeader(innerIp2)
116 .innerUdpHeader(innerUdp2)
117 .innerLispMessage(innerMsg2)
118 .build();
119
120 //Creates ecm2
121 LispEncapsulatedControl.EcmBuilder builder3 =
122 new DefaultLispEncapsulatedControl.DefaultEcmBuilder();
123
124 IP innerIp3 = new IPv4().setSourceAddress(ECM2_SRC_IP)
125 .setDestinationAddress(ECM2_DST_IP)
126 .setProtocol(IPv4.PROTOCOL_UDP);
127 UDP innerUdp3 = new UDP().setSourcePort(10)
128 .setDestinationPort(20);
129
130 LispMapRegister.RegisterBuilder msgBuilder3 =
131 new DefaultLispMapRegister.DefaultRegisterBuilder();
132
133 List<LispMapRecord> records3 = ImmutableList.of(getMapRecord(),
134 getMapRecord());
135
136 LispMapRegister innerMsg3 = msgBuilder3.withIsProxyMapReply(true)
137 .withIsWantMapNotify(false)
138 .withKeyId((short) 2)
Jian Lid1a109e2016-11-12 09:00:42 +0900139 .withAuthKey(AUTH_KEY)
Yoonseon Hanca814bf2016-09-12 11:37:48 -0700140 .withNonce(1L)
141 .withMapRecords(records3)
142 .build();
143
144 ecm2 = builder3.isSecurity(false)
145 .innerIpHeader(innerIp3)
146 .innerUdpHeader(innerUdp3)
147 .innerLispMessage(innerMsg3)
148 .build();
149 }
150
151 @Test
152 public void testEquality() {
153 new EqualsTester()
154 .addEqualityGroup(ecm1, sameAsEcm1)
155 .addEqualityGroup(ecm2).testEquals();
156 }
157
158 @Test
159 public void testConstruction() {
160 DefaultLispEncapsulatedControl ecm =
161 (DefaultLispEncapsulatedControl) ecm1;
162
163 assertThat("Inner Ip versions are not match",
164 ecm.innerIpHeader().getVersion(), is((byte) 4));
165 assertThat("Inner Ip protocols are not match",
166 ((IPv4) ecm.innerIpHeader()).getProtocol(),
167 is(IPv4.PROTOCOL_UDP));
168 assertThat("Inner IP source addresses are not match",
169 ((IPv4) ecm.innerIpHeader()).getSourceAddress(),
170 is(IPv4.toIPv4Address(ECM1_SRC_IP)));
171 assertThat("Inner IP destination addresses are not match",
172 ((IPv4) ecm.innerIpHeader()).getDestinationAddress(),
173 is(IPv4.toIPv4Address(ECM1_DST_IP)));
174 assertThat("Inner UDP source ports are not match",
175 ecm.innerUdp().getSourcePort(), is(1));
176 assertThat("Inner UDP destination ports are not match",
177 ecm.innerUdp().getDestinationPort(), is(2));
178 assertThat("Inner LISP control messages are not match",
179 ecm.getControlMessage().getType(),
180 is(LispType.LISP_MAP_REGISTER));
181 }
182
183 @Test
184 public void testSerialization() throws LispReaderException,
185 LispWriterException, LispParseError, DeserializationException {
186
187 ByteBuf byteBuf = Unpooled.buffer();
188 DefaultLispEncapsulatedControl.EcmWriter writer =
189 new DefaultLispEncapsulatedControl.EcmWriter();
190 writer.writeTo(byteBuf, ecm1);
191
192 DefaultLispEncapsulatedControl.EcmReader reader =
193 new DefaultLispEncapsulatedControl.EcmReader();
194
195 LispEncapsulatedControl deserialized = reader.readFrom(byteBuf);
196
197 new EqualsTester()
198 .addEqualityGroup(ecm1, deserialized).testEquals();
199 }
200
201 private LispMapRecord getMapRecord() {
202 LispMapRecord.MapRecordBuilder builder1 =
203 new DefaultLispMapRecord.DefaultMapRecordBuilder();
204
205 LispIpv4Address ipv4Locator1 =
206 new LispIpv4Address(IpAddress.valueOf(RECORD_EID));
207
208 return builder1
209 .withRecordTtl(100)
210 .withAuthoritative(true)
211 .withMapVersionNumber((short) 1)
212 .withMaskLength((byte) 0x01)
213 .withAction(LispMapReplyAction.NativelyForward)
214 .withEidPrefixAfi(ipv4Locator1)
215 .build();
216 }
217}