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 availabl at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.lispflowmapping.southbound.serializer;
11 import static org.junit.Assert.assertEquals;
13 import java.nio.ByteBuffer;
15 import junitx.framework.ArrayAssert;
17 import org.junit.Ignore;
18 import org.junit.Test;
19 import org.opendaylight.lispflowmapping.tools.junit.BaseTestCase;
20 import org.opendaylight.lispflowmapping.type.lisp.EidToLocatorRecord;
21 import org.opendaylight.lispflowmapping.type.lisp.LocatorRecord;
22 import org.opendaylight.lispflowmapping.type.lisp.MapReply;
23 import org.opendaylight.lispflowmapping.type.lisp.MapReplyAction;
24 import org.opendaylight.lispflowmapping.type.lisp.address.LispIpv4Address;
25 import org.opendaylight.lispflowmapping.type.lisp.address.LispIpv6Address;
27 public class MapReplySerializationTest extends BaseTestCase {
31 public void todo() throws Exception {
32 fail("support no eid to locator???");
33 fail("Validation of values in setters (map-version)");
34 fail("Non NPE for no locator in LocatorRecord.serialize");
38 public void serialize__SomeFlags() throws Exception {
39 MapReply mr = new MapReply();
40 mr.addEidToLocator(new EidToLocatorRecord().setPrefix(new LispIpv4Address(1)));
42 mr.setEchoNonceEnabled(false);
44 ByteBuffer packet = MapReplySerializer.getInstance().serialize(mr);
45 byte firstByte = packet.get(0);
46 assertHexEquals((byte) 0x28, firstByte);
49 mr.setEchoNonceEnabled(true);
51 packet = MapReplySerializer.getInstance().serialize(mr);
52 firstByte = packet.get(0);
53 assertHexEquals((byte) 0x24, firstByte);
57 public void serialize__MultipleRecordsWithoutRLOCs() throws Exception {
58 MapReply mr = new MapReply();
59 mr.addEidToLocator(new EidToLocatorRecord().setPrefix(new LispIpv6Address("::8")));
60 mr.addEidToLocator(new EidToLocatorRecord().setPrefix(new LispIpv4Address(0x08020405)));
62 ByteBuffer packet = MapReplySerializer.getInstance().serialize(mr);
63 assertEquals(2, packet.get(3));
65 packet.position(24); /* EID in first record */
66 byte[] expected = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08 };
67 byte[] actual = new byte[16];
69 ArrayAssert.assertEquals(expected, actual);
71 packet.position(packet.position() + 12); /* EID in second record */
72 assertEquals(0x08020405, packet.getInt());
76 public void serialize__EidRecordDefaultAction() throws Exception {
77 MapReply mr = new MapReply();
78 EidToLocatorRecord eidToLocator = new EidToLocatorRecord();
79 eidToLocator.setPrefix(new LispIpv4Address(1));
80 mr.addEidToLocator(eidToLocator);
82 ByteBuffer packet = MapReplySerializer.getInstance().serialize(mr);
85 assertHexEquals((byte) 0x00, packet.get()); // MapReplyAction.NoAction
89 public void serialize__EidRecordNullActionShouldTranslateToDefault() throws Exception {
90 MapReply mr = new MapReply();
91 EidToLocatorRecord eidToLocator = new EidToLocatorRecord();
92 eidToLocator.setPrefix(new LispIpv4Address(1));
93 eidToLocator.setAction(null);
94 mr.addEidToLocator(eidToLocator);
96 ByteBuffer packet = MapReplySerializer.getInstance().serialize(mr);
99 assertHexEquals((byte) 0x00, packet.get()); // MapReplyAction.NoAction
103 public void serialize__EidRecordFields() throws Exception {
104 MapReply mr = new MapReply();
106 EidToLocatorRecord eidToLocator1 = new EidToLocatorRecord();
107 eidToLocator1.setPrefix(new LispIpv4Address(1));
108 eidToLocator1.setRecordTtl(7);
109 eidToLocator1.setAction(MapReplyAction.SendMapRequest);
110 eidToLocator1.setAuthoritative(true);
111 eidToLocator1.setMapVersion((short) 3072);
112 mr.addEidToLocator(eidToLocator1);
114 EidToLocatorRecord eidToLocator2 = new EidToLocatorRecord();
115 eidToLocator2.setPrefix(new LispIpv4Address(7));
116 eidToLocator2.setRecordTtl(1000000);
117 eidToLocator2.setAction(MapReplyAction.Drop);
118 eidToLocator2.setAuthoritative(false);
119 eidToLocator2.setMapVersion((short) 29);
120 mr.addEidToLocator(eidToLocator2);
122 ByteBuffer packet = MapReplySerializer.getInstance().serialize(mr);
124 packet.position(12); /* First record */
125 assertEquals(7, packet.getInt());
126 packet.position(packet.position() + 2); /* skip Locator Count & Mask-len */
127 assertHexEquals((byte) 0x50, packet.get());
128 packet.position(packet.position() + 1); /* skip Reserved byte */
129 assertEquals((short) 3072, packet.getShort());
131 packet.position(packet.position() + 6); /* Second record */
132 assertEquals(1000000, packet.getInt());
133 packet.position(packet.position() + 2); /* skip Locator Count & Mask-len */
134 assertHexEquals((byte) 0x60, packet.get());
135 packet.position(packet.position() + 1); /* skip Reserved byte */
136 assertEquals((short) 29, packet.getShort());
140 public void serialize__LocatorRecordFields() throws Exception {
141 MapReply mr = new MapReply();
143 EidToLocatorRecord eidToLocator = new EidToLocatorRecord();
144 eidToLocator.setPrefix(new LispIpv4Address(1));
146 LocatorRecord locator1 = new LocatorRecord();
147 locator1.setPriority((byte) 0xF3);
148 locator1.setWeight((byte) 0xF6);
149 locator1.setMulticastPriority((byte) 0xA3);
150 locator1.setMulticastWeight((byte) 0x06);
151 locator1.setLocator(new LispIpv4Address(1));
152 locator1.setLocalLocator(true);
153 locator1.setRlocProbed(true);
154 locator1.setRouted(true);
155 eidToLocator.addLocator(locator1);
157 LocatorRecord locator2 = new LocatorRecord();
158 locator2.setPriority((byte) 0x03);
159 locator2.setWeight((byte) 0x06);
160 locator2.setMulticastPriority((byte) 0x03);
161 locator2.setMulticastWeight((byte) 0xF1);
162 locator2.setLocator(new LispIpv4Address(2));
163 locator2.setLocalLocator(false);
164 locator2.setRlocProbed(false);
165 locator2.setRouted(false);
166 eidToLocator.addLocator(locator2);
168 mr.addEidToLocator(eidToLocator);
170 ByteBuffer packet = MapReplySerializer.getInstance().serialize(mr);
172 packet.position(12 + 16); /* First locator record */
173 assertHexEquals((byte) 0xF3, packet.get());
174 assertHexEquals((byte) 0xF6, packet.get());
175 assertHexEquals((byte) 0xA3, packet.get());
176 assertHexEquals((byte) 0x06, packet.get());
177 packet.position(packet.position() + 1);
178 assertHexEquals((byte) 0x07, packet.get());
180 packet.position(packet.position() + 6); /* Second locator record */
181 assertHexEquals((byte) 0x03, packet.get());
182 assertHexEquals((byte) 0x06, packet.get());
183 assertHexEquals((byte) 0x03, packet.get());
184 assertHexEquals((byte) 0xF1, packet.get());
185 packet.position(packet.position() + 1);
186 assertHexEquals((byte) 0x00, packet.get());