ignore transiant files in git
[lispflowmapping.git] / mappingservice / api / src / test / java / org / opendaylight / lispflowmapping / type / lisp / MapReplyTest.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.type.lisp;
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.type.lisp.address.LispIpv4Address;
20 import org.opendaylight.lispflowmapping.type.lisp.address.LispIpv6Address;
21 import org.opendaylight.lispflowmapping.tools.junit.BaseTestCase;
22
23 public class MapReplyTest extends BaseTestCase {
24
25     @Test
26     @Ignore
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");
31     }
32
33     @Test
34     public void serialize__SomeFlags() throws Exception {
35         MapReply mr = new MapReply();
36         mr.addEidToLocator(new EidToLocatorRecord().setPrefix(new LispIpv4Address(1)));
37         mr.setProbe(true);
38         mr.setEchoNonceEnabled(false);
39
40         ByteBuffer packet = mr.serialize();
41         byte firstByte = packet.get(0);
42         assertHexEquals((byte) 0x28, firstByte);
43
44         mr.setProbe(false);
45         mr.setEchoNonceEnabled(true);
46
47         packet = mr.serialize();
48         firstByte = packet.get(0);
49         assertHexEquals((byte) 0x24, firstByte);
50     }
51
52     @Test
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)));
57
58         ByteBuffer packet = mr.serialize();
59         assertEquals(2, packet.get(3));
60
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];
64         packet.get(actual);
65         ArrayAssert.assertEquals(expected, actual);
66
67         packet.position(packet.position() + 12); /* EID in second record */
68         assertEquals(0x08020405, packet.getInt());
69     }
70
71     @Test
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);
77
78         ByteBuffer packet = mr.serialize();
79
80         packet.position(18);
81         assertHexEquals((byte) 0x00, packet.get()); // MapReplyAction.NoAction
82     }
83
84     @Test
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);
91
92         ByteBuffer packet = mr.serialize();
93
94         packet.position(18);
95         assertHexEquals((byte) 0x00, packet.get()); // MapReplyAction.NoAction
96     }
97
98     @Test
99     public void serialize__EidRecordFields() throws Exception {
100         MapReply mr = new MapReply();
101
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);
109
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);
117
118         ByteBuffer packet = mr.serialize();
119
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());
126
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());
133     }
134
135     @Test
136     public void serialize__LocatorRecordFields() throws Exception {
137         MapReply mr = new MapReply();
138
139         EidToLocatorRecord eidToLocator = new EidToLocatorRecord();
140         eidToLocator.setPrefix(new LispIpv4Address(1));
141
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);
152
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);
163
164         mr.addEidToLocator(eidToLocator);
165
166         ByteBuffer packet = mr.serialize();
167
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());
175
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());
183     }
184 }