2 * Copyright (c) 2013 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.LispAFIAddress;
41 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.MapRegister;
42 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.MapRequest;
43 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.RequestMapping;
44 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.eidrecords.EidRecord;
45 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.eidtolocatorrecords.EidToLocatorRecord;
46 import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.eidtolocatorrecords.EidToLocatorRecord.Action;
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 System.out.println(((Ipv4) LispAFIConvertor.toAFI(eidToLocator.getLispAddressContainer())).getIpv4Address().getValue());
298 assertEquals(getIP("153.16.254.1"), LispAFIConvertor.toAFI(eidToLocator.getLispAddressContainer()));
300 assertEquals(1, eidToLocator.getLocatorRecord().size());
301 assertEquals(getIP("192.168.136.10"), LispAFIConvertor.toAFI(eidToLocator.getLocatorRecord().get(0).getLispAddressContainer()));
306 public void mapRegister__NoResponseFromMapServerShouldReturnNullPacket() throws Exception {
307 oneOf(nps).publish(with(lispNotificationSaver));
308 mapNotifyBuilder = null;
310 assertNull(handleMapRegisterPacket(mapRegisterPacket));
314 public void mapRegister__NonSetMBit() throws Exception {
315 byte[] registerWithNonSetMBit = extractWSUdpByteArray(new String("0000 00 50 56 ee d1 4f 00 0c 29 7a ce 79 08 00 45 00 " //
316 + "0010 00 5c 00 00 40 00 40 11 d4 db c0 a8 88 0a 80 df " //
317 + "0020 9c 23 d6 40 10 f6 00 48 59 a4 38 00 00 01 00 00 "
318 + "0030 00 00 00 00 00 00 00 01 00 14 79 d1 44 66 19 99 "
319 + "0040 83 63 a7 79 6e f0 40 97 54 26 3a 44 b4 eb 00 00 " //
320 + "0050 00 0a 01 20 10 00 00 00 00 01 99 10 fe 01 01 64 " //
321 + "0060 ff 00 00 05 00 01 c0 a8 88 0a"));
322 stubMapRegister(true);
324 handleMapRegisterPacket(registerWithNonSetMBit);
326 assertFalse(lastMapRegister().isWantMapNotify());
330 public void mapRegister__NonSetMBitWithNonZeroReservedBits() throws Exception {
331 byte[] registerWithNonSetMBit = extractWSUdpByteArray(new String("0000 00 50 56 ee d1 4f 00 0c 29 7a ce 79 08 00 45 00 " //
332 + "0010 00 5c 00 00 40 00 40 11 d4 db c0 a8 88 0a 80 df "
333 + "0020 9c 23 d6 40 10 f6 00 48 59 a4 38 00 02 01 00 00 "
334 + "0030 00 00 00 00 00 00 00 01 00 14 c0 c7 c5 2f 57 f6 "
335 + "0040 e7 20 25 3d e8 b2 07 e2 63 de 62 2b 7a 20 00 00 "
336 + "0050 00 0a 01 20 10 00 00 00 00 01 99 10 fe 01 01 64 " //
337 + "0060 ff 00 00 05 00 01 c0 a8 88 0a"));
338 stubMapRegister(true);
340 handleMapRegisterPacket(registerWithNonSetMBit);
341 assertFalse(lastMapRegister().isWantMapNotify());
345 public void mapRegister__SetMBitWithNonZeroReservedBits() throws Exception {
346 byte[] registerWithNonSetMBit = extractWSUdpByteArray(new String("0000 00 50 56 ee d1 4f 00 0c 29 7a ce 79 08 00 45 00 " //
347 + "0010 00 5c 00 00 40 00 40 11 d4 db c0 a8 88 0a 80 df "
348 + "0020 9c 23 d6 40 10 f6 00 48 59 a4 38 00 03 01 00 00 "
349 + "0030 00 00 00 00 00 00 00 01 00 14 a2 72 40 7b 1a ae "
350 + "0040 4e 6b e2 e5 e1 01 40 8a c9 e1 d1 80 cb 72 00 00 "
351 + "0050 00 0a 01 20 10 00 00 00 00 01 99 10 fe 01 01 64 " //
352 + "0060 ff 00 00 05 00 01 c0 a8 88 0a"));
353 stubMapRegister(true);
355 handleMapRegisterPacket(registerWithNonSetMBit);
356 assertTrue(lastMapRegister().isWantMapNotify());
361 public void mapRegisterAndNotify__ValidExtraDataParsedSuccessfully() throws Exception {
362 byte[] extraDataPacket = new byte[mapRegisterPacket.length + 3];
363 extraDataPacket[mapRegisterPacket.length] = 0x9;
364 System.arraycopy(mapRegisterPacket, 0, extraDataPacket, 0, mapRegisterPacket.length);
365 stubMapRegister(true);
367 DatagramPacket dp = new DatagramPacket(extraDataPacket, extraDataPacket.length);
368 dp.setLength(mapRegisterPacket.length);
369 testedLispService.handlePacket(dp);
370 // Check map register fields.
372 // byte[] notifyResult = testedLispService.handlePacket(dp).getData();
373 byte[] notifyResult = lastMapNotifyPacket().getData();
374 assertEquals(mapRegisterPacket.length, notifyResult.length);
378 private DatagramPacket lastMapReplyPacket() {
379 ByteBuffer serialize = MapReplySerializer.getInstance().serialize(mapReplyBuilder.build());
380 return new DatagramPacket(serialize.array(), serialize.array().length);
383 private DatagramPacket lastMapNotifyPacket() {
384 if (mapNotifyBuilder.getEidToLocatorRecord() == null) {
385 mapNotifyBuilder.setEidToLocatorRecord(new ArrayList<EidToLocatorRecord>());
387 mapNotifyBuilder.getEidToLocatorRecord().add(eidToLocatorBuilder.build());
388 mapNotifyBuilder.setNonce((long) 0);
389 mapNotifyBuilder.setKeyId((short) 0);
390 mapNotifyBuilder.setAuthenticationData(new byte[0]);
391 ByteBuffer serialize = MapNotifySerializer.getInstance().serialize(mapNotifyBuilder.build());
392 return new DatagramPacket(serialize.array(), serialize.array().length);
397 public void mapNotify__VerifyBasicFields() throws Exception {
398 byte registerType = mapRegisterPacket[0];
399 assertEquals(LispMessageEnum.MapRegister.getValue(), registerType >> 4);
401 stubMapRegister(true);
403 byte[] result = handleMapRegisterAsByteArray(mapRegisterPacket);
405 assertEquals(mapRegisterPacket.length, result.length);
407 byte expectedType = (byte) (LispMessageEnum.MapNotify.getValue() << 4);
408 assertHexEquals(expectedType, result[0]);
409 assertHexEquals((byte) 0x00, result[1]);
410 assertHexEquals((byte) 0x00, result[2]);
412 byte[] registerWithoutTypeWithoutAuthenticationData = ArrayUtils.addAll(Arrays.copyOfRange(mapRegisterPacket, 3, 16),
413 Arrays.copyOfRange(mapRegisterPacket, 36, mapRegisterPacket.length));
414 byte[] notifyWithoutTypeWithOutAuthenticationData = ArrayUtils.addAll(Arrays.copyOfRange(result, 3, 16),
415 Arrays.copyOfRange(result, 36, result.length));
416 ArrayAssert.assertEquals(registerWithoutTypeWithoutAuthenticationData, notifyWithoutTypeWithOutAuthenticationData);
421 public void mapNotify__VerifyPort() throws Exception {
422 stubMapRegister(true);
424 DatagramPacket notifyPacket = handleMapRegisterPacket(mapRegisterPacket);
425 assertEquals(LispMessage.PORT_NUM, notifyPacket.getPort());
429 public void mapRequest__VerifyBasicFields() throws Exception {
430 oneOf(nps).publish(with(lispNotificationSaver));
431 handleMapRequestAsByteArray(mapRequestPacket);
432 List<EidRecord> eids = lastMapRequest().getEidRecord();
433 assertEquals(1, eids.size());
434 LispAFIAddress lispAddress = LispAFIConvertor.toAFI(eids.get(0).getLispAddressContainer());
435 assertTrue(lispAddress instanceof Ipv4);
436 assertEquals(getIP("1.2.3.4"), ((Ipv4) lispAddress));
437 assertEquals((byte) 0x20, eids.get(0).getMask().byteValue());
438 assertEquals(0x3d8d2acd39c8d608L, lastMapRequest().getNonce().longValue());
439 // assertEquals(AddressFamilyNumberEnum.RESERVED,
440 // AddressFamilyNumberEnum.valueOf(LispAFIToContainerConvertorFactory.toAFI(
441 // lastMapRequest().getSourceEid().getLispAddressContainer()).getAfi()));
445 public void mapRequest__Ipv6Eid() throws Exception {
446 // Internet Protocol Version 6, Src: 2610:d0:ffff:192::1
447 // (2610:d0:ffff:192::1), Dst: 2610:d0:ffff:192::2
448 // (2610:d0:ffff:192::2)
451 // Source EID: 2610:d0:ffff:192::1 (2610:d0:ffff:192::1)
452 // ITR-RLOC 1: 10.0.58.156
453 // Record 1: 2610:d0:ffff:192::2/128
454 // Map-Reply Record: EID prefix: 2610:d0:ffff:192::1/128, TTL: 10,
455 // Authoritative, No-Action
457 mapRequestPacket = extractWSUdpByteArray(new String("0000 00 0c 29 34 3e 1b 00 0c 29 f6 d6 0d 08 00 45 00 " //
458 + "0010 00 b0 00 00 40 00 40 11 ea 7b 0a 00 3a 9c 0a 00 "
459 + "0020 01 26 10 f6 10 f6 00 9c 9b 19 80 00 00 00 60 00 "
460 + "0030 00 00 00 68 11 ff 26 10 00 d0 ff ff 01 92 00 00 "
461 + "0040 00 00 00 00 00 01 26 10 00 d0 ff ff 01 92 00 00 "
462 + "0050 00 00 00 00 00 02 10 f6 10 f6 00 68 94 8b 14 00 "
463 + "0060 00 01 ff f5 bf 5d 7b 75 93 e6 00 02 26 10 00 d0 "
464 + "0070 ff ff 01 92 00 00 00 00 00 00 00 01 00 01 0a 00 "
465 + "0080 3a 9c 00 80 00 02 26 10 00 d0 ff ff 01 92 00 00 "
466 + "0090 00 00 00 00 00 02 00 00 00 0a 01 80 10 00 00 00 " //
467 + "00a0 00 02 26 10 00 d0 ff ff 01 92 00 00 00 00 00 00 " //
468 + "00b0 00 01 01 64 ff 00 00 05 00 01 0a 00 3a 9c"));
470 oneOf(nps).publish(with(lispNotificationSaver));
473 handleMapRequestAsByteArray(mapRequestPacket);
474 assertEquals(getIPV6("2610:d0:ffff:192:0:0:0:1"), LispAFIConvertor.toAFI(lastMapRequest().getSourceEid().getLispAddressContainer()));
475 System.out.println(((Ipv6) LispAFIConvertor.toAFI(lastMapRequest().getEidRecord().get(0).getLispAddressContainer())).getIpv6Address()
477 assertEquals(getIPV6("2610:d0:ffff:192:0:0:0:2"), LispAFIConvertor.toAFI(lastMapRequest().getEidRecord().get(0).getLispAddressContainer()));
482 public void mapRequest__UsesIpv6EncapsulatedUdpPort() throws Exception {
483 // Internet Protocol Version 6, Src: 2610:d0:ffff:192::1
484 // (2610:d0:ffff:192::1), Dst: 2610:d0:ffff:192::2
485 // (2610:d0:ffff:192::2)
486 // encapsulated UDP source port: 4342
488 mapRequestPacket = extractWSUdpByteArray(new String("0000 00 0c 29 34 3e 1b 00 0c 29 f6 d6 0d 08 00 45 00 " //
489 + "0010 00 b0 00 00 40 00 40 11 ea 7b 0a 00 3a 9c 0a 00 "
490 + "0020 01 26 10 f6 10 f6 00 9c 9b 19 80 00 00 00 60 00 "
491 + "0030 00 00 00 68 11 ff 26 10 00 d0 ff ff 01 92 00 00 "
492 + "0040 00 00 00 00 00 01 26 10 00 d0 ff ff 01 92 00 00 "
493 + "0050 00 00 00 00 00 02 10 f6 10 f6 00 68 94 8b 14 00 "
494 + "0060 00 01 ff f5 bf 5d 7b 75 93 e6 00 02 26 10 00 d0 "
495 + "0070 ff ff 01 92 00 00 00 00 00 00 00 01 00 01 0a 00 "
496 + "0080 3a 9c 00 80 00 02 26 10 00 d0 ff ff 01 92 00 00 "
497 + "0090 00 00 00 00 00 02 00 00 00 0a 01 80 10 00 00 00 " //
498 + "00a0 00 02 26 10 00 d0 ff ff 01 92 00 00 00 00 00 00 " //
499 + "00b0 00 01 01 64 ff 00 00 05 00 01 0a 00 3a 9c"));
500 oneOf(nps).publish(with(lispNotificationSaver));
503 DatagramPacket replyPacket = handleMapRequestPacket(mapRequestPacket);
504 assertEquals(4342, replyPacket.getPort());
508 public void mapRequest__WithSourceEid() throws Exception {
509 // encapsulated LISP packet
510 // Source EID = 153.16.254.1
512 mapRequestPacket = extractWSUdpByteArray(new String("0000 00 0c 29 7a ce 83 00 15 17 c6 4a c9 08 00 45 00 " //
513 + "0010 00 78 00 00 40 00 3e 11 ec b1 0a 00 01 26 0a 00 "
514 + "0020 3a 9e 10 f6 10 f6 00 64 c3 a5 80 00 00 00 45 00 "
515 + "0030 00 58 d4 31 00 00 ff 11 31 89 99 10 fe 01 0a 00 "
516 + "0040 14 c8 10 f6 10 f6 00 44 84 ee 14 00 00 01 ba f9 "
517 + "0050 ff 53 27 36 38 3a 00 01 99 10 fe 01 00 01 0a 00 "
518 + "0060 01 26 00 20 00 01 0a 00 14 c8 00 00 00 0a 01 20 "
519 + "0070 10 00 00 00 00 01 99 10 fe 01 01 64 ff 00 00 05 " //
520 + "0080 00 01 0a 00 01 26"));
522 oneOf(nps).publish(with(lispNotificationSaver));
525 handleMapRequestAsByteArray(mapRequestPacket);
526 Assert.assertNotEquals(AddressFamilyNumberEnum.IP, LispAFIConvertor.toAFI(lastMapRequest().getSourceEid().getLispAddressContainer()));
532 public void mapReply__VerifyBasicIPv4Fields() throws Exception {
533 eidToLocatorBuilder.setMaskLength((short) 0x20).setLispAddressContainer(LispAFIConvertor.toContainer(getIP("10.0.20.200")));
534 mapReplyBuilder.setNonce(0x3d8d2acd39c8d608L);
538 byte[] result = handleMapRequestAsByteArray(mapRequestPacket);
540 assertEquals(28, result.length);
542 byte expectedLispMessageType = 2;
543 assertEquals(expectedLispMessageType, (byte) (result[LispMessage.Pos.TYPE] >> 4));
544 assertEquals(0x3d8d2acd39c8d608L, ByteUtil.getLong(result, MapReplyIpv4SingleLocatorPos.NONCE));
546 byte expectedRecordCount = (byte) 1;
547 assertEquals(expectedRecordCount, result[MapReplyIpv4SingleLocatorPos.RECORD_COUNT]);
549 assertEquals(eidToLocatorBuilder.getMaskLength().byteValue(), result[MapReplyIpv4SingleLocatorPos.EID_MASK_LEN]);
550 assertEquals(AddressFamilyNumberEnum.IP.getIanaCode(), ByteUtil.getShort(result, MapReplyIpv4SingleLocatorPos.AFI_TYPE));
551 assertEquals(0x0a0014c8, ByteUtil.getInt(result, MapReplyIpv4SingleLocatorPos.EID_PREFIX));
556 public void mapReply__VerifyBasicIPv6() throws Exception {
557 eidToLocatorBuilder.setMaskLength((short) 0x80).setLispAddressContainer(LispAFIConvertor.toContainer(getIPV6("0:0:0:0:0:0:0:1")));
561 byte[] result = handleMapRequestAsByteArray(mapRequestPacket);
563 assertEquals(40, result.length);
565 byte expectedRecordCount = (byte) 1;
566 assertEquals(expectedRecordCount, result[MapReplyIpv4SingleLocatorPos.RECORD_COUNT]);
568 assertEquals(eidToLocatorBuilder.getMaskLength().byteValue(), result[MapReplyIpv4SingleLocatorPos.EID_MASK_LEN]);
569 assertEquals(AddressFamilyNumberEnum.IP6.getIanaCode(), ByteUtil.getShort(result, MapReplyIpv4SingleLocatorPos.AFI_TYPE));
570 byte[] expectedIpv6 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
572 ArrayAssert.assertEquals(expectedIpv6, Arrays.copyOfRange(result, 24, 40));
577 public void mapReply__VerifyIPv6EidAndLocator() throws Exception {
578 eidToLocatorBuilder.setLispAddressContainer(LispAFIConvertor.toContainer(getIPV6("0:0:0:0:0:0:0:1")));
579 eidToLocatorBuilder.getLocatorRecord().add(
580 new LocatorRecordBuilder().setLispAddressContainer(LispAFIConvertor.toContainer(getIPV6("0:0:0:0:0:0:0:2"))).build());
584 byte[] result = handleMapRequestAsByteArray(mapRequestPacket);
586 assertEquals(64, result.length);
588 byte[] expectedIpv6Eid = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
589 ArrayAssert.assertEquals(expectedIpv6Eid, Arrays.copyOfRange(result, 24, 40));
591 byte[] expectedIpv6Rloc = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2 };
592 ArrayAssert.assertEquals(expectedIpv6Rloc, Arrays.copyOfRange(result, 48, 64));
597 public void mapReply__UseEncapsulatedUdpPort() throws Exception {
600 assertEquals(LispMessage.PORT_NUM, handleMapRequestPacket(mapRequestPacket).getPort());
605 public void mapReply__WithNonRoutableSingleLocator() throws Exception {
606 eidToLocatorBuilder.setMaskLength((short) 0x20).setLispAddressContainer(LispAFIConvertor.toContainer(getIP("10.0.20.200")));
607 eidToLocatorBuilder.getLocatorRecord().add(
608 new LocatorRecordBuilder().setRouted(false).setLispAddressContainer(LispAFIConvertor.toContainer(getIP("4.3.2.1"))).build());
611 byte[] result = handleMapRequestAsByteArray(mapRequestPacket);
612 assertEquals(0x00, result[MapReplyIpv4SingleLocatorPos.LOCATOR_RBIT] & 0x01);
617 public void mapReply__WithSingleLocator() throws Exception {
618 eidToLocatorBuilder.setMaskLength((short) 0x20)//
619 .setLispAddressContainer(LispAFIConvertor.toContainer(getIP("10.0.20.200")));
620 eidToLocatorBuilder.getLocatorRecord().add(
621 new LocatorRecordBuilder().setRouted(true).setLispAddressContainer(LispAFIConvertor.toContainer(getIP("4.3.2.1"))).build());
624 byte[] result = handleMapRequestAsByteArray(mapRequestPacket);
625 assertEquals(40, result.length);
627 byte expectedLocCount = 1;
628 assertEquals(expectedLocCount, result[MapReplyIpv4SingleLocatorPos.LOCATOR_COUNT]);
630 assertEquals(AddressFamilyNumberEnum.IP.getIanaCode(), ByteUtil.getShort(result, MapReplyIpv4SingleLocatorPos.LOC_AFI));
632 assertEquals(0x04030201, ByteUtil.getInt(result, MapReplyIpv4SingleLocatorPos.LOCATOR));
633 assertEquals(0x01, result[MapReplyIpv4SingleLocatorPos.LOCATOR_RBIT] & 0x01);
638 public void mapReply__WithMultipleLocator() throws Exception {
639 eidToLocatorBuilder.getLocatorRecord().add(
640 new LocatorRecordBuilder().setRouted(true).setLispAddressContainer(LispAFIConvertor.toContainer(getIP("4.3.2.1"))).build());
641 eidToLocatorBuilder.getLocatorRecord().add(
642 new LocatorRecordBuilder().setRouted(true).setLispAddressContainer(LispAFIConvertor.toContainer(getIPV6("0:0:0:0:0:0:0:1"))).build());
645 byte[] result = handleMapRequestAsByteArray(mapRequestPacket);
646 assertEquals(64, result.length);
648 assertEquals(2, result[MapReplyIpv4SingleLocatorPos.LOCATOR_COUNT]);
650 assertEquals(AddressFamilyNumberEnum.IP.getIanaCode(), ByteUtil.getShort(result, MapReplyIpv4SingleLocatorPos.LOC_AFI));
651 assertEquals(0x04030201, ByteUtil.getInt(result, MapReplyIpv4SingleLocatorPos.LOCATOR));
652 assertEquals(0x01, result[MapReplyIpv4SingleLocatorPos.LOCATOR_RBIT] & 0x01);
654 assertEquals(AddressFamilyNumberEnum.IP6.getIanaCode(), ByteUtil.getShort(result, MapReplyIpv4SecondLocatorPos.LOC_AFI));
656 byte[] expectedIpv6Rloc = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
657 ArrayAssert.assertEquals(expectedIpv6Rloc,
658 Arrays.copyOfRange(result, MapReplyIpv4SecondLocatorPos.LOCATOR, MapReplyIpv4SecondLocatorPos.LOCATOR + 16));
660 assertEquals(0x01, result[MapReplyIpv4SecondLocatorPos.LOCATOR_RBIT] & 0x01);
664 public void handleUnknownLispMessage() throws Exception {
665 // IP: 192.168.136.10 -> 128.223.156.35
666 // UDP: 49289 -> 4342
667 // LISP(Type = 14 UNKNOWN!!!, P=1, M=1
669 byte[] unknownTypePacket = extractWSUdpByteArray(new String("0000 00 50 56 ee d1 4f 00 0c 29 7a ce 79 08 00 45 00 " //
670 + "0010 00 5c 00 00 40 00 40 11 d4 db c0 a8 88 0a 80 df "
671 + "0020 9c 23 d6 40 10 f6 00 48 59 a4 F8 00 01 01 00 00 "
672 + "0030 00 00 00 00 00 00 00 01 00 14 e8 f5 0b c5 c5 f2 "
673 + "0040 b0 21 27 a8 21 41 04 f3 46 5a a5 68 89 ec 00 00 "
674 + "0050 00 0a 01 20 10 00 00 00 00 01 99 10 fe 01 01 64 " //
675 + "0060 ff 00 00 05 00 01 c0 a8 88 0a"));
676 assertNull(handlePacket(unknownTypePacket));
680 public void mapRequest__MultipleItrRlocs() throws Exception {
681 // this is what LISPmob sends when configured multiple RLOCs for single
683 // ITR-RLOC 1: 10.1.0.111
684 // ITR-RLOC 2: 192.168.136.51
686 mapRequestPacket = extractWSUdpByteArray(new String("0000 00 0c 29 7a ce 8d 00 0c 29 e4 ef 70 08 00 45 00 "
687 + "0010 00 8a 00 00 40 00 40 11 25 f2 0a 01 00 6f 0a 01 " //
688 + "0020 00 01 10 f6 10 f6 00 76 06 1f 80 00 00 00 45 00 " //
689 + "0030 00 6a d4 31 00 00 ff 11 2a 3e ac 01 01 02 08 08 " //
690 + "0040 08 08 10 f6 10 f6 00 56 63 14 14 00 01 01 79 67 " //
691 + "0050 ff 75 a0 61 66 19 00 01 ac 01 01 02 00 01 0a 01 " //
692 + "0060 00 6f 00 01 c0 a8 88 33 00 20 00 01 08 08 08 08 " //
693 + "0070 00 00 00 0a 02 20 10 00 00 00 00 01 ac 01 01 02 " //
694 + "0080 01 64 ff 00 00 05 00 01 0a 01 00 6f 06 64 ff 00 " //
695 + "0090 00 05 00 01 c0 a8 88 33"));
697 oneOf(nps).publish(with(lispNotificationSaver));
698 handleMapRequestAsByteArray(mapRequestPacket);
702 private void stubMapRegister(final boolean setNotifyFromRegister) {
703 allowing(nps).publish(with(lispNotificationSaver));
704 will(new SimpleAction() {
707 public Object invoke(Invocation invocation) throws Throwable {
708 if (setNotifyFromRegister) {
709 MapNotifyBuilderHelper.setFromMapRegister(mapNotifyBuilder, lastMapRegister());
716 private void stubHandleRequest() {
717 allowing(nps).publish(wany(Notification.class));
720 private byte[] handleMapRequestAsByteArray(byte[] inPacket) {
721 handleMapRequestPacket(inPacket);
722 return lastMapReplyPacket().getData();
725 private byte[] handleMapRegisterAsByteArray(byte[] inPacket) {
726 handleMapRegisterPacket(inPacket);
727 return lastMapNotifyPacket().getData();
730 private DatagramPacket handleMapRequestPacket(byte[] inPacket) {
731 testedLispService.handlePacket(new DatagramPacket(inPacket, inPacket.length));
732 return lastMapReplyPacket();
735 private DatagramPacket handleMapRegisterPacket(byte[] inPacket) {
736 testedLispService.handlePacket(new DatagramPacket(inPacket, inPacket.length));
737 if (mapNotifyBuilder == null) {
740 return lastMapNotifyPacket();
744 private DatagramPacket handlePacket(byte[] inPacket) {
745 // TODO get from mock
746 testedLispService.handlePacket(new DatagramPacket(inPacket, inPacket.length));
750 private byte[] extractWSUdpByteArray(String wiresharkHex) {
751 final int HEADER_LEN = 42;
752 byte[] res = new byte[1000];
753 String[] split = wiresharkHex.split(" ");
755 for (String cur : split) {
757 if (cur.length() == 2) {
759 if (counter > HEADER_LEN) {
760 res[counter - HEADER_LEN - 1] = (byte) Integer.parseInt(cur, 16);
765 return Arrays.copyOf(res, counter - HEADER_LEN);
768 @Test(expected = LispMalformedPacketException.class)
769 public void mapRequest__NoIPITRRLOC() throws Exception {
770 mapRequestPacket = hexToByteBuffer("10 00 " //
771 + "02 " // This means 3 ITR - RLOCs
772 + "01 3d 8d 2a cd 39 c8 d6 08 00 00 " //
773 + "40 05 c0 a8 88 0a 01 02 " // MAC (ITR-RLOC #1 of 3)
774 + "40 05 00 00 00 00 00 00 " // MAC (ITR-RLOC #2 of 3)
775 + "40 05 11 22 34 56 78 90 " // MAC (ITR-RLOC #3 of 3)
776 + "00 20 00 01 01 02 03 04").array();
777 handleMapRequestPacket(mapRequestPacket);
782 // public void mapRequest__IPITRRLOCIsSecond() throws Exception {
783 // mapRequestPacket = hexToByteBuffer("10 00 " //
784 // + "01 " // This means 3 ITR - RLOCs
785 // + "01 3d 8d 2a cd 39 c8 d6 08 00 00 " //
786 // + "40 05 c0 a8 88 0a 01 02 " // MAC (ITR-RLOC #1 of 2)
787 // + "00 01 01 02 03 04 " // IP (ITR-RLOC #2 of 2)
788 // + "00 20 00 01 01 02 03 04").array();
789 // oneOf(nps).publish(with(lispNotificationSaver));
791 // DatagramPacket packet = handleMapRequestPacket(mapRequestPacket);
792 // assertEquals(2, lastMapRequest().getItrRlocs().size());
793 // assertEquals((new LispIpv4Address("1.2.3.4")).getAddress(),
794 // packet.getAddress());
799 // public void mapRequest__MULTIPLEIPITRRLOCs() throws Exception {
800 // mapRequestPacket = hexToByteBuffer("10 00 " //
801 // + "01 " // This means 3 ITR - RLOCs
802 // + "01 3d 8d 2a cd 39 c8 d6 08 00 00 " //
803 // + "00 01 01 02 03 04 " // IP (ITR-RLOC #1 of 2)
804 // + "00 01 c0 a8 88 0a " // MAC (ITR-RLOC #2 of 2)
805 // + "00 20 00 01 01 02 03 04").array();
806 // oneOf(nps).publish(with(lispNotificationSaver));
808 // DatagramPacket packet = handleMapRequestPacket(mapRequestPacket);
809 // assertEquals(2, lastMapRequest().getItrRloc().size());
810 // assertEquals((new LispIpv4Address("1.2.3.4")).getAddress(),
811 // packet.getAddress());