ignore transiant files in git
[lispflowmapping.git] / mappingservice / southbound / src / test / java / org / opendaylight / lispflowmapping / southbound / serializer / MapNotifySerializationTest.java
1 /*
2  * Copyright (c) 2013 Contextream, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.lispflowmapping.southbound.serializer;
10
11 import static junit.framework.Assert.assertEquals;
12
13 import java.nio.ByteBuffer;
14
15 import junitx.framework.ArrayAssert;
16
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;
23
24 public class MapNotifySerializationTest extends BaseTestCase {
25
26     @Test
27     public void serialize__Fields() throws Exception {
28         MapNotify mn = new MapNotify();
29         mn.addEidToLocator(new EidToLocatorRecord().setPrefix(new LispIpv4Address(1)));
30
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);
36
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());
44
45         byte[] actualAuthenticationData = new byte[8];
46         bb.get(actualAuthenticationData);
47         ArrayAssert.assertEquals(authenticationData, actualAuthenticationData);
48
49         bb.position(bb.position() + 12); /* EID in first record */
50         assertEquals(0x1, bb.getInt());
51
52         bb.position(bb.position() + 12); /* EID in second record */
53         assertEquals(73, bb.getInt());
54
55         assertEquals(bb.position(), bb.capacity());
56     }
57
58     @Test
59     public void serialize__NoAuthenticationData() throws Exception {
60         MapNotify mn = new MapNotify();
61         mn.addEidToLocator(new EidToLocatorRecord().setPrefix(new LispIpv4Address(1)).setRecordTtl(55));
62
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());
67
68         mn.setAuthenticationData(null);
69
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());
74
75         mn.setAuthenticationData(new byte[0]);
76
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());
81     }
82
83     @Test
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()));
89
90         ByteBuffer bb = MapNotifySerializer.getInstance().serialize(mn);
91         bb.position(bb.position() + 26); // jump to first record prefix AFI
92         assertEquals(0, bb.getShort());
93
94         bb.position(bb.position() + 10); // jump to second record prefix AFI
95         assertEquals(0, bb.getShort());
96
97         bb.position(bb.position() + 10); // jump to third record prefix AFI
98         assertEquals(0, bb.getShort());
99     }
100 }