support mask=0 TELSDN-369: #close
[lispflowmapping.git] / mappingservice / implementation / src / test / java / org / opendaylight / lispflowmapping / implementation / lisp / MapServerTest.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.implementation.lisp;
10
11 import static org.junit.Assert.assertEquals;
12
13 import java.util.HashMap;
14 import java.util.Map;
15
16 import junitx.framework.ArrayAssert;
17
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.opendaylight.lispflowmapping.implementation.serializer.MapRegisterSerializer;
21 import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO;
22 import org.opendaylight.lispflowmapping.interfaces.dao.IMappingServiceKey;
23 import org.opendaylight.lispflowmapping.interfaces.dao.MappingEntry;
24 import org.opendaylight.lispflowmapping.interfaces.dao.MappingServiceKeyUtil;
25 import org.opendaylight.lispflowmapping.interfaces.dao.MappingServiceValue;
26 import org.opendaylight.lispflowmapping.tools.junit.BaseTestCase;
27 import org.opendaylight.lispflowmapping.type.lisp.EidToLocatorRecord;
28 import org.opendaylight.lispflowmapping.type.lisp.LocatorRecord;
29 import org.opendaylight.lispflowmapping.type.lisp.MapNotify;
30 import org.opendaylight.lispflowmapping.type.lisp.MapRegister;
31 import org.opendaylight.lispflowmapping.type.lisp.address.IMaskable;
32 import org.opendaylight.lispflowmapping.type.lisp.address.LispAddress;
33 import org.opendaylight.lispflowmapping.type.lisp.address.LispIpv4Address;
34 import org.opendaylight.lispflowmapping.type.lisp.address.LispIpv6Address;
35 import org.opendaylight.lispflowmapping.type.lisp.address.LispMACAddress;
36 import org.opendaylight.lispflowmapping.type.lisp.address.LispNoAddress;
37
38 public class MapServerTest extends BaseTestCase {
39
40     private MapServer testedMapServer;
41     private ILispDAO lispDAO;
42     private MapRegister mapRegister;
43     private LispIpv4Address eid;
44     private LispIpv4Address rloc;
45     private ValueSaverAction<MappingEntry<?>[]> mappingEntriesSaver;
46     private MapRegister mapRegisterWithAuthentication;
47
48     @Override
49     @Before
50     public void before() throws Exception {
51         super.before();
52         lispDAO = context.mock(ILispDAO.class);
53         testedMapServer = new MapServer(lispDAO);
54         mapRegister = new MapRegister();
55         eid = new LispIpv4Address("10.31.0.5");
56         rloc = new LispIpv4Address(0xC0A8880A);
57         EidToLocatorRecord record = new EidToLocatorRecord();
58         record.setPrefix(eid).setMaskLength(32);
59         record.addLocator(new LocatorRecord().setLocator(rloc));
60         mapRegister.addEidToLocator(record);
61         mapRegisterWithAuthentication = MapRegisterSerializer.getInstance().deserialize(hexToByteBuffer("38 00 01 01 FF BB " //
62                 + "00 00 00 00 00 00 00 01 00 14 2c 61 b9 c9 9a 20 " //
63                 + "ba d8 f5 40 d3 55 6f 5f 6e 5a b2 0a bf b5 00 00 " //
64                 + "00 0a 01 20 10 00 00 00 00 01 99 10 fe 01 01 64 " //
65                 + "ff 00 00 05 00 01 c0 a8 88 0a"));
66
67         mappingEntriesSaver = new ValueSaverAction<MappingEntry<?>[]>();
68     }
69
70     @Test
71     public void handleMapRegister__NonSetMBit() throws Exception {
72         mapRegister.setWantMapNotify(false);
73
74         addDefaultPutAndGetExpectations(eid, 32);
75         assertNull(testedMapServer.handleMapRegister(mapRegister));
76
77         MappingEntry<?>[] entries = mappingEntriesSaver.lastValue;
78         assertEquals(1, entries.length);
79
80         assertEquals("value", entries[0].getKey());
81         assertEquals(rloc, ((MappingServiceValue) entries[0].getValue()).getRlocs().get(0).getRecord().getLocator());
82     }
83
84     @Test
85     public void handleMapRegisterIpv4__ValidNotifyEchoesRegister() throws Exception {
86         mapRegister.addEidToLocator(new EidToLocatorRecord().setPrefix(new LispNoAddress()));
87         mapRegister.setWantMapNotify(true);
88
89         addDefaultPutAndGetExpectations(eid, 32);
90         addDefaultPutAndGetExpectations(new LispNoAddress(), 32);
91         MapNotify mapNotify = testedMapServer.handleMapRegister(mapRegister);
92         assertEquals(mapRegister.getEidToLocatorRecords(), mapNotify.getEidToLocatorRecords());
93         ArrayAssert.assertEquals(mapRegister.getAuthenticationData(), mapNotify.getAuthenticationData());
94         assertEquals(mapRegister.getKeyId(), mapNotify.getKeyId());
95         assertEquals(mapRegister.getNonce(), mapNotify.getNonce());
96     }
97
98     @Test
99     public void handleMapRegisterIpv4__CloneNotOwnYouClown() throws Exception {
100         mapRegister = new MapRegister();
101         mapRegister.setKeyId((byte) 0);
102         mapRegister.setWantMapNotify(true);
103         EidToLocatorRecord eidToLocator = new EidToLocatorRecord();
104         eidToLocator.setMaskLength(32);
105         eid = new LispIpv4Address(1);
106         eidToLocator.setPrefix(eid);
107
108         LocatorRecord locator = new LocatorRecord();
109         locator.setLocator(new LispIpv4Address(2));
110         locator.setPriority((byte) 55);
111         eidToLocator.addLocator(locator);
112
113         mapRegister.addEidToLocator(eidToLocator);
114
115         addDefaultPutAndGetExpectations(eid, 32);
116
117         MapNotify mapNotify = testedMapServer.handleMapRegister(mapRegister);
118
119         mapRegister.getEidToLocatorRecords().get(0).setPrefix(new LispIpv4Address(55));
120         mapRegister.getEidToLocatorRecords().get(0).getLocators().get(0).setPriority((byte) 1);
121
122         EidToLocatorRecord actualEidToLocator = mapNotify.getEidToLocatorRecords().get(0);
123         assertEquals(new LispIpv4Address(1), actualEidToLocator.getPrefix());
124         assertEquals((byte) 55, actualEidToLocator.getLocators().get(0).getPriority());
125
126     }
127
128     @Test
129     public void handleMapRegisterIpv4__ValidMask() throws Exception {
130         int mask = 16;
131         mapRegister = new MapRegister();
132         EidToLocatorRecord record = new EidToLocatorRecord();
133         record.setPrefix(eid).setMaskLength(mask);
134         record.addLocator(new LocatorRecord().setLocator(rloc));
135         mapRegister.addEidToLocator(record);
136         mapRegister.setWantMapNotify(true);
137
138         addDefaultPutAndGetExpectations(eid, mask);
139
140         MapNotify mapNotify = testedMapServer.handleMapRegister(mapRegister);
141         assertEquals(new LispIpv4Address("10.31.0.0"), mapNotify.getEidToLocatorRecords().get(0).getPrefix());
142         ArrayAssert.assertEquals(mapRegister.getAuthenticationData(), mapNotify.getAuthenticationData());
143         assertEquals(mapRegister.getEidToLocatorRecords(), mapNotify.getEidToLocatorRecords());
144         assertEquals(mapRegister.getKeyId(), mapNotify.getKeyId());
145         assertEquals(mapRegister.getNonce(), mapNotify.getNonce());
146     }
147
148     @Test
149     public void handleMapRegister__NonMaskable() throws Exception {
150         int mask = 16;
151         mapRegister = new MapRegister();
152         EidToLocatorRecord record = new EidToLocatorRecord();
153         LispMACAddress addr = new LispMACAddress(new byte[] { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 });
154         record.setPrefix(addr).setMaskLength(mask);
155         record.addLocator(new LocatorRecord().setLocator(rloc));
156         mapRegister.addEidToLocator(record);
157         mapRegister.setWantMapNotify(true);
158
159         addDefaultPutAndGetExpectations(addr, mask);
160
161         MapNotify mapNotify = testedMapServer.handleMapRegister(mapRegister);
162         assertEquals(addr, mapNotify.getEidToLocatorRecords().get(0).getPrefix());
163         ArrayAssert.assertEquals(mapRegister.getAuthenticationData(), mapNotify.getAuthenticationData());
164         assertEquals(mapRegister.getEidToLocatorRecords(), mapNotify.getEidToLocatorRecords());
165         assertEquals(mapRegister.getKeyId(), mapNotify.getKeyId());
166         assertEquals(mapRegister.getNonce(), mapNotify.getNonce());
167     }
168
169     private void addDefaultPutAndGetExpectations(LispAddress addr, int mask) {
170         addPutExpectations(addr, mask);
171         addGetExpectations(addr, mask);
172     }
173
174     @Test
175     public void handleMapRegister__ZeroMask() throws Exception {
176         int mask = 0;
177         mapRegister = new MapRegister();
178         EidToLocatorRecord record = new EidToLocatorRecord();
179         LispMACAddress addr = new LispMACAddress(new byte[] { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 });
180         record.setPrefix(addr).setMaskLength(mask);
181         record.addLocator(new LocatorRecord().setLocator(rloc));
182         mapRegister.addEidToLocator(record);
183         mapRegister.setWantMapNotify(true);
184
185         addDefaultPutAndGetExpectations(addr, mask);
186
187         MapNotify mapNotify = testedMapServer.handleMapRegister(mapRegister);
188         assertEquals(addr, mapNotify.getEidToLocatorRecords().get(0).getPrefix());
189         ArrayAssert.assertEquals(mapRegister.getAuthenticationData(), mapNotify.getAuthenticationData());
190         assertEquals(mapRegister.getEidToLocatorRecords(), mapNotify.getEidToLocatorRecords());
191         assertEquals(mapRegister.getKeyId(), mapNotify.getKeyId());
192         assertEquals(mapRegister.getNonce(), mapNotify.getNonce());
193     }
194
195     @Test
196     public void handleMapRegisterIPv4__ZeroMask() throws Exception {
197         int mask = 0;
198         mapRegister = new MapRegister();
199         EidToLocatorRecord record = new EidToLocatorRecord();
200         record.setPrefix(eid).setMaskLength(mask);
201         record.addLocator(new LocatorRecord().setLocator(rloc));
202         mapRegister.addEidToLocator(record);
203         mapRegister.setWantMapNotify(true);
204
205         addDefaultPutAndGetExpectations(eid, mask);
206
207         MapNotify mapNotify = testedMapServer.handleMapRegister(mapRegister);
208         assertEquals(new LispIpv4Address("0.0.0.0"), mapNotify.getEidToLocatorRecords().get(0).getPrefix());
209         ArrayAssert.assertEquals(mapRegister.getAuthenticationData(), mapNotify.getAuthenticationData());
210         assertEquals(mapRegister.getEidToLocatorRecords(), mapNotify.getEidToLocatorRecords());
211         assertEquals(mapRegister.getKeyId(), mapNotify.getKeyId());
212         assertEquals(mapRegister.getNonce(), mapNotify.getNonce());
213     }
214
215     @Test
216     public void handleMapRegisterIpv4__ValidMask32() throws Exception {
217         int mask = 32;
218         mapRegister = new MapRegister();
219         EidToLocatorRecord record = new EidToLocatorRecord();
220         record.setPrefix(new LispIpv4Address("10.31.0.5")).setMaskLength(mask);
221         record.addLocator(new LocatorRecord().setLocator(rloc));
222         mapRegister.addEidToLocator(record);
223         mapRegister.setWantMapNotify(true);
224
225         addDefaultPutAndGetExpectations(eid, mask);
226
227         MapNotify mapNotify = testedMapServer.handleMapRegister(mapRegister);
228         assertEquals(new LispIpv4Address("10.31.0.5"), mapNotify.getEidToLocatorRecords().get(0).getPrefix());
229         assertEquals(mapRegister.getEidToLocatorRecords(), mapNotify.getEidToLocatorRecords());
230         ArrayAssert.assertEquals(mapRegister.getAuthenticationData(), mapNotify.getAuthenticationData());
231         assertEquals(mapRegister.getKeyId(), mapNotify.getKeyId());
232         assertEquals(mapRegister.getNonce(), mapNotify.getNonce());
233     }
234
235     @Test
236     public void handleMapRegisterIpv6__ValidMask96() throws Exception {
237         int mask = 96;
238         mapRegister = new MapRegister();
239         EidToLocatorRecord record = new EidToLocatorRecord();
240         LispIpv6Address addr = new LispIpv6Address("1:1:1:1:1:1:1:0");
241         record.setPrefix(addr).setMaskLength(mask);
242         record.addLocator(new LocatorRecord().setLocator(rloc));
243         mapRegister.addEidToLocator(record);
244         mapRegister.setWantMapNotify(true);
245
246         addDefaultPutAndGetExpectations(addr, mask);
247
248         MapNotify mapNotify = testedMapServer.handleMapRegister(mapRegister);
249         assertEquals(new LispIpv6Address("1:1:1:1:1:1:0:0"), mapNotify.getEidToLocatorRecords().get(0).getPrefix());
250         assertEquals(mapRegister.getEidToLocatorRecords(), mapNotify.getEidToLocatorRecords());
251         ArrayAssert.assertEquals(mapRegister.getAuthenticationData(), mapNotify.getAuthenticationData());
252         assertEquals(mapRegister.getKeyId(), mapNotify.getKeyId());
253         assertEquals(mapRegister.getNonce(), mapNotify.getNonce());
254     }
255
256     @Test
257     public void handleMapRegisterIpv6__ZeroMask() throws Exception {
258         int mask = 0;
259         mapRegister = new MapRegister();
260         EidToLocatorRecord record = new EidToLocatorRecord();
261         LispIpv6Address addr = new LispIpv6Address("1:1:1:1:1:1:1:0");
262         record.setPrefix(addr).setMaskLength(mask);
263         record.addLocator(new LocatorRecord().setLocator(rloc));
264         mapRegister.addEidToLocator(record);
265         mapRegister.setWantMapNotify(true);
266
267         addDefaultPutAndGetExpectations(addr, mask);
268
269         MapNotify mapNotify = testedMapServer.handleMapRegister(mapRegister);
270         assertEquals(new LispIpv6Address("0:0:0:0:0:0:0:0"), mapNotify.getEidToLocatorRecords().get(0).getPrefix());
271         assertEquals(mapRegister.getEidToLocatorRecords(), mapNotify.getEidToLocatorRecords());
272         ArrayAssert.assertEquals(mapRegister.getAuthenticationData(), mapNotify.getAuthenticationData());
273         assertEquals(mapRegister.getKeyId(), mapNotify.getKeyId());
274         assertEquals(mapRegister.getNonce(), mapNotify.getNonce());
275     }
276
277     @Test
278     public void handleMapRegisterIPV4AndIpv6__ValidMask96() throws Exception {
279         int mask = 96;
280         EidToLocatorRecord record = new EidToLocatorRecord();
281         LispIpv6Address addr = new LispIpv6Address("1:1:1:1:1:1:1:0");
282         record.setPrefix(addr).setMaskLength(mask);
283         record.addLocator(new LocatorRecord().setLocator(rloc));
284         mapRegister.addEidToLocator(record);
285         mapRegister.setWantMapNotify(true);
286
287         addDefaultPutAndGetExpectations(eid, 32);
288         addDefaultPutAndGetExpectations(addr, mask);
289
290         MapNotify mapNotify = testedMapServer.handleMapRegister(mapRegister);
291         assertEquals(new LispIpv6Address("1:1:1:1:1:1:0:0"), mapNotify.getEidToLocatorRecords().get(1).getPrefix());
292         assertEquals(eid, mapNotify.getEidToLocatorRecords().get(0).getPrefix());
293         assertEquals(mapRegister.getEidToLocatorRecords(), mapNotify.getEidToLocatorRecords());
294         ArrayAssert.assertEquals(mapRegister.getAuthenticationData(), mapNotify.getAuthenticationData());
295         assertEquals(mapRegister.getKeyId(), mapNotify.getKeyId());
296         assertEquals(mapRegister.getNonce(), mapNotify.getNonce());
297     }
298
299     @Test
300     public void handleMapRegisterIpv6__ValidMask32() throws Exception {
301         int mask = 48;
302         mapRegister = new MapRegister();
303         EidToLocatorRecord record = new EidToLocatorRecord();
304         LispIpv6Address addr = new LispIpv6Address("1:1:1:1:0:0:0:0");
305         record.setPrefix(addr).setMaskLength(mask);
306         record.addLocator(new LocatorRecord().setLocator(rloc));
307         mapRegister.addEidToLocator(record);
308         mapRegister.setWantMapNotify(true);
309
310         addDefaultPutAndGetExpectations(addr, mask);
311
312         MapNotify mapNotify = testedMapServer.handleMapRegister(mapRegister);
313         assertEquals(new LispIpv6Address("1:1:1:0:0:0:0:0"), mapNotify.getEidToLocatorRecords().get(0).getPrefix());
314         assertEquals(mask, mapNotify.getEidToLocatorRecords().get(0).getMaskLength());
315         assertEquals(mapRegister.getEidToLocatorRecords(), mapNotify.getEidToLocatorRecords());
316         ArrayAssert.assertEquals(mapRegister.getAuthenticationData(), mapNotify.getAuthenticationData());
317         assertEquals(mapRegister.getKeyId(), mapNotify.getKeyId());
318         assertEquals(mapRegister.getNonce(), mapNotify.getNonce());
319     }
320
321     @Test
322     public void handleMapRegisterIpv6__ValidMask128() throws Exception {
323         int mask = 128;
324         mapRegister = new MapRegister();
325         EidToLocatorRecord record = new EidToLocatorRecord();
326         LispIpv6Address addr = new LispIpv6Address("1:1:1:1:1:1:1:2");
327         record.setPrefix(addr).setMaskLength(mask);
328         record.addLocator(new LocatorRecord().setLocator(rloc));
329         mapRegister.addEidToLocator(record);
330         mapRegister.setWantMapNotify(true);
331
332         addPutExpectations(addr, mask);
333
334         MapNotify mapNotify = testedMapServer.handleMapRegister(mapRegister);
335         assertEquals(new LispIpv6Address("1:1:1:1:1:1:1:2"), mapNotify.getEidToLocatorRecords().get(0).getPrefix());
336         assertEquals(mapRegister.getEidToLocatorRecords(), mapNotify.getEidToLocatorRecords());
337         ArrayAssert.assertEquals(mapRegister.getAuthenticationData(), mapNotify.getAuthenticationData());
338         assertEquals(mapRegister.getKeyId(), mapNotify.getKeyId());
339         assertEquals(mapRegister.getNonce(), mapNotify.getNonce());
340     }
341
342     @Test
343     public void handleMapRegister__MultipleRLOCs() throws Exception {
344         addDefaultPutAndGetExpectations(eid, 32);
345
346         LispIpv4Address rloc0 = rloc;
347         LispIpv6Address rloc1 = new LispIpv6Address("::7");
348         mapRegister.getEidToLocatorRecords().get(0).addLocator(new LocatorRecord().setLocator(rloc1));
349
350         testedMapServer.handleMapRegister(mapRegister);
351
352         MappingEntry<?>[] entries = mappingEntriesSaver.lastValue;
353         assertEquals(1, entries.length);
354
355         assertEquals("value", entries[0].getKey());
356         assertEquals(rloc0, ((MappingServiceValue) entries[0].getValue()).getRlocs().get(0).getRecord().getLocator());
357         assertEquals(rloc1, ((MappingServiceValue) entries[0].getValue()).getRlocs().get(1).getRecord().getLocator());
358
359     }
360
361     @Test
362     public void handleMapRegister__MultipleEIDs() throws Exception {
363         addDefaultPutAndGetExpectations(eid, 32);
364
365         LispIpv4Address rloc0 = rloc;
366         LispIpv6Address rloc1 = new LispIpv6Address("::7");
367         mapRegister.getEidToLocatorRecords().get(0).addLocator(new LocatorRecord().setLocator(rloc1));
368         mapRegister.setWantMapNotify(true);
369
370         EidToLocatorRecord etlr = new EidToLocatorRecord();
371         LispIpv4Address address = new LispIpv4Address("1.1.1.1");
372         etlr.setPrefix(address);
373         etlr.setMaskLength(32);
374         int recordTtl = 5;
375         etlr.setRecordTtl(recordTtl);
376         etlr.addLocator(new LocatorRecord().setLocator(new LispIpv4Address("2.2.2.2")).setPriority((byte) 10));
377         mapRegister.addEidToLocator(etlr);
378         addPutExpectations(address, 32);
379         addGetExpectations(address, 32);
380         MapNotify mapNotify = testedMapServer.handleMapRegister(mapRegister);
381
382         assertEquals(rloc0, mapNotify.getEidToLocatorRecords().get(0).getLocators().get(0).getLocator());
383         assertEquals(rloc1, mapNotify.getEidToLocatorRecords().get(0).getLocators().get(1).getLocator());
384         assertEquals(new LispIpv4Address("2.2.2.2"), mapNotify.getEidToLocatorRecords().get(1).getLocators().get(0).getLocator());
385         assertEquals(recordTtl, mapNotify.getEidToLocatorRecords().get(1).getRecordTtl());
386
387     }
388
389     @Test
390     public void handleMapRegisterIpv4__ChekWrongPassword() throws Exception {
391
392         addGetExpectations(new LispIpv4Address("153.16.254.1"), 32, 0, 31, "bla");
393         MapNotify mapNotify = testedMapServer.handleMapRegister(mapRegisterWithAuthentication);
394         assertEquals(null, mapNotify);
395     }
396
397     @Test
398     public void handleMapRegisterIpv4__ChcekNoPasswordAndThenPassword() throws Exception {
399
400         addGetExpectations(new LispIpv4Address("153.16.254.1"), 32, 30, 25, "password");
401         addPutExpectations(new LispIpv4Address("153.16.254.1"), 32);
402         MapNotify mapNotify = testedMapServer.handleMapRegister(mapRegisterWithAuthentication);
403         assertEquals(mapRegisterWithAuthentication.getEidToLocatorRecords(), mapNotify.getEidToLocatorRecords());
404         assertEquals(mapRegisterWithAuthentication.getKeyId(), mapNotify.getKeyId());
405         assertEquals(mapRegisterWithAuthentication.getNonce(), mapNotify.getNonce());
406     }
407
408     @Test
409     public void handleMapRegisterIpv4__ChcekNoPassword() throws Exception {
410
411         addGetExpectations(new LispIpv4Address("153.16.254.1"), 32, 30, 0, "password");
412         MapNotify mapNotify = testedMapServer.handleMapRegister(mapRegisterWithAuthentication);
413         assertEquals(null, mapNotify);
414     }
415
416     @Test
417     public void handleMapRegisterIpv4__ChcekNoreturn() throws Exception {
418
419         addGetExpectations(new LispIpv4Address("153.16.254.1"), 32);
420         MapNotify mapNotify = testedMapServer.handleMapRegister(mapRegisterWithAuthentication);
421         assertEquals(mapNotify, null);
422     }
423
424     @Test
425     public void handleAddAuthenticationKey() throws Exception {
426         IMappingServiceKey key = getDefualtKey();
427         MappingServiceValue value = getDefualtValue();
428         MappingEntry<MappingServiceValue> mappingEntry = new MappingEntry<MappingServiceValue>("value", value);
429         oneOf(lispDAO).get(weq(key));
430         oneOf(lispDAO).put(weq(key), weq(convertToArray(mappingEntry)));
431         assertEquals(true, testedMapServer.addAuthenticationKey(eid, key.getMask(), value.getKey()));
432     }
433
434     @Test
435     public void handleGetAuthenticationKey() throws Exception {
436         IMappingServiceKey key = getDefualtKey();
437         MappingServiceValue value = getDefualtValue();
438         Map<String, MappingServiceValue> map = new HashMap<String, MappingServiceValue>();
439         map.put("value", value);
440         atLeast(1).of(lispDAO).get(weq(key));
441         ret(map);
442         assertEquals(value.getKey(), testedMapServer.getAuthenticationKey(eid, key.getMask()));
443     }
444
445     @Test
446     public void handleGetAuthenticationKeyNoIteration() throws Exception {
447         testedMapServer.setShouldIterateMask(false);
448         IMappingServiceKey key = getDefualtKey();
449         IMappingServiceKey passKey = getKey(30);
450         MappingServiceValue value = getDefualtValue();
451         Map<String, MappingServiceValue> map = new HashMap<String, MappingServiceValue>();
452         map.put("value", value);
453         oneOf(lispDAO).get(weq(key));
454         allowing(lispDAO).get(weq(passKey));
455         ret(map);
456         assertEquals(null, testedMapServer.getAuthenticationKey(eid, key.getMask()));
457     }
458
459     @Test
460     public void handleRemoveAuthenticationKey() throws Exception {
461         IMappingServiceKey key = getDefualtKey();
462         MappingServiceValue value = new MappingServiceValue();
463         Map<String, MappingServiceValue> map = new HashMap<String, MappingServiceValue>();
464         map.put("value", value);
465         oneOf(lispDAO).get(weq(key));
466         ret(map);
467         oneOf(lispDAO).remove(weq(key));
468         assertEquals(true, testedMapServer.removeAuthenticationKey(eid, key.getMask()));
469     }
470
471     @Test
472     public void handleRemoveAuthenticationKeyWhereKeyDosntExist() throws Exception {
473         IMappingServiceKey key = getDefualtKey();
474         MappingServiceValue value = new MappingServiceValue();
475         Map<String, MappingServiceValue> map = new HashMap<String, MappingServiceValue>();
476         map.put("value", value);
477         oneOf(lispDAO).get(weq(key));
478         assertEquals(false, testedMapServer.removeAuthenticationKey(eid, key.getMask()));
479     }
480
481     @SuppressWarnings("rawtypes")
482     private MappingEntry[] convertToArray(MappingEntry<MappingServiceValue> entry) {
483         MappingEntry[] arr = new MappingEntry[1];
484         arr[0] = entry;
485         return arr;
486     }
487
488     private void addGetExpectations(LispAddress address, int mask) {
489         addGetExpectations(address, mask, 0, 0, "password");
490     }
491
492     private void addPutExpectations(LispAddress address, int mask) {
493         LispAddress clonedAddress = address;
494         if (address instanceof IMaskable) {
495             clonedAddress = (LispAddress) ((IMaskable) address).clone();
496         }
497         oneOf(lispDAO).put(weq(MappingServiceKeyUtil.generateMappingServiceKey(clonedAddress, (byte) mask)), with(mappingEntriesSaver));
498     }
499
500     private void addGetExpectations(LispAddress address, int mask, int withoutPassword, int withPassword, String password) {
501         LispAddress clonedAddress = address;
502         if (address instanceof IMaskable) {
503             clonedAddress = (LispAddress) ((IMaskable) address).clone();
504         }
505
506         if (withoutPassword > 0) {
507             Map<String, MappingServiceValue> result = new HashMap<String, MappingServiceValue>();
508             result.put("value", new MappingServiceValue());
509             allowing(lispDAO)
510                     .getSpecific(with(MappingServiceKeyUtil.generateMappingServiceKey(clonedAddress, (byte) withoutPassword)), with("value"));
511             ret(result);
512         }
513         if (address instanceof IMaskable) {
514             clonedAddress = (LispAddress) ((IMaskable) address).clone();
515         }
516         if (withPassword > 0) {
517             Map<String, MappingServiceValue> result = new HashMap<String, MappingServiceValue>();
518             result.put("value", new MappingServiceValue());
519             result.get("value").setKey(password);
520             allowing(lispDAO).get(with(MappingServiceKeyUtil.generateMappingServiceKey(clonedAddress, (byte) withPassword)));
521             ret(result);
522         }
523         for (int i = mask; i >= 0; i--) {
524             if (address instanceof IMaskable) {
525                 clonedAddress = (LispAddress) ((IMaskable) address).clone();
526             }
527             allowing(lispDAO).get(with(MappingServiceKeyUtil.generateMappingServiceKey(clonedAddress, (byte) i)));
528             ret(new HashMap<String, MappingServiceValue>());
529         }
530     }
531
532     private IMappingServiceKey getDefualtKey() {
533         return getKey(32);
534     }
535
536     private IMappingServiceKey getKey(int mask) {
537         IMappingServiceKey key = MappingServiceKeyUtil.generateMappingServiceKey(eid, mask);
538         return key;
539     }
540
541     private MappingServiceValue getDefualtValue() {
542         MappingServiceValue value = new MappingServiceValue();
543         String password = "pass";
544         value.setKey(password);
545         return value;
546     }
547 }