blob: 6f8c518e7d909c3b12a2a0620ccb1c1890fb20c9 [file] [log] [blame]
Yi Tsengca34e1d2017-07-18 16:16:25 -07001/*
2 * Copyright 2017-present Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17
18package org.onlab.packet.dhcp;
19
20import com.google.common.collect.ImmutableList;
21import com.google.common.collect.Lists;
Yuta HIGUCHI7bfd6072017-08-01 16:23:50 -070022import com.google.common.io.Resources;
23
Yi Tsengca34e1d2017-07-18 16:16:25 -070024import org.junit.Test;
25import org.onlab.packet.DHCP6;
26import org.onlab.packet.Ip6Address;
27import org.onlab.packet.MacAddress;
28
29import java.util.Collections;
30import java.util.List;
31
32import static org.junit.Assert.*;
33
34/**
35 * Test serializing/deserializing DHCPv6 relay message.
36 */
37public class Dhcp6RelayTest {
38 private static final String SOLICIT = "dhcp6_relay_solicit.bin";
39 private static final String ADVERTISE = "dhcp6_relay_advertise.bin";
40 private static final String REQUEST = "dhcp6_relay_request.bin";
41 private static final String REPLY = "dhcp6_relay_reply.bin";
42
43 private static final int HOP_COUNT = 0;
44 private static final Ip6Address LINK_ADDRESS = Ip6Address.valueOf("2000::2ff");
45 private static final Ip6Address PEER_ADDRESS = Ip6Address.valueOf("fe80::1");
46 private static final int XID_1 = 13346301;
47 private static final int XID_2 = 9807588;
48 private static final int IA_ID = 1;
49 private static final int T1_CLIENT = 3600;
50 private static final int T2_CLIENT = 5400;
51 private static final int T1_SERVER = 0;
52 private static final int T2_SERVER = 0;
53 private static final Ip6Address IA_ADDRESS = Ip6Address.valueOf("2000::201");
54 private static final int PREFFERRED_LT_SERVER = 375;
55 private static final int VALID_LT_SERVER = 600;
56 private static final int PREFFERRED_LT_REQ = 7200;
57 private static final int VALID_LT_REQ = 7500;
58 private static final MacAddress CLIENT_MAC = MacAddress.valueOf("00:bb:00:00:00:01");
59 private static final int CLIENT_DUID_TIME = 0x210016b4;
60
61 /**
62 * Test deserialize relay message with solicit message.
63 *
64 * @throws Exception exception while deserialize the DHCPv6 payload
65 */
66 @Test
67 public void deserializeSolicit() throws Exception {
Yuta HIGUCHI7bfd6072017-08-01 16:23:50 -070068 byte[] data = Resources.toByteArray(Dhcp6RelayTest.class.getResource(SOLICIT));
Yi Tsengca34e1d2017-07-18 16:16:25 -070069 DHCP6 relayMsg = DHCP6.deserializer().deserialize(data, 0, data.length);
70 assertEquals(relayMsg.getMsgType(), DHCP6.MsgType.RELAY_FORW.value());
71 assertEquals(relayMsg.getHopCount(), HOP_COUNT);
72 assertEquals(relayMsg.getIp6LinkAddress(), LINK_ADDRESS);
73 assertEquals(relayMsg.getIp6PeerAddress(), PEER_ADDRESS);
74
75 assertEquals(relayMsg.getOptions().size(), 1);
76 Dhcp6Option option = relayMsg.getOptions().get(0);
77 assertEquals(option.getCode(), DHCP6.OptionCode.RELAY_MSG.value());
78 assertEquals(option.getLength(), 56);
79 assertTrue(option.getPayload() instanceof DHCP6);
80
81 DHCP6 relaiedDhcp6 = (DHCP6) option.getPayload();
82 assertEquals(relaiedDhcp6.getMsgType(), DHCP6.MsgType.SOLICIT.value());
83 assertEquals(relaiedDhcp6.getTransactionId(), XID_1);
84 assertEquals(relaiedDhcp6.getOptions().size(), 4);
85
86 // Client ID
87 option = relaiedDhcp6.getOptions().get(0);
88 assertTrue(option instanceof Dhcp6ClientIdOption);
89 Dhcp6ClientIdOption clientIdOption = (Dhcp6ClientIdOption) option;
90 assertEquals(clientIdOption.getCode(), DHCP6.OptionCode.CLIENTID.value());
91 assertEquals(clientIdOption.getLength(), 14);
92 assertEquals(clientIdOption.getDuid().getDuidType(), Dhcp6Duid.DuidType.DUID_LLT);
93 assertEquals(clientIdOption.getDuid().getHardwareType(), 1);
94 assertEquals(clientIdOption.getDuid().getDuidTime(), CLIENT_DUID_TIME);
95 assertArrayEquals(clientIdOption.getDuid().getLinkLayerAddress(), CLIENT_MAC.toBytes());
96
97 // ORO
98 option = relaiedDhcp6.getOptions().get(1);
99 assertEquals(option.getCode(), DHCP6.OptionCode.ORO.value());
100 assertEquals(option.getLength(), 8);
101 assertArrayEquals(option.getData(),
102 new byte[]{0, 23, 0, 24, 0, 39, 0, 31});
103
104 // ELAPSED_TIME
105 option = relaiedDhcp6.getOptions().get(2);
106 assertEquals(option.getCode(), DHCP6.OptionCode.ELAPSED_TIME.value());
107 assertEquals(option.getLength(), 2);
108 assertArrayEquals(option.getData(),
109 new byte[]{0, 0});
110
111 // IA NA
112 option = relaiedDhcp6.getOptions().get(3);
113 assertTrue(option instanceof Dhcp6IaNaOption);
114 Dhcp6IaNaOption iaNaOption = (Dhcp6IaNaOption) option;
115 assertEquals(iaNaOption.getCode(), DHCP6.OptionCode.IA_NA.value());
116 assertEquals(iaNaOption.getLength(), 12);
117 assertEquals(iaNaOption.getIaId(), IA_ID);
118 assertEquals(iaNaOption.getT1(), T1_CLIENT);
119 assertEquals(iaNaOption.getT2(), T2_CLIENT);
120 assertEquals(iaNaOption.getOptions().size(), 0);
121
122 assertArrayEquals(data, relayMsg.serialize());
123 }
124
125 /**
126 * Test serialize relay message with solicit message.
127 *
128 * @throws Exception exception while serialize the DHCPv6 payload
129 */
130 @Test
131 public void serializeSolicit() throws Exception {
132 DHCP6 relayMsg = new DHCP6();
133 relayMsg.setMsgType(DHCP6.MsgType.RELAY_FORW.value());
134 relayMsg.setHopCount((byte) HOP_COUNT);
135 relayMsg.setLinkAddress(LINK_ADDRESS.toOctets());
136 relayMsg.setPeerAddress(PEER_ADDRESS.toOctets());
137
138 DHCP6 relaiedDhcp6 = new DHCP6();
139 relaiedDhcp6.setMsgType(DHCP6.MsgType.SOLICIT.value());
140 relaiedDhcp6.setTransactionId(XID_1);
141 List<Dhcp6Option> options = Lists.newArrayList();
142
143 // Client ID
144 Dhcp6Duid duid = new Dhcp6Duid();
145 duid.setDuidType(Dhcp6Duid.DuidType.DUID_LLT);
146 duid.setHardwareType((short) 1);
147 duid.setDuidTime(CLIENT_DUID_TIME);
148 duid.setLinkLayerAddress(CLIENT_MAC.toBytes());
149 Dhcp6ClientIdOption clientIdOption = new Dhcp6ClientIdOption();
150 clientIdOption.setDuid(duid);
151 options.add(clientIdOption);
152
153 // Option request
154 Dhcp6Option option = new Dhcp6Option();
155 option.setCode(DHCP6.OptionCode.ORO.value());
156 option.setLength((short) 8);
157 option.setData(new byte[]{0, 23, 0, 24, 0, 39, 0, 31});
158 options.add(option);
159
160 // Elapsed Time
161 option = new Dhcp6Option();
162 option.setCode(DHCP6.OptionCode.ELAPSED_TIME.value());
163 option.setLength((short) 2);
164 option.setData(new byte[]{0, 0});
165 options.add(option);
166
167 // IA NA
168 Dhcp6IaNaOption iaNaOption = new Dhcp6IaNaOption();
169 iaNaOption.setIaId(IA_ID);
170 iaNaOption.setT1(T1_CLIENT);
171 iaNaOption.setT2(T2_CLIENT);
172 iaNaOption.setOptions(Collections.emptyList());
173 options.add(iaNaOption);
174 relaiedDhcp6.setOptions(options);
175
176
177 Dhcp6RelayOption relayOption = new Dhcp6RelayOption();
178 relayOption.setPayload(relaiedDhcp6);
179
180 relayMsg.setOptions(ImmutableList.of(relayOption));
Yuta HIGUCHI7bfd6072017-08-01 16:23:50 -0700181 assertArrayEquals(Resources.toByteArray(Dhcp6RelayTest.class.getResource(SOLICIT)),
Yi Tsengca34e1d2017-07-18 16:16:25 -0700182 relayMsg.serialize());
183 }
184
185 /**
186 * Test deserialize relay message with advertise message.
187 *
188 * @throws Exception exception while deserialize the DHCPv6 payload
189 */
190 @Test
191 public void deserializeAdvertise() throws Exception {
Yuta HIGUCHI7bfd6072017-08-01 16:23:50 -0700192 byte[] data = Resources.toByteArray(getClass().getResource(ADVERTISE));
Yi Tsengca34e1d2017-07-18 16:16:25 -0700193 DHCP6 relayMsg = DHCP6.deserializer().deserialize(data, 0, data.length);
194
195 assertEquals(relayMsg.getMsgType(), DHCP6.MsgType.RELAY_REPL.value());
196 assertEquals(relayMsg.getHopCount(), HOP_COUNT);
197 assertEquals(relayMsg.getIp6LinkAddress(), LINK_ADDRESS);
198 assertEquals(relayMsg.getIp6PeerAddress(), PEER_ADDRESS);
199
200 assertEquals(relayMsg.getOptions().size(), 1);
201 Dhcp6Option option = relayMsg.getOptions().get(0);
202 assertEquals(option.getCode(), DHCP6.OptionCode.RELAY_MSG.value());
203 assertEquals(option.getLength(), 84);
204 assertTrue(option.getPayload() instanceof DHCP6);
205
206 DHCP6 relaiedDhcp6 = (DHCP6) option.getPayload();
207 assertEquals(relaiedDhcp6.getMsgType(), DHCP6.MsgType.ADVERTISE.value());
208 assertEquals(relaiedDhcp6.getTransactionId(), XID_1);
209 assertEquals(relaiedDhcp6.getOptions().size(), 3);
210
211 // IA NA
212 option = relaiedDhcp6.getOptions().get(0);
213 assertTrue(option instanceof Dhcp6IaNaOption);
214 Dhcp6IaNaOption iaNaOption = (Dhcp6IaNaOption) option;
215 assertEquals(iaNaOption.getCode(), DHCP6.OptionCode.IA_NA.value());
216 assertEquals(iaNaOption.getLength(), 40);
217 assertEquals(iaNaOption.getIaId(), IA_ID);
218 assertEquals(iaNaOption.getT1(), T1_SERVER);
219 assertEquals(iaNaOption.getT2(), T2_SERVER);
220 assertEquals(iaNaOption.getOptions().size(), 1);
221
222 // IA Address (in IA NA)
223 assertTrue(iaNaOption.getOptions().get(0) instanceof Dhcp6IaAddressOption);
224 Dhcp6IaAddressOption iaAddressOption =
225 (Dhcp6IaAddressOption) iaNaOption.getOptions().get(0);
226 assertEquals(iaAddressOption.getIp6Address(), IA_ADDRESS);
227 assertEquals(iaAddressOption.getPreferredLifetime(), PREFFERRED_LT_SERVER);
228 assertEquals(iaAddressOption.getValidLifetime(), VALID_LT_SERVER);
229 assertNull(iaAddressOption.getOptions());
230
231 // Client ID
232 option = relaiedDhcp6.getOptions().get(1);
233 assertTrue(option instanceof Dhcp6ClientIdOption);
234 Dhcp6ClientIdOption clientIdOption = (Dhcp6ClientIdOption) option;
235 assertEquals(clientIdOption.getCode(), DHCP6.OptionCode.CLIENTID.value());
236 assertEquals(clientIdOption.getLength(), 14);
237 assertEquals(clientIdOption.getDuid().getDuidType(), Dhcp6Duid.DuidType.DUID_LLT);
238 assertEquals(clientIdOption.getDuid().getHardwareType(), 1);
239 assertEquals(clientIdOption.getDuid().getDuidTime(), CLIENT_DUID_TIME);
240 assertArrayEquals(clientIdOption.getDuid().getLinkLayerAddress(), CLIENT_MAC.toBytes());
241
242 // Server ID
243 option = relaiedDhcp6.getOptions().get(2);
244 assertEquals(option.getCode(), DHCP6.OptionCode.SERVERID.value());
245 assertEquals(option.getLength(), 14);
246 assertArrayEquals(option.getData(),
247 new byte[]{0, 1, 0, 1, 32, -1, -8, -17, 0, -103, 102, 0, 0, 1});
248
249 assertArrayEquals(data, relayMsg.serialize());
250 }
251
252 /**
253 * Test serialize relay message with advertise message.
254 *
255 * @throws Exception exception while serialize the DHCPv6 payload
256 */
257 @Test
258 public void serializeAdvertise() throws Exception {
259 DHCP6 relayMsg = new DHCP6();
260 relayMsg.setMsgType(DHCP6.MsgType.RELAY_REPL.value());
261 relayMsg.setHopCount((byte) HOP_COUNT);
262 relayMsg.setLinkAddress(LINK_ADDRESS.toOctets());
263 relayMsg.setPeerAddress(PEER_ADDRESS.toOctets());
264
265 DHCP6 relaiedDhcp6 = new DHCP6();
266 relaiedDhcp6.setMsgType(DHCP6.MsgType.ADVERTISE.value());
267 relaiedDhcp6.setTransactionId(XID_1);
268 List<Dhcp6Option> options = Lists.newArrayList();
269
270 // IA address
271 Dhcp6IaAddressOption iaAddressOption = new Dhcp6IaAddressOption();
272 iaAddressOption.setIp6Address(IA_ADDRESS);
273 iaAddressOption.setPreferredLifetime(PREFFERRED_LT_SERVER);
274 iaAddressOption.setValidLifetime(VALID_LT_SERVER);
275
276 // IA NA
277 Dhcp6IaNaOption iaNaOption = new Dhcp6IaNaOption();
278 iaNaOption.setIaId(IA_ID);
279 iaNaOption.setT1(T1_SERVER);
280 iaNaOption.setT2(T2_SERVER);
281 iaNaOption.setOptions(ImmutableList.of(iaAddressOption));
282 options.add(iaNaOption);
283
284 // Client ID
285 Dhcp6Duid duid = new Dhcp6Duid();
286 duid.setDuidType(Dhcp6Duid.DuidType.DUID_LLT);
287 duid.setHardwareType((short) 1);
288 duid.setDuidTime(CLIENT_DUID_TIME);
289 duid.setLinkLayerAddress(CLIENT_MAC.toBytes());
290 Dhcp6ClientIdOption clientIdOption = new Dhcp6ClientIdOption();
291 clientIdOption.setDuid(duid);
292 options.add(clientIdOption);
293
294 // Server ID
295 Dhcp6Option option = new Dhcp6Option();
296 option.setCode(DHCP6.OptionCode.SERVERID.value());
297 option.setLength((short) 14);
298 option.setData(new byte[]{0, 1, 0, 1, 32, -1, -8, -17, 0, -103, 102, 0, 0, 1});
299 options.add(option);
300
301 relaiedDhcp6.setOptions(options);
302
303 Dhcp6RelayOption relayOption = new Dhcp6RelayOption();
304 relayOption.setPayload(relaiedDhcp6);
305
306 relayMsg.setOptions(ImmutableList.of(relayOption));
307
Yuta HIGUCHI7bfd6072017-08-01 16:23:50 -0700308 assertArrayEquals(Resources.toByteArray(Dhcp6RelayTest.class.getResource(ADVERTISE)),
Yi Tsengca34e1d2017-07-18 16:16:25 -0700309 relayMsg.serialize());
310 }
311
312 /**
313 * Test deserialize relay message with request message.
314 *
315 * @throws Exception exception while deserialize the DHCPv6 payload
316 */
317 @Test
318 public void deserializeRequest() throws Exception {
Yuta HIGUCHI7bfd6072017-08-01 16:23:50 -0700319 byte[] data = Resources.toByteArray(getClass().getResource(REQUEST));
Yi Tsengca34e1d2017-07-18 16:16:25 -0700320 DHCP6 relayMsg = DHCP6.deserializer().deserialize(data, 0, data.length);
321
322 assertEquals(relayMsg.getMsgType(), DHCP6.MsgType.RELAY_FORW.value());
323 assertEquals(relayMsg.getHopCount(), HOP_COUNT);
324 assertEquals(relayMsg.getIp6LinkAddress(), LINK_ADDRESS);
325 assertEquals(relayMsg.getIp6PeerAddress(), PEER_ADDRESS);
326
327 assertEquals(relayMsg.getOptions().size(), 1);
328 Dhcp6Option option = relayMsg.getOptions().get(0);
329 assertEquals(option.getCode(), DHCP6.OptionCode.RELAY_MSG.value());
330 assertEquals(option.getLength(), 102);
331 assertTrue(option.getPayload() instanceof DHCP6);
332
333 DHCP6 relaiedDhcp6 = (DHCP6) option.getPayload();
334 assertEquals(relaiedDhcp6.getMsgType(), DHCP6.MsgType.REQUEST.value());
335 assertEquals(relaiedDhcp6.getTransactionId(), XID_2);
336 assertEquals(relaiedDhcp6.getOptions().size(), 5);
337
338 // Client ID
339 option = relaiedDhcp6.getOptions().get(0);
340 assertTrue(option instanceof Dhcp6ClientIdOption);
341 Dhcp6ClientIdOption clientIdOption = (Dhcp6ClientIdOption) option;
342 assertEquals(clientIdOption.getCode(), DHCP6.OptionCode.CLIENTID.value());
343 assertEquals(clientIdOption.getLength(), 14);
344 assertEquals(clientIdOption.getDuid().getDuidType(), Dhcp6Duid.DuidType.DUID_LLT);
345 assertEquals(clientIdOption.getDuid().getHardwareType(), 1);
346 assertEquals(clientIdOption.getDuid().getDuidTime(), CLIENT_DUID_TIME);
347 assertArrayEquals(clientIdOption.getDuid().getLinkLayerAddress(), CLIENT_MAC.toBytes());
348
349 // Server ID
350 option = relaiedDhcp6.getOptions().get(1);
351 assertEquals(option.getCode(), DHCP6.OptionCode.SERVERID.value());
352 assertEquals(option.getLength(), 14);
353 assertArrayEquals(option.getData(),
354 new byte[]{0, 1, 0, 1, 32, -1, -8, -17, 0, -103, 102, 0, 0, 1});
355
356 // Option Request
357 option = relaiedDhcp6.getOptions().get(2);
358 assertEquals(option.getCode(), DHCP6.OptionCode.ORO.value());
359 assertEquals(option.getLength(), 8);
360 assertArrayEquals(option.getData(), new byte[]{0, 23, 0, 24, 0, 39, 0, 31});
361
362 // ELAPSED_TIME
363 option = relaiedDhcp6.getOptions().get(3);
364 assertEquals(option.getCode(), DHCP6.OptionCode.ELAPSED_TIME.value());
365 assertEquals(option.getLength(), 2);
366 assertArrayEquals(option.getData(),
367 new byte[]{0, 0});
368
369 // IA NA
370 option = relaiedDhcp6.getOptions().get(4);
371 assertTrue(option instanceof Dhcp6IaNaOption);
372 Dhcp6IaNaOption iaNaOption = (Dhcp6IaNaOption) option;
373 assertEquals(iaNaOption.getCode(), DHCP6.OptionCode.IA_NA.value());
374 assertEquals(iaNaOption.getLength(), 40);
375 assertEquals(iaNaOption.getIaId(), IA_ID);
376 assertEquals(iaNaOption.getT1(), T1_CLIENT);
377 assertEquals(iaNaOption.getT2(), T2_CLIENT);
378 assertEquals(iaNaOption.getOptions().size(), 1);
379
380 // IA Address (in IA NA)
381 assertTrue(iaNaOption.getOptions().get(0) instanceof Dhcp6IaAddressOption);
382 Dhcp6IaAddressOption iaAddressOption =
383 (Dhcp6IaAddressOption) iaNaOption.getOptions().get(0);
384 assertEquals(iaAddressOption.getIp6Address(), IA_ADDRESS);
385 assertEquals(iaAddressOption.getPreferredLifetime(), PREFFERRED_LT_REQ);
386 assertEquals(iaAddressOption.getValidLifetime(), VALID_LT_REQ);
387 assertNull(iaAddressOption.getOptions());
388
389 assertArrayEquals(data, relayMsg.serialize());
390 }
391
392 /**
393 * Test serialize relay message with request message.
394 *
395 * @throws Exception exception while serialize the DHCPv6 payload
396 */
397 @Test
398 public void serializeRequest() throws Exception {
399 DHCP6 relayMsg = new DHCP6();
400 relayMsg.setMsgType(DHCP6.MsgType.RELAY_FORW.value());
401 relayMsg.setHopCount((byte) HOP_COUNT);
402 relayMsg.setLinkAddress(LINK_ADDRESS.toOctets());
403 relayMsg.setPeerAddress(PEER_ADDRESS.toOctets());
404
405 DHCP6 relaiedDhcp6 = new DHCP6();
406 relaiedDhcp6.setMsgType(DHCP6.MsgType.REQUEST.value());
407 relaiedDhcp6.setTransactionId(XID_2);
408 List<Dhcp6Option> options = Lists.newArrayList();
409
410 // Client ID
411 Dhcp6Duid duid = new Dhcp6Duid();
412 duid.setDuidType(Dhcp6Duid.DuidType.DUID_LLT);
413 duid.setHardwareType((short) 1);
414 duid.setDuidTime(CLIENT_DUID_TIME);
415 duid.setLinkLayerAddress(CLIENT_MAC.toBytes());
416 Dhcp6ClientIdOption clientIdOption = new Dhcp6ClientIdOption();
417 clientIdOption.setDuid(duid);
418 options.add(clientIdOption);
419
420 // Server ID
421 Dhcp6Option option = new Dhcp6Option();
422 option.setCode(DHCP6.OptionCode.SERVERID.value());
423 option.setLength((short) 14);
424 option.setData(new byte[]{0, 1, 0, 1, 32, -1, -8, -17, 0, -103, 102, 0, 0, 1});
425 options.add(option);
426
427 // Option request
428 option = new Dhcp6Option();
429 option.setCode(DHCP6.OptionCode.ORO.value());
430 option.setLength((short) 8);
431 option.setData(new byte[]{0, 23, 0, 24, 0, 39, 0, 31});
432 options.add(option);
433
434 // Elapsed Time
435 option = new Dhcp6Option();
436 option.setCode(DHCP6.OptionCode.ELAPSED_TIME.value());
437 option.setLength((short) 2);
438 option.setData(new byte[]{0, 0});
439 options.add(option);
440
441 // IA address
442 Dhcp6IaAddressOption iaAddressOption = new Dhcp6IaAddressOption();
443 iaAddressOption.setIp6Address(IA_ADDRESS);
444 iaAddressOption.setPreferredLifetime(PREFFERRED_LT_REQ);
445 iaAddressOption.setValidLifetime(VALID_LT_REQ);
446
447 // IA NA
448 Dhcp6IaNaOption iaNaOption = new Dhcp6IaNaOption();
449 iaNaOption.setIaId(IA_ID);
450 iaNaOption.setT1(T1_CLIENT);
451 iaNaOption.setT2(T2_CLIENT);
452 iaNaOption.setOptions(ImmutableList.of(iaAddressOption));
453 options.add(iaNaOption);
454
455 relaiedDhcp6.setOptions(options);
456
457 Dhcp6RelayOption relayOption = new Dhcp6RelayOption();
458 relayOption.setPayload(relaiedDhcp6);
459
460 relayMsg.setOptions(ImmutableList.of(relayOption));
461
Yuta HIGUCHI7bfd6072017-08-01 16:23:50 -0700462 assertArrayEquals(Resources.toByteArray(Dhcp6RelayTest.class.getResource(REQUEST)),
Yi Tsengca34e1d2017-07-18 16:16:25 -0700463 relayMsg.serialize());
464 }
465
466 /**
467 * Test deserialize relay message with reply message.
468 *
469 * @throws Exception exception while deserialize the DHCPv6 payload
470 */
471 @Test
472 public void deserializeReply() throws Exception {
Yuta HIGUCHI7bfd6072017-08-01 16:23:50 -0700473 byte[] data = Resources.toByteArray(getClass().getResource(REPLY));
Yi Tsengca34e1d2017-07-18 16:16:25 -0700474 DHCP6 relayMsg = DHCP6.deserializer().deserialize(data, 0, data.length);
475
476 assertEquals(relayMsg.getMsgType(), DHCP6.MsgType.RELAY_REPL.value());
477 assertEquals(relayMsg.getHopCount(), HOP_COUNT);
478 assertEquals(relayMsg.getIp6LinkAddress(), LINK_ADDRESS);
479 assertEquals(relayMsg.getIp6PeerAddress(), PEER_ADDRESS);
480
481 assertEquals(relayMsg.getOptions().size(), 1);
482 Dhcp6Option option = relayMsg.getOptions().get(0);
483 assertEquals(option.getCode(), DHCP6.OptionCode.RELAY_MSG.value());
484 assertEquals(option.getLength(), 84);
485 assertTrue(option.getPayload() instanceof DHCP6);
486
487 DHCP6 relaiedDhcp6 = (DHCP6) option.getPayload();
488 assertEquals(relaiedDhcp6.getMsgType(), DHCP6.MsgType.REPLY.value());
489 assertEquals(relaiedDhcp6.getTransactionId(), XID_2);
490 assertEquals(relaiedDhcp6.getOptions().size(), 3);
491
492 // IA NA
493 option = relaiedDhcp6.getOptions().get(0);
494 assertTrue(option instanceof Dhcp6IaNaOption);
495 Dhcp6IaNaOption iaNaOption = (Dhcp6IaNaOption) option;
496 assertEquals(iaNaOption.getCode(), DHCP6.OptionCode.IA_NA.value());
497 assertEquals(iaNaOption.getLength(), 40);
498 assertEquals(iaNaOption.getIaId(), IA_ID);
499 assertEquals(iaNaOption.getT1(), T1_SERVER);
500 assertEquals(iaNaOption.getT2(), T2_SERVER);
501 assertEquals(iaNaOption.getOptions().size(), 1);
502
503 // IA Address (in IA NA)
504 assertTrue(iaNaOption.getOptions().get(0) instanceof Dhcp6IaAddressOption);
505 Dhcp6IaAddressOption iaAddressOption =
506 (Dhcp6IaAddressOption) iaNaOption.getOptions().get(0);
507 assertEquals(iaAddressOption.getIp6Address(), IA_ADDRESS);
508 assertEquals(iaAddressOption.getPreferredLifetime(), PREFFERRED_LT_SERVER);
509 assertEquals(iaAddressOption.getValidLifetime(), VALID_LT_SERVER);
510 assertNull(iaAddressOption.getOptions());
511
512 // Client ID
513 option = relaiedDhcp6.getOptions().get(1);
514 assertTrue(option instanceof Dhcp6ClientIdOption);
515 Dhcp6ClientIdOption clientIdOption = (Dhcp6ClientIdOption) option;
516 assertEquals(clientIdOption.getCode(), DHCP6.OptionCode.CLIENTID.value());
517 assertEquals(clientIdOption.getLength(), 14);
518 assertEquals(clientIdOption.getDuid().getDuidType(), Dhcp6Duid.DuidType.DUID_LLT);
519 assertEquals(clientIdOption.getDuid().getHardwareType(), 1);
520 assertEquals(clientIdOption.getDuid().getDuidTime(), CLIENT_DUID_TIME);
521 assertArrayEquals(clientIdOption.getDuid().getLinkLayerAddress(), CLIENT_MAC.toBytes());
522
523 // Server ID
524 option = relaiedDhcp6.getOptions().get(2);
525 assertEquals(option.getCode(), DHCP6.OptionCode.SERVERID.value());
526 assertEquals(option.getLength(), 14);
527 assertArrayEquals(option.getData(),
528 new byte[]{0, 1, 0, 1, 32, -1, -8, -17, 0, -103, 102, 0, 0, 1});
529
530 assertArrayEquals(data, relayMsg.serialize());
531 }
532
533 @Test
534 public void serializeReply() throws Exception {
535 DHCP6 relayMsg = new DHCP6();
536 relayMsg.setMsgType(DHCP6.MsgType.RELAY_REPL.value());
537 relayMsg.setHopCount((byte) HOP_COUNT);
538 relayMsg.setLinkAddress(LINK_ADDRESS.toOctets());
539 relayMsg.setPeerAddress(PEER_ADDRESS.toOctets());
540
541 DHCP6 relaiedDhcp6 = new DHCP6();
542 relaiedDhcp6.setMsgType(DHCP6.MsgType.REPLY.value());
543 relaiedDhcp6.setTransactionId(XID_2);
544 List<Dhcp6Option> options = Lists.newArrayList();
545
546 // IA address
547 Dhcp6IaAddressOption iaAddressOption = new Dhcp6IaAddressOption();
548 iaAddressOption.setIp6Address(IA_ADDRESS);
549 iaAddressOption.setPreferredLifetime(PREFFERRED_LT_SERVER);
550 iaAddressOption.setValidLifetime(VALID_LT_SERVER);
551
552 // IA NA
553 Dhcp6IaNaOption iaNaOption = new Dhcp6IaNaOption();
554 iaNaOption.setIaId(IA_ID);
555 iaNaOption.setT1(T1_SERVER);
556 iaNaOption.setT2(T2_SERVER);
557 iaNaOption.setOptions(ImmutableList.of(iaAddressOption));
558 options.add(iaNaOption);
559
560 // Client ID
561 Dhcp6Duid duid = new Dhcp6Duid();
562 duid.setDuidType(Dhcp6Duid.DuidType.DUID_LLT);
563 duid.setHardwareType((short) 1);
564 duid.setDuidTime(CLIENT_DUID_TIME);
565 duid.setLinkLayerAddress(CLIENT_MAC.toBytes());
566 Dhcp6ClientIdOption clientIdOption = new Dhcp6ClientIdOption();
567 clientIdOption.setDuid(duid);
568 options.add(clientIdOption);
569
570 // Server ID
571 Dhcp6Option option = new Dhcp6Option();
572 option.setCode(DHCP6.OptionCode.SERVERID.value());
573 option.setLength((short) 14);
574 option.setData(new byte[]{0, 1, 0, 1, 32, -1, -8, -17, 0, -103, 102, 0, 0, 1});
575 options.add(option);
576
577 relaiedDhcp6.setOptions(options);
578
579 Dhcp6RelayOption relayOption = new Dhcp6RelayOption();
580 relayOption.setPayload(relaiedDhcp6);
581
582 relayMsg.setOptions(ImmutableList.of(relayOption));
583
Yuta HIGUCHI7bfd6072017-08-01 16:23:50 -0700584 assertArrayEquals(Resources.toByteArray(Dhcp6RelayTest.class.getResource(REPLY)),
Yi Tsengca34e1d2017-07-18 16:16:25 -0700585 relayMsg.serialize());
586 }
587}