Yi Tseng | ca34e1d | 2017-07-18 16:16:25 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame^] | 2 | * Copyright 2017-present Open Networking Foundation |
Yi Tseng | ca34e1d | 2017-07-18 16:16:25 -0700 | [diff] [blame] | 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 | package org.onlab.packet.dhcp; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableList; |
| 20 | import com.google.common.collect.Lists; |
Yuta HIGUCHI | 7bfd607 | 2017-08-01 16:23:50 -0700 | [diff] [blame] | 21 | import com.google.common.io.Resources; |
| 22 | |
Yi Tseng | ca34e1d | 2017-07-18 16:16:25 -0700 | [diff] [blame] | 23 | import org.junit.Test; |
| 24 | import org.onlab.packet.DHCP6; |
| 25 | import org.onlab.packet.Deserializer; |
| 26 | import org.onlab.packet.Ip6Address; |
| 27 | import org.onlab.packet.MacAddress; |
| 28 | import org.onlab.packet.PacketTestUtils; |
| 29 | |
| 30 | import java.nio.ByteBuffer; |
| 31 | import java.util.Collections; |
| 32 | import java.util.List; |
| 33 | |
| 34 | import static org.junit.Assert.*; |
| 35 | import static org.junit.Assert.assertNull; |
| 36 | |
| 37 | public class Dhcp6Test { |
| 38 | private static final String SOLICIT = "dhcp6_solicit.bin"; |
| 39 | private static final String ADVERTISE = "dhcp6_advertise.bin"; |
| 40 | private static final String REQUEST = "dhcp6_request.bin"; |
| 41 | private static final String REPLY = "dhcp6_reply.bin"; |
| 42 | |
| 43 | private static final int XID_1 = 13346301; |
| 44 | private static final int XID_2 = 9807588; |
| 45 | private static final int IA_ID = 1; |
| 46 | private static final int T1_CLIENT = 3600; |
| 47 | private static final int T2_CLIENT = 5400; |
| 48 | private static final int T1_SERVER = 0; |
| 49 | private static final int T2_SERVER = 0; |
| 50 | private static final Ip6Address IA_ADDRESS = Ip6Address.valueOf("2000::201"); |
| 51 | private static final int PREFFERRED_LT_SERVER = 375; |
| 52 | private static final int VALID_LT_SERVER = 600; |
| 53 | private static final int PREFFERRED_LT_REQ = 7200; |
| 54 | private static final int VALID_LT_REQ = 7500; |
| 55 | private static final MacAddress CLIENT_MAC = MacAddress.valueOf("00:bb:00:00:00:01"); |
| 56 | private static final int CLIENT_DUID_TIME = 0x210016b4; |
| 57 | |
| 58 | |
| 59 | private Deserializer<DHCP6> deserializer = DHCP6.deserializer(); |
| 60 | |
| 61 | |
| 62 | @Test |
| 63 | public void testDeserializeBadInput() throws Exception { |
| 64 | PacketTestUtils.testDeserializeBadInput(deserializer); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Truncated a simple DHCPv6 payload. |
| 69 | */ |
| 70 | @Test |
| 71 | public void testDeserializeTruncated() throws Exception { |
| 72 | ByteBuffer bb = ByteBuffer.allocate(4); |
| 73 | bb.put(DHCP6.MsgType.REQUEST.value()); |
| 74 | bb.put(new byte[]{0x00, 0x00}); |
| 75 | PacketTestUtils.testDeserializeTruncated(deserializer, bb.array()); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Test DHCPv6 solicit message. |
| 80 | * |
| 81 | * @throws Exception exception while deserialize the DHCPv6 payload |
| 82 | */ |
| 83 | @Test |
| 84 | public void testDeserializeSolicit() throws Exception { |
Yuta HIGUCHI | 7bfd607 | 2017-08-01 16:23:50 -0700 | [diff] [blame] | 85 | byte[] data = Resources.toByteArray(Dhcp6RelayTest.class.getResource(SOLICIT)); |
Yi Tseng | ca34e1d | 2017-07-18 16:16:25 -0700 | [diff] [blame] | 86 | DHCP6 dhcp6 = DHCP6.deserializer().deserialize(data, 0, data.length); |
| 87 | assertEquals(dhcp6.getMsgType(), DHCP6.MsgType.SOLICIT.value()); |
| 88 | assertEquals(dhcp6.getTransactionId(), XID_1); |
| 89 | assertEquals(dhcp6.getOptions().size(), 4); |
| 90 | |
| 91 | // Client ID |
| 92 | Dhcp6Option option = dhcp6.getOptions().get(0); |
| 93 | assertTrue(option instanceof Dhcp6ClientIdOption); |
| 94 | Dhcp6ClientIdOption clientIdOption = (Dhcp6ClientIdOption) option; |
| 95 | assertEquals(clientIdOption.getCode(), DHCP6.OptionCode.CLIENTID.value()); |
| 96 | assertEquals(clientIdOption.getLength(), 14); |
| 97 | assertEquals(clientIdOption.getDuid().getDuidType(), Dhcp6Duid.DuidType.DUID_LLT); |
| 98 | assertEquals(clientIdOption.getDuid().getHardwareType(), 1); |
| 99 | assertEquals(clientIdOption.getDuid().getDuidTime(), CLIENT_DUID_TIME); |
| 100 | assertArrayEquals(clientIdOption.getDuid().getLinkLayerAddress(), CLIENT_MAC.toBytes()); |
| 101 | |
| 102 | // ORO |
| 103 | option = dhcp6.getOptions().get(1); |
| 104 | assertEquals(option.getCode(), DHCP6.OptionCode.ORO.value()); |
| 105 | assertEquals(option.getLength(), 8); |
| 106 | assertArrayEquals(option.getData(), |
| 107 | new byte[]{0, 23, 0, 24, 0, 39, 0, 31}); |
| 108 | |
| 109 | // ELAPSED_TIME |
| 110 | option = dhcp6.getOptions().get(2); |
| 111 | assertEquals(option.getCode(), DHCP6.OptionCode.ELAPSED_TIME.value()); |
| 112 | assertEquals(option.getLength(), 2); |
| 113 | assertArrayEquals(option.getData(), |
| 114 | new byte[]{0, 0}); |
| 115 | |
| 116 | // IA NA |
| 117 | option = dhcp6.getOptions().get(3); |
| 118 | assertTrue(option instanceof Dhcp6IaNaOption); |
| 119 | Dhcp6IaNaOption iaNaOption = (Dhcp6IaNaOption) option; |
| 120 | assertEquals(iaNaOption.getCode(), DHCP6.OptionCode.IA_NA.value()); |
| 121 | assertEquals(iaNaOption.getLength(), 12); |
| 122 | assertEquals(iaNaOption.getIaId(), IA_ID); |
| 123 | assertEquals(iaNaOption.getT1(), T1_CLIENT); |
| 124 | assertEquals(iaNaOption.getT2(), T2_CLIENT); |
| 125 | assertEquals(iaNaOption.getOptions().size(), 0); |
| 126 | |
| 127 | assertArrayEquals(data, dhcp6.serialize()); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Test serialize solicit message. |
| 132 | * |
| 133 | * @throws Exception exception while serialize the DHCPv6 payload |
| 134 | */ |
| 135 | @Test |
| 136 | public void serializeSolicit() throws Exception { |
| 137 | DHCP6 dhcp6 = new DHCP6(); |
| 138 | dhcp6.setMsgType(DHCP6.MsgType.SOLICIT.value()); |
| 139 | dhcp6.setTransactionId(XID_1); |
| 140 | List<Dhcp6Option> options = Lists.newArrayList(); |
| 141 | |
| 142 | // Client ID |
| 143 | Dhcp6Duid duid = new Dhcp6Duid(); |
| 144 | duid.setDuidType(Dhcp6Duid.DuidType.DUID_LLT); |
| 145 | duid.setHardwareType((short) 1); |
| 146 | duid.setDuidTime(CLIENT_DUID_TIME); |
| 147 | duid.setLinkLayerAddress(CLIENT_MAC.toBytes()); |
| 148 | Dhcp6ClientIdOption clientIdOption = new Dhcp6ClientIdOption(); |
| 149 | clientIdOption.setDuid(duid); |
| 150 | options.add(clientIdOption); |
| 151 | |
| 152 | // Option request |
| 153 | Dhcp6Option option = new Dhcp6Option(); |
| 154 | option.setCode(DHCP6.OptionCode.ORO.value()); |
| 155 | option.setLength((short) 8); |
| 156 | option.setData(new byte[]{0, 23, 0, 24, 0, 39, 0, 31}); |
| 157 | options.add(option); |
| 158 | |
| 159 | // Elapsed Time |
| 160 | option = new Dhcp6Option(); |
| 161 | option.setCode(DHCP6.OptionCode.ELAPSED_TIME.value()); |
| 162 | option.setLength((short) 2); |
| 163 | option.setData(new byte[]{0, 0}); |
| 164 | options.add(option); |
| 165 | |
| 166 | // IA NA |
| 167 | Dhcp6IaNaOption iaNaOption = new Dhcp6IaNaOption(); |
| 168 | iaNaOption.setIaId(IA_ID); |
| 169 | iaNaOption.setT1(T1_CLIENT); |
| 170 | iaNaOption.setT2(T2_CLIENT); |
| 171 | iaNaOption.setOptions(Collections.emptyList()); |
| 172 | options.add(iaNaOption); |
| 173 | dhcp6.setOptions(options); |
| 174 | |
| 175 | |
| 176 | Dhcp6RelayOption relayOption = new Dhcp6RelayOption(); |
| 177 | relayOption.setPayload(dhcp6); |
| 178 | |
Yuta HIGUCHI | 7bfd607 | 2017-08-01 16:23:50 -0700 | [diff] [blame] | 179 | assertArrayEquals(Resources.toByteArray(Dhcp6RelayTest.class.getResource(SOLICIT)), |
Yi Tseng | ca34e1d | 2017-07-18 16:16:25 -0700 | [diff] [blame] | 180 | dhcp6.serialize()); |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Test deserialize advertise message. |
| 185 | * |
| 186 | * @throws Exception exception while deserialize the DHCPv6 payload |
| 187 | */ |
| 188 | @Test |
| 189 | public void deserializeAdvertise() throws Exception { |
Yuta HIGUCHI | 7bfd607 | 2017-08-01 16:23:50 -0700 | [diff] [blame] | 190 | byte[] data = Resources.toByteArray(getClass().getResource(ADVERTISE)); |
Yi Tseng | ca34e1d | 2017-07-18 16:16:25 -0700 | [diff] [blame] | 191 | |
| 192 | |
| 193 | DHCP6 dhcp6 = DHCP6.deserializer().deserialize(data, 0, data.length); |
| 194 | assertEquals(dhcp6.getMsgType(), DHCP6.MsgType.ADVERTISE.value()); |
| 195 | assertEquals(dhcp6.getTransactionId(), XID_1); |
| 196 | assertEquals(dhcp6.getOptions().size(), 3); |
| 197 | |
| 198 | // IA NA |
| 199 | Dhcp6Option option = dhcp6.getOptions().get(0); |
| 200 | assertTrue(option instanceof Dhcp6IaNaOption); |
| 201 | Dhcp6IaNaOption iaNaOption = (Dhcp6IaNaOption) option; |
| 202 | assertEquals(iaNaOption.getCode(), DHCP6.OptionCode.IA_NA.value()); |
| 203 | assertEquals(iaNaOption.getLength(), 40); |
| 204 | assertEquals(iaNaOption.getIaId(), IA_ID); |
| 205 | assertEquals(iaNaOption.getT1(), T1_SERVER); |
| 206 | assertEquals(iaNaOption.getT2(), T2_SERVER); |
| 207 | assertEquals(iaNaOption.getOptions().size(), 1); |
| 208 | |
| 209 | // IA Address (in IA NA) |
| 210 | assertTrue(iaNaOption.getOptions().get(0) instanceof Dhcp6IaAddressOption); |
| 211 | Dhcp6IaAddressOption iaAddressOption = |
| 212 | (Dhcp6IaAddressOption) iaNaOption.getOptions().get(0); |
| 213 | assertEquals(iaAddressOption.getIp6Address(), IA_ADDRESS); |
| 214 | assertEquals(iaAddressOption.getPreferredLifetime(), PREFFERRED_LT_SERVER); |
| 215 | assertEquals(iaAddressOption.getValidLifetime(), VALID_LT_SERVER); |
| 216 | assertNull(iaAddressOption.getOptions()); |
| 217 | |
| 218 | // Client ID |
| 219 | option = dhcp6.getOptions().get(1); |
| 220 | assertTrue(option instanceof Dhcp6ClientIdOption); |
| 221 | Dhcp6ClientIdOption clientIdOption = (Dhcp6ClientIdOption) option; |
| 222 | assertEquals(clientIdOption.getCode(), DHCP6.OptionCode.CLIENTID.value()); |
| 223 | assertEquals(clientIdOption.getLength(), 14); |
| 224 | assertEquals(clientIdOption.getDuid().getDuidType(), Dhcp6Duid.DuidType.DUID_LLT); |
| 225 | assertEquals(clientIdOption.getDuid().getHardwareType(), 1); |
| 226 | assertEquals(clientIdOption.getDuid().getDuidTime(), CLIENT_DUID_TIME); |
| 227 | assertArrayEquals(clientIdOption.getDuid().getLinkLayerAddress(), CLIENT_MAC.toBytes()); |
| 228 | |
| 229 | // Server ID |
| 230 | option = dhcp6.getOptions().get(2); |
| 231 | assertEquals(option.getCode(), DHCP6.OptionCode.SERVERID.value()); |
| 232 | assertEquals(option.getLength(), 14); |
| 233 | assertArrayEquals(option.getData(), |
| 234 | new byte[]{0, 1, 0, 1, 32, -1, -8, -17, 0, -103, 102, 0, 0, 1}); |
| 235 | |
| 236 | assertArrayEquals(data, dhcp6.serialize()); |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Test serialize advertise message. |
| 241 | * |
| 242 | * @throws Exception exception while serialize the DHCPv6 payload |
| 243 | */ |
| 244 | @Test |
| 245 | public void serializeAdvertise() throws Exception { |
| 246 | DHCP6 dhcp6 = new DHCP6(); |
| 247 | dhcp6.setMsgType(DHCP6.MsgType.ADVERTISE.value()); |
| 248 | dhcp6.setTransactionId(XID_1); |
| 249 | List<Dhcp6Option> options = Lists.newArrayList(); |
| 250 | |
| 251 | // IA address |
| 252 | Dhcp6IaAddressOption iaAddressOption = new Dhcp6IaAddressOption(); |
| 253 | iaAddressOption.setIp6Address(IA_ADDRESS); |
| 254 | iaAddressOption.setPreferredLifetime(PREFFERRED_LT_SERVER); |
| 255 | iaAddressOption.setValidLifetime(VALID_LT_SERVER); |
| 256 | |
| 257 | // IA NA |
| 258 | Dhcp6IaNaOption iaNaOption = new Dhcp6IaNaOption(); |
| 259 | iaNaOption.setIaId(IA_ID); |
| 260 | iaNaOption.setT1(T1_SERVER); |
| 261 | iaNaOption.setT2(T2_SERVER); |
| 262 | iaNaOption.setOptions(ImmutableList.of(iaAddressOption)); |
| 263 | options.add(iaNaOption); |
| 264 | |
| 265 | // Client ID |
| 266 | Dhcp6Duid duid = new Dhcp6Duid(); |
| 267 | duid.setDuidType(Dhcp6Duid.DuidType.DUID_LLT); |
| 268 | duid.setHardwareType((short) 1); |
| 269 | duid.setDuidTime(CLIENT_DUID_TIME); |
| 270 | duid.setLinkLayerAddress(CLIENT_MAC.toBytes()); |
| 271 | Dhcp6ClientIdOption clientIdOption = new Dhcp6ClientIdOption(); |
| 272 | clientIdOption.setDuid(duid); |
| 273 | options.add(clientIdOption); |
| 274 | |
| 275 | // Server ID |
| 276 | Dhcp6Option option = new Dhcp6Option(); |
| 277 | option.setCode(DHCP6.OptionCode.SERVERID.value()); |
| 278 | option.setLength((short) 14); |
| 279 | option.setData(new byte[]{0, 1, 0, 1, 32, -1, -8, -17, 0, -103, 102, 0, 0, 1}); |
| 280 | options.add(option); |
| 281 | |
| 282 | dhcp6.setOptions(options); |
| 283 | |
| 284 | Dhcp6RelayOption relayOption = new Dhcp6RelayOption(); |
| 285 | relayOption.setPayload(dhcp6); |
| 286 | |
Yuta HIGUCHI | 7bfd607 | 2017-08-01 16:23:50 -0700 | [diff] [blame] | 287 | assertArrayEquals(Resources.toByteArray(Dhcp6RelayTest.class.getResource(ADVERTISE)), |
Yi Tseng | ca34e1d | 2017-07-18 16:16:25 -0700 | [diff] [blame] | 288 | dhcp6.serialize()); |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * Test deserialize request message. |
| 293 | * |
| 294 | * @throws Exception exception while deserialize the DHCPv6 payload |
| 295 | */ |
| 296 | @Test |
| 297 | public void deserializeRequest() throws Exception { |
Yuta HIGUCHI | 7bfd607 | 2017-08-01 16:23:50 -0700 | [diff] [blame] | 298 | byte[] data = Resources.toByteArray(getClass().getResource(REQUEST)); |
Yi Tseng | ca34e1d | 2017-07-18 16:16:25 -0700 | [diff] [blame] | 299 | DHCP6 dhcp6 = DHCP6.deserializer().deserialize(data, 0, data.length); |
| 300 | assertEquals(dhcp6.getMsgType(), DHCP6.MsgType.REQUEST.value()); |
| 301 | assertEquals(dhcp6.getTransactionId(), XID_2); |
| 302 | assertEquals(dhcp6.getOptions().size(), 5); |
| 303 | |
| 304 | // Client ID |
| 305 | Dhcp6Option option = dhcp6.getOptions().get(0); |
| 306 | assertTrue(option instanceof Dhcp6ClientIdOption); |
| 307 | Dhcp6ClientIdOption clientIdOption = (Dhcp6ClientIdOption) option; |
| 308 | assertEquals(clientIdOption.getCode(), DHCP6.OptionCode.CLIENTID.value()); |
| 309 | assertEquals(clientIdOption.getLength(), 14); |
| 310 | assertEquals(clientIdOption.getDuid().getDuidType(), Dhcp6Duid.DuidType.DUID_LLT); |
| 311 | assertEquals(clientIdOption.getDuid().getHardwareType(), 1); |
| 312 | assertEquals(clientIdOption.getDuid().getDuidTime(), CLIENT_DUID_TIME); |
| 313 | assertArrayEquals(clientIdOption.getDuid().getLinkLayerAddress(), CLIENT_MAC.toBytes()); |
| 314 | |
| 315 | // Server ID |
| 316 | option = dhcp6.getOptions().get(1); |
| 317 | assertEquals(option.getCode(), DHCP6.OptionCode.SERVERID.value()); |
| 318 | assertEquals(option.getLength(), 14); |
| 319 | assertArrayEquals(option.getData(), |
| 320 | new byte[]{0, 1, 0, 1, 32, -1, -8, -17, 0, -103, 102, 0, 0, 1}); |
| 321 | |
| 322 | // Option Request |
| 323 | option = dhcp6.getOptions().get(2); |
| 324 | assertEquals(option.getCode(), DHCP6.OptionCode.ORO.value()); |
| 325 | assertEquals(option.getLength(), 8); |
| 326 | assertArrayEquals(option.getData(), new byte[]{0, 23, 0, 24, 0, 39, 0, 31}); |
| 327 | |
| 328 | // ELAPSED_TIME |
| 329 | option = dhcp6.getOptions().get(3); |
| 330 | assertEquals(option.getCode(), DHCP6.OptionCode.ELAPSED_TIME.value()); |
| 331 | assertEquals(option.getLength(), 2); |
| 332 | assertArrayEquals(option.getData(), |
| 333 | new byte[]{0, 0}); |
| 334 | |
| 335 | // IA NA |
| 336 | option = dhcp6.getOptions().get(4); |
| 337 | assertTrue(option instanceof Dhcp6IaNaOption); |
| 338 | Dhcp6IaNaOption iaNaOption = (Dhcp6IaNaOption) option; |
| 339 | assertEquals(iaNaOption.getCode(), DHCP6.OptionCode.IA_NA.value()); |
| 340 | assertEquals(iaNaOption.getLength(), 40); |
| 341 | assertEquals(iaNaOption.getIaId(), IA_ID); |
| 342 | assertEquals(iaNaOption.getT1(), T1_CLIENT); |
| 343 | assertEquals(iaNaOption.getT2(), T2_CLIENT); |
| 344 | assertEquals(iaNaOption.getOptions().size(), 1); |
| 345 | |
| 346 | // IA Address (in IA NA) |
| 347 | assertTrue(iaNaOption.getOptions().get(0) instanceof Dhcp6IaAddressOption); |
| 348 | Dhcp6IaAddressOption iaAddressOption = |
| 349 | (Dhcp6IaAddressOption) iaNaOption.getOptions().get(0); |
| 350 | assertEquals(iaAddressOption.getIp6Address(), IA_ADDRESS); |
| 351 | assertEquals(iaAddressOption.getPreferredLifetime(), PREFFERRED_LT_REQ); |
| 352 | assertEquals(iaAddressOption.getValidLifetime(), VALID_LT_REQ); |
| 353 | assertNull(iaAddressOption.getOptions()); |
| 354 | |
| 355 | assertArrayEquals(data, dhcp6.serialize()); |
| 356 | } |
| 357 | |
| 358 | /** |
| 359 | * Test serialize request message. |
| 360 | * |
| 361 | * @throws Exception exception while serialize the DHCPv6 payload |
| 362 | */ |
| 363 | @Test |
| 364 | public void serializeRequest() throws Exception { |
| 365 | DHCP6 dhcp6 = new DHCP6(); |
| 366 | dhcp6.setMsgType(DHCP6.MsgType.REQUEST.value()); |
| 367 | dhcp6.setTransactionId(XID_2); |
| 368 | List<Dhcp6Option> options = Lists.newArrayList(); |
| 369 | |
| 370 | // Client ID |
| 371 | Dhcp6Duid duid = new Dhcp6Duid(); |
| 372 | duid.setDuidType(Dhcp6Duid.DuidType.DUID_LLT); |
| 373 | duid.setHardwareType((short) 1); |
| 374 | duid.setDuidTime(CLIENT_DUID_TIME); |
| 375 | duid.setLinkLayerAddress(CLIENT_MAC.toBytes()); |
| 376 | Dhcp6ClientIdOption clientIdOption = new Dhcp6ClientIdOption(); |
| 377 | clientIdOption.setDuid(duid); |
| 378 | options.add(clientIdOption); |
| 379 | |
| 380 | // Server ID |
| 381 | Dhcp6Option option = new Dhcp6Option(); |
| 382 | option.setCode(DHCP6.OptionCode.SERVERID.value()); |
| 383 | option.setLength((short) 14); |
| 384 | option.setData(new byte[]{0, 1, 0, 1, 32, -1, -8, -17, 0, -103, 102, 0, 0, 1}); |
| 385 | options.add(option); |
| 386 | |
| 387 | // Option request |
| 388 | option = new Dhcp6Option(); |
| 389 | option.setCode(DHCP6.OptionCode.ORO.value()); |
| 390 | option.setLength((short) 8); |
| 391 | option.setData(new byte[]{0, 23, 0, 24, 0, 39, 0, 31}); |
| 392 | options.add(option); |
| 393 | |
| 394 | // Elapsed Time |
| 395 | option = new Dhcp6Option(); |
| 396 | option.setCode(DHCP6.OptionCode.ELAPSED_TIME.value()); |
| 397 | option.setLength((short) 2); |
| 398 | option.setData(new byte[]{0, 0}); |
| 399 | options.add(option); |
| 400 | |
| 401 | // IA address |
| 402 | Dhcp6IaAddressOption iaAddressOption = new Dhcp6IaAddressOption(); |
| 403 | iaAddressOption.setIp6Address(IA_ADDRESS); |
| 404 | iaAddressOption.setPreferredLifetime(PREFFERRED_LT_REQ); |
| 405 | iaAddressOption.setValidLifetime(VALID_LT_REQ); |
| 406 | |
| 407 | // IA NA |
| 408 | Dhcp6IaNaOption iaNaOption = new Dhcp6IaNaOption(); |
| 409 | iaNaOption.setIaId(IA_ID); |
| 410 | iaNaOption.setT1(T1_CLIENT); |
| 411 | iaNaOption.setT2(T2_CLIENT); |
| 412 | iaNaOption.setOptions(ImmutableList.of(iaAddressOption)); |
| 413 | options.add(iaNaOption); |
| 414 | |
| 415 | dhcp6.setOptions(options); |
| 416 | |
| 417 | Dhcp6RelayOption relayOption = new Dhcp6RelayOption(); |
| 418 | relayOption.setPayload(dhcp6); |
| 419 | |
Yuta HIGUCHI | 7bfd607 | 2017-08-01 16:23:50 -0700 | [diff] [blame] | 420 | assertArrayEquals(Resources.toByteArray(Dhcp6RelayTest.class.getResource(REQUEST)), |
Yi Tseng | ca34e1d | 2017-07-18 16:16:25 -0700 | [diff] [blame] | 421 | dhcp6.serialize()); |
| 422 | } |
| 423 | |
| 424 | /** |
| 425 | * Test deserialize relay message with reply message. |
| 426 | * |
| 427 | * @throws Exception exception while deserialize the DHCPv6 payload |
| 428 | */ |
| 429 | @Test |
| 430 | public void deserializeReply() throws Exception { |
Yuta HIGUCHI | 7bfd607 | 2017-08-01 16:23:50 -0700 | [diff] [blame] | 431 | byte[] data = Resources.toByteArray(getClass().getResource(REPLY)); |
Yi Tseng | ca34e1d | 2017-07-18 16:16:25 -0700 | [diff] [blame] | 432 | |
| 433 | DHCP6 dhcp6 = DHCP6.deserializer().deserialize(data, 0, data.length); |
| 434 | assertEquals(dhcp6.getMsgType(), DHCP6.MsgType.REPLY.value()); |
| 435 | assertEquals(dhcp6.getTransactionId(), XID_2); |
| 436 | assertEquals(dhcp6.getOptions().size(), 3); |
| 437 | |
| 438 | // IA NA |
| 439 | Dhcp6Option option = dhcp6.getOptions().get(0); |
| 440 | assertTrue(option instanceof Dhcp6IaNaOption); |
| 441 | Dhcp6IaNaOption iaNaOption = (Dhcp6IaNaOption) option; |
| 442 | assertEquals(iaNaOption.getCode(), DHCP6.OptionCode.IA_NA.value()); |
| 443 | assertEquals(iaNaOption.getLength(), 40); |
| 444 | assertEquals(iaNaOption.getIaId(), IA_ID); |
| 445 | assertEquals(iaNaOption.getT1(), T1_SERVER); |
| 446 | assertEquals(iaNaOption.getT2(), T2_SERVER); |
| 447 | assertEquals(iaNaOption.getOptions().size(), 1); |
| 448 | |
| 449 | // IA Address (in IA NA) |
| 450 | assertTrue(iaNaOption.getOptions().get(0) instanceof Dhcp6IaAddressOption); |
| 451 | Dhcp6IaAddressOption iaAddressOption = |
| 452 | (Dhcp6IaAddressOption) iaNaOption.getOptions().get(0); |
| 453 | assertEquals(iaAddressOption.getIp6Address(), IA_ADDRESS); |
| 454 | assertEquals(iaAddressOption.getPreferredLifetime(), PREFFERRED_LT_SERVER); |
| 455 | assertEquals(iaAddressOption.getValidLifetime(), VALID_LT_SERVER); |
| 456 | assertNull(iaAddressOption.getOptions()); |
| 457 | |
| 458 | // Client ID |
| 459 | option = dhcp6.getOptions().get(1); |
| 460 | assertTrue(option instanceof Dhcp6ClientIdOption); |
| 461 | Dhcp6ClientIdOption clientIdOption = (Dhcp6ClientIdOption) option; |
| 462 | assertEquals(clientIdOption.getCode(), DHCP6.OptionCode.CLIENTID.value()); |
| 463 | assertEquals(clientIdOption.getLength(), 14); |
| 464 | assertEquals(clientIdOption.getDuid().getDuidType(), Dhcp6Duid.DuidType.DUID_LLT); |
| 465 | assertEquals(clientIdOption.getDuid().getHardwareType(), 1); |
| 466 | assertEquals(clientIdOption.getDuid().getDuidTime(), CLIENT_DUID_TIME); |
| 467 | assertArrayEquals(clientIdOption.getDuid().getLinkLayerAddress(), CLIENT_MAC.toBytes()); |
| 468 | |
| 469 | // Server ID |
| 470 | option = dhcp6.getOptions().get(2); |
| 471 | assertEquals(option.getCode(), DHCP6.OptionCode.SERVERID.value()); |
| 472 | assertEquals(option.getLength(), 14); |
| 473 | assertArrayEquals(option.getData(), |
| 474 | new byte[]{0, 1, 0, 1, 32, -1, -8, -17, 0, -103, 102, 0, 0, 1}); |
| 475 | |
| 476 | assertArrayEquals(data, dhcp6.serialize()); |
| 477 | } |
| 478 | |
| 479 | @Test |
| 480 | public void serializeReply() throws Exception { |
| 481 | DHCP6 dhcp6 = new DHCP6(); |
| 482 | dhcp6.setMsgType(DHCP6.MsgType.REPLY.value()); |
| 483 | dhcp6.setTransactionId(XID_2); |
| 484 | List<Dhcp6Option> options = Lists.newArrayList(); |
| 485 | |
| 486 | // IA address |
| 487 | Dhcp6IaAddressOption iaAddressOption = new Dhcp6IaAddressOption(); |
| 488 | iaAddressOption.setIp6Address(IA_ADDRESS); |
| 489 | iaAddressOption.setPreferredLifetime(PREFFERRED_LT_SERVER); |
| 490 | iaAddressOption.setValidLifetime(VALID_LT_SERVER); |
| 491 | |
| 492 | // IA NA |
| 493 | Dhcp6IaNaOption iaNaOption = new Dhcp6IaNaOption(); |
| 494 | iaNaOption.setIaId(IA_ID); |
| 495 | iaNaOption.setT1(T1_SERVER); |
| 496 | iaNaOption.setT2(T2_SERVER); |
| 497 | iaNaOption.setOptions(ImmutableList.of(iaAddressOption)); |
| 498 | options.add(iaNaOption); |
| 499 | |
| 500 | // Client ID |
| 501 | Dhcp6Duid duid = new Dhcp6Duid(); |
| 502 | duid.setDuidType(Dhcp6Duid.DuidType.DUID_LLT); |
| 503 | duid.setHardwareType((short) 1); |
| 504 | duid.setDuidTime(CLIENT_DUID_TIME); |
| 505 | duid.setLinkLayerAddress(CLIENT_MAC.toBytes()); |
| 506 | Dhcp6ClientIdOption clientIdOption = new Dhcp6ClientIdOption(); |
| 507 | clientIdOption.setDuid(duid); |
| 508 | options.add(clientIdOption); |
| 509 | |
| 510 | // Server ID |
| 511 | Dhcp6Option option = new Dhcp6Option(); |
| 512 | option.setCode(DHCP6.OptionCode.SERVERID.value()); |
| 513 | option.setLength((short) 14); |
| 514 | option.setData(new byte[]{0, 1, 0, 1, 32, -1, -8, -17, 0, -103, 102, 0, 0, 1}); |
| 515 | options.add(option); |
| 516 | |
| 517 | dhcp6.setOptions(options); |
| 518 | |
| 519 | Dhcp6RelayOption relayOption = new Dhcp6RelayOption(); |
| 520 | relayOption.setPayload(dhcp6); |
| 521 | |
Yuta HIGUCHI | 7bfd607 | 2017-08-01 16:23:50 -0700 | [diff] [blame] | 522 | assertArrayEquals(Resources.toByteArray(Dhcp6RelayTest.class.getResource(REPLY)), |
Yi Tseng | ca34e1d | 2017-07-18 16:16:25 -0700 | [diff] [blame] | 523 | dhcp6.serialize()); |
| 524 | } |
| 525 | } |