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.lisp;
11 import static org.junit.Assert.assertEquals;
12 import junitx.framework.ArrayAssert;
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO;
17 import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO.MappingEntry;
18 import org.opendaylight.lispflowmapping.interfaces.dao.MappingServiceKey;
19 import org.opendaylight.lispflowmapping.interfaces.dao.MappingServiceNoMaskKey;
20 import org.opendaylight.lispflowmapping.interfaces.dao.MappingServiceValue;
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.MapNotify;
25 import org.opendaylight.lispflowmapping.type.lisp.MapRegister;
26 import org.opendaylight.lispflowmapping.type.lisp.address.IMaskable;
27 import org.opendaylight.lispflowmapping.type.lisp.address.LispAddress;
28 import org.opendaylight.lispflowmapping.type.lisp.address.LispIpv4Address;
29 import org.opendaylight.lispflowmapping.type.lisp.address.LispIpv6Address;
30 import org.opendaylight.lispflowmapping.type.lisp.address.LispNoAddress;
32 public class MapServerTest extends BaseTestCase {
34 private MapServer testedMapServer;
35 private ILispDAO lispDAO;
36 private MapRegister mapRegister;
37 private LispIpv4Address eid;
38 private LispIpv4Address rloc;
39 private ValueSaverAction<MappingEntry<?>[]> mappingEntriesSaver;
43 public void before() throws Exception {
45 lispDAO = context.mock(ILispDAO.class);
46 testedMapServer = new MapServer(lispDAO);
47 mapRegister = new MapRegister();
48 eid = new LispIpv4Address("10.31.0.5");
49 rloc = new LispIpv4Address(0xC0A8880A);
50 EidToLocatorRecord record = new EidToLocatorRecord();
51 record.setPrefix(eid);
52 record.addLocator(new LocatorRecord().setLocator(rloc));
53 mapRegister.addEidToLocator(record);
55 mappingEntriesSaver = new ValueSaverAction<MappingEntry<?>[]>();
59 public void handleMapRegister__NonSetMBit() throws Exception {
60 mapRegister.setWantMapNotify(false);
63 assertNull(testedMapServer.handleMapRegister(mapRegister));
65 MappingEntry<?>[] entries = mappingEntriesSaver.lastValue;
66 assertEquals(2, entries.length);
68 assertEquals("NumRLOCs", entries[0].getKey());
69 assertEquals(1, entries[0].getValue());
70 assertEquals("RLOC0", entries[1].getKey());
71 assertEquals(rloc, ((MappingServiceValue) entries[1].getValue()).getRecord().getLocator());
75 public void handleMapRegisterIpv4__ValidNotifyEchoesRegister() throws Exception {
76 mapRegister.addEidToLocator(new EidToLocatorRecord().setPrefix(new LispNoAddress()));
77 mapRegister.setWantMapNotify(true);
80 MapNotify mapNotify = testedMapServer.handleMapRegister(mapRegister);
81 assertEquals(mapRegister.getEidToLocatorRecords(), mapNotify.getEidToLocatorRecords());
82 ArrayAssert.assertEquals(mapRegister.getAuthenticationData(), mapNotify.getAuthenticationData());
83 assertEquals(mapRegister.getKeyId(), mapNotify.getKeyId());
84 assertEquals(mapRegister.getNonce(), mapNotify.getNonce());
88 public void handleMapRegisterIpv4__CloneNotOwnYouClown() throws Exception {
89 mapRegister = new MapRegister();
90 mapRegister.setKeyId((byte) 0);
91 mapRegister.setWantMapNotify(true);
92 EidToLocatorRecord eidToLocator = new EidToLocatorRecord();
93 eid = new LispIpv4Address(1);
94 eidToLocator.setPrefix(eid);
96 LocatorRecord locator = new LocatorRecord();
97 locator.setLocator(new LispIpv4Address(2));
98 locator.setPriority((byte) 55);
99 eidToLocator.addLocator(locator);
101 mapRegister.addEidToLocator(eidToLocator);
105 MapNotify mapNotify = testedMapServer.handleMapRegister(mapRegister);
107 mapRegister.getEidToLocatorRecords().get(0).setPrefix(new LispIpv4Address(55));
108 mapRegister.getEidToLocatorRecords().get(0).getLocators().get(0).setPriority((byte) 1);
110 EidToLocatorRecord actualEidToLocator = mapNotify.getEidToLocatorRecords().get(0);
111 assertEquals(new LispIpv4Address(1), actualEidToLocator.getPrefix());
112 assertEquals((byte) 55, actualEidToLocator.getLocators().get(0).getPriority());
117 public void handleMapRegisterIpv4__ValidMask() throws Exception {
119 mapRegister = new MapRegister();
120 EidToLocatorRecord record = new EidToLocatorRecord();
121 record.setPrefix(new LispIpv4Address("10.31.0.5")).setMaskLength(mask);
122 record.addLocator(new LocatorRecord().setLocator(rloc));
123 mapRegister.addEidToLocator(record);
124 mapRegister.setWantMapNotify(true);
126 expectPut(eid, mask);
128 MapNotify mapNotify = testedMapServer.handleMapRegister(mapRegister);
129 assertEquals(new LispIpv4Address("10.31.0.0"), mapNotify.getEidToLocatorRecords().get(0).getPrefix());
130 assertEquals(mapRegister.getEidToLocatorRecords(), mapNotify.getEidToLocatorRecords());
131 ArrayAssert.assertEquals(mapRegister.getAuthenticationData(), mapNotify.getAuthenticationData());
132 assertEquals(mapRegister.getKeyId(), mapNotify.getKeyId());
133 assertEquals(mapRegister.getNonce(), mapNotify.getNonce());
137 public void handleMapRegisterIpv4__ValidMask32() throws Exception {
139 mapRegister = new MapRegister();
140 EidToLocatorRecord record = new EidToLocatorRecord();
141 record.setPrefix(new LispIpv4Address("10.31.0.5")).setMaskLength(mask);
142 record.addLocator(new LocatorRecord().setLocator(rloc));
143 mapRegister.addEidToLocator(record);
144 mapRegister.setWantMapNotify(true);
146 expectPut(eid, mask);
148 MapNotify mapNotify = testedMapServer.handleMapRegister(mapRegister);
149 assertEquals(new LispIpv4Address("10.31.0.5"), mapNotify.getEidToLocatorRecords().get(0).getPrefix());
150 assertEquals(mapRegister.getEidToLocatorRecords(), mapNotify.getEidToLocatorRecords());
151 ArrayAssert.assertEquals(mapRegister.getAuthenticationData(), mapNotify.getAuthenticationData());
152 assertEquals(mapRegister.getKeyId(), mapNotify.getKeyId());
153 assertEquals(mapRegister.getNonce(), mapNotify.getNonce());
157 public void handleMapRegisterIpv6__ValidMask96() throws Exception {
159 mapRegister = new MapRegister();
160 EidToLocatorRecord record = new EidToLocatorRecord();
161 LispIpv6Address addr = new LispIpv6Address("1:1:1:1:1:1:1:0");
162 record.setPrefix(addr).setMaskLength(mask);
163 record.addLocator(new LocatorRecord().setLocator(rloc));
164 mapRegister.addEidToLocator(record);
165 mapRegister.setWantMapNotify(true);
167 expectPut(addr, mask);
169 MapNotify mapNotify = testedMapServer.handleMapRegister(mapRegister);
170 assertEquals(new LispIpv6Address("1:1:1:1:1:1:0:0"), mapNotify.getEidToLocatorRecords().get(0).getPrefix());
171 assertEquals(mapRegister.getEidToLocatorRecords(), mapNotify.getEidToLocatorRecords());
172 ArrayAssert.assertEquals(mapRegister.getAuthenticationData(), mapNotify.getAuthenticationData());
173 assertEquals(mapRegister.getKeyId(), mapNotify.getKeyId());
174 assertEquals(mapRegister.getNonce(), mapNotify.getNonce());
178 public void handleMapRegisterIpv6__ValidMask32() throws Exception {
180 mapRegister = new MapRegister();
181 EidToLocatorRecord record = new EidToLocatorRecord();
182 LispIpv6Address addr = new LispIpv6Address("1:1:0:0:0:0:0:0");
183 record.setPrefix(addr).setMaskLength(mask);
184 record.addLocator(new LocatorRecord().setLocator(rloc));
185 mapRegister.addEidToLocator(record);
186 mapRegister.setWantMapNotify(true);
188 expectPut(addr, mask);
190 MapNotify mapNotify = testedMapServer.handleMapRegister(mapRegister);
191 assertEquals(new LispIpv6Address("1:1:0:0:0:0:0:0"), mapNotify.getEidToLocatorRecords().get(0).getPrefix());
192 assertEquals(mapRegister.getEidToLocatorRecords(), mapNotify.getEidToLocatorRecords());
193 ArrayAssert.assertEquals(mapRegister.getAuthenticationData(), mapNotify.getAuthenticationData());
194 assertEquals(mapRegister.getKeyId(), mapNotify.getKeyId());
195 assertEquals(mapRegister.getNonce(), mapNotify.getNonce());
199 public void handleMapRegisterIpv6__ValidMask128() throws Exception {
201 mapRegister = new MapRegister();
202 EidToLocatorRecord record = new EidToLocatorRecord();
203 LispIpv6Address addr = new LispIpv6Address("1:1:1:1:1:1:1:2");
204 record.setPrefix(addr).setMaskLength(mask);
205 record.addLocator(new LocatorRecord().setLocator(rloc));
206 mapRegister.addEidToLocator(record);
207 mapRegister.setWantMapNotify(true);
209 expectPut(addr, mask);
211 MapNotify mapNotify = testedMapServer.handleMapRegister(mapRegister);
212 assertEquals(new LispIpv6Address("1:1:1:1:1:1:1:2"), mapNotify.getEidToLocatorRecords().get(0).getPrefix());
213 assertEquals(mapRegister.getEidToLocatorRecords(), mapNotify.getEidToLocatorRecords());
214 ArrayAssert.assertEquals(mapRegister.getAuthenticationData(), mapNotify.getAuthenticationData());
215 assertEquals(mapRegister.getKeyId(), mapNotify.getKeyId());
216 assertEquals(mapRegister.getNonce(), mapNotify.getNonce());
221 public void handleMapRegister__MultipleRLOCs() throws Exception {
224 LispIpv4Address rloc0 = rloc;
225 LispIpv6Address rloc1 = new LispIpv6Address("::7");
226 mapRegister.getEidToLocatorRecords().get(0).addLocator(new LocatorRecord().setLocator(rloc1));
228 testedMapServer.handleMapRegister(mapRegister);
230 MappingEntry<?>[] entries = mappingEntriesSaver.lastValue;
231 assertEquals(3, entries.length);
233 assertEquals("NumRLOCs", entries[0].getKey());
234 assertEquals(2, entries[0].getValue());
235 assertEquals("RLOC0", entries[1].getKey());
236 assertEquals(rloc0, ((MappingServiceValue) entries[1].getValue()).getRecord().getLocator());
237 assertEquals("RLOC1", entries[2].getKey());
238 assertEquals(rloc1, ((MappingServiceValue) entries[2].getValue()).getRecord().getLocator());
240 // When refactoring: reconsider arrays/count
243 private void expectPut(LispAddress address, int mask) {
244 if (address instanceof IMaskable && mask > 0 && mask < ((IMaskable)address).getMaxMask()) {
245 ((IMaskable)address).normalize(mask);
246 allowing(lispDAO).put(weq(new MappingServiceKey(address, (byte) mask)), with(mappingEntriesSaver));
248 allowing(lispDAO).put(weq(new MappingServiceNoMaskKey(address)), with(mappingEntriesSaver));