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.serializer;
11 import static junit.framework.Assert.assertEquals;
13 import java.nio.ByteBuffer;
15 import junitx.framework.ArrayAssert;
17 import org.junit.Test;
18 import org.opendaylight.lispflowmapping.tools.junit.BaseTestCase;
19 import org.opendaylight.lispflowmapping.type.lisp.EidToLocatorRecord;
20 import org.opendaylight.lispflowmapping.type.lisp.MapNotify;
21 import org.opendaylight.lispflowmapping.type.lisp.address.LispIpv4Address;
22 import org.opendaylight.lispflowmapping.type.lisp.address.LispNoAddress;
24 public class MapNotifySerializationTest extends BaseTestCase {
27 public void serialize__Fields() throws Exception {
28 MapNotify mn = new MapNotify();
29 mn.addEidToLocator(new EidToLocatorRecord().setPrefix(new LispIpv4Address(1)));
31 mn.addEidToLocator(new EidToLocatorRecord().setPrefix(new LispIpv4Address(73)));
32 mn.setNonce(6161616161L);
33 mn.setKeyId((short) 0xABCD);
34 byte[] authenticationData = new byte[] { 0x03, 0x01, 0x04, 0x01, 0x05, 0x09, 0x02, 0x06 };
35 mn.setAuthenticationData(authenticationData);
37 ByteBuffer bb = MapNotifySerializer.getInstance().serialize(mn);
38 assertHexEquals((byte) 0x40, bb.get()); // Type + MSByte of reserved
39 assertEquals(0, bb.getShort()); // Rest of reserved
40 assertEquals(2, bb.get()); // Record Count
41 assertEquals(6161616161L, bb.getLong()); // Nonce
42 assertHexEquals((short) 0xABCD, bb.getShort()); // Key ID
43 assertEquals(authenticationData.length, bb.getShort());
45 byte[] actualAuthenticationData = new byte[8];
46 bb.get(actualAuthenticationData);
47 ArrayAssert.assertEquals(authenticationData, actualAuthenticationData);
49 bb.position(bb.position() + 12); /* EID in first record */
50 assertEquals(0x1, bb.getInt());
52 bb.position(bb.position() + 12); /* EID in second record */
53 assertEquals(73, bb.getInt());
55 assertEquals(bb.position(), bb.capacity());
59 public void serialize__NoAuthenticationData() throws Exception {
60 MapNotify mn = new MapNotify();
61 mn.addEidToLocator(new EidToLocatorRecord().setPrefix(new LispIpv4Address(1)).setRecordTtl(55));
63 ByteBuffer bb = MapNotifySerializer.getInstance().serialize(mn);
64 bb.position(bb.position() + 14); // jump to AuthenticationDataLength
65 assertEquals(0, bb.getShort());
66 assertEquals(55, bb.getInt());
68 mn.setAuthenticationData(null);
70 bb = MapNotifySerializer.getInstance().serialize(mn);
71 bb.position(bb.position() + 14); // jump to AuthenticationDataLength
72 assertEquals(0, bb.getShort());
73 assertEquals(55, bb.getInt());
75 mn.setAuthenticationData(new byte[0]);
77 bb = MapNotifySerializer.getInstance().serialize(mn);
78 bb.position(bb.position() + 14); // jump to AuthenticationDataLength
79 assertEquals(0, bb.getShort());
80 assertEquals(55, bb.getInt());
84 public void serialize__NoPrefixInEidToLocator() throws Exception {
85 MapNotify mn = new MapNotify();
86 mn.addEidToLocator(new EidToLocatorRecord());
87 mn.addEidToLocator(new EidToLocatorRecord().setPrefix(null));
88 mn.addEidToLocator(new EidToLocatorRecord().setPrefix(new LispNoAddress()));
90 ByteBuffer bb = MapNotifySerializer.getInstance().serialize(mn);
91 bb.position(bb.position() + 26); // jump to first record prefix AFI
92 assertEquals(0, bb.getShort());
94 bb.position(bb.position() + 10); // jump to second record prefix AFI
95 assertEquals(0, bb.getShort());
97 bb.position(bb.position() + 10); // jump to third record prefix AFI
98 assertEquals(0, bb.getShort());