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 io.netty.buffer.Unpooled.wrappedBuffer;
12 import io.netty.channel.socket.DatagramPacket;
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertFalse;
15 import static org.junit.Assert.assertTrue;
17 import java.net.InetSocketAddress;
18 import java.nio.ByteBuffer;
19 import java.util.ArrayList;
20 import java.util.Arrays;
21 import java.util.List;
23 import junitx.framework.ArrayAssert;
25 import org.apache.commons.lang3.ArrayUtils;
26 import org.jmock.api.Invocation;
27 import org.junit.Before;
28 import org.junit.Ignore;
29 import org.junit.Test;
30 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
31 import org.opendaylight.lispflowmapping.lisp.type.LispMessage;
32 import org.opendaylight.lispflowmapping.lisp.util.ByteUtil;
33 import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil;
34 import org.opendaylight.lispflowmapping.lisp.util.MapNotifyBuilderHelper;
35 import org.opendaylight.lispflowmapping.lisp.util.MaskUtil;
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.ietf.params.xml.ns.yang.iana.afn.safi.rev130704.AddressFamily;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.Ipv4Afi;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.Ipv4PrefixAfi;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.Ipv6PrefixAfi;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv6Prefix;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.AddMapping;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapRegister;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapRequest;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MessageType;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.RequestMapping;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.list.EidItem;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.locatorrecords.LocatorRecord;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.locatorrecords.LocatorRecordBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapnotifymessage.MapNotifyBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecord;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecord.Action;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecordBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.list.MappingRecordItem;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.list.MappingRecordItemBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapreplymessage.MapReplyBuilder;
61 import org.opendaylight.yangtools.yang.binding.Notification;
63 public class LispSouthboundServiceTest extends BaseTestCase {
65 private LispSouthboundHandler testedLispService;
66 private NotificationPublishService nps;
67 private byte[] mapRequestPacket;
68 private byte[] mapRegisterPacket;
69 private ValueSaverAction<Notification> lispNotificationSaver;
70 // private ValueSaverAction<MapRegister> mapRegisterSaver;
71 // private ValueSaverAction<MapRequest> mapRequestSaver;
72 private MapNotifyBuilder mapNotifyBuilder;
73 private MapReplyBuilder mapReplyBuilder;
74 private MappingRecordBuilder mappingRecordBuilder;
76 private interface MapReplyIpv4SingleLocatorPos {
79 int LOCATOR_COUNT = 16;
80 int EID_MASK_LEN = 17;
84 int LOCATOR_RBIT = 33;
88 private interface MapReplyIpv4SecondLocatorPos {
89 int FIRST_LOCATOR_IPV4_LENGTH = 12;
90 int LOC_AFI = MapReplyIpv4SingleLocatorPos.LOC_AFI + FIRST_LOCATOR_IPV4_LENGTH;
91 int LOCATOR_RBIT = MapReplyIpv4SingleLocatorPos.LOCATOR_RBIT + FIRST_LOCATOR_IPV4_LENGTH;
92 int LOCATOR = MapReplyIpv4SingleLocatorPos.LOCATOR + FIRST_LOCATOR_IPV4_LENGTH;
97 public void before() throws Exception {
99 // mapResolver = context.mock(IMapResolver.class);
100 // mapServer = context.mock(IMapServer.class);
101 testedLispService = new LispSouthboundHandler(null);
102 nps = context.mock(NotificationPublishService.class);
103 testedLispService.setNotificationProvider(nps);
104 lispNotificationSaver = new ValueSaverAction<Notification>();
105 // mapRegisterSaver = new ValueSaverAction<MapRegister>();
106 // mapRequestSaver = new ValueSaverAction<MapRequest>();
107 // SRC: 127.0.0.1:58560 to 127.0.0.1:4342
108 // LISP(Type = 8 - Encapsulated)
109 // IP: 192.168.136.10 -> 1.2.3.4
111 // LISP(Type = 1 Map-Request
115 // Source EID not present
116 // Nonce: 0x3d8d2acd39c8d608
117 // ITR-RLOC AFI=1 Address=192.168.136.10
118 // Record 1: 1.2.3.4/32
119 mapRequestPacket = extractWSUdpByteArray(new String("0000 00 00 00 00 00 00 00 00 00 00 00 00 08 00 45 00 " //
120 + "0010 00 58 00 00 40 00 40 11 3c 93 7f 00 00 01 7f 00 "
121 + "0020 00 01 e4 c0 10 f6 00 44 fe 57 80 00 00 00 45 00 "
122 + "0030 00 38 d4 31 00 00 ff 11 56 f3 c0 a8 88 0a 01 02 "
123 + "0040 03 04 dd b4 10 f6 00 24 ef 3a 10 00 00 01 3d 8d "
124 + "0050 2a cd 39 c8 d6 08 00 01 01 02 03 04 00 01 c0 a8 88 0a 00 20 " //
125 + "0060 00 01 01 02 03 04"));
126 mapReplyBuilder = new MapReplyBuilder();
127 mapReplyBuilder.setMappingRecordItem(new ArrayList<MappingRecordItem>());
128 mapReplyBuilder.setNonce((long) 0);
129 mapReplyBuilder.setEchoNonceEnabled(false);
130 mapReplyBuilder.setProbe(true);
131 mapReplyBuilder.setSecurityEnabled(true);
132 mappingRecordBuilder = new MappingRecordBuilder();
133 String ip = "0.0.0.0";
134 mappingRecordBuilder.setEid(LispAddressUtil.asIpv4PrefixEid(ip + "/0"));
135 mappingRecordBuilder.setLocatorRecord(new ArrayList<LocatorRecord>());
136 mappingRecordBuilder.setRecordTtl(10);
137 mappingRecordBuilder.setMapVersion((short) 0);
138 mappingRecordBuilder.setMaskLength((short) 0);
139 mappingRecordBuilder.setAction(Action.NativelyForward);
140 mappingRecordBuilder.setAuthoritative(false);
141 // eidToLocatorBuilder.setPrefix(new LispIpv4Address(0));
142 // mapReply.addEidToLocator(eidToLocatorBuilder);
144 // IP: 192.168.136.10 -> 128.223.156.35
145 // UDP: 49289 -> 4342
146 // LISP(Type = 3 Map-Register, P=1, M=1
150 // AuthDataLength: 20 Data:
151 // e8:f5:0b:c5:c5:f2:b0:21:27:a8:21:41:04:f3:46:5a:a5:68:89:ec
152 // EID prefix: 153.16.254.1/32 (EID=0x9910FE01), TTL: 10, Authoritative,
154 // Local RLOC: 192.168.136.10 (RLOC=0xC0A8880A), Reachable,
155 // Priority/Weight: 1/100, Multicast Priority/Weight:
159 mapRegisterPacket = extractWSUdpByteArray(new String("0000 00 50 56 ee d1 4f 00 0c 29 7a ce 79 08 00 45 00 " //
160 + "0010 00 5c 00 00 40 00 40 11 d4 db c0 a8 88 0a 80 df "
161 + "0020 9c 23 d6 40 10 f6 00 48 59 a4 38 00 01 01 00 00 "
162 + "0030 00 00 00 00 00 00 00 01 00 14 0e a4 c6 d8 a4 06 "
163 + "0040 71 7c 33 a4 5c 4a 83 1c de 74 53 03 0c ad 00 00 "
164 + "0050 00 0a 01 20 10 00 00 00 00 01 99 10 fe 01 01 64 " //
165 + "0060 ff 00 00 05 00 01 c0 a8 88 0a"));
166 mapNotifyBuilder = new MapNotifyBuilder();
167 mapNotifyBuilder.setAuthenticationData(new byte[0]);
172 public void todos() throws Exception {
174 // TODO: MapRequest: usage of Map-Reply-Record in MapRequest packet.
175 // TODO: Non-Encapsulated packets
178 @Test(expected = LispMalformedPacketException.class)
179 public void mapRegister__IllegalPacket() throws Exception {
180 mapRegisterPacket = extractWSUdpByteArray(new String("0000 00 0c 29 7a ce 8d 00 0c 29 e4 ef 70 08 00 45 00 "
181 + "0010 00 68 00 00 40 00 40 11 26 15 0a 01 00 6e 0a 01 " //
182 + "0020 00 01 10 f6 10 f6 00 54 03 3b 38 00 01 01 00 00 "));
184 handleMapRegisterPacket(mapRegisterPacket);
187 @Test(expected = LispMalformedPacketException.class)
188 public void mapRequest__IllegalPacket() throws Exception {
189 mapRequestPacket = extractWSUdpByteArray(new String("0000 00 00 00 00 00 00 00 00 00 00 00 00 08 00 45 00 " //
190 + "0010 00 58 00 00 40 00 40 11 3c 93 7f 00 00 01 7f 00 "
191 + "0020 00 01 e4 c0 10 f6 00 44 fe 57 80 00 00 00 45 00 "
192 + "0030 00 38 d4 31 00 00 ff 11 56 f3 c0 a8 88 0a 01 02 " //
193 + "0040 03 04 dd b4 10 f6 00 24 ef 3a 10 00 00 01 3d 8d "));
194 handleMapRequestPacket(mapRequestPacket);
197 @Test(expected = LispMalformedPacketException.class)
198 public void mapRequest__IllegalEncapsulatedPacket() throws Exception {
199 mapRequestPacket = extractWSUdpByteArray(new String("0000 00 00 00 00 00 00 00 00 00 00 00 00 08 00 45 00 " //
200 + "0010 00 58 00 00 40 00 40 11 3c 93 7f 00 00 01 7f 00 " //
201 + "0020 00 01 e4 c0 10 f6 00 44 fe 57 80 00 00 00 45 00 "));
202 handleMapRequestPacket(mapRequestPacket);
205 private MapRegister lastMapRegister() {
206 assertTrue(lispNotificationSaver.lastValue instanceof AddMapping);
207 AddMapping lastValue = (AddMapping) lispNotificationSaver.lastValue;
208 return lastValue.getMapRegister();
211 private MapRequest lastMapRequest() {
212 RequestMapping lastValue = (RequestMapping) lispNotificationSaver.lastValue;
213 return lastValue.getMapRequest();
217 public void mapRegister__TwoRlocs() throws Exception {
219 // EID prefix: 172.1.1.2/32, TTL: 10, Authoritative, No-Action
220 // Local RLOC: 10.1.0.110, Reachable, Priority/Weight: 1/100, Multicast
221 // Priority/Weight: 255/0
222 // Local RLOC: 192.168.136.51, Reachable, Priority/Weight: 6/100,
223 // Multicast Priority/Weight: 255/0
224 mapRegisterPacket = extractWSUdpByteArray(new String("0000 00 0c 29 7a ce 8d 00 0c 29 e4 ef 70 08 00 45 00 "
225 + "0010 00 68 00 00 40 00 40 11 26 15 0a 01 00 6e 0a 01 " //
226 + "0020 00 01 10 f6 10 f6 00 54 03 3b 38 00 01 01 00 00 " //
227 + "0030 00 00 00 00 00 00 00 01 00 14 ae d8 7b d4 9c 59 " //
228 + "0040 e9 35 75 6e f1 29 27 a3 45 20 96 06 c2 e1 00 00 " //
229 + "0050 00 0a 02 20 10 00 00 00 00 01 ac 01 01 02 01 64 " //
230 + "0060 ff 00 00 05 00 01 0a 01 00 6e 06 64 ff 00 00 05 " //
231 + "0070 00 01 c0 a8 88 33"));
233 oneOf(nps).putNotification(with(lispNotificationSaver));
235 handleMapRegisterPacket(mapRegisterPacket);
237 List<MappingRecordItem> eidRecords = lastMapRegister().getMappingRecordItem();
238 assertEquals(1, eidRecords.size());
239 MappingRecord eidRecord = eidRecords.get(0).getMappingRecord();
240 assertEquals(2, eidRecord.getLocatorRecord().size());
241 assertEquals(LispAddressUtil.asIpv4Rloc("10.1.0.110"), eidRecord.getLocatorRecord().get(0).getRloc());
242 assertEquals(LispAddressUtil.asIpv4Rloc("192.168.136.51"), eidRecord.getLocatorRecord().get(1).getRloc());
246 public void mapRegister__Ipv6Rloc() throws Exception {
247 // P bit (Proxy-Map-Reply): Set
248 // M bit (Want-Map-Notify): Set
253 // Authentication Data: 5bc4d44a57e2a55d577a6f89779c004f5da713fb
254 // EID prefix: 2610:d0:ffff:192::1/128, TTL: 10, Authoritative,
256 // Local RLOC: 10.0.58.156, Reachable, Priority/Weight: 1/100, Multicast
257 // Priority/Weight: 255/0
259 mapRegisterPacket = extractWSUdpByteArray(new String("0000 00 0c 29 34 3e 1b 00 0c 29 f6 d6 0d 08 00 45 00 " //
260 + "0010 00 68 00 00 40 00 40 11 ea c3 0a 00 3a 9c 0a 00 " //
261 + "0020 01 26 10 f6 10 f6 00 54 f5 9a 38 00 03 01 00 00 " //
262 + "0030 00 00 00 00 00 00 00 01 00 14 22 97 ff 61 ec d8 " //
263 + "0040 0f 91 c6 c4 01 ef 7f bb 77 58 39 5c 92 23 00 00 " //
264 + "0050 00 0a 01 80 10 00 00 00 00 02 26 10 00 d0 ff ff " //
265 + "0060 01 92 00 00 00 00 00 00 00 01 01 64 ff 00 00 05 " //
266 + "0070 00 01 0a 00 3a 9c"));
268 oneOf(nps).putNotification(with(lispNotificationSaver));
270 handleMapRegisterPacket(mapRegisterPacket);
272 MappingRecord eidToLocatorRecord = lastMapRegister().getMappingRecordItem().get(0).getMappingRecord();
273 assertEquals("2610:d0:ffff:192:0:0:0:1/128",
274 ((Ipv6Prefix) eidToLocatorRecord.getEid().getAddress()).getIpv6Prefix().getValue());
275 assertEquals(Ipv6PrefixAfi.class, eidToLocatorRecord.getEid().getAddressType());
277 assertEquals(LispAddressUtil.asIpv4Rloc("10.0.58.156"), eidToLocatorRecord.getLocatorRecord().get(0).getRloc());
281 public void mapRegister__VerifyBasicFields() throws Exception {
282 oneOf(nps).putNotification(with(lispNotificationSaver));
283 handleMapRegisterPacket(mapRegisterPacket);
285 MappingRecord eidToLocator = lastMapRegister().getMappingRecordItem().get(0).getMappingRecord();
286 assertEquals(LispAddressUtil.asIpv4PrefixEid("153.16.254.1/32"), eidToLocator.getEid());
288 assertEquals(1, eidToLocator.getLocatorRecord().size());
289 assertEquals(LispAddressUtil.asIpv4Rloc("192.168.136.10"), eidToLocator.getLocatorRecord().get(0).getRloc());
294 public void mapRegister__NoResponseFromMapServerShouldReturnNullPacket() throws Exception {
295 oneOf(nps).putNotification(with(lispNotificationSaver));
296 mapNotifyBuilder = null;
298 assertNull(handleMapRegisterPacket(mapRegisterPacket));
302 public void mapRegister__NonSetMBit() throws Exception {
303 byte[] registerWithNonSetMBit = extractWSUdpByteArray(new String("0000 00 50 56 ee d1 4f 00 0c 29 7a ce 79 08 00 45 00 " //
304 + "0010 00 5c 00 00 40 00 40 11 d4 db c0 a8 88 0a 80 df " //
305 + "0020 9c 23 d6 40 10 f6 00 48 59 a4 38 00 00 01 00 00 "
306 + "0030 00 00 00 00 00 00 00 01 00 14 79 d1 44 66 19 99 "
307 + "0040 83 63 a7 79 6e f0 40 97 54 26 3a 44 b4 eb 00 00 " //
308 + "0050 00 0a 01 20 10 00 00 00 00 01 99 10 fe 01 01 64 " //
309 + "0060 ff 00 00 05 00 01 c0 a8 88 0a"));
310 stubMapRegister(true);
312 handleMapRegisterPacket(registerWithNonSetMBit);
314 assertFalse(lastMapRegister().isWantMapNotify());
318 public void mapRegister__NonSetMBitWithNonZeroReservedBits() throws Exception {
319 byte[] registerWithNonSetMBit = extractWSUdpByteArray(new String("0000 00 50 56 ee d1 4f 00 0c 29 7a ce 79 08 00 45 00 " //
320 + "0010 00 5c 00 00 40 00 40 11 d4 db c0 a8 88 0a 80 df "
321 + "0020 9c 23 d6 40 10 f6 00 48 59 a4 38 00 02 01 00 00 "
322 + "0030 00 00 00 00 00 00 00 01 00 14 c0 c7 c5 2f 57 f6 "
323 + "0040 e7 20 25 3d e8 b2 07 e2 63 de 62 2b 7a 20 00 00 "
324 + "0050 00 0a 01 20 10 00 00 00 00 01 99 10 fe 01 01 64 " //
325 + "0060 ff 00 00 05 00 01 c0 a8 88 0a"));
326 stubMapRegister(true);
328 handleMapRegisterPacket(registerWithNonSetMBit);
329 assertFalse(lastMapRegister().isWantMapNotify());
333 public void mapRegister__SetMBitWithNonZeroReservedBits() throws Exception {
334 byte[] registerWithNonSetMBit = extractWSUdpByteArray(new String("0000 00 50 56 ee d1 4f 00 0c 29 7a ce 79 08 00 45 00 " //
335 + "0010 00 5c 00 00 40 00 40 11 d4 db c0 a8 88 0a 80 df "
336 + "0020 9c 23 d6 40 10 f6 00 48 59 a4 38 00 03 01 00 00 "
337 + "0030 00 00 00 00 00 00 00 01 00 14 a2 72 40 7b 1a ae "
338 + "0040 4e 6b e2 e5 e1 01 40 8a c9 e1 d1 80 cb 72 00 00 "
339 + "0050 00 0a 01 20 10 00 00 00 00 01 99 10 fe 01 01 64 " //
340 + "0060 ff 00 00 05 00 01 c0 a8 88 0a"));
341 stubMapRegister(true);
343 handleMapRegisterPacket(registerWithNonSetMBit);
344 assertTrue(lastMapRegister().isWantMapNotify());
349 public void mapRegisterAndNotify__ValidExtraDataParsedSuccessfully() throws Exception {
350 byte[] extraDataPacket = new byte[mapRegisterPacket.length + 3];
351 extraDataPacket[mapRegisterPacket.length] = 0x9;
352 System.arraycopy(mapRegisterPacket, 0, extraDataPacket, 0, mapRegisterPacket.length);
353 stubMapRegister(true);
355 DatagramPacket dp = new DatagramPacket(wrappedBuffer(extraDataPacket), new InetSocketAddress(0), new InetSocketAddress(0));
356 testedLispService.handlePacket(dp);
357 // Check map register fields.
359 // byte[] notifyResult = testedLispService.handlePacket(dp).getData();
360 byte[] notifyResult = lastMapNotifyPacket().content().array();
361 assertEquals(mapRegisterPacket.length, notifyResult.length);
365 private DatagramPacket lastMapReplyPacket() {
366 ByteBuffer serialize = MapReplySerializer.getInstance().serialize(mapReplyBuilder.build());
367 return new DatagramPacket(wrappedBuffer(serialize), new InetSocketAddress(0), new InetSocketAddress(0));
370 private DatagramPacket lastMapNotifyPacket() {
371 if (mapNotifyBuilder.getMappingRecordItem() == null) {
372 mapNotifyBuilder.setMappingRecordItem(new ArrayList<MappingRecordItem>());
374 mapNotifyBuilder.getMappingRecordItem().add(new MappingRecordItemBuilder().setMappingRecord(
375 mappingRecordBuilder.build()).build());
376 mapNotifyBuilder.setNonce((long) 0);
377 mapNotifyBuilder.setKeyId((short) 0);
378 mapNotifyBuilder.setAuthenticationData(new byte[0]);
379 ByteBuffer serialize = MapNotifySerializer.getInstance().serialize(mapNotifyBuilder.build());
380 return new DatagramPacket(wrappedBuffer(serialize), new InetSocketAddress(0), new InetSocketAddress(0));
385 public void mapNotify__VerifyBasicFields() throws Exception {
386 byte registerType = mapRegisterPacket[0];
387 assertEquals(MessageType.MapRegister.getIntValue(), registerType >> 4);
389 stubMapRegister(true);
391 byte[] result = handleMapRegisterAsByteArray(mapRegisterPacket);
393 assertEquals(mapRegisterPacket.length, result.length);
395 byte expectedType = (byte) (MessageType.MapNotify.getIntValue() << 4);
396 assertHexEquals(expectedType, result[0]);
397 assertHexEquals((byte) 0x00, result[1]);
398 assertHexEquals((byte) 0x00, result[2]);
400 byte[] registerWithoutTypeWithoutAuthenticationData = ArrayUtils.addAll(Arrays.copyOfRange(mapRegisterPacket, 3, 16),
401 Arrays.copyOfRange(mapRegisterPacket, 36, mapRegisterPacket.length));
402 byte[] notifyWithoutTypeWithOutAuthenticationData = ArrayUtils.addAll(Arrays.copyOfRange(result, 3, 16),
403 Arrays.copyOfRange(result, 36, result.length));
404 ArrayAssert.assertEquals(registerWithoutTypeWithoutAuthenticationData, notifyWithoutTypeWithOutAuthenticationData);
409 public void mapNotify__VerifyPort() throws Exception {
410 stubMapRegister(true);
412 DatagramPacket notifyPacket = handleMapRegisterPacket(mapRegisterPacket);
413 assertEquals(LispMessage.PORT_NUM, notifyPacket.recipient().getPort());
417 public void mapRequest__VerifyBasicFields() throws Exception {
418 oneOf(nps).putNotification(with(lispNotificationSaver));
419 handleMapRequestAsByteArray(mapRequestPacket);
420 List<EidItem> eids = lastMapRequest().getEidItem();
421 assertEquals(1, eids.size());
422 Eid lispAddress = eids.get(0).getEid();
423 assertEquals(Ipv4PrefixAfi.class, lispAddress.getAddressType());
424 assertEquals(LispAddressUtil.asIpv4PrefixEid("1.2.3.4/32"), lispAddress);
425 assertEquals(0x3d8d2acd39c8d608L, lastMapRequest().getNonce().longValue());
429 public void mapRequest__Ipv6Eid() throws Exception {
430 // Internet Protocol Version 6, Src: 2610:d0:ffff:192::1
431 // (2610:d0:ffff:192::1), Dst: 2610:d0:ffff:192::2
432 // (2610:d0:ffff:192::2)
435 // Source EID: 2610:d0:ffff:192::1 (2610:d0:ffff:192::1)
436 // ITR-RLOC 1: 10.0.58.156
437 // Record 1: 2610:d0:ffff:192::2/128
438 // Map-Reply Record: EID prefix: 2610:d0:ffff:192::1/128, TTL: 10,
439 // Authoritative, No-Action
441 mapRequestPacket = extractWSUdpByteArray(new String("0000 00 0c 29 34 3e 1b 00 0c 29 f6 d6 0d 08 00 45 00 " //
442 + "0010 00 b0 00 00 40 00 40 11 ea 7b 0a 00 3a 9c 0a 00 "
443 + "0020 01 26 10 f6 10 f6 00 9c 9b 19 80 00 00 00 60 00 "
444 + "0030 00 00 00 68 11 ff 26 10 00 d0 ff ff 01 92 00 00 "
445 + "0040 00 00 00 00 00 01 26 10 00 d0 ff ff 01 92 00 00 "
446 + "0050 00 00 00 00 00 02 10 f6 10 f6 00 68 94 8b 10 00 "
447 + "0060 00 01 ff f5 bf 5d 7b 75 93 e6 00 02 26 10 00 d0 "
448 + "0070 ff ff 01 92 00 00 00 00 00 00 00 01 00 01 0a 00 "
449 + "0080 3a 9c 00 80 00 02 26 10 00 d0 ff ff 01 92 00 00 "
450 + "0090 00 00 00 00 00 02 00 00 00 0a 01 80 10 00 00 00 " //
451 + "00a0 00 02 26 10 00 d0 ff ff 01 92 00 00 00 00 00 00 " //
452 + "00b0 00 01 01 64 ff 00 00 05 00 01 0a 00 3a 9c"));
454 oneOf(nps).putNotification(with(lispNotificationSaver));
457 handleMapRequestAsByteArray(mapRequestPacket);
458 assertEquals(LispAddressUtil.asIpv6Eid("2610:d0:ffff:192:0:0:0:1"), lastMapRequest().getSourceEid().getEid());
459 assertEquals(LispAddressUtil.asIpv6PrefixEid("2610:d0:ffff:192:0:0:0:2/128"), lastMapRequest().getEidItem().get(0).getEid());
464 public void mapRequest__UsesIpv6EncapsulatedUdpPort() throws Exception {
465 // Internet Protocol Version 6, Src: 2610:d0:ffff:192::1
466 // (2610:d0:ffff:192::1), Dst: 2610:d0:ffff:192::2
467 // (2610:d0:ffff:192::2)
468 // encapsulated UDP source port: 4342
470 mapRequestPacket = extractWSUdpByteArray(new String("0000 00 0c 29 34 3e 1b 00 0c 29 f6 d6 0d 08 00 45 00 " //
471 + "0010 00 b0 00 00 40 00 40 11 ea 7b 0a 00 3a 9c 0a 00 "
472 + "0020 01 26 10 f6 10 f6 00 9c 9b 19 80 00 00 00 60 00 "
473 + "0030 00 00 00 68 11 ff 26 10 00 d0 ff ff 01 92 00 00 "
474 + "0040 00 00 00 00 00 01 26 10 00 d0 ff ff 01 92 00 00 "
475 + "0050 00 00 00 00 00 02 10 f6 10 f6 00 68 94 8b 14 00 "
476 + "0060 00 01 ff f5 bf 5d 7b 75 93 e6 00 02 26 10 00 d0 "
477 + "0070 ff ff 01 92 00 00 00 00 00 00 00 01 00 01 0a 00 "
478 + "0080 3a 9c 00 80 00 02 26 10 00 d0 ff ff 01 92 00 00 "
479 + "0090 00 00 00 00 00 02 00 00 00 0a 01 80 10 00 00 00 " //
480 + "00a0 00 02 26 10 00 d0 ff ff 01 92 00 00 00 00 00 00 " //
481 + "00b0 00 01 01 64 ff 00 00 05 00 01 0a 00 3a 9c"));
482 oneOf(nps).putNotification(with(lispNotificationSaver));
485 DatagramPacket replyPacket = handleMapRequestPacket(mapRequestPacket);
486 assertEquals(4342, replyPacket.recipient().getPort());
490 public void mapRequest__WithSourceEid() throws Exception {
491 // encapsulated LISP packet
492 // Source EID = 153.16.254.1
494 mapRequestPacket = extractWSUdpByteArray(new String("0000 00 0c 29 7a ce 83 00 15 17 c6 4a c9 08 00 45 00 " //
495 + "0010 00 78 00 00 40 00 3e 11 ec b1 0a 00 01 26 0a 00 "
496 + "0020 3a 9e 10 f6 10 f6 00 64 c3 a5 80 00 00 00 45 00 "
497 + "0030 00 58 d4 31 00 00 ff 11 31 89 99 10 fe 01 0a 00 "
498 + "0040 14 c8 10 f6 10 f6 00 44 84 ee 10 00 00 01 ba f9 "
499 + "0050 ff 53 27 36 38 3a 00 01 99 10 fe 01 00 01 0a 00 "
500 + "0060 01 26 00 20 00 01 0a 00 14 c8 00 00 00 0a 01 20 "
501 + "0070 10 00 00 00 00 01 99 10 fe 01 01 64 ff 00 00 05 " //
502 + "0080 00 01 0a 00 01 26"));
504 oneOf(nps).putNotification(with(lispNotificationSaver));
507 handleMapRequestAsByteArray(mapRequestPacket);
508 assertEquals(Ipv4Afi.class, lastMapRequest().getSourceEid().getEid().getAddressType());
514 public void mapReply__VerifyBasicIPv4Fields() throws Exception {
515 mappingRecordBuilder.setEid(LispAddressUtil.asIpv4PrefixEid("10.0.20.200/32"));
516 mapReplyBuilder.setNonce(0x3d8d2acd39c8d608L);
520 byte[] result = handleMapRequestAsByteArray(mapRequestPacket);
522 assertEquals(28, result.length);
524 byte expectedLispMessageType = 2;
525 assertEquals(expectedLispMessageType, (byte) (result[LispMessage.Pos.TYPE] >> 4));
526 assertEquals(0x3d8d2acd39c8d608L, ByteUtil.getLong(result, MapReplyIpv4SingleLocatorPos.NONCE));
528 byte expectedRecordCount = (byte) 1;
529 assertEquals(expectedRecordCount, result[MapReplyIpv4SingleLocatorPos.RECORD_COUNT]);
531 assertEquals(MaskUtil.getMaskForAddress(mappingRecordBuilder.getEid().getAddress()), result[MapReplyIpv4SingleLocatorPos.EID_MASK_LEN]);
532 assertEquals(AddressFamily.IpV4.getIntValue(), ByteUtil.getInt(result, MapReplyIpv4SingleLocatorPos.AFI_TYPE));
533 assertEquals(0x0a0014c8, ByteUtil.getInt(result, MapReplyIpv4SingleLocatorPos.EID_PREFIX));
538 public void mapReply__VerifyBasicIPv6() throws Exception {
539 mappingRecordBuilder.setEid(LispAddressUtil.asIpv6PrefixEid("0:0:0:0:0:0:0:1/128"));
543 byte[] result = handleMapRequestAsByteArray(mapRequestPacket);
545 assertEquals(40, result.length);
547 byte expectedRecordCount = (byte) 1;
548 assertEquals(expectedRecordCount, result[MapReplyIpv4SingleLocatorPos.RECORD_COUNT]);
550 assertEquals(MaskUtil.getMaskForAddress(mappingRecordBuilder.getEid().getAddress()), result[MapReplyIpv4SingleLocatorPos.EID_MASK_LEN]);
551 assertEquals(AddressFamily.IpV6.getIntValue(), ByteUtil.getInt(result, MapReplyIpv4SingleLocatorPos.AFI_TYPE));
552 byte[] expectedIpv6 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
554 ArrayAssert.assertEquals(expectedIpv6, Arrays.copyOfRange(result, 24, 40));
559 public void mapReply__VerifyIPv6EidAndLocator() throws Exception {
560 mappingRecordBuilder.setEid(LispAddressUtil.asIpv6PrefixEid("0:0:0:0:0:0:0:1/128"));
561 mappingRecordBuilder.getLocatorRecord().add(
562 new LocatorRecordBuilder().setRloc(LispAddressUtil.asIpv6Rloc("0:0:0:0:0:0:0:2")).build());
566 byte[] result = handleMapRequestAsByteArray(mapRequestPacket);
568 assertEquals(64, result.length);
570 byte[] expectedIpv6Eid = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
571 ArrayAssert.assertEquals(expectedIpv6Eid, Arrays.copyOfRange(result, 24, 40));
573 byte[] expectedIpv6Rloc = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2 };
574 ArrayAssert.assertEquals(expectedIpv6Rloc, Arrays.copyOfRange(result, 48, 64));
579 public void mapReply__UseEncapsulatedUdpPort() throws Exception {
582 assertEquals(LispMessage.PORT_NUM, handleMapRequestPacket(mapRequestPacket).recipient().getPort());
587 public void mapReply__WithNonRoutableSingleLocator() throws Exception {
588 mappingRecordBuilder.setEid(LispAddressUtil.asIpv4PrefixEid("10.0.20.200/32"));
589 mappingRecordBuilder.getLocatorRecord().add(
590 new LocatorRecordBuilder().setRouted(false).setRloc(LispAddressUtil.asIpv4Rloc("4.3.2.1")).build());
593 byte[] result = handleMapRequestAsByteArray(mapRequestPacket);
594 assertEquals(0x00, result[MapReplyIpv4SingleLocatorPos.LOCATOR_RBIT] & 0x01);
599 public void mapReply__WithSingleLocator() throws Exception {
600 mappingRecordBuilder.setEid(LispAddressUtil.asIpv4PrefixEid("10.0.20.200/32"));
601 mappingRecordBuilder.getLocatorRecord().add(
602 new LocatorRecordBuilder().setRouted(true).setRloc(LispAddressUtil.asIpv4Rloc("4.3.2.1")).build());
605 byte[] result = handleMapRequestAsByteArray(mapRequestPacket);
606 assertEquals(40, result.length);
608 byte expectedLocCount = 1;
609 assertEquals(expectedLocCount, result[MapReplyIpv4SingleLocatorPos.LOCATOR_COUNT]);
611 assertEquals(AddressFamily.IpV4.getIntValue(), ByteUtil.getInt(result, MapReplyIpv4SingleLocatorPos.LOC_AFI));
613 assertEquals(0x04030201, ByteUtil.getInt(result, MapReplyIpv4SingleLocatorPos.LOCATOR));
614 assertEquals(0x01, result[MapReplyIpv4SingleLocatorPos.LOCATOR_RBIT] & 0x01);
619 public void mapReply__WithMultipleLocator() throws Exception {
620 mappingRecordBuilder.getLocatorRecord().add(
621 new LocatorRecordBuilder().setRouted(true).setRloc(LispAddressUtil.asIpv4Rloc("4.3.2.1")).build());
622 mappingRecordBuilder.getLocatorRecord().add(
623 new LocatorRecordBuilder().setRouted(true).setRloc(LispAddressUtil.asIpv6Rloc("0:0:0:0:0:0:0:1")).build());
626 byte[] result = handleMapRequestAsByteArray(mapRequestPacket);
627 assertEquals(64, result.length);
629 assertEquals(2, result[MapReplyIpv4SingleLocatorPos.LOCATOR_COUNT]);
631 assertEquals(AddressFamily.IpV4.getIntValue(), ByteUtil.getInt(result, MapReplyIpv4SingleLocatorPos.LOC_AFI));
632 assertEquals(0x04030201, ByteUtil.getInt(result, MapReplyIpv4SingleLocatorPos.LOCATOR));
633 assertEquals(0x01, result[MapReplyIpv4SingleLocatorPos.LOCATOR_RBIT] & 0x01);
635 assertEquals(AddressFamily.IpV6.getIntValue(), ByteUtil.getInt(result, MapReplyIpv4SecondLocatorPos.LOC_AFI));
637 byte[] expectedIpv6Rloc = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
638 ArrayAssert.assertEquals(expectedIpv6Rloc,
639 Arrays.copyOfRange(result, MapReplyIpv4SecondLocatorPos.LOCATOR, MapReplyIpv4SecondLocatorPos.LOCATOR + 16));
641 assertEquals(0x01, result[MapReplyIpv4SecondLocatorPos.LOCATOR_RBIT] & 0x01);
645 public void handleUnknownLispMessage() throws Exception {
646 // IP: 192.168.136.10 -> 128.223.156.35
647 // UDP: 49289 -> 4342
648 // LISP(Type = 14 UNKNOWN!!!, P=1, M=1
650 byte[] unknownTypePacket = extractWSUdpByteArray(new String("0000 00 50 56 ee d1 4f 00 0c 29 7a ce 79 08 00 45 00 " //
651 + "0010 00 5c 00 00 40 00 40 11 d4 db c0 a8 88 0a 80 df "
652 + "0020 9c 23 d6 40 10 f6 00 48 59 a4 F8 00 01 01 00 00 "
653 + "0030 00 00 00 00 00 00 00 01 00 14 e8 f5 0b c5 c5 f2 "
654 + "0040 b0 21 27 a8 21 41 04 f3 46 5a a5 68 89 ec 00 00 "
655 + "0050 00 0a 01 20 10 00 00 00 00 01 99 10 fe 01 01 64 " //
656 + "0060 ff 00 00 05 00 01 c0 a8 88 0a"));
657 assertNull(handlePacket(unknownTypePacket));
661 public void mapRequest__MultipleItrRlocs() throws Exception {
662 // this is what LISPmob sends when configured multiple RLOCs for single
664 // ITR-RLOC 1: 10.1.0.111
665 // ITR-RLOC 2: 192.168.136.51
667 mapRequestPacket = extractWSUdpByteArray(new String("0000 00 0c 29 7a ce 8d 00 0c 29 e4 ef 70 08 00 45 00 "
668 + "0010 00 8a 00 00 40 00 40 11 25 f2 0a 01 00 6f 0a 01 " //
669 + "0020 00 01 10 f6 10 f6 00 76 06 1f 80 00 00 00 45 00 " //
670 + "0030 00 6a d4 31 00 00 ff 11 2a 3e ac 01 01 02 08 08 " //
671 + "0040 08 08 10 f6 10 f6 00 56 63 14 10 00 01 01 79 67 " //
672 + "0050 ff 75 a0 61 66 19 00 01 ac 01 01 02 00 01 0a 01 " //
673 + "0060 00 6f 00 01 c0 a8 88 33 00 20 00 01 08 08 08 08 " //
674 + "0070 00 00 00 0a 02 20 10 00 00 00 00 01 ac 01 01 02 " //
675 + "0080 01 64 ff 00 00 05 00 01 0a 01 00 6f 06 64 ff 00 " //
676 + "0090 00 05 00 01 c0 a8 88 33"));
678 oneOf(nps).putNotification(with(lispNotificationSaver));
679 handleMapRequestAsByteArray(mapRequestPacket);
683 private void stubMapRegister(final boolean setNotifyFromRegister) {
685 allowing(nps).putNotification(with(lispNotificationSaver));
686 } catch (InterruptedException e) {
688 will(new SimpleAction() {
691 public Object invoke(Invocation invocation) throws Throwable {
692 if (setNotifyFromRegister) {
693 MapNotifyBuilderHelper.setFromMapRegister(mapNotifyBuilder, lastMapRegister());
700 private void stubHandleRequest() {
702 allowing(nps).putNotification(wany(Notification.class));
703 } catch (InterruptedException e) {
707 private byte[] handleMapRequestAsByteArray(byte[] inPacket) {
708 handleMapRequestPacket(inPacket);
709 return lastMapReplyPacket().content().array();
712 private byte[] handleMapRegisterAsByteArray(byte[] inPacket) {
713 handleMapRegisterPacket(inPacket);
714 return lastMapNotifyPacket().content().array();
717 private DatagramPacket handleMapRequestPacket(byte[] inPacket) {
718 DatagramPacket dp = new DatagramPacket(wrappedBuffer(inPacket), new InetSocketAddress(0), new InetSocketAddress(0));
719 // Unless we explicitly set the source port, it will be -1, which breaks some tests
720 // This is till not the real port number, but it's better
721 //dp.setPort(LispMessage.PORT_NUM);
722 testedLispService.handlePacket(dp);
723 return lastMapReplyPacket();
726 private DatagramPacket handleMapRegisterPacket(byte[] inPacket) {
727 DatagramPacket dp = new DatagramPacket(wrappedBuffer(inPacket), new InetSocketAddress(0), new InetSocketAddress(0));
728 // Unless we explicitly set the source port, it will be -1, which breaks some tests
729 // This is till not the real port number, but it's better
730 //dp.setPort(LispMessage.PORT_NUM);
731 testedLispService.handlePacket(dp);
732 if (mapNotifyBuilder == null) {
735 return lastMapNotifyPacket();
739 private DatagramPacket handlePacket(byte[] inPacket) {
740 // TODO get from mock
741 testedLispService.handlePacket(new DatagramPacket(wrappedBuffer(inPacket), new InetSocketAddress(0), new InetSocketAddress(0)));
745 private byte[] extractWSUdpByteArray(String wiresharkHex) {
746 final int HEADER_LEN = 42;
747 byte[] res = new byte[1000];
748 String[] split = wiresharkHex.split(" ");
750 for (String cur : split) {
752 if (cur.length() == 2) {
754 if (counter > HEADER_LEN) {
755 res[counter - HEADER_LEN - 1] = (byte) Integer.parseInt(cur, 16);
760 return Arrays.copyOf(res, counter - HEADER_LEN);
763 @Test(expected = LispMalformedPacketException.class)
764 public void mapRequest__NoIPITRRLOC() throws Exception {
765 mapRequestPacket = hexToByteBuffer("10 00 " //
766 + "02 " // This means 3 ITR - RLOCs
767 + "01 3d 8d 2a cd 39 c8 d6 08 00 00 " //
768 + "40 05 c0 a8 88 0a 01 02 " // MAC (ITR-RLOC #1 of 3)
769 + "40 05 00 00 00 00 00 00 " // MAC (ITR-RLOC #2 of 3)
770 + "40 05 11 22 34 56 78 90 " // MAC (ITR-RLOC #3 of 3)
771 + "00 20 00 01 01 02 03 04").array();
772 handleMapRequestPacket(mapRequestPacket);
777 // public void mapRequest__IPITRRLOCIsSecond() throws Exception {
778 // mapRequestPacket = hexToByteBuffer("10 00 " //
779 // + "01 " // This means 3 ITR - RLOCs
780 // + "01 3d 8d 2a cd 39 c8 d6 08 00 00 " //
781 // + "40 05 c0 a8 88 0a 01 02 " // MAC (ITR-RLOC #1 of 2)
782 // + "00 01 01 02 03 04 " // IP (ITR-RLOC #2 of 2)
783 // + "00 20 00 01 01 02 03 04").array();
784 // oneOf(nps).putNotification(with(lispNotificationSaver));
786 // DatagramPacket packet = handleMapRequestPacket(mapRequestPacket);
787 // assertEquals(2, lastMapRequest().getItrRlocs().size());
788 // assertEquals((new LispIpv4Address("1.2.3.4")).getAddress(),
789 // packet.getAddress());
794 // public void mapRequest__MULTIPLEIPITRRLOCs() throws Exception {
795 // mapRequestPacket = hexToByteBuffer("10 00 " //
796 // + "01 " // This means 3 ITR - RLOCs
797 // + "01 3d 8d 2a cd 39 c8 d6 08 00 00 " //
798 // + "00 01 01 02 03 04 " // IP (ITR-RLOC #1 of 2)
799 // + "00 01 c0 a8 88 0a " // MAC (ITR-RLOC #2 of 2)
800 // + "00 20 00 01 01 02 03 04").array();
801 // oneOf(nps).putNotification(with(lispNotificationSaver));
803 // DatagramPacket packet = handleMapRequestPacket(mapRequestPacket);
804 // assertEquals(2, lastMapRequest().getItrRloc().size());
805 // assertEquals((new LispIpv4Address("1.2.3.4")).getAddress(),
806 // packet.getAddress());