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.implementation.serializer;
11 import static junit.framework.Assert.assertEquals;
12 import static junit.framework.Assert.assertFalse;
13 import static junit.framework.Assert.assertTrue;
15 import java.nio.ByteBuffer;
17 import junitx.framework.ArrayAssert;
19 import org.junit.Test;
20 import org.opendaylight.lispflowmapping.implementation.lisp.exception.LispSerializationException;
21 import org.opendaylight.lispflowmapping.tools.junit.BaseTestCase;
22 import org.opendaylight.lispflowmapping.type.lisp.EidToLocatorRecord;
23 import org.opendaylight.lispflowmapping.type.lisp.LocatorRecord;
24 import org.opendaylight.lispflowmapping.type.lisp.MapRegister;
25 import org.opendaylight.lispflowmapping.type.lisp.MapReplyAction;
26 import org.opendaylight.lispflowmapping.type.lisp.address.LispIpv4Address;
27 import org.opendaylight.lispflowmapping.type.lisp.address.LispNoAddress;
29 public class MapRegisterSerializationTest extends BaseTestCase {
32 public void serialize__Fields() throws Exception {
33 MapRegister mr = new MapRegister();
34 mr.addEidToLocator(new EidToLocatorRecord().setPrefix(new LispIpv4Address(1)));
36 mr.addEidToLocator(new EidToLocatorRecord().setPrefix(new LispIpv4Address(73)));
37 mr.setNonce(6161616161L);
38 mr.setKeyId((short) 0x0001);
39 mr.setWantMapNotify(true);
40 mr.setProxyMapReply(true);
41 byte[] authenticationData = new byte[] { (byte) 0x16, (byte) 0x98, (byte) 0x96, (byte) 0xeb, (byte) 0x88, (byte) 0x2d, (byte) 0x4d,
42 (byte) 0x22, (byte) 0xe5, (byte) 0x8f, (byte) 0xe6, (byte) 0x89, (byte) 0x64, (byte) 0xb9, (byte) 0x17, (byte) 0xa4, (byte) 0xba,
43 (byte) 0x4e, (byte) 0x8c, (byte) 0x41 };
44 mr.setAuthenticationData(authenticationData);
46 ByteBuffer bb = MapRegisterSerializer.getInstance().serialize(mr);
47 assertHexEquals((byte) 0x31, bb.get()); // Type + MSByte of reserved
48 assertEquals(1, bb.getShort()); // Rest of reserved + want map notify
49 assertEquals(2, bb.get()); // Record Count
50 assertEquals(6161616161L, bb.getLong()); // Nonce
51 assertHexEquals((short) 0x0001, bb.getShort()); // Key ID
52 assertEquals(authenticationData.length, bb.getShort());
54 byte[] actualAuthenticationData = new byte[20];
55 bb.get(actualAuthenticationData);
56 ArrayAssert.assertEquals(authenticationData, actualAuthenticationData);
58 bb.position(bb.position() + 12); /* EID in first record */
59 assertEquals(0x1, bb.getInt());
61 bb.position(bb.position() + 12); /* EID in second record */
62 assertEquals(73, bb.getInt());
64 assertEquals(bb.position(), bb.capacity());
68 public void serialize__NoAuthenticationData() throws Exception {
69 MapRegister mr = new MapRegister();
70 mr.addEidToLocator(new EidToLocatorRecord().setPrefix(new LispIpv4Address(1)).setRecordTtl(55));
72 ByteBuffer bb = MapRegisterSerializer.getInstance().serialize(mr);
73 bb.position(bb.position() + 14); // jump to AuthenticationDataLength
74 assertEquals(0, bb.getShort());
75 assertEquals(55, bb.getInt());
77 mr.setAuthenticationData(null);
79 bb = MapRegisterSerializer.getInstance().serialize(mr);
80 bb.position(bb.position() + 14); // jump to AuthenticationDataLength
81 assertEquals(0, bb.getShort());
82 assertEquals(55, bb.getInt());
84 mr.setAuthenticationData(new byte[0]);
86 bb = MapRegisterSerializer.getInstance().serialize(mr);
87 bb.position(bb.position() + 14); // jump to AuthenticationDataLength
88 assertEquals(0, bb.getShort());
89 assertEquals(55, bb.getInt());
93 public void serialize__NoPrefixInEidToLocator() throws Exception {
94 MapRegister mr = new MapRegister();
95 mr.addEidToLocator(new EidToLocatorRecord());
96 mr.addEidToLocator(new EidToLocatorRecord().setPrefix(null));
97 mr.addEidToLocator(new EidToLocatorRecord().setPrefix(new LispNoAddress()));
99 ByteBuffer bb = MapRegisterSerializer.getInstance().serialize(mr);
100 bb.position(bb.position() + 26); // jump to first record prefix AFI
101 assertEquals(0, bb.getShort());
103 bb.position(bb.position() + 10); // jump to second record prefix AFI
104 assertEquals(0, bb.getShort());
106 bb.position(bb.position() + 10); // jump to third record prefix AFI
107 assertEquals(0, bb.getShort());
111 public void deserialize__AllFields() throws Exception {
112 // LISP(Type = 3 Map-Register, P=1, M=1
114 // Nonce: (something)
116 // AuthDataLength: 00 Data:
117 // EID prefix: 153.16.254.1/32 (EID=0x9910FE01), TTL: 10, Authoritative,
119 // Local RLOC: 192.168.136.10 (RLOC=0xC0A8880A), Reachable,
120 // Priority/Weight: 1/100, Multicast Priority/Weight: 255/0
122 MapRegister mr = MapRegisterSerializer.getInstance().deserialize(hexToByteBuffer("38 00 01 01 FF BB " //
123 + "00 00 00 00 00 00 00 00 00 00 00 00 " //
124 + "00 0a 01 20 10 00 00 00 00 01 99 10 fe 01 01 64 " //
125 + "ff 00 00 05 00 01 c0 a8 88 0a"));
127 assertTrue(mr.isProxyMapReply());
128 assertTrue(mr.isWantMapNotify());
130 assertEquals(1, mr.getEidToLocatorRecords().size());
131 assertEquals(0xFFBB000000000000L, mr.getNonce());
132 assertEquals(0x0000, mr.getKeyId());
133 byte[] expectedAuthenticationData = {};
134 ArrayAssert.assertEquals(expectedAuthenticationData, mr.getAuthenticationData());
138 public void deserialize__MultipleRecords() throws Exception {
139 // LISP(Type = 3 Map-Register, P=1, M=1
141 // EID prefixes: 153.16.254.1 -- 152.16.254.1 -- 151.16.254.1 --
143 // Local RLOCs: 192.168.136.10 -- 192.168.136.11 -- 192.168.136.12 --
146 MapRegister mr = MapRegisterSerializer.getInstance().deserialize(hexToByteBuffer("38 00 01 " //
147 + "04 " // Record count
148 + "FF BB 00 00 00 00 00 00 00 01 00 14 87 c1 33 cd " //
149 + "d1 1e bc 80 fd 3e 71 11 81 17 40 74 26 25 44 bd " //
150 + "00 00 00 0a 01 20 10 00 00 00 00 01 99 10 fe 01 01 64 " // Record
152 + "ff 00 00 05 00 01 c0 a8 88 0a " // contd
153 + "00 00 00 0a 01 20 10 00 00 00 00 01 98 10 fe 01 01 64 " // Record
155 + "ff 00 00 05 00 01 c0 a8 88 0b " // contd
156 + "00 00 00 0a 01 20 10 00 00 00 00 01 97 10 fe 01 01 64 " // Record
158 + "ff 00 00 05 00 01 c0 a8 88 0c " // contd
159 + "00 00 00 0a 01 20 10 00 00 00 00 01 96 10 fe 01 01 64 " // Record
161 + "ff 00 00 05 00 01 c0 a8 88 0d " // contd
164 assertEquals(4, mr.getEidToLocatorRecords().size());
165 assertEquals(new LispIpv4Address("153.16.254.1"), mr.getEidToLocatorRecords().get(0).getPrefix());
166 assertEquals(new LispIpv4Address("151.16.254.1"), mr.getEidToLocatorRecords().get(2).getPrefix());
167 assertEquals(new LispIpv4Address("192.168.136.11"), mr.getEidToLocatorRecords().get(1).getLocators().get(0).getLocator());
168 assertEquals(new LispIpv4Address("192.168.136.13"), mr.getEidToLocatorRecords().get(3).getLocators().get(0).getLocator());
172 public void deserialize__Locators() throws Exception {
173 MapRegister mr = MapRegisterSerializer.getInstance().deserialize(hexToByteBuffer("38 00 01 01 " //
174 + "FF BB 00 00 00 00 00 00 00 01 00 14 f1 b8 ab f0 " //
175 + "66 bb 2e ef 12 70 74 46 6f 6b 8e ca bf 1e 68 40 " //
177 + "03 " // Locator Count
178 + "20 10 00 00 00 00 01 99 10 fe 01 "
180 + "01 64 1f 00 " // priorities + weights
182 + "00 01 c0 a8 88 0a " // Locator
184 + "67 00 30 34 " // priorities + weights
186 + "00 01 cc aa AA 11 " // Locator
188 + "60 11 34 A4 " // priorities + weights
190 + "00 01 c0 a8 88 0a " // Locator
193 assertEquals(1, mr.getEidToLocatorRecords().size());
194 EidToLocatorRecord eidToLocator = mr.getEidToLocatorRecords().get(0);
195 assertEquals(3, eidToLocator.getLocators().size());
196 LocatorRecord loc0 = eidToLocator.getLocators().get(0);
197 LocatorRecord loc1 = eidToLocator.getLocators().get(1);
198 LocatorRecord loc2 = eidToLocator.getLocators().get(2);
199 assertEquals((byte) 0x01, loc0.getPriority());
200 assertEquals((byte) 0x67, loc1.getPriority());
201 assertEquals((byte) 0x60, loc2.getPriority());
203 assertEquals((byte) 0x64, loc0.getWeight());
204 assertEquals((byte) 0x00, loc1.getWeight());
205 assertEquals((byte) 0x11, loc2.getWeight());
207 assertEquals((byte) 0x1F, loc0.getMulticastPriority());
208 assertEquals((byte) 0x30, loc1.getMulticastPriority());
209 assertEquals((byte) 0x34, loc2.getMulticastPriority());
211 assertEquals((byte) 0x00, loc0.getMulticastWeight());
212 assertEquals((byte) 0x34, loc1.getMulticastWeight());
213 assertEquals((byte) 0xA4, loc2.getMulticastWeight());
215 assertTrue(loc0.isLocalLocator());
216 assertFalse(loc1.isLocalLocator());
217 assertFalse(loc2.isLocalLocator());
219 assertFalse(loc0.isRlocProbed());
220 assertTrue(loc1.isRlocProbed());
221 assertTrue(loc2.isRlocProbed());
223 assertTrue(loc0.isRouted());
224 assertFalse(loc1.isRouted());
225 assertTrue(loc2.isRouted());
227 assertEquals(new LispIpv4Address(0xC0A8880A), loc0.getLocator());
228 assertEquals(new LispIpv4Address(0xCCAAAA11), loc1.getLocator());
232 public void deserialize__SomeEidToLocatorFiels() throws Exception {
233 // LISP(Type = 3 Map-Register, P=1, M=1
235 // EID prefixes: 153.16.254.1 -- 152.16.254.1 -- 151.16.254.1 --
237 // Local RLOCs: 192.168.136.10 -- 192.168.136.11 -- 192.168.136.12 --
240 MapRegister mr = MapRegisterSerializer.getInstance().deserialize(hexToByteBuffer("38 00 01 " //
241 + "04 " // Record count
242 + "FF BB 00 00 00 00 00 00 00 01 00 14 b9 cd 7b 89 " //
243 + "65 c2 56 03 be dd 81 20 47 e5 c3 4f 56 02 e1 59 " //
244 + "00 00 00 0a 01 20 10 00 00 00 00 01 99 10 fe 01 01 64 " // Record
246 + "ff 00 00 05 00 01 c0 a8 88 0a " // contd
247 + "00 00 00 0b 01 17 50 00 01 11 00 01 98 10 fe 01 01 64 " // Record
249 + "ff 00 00 05 00 01 c0 a8 88 0b " // contd
250 + "00 00 00 0c 01 20 00 00 02 22 00 01 97 10 fe 01 01 64 " // Record
252 + "ff 00 00 05 00 01 c0 a8 88 0c " // contd
253 + "00 00 00 0d 01 20 20 00 03 33 00 01 96 10 fe 01 01 64 " // Record
255 + "ff 00 00 05 00 01 c0 a8 88 0d " // contd
258 assertEquals(4, mr.getEidToLocatorRecords().size());
260 EidToLocatorRecord record0 = mr.getEidToLocatorRecords().get(0);
261 EidToLocatorRecord record1 = mr.getEidToLocatorRecords().get(1);
262 EidToLocatorRecord record2 = mr.getEidToLocatorRecords().get(2);
263 EidToLocatorRecord record3 = mr.getEidToLocatorRecords().get(3);
265 assertEquals(10, record0.getRecordTtl());
266 assertEquals(13, record3.getRecordTtl());
268 assertEquals(32, record0.getMaskLength());
269 assertEquals(23, record1.getMaskLength());
271 assertEquals(MapReplyAction.NoAction, record0.getAction());
272 assertEquals(MapReplyAction.SendMapRequest, record1.getAction());
273 assertEquals(MapReplyAction.NoAction, record2.getAction());
274 assertEquals(MapReplyAction.NativelyForward, record3.getAction());
276 assertTrue(record0.isAuthoritative());
277 assertTrue(record1.isAuthoritative());
278 assertFalse(record2.isAuthoritative());
279 assertFalse(record3.isAuthoritative());
281 assertEquals(0x000, record0.getMapVersion());
282 assertEquals(0x111, record1.getMapVersion());
283 assertEquals(0x222, record2.getMapVersion());
284 assertEquals(0x333, record3.getMapVersion());
288 public void deserialize__IllegalAction() throws Exception {
289 MapRegister mr = MapRegisterSerializer.getInstance().deserialize(hexToByteBuffer("38 00 01 01 FF BB " //
290 + "00 00 00 00 00 00 00 01 00 14 ec 47 1e 53 25 91 " //
291 + "2f 68 10 75 13 dd 2c e8 6e 3c ac 94 ed e4 00 00 " //
292 + "00 0a 01 20 F0 00 00 00 00 01 99 10 fe 01 01 64 " //
293 + "ff 00 00 05 00 01 c0 a8 88 0a"));
295 assertEquals(1, mr.getEidToLocatorRecords().size());
296 assertEquals(MapReplyAction.NoAction, mr.getEidToLocatorRecords().get(0).getAction());
299 @Test(expected = LispSerializationException.class)
300 public void deserialize_WrongAuthenticationLength() throws Exception {
301 // LISP(Type = 3 Map-Register, P=1, M=1
303 // Nonce: (something)
305 // AuthDataLength: 20 Data:
306 // e8:f5:0b:c5:c5:f2:b0:21:27:a8:a5:68:89:ec
307 // EID prefix: 153.16.254.1/32 (EID=0x9910FE01), TTL: 10, Authoritative,
309 // Local RLOC: 192.168.136.10 (RLOC=0xC0A8880A), Reachable,
310 // Priority/Weight: 1/100, Multicast Priority/Weight: 255/0
312 MapRegisterSerializer.getInstance().deserialize(hexToByteBuffer("38 00 01 01 FF BB " //
313 + "00 00 00 00 00 00 00 00 00 14 e8 f5 0b c5 c5 f2 " //
314 + "b0 21 27 a8 21 a5 68 89 ec 00 00 " //
315 + "00 0a 01 20 10 00 00 00 00 01 99 10 fe 01 01 64 " //
316 + "ff 00 00 05 00 01 c0 a8 88 0a"));
320 public void deserialize__SHA1() throws Exception {
321 // LISP(Type = 3 Map-Register, P=1, M=1
323 // Nonce: (something)
325 // AuthDataLength: 20 Data:
326 // b2:dd:1a:25:c0:60:b1:46:e8:dc:6d:a6:ae:2e:92:92:a6:ca:b7:9d
327 // EID prefix: 153.16.254.1/32 (EID=0x9910FE01), TTL: 10, Authoritative,
329 // Local RLOC: 192.168.136.10 (RLOC=0xC0A8880A), Reachable,
330 // Priority/Weight: 1/100, Multicast Priority/Weight: 255/0
332 MapRegister mr = MapRegisterSerializer.getInstance().deserialize(hexToByteBuffer("38 00 01 01 FF BB " //
333 + "00 00 00 00 00 00 00 01 00 14 2c 61 b9 c9 9a 20 " //
334 + "ba d8 f5 40 d3 55 6f 5f 6e 5a b2 0a bf b5 00 00 " //
335 + "00 0a 01 20 10 00 00 00 00 01 99 10 fe 01 01 64 " //
336 + "ff 00 00 05 00 01 c0 a8 88 0a"));
338 assertTrue(mr.isProxyMapReply());
339 assertTrue(mr.isWantMapNotify());
341 assertEquals(1, mr.getEidToLocatorRecords().size());
342 assertEquals(0xFFBB000000000000L, mr.getNonce());
343 assertEquals(0x0001, mr.getKeyId());
344 byte[] expectedAuthenticationData = { (byte) 0x2c, (byte) 0x61, (byte) 0xb9, (byte) 0xc9, (byte) 0x9a, (byte) 0x20, (byte) 0xba, (byte) 0xd8, //
345 (byte) 0xf5, (byte) 0x40, (byte) 0xd3, (byte) 0x55, (byte) 0x6f, (byte) 0x5f, (byte) 0x6e, (byte) 0x5a, //
346 (byte) 0xb2, (byte) 0x0a, (byte) 0xbf, (byte) 0xb5 };
347 ArrayAssert.assertEquals(expectedAuthenticationData, mr.getAuthenticationData());
351 public void deserialize__SHA256() throws Exception {
352 // LISP(Type = 3 Map-Register, P=1, M=1
354 // Nonce: (something)
356 // AuthDataLength: 32 Data:
357 // 70 30 d4 c6 10 44 0d 83 be 4d bf fd a9 8c 57 6d 68 a5 bf 32 11 c9 7b
358 // 58 c4 b9 9f 06 11 23 b9 38
359 // EID prefix: 153.16.254.1/32 (EID=0x9910FE01), TTL: 10, Authoritative,
361 // Local RLOC: 192.168.136.10 (RLOC=0xC0A8880A), Reachable,
362 // Priority/Weight: 1/100, Multicast Priority/Weight: 255/0
364 MapRegister mr = MapRegisterSerializer.getInstance().deserialize(hexToByteBuffer("38 00 01 01 FF BB " //
365 + "00 00 00 00 00 00 00 02 00 20 70 30 d4 c6 10 44 0d 83 be 4d bf fd a9 8c 57 6d 68 a5 bf 32 " //
366 + "11 c9 7b 58 c4 b9 9f 06 11 23 b9 38 00 00 " //
367 + "00 0a 01 20 10 00 00 00 00 01 99 10 fe 01 01 64 " //
368 + "ff 00 00 05 00 01 c0 a8 88 0a"));
370 assertTrue(mr.isProxyMapReply());
371 assertTrue(mr.isWantMapNotify());
373 assertEquals(1, mr.getEidToLocatorRecords().size());
374 assertEquals(0xFFBB000000000000L, mr.getNonce());
375 assertEquals(0x0002, mr.getKeyId());
376 byte[] expectedAuthenticationData = { (byte) 0x70, (byte) 0x30, (byte) 0xd4, (byte) 0xc6, (byte) 0x10, (byte) 0x44, (byte) 0x0d, (byte) 0x83,
377 (byte) 0xbe, (byte) 0x4d, (byte) 0xbf, (byte) 0xfd, (byte) 0xa9, (byte) 0x8c, (byte) 0x57, (byte) 0x6d, (byte) 0x68, (byte) 0xa5,
378 (byte) 0xbf, (byte) 0x32, (byte) 0x11, (byte) 0xc9, (byte) 0x7b, (byte) 0x58, (byte) 0xc4, (byte) 0xb9, (byte) 0x9f, (byte) 0x06,
379 (byte) 0x11, (byte) 0x23, (byte) 0xb9, (byte) 0x38 };
380 ArrayAssert.assertEquals(expectedAuthenticationData, mr.getAuthenticationData());