2 * Copyright (c) 2014 Contextream, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.lispflowmapping.southbound.lisp;
11 import static junit.framework.Assert.assertEquals;
12 import static org.junit.Assert.*;
14 import java.net.DatagramPacket;
15 import java.nio.ByteBuffer;
16 import java.util.ArrayList;
17 import java.util.Arrays;
18 import java.util.List;
20 import junitx.framework.ArrayAssert;
21 import junitx.framework.Assert;
23 import org.apache.commons.lang3.ArrayUtils;
24 import org.jmock.api.Invocation;
25 import org.junit.Before;
26 import org.junit.Ignore;
27 import org.junit.Test;
28 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
29 import org.opendaylight.lispflowmapping.implementation.serializer.LispMessage;
30 import org.opendaylight.lispflowmapping.implementation.serializer.LispMessageEnum;
31 import org.opendaylight.lispflowmapping.implementation.serializer.MapNotifySerializer;
32 import org.opendaylight.lispflowmapping.implementation.serializer.MapReplySerializer;
33 import org.opendaylight.lispflowmapping.implementation.util.ByteUtil;
34 import org.opendaylight.lispflowmapping.implementation.util.LispAFIConvertor;
35 import org.opendaylight.lispflowmapping.implementation.util.MapNotifyBuilderHelper;
36 import org.opendaylight.lispflowmapping.southbound.lisp.exception.LispMalformedPacketException;
37 import org.opendaylight.lispflowmapping.tools.junit.BaseTestCase;
38 import org.opendaylight.lispflowmapping.type.AddressFamilyNumberEnum;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.AddMapping;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.EidToLocatorRecord.Action;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.LispAFIAddress;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.MapRegister;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.MapRequest;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.RequestMapping;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.eidrecords.EidRecord;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.eidtolocatorrecords.EidToLocatorRecord;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.eidtolocatorrecords.EidToLocatorRecordBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.lispaddress.LispAddressContainer;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.lispaddress.lispaddresscontainer.address.ipv4.Ipv4AddressBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.lispaddress.lispaddresscontainer.address.ipv6.Ipv6AddressBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.locatorrecords.LocatorRecord;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.locatorrecords.LocatorRecordBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.mapnotifymessage.MapNotifyBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.mapreplymessage.MapReplyBuilder;
55 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
56 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
57 import org.opendaylight.yangtools.yang.binding.Notification;
59 public class LispSouthboundServiceTest extends BaseTestCase {
61 private LispSouthboundService testedLispService;
62 private NotificationProviderService nps;
63 private byte[] mapRequestPacket;
64 private byte[] mapRegisterPacket;
65 private ValueSaverAction<Notification> lispNotificationSaver;
66 // private ValueSaverAction<MapRegister> mapRegisterSaver;
67 // private ValueSaverAction<MapRequest> mapRequestSaver;
68 private MapNotifyBuilder mapNotifyBuilder;
69 private MapReplyBuilder mapReplyBuilder;
70 private EidToLocatorRecordBuilder eidToLocatorBuilder;
72 private interface MapReplyIpv4SingleLocatorPos {
75 int LOCATOR_COUNT = 16;
76 int EID_MASK_LEN = 17;
80 int LOCATOR_RBIT = 33;
84 private interface MapReplyIpv4SecondLocatorPos {
85 int FIRST_LOCATOR_IPV4_LENGTH = 12;
86 int LOC_AFI = MapReplyIpv4SingleLocatorPos.LOC_AFI + FIRST_LOCATOR_IPV4_LENGTH;
87 int LOCATOR_RBIT = MapReplyIpv4SingleLocatorPos.LOCATOR_RBIT + FIRST_LOCATOR_IPV4_LENGTH;
88 int LOCATOR = MapReplyIpv4SingleLocatorPos.LOCATOR + FIRST_LOCATOR_IPV4_LENGTH;
93 public void before() throws Exception {
95 // mapResolver = context.mock(IMapResolver.class);
96 // mapServer = context.mock(IMapServer.class);
97 testedLispService = new LispSouthboundService();
98 nps = context.mock(NotificationProviderService.class);
99 testedLispService.setNotificationProvider(nps);
100 lispNotificationSaver = new ValueSaverAction<Notification>();
101 // mapRegisterSaver = new ValueSaverAction<MapRegister>();
102 // mapRequestSaver = new ValueSaverAction<MapRequest>();
103 // SRC: 127.0.0.1:58560 to 127.0.0.1:4342
104 // LISP(Type = 8 - Encapsulated)
105 // IP: 192.168.136.10 -> 1.2.3.4
107 // LISP(Type = 1 Map-Request
111 // Source EID not present
112 // Nonce: 0x3d8d2acd39c8d608
113 // ITR-RLOC AFI=1 Address=192.168.136.10
114 // Record 1: 1.2.3.4/32
115 mapRequestPacket = extractWSUdpByteArray(new String("0000 00 00 00 00 00 00 00 00 00 00 00 00 08 00 45 00 " //
116 + "0010 00 58 00 00 40 00 40 11 3c 93 7f 00 00 01 7f 00 "
117 + "0020 00 01 e4 c0 10 f6 00 44 fe 57 80 00 00 00 45 00 "
118 + "0030 00 38 d4 31 00 00 ff 11 56 f3 c0 a8 88 0a 01 02 "
119 + "0040 03 04 dd b4 10 f6 00 24 ef 3a 10 00 00 01 3d 8d "
120 + "0050 2a cd 39 c8 d6 08 00 01 01 02 03 04 00 01 c0 a8 88 0a 00 20 " //
121 + "0060 00 01 01 02 03 04"));
122 mapReplyBuilder = new MapReplyBuilder();
123 mapReplyBuilder.setEidToLocatorRecord(new ArrayList<EidToLocatorRecord>());
124 mapReplyBuilder.setNonce((long) 0);
125 mapReplyBuilder.setEchoNonceEnabled(false);
126 mapReplyBuilder.setProbe(true);
127 mapReplyBuilder.setSecurityEnabled(true);
128 eidToLocatorBuilder = new EidToLocatorRecordBuilder();
129 String ip = "0.0.0.0";
130 eidToLocatorBuilder.setLispAddressContainer(getIPContainer(ip));
131 eidToLocatorBuilder.setLocatorRecord(new ArrayList<LocatorRecord>());
132 eidToLocatorBuilder.setRecordTtl(10);
133 eidToLocatorBuilder.setMapVersion((short) 0);
134 eidToLocatorBuilder.setMaskLength((short) 0);
135 eidToLocatorBuilder.setAction(Action.NativelyForward);
136 eidToLocatorBuilder.setAuthoritative(false);
137 // eidToLocatorBuilder.setPrefix(new LispIpv4Address(0));
138 // mapReply.addEidToLocator(eidToLocatorBuilder);
140 // IP: 192.168.136.10 -> 128.223.156.35
141 // UDP: 49289 -> 4342
142 // LISP(Type = 3 Map-Register, P=1, M=1
146 // AuthDataLength: 20 Data:
147 // e8:f5:0b:c5:c5:f2:b0:21:27:a8:21:41:04:f3:46:5a:a5:68:89:ec
148 // EID prefix: 153.16.254.1/32 (EID=0x9910FE01), TTL: 10, Authoritative,
150 // Local RLOC: 192.168.136.10 (RLOC=0xC0A8880A), Reachable,
151 // Priority/Weight: 1/100, Multicast Priority/Weight:
155 mapRegisterPacket = extractWSUdpByteArray(new String("0000 00 50 56 ee d1 4f 00 0c 29 7a ce 79 08 00 45 00 " //
156 + "0010 00 5c 00 00 40 00 40 11 d4 db c0 a8 88 0a 80 df "
157 + "0020 9c 23 d6 40 10 f6 00 48 59 a4 38 00 01 01 00 00 "
158 + "0030 00 00 00 00 00 00 00 01 00 14 0e a4 c6 d8 a4 06 "
159 + "0040 71 7c 33 a4 5c 4a 83 1c de 74 53 03 0c ad 00 00 "
160 + "0050 00 0a 01 20 10 00 00 00 00 01 99 10 fe 01 01 64 " //
161 + "0060 ff 00 00 05 00 01 c0 a8 88 0a"));
162 mapNotifyBuilder = new MapNotifyBuilder();
163 mapNotifyBuilder.setAuthenticationData(new byte[0]);
166 private LispAddressContainer getIPContainer(String ip) {
167 return LispAFIConvertor.toContainer(getIP(ip));
170 private org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.lispaddress.lispaddresscontainer.address.ipv4.Ipv4Address getIP(String ip) {
171 return new Ipv4AddressBuilder().setIpv4Address(new Ipv4Address(ip)).setAfi((short) 1).build();
174 private org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.lispaddress.lispaddresscontainer.address.ipv6.Ipv6Address getIPV6(String ip) {
175 return new Ipv6AddressBuilder().setIpv6Address(new Ipv6Address(ip)).setAfi((short) 2).build();
180 public void todos() throws Exception {
182 // TODO: MapRequest: usage of Map-Reply-Record in MapRequest packet.
183 // TODO: Non-Encapsulated packets
186 @Test(expected = LispMalformedPacketException.class)
187 public void mapRegister__IllegalPacket() throws Exception {
188 mapRegisterPacket = extractWSUdpByteArray(new String("0000 00 0c 29 7a ce 8d 00 0c 29 e4 ef 70 08 00 45 00 "
189 + "0010 00 68 00 00 40 00 40 11 26 15 0a 01 00 6e 0a 01 " //
190 + "0020 00 01 10 f6 10 f6 00 54 03 3b 38 00 01 01 00 00 "));
192 handleMapRegisterPacket(mapRegisterPacket);
195 @Test(expected = LispMalformedPacketException.class)
196 public void mapRequest__IllegalPacket() throws Exception {
197 mapRequestPacket = extractWSUdpByteArray(new String("0000 00 00 00 00 00 00 00 00 00 00 00 00 08 00 45 00 " //
198 + "0010 00 58 00 00 40 00 40 11 3c 93 7f 00 00 01 7f 00 "
199 + "0020 00 01 e4 c0 10 f6 00 44 fe 57 80 00 00 00 45 00 "
200 + "0030 00 38 d4 31 00 00 ff 11 56 f3 c0 a8 88 0a 01 02 " //
201 + "0040 03 04 dd b4 10 f6 00 24 ef 3a 10 00 00 01 3d 8d "));
202 handleMapRequestPacket(mapRequestPacket);
205 @Test(expected = LispMalformedPacketException.class)
206 public void mapRequest__IllegalEncapsulatedPacket() throws Exception {
207 mapRequestPacket = extractWSUdpByteArray(new String("0000 00 00 00 00 00 00 00 00 00 00 00 00 08 00 45 00 " //
208 + "0010 00 58 00 00 40 00 40 11 3c 93 7f 00 00 01 7f 00 " //
209 + "0020 00 01 e4 c0 10 f6 00 44 fe 57 80 00 00 00 45 00 "));
210 handleMapRequestPacket(mapRequestPacket);
213 private MapRegister lastMapRegister() {
214 assertTrue(lispNotificationSaver.lastValue instanceof AddMapping);
215 AddMapping lastValue = (AddMapping) lispNotificationSaver.lastValue;
216 return lastValue.getMapRegister();
219 private MapRequest lastMapRequest() {
220 RequestMapping lastValue = (RequestMapping) lispNotificationSaver.lastValue;
221 return lastValue.getMapRequest();
225 public void mapRegister__TwoRlocs() throws Exception {
227 // EID prefix: 172.1.1.2/32, TTL: 10, Authoritative, No-Action
228 // Local RLOC: 10.1.0.110, Reachable, Priority/Weight: 1/100, Multicast
229 // Priority/Weight: 255/0
230 // Local RLOC: 192.168.136.51, Reachable, Priority/Weight: 6/100,
231 // Multicast Priority/Weight: 255/0
232 mapRegisterPacket = extractWSUdpByteArray(new String("0000 00 0c 29 7a ce 8d 00 0c 29 e4 ef 70 08 00 45 00 "
233 + "0010 00 68 00 00 40 00 40 11 26 15 0a 01 00 6e 0a 01 " //
234 + "0020 00 01 10 f6 10 f6 00 54 03 3b 38 00 01 01 00 00 " //
235 + "0030 00 00 00 00 00 00 00 01 00 14 ae d8 7b d4 9c 59 " //
236 + "0040 e9 35 75 6e f1 29 27 a3 45 20 96 06 c2 e1 00 00 " //
237 + "0050 00 0a 02 20 10 00 00 00 00 01 ac 01 01 02 01 64 " //
238 + "0060 ff 00 00 05 00 01 0a 01 00 6e 06 64 ff 00 00 05 " //
239 + "0070 00 01 c0 a8 88 33"));
241 oneOf(nps).publish(with(lispNotificationSaver));
243 handleMapRegisterPacket(mapRegisterPacket);
245 List<EidToLocatorRecord> eidRecords = lastMapRegister().getEidToLocatorRecord();
246 assertEquals(1, eidRecords.size());
247 EidToLocatorRecord eidRecord = eidRecords.get(0);
248 assertEquals(2, eidRecord.getLocatorRecord().size());
249 assertEquals(getIPContainer("10.1.0.110"), eidRecord.getLocatorRecord().get(0).getLispAddressContainer());
250 assertEquals(getIPContainer("192.168.136.51"), eidRecord.getLocatorRecord().get(1).getLispAddressContainer());
254 public void mapRegister__Ipv6Rloc() throws Exception {
255 // P bit (Proxy-Map-Reply): Set
256 // M bit (Want-Map-Notify): Set
261 // Authentication Data: 5bc4d44a57e2a55d577a6f89779c004f5da713fb
262 // EID prefix: 2610:d0:ffff:192::1/128, TTL: 10, Authoritative,
264 // Local RLOC: 10.0.58.156, Reachable, Priority/Weight: 1/100, Multicast
265 // Priority/Weight: 255/0
267 mapRegisterPacket = extractWSUdpByteArray(new String("0000 00 0c 29 34 3e 1b 00 0c 29 f6 d6 0d 08 00 45 00 " //
268 + "0010 00 68 00 00 40 00 40 11 ea c3 0a 00 3a 9c 0a 00 " //
269 + "0020 01 26 10 f6 10 f6 00 54 f5 9a 38 00 03 01 00 00 " //
270 + "0030 00 00 00 00 00 00 00 01 00 14 22 97 ff 61 ec d8 " //
271 + "0040 0f 91 c6 c4 01 ef 7f bb 77 58 39 5c 92 23 00 00 " //
272 + "0050 00 0a 01 80 10 00 00 00 00 02 26 10 00 d0 ff ff " //
273 + "0060 01 92 00 00 00 00 00 00 00 01 01 64 ff 00 00 05 " //
274 + "0070 00 01 0a 00 3a 9c"));
276 oneOf(nps).publish(with(lispNotificationSaver));
278 handleMapRegisterPacket(mapRegisterPacket);
280 EidToLocatorRecord eidToLocatorRecord = lastMapRegister().getEidToLocatorRecord().get(0);
281 assertEquals(getIPV6("2610:d0:ffff:192:0:0:0:1"), LispAFIConvertor.toAFI(eidToLocatorRecord.getLispAddressContainer()));
282 assertEquals(AddressFamilyNumberEnum.IP6,
283 AddressFamilyNumberEnum.valueOf(LispAFIConvertor.toAFI(eidToLocatorRecord.getLispAddressContainer()).getAfi()));
285 assertEquals(getIP("10.0.58.156"), LispAFIConvertor.toAFI(eidToLocatorRecord.getLocatorRecord().get(0).getLispAddressContainer()));
289 public void mapRegister__VerifyBasicFields() throws Exception {
290 oneOf(nps).publish(with(lispNotificationSaver));
291 handleMapRegisterPacket(mapRegisterPacket);
293 EidToLocatorRecord eidToLocator = lastMapRegister().getEidToLocatorRecord().get(0);
294 assertEquals(getIP("153.16.254.1"), LispAFIConvertor.toAFI(eidToLocator.getLispAddressContainer()));
296 assertEquals(1, eidToLocator.getLocatorRecord().size());
297 assertEquals(getIP("192.168.136.10"), LispAFIConvertor.toAFI(eidToLocator.getLocatorRecord().get(0).getLispAddressContainer()));
302 public void mapRegister__NoResponseFromMapServerShouldReturnNullPacket() throws Exception {
303 oneOf(nps).publish(with(lispNotificationSaver));
304 mapNotifyBuilder = null;
306 assertNull(handleMapRegisterPacket(mapRegisterPacket));
310 public void mapRegister__NonSetMBit() throws Exception {
311 byte[] registerWithNonSetMBit = extractWSUdpByteArray(new String("0000 00 50 56 ee d1 4f 00 0c 29 7a ce 79 08 00 45 00 " //
312 + "0010 00 5c 00 00 40 00 40 11 d4 db c0 a8 88 0a 80 df " //
313 + "0020 9c 23 d6 40 10 f6 00 48 59 a4 38 00 00 01 00 00 "
314 + "0030 00 00 00 00 00 00 00 01 00 14 79 d1 44 66 19 99 "
315 + "0040 83 63 a7 79 6e f0 40 97 54 26 3a 44 b4 eb 00 00 " //
316 + "0050 00 0a 01 20 10 00 00 00 00 01 99 10 fe 01 01 64 " //
317 + "0060 ff 00 00 05 00 01 c0 a8 88 0a"));
318 stubMapRegister(true);
320 handleMapRegisterPacket(registerWithNonSetMBit);
322 assertFalse(lastMapRegister().isWantMapNotify());
326 public void mapRegister__NonSetMBitWithNonZeroReservedBits() throws Exception {
327 byte[] registerWithNonSetMBit = extractWSUdpByteArray(new String("0000 00 50 56 ee d1 4f 00 0c 29 7a ce 79 08 00 45 00 " //
328 + "0010 00 5c 00 00 40 00 40 11 d4 db c0 a8 88 0a 80 df "
329 + "0020 9c 23 d6 40 10 f6 00 48 59 a4 38 00 02 01 00 00 "
330 + "0030 00 00 00 00 00 00 00 01 00 14 c0 c7 c5 2f 57 f6 "
331 + "0040 e7 20 25 3d e8 b2 07 e2 63 de 62 2b 7a 20 00 00 "
332 + "0050 00 0a 01 20 10 00 00 00 00 01 99 10 fe 01 01 64 " //
333 + "0060 ff 00 00 05 00 01 c0 a8 88 0a"));
334 stubMapRegister(true);
336 handleMapRegisterPacket(registerWithNonSetMBit);
337 assertFalse(lastMapRegister().isWantMapNotify());
341 public void mapRegister__SetMBitWithNonZeroReservedBits() throws Exception {
342 byte[] registerWithNonSetMBit = extractWSUdpByteArray(new String("0000 00 50 56 ee d1 4f 00 0c 29 7a ce 79 08 00 45 00 " //
343 + "0010 00 5c 00 00 40 00 40 11 d4 db c0 a8 88 0a 80 df "
344 + "0020 9c 23 d6 40 10 f6 00 48 59 a4 38 00 03 01 00 00 "
345 + "0030 00 00 00 00 00 00 00 01 00 14 a2 72 40 7b 1a ae "
346 + "0040 4e 6b e2 e5 e1 01 40 8a c9 e1 d1 80 cb 72 00 00 "
347 + "0050 00 0a 01 20 10 00 00 00 00 01 99 10 fe 01 01 64 " //
348 + "0060 ff 00 00 05 00 01 c0 a8 88 0a"));
349 stubMapRegister(true);
351 handleMapRegisterPacket(registerWithNonSetMBit);
352 assertTrue(lastMapRegister().isWantMapNotify());
357 public void mapRegisterAndNotify__ValidExtraDataParsedSuccessfully() throws Exception {
358 byte[] extraDataPacket = new byte[mapRegisterPacket.length + 3];
359 extraDataPacket[mapRegisterPacket.length] = 0x9;
360 System.arraycopy(mapRegisterPacket, 0, extraDataPacket, 0, mapRegisterPacket.length);
361 stubMapRegister(true);
363 DatagramPacket dp = new DatagramPacket(extraDataPacket, extraDataPacket.length);
364 dp.setLength(mapRegisterPacket.length);
365 testedLispService.handlePacket(dp);
366 // Check map register fields.
368 // byte[] notifyResult = testedLispService.handlePacket(dp).getData();
369 byte[] notifyResult = lastMapNotifyPacket().getData();
370 assertEquals(mapRegisterPacket.length, notifyResult.length);
374 private DatagramPacket lastMapReplyPacket() {
375 ByteBuffer serialize = MapReplySerializer.getInstance().serialize(mapReplyBuilder.build());
376 return new DatagramPacket(serialize.array(), serialize.array().length);
379 private DatagramPacket lastMapNotifyPacket() {
380 if (mapNotifyBuilder.getEidToLocatorRecord() == null) {
381 mapNotifyBuilder.setEidToLocatorRecord(new ArrayList<EidToLocatorRecord>());
383 mapNotifyBuilder.getEidToLocatorRecord().add(eidToLocatorBuilder.build());
384 mapNotifyBuilder.setNonce((long) 0);
385 mapNotifyBuilder.setKeyId((short) 0);
386 mapNotifyBuilder.setAuthenticationData(new byte[0]);
387 ByteBuffer serialize = MapNotifySerializer.getInstance().serialize(mapNotifyBuilder.build());
388 return new DatagramPacket(serialize.array(), serialize.array().length);
393 public void mapNotify__VerifyBasicFields() throws Exception {
394 byte registerType = mapRegisterPacket[0];
395 assertEquals(LispMessageEnum.MapRegister.getValue(), registerType >> 4);
397 stubMapRegister(true);
399 byte[] result = handleMapRegisterAsByteArray(mapRegisterPacket);
401 assertEquals(mapRegisterPacket.length, result.length);
403 byte expectedType = (byte) (LispMessageEnum.MapNotify.getValue() << 4);
404 assertHexEquals(expectedType, result[0]);
405 assertHexEquals((byte) 0x00, result[1]);
406 assertHexEquals((byte) 0x00, result[2]);
408 byte[] registerWithoutTypeWithoutAuthenticationData = ArrayUtils.addAll(Arrays.copyOfRange(mapRegisterPacket, 3, 16),
409 Arrays.copyOfRange(mapRegisterPacket, 36, mapRegisterPacket.length));
410 byte[] notifyWithoutTypeWithOutAuthenticationData = ArrayUtils.addAll(Arrays.copyOfRange(result, 3, 16),
411 Arrays.copyOfRange(result, 36, result.length));
412 ArrayAssert.assertEquals(registerWithoutTypeWithoutAuthenticationData, notifyWithoutTypeWithOutAuthenticationData);
417 public void mapNotify__VerifyPort() throws Exception {
418 stubMapRegister(true);
420 DatagramPacket notifyPacket = handleMapRegisterPacket(mapRegisterPacket);
421 assertEquals(LispMessage.PORT_NUM, notifyPacket.getPort());
425 public void mapRequest__VerifyBasicFields() throws Exception {
426 oneOf(nps).publish(with(lispNotificationSaver));
427 handleMapRequestAsByteArray(mapRequestPacket);
428 List<EidRecord> eids = lastMapRequest().getEidRecord();
429 assertEquals(1, eids.size());
430 LispAFIAddress lispAddress = LispAFIConvertor.toAFI(eids.get(0).getLispAddressContainer());
431 assertTrue(lispAddress instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.lispaddress.lispaddresscontainer.address.ipv4.Ipv4Address);
432 assertEquals(getIP("1.2.3.4"), ((org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.lispaddress.lispaddresscontainer.address.ipv4.Ipv4Address) lispAddress));
433 assertEquals((byte) 0x20, eids.get(0).getMask().byteValue());
434 assertEquals(0x3d8d2acd39c8d608L, lastMapRequest().getNonce().longValue());
435 // assertEquals(AddressFamilyNumberEnum.RESERVED,
436 // AddressFamilyNumberEnum.valueOf(LispAFIToContainerConvertorFactory.toAFI(
437 // lastMapRequest().getSourceEid().getLispAddressContainer()).getAfi()));
441 public void mapRequest__Ipv6Eid() throws Exception {
442 // Internet Protocol Version 6, Src: 2610:d0:ffff:192::1
443 // (2610:d0:ffff:192::1), Dst: 2610:d0:ffff:192::2
444 // (2610:d0:ffff:192::2)
447 // Source EID: 2610:d0:ffff:192::1 (2610:d0:ffff:192::1)
448 // ITR-RLOC 1: 10.0.58.156
449 // Record 1: 2610:d0:ffff:192::2/128
450 // Map-Reply Record: EID prefix: 2610:d0:ffff:192::1/128, TTL: 10,
451 // Authoritative, No-Action
453 mapRequestPacket = extractWSUdpByteArray(new String("0000 00 0c 29 34 3e 1b 00 0c 29 f6 d6 0d 08 00 45 00 " //
454 + "0010 00 b0 00 00 40 00 40 11 ea 7b 0a 00 3a 9c 0a 00 "
455 + "0020 01 26 10 f6 10 f6 00 9c 9b 19 80 00 00 00 60 00 "
456 + "0030 00 00 00 68 11 ff 26 10 00 d0 ff ff 01 92 00 00 "
457 + "0040 00 00 00 00 00 01 26 10 00 d0 ff ff 01 92 00 00 "
458 + "0050 00 00 00 00 00 02 10 f6 10 f6 00 68 94 8b 10 00 "
459 + "0060 00 01 ff f5 bf 5d 7b 75 93 e6 00 02 26 10 00 d0 "
460 + "0070 ff ff 01 92 00 00 00 00 00 00 00 01 00 01 0a 00 "
461 + "0080 3a 9c 00 80 00 02 26 10 00 d0 ff ff 01 92 00 00 "
462 + "0090 00 00 00 00 00 02 00 00 00 0a 01 80 10 00 00 00 " //
463 + "00a0 00 02 26 10 00 d0 ff ff 01 92 00 00 00 00 00 00 " //
464 + "00b0 00 01 01 64 ff 00 00 05 00 01 0a 00 3a 9c"));
466 oneOf(nps).publish(with(lispNotificationSaver));
469 handleMapRequestAsByteArray(mapRequestPacket);
470 assertEquals(getIPV6("2610:d0:ffff:192:0:0:0:1"), LispAFIConvertor.toAFI(lastMapRequest().getSourceEid().getLispAddressContainer()));
471 assertEquals(getIPV6("2610:d0:ffff:192:0:0:0:2"), LispAFIConvertor.toAFI(lastMapRequest().getEidRecord().get(0).getLispAddressContainer()));
476 public void mapRequest__UsesIpv6EncapsulatedUdpPort() throws Exception {
477 // Internet Protocol Version 6, Src: 2610:d0:ffff:192::1
478 // (2610:d0:ffff:192::1), Dst: 2610:d0:ffff:192::2
479 // (2610:d0:ffff:192::2)
480 // encapsulated UDP source port: 4342
482 mapRequestPacket = extractWSUdpByteArray(new String("0000 00 0c 29 34 3e 1b 00 0c 29 f6 d6 0d 08 00 45 00 " //
483 + "0010 00 b0 00 00 40 00 40 11 ea 7b 0a 00 3a 9c 0a 00 "
484 + "0020 01 26 10 f6 10 f6 00 9c 9b 19 80 00 00 00 60 00 "
485 + "0030 00 00 00 68 11 ff 26 10 00 d0 ff ff 01 92 00 00 "
486 + "0040 00 00 00 00 00 01 26 10 00 d0 ff ff 01 92 00 00 "
487 + "0050 00 00 00 00 00 02 10 f6 10 f6 00 68 94 8b 14 00 "
488 + "0060 00 01 ff f5 bf 5d 7b 75 93 e6 00 02 26 10 00 d0 "
489 + "0070 ff ff 01 92 00 00 00 00 00 00 00 01 00 01 0a 00 "
490 + "0080 3a 9c 00 80 00 02 26 10 00 d0 ff ff 01 92 00 00 "
491 + "0090 00 00 00 00 00 02 00 00 00 0a 01 80 10 00 00 00 " //
492 + "00a0 00 02 26 10 00 d0 ff ff 01 92 00 00 00 00 00 00 " //
493 + "00b0 00 01 01 64 ff 00 00 05 00 01 0a 00 3a 9c"));
494 oneOf(nps).publish(with(lispNotificationSaver));
497 DatagramPacket replyPacket = handleMapRequestPacket(mapRequestPacket);
498 assertEquals(4342, replyPacket.getPort());
502 public void mapRequest__WithSourceEid() throws Exception {
503 // encapsulated LISP packet
504 // Source EID = 153.16.254.1
506 mapRequestPacket = extractWSUdpByteArray(new String("0000 00 0c 29 7a ce 83 00 15 17 c6 4a c9 08 00 45 00 " //
507 + "0010 00 78 00 00 40 00 3e 11 ec b1 0a 00 01 26 0a 00 "
508 + "0020 3a 9e 10 f6 10 f6 00 64 c3 a5 80 00 00 00 45 00 "
509 + "0030 00 58 d4 31 00 00 ff 11 31 89 99 10 fe 01 0a 00 "
510 + "0040 14 c8 10 f6 10 f6 00 44 84 ee 10 00 00 01 ba f9 "
511 + "0050 ff 53 27 36 38 3a 00 01 99 10 fe 01 00 01 0a 00 "
512 + "0060 01 26 00 20 00 01 0a 00 14 c8 00 00 00 0a 01 20 "
513 + "0070 10 00 00 00 00 01 99 10 fe 01 01 64 ff 00 00 05 " //
514 + "0080 00 01 0a 00 01 26"));
516 oneOf(nps).publish(with(lispNotificationSaver));
519 handleMapRequestAsByteArray(mapRequestPacket);
520 Assert.assertNotEquals(AddressFamilyNumberEnum.IP, LispAFIConvertor.toAFI(lastMapRequest().getSourceEid().getLispAddressContainer()));
526 public void mapReply__VerifyBasicIPv4Fields() throws Exception {
527 eidToLocatorBuilder.setMaskLength((short) 0x20).setLispAddressContainer(LispAFIConvertor.toContainer(getIP("10.0.20.200")));
528 mapReplyBuilder.setNonce(0x3d8d2acd39c8d608L);
532 byte[] result = handleMapRequestAsByteArray(mapRequestPacket);
534 assertEquals(28, result.length);
536 byte expectedLispMessageType = 2;
537 assertEquals(expectedLispMessageType, (byte) (result[LispMessage.Pos.TYPE] >> 4));
538 assertEquals(0x3d8d2acd39c8d608L, ByteUtil.getLong(result, MapReplyIpv4SingleLocatorPos.NONCE));
540 byte expectedRecordCount = (byte) 1;
541 assertEquals(expectedRecordCount, result[MapReplyIpv4SingleLocatorPos.RECORD_COUNT]);
543 assertEquals(eidToLocatorBuilder.getMaskLength().byteValue(), result[MapReplyIpv4SingleLocatorPos.EID_MASK_LEN]);
544 assertEquals(AddressFamilyNumberEnum.IP.getIanaCode(), ByteUtil.getShort(result, MapReplyIpv4SingleLocatorPos.AFI_TYPE));
545 assertEquals(0x0a0014c8, ByteUtil.getInt(result, MapReplyIpv4SingleLocatorPos.EID_PREFIX));
550 public void mapReply__VerifyBasicIPv6() throws Exception {
551 eidToLocatorBuilder.setMaskLength((short) 0x80).setLispAddressContainer(LispAFIConvertor.toContainer(getIPV6("0:0:0:0:0:0:0:1")));
555 byte[] result = handleMapRequestAsByteArray(mapRequestPacket);
557 assertEquals(40, result.length);
559 byte expectedRecordCount = (byte) 1;
560 assertEquals(expectedRecordCount, result[MapReplyIpv4SingleLocatorPos.RECORD_COUNT]);
562 assertEquals(eidToLocatorBuilder.getMaskLength().byteValue(), result[MapReplyIpv4SingleLocatorPos.EID_MASK_LEN]);
563 assertEquals(AddressFamilyNumberEnum.IP6.getIanaCode(), ByteUtil.getShort(result, MapReplyIpv4SingleLocatorPos.AFI_TYPE));
564 byte[] expectedIpv6 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
566 ArrayAssert.assertEquals(expectedIpv6, Arrays.copyOfRange(result, 24, 40));
571 public void mapReply__VerifyIPv6EidAndLocator() throws Exception {
572 eidToLocatorBuilder.setLispAddressContainer(LispAFIConvertor.toContainer(getIPV6("0:0:0:0:0:0:0:1")));
573 eidToLocatorBuilder.getLocatorRecord().add(
574 new LocatorRecordBuilder().setLispAddressContainer(LispAFIConvertor.toContainer(getIPV6("0:0:0:0:0:0:0:2"))).build());
578 byte[] result = handleMapRequestAsByteArray(mapRequestPacket);
580 assertEquals(64, result.length);
582 byte[] expectedIpv6Eid = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
583 ArrayAssert.assertEquals(expectedIpv6Eid, Arrays.copyOfRange(result, 24, 40));
585 byte[] expectedIpv6Rloc = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2 };
586 ArrayAssert.assertEquals(expectedIpv6Rloc, Arrays.copyOfRange(result, 48, 64));
591 public void mapReply__UseEncapsulatedUdpPort() throws Exception {
594 assertEquals(LispMessage.PORT_NUM, handleMapRequestPacket(mapRequestPacket).getPort());
599 public void mapReply__WithNonRoutableSingleLocator() throws Exception {
600 eidToLocatorBuilder.setMaskLength((short) 0x20).setLispAddressContainer(LispAFIConvertor.toContainer(getIP("10.0.20.200")));
601 eidToLocatorBuilder.getLocatorRecord().add(
602 new LocatorRecordBuilder().setRouted(false).setLispAddressContainer(LispAFIConvertor.toContainer(getIP("4.3.2.1"))).build());
605 byte[] result = handleMapRequestAsByteArray(mapRequestPacket);
606 assertEquals(0x00, result[MapReplyIpv4SingleLocatorPos.LOCATOR_RBIT] & 0x01);
611 public void mapReply__WithSingleLocator() throws Exception {
612 eidToLocatorBuilder.setMaskLength((short) 0x20)//
613 .setLispAddressContainer(LispAFIConvertor.toContainer(getIP("10.0.20.200")));
614 eidToLocatorBuilder.getLocatorRecord().add(
615 new LocatorRecordBuilder().setRouted(true).setLispAddressContainer(LispAFIConvertor.toContainer(getIP("4.3.2.1"))).build());
618 byte[] result = handleMapRequestAsByteArray(mapRequestPacket);
619 assertEquals(40, result.length);
621 byte expectedLocCount = 1;
622 assertEquals(expectedLocCount, result[MapReplyIpv4SingleLocatorPos.LOCATOR_COUNT]);
624 assertEquals(AddressFamilyNumberEnum.IP.getIanaCode(), ByteUtil.getShort(result, MapReplyIpv4SingleLocatorPos.LOC_AFI));
626 assertEquals(0x04030201, ByteUtil.getInt(result, MapReplyIpv4SingleLocatorPos.LOCATOR));
627 assertEquals(0x01, result[MapReplyIpv4SingleLocatorPos.LOCATOR_RBIT] & 0x01);
632 public void mapReply__WithMultipleLocator() throws Exception {
633 eidToLocatorBuilder.getLocatorRecord().add(
634 new LocatorRecordBuilder().setRouted(true).setLispAddressContainer(LispAFIConvertor.toContainer(getIP("4.3.2.1"))).build());
635 eidToLocatorBuilder.getLocatorRecord().add(
636 new LocatorRecordBuilder().setRouted(true).setLispAddressContainer(LispAFIConvertor.toContainer(getIPV6("0:0:0:0:0:0:0:1"))).build());
639 byte[] result = handleMapRequestAsByteArray(mapRequestPacket);
640 assertEquals(64, result.length);
642 assertEquals(2, result[MapReplyIpv4SingleLocatorPos.LOCATOR_COUNT]);
644 assertEquals(AddressFamilyNumberEnum.IP.getIanaCode(), ByteUtil.getShort(result, MapReplyIpv4SingleLocatorPos.LOC_AFI));
645 assertEquals(0x04030201, ByteUtil.getInt(result, MapReplyIpv4SingleLocatorPos.LOCATOR));
646 assertEquals(0x01, result[MapReplyIpv4SingleLocatorPos.LOCATOR_RBIT] & 0x01);
648 assertEquals(AddressFamilyNumberEnum.IP6.getIanaCode(), ByteUtil.getShort(result, MapReplyIpv4SecondLocatorPos.LOC_AFI));
650 byte[] expectedIpv6Rloc = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
651 ArrayAssert.assertEquals(expectedIpv6Rloc,
652 Arrays.copyOfRange(result, MapReplyIpv4SecondLocatorPos.LOCATOR, MapReplyIpv4SecondLocatorPos.LOCATOR + 16));
654 assertEquals(0x01, result[MapReplyIpv4SecondLocatorPos.LOCATOR_RBIT] & 0x01);
658 public void handleUnknownLispMessage() throws Exception {
659 // IP: 192.168.136.10 -> 128.223.156.35
660 // UDP: 49289 -> 4342
661 // LISP(Type = 14 UNKNOWN!!!, P=1, M=1
663 byte[] unknownTypePacket = extractWSUdpByteArray(new String("0000 00 50 56 ee d1 4f 00 0c 29 7a ce 79 08 00 45 00 " //
664 + "0010 00 5c 00 00 40 00 40 11 d4 db c0 a8 88 0a 80 df "
665 + "0020 9c 23 d6 40 10 f6 00 48 59 a4 F8 00 01 01 00 00 "
666 + "0030 00 00 00 00 00 00 00 01 00 14 e8 f5 0b c5 c5 f2 "
667 + "0040 b0 21 27 a8 21 41 04 f3 46 5a a5 68 89 ec 00 00 "
668 + "0050 00 0a 01 20 10 00 00 00 00 01 99 10 fe 01 01 64 " //
669 + "0060 ff 00 00 05 00 01 c0 a8 88 0a"));
670 assertNull(handlePacket(unknownTypePacket));
674 public void mapRequest__MultipleItrRlocs() throws Exception {
675 // this is what LISPmob sends when configured multiple RLOCs for single
677 // ITR-RLOC 1: 10.1.0.111
678 // ITR-RLOC 2: 192.168.136.51
680 mapRequestPacket = extractWSUdpByteArray(new String("0000 00 0c 29 7a ce 8d 00 0c 29 e4 ef 70 08 00 45 00 "
681 + "0010 00 8a 00 00 40 00 40 11 25 f2 0a 01 00 6f 0a 01 " //
682 + "0020 00 01 10 f6 10 f6 00 76 06 1f 80 00 00 00 45 00 " //
683 + "0030 00 6a d4 31 00 00 ff 11 2a 3e ac 01 01 02 08 08 " //
684 + "0040 08 08 10 f6 10 f6 00 56 63 14 10 00 01 01 79 67 " //
685 + "0050 ff 75 a0 61 66 19 00 01 ac 01 01 02 00 01 0a 01 " //
686 + "0060 00 6f 00 01 c0 a8 88 33 00 20 00 01 08 08 08 08 " //
687 + "0070 00 00 00 0a 02 20 10 00 00 00 00 01 ac 01 01 02 " //
688 + "0080 01 64 ff 00 00 05 00 01 0a 01 00 6f 06 64 ff 00 " //
689 + "0090 00 05 00 01 c0 a8 88 33"));
691 oneOf(nps).publish(with(lispNotificationSaver));
692 handleMapRequestAsByteArray(mapRequestPacket);
696 private void stubMapRegister(final boolean setNotifyFromRegister) {
697 allowing(nps).publish(with(lispNotificationSaver));
698 will(new SimpleAction() {
701 public Object invoke(Invocation invocation) throws Throwable {
702 if (setNotifyFromRegister) {
703 MapNotifyBuilderHelper.setFromMapRegister(mapNotifyBuilder, lastMapRegister());
710 private void stubHandleRequest() {
711 allowing(nps).publish(wany(Notification.class));
714 private byte[] handleMapRequestAsByteArray(byte[] inPacket) {
715 handleMapRequestPacket(inPacket);
716 return lastMapReplyPacket().getData();
719 private byte[] handleMapRegisterAsByteArray(byte[] inPacket) {
720 handleMapRegisterPacket(inPacket);
721 return lastMapNotifyPacket().getData();
724 private DatagramPacket handleMapRequestPacket(byte[] inPacket) {
725 DatagramPacket dp = new DatagramPacket(inPacket, inPacket.length);
726 // Unless we explicitly set the source port, it will be -1, which breaks some tests
727 // This is till not the real port number, but it's better
728 dp.setPort(LispMessage.PORT_NUM);
729 testedLispService.handlePacket(dp);
730 return lastMapReplyPacket();
733 private DatagramPacket handleMapRegisterPacket(byte[] inPacket) {
734 DatagramPacket dp = new DatagramPacket(inPacket, inPacket.length);
735 // Unless we explicitly set the source port, it will be -1, which breaks some tests
736 // This is till not the real port number, but it's better
737 dp.setPort(LispMessage.PORT_NUM);
738 testedLispService.handlePacket(dp);
739 if (mapNotifyBuilder == null) {
742 return lastMapNotifyPacket();
746 private DatagramPacket handlePacket(byte[] inPacket) {
747 // TODO get from mock
748 testedLispService.handlePacket(new DatagramPacket(inPacket, inPacket.length));
752 private byte[] extractWSUdpByteArray(String wiresharkHex) {
753 final int HEADER_LEN = 42;
754 byte[] res = new byte[1000];
755 String[] split = wiresharkHex.split(" ");
757 for (String cur : split) {
759 if (cur.length() == 2) {
761 if (counter > HEADER_LEN) {
762 res[counter - HEADER_LEN - 1] = (byte) Integer.parseInt(cur, 16);
767 return Arrays.copyOf(res, counter - HEADER_LEN);
770 @Test(expected = LispMalformedPacketException.class)
771 public void mapRequest__NoIPITRRLOC() throws Exception {
772 mapRequestPacket = hexToByteBuffer("10 00 " //
773 + "02 " // This means 3 ITR - RLOCs
774 + "01 3d 8d 2a cd 39 c8 d6 08 00 00 " //
775 + "40 05 c0 a8 88 0a 01 02 " // MAC (ITR-RLOC #1 of 3)
776 + "40 05 00 00 00 00 00 00 " // MAC (ITR-RLOC #2 of 3)
777 + "40 05 11 22 34 56 78 90 " // MAC (ITR-RLOC #3 of 3)
778 + "00 20 00 01 01 02 03 04").array();
779 handleMapRequestPacket(mapRequestPacket);
784 // public void mapRequest__IPITRRLOCIsSecond() throws Exception {
785 // mapRequestPacket = hexToByteBuffer("10 00 " //
786 // + "01 " // This means 3 ITR - RLOCs
787 // + "01 3d 8d 2a cd 39 c8 d6 08 00 00 " //
788 // + "40 05 c0 a8 88 0a 01 02 " // MAC (ITR-RLOC #1 of 2)
789 // + "00 01 01 02 03 04 " // IP (ITR-RLOC #2 of 2)
790 // + "00 20 00 01 01 02 03 04").array();
791 // oneOf(nps).publish(with(lispNotificationSaver));
793 // DatagramPacket packet = handleMapRequestPacket(mapRequestPacket);
794 // assertEquals(2, lastMapRequest().getItrRlocs().size());
795 // assertEquals((new LispIpv4Address("1.2.3.4")).getAddress(),
796 // packet.getAddress());
801 // public void mapRequest__MULTIPLEIPITRRLOCs() throws Exception {
802 // mapRequestPacket = hexToByteBuffer("10 00 " //
803 // + "01 " // This means 3 ITR - RLOCs
804 // + "01 3d 8d 2a cd 39 c8 d6 08 00 00 " //
805 // + "00 01 01 02 03 04 " // IP (ITR-RLOC #1 of 2)
806 // + "00 01 c0 a8 88 0a " // MAC (ITR-RLOC #2 of 2)
807 // + "00 20 00 01 01 02 03 04").array();
808 // oneOf(nps).publish(with(lispNotificationSaver));
810 // DatagramPacket packet = handleMapRequestPacket(mapRequestPacket);
811 // assertEquals(2, lastMapRequest().getItrRloc().size());
812 // assertEquals((new LispIpv4Address("1.2.3.4")).getAddress(),
813 // packet.getAddress());