ab7ffa2b1d7a6241bcb971d7576cca97b9f2d8f5
[lispflowmapping.git] / mappingservice / southbound / src / test / java / org / opendaylight / lispflowmapping / southbound / serializer / MapReplySerializationTest.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 availabl at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.lispflowmapping.southbound.serializer;
10
11 import static org.junit.Assert.assertEquals;
12
13 import java.nio.ByteBuffer;
14
15 import junitx.framework.ArrayAssert;
16
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;
26
27 public class MapReplySerializationTest extends BaseTestCase {
28
29     @Test
30     @Ignore
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");
35     }
36
37     @Test
38     public void serialize__SomeFlags() throws Exception {
39         MapReply mr = new MapReply();
40         mr.addEidToLocator(new EidToLocatorRecord().setPrefix(new LispIpv4Address(1)));
41         mr.setProbe(true);
42         mr.setEchoNonceEnabled(false);
43
44         ByteBuffer packet = MapReplySerializer.getInstance().serialize(mr);
45         byte firstByte = packet.get(0);
46         assertHexEquals((byte) 0x28, firstByte);
47
48         mr.setProbe(false);
49         mr.setEchoNonceEnabled(true);
50
51         packet = MapReplySerializer.getInstance().serialize(mr);
52         firstByte = packet.get(0);
53         assertHexEquals((byte) 0x24, firstByte);
54     }
55
56     @Test
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)));
61
62         ByteBuffer packet = MapReplySerializer.getInstance().serialize(mr);
63         assertEquals(2, packet.get(3));
64
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];
68         packet.get(actual);
69         ArrayAssert.assertEquals(expected, actual);
70
71         packet.position(packet.position() + 12); /* EID in second record */
72         assertEquals(0x08020405, packet.getInt());
73     }
74
75     @Test
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);
81
82         ByteBuffer packet = MapReplySerializer.getInstance().serialize(mr);
83
84         packet.position(18);
85         assertHexEquals((byte) 0x00, packet.get()); // MapReplyAction.NoAction
86     }
87
88     @Test
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);
95
96         ByteBuffer packet = MapReplySerializer.getInstance().serialize(mr);
97
98         packet.position(18);
99         assertHexEquals((byte) 0x00, packet.get()); // MapReplyAction.NoAction
100     }
101
102     @Test
103     public void serialize__EidRecordFields() throws Exception {
104         MapReply mr = new MapReply();
105
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);
113
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);
121
122         ByteBuffer packet = MapReplySerializer.getInstance().serialize(mr);
123
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());
130
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());
137     }
138
139     @Test
140     public void serialize__LocatorRecordFields() throws Exception {
141         MapReply mr = new MapReply();
142
143         EidToLocatorRecord eidToLocator = new EidToLocatorRecord();
144         eidToLocator.setPrefix(new LispIpv4Address(1));
145
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);
156
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);
167
168         mr.addEidToLocator(eidToLocator);
169
170         ByteBuffer packet = MapReplySerializer.getInstance().serialize(mr);
171
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());
179
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());
187     }
188 }