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.type.lisp;
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.type.lisp.address.LispIpv4Address;
20 import org.opendaylight.lispflowmapping.type.lisp.address.LispIpv6Address;
21 import org.opendaylight.lispflowmapping.tools.junit.BaseTestCase;
23 public class MapReplyTest extends BaseTestCase {
27 public void todo() throws Exception {
28 fail("support no eid to locator???");
29 fail("Validation of values in setters (map-version)");
30 fail("Non NPE for no locator in LocatorRecord.serialize");
34 public void serialize__SomeFlags() throws Exception {
35 MapReply mr = new MapReply();
36 mr.addEidToLocator(new EidToLocatorRecord().setPrefix(new LispIpv4Address(1)));
38 mr.setEchoNonceEnabled(false);
40 ByteBuffer packet = mr.serialize();
41 byte firstByte = packet.get(0);
42 assertHexEquals((byte) 0x28, firstByte);
45 mr.setEchoNonceEnabled(true);
47 packet = mr.serialize();
48 firstByte = packet.get(0);
49 assertHexEquals((byte) 0x24, firstByte);
53 public void serialize__MultipleRecordsWithoutRLOCs() throws Exception {
54 MapReply mr = new MapReply();
55 mr.addEidToLocator(new EidToLocatorRecord().setPrefix(new LispIpv6Address("::8")));
56 mr.addEidToLocator(new EidToLocatorRecord().setPrefix(new LispIpv4Address(0x08020405)));
58 ByteBuffer packet = mr.serialize();
59 assertEquals(2, packet.get(3));
61 packet.position(24); /* EID in first record */
62 byte[] expected = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08 };
63 byte[] actual = new byte[16];
65 ArrayAssert.assertEquals(expected, actual);
67 packet.position(packet.position() + 12); /* EID in second record */
68 assertEquals(0x08020405, packet.getInt());
72 public void serialize__EidRecordDefaultAction() throws Exception {
73 MapReply mr = new MapReply();
74 EidToLocatorRecord eidToLocator = new EidToLocatorRecord();
75 eidToLocator.setPrefix(new LispIpv4Address(1));
76 mr.addEidToLocator(eidToLocator);
78 ByteBuffer packet = mr.serialize();
81 assertHexEquals((byte) 0x00, packet.get()); // MapReplyAction.NoAction
85 public void serialize__EidRecordNullActionShouldTranslateToDefault() throws Exception {
86 MapReply mr = new MapReply();
87 EidToLocatorRecord eidToLocator = new EidToLocatorRecord();
88 eidToLocator.setPrefix(new LispIpv4Address(1));
89 eidToLocator.setAction(null);
90 mr.addEidToLocator(eidToLocator);
92 ByteBuffer packet = mr.serialize();
95 assertHexEquals((byte) 0x00, packet.get()); // MapReplyAction.NoAction
99 public void serialize__EidRecordFields() throws Exception {
100 MapReply mr = new MapReply();
102 EidToLocatorRecord eidToLocator1 = new EidToLocatorRecord();
103 eidToLocator1.setPrefix(new LispIpv4Address(1));
104 eidToLocator1.setRecordTtl(7);
105 eidToLocator1.setAction(MapReplyAction.SendMapRequest);
106 eidToLocator1.setAuthoritative(true);
107 eidToLocator1.setMapVersion((short) 3072);
108 mr.addEidToLocator(eidToLocator1);
110 EidToLocatorRecord eidToLocator2 = new EidToLocatorRecord();
111 eidToLocator2.setPrefix(new LispIpv4Address(7));
112 eidToLocator2.setRecordTtl(1000000);
113 eidToLocator2.setAction(MapReplyAction.Drop);
114 eidToLocator2.setAuthoritative(false);
115 eidToLocator2.setMapVersion((short) 29);
116 mr.addEidToLocator(eidToLocator2);
118 ByteBuffer packet = mr.serialize();
120 packet.position(12); /* First record */
121 assertEquals(7, packet.getInt());
122 packet.position(packet.position() + 2); /* skip Locator Count & Mask-len */
123 assertHexEquals((byte) 0x50, packet.get());
124 packet.position(packet.position() + 1); /* skip Reserved byte */
125 assertEquals((short) 3072, packet.getShort());
127 packet.position(packet.position() + 6); /* Second record */
128 assertEquals(1000000, packet.getInt());
129 packet.position(packet.position() + 2); /* skip Locator Count & Mask-len */
130 assertHexEquals((byte) 0x60, packet.get());
131 packet.position(packet.position() + 1); /* skip Reserved byte */
132 assertEquals((short) 29, packet.getShort());
136 public void serialize__LocatorRecordFields() throws Exception {
137 MapReply mr = new MapReply();
139 EidToLocatorRecord eidToLocator = new EidToLocatorRecord();
140 eidToLocator.setPrefix(new LispIpv4Address(1));
142 LocatorRecord locator1 = new LocatorRecord();
143 locator1.setPriority((byte) 0xF3);
144 locator1.setWeight((byte) 0xF6);
145 locator1.setMulticastPriority((byte) 0xA3);
146 locator1.setMulticastWeight((byte) 0x06);
147 locator1.setLocator(new LispIpv4Address(1));
148 locator1.setLocalLocator(true);
149 locator1.setRlocProbed(true);
150 locator1.setRouted(true);
151 eidToLocator.addLocator(locator1);
153 LocatorRecord locator2 = new LocatorRecord();
154 locator2.setPriority((byte) 0x03);
155 locator2.setWeight((byte) 0x06);
156 locator2.setMulticastPriority((byte) 0x03);
157 locator2.setMulticastWeight((byte) 0xF1);
158 locator2.setLocator(new LispIpv4Address(2));
159 locator2.setLocalLocator(false);
160 locator2.setRlocProbed(false);
161 locator2.setRouted(false);
162 eidToLocator.addLocator(locator2);
164 mr.addEidToLocator(eidToLocator);
166 ByteBuffer packet = mr.serialize();
168 packet.position(12 + 16); /* First locator record */
169 assertHexEquals((byte) 0xF3, packet.get());
170 assertHexEquals((byte) 0xF6, packet.get());
171 assertHexEquals((byte) 0xA3, packet.get());
172 assertHexEquals((byte) 0x06, packet.get());
173 packet.position(packet.position() + 1);
174 assertHexEquals((byte) 0x07, packet.get());
176 packet.position(packet.position() + 6); /* Second locator record */
177 assertHexEquals((byte) 0x03, packet.get());
178 assertHexEquals((byte) 0x06, packet.get());
179 assertHexEquals((byte) 0x03, packet.get());
180 assertHexEquals((byte) 0xF1, packet.get());
181 packet.position(packet.position() + 1);
182 assertHexEquals((byte) 0x00, packet.get());