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