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.lispflowmapping.rev131031.AddMapping;
40 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.EidToLocatorRecord.Action;
41 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.LispAFIAddress;
42 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.MapRegister;
43 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.MapRequest;
44 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.RequestMapping;
45 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.eidrecords.EidRecord;
46 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.eidtolocatorrecords.EidToLocatorRecord;
47 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.eidtolocatorrecords.EidToLocatorRecordBuilder;
48 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.LispAddressContainer;
49 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.LispAddressContainerBuilder;
50 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.lispaddresscontainer.address.Ipv4;
51 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.lispaddresscontainer.address.Ipv4Builder;
52 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.lispaddresscontainer.address.Ipv6;
53 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.lispaddresscontainer.address.Ipv6Builder;
54 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.locatorrecords.LocatorRecord;
55 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.locatorrecords.LocatorRecordBuilder;
56 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.mapnotifymessage.MapNotifyBuilder;
57 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.mapreplymessage.MapReplyBuilder;
58 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
59 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
60 import org.opendaylight.yangtools.yang.binding.Notification;
62 public class LispSouthboundServiceTest extends BaseTestCase {
64 private LispSouthboundService testedLispService;
65 private NotificationProviderService nps;
66 private byte[] mapRequestPacket;
67 private byte[] mapRegisterPacket;
68 private ValueSaverAction<Notification> lispNotificationSaver;
69 // private ValueSaverAction<MapRegister> mapRegisterSaver;
70 // private ValueSaverAction<MapRequest> mapRequestSaver;
71 private MapNotifyBuilder mapNotifyBuilder;
72 private MapReplyBuilder mapReplyBuilder;
73 private EidToLocatorRecordBuilder eidToLocatorBuilder;
75 private interface MapReplyIpv4SingleLocatorPos {
78 int LOCATOR_COUNT = 16;
79 int EID_MASK_LEN = 17;
83 int LOCATOR_RBIT = 33;
87 private interface MapReplyIpv4SecondLocatorPos {
88 int FIRST_LOCATOR_IPV4_LENGTH = 12;
89 int LOC_AFI = MapReplyIpv4SingleLocatorPos.LOC_AFI + FIRST_LOCATOR_IPV4_LENGTH;
90 int LOCATOR_RBIT = MapReplyIpv4SingleLocatorPos.LOCATOR_RBIT + FIRST_LOCATOR_IPV4_LENGTH;
91 int LOCATOR = MapReplyIpv4SingleLocatorPos.LOCATOR + FIRST_LOCATOR_IPV4_LENGTH;
96 public void before() throws Exception {
98 // mapResolver = context.mock(IMapResolver.class);
99 // mapServer = context.mock(IMapServer.class);
100 testedLispService = new LispSouthboundService();
101 nps = context.mock(NotificationProviderService.class);
102 testedLispService.setNotificationProvider(nps);
103 lispNotificationSaver = new ValueSaverAction<Notification>();
104 // mapRegisterSaver = new ValueSaverAction<MapRegister>();
105 // mapRequestSaver = new ValueSaverAction<MapRequest>();
106 // SRC: 127.0.0.1:58560 to 127.0.0.1:4342
107 // LISP(Type = 8 - Encapsulated)
108 // IP: 192.168.136.10 -> 1.2.3.4
110 // LISP(Type = 1 Map-Request
114 // Source EID not present
115 // Nonce: 0x3d8d2acd39c8d608
116 // ITR-RLOC AFI=1 Address=192.168.136.10
117 // Record 1: 1.2.3.4/32
118 mapRequestPacket = extractWSUdpByteArray(new String("0000 00 00 00 00 00 00 00 00 00 00 00 00 08 00 45 00 " //
119 + "0010 00 58 00 00 40 00 40 11 3c 93 7f 00 00 01 7f 00 "
120 + "0020 00 01 e4 c0 10 f6 00 44 fe 57 80 00 00 00 45 00 "
121 + "0030 00 38 d4 31 00 00 ff 11 56 f3 c0 a8 88 0a 01 02 "
122 + "0040 03 04 dd b4 10 f6 00 24 ef 3a 10 00 00 01 3d 8d "
123 + "0050 2a cd 39 c8 d6 08 00 01 01 02 03 04 00 01 c0 a8 88 0a 00 20 " //
124 + "0060 00 01 01 02 03 04"));
125 mapReplyBuilder = new MapReplyBuilder();
126 mapReplyBuilder.setEidToLocatorRecord(new ArrayList<EidToLocatorRecord>());
127 mapReplyBuilder.setNonce((long) 0);
128 mapReplyBuilder.setEchoNonceEnabled(false);
129 mapReplyBuilder.setProbe(true);
130 mapReplyBuilder.setSecurityEnabled(true);
131 eidToLocatorBuilder = new EidToLocatorRecordBuilder();
132 String ip = "0.0.0.0";
133 eidToLocatorBuilder.setLispAddressContainer(getIPContainer(ip));
134 eidToLocatorBuilder.setLocatorRecord(new ArrayList<LocatorRecord>());
135 eidToLocatorBuilder.setRecordTtl(10);
136 eidToLocatorBuilder.setMapVersion((short) 0);
137 eidToLocatorBuilder.setMaskLength((short) 0);
138 eidToLocatorBuilder.setAction(Action.NativelyForward);
139 eidToLocatorBuilder.setAuthoritative(false);
140 // eidToLocatorBuilder.setPrefix(new LispIpv4Address(0));
141 // mapReply.addEidToLocator(eidToLocatorBuilder);
143 // IP: 192.168.136.10 -> 128.223.156.35
144 // UDP: 49289 -> 4342
145 // LISP(Type = 3 Map-Register, P=1, M=1
149 // AuthDataLength: 20 Data:
150 // e8:f5:0b:c5:c5:f2:b0:21:27:a8:21:41:04:f3:46:5a:a5:68:89:ec
151 // EID prefix: 153.16.254.1/32 (EID=0x9910FE01), TTL: 10, Authoritative,
153 // Local RLOC: 192.168.136.10 (RLOC=0xC0A8880A), Reachable,
154 // Priority/Weight: 1/100, Multicast Priority/Weight:
158 mapRegisterPacket = extractWSUdpByteArray(new String("0000 00 50 56 ee d1 4f 00 0c 29 7a ce 79 08 00 45 00 " //
159 + "0010 00 5c 00 00 40 00 40 11 d4 db c0 a8 88 0a 80 df "
160 + "0020 9c 23 d6 40 10 f6 00 48 59 a4 38 00 01 01 00 00 "
161 + "0030 00 00 00 00 00 00 00 01 00 14 0e a4 c6 d8 a4 06 "
162 + "0040 71 7c 33 a4 5c 4a 83 1c de 74 53 03 0c ad 00 00 "
163 + "0050 00 0a 01 20 10 00 00 00 00 01 99 10 fe 01 01 64 " //
164 + "0060 ff 00 00 05 00 01 c0 a8 88 0a"));
165 mapNotifyBuilder = new MapNotifyBuilder();
166 mapNotifyBuilder.setAuthenticationData(new byte[0]);
169 private LispAddressContainer getIPContainer(String ip) {
170 return new LispAddressContainerBuilder().setAddress(getIP(ip)).build();
173 private Ipv4 getIP(String ip) {
174 return new Ipv4Builder().setIpv4Address(new Ipv4Address(ip)).setAfi((short) 1).build();
177 private Ipv6 getIPV6(String ip) {
178 return new Ipv6Builder().setIpv6Address(new Ipv6Address(ip)).setAfi((short) 2).build();
183 public void todos() throws Exception {
185 // TODO: MapRequest: usage of Map-Reply-Record in MapRequest packet.
186 // TODO: Non-Encapsulated packets
189 @Test(expected = LispMalformedPacketException.class)
190 public void mapRegister__IllegalPacket() throws Exception {
191 mapRegisterPacket = extractWSUdpByteArray(new String("0000 00 0c 29 7a ce 8d 00 0c 29 e4 ef 70 08 00 45 00 "
192 + "0010 00 68 00 00 40 00 40 11 26 15 0a 01 00 6e 0a 01 " //
193 + "0020 00 01 10 f6 10 f6 00 54 03 3b 38 00 01 01 00 00 "));
195 handleMapRegisterPacket(mapRegisterPacket);
198 @Test(expected = LispMalformedPacketException.class)
199 public void mapRequest__IllegalPacket() throws Exception {
200 mapRequestPacket = extractWSUdpByteArray(new String("0000 00 00 00 00 00 00 00 00 00 00 00 00 08 00 45 00 " //
201 + "0010 00 58 00 00 40 00 40 11 3c 93 7f 00 00 01 7f 00 "
202 + "0020 00 01 e4 c0 10 f6 00 44 fe 57 80 00 00 00 45 00 "
203 + "0030 00 38 d4 31 00 00 ff 11 56 f3 c0 a8 88 0a 01 02 " //
204 + "0040 03 04 dd b4 10 f6 00 24 ef 3a 10 00 00 01 3d 8d "));
205 handleMapRequestPacket(mapRequestPacket);
208 @Test(expected = LispMalformedPacketException.class)
209 public void mapRequest__IllegalEncapsulatedPacket() throws Exception {
210 mapRequestPacket = extractWSUdpByteArray(new String("0000 00 00 00 00 00 00 00 00 00 00 00 00 08 00 45 00 " //
211 + "0010 00 58 00 00 40 00 40 11 3c 93 7f 00 00 01 7f 00 " //
212 + "0020 00 01 e4 c0 10 f6 00 44 fe 57 80 00 00 00 45 00 "));
213 handleMapRequestPacket(mapRequestPacket);
216 private MapRegister lastMapRegister() {
217 assertTrue(lispNotificationSaver.lastValue instanceof AddMapping);
218 AddMapping lastValue = (AddMapping) lispNotificationSaver.lastValue;
219 return lastValue.getMapRegister();
222 private MapRequest lastMapRequest() {
223 RequestMapping lastValue = (RequestMapping) lispNotificationSaver.lastValue;
224 return lastValue.getMapRequest();
228 public void mapRegister__TwoRlocs() throws Exception {
230 // EID prefix: 172.1.1.2/32, TTL: 10, Authoritative, No-Action
231 // Local RLOC: 10.1.0.110, Reachable, Priority/Weight: 1/100, Multicast
232 // Priority/Weight: 255/0
233 // Local RLOC: 192.168.136.51, Reachable, Priority/Weight: 6/100,
234 // Multicast Priority/Weight: 255/0
235 mapRegisterPacket = extractWSUdpByteArray(new String("0000 00 0c 29 7a ce 8d 00 0c 29 e4 ef 70 08 00 45 00 "
236 + "0010 00 68 00 00 40 00 40 11 26 15 0a 01 00 6e 0a 01 " //
237 + "0020 00 01 10 f6 10 f6 00 54 03 3b 38 00 01 01 00 00 " //
238 + "0030 00 00 00 00 00 00 00 01 00 14 ae d8 7b d4 9c 59 " //
239 + "0040 e9 35 75 6e f1 29 27 a3 45 20 96 06 c2 e1 00 00 " //
240 + "0050 00 0a 02 20 10 00 00 00 00 01 ac 01 01 02 01 64 " //
241 + "0060 ff 00 00 05 00 01 0a 01 00 6e 06 64 ff 00 00 05 " //
242 + "0070 00 01 c0 a8 88 33"));
244 oneOf(nps).publish(with(lispNotificationSaver));
246 handleMapRegisterPacket(mapRegisterPacket);
248 List<EidToLocatorRecord> eidRecords = lastMapRegister().getEidToLocatorRecord();
249 assertEquals(1, eidRecords.size());
250 EidToLocatorRecord eidRecord = eidRecords.get(0);
251 assertEquals(2, eidRecord.getLocatorRecord().size());
252 assertEquals(getIPContainer("10.1.0.110"), eidRecord.getLocatorRecord().get(0).getLispAddressContainer());
253 assertEquals(getIPContainer("192.168.136.51"), eidRecord.getLocatorRecord().get(1).getLispAddressContainer());
257 public void mapRegister__Ipv6Rloc() throws Exception {
258 // P bit (Proxy-Map-Reply): Set
259 // M bit (Want-Map-Notify): Set
264 // Authentication Data: 5bc4d44a57e2a55d577a6f89779c004f5da713fb
265 // EID prefix: 2610:d0:ffff:192::1/128, TTL: 10, Authoritative,
267 // Local RLOC: 10.0.58.156, Reachable, Priority/Weight: 1/100, Multicast
268 // Priority/Weight: 255/0
270 mapRegisterPacket = extractWSUdpByteArray(new String("0000 00 0c 29 34 3e 1b 00 0c 29 f6 d6 0d 08 00 45 00 " //
271 + "0010 00 68 00 00 40 00 40 11 ea c3 0a 00 3a 9c 0a 00 " //
272 + "0020 01 26 10 f6 10 f6 00 54 f5 9a 38 00 03 01 00 00 " //
273 + "0030 00 00 00 00 00 00 00 01 00 14 22 97 ff 61 ec d8 " //
274 + "0040 0f 91 c6 c4 01 ef 7f bb 77 58 39 5c 92 23 00 00 " //
275 + "0050 00 0a 01 80 10 00 00 00 00 02 26 10 00 d0 ff ff " //
276 + "0060 01 92 00 00 00 00 00 00 00 01 01 64 ff 00 00 05 " //
277 + "0070 00 01 0a 00 3a 9c"));
279 oneOf(nps).publish(with(lispNotificationSaver));
281 handleMapRegisterPacket(mapRegisterPacket);
283 EidToLocatorRecord eidToLocatorRecord = lastMapRegister().getEidToLocatorRecord().get(0);
284 assertEquals(getIPV6("2610:d0:ffff:192:0:0:0:1"), LispAFIConvertor.toAFI(eidToLocatorRecord.getLispAddressContainer()));
285 assertEquals(AddressFamilyNumberEnum.IP6,
286 AddressFamilyNumberEnum.valueOf(LispAFIConvertor.toAFI(eidToLocatorRecord.getLispAddressContainer()).getAfi()));
288 assertEquals(getIP("10.0.58.156"), LispAFIConvertor.toAFI(eidToLocatorRecord.getLocatorRecord().get(0).getLispAddressContainer()));
292 public void mapRegister__VerifyBasicFields() throws Exception {
293 oneOf(nps).publish(with(lispNotificationSaver));
294 handleMapRegisterPacket(mapRegisterPacket);
296 EidToLocatorRecord eidToLocator = lastMapRegister().getEidToLocatorRecord().get(0);
297 assertEquals(getIP("153.16.254.1"), LispAFIConvertor.toAFI(eidToLocator.getLispAddressContainer()));
299 assertEquals(1, eidToLocator.getLocatorRecord().size());
300 assertEquals(getIP("192.168.136.10"), LispAFIConvertor.toAFI(eidToLocator.getLocatorRecord().get(0).getLispAddressContainer()));
305 public void mapRegister__NoResponseFromMapServerShouldReturnNullPacket() throws Exception {
306 oneOf(nps).publish(with(lispNotificationSaver));
307 mapNotifyBuilder = null;
309 assertNull(handleMapRegisterPacket(mapRegisterPacket));
313 public void mapRegister__NonSetMBit() throws Exception {
314 byte[] registerWithNonSetMBit = extractWSUdpByteArray(new String("0000 00 50 56 ee d1 4f 00 0c 29 7a ce 79 08 00 45 00 " //
315 + "0010 00 5c 00 00 40 00 40 11 d4 db c0 a8 88 0a 80 df " //
316 + "0020 9c 23 d6 40 10 f6 00 48 59 a4 38 00 00 01 00 00 "
317 + "0030 00 00 00 00 00 00 00 01 00 14 79 d1 44 66 19 99 "
318 + "0040 83 63 a7 79 6e f0 40 97 54 26 3a 44 b4 eb 00 00 " //
319 + "0050 00 0a 01 20 10 00 00 00 00 01 99 10 fe 01 01 64 " //
320 + "0060 ff 00 00 05 00 01 c0 a8 88 0a"));
321 stubMapRegister(true);
323 handleMapRegisterPacket(registerWithNonSetMBit);
325 assertFalse(lastMapRegister().isWantMapNotify());
329 public void mapRegister__NonSetMBitWithNonZeroReservedBits() throws Exception {
330 byte[] registerWithNonSetMBit = extractWSUdpByteArray(new String("0000 00 50 56 ee d1 4f 00 0c 29 7a ce 79 08 00 45 00 " //
331 + "0010 00 5c 00 00 40 00 40 11 d4 db c0 a8 88 0a 80 df "
332 + "0020 9c 23 d6 40 10 f6 00 48 59 a4 38 00 02 01 00 00 "
333 + "0030 00 00 00 00 00 00 00 01 00 14 c0 c7 c5 2f 57 f6 "
334 + "0040 e7 20 25 3d e8 b2 07 e2 63 de 62 2b 7a 20 00 00 "
335 + "0050 00 0a 01 20 10 00 00 00 00 01 99 10 fe 01 01 64 " //
336 + "0060 ff 00 00 05 00 01 c0 a8 88 0a"));
337 stubMapRegister(true);
339 handleMapRegisterPacket(registerWithNonSetMBit);
340 assertFalse(lastMapRegister().isWantMapNotify());
344 public void mapRegister__SetMBitWithNonZeroReservedBits() throws Exception {
345 byte[] registerWithNonSetMBit = extractWSUdpByteArray(new String("0000 00 50 56 ee d1 4f 00 0c 29 7a ce 79 08 00 45 00 " //
346 + "0010 00 5c 00 00 40 00 40 11 d4 db c0 a8 88 0a 80 df "
347 + "0020 9c 23 d6 40 10 f6 00 48 59 a4 38 00 03 01 00 00 "
348 + "0030 00 00 00 00 00 00 00 01 00 14 a2 72 40 7b 1a ae "
349 + "0040 4e 6b e2 e5 e1 01 40 8a c9 e1 d1 80 cb 72 00 00 "
350 + "0050 00 0a 01 20 10 00 00 00 00 01 99 10 fe 01 01 64 " //
351 + "0060 ff 00 00 05 00 01 c0 a8 88 0a"));
352 stubMapRegister(true);
354 handleMapRegisterPacket(registerWithNonSetMBit);
355 assertTrue(lastMapRegister().isWantMapNotify());
360 public void mapRegisterAndNotify__ValidExtraDataParsedSuccessfully() throws Exception {
361 byte[] extraDataPacket = new byte[mapRegisterPacket.length + 3];
362 extraDataPacket[mapRegisterPacket.length] = 0x9;
363 System.arraycopy(mapRegisterPacket, 0, extraDataPacket, 0, mapRegisterPacket.length);
364 stubMapRegister(true);
366 DatagramPacket dp = new DatagramPacket(extraDataPacket, extraDataPacket.length);
367 dp.setLength(mapRegisterPacket.length);
368 testedLispService.handlePacket(dp);
369 // Check map register fields.
371 // byte[] notifyResult = testedLispService.handlePacket(dp).getData();
372 byte[] notifyResult = lastMapNotifyPacket().getData();
373 assertEquals(mapRegisterPacket.length, notifyResult.length);
377 private DatagramPacket lastMapReplyPacket() {
378 ByteBuffer serialize = MapReplySerializer.getInstance().serialize(mapReplyBuilder.build());
379 return new DatagramPacket(serialize.array(), serialize.array().length);
382 private DatagramPacket lastMapNotifyPacket() {
383 if (mapNotifyBuilder.getEidToLocatorRecord() == null) {
384 mapNotifyBuilder.setEidToLocatorRecord(new ArrayList<EidToLocatorRecord>());
386 mapNotifyBuilder.getEidToLocatorRecord().add(eidToLocatorBuilder.build());
387 mapNotifyBuilder.setNonce((long) 0);
388 mapNotifyBuilder.setKeyId((short) 0);
389 mapNotifyBuilder.setAuthenticationData(new byte[0]);
390 ByteBuffer serialize = MapNotifySerializer.getInstance().serialize(mapNotifyBuilder.build());
391 return new DatagramPacket(serialize.array(), serialize.array().length);
396 public void mapNotify__VerifyBasicFields() throws Exception {
397 byte registerType = mapRegisterPacket[0];
398 assertEquals(LispMessageEnum.MapRegister.getValue(), registerType >> 4);
400 stubMapRegister(true);
402 byte[] result = handleMapRegisterAsByteArray(mapRegisterPacket);
404 assertEquals(mapRegisterPacket.length, result.length);
406 byte expectedType = (byte) (LispMessageEnum.MapNotify.getValue() << 4);
407 assertHexEquals(expectedType, result[0]);
408 assertHexEquals((byte) 0x00, result[1]);
409 assertHexEquals((byte) 0x00, result[2]);
411 byte[] registerWithoutTypeWithoutAuthenticationData = ArrayUtils.addAll(Arrays.copyOfRange(mapRegisterPacket, 3, 16),
412 Arrays.copyOfRange(mapRegisterPacket, 36, mapRegisterPacket.length));
413 byte[] notifyWithoutTypeWithOutAuthenticationData = ArrayUtils.addAll(Arrays.copyOfRange(result, 3, 16),
414 Arrays.copyOfRange(result, 36, result.length));
415 ArrayAssert.assertEquals(registerWithoutTypeWithoutAuthenticationData, notifyWithoutTypeWithOutAuthenticationData);
420 public void mapNotify__VerifyPort() throws Exception {
421 stubMapRegister(true);
423 DatagramPacket notifyPacket = handleMapRegisterPacket(mapRegisterPacket);
424 assertEquals(LispMessage.PORT_NUM, notifyPacket.getPort());
428 public void mapRequest__VerifyBasicFields() throws Exception {
429 oneOf(nps).publish(with(lispNotificationSaver));
430 handleMapRequestAsByteArray(mapRequestPacket);
431 List<EidRecord> eids = lastMapRequest().getEidRecord();
432 assertEquals(1, eids.size());
433 LispAFIAddress lispAddress = LispAFIConvertor.toAFI(eids.get(0).getLispAddressContainer());
434 assertTrue(lispAddress instanceof Ipv4);
435 assertEquals(getIP("1.2.3.4"), ((Ipv4) lispAddress));
436 assertEquals((byte) 0x20, eids.get(0).getMask().byteValue());
437 assertEquals(0x3d8d2acd39c8d608L, lastMapRequest().getNonce().longValue());
438 // assertEquals(AddressFamilyNumberEnum.RESERVED,
439 // AddressFamilyNumberEnum.valueOf(LispAFIToContainerConvertorFactory.toAFI(
440 // lastMapRequest().getSourceEid().getLispAddressContainer()).getAfi()));
444 public void mapRequest__Ipv6Eid() throws Exception {
445 // Internet Protocol Version 6, Src: 2610:d0:ffff:192::1
446 // (2610:d0:ffff:192::1), Dst: 2610:d0:ffff:192::2
447 // (2610:d0:ffff:192::2)
450 // Source EID: 2610:d0:ffff:192::1 (2610:d0:ffff:192::1)
451 // ITR-RLOC 1: 10.0.58.156
452 // Record 1: 2610:d0:ffff:192::2/128
453 // Map-Reply Record: EID prefix: 2610:d0:ffff:192::1/128, TTL: 10,
454 // Authoritative, No-Action
456 mapRequestPacket = extractWSUdpByteArray(new String("0000 00 0c 29 34 3e 1b 00 0c 29 f6 d6 0d 08 00 45 00 " //
457 + "0010 00 b0 00 00 40 00 40 11 ea 7b 0a 00 3a 9c 0a 00 "
458 + "0020 01 26 10 f6 10 f6 00 9c 9b 19 80 00 00 00 60 00 "
459 + "0030 00 00 00 68 11 ff 26 10 00 d0 ff ff 01 92 00 00 "
460 + "0040 00 00 00 00 00 01 26 10 00 d0 ff ff 01 92 00 00 "
461 + "0050 00 00 00 00 00 02 10 f6 10 f6 00 68 94 8b 10 00 "
462 + "0060 00 01 ff f5 bf 5d 7b 75 93 e6 00 02 26 10 00 d0 "
463 + "0070 ff ff 01 92 00 00 00 00 00 00 00 01 00 01 0a 00 "
464 + "0080 3a 9c 00 80 00 02 26 10 00 d0 ff ff 01 92 00 00 "
465 + "0090 00 00 00 00 00 02 00 00 00 0a 01 80 10 00 00 00 " //
466 + "00a0 00 02 26 10 00 d0 ff ff 01 92 00 00 00 00 00 00 " //
467 + "00b0 00 01 01 64 ff 00 00 05 00 01 0a 00 3a 9c"));
469 oneOf(nps).publish(with(lispNotificationSaver));
472 handleMapRequestAsByteArray(mapRequestPacket);
473 assertEquals(getIPV6("2610:d0:ffff:192:0:0:0:1"), LispAFIConvertor.toAFI(lastMapRequest().getSourceEid().getLispAddressContainer()));
474 assertEquals(getIPV6("2610:d0:ffff:192:0:0:0:2"), LispAFIConvertor.toAFI(lastMapRequest().getEidRecord().get(0).getLispAddressContainer()));
479 public void mapRequest__UsesIpv6EncapsulatedUdpPort() throws Exception {
480 // Internet Protocol Version 6, Src: 2610:d0:ffff:192::1
481 // (2610:d0:ffff:192::1), Dst: 2610:d0:ffff:192::2
482 // (2610:d0:ffff:192::2)
483 // encapsulated UDP source port: 4342
485 mapRequestPacket = extractWSUdpByteArray(new String("0000 00 0c 29 34 3e 1b 00 0c 29 f6 d6 0d 08 00 45 00 " //
486 + "0010 00 b0 00 00 40 00 40 11 ea 7b 0a 00 3a 9c 0a 00 "
487 + "0020 01 26 10 f6 10 f6 00 9c 9b 19 80 00 00 00 60 00 "
488 + "0030 00 00 00 68 11 ff 26 10 00 d0 ff ff 01 92 00 00 "
489 + "0040 00 00 00 00 00 01 26 10 00 d0 ff ff 01 92 00 00 "
490 + "0050 00 00 00 00 00 02 10 f6 10 f6 00 68 94 8b 14 00 "
491 + "0060 00 01 ff f5 bf 5d 7b 75 93 e6 00 02 26 10 00 d0 "
492 + "0070 ff ff 01 92 00 00 00 00 00 00 00 01 00 01 0a 00 "
493 + "0080 3a 9c 00 80 00 02 26 10 00 d0 ff ff 01 92 00 00 "
494 + "0090 00 00 00 00 00 02 00 00 00 0a 01 80 10 00 00 00 " //
495 + "00a0 00 02 26 10 00 d0 ff ff 01 92 00 00 00 00 00 00 " //
496 + "00b0 00 01 01 64 ff 00 00 05 00 01 0a 00 3a 9c"));
497 oneOf(nps).publish(with(lispNotificationSaver));
500 DatagramPacket replyPacket = handleMapRequestPacket(mapRequestPacket);
501 assertEquals(4342, replyPacket.getPort());
505 public void mapRequest__WithSourceEid() throws Exception {
506 // encapsulated LISP packet
507 // Source EID = 153.16.254.1
509 mapRequestPacket = extractWSUdpByteArray(new String("0000 00 0c 29 7a ce 83 00 15 17 c6 4a c9 08 00 45 00 " //
510 + "0010 00 78 00 00 40 00 3e 11 ec b1 0a 00 01 26 0a 00 "
511 + "0020 3a 9e 10 f6 10 f6 00 64 c3 a5 80 00 00 00 45 00 "
512 + "0030 00 58 d4 31 00 00 ff 11 31 89 99 10 fe 01 0a 00 "
513 + "0040 14 c8 10 f6 10 f6 00 44 84 ee 10 00 00 01 ba f9 "
514 + "0050 ff 53 27 36 38 3a 00 01 99 10 fe 01 00 01 0a 00 "
515 + "0060 01 26 00 20 00 01 0a 00 14 c8 00 00 00 0a 01 20 "
516 + "0070 10 00 00 00 00 01 99 10 fe 01 01 64 ff 00 00 05 " //
517 + "0080 00 01 0a 00 01 26"));
519 oneOf(nps).publish(with(lispNotificationSaver));
522 handleMapRequestAsByteArray(mapRequestPacket);
523 Assert.assertNotEquals(AddressFamilyNumberEnum.IP, LispAFIConvertor.toAFI(lastMapRequest().getSourceEid().getLispAddressContainer()));
529 public void mapReply__VerifyBasicIPv4Fields() throws Exception {
530 eidToLocatorBuilder.setMaskLength((short) 0x20).setLispAddressContainer(LispAFIConvertor.toContainer(getIP("10.0.20.200")));
531 mapReplyBuilder.setNonce(0x3d8d2acd39c8d608L);
535 byte[] result = handleMapRequestAsByteArray(mapRequestPacket);
537 assertEquals(28, result.length);
539 byte expectedLispMessageType = 2;
540 assertEquals(expectedLispMessageType, (byte) (result[LispMessage.Pos.TYPE] >> 4));
541 assertEquals(0x3d8d2acd39c8d608L, ByteUtil.getLong(result, MapReplyIpv4SingleLocatorPos.NONCE));
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.IP.getIanaCode(), ByteUtil.getShort(result, MapReplyIpv4SingleLocatorPos.AFI_TYPE));
548 assertEquals(0x0a0014c8, ByteUtil.getInt(result, MapReplyIpv4SingleLocatorPos.EID_PREFIX));
553 public void mapReply__VerifyBasicIPv6() throws Exception {
554 eidToLocatorBuilder.setMaskLength((short) 0x80).setLispAddressContainer(LispAFIConvertor.toContainer(getIPV6("0:0:0:0:0:0:0:1")));
558 byte[] result = handleMapRequestAsByteArray(mapRequestPacket);
560 assertEquals(40, result.length);
562 byte expectedRecordCount = (byte) 1;
563 assertEquals(expectedRecordCount, result[MapReplyIpv4SingleLocatorPos.RECORD_COUNT]);
565 assertEquals(eidToLocatorBuilder.getMaskLength().byteValue(), result[MapReplyIpv4SingleLocatorPos.EID_MASK_LEN]);
566 assertEquals(AddressFamilyNumberEnum.IP6.getIanaCode(), ByteUtil.getShort(result, MapReplyIpv4SingleLocatorPos.AFI_TYPE));
567 byte[] expectedIpv6 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
569 ArrayAssert.assertEquals(expectedIpv6, Arrays.copyOfRange(result, 24, 40));
574 public void mapReply__VerifyIPv6EidAndLocator() throws Exception {
575 eidToLocatorBuilder.setLispAddressContainer(LispAFIConvertor.toContainer(getIPV6("0:0:0:0:0:0:0:1")));
576 eidToLocatorBuilder.getLocatorRecord().add(
577 new LocatorRecordBuilder().setLispAddressContainer(LispAFIConvertor.toContainer(getIPV6("0:0:0:0:0:0:0:2"))).build());
581 byte[] result = handleMapRequestAsByteArray(mapRequestPacket);
583 assertEquals(64, result.length);
585 byte[] expectedIpv6Eid = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
586 ArrayAssert.assertEquals(expectedIpv6Eid, Arrays.copyOfRange(result, 24, 40));
588 byte[] expectedIpv6Rloc = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2 };
589 ArrayAssert.assertEquals(expectedIpv6Rloc, Arrays.copyOfRange(result, 48, 64));
594 public void mapReply__UseEncapsulatedUdpPort() throws Exception {
597 assertEquals(LispMessage.PORT_NUM, handleMapRequestPacket(mapRequestPacket).getPort());
602 public void mapReply__WithNonRoutableSingleLocator() throws Exception {
603 eidToLocatorBuilder.setMaskLength((short) 0x20).setLispAddressContainer(LispAFIConvertor.toContainer(getIP("10.0.20.200")));
604 eidToLocatorBuilder.getLocatorRecord().add(
605 new LocatorRecordBuilder().setRouted(false).setLispAddressContainer(LispAFIConvertor.toContainer(getIP("4.3.2.1"))).build());
608 byte[] result = handleMapRequestAsByteArray(mapRequestPacket);
609 assertEquals(0x00, result[MapReplyIpv4SingleLocatorPos.LOCATOR_RBIT] & 0x01);
614 public void mapReply__WithSingleLocator() throws Exception {
615 eidToLocatorBuilder.setMaskLength((short) 0x20)//
616 .setLispAddressContainer(LispAFIConvertor.toContainer(getIP("10.0.20.200")));
617 eidToLocatorBuilder.getLocatorRecord().add(
618 new LocatorRecordBuilder().setRouted(true).setLispAddressContainer(LispAFIConvertor.toContainer(getIP("4.3.2.1"))).build());
621 byte[] result = handleMapRequestAsByteArray(mapRequestPacket);
622 assertEquals(40, result.length);
624 byte expectedLocCount = 1;
625 assertEquals(expectedLocCount, result[MapReplyIpv4SingleLocatorPos.LOCATOR_COUNT]);
627 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);
635 public void mapReply__WithMultipleLocator() throws Exception {
636 eidToLocatorBuilder.getLocatorRecord().add(
637 new LocatorRecordBuilder().setRouted(true).setLispAddressContainer(LispAFIConvertor.toContainer(getIP("4.3.2.1"))).build());
638 eidToLocatorBuilder.getLocatorRecord().add(
639 new LocatorRecordBuilder().setRouted(true).setLispAddressContainer(LispAFIConvertor.toContainer(getIPV6("0:0:0:0:0:0:0:1"))).build());
642 byte[] result = handleMapRequestAsByteArray(mapRequestPacket);
643 assertEquals(64, result.length);
645 assertEquals(2, result[MapReplyIpv4SingleLocatorPos.LOCATOR_COUNT]);
647 assertEquals(AddressFamilyNumberEnum.IP.getIanaCode(), ByteUtil.getShort(result, MapReplyIpv4SingleLocatorPos.LOC_AFI));
648 assertEquals(0x04030201, ByteUtil.getInt(result, MapReplyIpv4SingleLocatorPos.LOCATOR));
649 assertEquals(0x01, result[MapReplyIpv4SingleLocatorPos.LOCATOR_RBIT] & 0x01);
651 assertEquals(AddressFamilyNumberEnum.IP6.getIanaCode(), ByteUtil.getShort(result, MapReplyIpv4SecondLocatorPos.LOC_AFI));
653 byte[] expectedIpv6Rloc = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
654 ArrayAssert.assertEquals(expectedIpv6Rloc,
655 Arrays.copyOfRange(result, MapReplyIpv4SecondLocatorPos.LOCATOR, MapReplyIpv4SecondLocatorPos.LOCATOR + 16));
657 assertEquals(0x01, result[MapReplyIpv4SecondLocatorPos.LOCATOR_RBIT] & 0x01);
661 public void handleUnknownLispMessage() throws Exception {
662 // IP: 192.168.136.10 -> 128.223.156.35
663 // UDP: 49289 -> 4342
664 // LISP(Type = 14 UNKNOWN!!!, P=1, M=1
666 byte[] unknownTypePacket = extractWSUdpByteArray(new String("0000 00 50 56 ee d1 4f 00 0c 29 7a ce 79 08 00 45 00 " //
667 + "0010 00 5c 00 00 40 00 40 11 d4 db c0 a8 88 0a 80 df "
668 + "0020 9c 23 d6 40 10 f6 00 48 59 a4 F8 00 01 01 00 00 "
669 + "0030 00 00 00 00 00 00 00 01 00 14 e8 f5 0b c5 c5 f2 "
670 + "0040 b0 21 27 a8 21 41 04 f3 46 5a a5 68 89 ec 00 00 "
671 + "0050 00 0a 01 20 10 00 00 00 00 01 99 10 fe 01 01 64 " //
672 + "0060 ff 00 00 05 00 01 c0 a8 88 0a"));
673 assertNull(handlePacket(unknownTypePacket));
677 public void mapRequest__MultipleItrRlocs() throws Exception {
678 // this is what LISPmob sends when configured multiple RLOCs for single
680 // ITR-RLOC 1: 10.1.0.111
681 // ITR-RLOC 2: 192.168.136.51
683 mapRequestPacket = extractWSUdpByteArray(new String("0000 00 0c 29 7a ce 8d 00 0c 29 e4 ef 70 08 00 45 00 "
684 + "0010 00 8a 00 00 40 00 40 11 25 f2 0a 01 00 6f 0a 01 " //
685 + "0020 00 01 10 f6 10 f6 00 76 06 1f 80 00 00 00 45 00 " //
686 + "0030 00 6a d4 31 00 00 ff 11 2a 3e ac 01 01 02 08 08 " //
687 + "0040 08 08 10 f6 10 f6 00 56 63 14 10 00 01 01 79 67 " //
688 + "0050 ff 75 a0 61 66 19 00 01 ac 01 01 02 00 01 0a 01 " //
689 + "0060 00 6f 00 01 c0 a8 88 33 00 20 00 01 08 08 08 08 " //
690 + "0070 00 00 00 0a 02 20 10 00 00 00 00 01 ac 01 01 02 " //
691 + "0080 01 64 ff 00 00 05 00 01 0a 01 00 6f 06 64 ff 00 " //
692 + "0090 00 05 00 01 c0 a8 88 33"));
694 oneOf(nps).publish(with(lispNotificationSaver));
695 handleMapRequestAsByteArray(mapRequestPacket);
699 private void stubMapRegister(final boolean setNotifyFromRegister) {
700 allowing(nps).publish(with(lispNotificationSaver));
701 will(new SimpleAction() {
704 public Object invoke(Invocation invocation) throws Throwable {
705 if (setNotifyFromRegister) {
706 MapNotifyBuilderHelper.setFromMapRegister(mapNotifyBuilder, lastMapRegister());
713 private void stubHandleRequest() {
714 allowing(nps).publish(wany(Notification.class));
717 private byte[] handleMapRequestAsByteArray(byte[] inPacket) {
718 handleMapRequestPacket(inPacket);
719 return lastMapReplyPacket().getData();
722 private byte[] handleMapRegisterAsByteArray(byte[] inPacket) {
723 handleMapRegisterPacket(inPacket);
724 return lastMapNotifyPacket().getData();
727 private DatagramPacket handleMapRequestPacket(byte[] inPacket) {
728 DatagramPacket dp = new DatagramPacket(inPacket, inPacket.length);
729 // Unless we explicitly set the source port, it will be -1, which breaks some tests
730 // This is till not the real port number, but it's better
731 dp.setPort(LispMessage.PORT_NUM);
732 testedLispService.handlePacket(dp);
733 return lastMapReplyPacket();
736 private DatagramPacket handleMapRegisterPacket(byte[] inPacket) {
737 DatagramPacket dp = new DatagramPacket(inPacket, inPacket.length);
738 // Unless we explicitly set the source port, it will be -1, which breaks some tests
739 // This is till not the real port number, but it's better
740 dp.setPort(LispMessage.PORT_NUM);
741 testedLispService.handlePacket(dp);
742 if (mapNotifyBuilder == null) {
745 return lastMapNotifyPacket();
749 private DatagramPacket handlePacket(byte[] inPacket) {
750 // TODO get from mock
751 testedLispService.handlePacket(new DatagramPacket(inPacket, inPacket.length));
755 private byte[] extractWSUdpByteArray(String wiresharkHex) {
756 final int HEADER_LEN = 42;
757 byte[] res = new byte[1000];
758 String[] split = wiresharkHex.split(" ");
760 for (String cur : split) {
762 if (cur.length() == 2) {
764 if (counter > HEADER_LEN) {
765 res[counter - HEADER_LEN - 1] = (byte) Integer.parseInt(cur, 16);
770 return Arrays.copyOf(res, counter - HEADER_LEN);
773 @Test(expected = LispMalformedPacketException.class)
774 public void mapRequest__NoIPITRRLOC() throws Exception {
775 mapRequestPacket = hexToByteBuffer("10 00 " //
776 + "02 " // 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 3)
779 + "40 05 00 00 00 00 00 00 " // MAC (ITR-RLOC #2 of 3)
780 + "40 05 11 22 34 56 78 90 " // MAC (ITR-RLOC #3 of 3)
781 + "00 20 00 01 01 02 03 04").array();
782 handleMapRequestPacket(mapRequestPacket);
787 // public void mapRequest__IPITRRLOCIsSecond() throws Exception {
788 // mapRequestPacket = hexToByteBuffer("10 00 " //
789 // + "01 " // This means 3 ITR - RLOCs
790 // + "01 3d 8d 2a cd 39 c8 d6 08 00 00 " //
791 // + "40 05 c0 a8 88 0a 01 02 " // MAC (ITR-RLOC #1 of 2)
792 // + "00 01 01 02 03 04 " // IP (ITR-RLOC #2 of 2)
793 // + "00 20 00 01 01 02 03 04").array();
794 // oneOf(nps).publish(with(lispNotificationSaver));
796 // DatagramPacket packet = handleMapRequestPacket(mapRequestPacket);
797 // assertEquals(2, lastMapRequest().getItrRlocs().size());
798 // assertEquals((new LispIpv4Address("1.2.3.4")).getAddress(),
799 // packet.getAddress());
804 // public void mapRequest__MULTIPLEIPITRRLOCs() throws Exception {
805 // mapRequestPacket = hexToByteBuffer("10 00 " //
806 // + "01 " // This means 3 ITR - RLOCs
807 // + "01 3d 8d 2a cd 39 c8 d6 08 00 00 " //
808 // + "00 01 01 02 03 04 " // IP (ITR-RLOC #1 of 2)
809 // + "00 01 c0 a8 88 0a " // MAC (ITR-RLOC #2 of 2)
810 // + "00 20 00 01 01 02 03 04").array();
811 // oneOf(nps).publish(with(lispNotificationSaver));
813 // DatagramPacket packet = handleMapRequestPacket(mapRequestPacket);
814 // assertEquals(2, lastMapRequest().getItrRloc().size());
815 // assertEquals((new LispIpv4Address("1.2.3.4")).getAddress(),
816 // packet.getAddress());