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 org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertTrue;
15 import java.net.DatagramPacket;
16 import java.nio.ByteBuffer;
17 import java.util.ArrayList;
18 import java.util.Arrays;
19 import java.util.List;
21 import junitx.framework.ArrayAssert;
22 import junitx.framework.Assert;
24 import org.apache.commons.lang3.ArrayUtils;
25 import org.jmock.api.Invocation;
26 import org.junit.Before;
27 import org.junit.Ignore;
28 import org.junit.Test;
29 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
30 import org.opendaylight.lispflowmapping.lisp.type.AddressFamilyNumberEnum;
31 import org.opendaylight.lispflowmapping.lisp.type.LispMessage;
32 import org.opendaylight.lispflowmapping.lisp.type.LispMessageEnum;
33 import org.opendaylight.lispflowmapping.lisp.util.ByteUtil;
34 import org.opendaylight.lispflowmapping.lisp.util.LispAFIConvertor;
35 import org.opendaylight.lispflowmapping.lisp.util.MapNotifyBuilderHelper;
36 import org.opendaylight.lispflowmapping.lisp.serializer.MapNotifySerializer;
37 import org.opendaylight.lispflowmapping.lisp.serializer.MapReplySerializer;
38 import org.opendaylight.lispflowmapping.southbound.lisp.exception.LispMalformedPacketException;
39 import org.opendaylight.lispflowmapping.tools.junit.BaseTestCase;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev150820.AddMapping;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev150820.EidToLocatorRecord.Action;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev150820.MapRegister;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev150820.MapRequest;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev150820.RequestMapping;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev150820.eidrecords.EidRecord;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev150820.eidtolocatorrecords.EidToLocatorRecord;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev150820.eidtolocatorrecords.EidToLocatorRecordBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev150820.lispaddress.LispAddressContainer;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev150820.locatorrecords.LocatorRecord;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev150820.locatorrecords.LocatorRecordBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev150820.mapnotifymessage.MapNotifyBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev150820.mapreplymessage.MapReplyBuilder;
53 import org.opendaylight.yangtools.yang.binding.Notification;
55 public class LispSouthboundServiceTest extends BaseTestCase {
57 private LispSouthboundService testedLispService;
58 private NotificationPublishService nps;
59 private byte[] mapRequestPacket;
60 private byte[] mapRegisterPacket;
61 private ValueSaverAction<Notification> lispNotificationSaver;
62 // private ValueSaverAction<MapRegister> mapRegisterSaver;
63 // private ValueSaverAction<MapRequest> mapRequestSaver;
64 private MapNotifyBuilder mapNotifyBuilder;
65 private MapReplyBuilder mapReplyBuilder;
66 private EidToLocatorRecordBuilder eidToLocatorBuilder;
68 private interface MapReplyIpv4SingleLocatorPos {
71 int LOCATOR_COUNT = 16;
72 int EID_MASK_LEN = 17;
76 int LOCATOR_RBIT = 33;
80 private interface MapReplyIpv4SecondLocatorPos {
81 int FIRST_LOCATOR_IPV4_LENGTH = 12;
82 int LOC_AFI = MapReplyIpv4SingleLocatorPos.LOC_AFI + FIRST_LOCATOR_IPV4_LENGTH;
83 int LOCATOR_RBIT = MapReplyIpv4SingleLocatorPos.LOCATOR_RBIT + FIRST_LOCATOR_IPV4_LENGTH;
84 int LOCATOR = MapReplyIpv4SingleLocatorPos.LOCATOR + FIRST_LOCATOR_IPV4_LENGTH;
89 public void before() throws Exception {
91 // mapResolver = context.mock(IMapResolver.class);
92 // mapServer = context.mock(IMapServer.class);
93 testedLispService = new LispSouthboundService(null);
94 nps = context.mock(NotificationPublishService.class);
95 testedLispService.setNotificationProvider(nps);
96 lispNotificationSaver = new ValueSaverAction<Notification>();
97 // mapRegisterSaver = new ValueSaverAction<MapRegister>();
98 // mapRequestSaver = new ValueSaverAction<MapRequest>();
99 // SRC: 127.0.0.1:58560 to 127.0.0.1:4342
100 // LISP(Type = 8 - Encapsulated)
101 // IP: 192.168.136.10 -> 1.2.3.4
103 // LISP(Type = 1 Map-Request
107 // Source EID not present
108 // Nonce: 0x3d8d2acd39c8d608
109 // ITR-RLOC AFI=1 Address=192.168.136.10
110 // Record 1: 1.2.3.4/32
111 mapRequestPacket = extractWSUdpByteArray(new String("0000 00 00 00 00 00 00 00 00 00 00 00 00 08 00 45 00 " //
112 + "0010 00 58 00 00 40 00 40 11 3c 93 7f 00 00 01 7f 00 "
113 + "0020 00 01 e4 c0 10 f6 00 44 fe 57 80 00 00 00 45 00 "
114 + "0030 00 38 d4 31 00 00 ff 11 56 f3 c0 a8 88 0a 01 02 "
115 + "0040 03 04 dd b4 10 f6 00 24 ef 3a 10 00 00 01 3d 8d "
116 + "0050 2a cd 39 c8 d6 08 00 01 01 02 03 04 00 01 c0 a8 88 0a 00 20 " //
117 + "0060 00 01 01 02 03 04"));
118 mapReplyBuilder = new MapReplyBuilder();
119 mapReplyBuilder.setEidToLocatorRecord(new ArrayList<EidToLocatorRecord>());
120 mapReplyBuilder.setNonce((long) 0);
121 mapReplyBuilder.setEchoNonceEnabled(false);
122 mapReplyBuilder.setProbe(true);
123 mapReplyBuilder.setSecurityEnabled(true);
124 eidToLocatorBuilder = new EidToLocatorRecordBuilder();
125 String ip = "0.0.0.0";
126 eidToLocatorBuilder.setLispAddressContainer(LispAFIConvertor.asIPv4Prefix(ip, 0));
127 eidToLocatorBuilder.setLocatorRecord(new ArrayList<LocatorRecord>());
128 eidToLocatorBuilder.setRecordTtl(10);
129 eidToLocatorBuilder.setMapVersion((short) 0);
130 eidToLocatorBuilder.setMaskLength((short) 0);
131 eidToLocatorBuilder.setAction(Action.NativelyForward);
132 eidToLocatorBuilder.setAuthoritative(false);
133 // eidToLocatorBuilder.setPrefix(new LispIpv4Address(0));
134 // mapReply.addEidToLocator(eidToLocatorBuilder);
136 // IP: 192.168.136.10 -> 128.223.156.35
137 // UDP: 49289 -> 4342
138 // LISP(Type = 3 Map-Register, P=1, M=1
142 // AuthDataLength: 20 Data:
143 // e8:f5:0b:c5:c5:f2:b0:21:27:a8:21:41:04:f3:46:5a:a5:68:89:ec
144 // EID prefix: 153.16.254.1/32 (EID=0x9910FE01), TTL: 10, Authoritative,
146 // Local RLOC: 192.168.136.10 (RLOC=0xC0A8880A), Reachable,
147 // Priority/Weight: 1/100, Multicast Priority/Weight:
151 mapRegisterPacket = extractWSUdpByteArray(new String("0000 00 50 56 ee d1 4f 00 0c 29 7a ce 79 08 00 45 00 " //
152 + "0010 00 5c 00 00 40 00 40 11 d4 db c0 a8 88 0a 80 df "
153 + "0020 9c 23 d6 40 10 f6 00 48 59 a4 38 00 01 01 00 00 "
154 + "0030 00 00 00 00 00 00 00 01 00 14 0e a4 c6 d8 a4 06 "
155 + "0040 71 7c 33 a4 5c 4a 83 1c de 74 53 03 0c ad 00 00 "
156 + "0050 00 0a 01 20 10 00 00 00 00 01 99 10 fe 01 01 64 " //
157 + "0060 ff 00 00 05 00 01 c0 a8 88 0a"));
158 mapNotifyBuilder = new MapNotifyBuilder();
159 mapNotifyBuilder.setAuthenticationData(new byte[0]);
164 public void todos() throws Exception {
166 // TODO: MapRequest: usage of Map-Reply-Record in MapRequest packet.
167 // TODO: Non-Encapsulated packets
170 @Test(expected = LispMalformedPacketException.class)
171 public void mapRegister__IllegalPacket() throws Exception {
172 mapRegisterPacket = extractWSUdpByteArray(new String("0000 00 0c 29 7a ce 8d 00 0c 29 e4 ef 70 08 00 45 00 "
173 + "0010 00 68 00 00 40 00 40 11 26 15 0a 01 00 6e 0a 01 " //
174 + "0020 00 01 10 f6 10 f6 00 54 03 3b 38 00 01 01 00 00 "));
176 handleMapRegisterPacket(mapRegisterPacket);
179 @Test(expected = LispMalformedPacketException.class)
180 public void mapRequest__IllegalPacket() throws Exception {
181 mapRequestPacket = extractWSUdpByteArray(new String("0000 00 00 00 00 00 00 00 00 00 00 00 00 08 00 45 00 " //
182 + "0010 00 58 00 00 40 00 40 11 3c 93 7f 00 00 01 7f 00 "
183 + "0020 00 01 e4 c0 10 f6 00 44 fe 57 80 00 00 00 45 00 "
184 + "0030 00 38 d4 31 00 00 ff 11 56 f3 c0 a8 88 0a 01 02 " //
185 + "0040 03 04 dd b4 10 f6 00 24 ef 3a 10 00 00 01 3d 8d "));
186 handleMapRequestPacket(mapRequestPacket);
189 @Test(expected = LispMalformedPacketException.class)
190 public void mapRequest__IllegalEncapsulatedPacket() throws Exception {
191 mapRequestPacket = extractWSUdpByteArray(new String("0000 00 00 00 00 00 00 00 00 00 00 00 00 08 00 45 00 " //
192 + "0010 00 58 00 00 40 00 40 11 3c 93 7f 00 00 01 7f 00 " //
193 + "0020 00 01 e4 c0 10 f6 00 44 fe 57 80 00 00 00 45 00 "));
194 handleMapRequestPacket(mapRequestPacket);
197 private MapRegister lastMapRegister() {
198 assertTrue(lispNotificationSaver.lastValue instanceof AddMapping);
199 AddMapping lastValue = (AddMapping) lispNotificationSaver.lastValue;
200 return lastValue.getMapRegister();
203 private MapRequest lastMapRequest() {
204 RequestMapping lastValue = (RequestMapping) lispNotificationSaver.lastValue;
205 return lastValue.getMapRequest();
209 public void mapRegister__TwoRlocs() throws Exception {
211 // EID prefix: 172.1.1.2/32, TTL: 10, Authoritative, No-Action
212 // Local RLOC: 10.1.0.110, Reachable, Priority/Weight: 1/100, Multicast
213 // Priority/Weight: 255/0
214 // Local RLOC: 192.168.136.51, Reachable, Priority/Weight: 6/100,
215 // Multicast Priority/Weight: 255/0
216 mapRegisterPacket = extractWSUdpByteArray(new String("0000 00 0c 29 7a ce 8d 00 0c 29 e4 ef 70 08 00 45 00 "
217 + "0010 00 68 00 00 40 00 40 11 26 15 0a 01 00 6e 0a 01 " //
218 + "0020 00 01 10 f6 10 f6 00 54 03 3b 38 00 01 01 00 00 " //
219 + "0030 00 00 00 00 00 00 00 01 00 14 ae d8 7b d4 9c 59 " //
220 + "0040 e9 35 75 6e f1 29 27 a3 45 20 96 06 c2 e1 00 00 " //
221 + "0050 00 0a 02 20 10 00 00 00 00 01 ac 01 01 02 01 64 " //
222 + "0060 ff 00 00 05 00 01 0a 01 00 6e 06 64 ff 00 00 05 " //
223 + "0070 00 01 c0 a8 88 33"));
225 oneOf(nps).putNotification(with(lispNotificationSaver));
227 handleMapRegisterPacket(mapRegisterPacket);
229 List<EidToLocatorRecord> eidRecords = lastMapRegister().getEidToLocatorRecord();
230 assertEquals(1, eidRecords.size());
231 EidToLocatorRecord eidRecord = eidRecords.get(0);
232 assertEquals(2, eidRecord.getLocatorRecord().size());
233 assertEquals(LispAFIConvertor.toContainer(LispAFIConvertor.asIPAfiAddress("10.1.0.110")), eidRecord.getLocatorRecord().get(0).getLispAddressContainer());
234 assertEquals(LispAFIConvertor.toContainer(LispAFIConvertor.asIPAfiAddress("192.168.136.51")), eidRecord.getLocatorRecord().get(1).getLispAddressContainer());
238 public void mapRegister__Ipv6Rloc() throws Exception {
239 // P bit (Proxy-Map-Reply): Set
240 // M bit (Want-Map-Notify): Set
245 // Authentication Data: 5bc4d44a57e2a55d577a6f89779c004f5da713fb
246 // EID prefix: 2610:d0:ffff:192::1/128, TTL: 10, Authoritative,
248 // Local RLOC: 10.0.58.156, Reachable, Priority/Weight: 1/100, Multicast
249 // Priority/Weight: 255/0
251 mapRegisterPacket = extractWSUdpByteArray(new String("0000 00 0c 29 34 3e 1b 00 0c 29 f6 d6 0d 08 00 45 00 " //
252 + "0010 00 68 00 00 40 00 40 11 ea c3 0a 00 3a 9c 0a 00 " //
253 + "0020 01 26 10 f6 10 f6 00 54 f5 9a 38 00 03 01 00 00 " //
254 + "0030 00 00 00 00 00 00 00 01 00 14 22 97 ff 61 ec d8 " //
255 + "0040 0f 91 c6 c4 01 ef 7f bb 77 58 39 5c 92 23 00 00 " //
256 + "0050 00 0a 01 80 10 00 00 00 00 02 26 10 00 d0 ff ff " //
257 + "0060 01 92 00 00 00 00 00 00 00 01 01 64 ff 00 00 05 " //
258 + "0070 00 01 0a 00 3a 9c"));
260 oneOf(nps).putNotification(with(lispNotificationSaver));
262 handleMapRegisterPacket(mapRegisterPacket);
264 EidToLocatorRecord eidToLocatorRecord = lastMapRegister().getEidToLocatorRecord().get(0);
265 assertEquals(LispAFIConvertor.asIPv6Address("2610:d0:ffff:192:0:0:0:1"), eidToLocatorRecord.getLispAddressContainer());
266 assertEquals(AddressFamilyNumberEnum.IP6,
267 AddressFamilyNumberEnum.valueOf(LispAFIConvertor.toAFI(eidToLocatorRecord.getLispAddressContainer()).getAfi()));
269 assertEquals(LispAFIConvertor.asIPAfiAddress("10.0.58.156"), LispAFIConvertor.toAFI(eidToLocatorRecord.getLocatorRecord().get(0).getLispAddressContainer()));
273 public void mapRegister__VerifyBasicFields() throws Exception {
274 oneOf(nps).putNotification(with(lispNotificationSaver));
275 handleMapRegisterPacket(mapRegisterPacket);
277 EidToLocatorRecord eidToLocator = lastMapRegister().getEidToLocatorRecord().get(0);
278 assertEquals(LispAFIConvertor.asIPv4Address("153.16.254.1"), eidToLocator.getLispAddressContainer());
280 assertEquals(1, eidToLocator.getLocatorRecord().size());
281 assertEquals(LispAFIConvertor.asIPAfiAddress("192.168.136.10"), LispAFIConvertor.toAFI(eidToLocator.getLocatorRecord().get(0).getLispAddressContainer()));
286 public void mapRegister__NoResponseFromMapServerShouldReturnNullPacket() throws Exception {
287 oneOf(nps).putNotification(with(lispNotificationSaver));
288 mapNotifyBuilder = null;
290 assertNull(handleMapRegisterPacket(mapRegisterPacket));
294 public void mapRegister__NonSetMBit() throws Exception {
295 byte[] registerWithNonSetMBit = extractWSUdpByteArray(new String("0000 00 50 56 ee d1 4f 00 0c 29 7a ce 79 08 00 45 00 " //
296 + "0010 00 5c 00 00 40 00 40 11 d4 db c0 a8 88 0a 80 df " //
297 + "0020 9c 23 d6 40 10 f6 00 48 59 a4 38 00 00 01 00 00 "
298 + "0030 00 00 00 00 00 00 00 01 00 14 79 d1 44 66 19 99 "
299 + "0040 83 63 a7 79 6e f0 40 97 54 26 3a 44 b4 eb 00 00 " //
300 + "0050 00 0a 01 20 10 00 00 00 00 01 99 10 fe 01 01 64 " //
301 + "0060 ff 00 00 05 00 01 c0 a8 88 0a"));
302 stubMapRegister(true);
304 handleMapRegisterPacket(registerWithNonSetMBit);
306 assertFalse(lastMapRegister().isWantMapNotify());
310 public void mapRegister__NonSetMBitWithNonZeroReservedBits() 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 02 01 00 00 "
314 + "0030 00 00 00 00 00 00 00 01 00 14 c0 c7 c5 2f 57 f6 "
315 + "0040 e7 20 25 3d e8 b2 07 e2 63 de 62 2b 7a 20 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);
321 assertFalse(lastMapRegister().isWantMapNotify());
325 public void mapRegister__SetMBitWithNonZeroReservedBits() throws Exception {
326 byte[] registerWithNonSetMBit = extractWSUdpByteArray(new String("0000 00 50 56 ee d1 4f 00 0c 29 7a ce 79 08 00 45 00 " //
327 + "0010 00 5c 00 00 40 00 40 11 d4 db c0 a8 88 0a 80 df "
328 + "0020 9c 23 d6 40 10 f6 00 48 59 a4 38 00 03 01 00 00 "
329 + "0030 00 00 00 00 00 00 00 01 00 14 a2 72 40 7b 1a ae "
330 + "0040 4e 6b e2 e5 e1 01 40 8a c9 e1 d1 80 cb 72 00 00 "
331 + "0050 00 0a 01 20 10 00 00 00 00 01 99 10 fe 01 01 64 " //
332 + "0060 ff 00 00 05 00 01 c0 a8 88 0a"));
333 stubMapRegister(true);
335 handleMapRegisterPacket(registerWithNonSetMBit);
336 assertTrue(lastMapRegister().isWantMapNotify());
341 public void mapRegisterAndNotify__ValidExtraDataParsedSuccessfully() throws Exception {
342 byte[] extraDataPacket = new byte[mapRegisterPacket.length + 3];
343 extraDataPacket[mapRegisterPacket.length] = 0x9;
344 System.arraycopy(mapRegisterPacket, 0, extraDataPacket, 0, mapRegisterPacket.length);
345 stubMapRegister(true);
347 DatagramPacket dp = new DatagramPacket(extraDataPacket, extraDataPacket.length);
348 dp.setLength(mapRegisterPacket.length);
349 testedLispService.handlePacket(dp);
350 // Check map register fields.
352 // byte[] notifyResult = testedLispService.handlePacket(dp).getData();
353 byte[] notifyResult = lastMapNotifyPacket().getData();
354 assertEquals(mapRegisterPacket.length, notifyResult.length);
358 private DatagramPacket lastMapReplyPacket() {
359 ByteBuffer serialize = MapReplySerializer.getInstance().serialize(mapReplyBuilder.build());
360 return new DatagramPacket(serialize.array(), serialize.array().length);
363 private DatagramPacket lastMapNotifyPacket() {
364 if (mapNotifyBuilder.getEidToLocatorRecord() == null) {
365 mapNotifyBuilder.setEidToLocatorRecord(new ArrayList<EidToLocatorRecord>());
367 mapNotifyBuilder.getEidToLocatorRecord().add(eidToLocatorBuilder.build());
368 mapNotifyBuilder.setNonce((long) 0);
369 mapNotifyBuilder.setKeyId((short) 0);
370 mapNotifyBuilder.setAuthenticationData(new byte[0]);
371 ByteBuffer serialize = MapNotifySerializer.getInstance().serialize(mapNotifyBuilder.build());
372 return new DatagramPacket(serialize.array(), serialize.array().length);
377 public void mapNotify__VerifyBasicFields() throws Exception {
378 byte registerType = mapRegisterPacket[0];
379 assertEquals(LispMessageEnum.MapRegister.getValue(), registerType >> 4);
381 stubMapRegister(true);
383 byte[] result = handleMapRegisterAsByteArray(mapRegisterPacket);
385 assertEquals(mapRegisterPacket.length, result.length);
387 byte expectedType = (byte) (LispMessageEnum.MapNotify.getValue() << 4);
388 assertHexEquals(expectedType, result[0]);
389 assertHexEquals((byte) 0x00, result[1]);
390 assertHexEquals((byte) 0x00, result[2]);
392 byte[] registerWithoutTypeWithoutAuthenticationData = ArrayUtils.addAll(Arrays.copyOfRange(mapRegisterPacket, 3, 16),
393 Arrays.copyOfRange(mapRegisterPacket, 36, mapRegisterPacket.length));
394 byte[] notifyWithoutTypeWithOutAuthenticationData = ArrayUtils.addAll(Arrays.copyOfRange(result, 3, 16),
395 Arrays.copyOfRange(result, 36, result.length));
396 ArrayAssert.assertEquals(registerWithoutTypeWithoutAuthenticationData, notifyWithoutTypeWithOutAuthenticationData);
401 public void mapNotify__VerifyPort() throws Exception {
402 stubMapRegister(true);
404 DatagramPacket notifyPacket = handleMapRegisterPacket(mapRegisterPacket);
405 assertEquals(LispMessage.PORT_NUM, notifyPacket.getPort());
409 public void mapRequest__VerifyBasicFields() throws Exception {
410 oneOf(nps).putNotification(with(lispNotificationSaver));
411 handleMapRequestAsByteArray(mapRequestPacket);
412 List<EidRecord> eids = lastMapRequest().getEidRecord();
413 assertEquals(1, eids.size());
414 LispAddressContainer lispAddress = eids.get(0).getLispAddressContainer();
415 assertTrue(LispAFIConvertor.toAFI(lispAddress) instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev150820.lispaddress.lispaddresscontainer.address.ipv4.Ipv4Address);
416 assertEquals(LispAFIConvertor.asIPv4Address("1.2.3.4"), lispAddress);
417 assertEquals((byte) 0x20, eids.get(0).getMask().byteValue());
418 assertEquals(0x3d8d2acd39c8d608L, lastMapRequest().getNonce().longValue());
419 // assertEquals(AddressFamilyNumberEnum.RESERVED,
420 // AddressFamilyNumberEnum.valueOf(LispAFIToContainerConvertorFactory.toAFI(
421 // lastMapRequest().getSourceEid().getLispAddressContainer()).getAfi()));
425 public void mapRequest__Ipv6Eid() throws Exception {
426 // Internet Protocol Version 6, Src: 2610:d0:ffff:192::1
427 // (2610:d0:ffff:192::1), Dst: 2610:d0:ffff:192::2
428 // (2610:d0:ffff:192::2)
431 // Source EID: 2610:d0:ffff:192::1 (2610:d0:ffff:192::1)
432 // ITR-RLOC 1: 10.0.58.156
433 // Record 1: 2610:d0:ffff:192::2/128
434 // Map-Reply Record: EID prefix: 2610:d0:ffff:192::1/128, TTL: 10,
435 // Authoritative, No-Action
437 mapRequestPacket = extractWSUdpByteArray(new String("0000 00 0c 29 34 3e 1b 00 0c 29 f6 d6 0d 08 00 45 00 " //
438 + "0010 00 b0 00 00 40 00 40 11 ea 7b 0a 00 3a 9c 0a 00 "
439 + "0020 01 26 10 f6 10 f6 00 9c 9b 19 80 00 00 00 60 00 "
440 + "0030 00 00 00 68 11 ff 26 10 00 d0 ff ff 01 92 00 00 "
441 + "0040 00 00 00 00 00 01 26 10 00 d0 ff ff 01 92 00 00 "
442 + "0050 00 00 00 00 00 02 10 f6 10 f6 00 68 94 8b 10 00 "
443 + "0060 00 01 ff f5 bf 5d 7b 75 93 e6 00 02 26 10 00 d0 "
444 + "0070 ff ff 01 92 00 00 00 00 00 00 00 01 00 01 0a 00 "
445 + "0080 3a 9c 00 80 00 02 26 10 00 d0 ff ff 01 92 00 00 "
446 + "0090 00 00 00 00 00 02 00 00 00 0a 01 80 10 00 00 00 " //
447 + "00a0 00 02 26 10 00 d0 ff ff 01 92 00 00 00 00 00 00 " //
448 + "00b0 00 01 01 64 ff 00 00 05 00 01 0a 00 3a 9c"));
450 oneOf(nps).putNotification(with(lispNotificationSaver));
453 handleMapRequestAsByteArray(mapRequestPacket);
454 assertEquals(LispAFIConvertor.asIPv6AfiAddress("2610:d0:ffff:192:0:0:0:1"), LispAFIConvertor.toAFI(lastMapRequest().getSourceEid().getLispAddressContainer()));
455 assertEquals(LispAFIConvertor.asIPv6Address("2610:d0:ffff:192:0:0:0:2"), lastMapRequest().getEidRecord().get(0).getLispAddressContainer());
460 public void mapRequest__UsesIpv6EncapsulatedUdpPort() throws Exception {
461 // Internet Protocol Version 6, Src: 2610:d0:ffff:192::1
462 // (2610:d0:ffff:192::1), Dst: 2610:d0:ffff:192::2
463 // (2610:d0:ffff:192::2)
464 // encapsulated UDP source port: 4342
466 mapRequestPacket = extractWSUdpByteArray(new String("0000 00 0c 29 34 3e 1b 00 0c 29 f6 d6 0d 08 00 45 00 " //
467 + "0010 00 b0 00 00 40 00 40 11 ea 7b 0a 00 3a 9c 0a 00 "
468 + "0020 01 26 10 f6 10 f6 00 9c 9b 19 80 00 00 00 60 00 "
469 + "0030 00 00 00 68 11 ff 26 10 00 d0 ff ff 01 92 00 00 "
470 + "0040 00 00 00 00 00 01 26 10 00 d0 ff ff 01 92 00 00 "
471 + "0050 00 00 00 00 00 02 10 f6 10 f6 00 68 94 8b 14 00 "
472 + "0060 00 01 ff f5 bf 5d 7b 75 93 e6 00 02 26 10 00 d0 "
473 + "0070 ff ff 01 92 00 00 00 00 00 00 00 01 00 01 0a 00 "
474 + "0080 3a 9c 00 80 00 02 26 10 00 d0 ff ff 01 92 00 00 "
475 + "0090 00 00 00 00 00 02 00 00 00 0a 01 80 10 00 00 00 " //
476 + "00a0 00 02 26 10 00 d0 ff ff 01 92 00 00 00 00 00 00 " //
477 + "00b0 00 01 01 64 ff 00 00 05 00 01 0a 00 3a 9c"));
478 oneOf(nps).putNotification(with(lispNotificationSaver));
481 DatagramPacket replyPacket = handleMapRequestPacket(mapRequestPacket);
482 assertEquals(4342, replyPacket.getPort());
486 public void mapRequest__WithSourceEid() throws Exception {
487 // encapsulated LISP packet
488 // Source EID = 153.16.254.1
490 mapRequestPacket = extractWSUdpByteArray(new String("0000 00 0c 29 7a ce 83 00 15 17 c6 4a c9 08 00 45 00 " //
491 + "0010 00 78 00 00 40 00 3e 11 ec b1 0a 00 01 26 0a 00 "
492 + "0020 3a 9e 10 f6 10 f6 00 64 c3 a5 80 00 00 00 45 00 "
493 + "0030 00 58 d4 31 00 00 ff 11 31 89 99 10 fe 01 0a 00 "
494 + "0040 14 c8 10 f6 10 f6 00 44 84 ee 10 00 00 01 ba f9 "
495 + "0050 ff 53 27 36 38 3a 00 01 99 10 fe 01 00 01 0a 00 "
496 + "0060 01 26 00 20 00 01 0a 00 14 c8 00 00 00 0a 01 20 "
497 + "0070 10 00 00 00 00 01 99 10 fe 01 01 64 ff 00 00 05 " //
498 + "0080 00 01 0a 00 01 26"));
500 oneOf(nps).putNotification(with(lispNotificationSaver));
503 handleMapRequestAsByteArray(mapRequestPacket);
504 Assert.assertNotEquals(AddressFamilyNumberEnum.IP, LispAFIConvertor.toAFI(lastMapRequest().getSourceEid().getLispAddressContainer()));
510 public void mapReply__VerifyBasicIPv4Fields() throws Exception {
511 eidToLocatorBuilder.setMaskLength((short) 0x20).setLispAddressContainer(LispAFIConvertor.toContainer(LispAFIConvertor.asIPAfiAddress("10.0.20.200")));
512 mapReplyBuilder.setNonce(0x3d8d2acd39c8d608L);
516 byte[] result = handleMapRequestAsByteArray(mapRequestPacket);
518 assertEquals(28, result.length);
520 byte expectedLispMessageType = 2;
521 assertEquals(expectedLispMessageType, (byte) (result[LispMessage.Pos.TYPE] >> 4));
522 assertEquals(0x3d8d2acd39c8d608L, ByteUtil.getLong(result, MapReplyIpv4SingleLocatorPos.NONCE));
524 byte expectedRecordCount = (byte) 1;
525 assertEquals(expectedRecordCount, result[MapReplyIpv4SingleLocatorPos.RECORD_COUNT]);
527 assertEquals(eidToLocatorBuilder.getMaskLength().byteValue(), result[MapReplyIpv4SingleLocatorPos.EID_MASK_LEN]);
528 assertEquals(AddressFamilyNumberEnum.IP.getIanaCode(), ByteUtil.getShort(result, MapReplyIpv4SingleLocatorPos.AFI_TYPE));
529 assertEquals(0x0a0014c8, ByteUtil.getInt(result, MapReplyIpv4SingleLocatorPos.EID_PREFIX));
534 public void mapReply__VerifyBasicIPv6() throws Exception {
535 eidToLocatorBuilder.setMaskLength((short) 0x80).setLispAddressContainer(LispAFIConvertor.toContainer(LispAFIConvertor.asIPv6AfiAddress("0:0:0:0:0:0:0:1")));
539 byte[] result = handleMapRequestAsByteArray(mapRequestPacket);
541 assertEquals(40, result.length);
543 byte expectedRecordCount = (byte) 1;
544 assertEquals(expectedRecordCount, result[MapReplyIpv4SingleLocatorPos.RECORD_COUNT]);
546 assertEquals(eidToLocatorBuilder.getMaskLength().byteValue(), result[MapReplyIpv4SingleLocatorPos.EID_MASK_LEN]);
547 assertEquals(AddressFamilyNumberEnum.IP6.getIanaCode(), ByteUtil.getShort(result, MapReplyIpv4SingleLocatorPos.AFI_TYPE));
548 byte[] expectedIpv6 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
550 ArrayAssert.assertEquals(expectedIpv6, Arrays.copyOfRange(result, 24, 40));
555 public void mapReply__VerifyIPv6EidAndLocator() throws Exception {
556 eidToLocatorBuilder.setLispAddressContainer(LispAFIConvertor.toContainer(LispAFIConvertor.asIPv6AfiAddress("0:0:0:0:0:0:0:1")));
557 eidToLocatorBuilder.getLocatorRecord().add(
558 new LocatorRecordBuilder().setLispAddressContainer(LispAFIConvertor.toContainer(LispAFIConvertor.asIPv6AfiAddress("0:0:0:0:0:0:0:2"))).build());
562 byte[] result = handleMapRequestAsByteArray(mapRequestPacket);
564 assertEquals(64, result.length);
566 byte[] expectedIpv6Eid = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
567 ArrayAssert.assertEquals(expectedIpv6Eid, Arrays.copyOfRange(result, 24, 40));
569 byte[] expectedIpv6Rloc = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2 };
570 ArrayAssert.assertEquals(expectedIpv6Rloc, Arrays.copyOfRange(result, 48, 64));
575 public void mapReply__UseEncapsulatedUdpPort() throws Exception {
578 assertEquals(LispMessage.PORT_NUM, handleMapRequestPacket(mapRequestPacket).getPort());
583 public void mapReply__WithNonRoutableSingleLocator() throws Exception {
584 eidToLocatorBuilder.setMaskLength((short) 0x20).setLispAddressContainer(LispAFIConvertor.toContainer(LispAFIConvertor.asIPAfiAddress("10.0.20.200")));
585 eidToLocatorBuilder.getLocatorRecord().add(
586 new LocatorRecordBuilder().setRouted(false).setLispAddressContainer(LispAFIConvertor.toContainer(LispAFIConvertor.asIPAfiAddress("4.3.2.1"))).build());
589 byte[] result = handleMapRequestAsByteArray(mapRequestPacket);
590 assertEquals(0x00, result[MapReplyIpv4SingleLocatorPos.LOCATOR_RBIT] & 0x01);
595 public void mapReply__WithSingleLocator() throws Exception {
596 eidToLocatorBuilder.setMaskLength((short) 0x20)//
597 .setLispAddressContainer(LispAFIConvertor.toContainer(LispAFIConvertor.asIPAfiAddress("10.0.20.200")));
598 eidToLocatorBuilder.getLocatorRecord().add(
599 new LocatorRecordBuilder().setRouted(true).setLispAddressContainer(LispAFIConvertor.toContainer(LispAFIConvertor.asIPAfiAddress("4.3.2.1"))).build());
602 byte[] result = handleMapRequestAsByteArray(mapRequestPacket);
603 assertEquals(40, result.length);
605 byte expectedLocCount = 1;
606 assertEquals(expectedLocCount, result[MapReplyIpv4SingleLocatorPos.LOCATOR_COUNT]);
608 assertEquals(AddressFamilyNumberEnum.IP.getIanaCode(), ByteUtil.getShort(result, MapReplyIpv4SingleLocatorPos.LOC_AFI));
610 assertEquals(0x04030201, ByteUtil.getInt(result, MapReplyIpv4SingleLocatorPos.LOCATOR));
611 assertEquals(0x01, result[MapReplyIpv4SingleLocatorPos.LOCATOR_RBIT] & 0x01);
616 public void mapReply__WithMultipleLocator() throws Exception {
617 eidToLocatorBuilder.getLocatorRecord().add(
618 new LocatorRecordBuilder().setRouted(true).setLispAddressContainer(LispAFIConvertor.toContainer(LispAFIConvertor.asIPAfiAddress("4.3.2.1"))).build());
619 eidToLocatorBuilder.getLocatorRecord().add(
620 new LocatorRecordBuilder().setRouted(true).setLispAddressContainer(LispAFIConvertor.toContainer(LispAFIConvertor.asIPv6AfiAddress("0:0:0:0:0:0:0:1"))).build());
623 byte[] result = handleMapRequestAsByteArray(mapRequestPacket);
624 assertEquals(64, result.length);
626 assertEquals(2, result[MapReplyIpv4SingleLocatorPos.LOCATOR_COUNT]);
628 assertEquals(AddressFamilyNumberEnum.IP.getIanaCode(), ByteUtil.getShort(result, MapReplyIpv4SingleLocatorPos.LOC_AFI));
629 assertEquals(0x04030201, ByteUtil.getInt(result, MapReplyIpv4SingleLocatorPos.LOCATOR));
630 assertEquals(0x01, result[MapReplyIpv4SingleLocatorPos.LOCATOR_RBIT] & 0x01);
632 assertEquals(AddressFamilyNumberEnum.IP6.getIanaCode(), ByteUtil.getShort(result, MapReplyIpv4SecondLocatorPos.LOC_AFI));
634 byte[] expectedIpv6Rloc = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
635 ArrayAssert.assertEquals(expectedIpv6Rloc,
636 Arrays.copyOfRange(result, MapReplyIpv4SecondLocatorPos.LOCATOR, MapReplyIpv4SecondLocatorPos.LOCATOR + 16));
638 assertEquals(0x01, result[MapReplyIpv4SecondLocatorPos.LOCATOR_RBIT] & 0x01);
642 public void handleUnknownLispMessage() throws Exception {
643 // IP: 192.168.136.10 -> 128.223.156.35
644 // UDP: 49289 -> 4342
645 // LISP(Type = 14 UNKNOWN!!!, P=1, M=1
647 byte[] unknownTypePacket = extractWSUdpByteArray(new String("0000 00 50 56 ee d1 4f 00 0c 29 7a ce 79 08 00 45 00 " //
648 + "0010 00 5c 00 00 40 00 40 11 d4 db c0 a8 88 0a 80 df "
649 + "0020 9c 23 d6 40 10 f6 00 48 59 a4 F8 00 01 01 00 00 "
650 + "0030 00 00 00 00 00 00 00 01 00 14 e8 f5 0b c5 c5 f2 "
651 + "0040 b0 21 27 a8 21 41 04 f3 46 5a a5 68 89 ec 00 00 "
652 + "0050 00 0a 01 20 10 00 00 00 00 01 99 10 fe 01 01 64 " //
653 + "0060 ff 00 00 05 00 01 c0 a8 88 0a"));
654 assertNull(handlePacket(unknownTypePacket));
658 public void mapRequest__MultipleItrRlocs() throws Exception {
659 // this is what LISPmob sends when configured multiple RLOCs for single
661 // ITR-RLOC 1: 10.1.0.111
662 // ITR-RLOC 2: 192.168.136.51
664 mapRequestPacket = extractWSUdpByteArray(new String("0000 00 0c 29 7a ce 8d 00 0c 29 e4 ef 70 08 00 45 00 "
665 + "0010 00 8a 00 00 40 00 40 11 25 f2 0a 01 00 6f 0a 01 " //
666 + "0020 00 01 10 f6 10 f6 00 76 06 1f 80 00 00 00 45 00 " //
667 + "0030 00 6a d4 31 00 00 ff 11 2a 3e ac 01 01 02 08 08 " //
668 + "0040 08 08 10 f6 10 f6 00 56 63 14 10 00 01 01 79 67 " //
669 + "0050 ff 75 a0 61 66 19 00 01 ac 01 01 02 00 01 0a 01 " //
670 + "0060 00 6f 00 01 c0 a8 88 33 00 20 00 01 08 08 08 08 " //
671 + "0070 00 00 00 0a 02 20 10 00 00 00 00 01 ac 01 01 02 " //
672 + "0080 01 64 ff 00 00 05 00 01 0a 01 00 6f 06 64 ff 00 " //
673 + "0090 00 05 00 01 c0 a8 88 33"));
675 oneOf(nps).putNotification(with(lispNotificationSaver));
676 handleMapRequestAsByteArray(mapRequestPacket);
680 private void stubMapRegister(final boolean setNotifyFromRegister) {
682 allowing(nps).putNotification(with(lispNotificationSaver));
683 } catch (InterruptedException e) {
685 will(new SimpleAction() {
688 public Object invoke(Invocation invocation) throws Throwable {
689 if (setNotifyFromRegister) {
690 MapNotifyBuilderHelper.setFromMapRegister(mapNotifyBuilder, lastMapRegister());
697 private void stubHandleRequest() {
699 allowing(nps).putNotification(wany(Notification.class));
700 } catch (InterruptedException e) {
704 private byte[] handleMapRequestAsByteArray(byte[] inPacket) {
705 handleMapRequestPacket(inPacket);
706 return lastMapReplyPacket().getData();
709 private byte[] handleMapRegisterAsByteArray(byte[] inPacket) {
710 handleMapRegisterPacket(inPacket);
711 return lastMapNotifyPacket().getData();
714 private DatagramPacket handleMapRequestPacket(byte[] inPacket) {
715 DatagramPacket dp = new DatagramPacket(inPacket, inPacket.length);
716 // Unless we explicitly set the source port, it will be -1, which breaks some tests
717 // This is till not the real port number, but it's better
718 dp.setPort(LispMessage.PORT_NUM);
719 testedLispService.handlePacket(dp);
720 return lastMapReplyPacket();
723 private DatagramPacket handleMapRegisterPacket(byte[] inPacket) {
724 DatagramPacket dp = new DatagramPacket(inPacket, inPacket.length);
725 // Unless we explicitly set the source port, it will be -1, which breaks some tests
726 // This is till not the real port number, but it's better
727 dp.setPort(LispMessage.PORT_NUM);
728 testedLispService.handlePacket(dp);
729 if (mapNotifyBuilder == null) {
732 return lastMapNotifyPacket();
736 private DatagramPacket handlePacket(byte[] inPacket) {
737 // TODO get from mock
738 testedLispService.handlePacket(new DatagramPacket(inPacket, inPacket.length));
742 private byte[] extractWSUdpByteArray(String wiresharkHex) {
743 final int HEADER_LEN = 42;
744 byte[] res = new byte[1000];
745 String[] split = wiresharkHex.split(" ");
747 for (String cur : split) {
749 if (cur.length() == 2) {
751 if (counter > HEADER_LEN) {
752 res[counter - HEADER_LEN - 1] = (byte) Integer.parseInt(cur, 16);
757 return Arrays.copyOf(res, counter - HEADER_LEN);
760 @Test(expected = LispMalformedPacketException.class)
761 public void mapRequest__NoIPITRRLOC() throws Exception {
762 mapRequestPacket = hexToByteBuffer("10 00 " //
763 + "02 " // This means 3 ITR - RLOCs
764 + "01 3d 8d 2a cd 39 c8 d6 08 00 00 " //
765 + "40 05 c0 a8 88 0a 01 02 " // MAC (ITR-RLOC #1 of 3)
766 + "40 05 00 00 00 00 00 00 " // MAC (ITR-RLOC #2 of 3)
767 + "40 05 11 22 34 56 78 90 " // MAC (ITR-RLOC #3 of 3)
768 + "00 20 00 01 01 02 03 04").array();
769 handleMapRequestPacket(mapRequestPacket);
774 // public void mapRequest__IPITRRLOCIsSecond() throws Exception {
775 // mapRequestPacket = hexToByteBuffer("10 00 " //
776 // + "01 " // This means 3 ITR - RLOCs
777 // + "01 3d 8d 2a cd 39 c8 d6 08 00 00 " //
778 // + "40 05 c0 a8 88 0a 01 02 " // MAC (ITR-RLOC #1 of 2)
779 // + "00 01 01 02 03 04 " // IP (ITR-RLOC #2 of 2)
780 // + "00 20 00 01 01 02 03 04").array();
781 // oneOf(nps).putNotification(with(lispNotificationSaver));
783 // DatagramPacket packet = handleMapRequestPacket(mapRequestPacket);
784 // assertEquals(2, lastMapRequest().getItrRlocs().size());
785 // assertEquals((new LispIpv4Address("1.2.3.4")).getAddress(),
786 // packet.getAddress());
791 // public void mapRequest__MULTIPLEIPITRRLOCs() throws Exception {
792 // mapRequestPacket = hexToByteBuffer("10 00 " //
793 // + "01 " // This means 3 ITR - RLOCs
794 // + "01 3d 8d 2a cd 39 c8 d6 08 00 00 " //
795 // + "00 01 01 02 03 04 " // IP (ITR-RLOC #1 of 2)
796 // + "00 01 c0 a8 88 0a " // MAC (ITR-RLOC #2 of 2)
797 // + "00 20 00 01 01 02 03 04").array();
798 // oneOf(nps).putNotification(with(lispNotificationSaver));
800 // DatagramPacket packet = handleMapRequestPacket(mapRequestPacket);
801 // assertEquals(2, lastMapRequest().getItrRloc().size());
802 // assertEquals((new LispIpv4Address("1.2.3.4")).getAddress(),
803 // packet.getAddress());