blob: 927266d89cd74fd90bb75e98075479808542a609 [file] [log] [blame]
Yi Tseng60bf35a2017-06-02 17:05:48 -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
17package org.onlab.packet;
18
19import com.google.common.collect.ImmutableList;
20import org.junit.Test;
21
22import static org.junit.Assert.assertArrayEquals;
23import static org.junit.Assert.assertEquals;
24
25import java.nio.ByteBuffer;
26
27public class Dhcp6Test {
28 private static final int OPT_CLIENT_ID = 0xBEEFBEEF;
29 private static final byte[] OPT_CLIENT_ID_BYTE_ARR =
30 {(byte) 0xBE, (byte) 0xEF, (byte) 0xBE, (byte) 0xEF};
31 private static final short OPT_CLIENT_ID_SIZE = 4;
32 private static final int OPT_AUTH = 0xBA11BA11;
33 private static final byte[] OPT_AUTH_BYTE_AR =
34 {(byte) 0xBA, 0x11, (byte) 0xBA, 0x11};
35 private static final short OPT_AUTH_SIZE = 4;
36 private static final int TRANSACTION_ID = 0xC0FFEE;
37 private static final byte[] TRANSACTION_ID_BYTE_ARR =
38 {(byte) 0xC0, (byte) 0xFF, (byte) 0xEE};
39 private static final byte HOP_COUNT = 3;
40 private static final Ip6Address LINK_ADDRESS = Ip6Address.valueOf("1111:2222::8888");
41 private static final Ip6Address PEER_ADDRESS = Ip6Address.valueOf("3333:4444::9999");
42 Deserializer<DHCP6> deserializer = DHCP6.deserializer();
43 private byte[] byteHeader;
44
45 @Test
46 public void testDeserializeBadInput() throws Exception {
47 PacketTestUtils.testDeserializeBadInput(deserializer);
48 }
49
50 /**
51 * Truncated a simple DHCPv6 payload.
52 */
53 @Test
54 public void testDeserializeTruncated() throws Exception {
55 ByteBuffer bb = ByteBuffer.allocate(4);
56 bb.put(DHCP6.MsgType.REQUEST.value());
57 bb.put(TRANSACTION_ID_BYTE_ARR);
58 byteHeader = bb.array();
59
60 PacketTestUtils.testDeserializeTruncated(deserializer, byteHeader);
61 }
62
63 /**
64 * Basic DHCPv6 header with one msg type and one transaction id.
65 */
66 @Test
67 public void testDeserializeDefaultPayload() throws Exception {
68 ByteBuffer bb = ByteBuffer.allocate(12);
69 bb.put(DHCP6.MsgType.REQUEST.value());
70 bb.put(TRANSACTION_ID_BYTE_ARR);
71
72 // put a simple client id (4 bytes)
73 bb.putShort(DHCP6.OptionCode.CLIENTID.value());
74 bb.putShort(OPT_CLIENT_ID_SIZE);
75 bb.putInt(OPT_CLIENT_ID);
76 byteHeader = bb.array();
77
78 DHCP6 dhcp6 = deserializer.deserialize(byteHeader, 0, byteHeader.length);
79 assertEquals(dhcp6.getMsgType(), DHCP6.MsgType.REQUEST.value());
80 assertEquals(dhcp6.getTransactionId(), TRANSACTION_ID);
81 assertEquals(dhcp6.getOptions().size(), 1);
82
83 DHCP6Option clientIdOption = dhcp6.getOptions().get(0);
84 assertEquals(clientIdOption.getCode(), DHCP6.OptionCode.CLIENTID.value());
85 assertArrayEquals(clientIdOption.getData(), OPT_CLIENT_ID_BYTE_ARR);
86 }
87
88 /**
89 * DHCPv6 header with relay agent information.
90 */
91 @Test
92 public void testDeserializeRelayAgent() throws Exception {
93 ByteBuffer bb = ByteBuffer.allocate(42);
94 bb.put(DHCP6.MsgType.RELAY_FORW.value());
95 bb.put(HOP_COUNT);
96
97 bb.put(LINK_ADDRESS.toOctets());
98 bb.put(PEER_ADDRESS.toOctets());
99
100 // put a simple client id (4 bytes)
101 bb.putShort(DHCP6.OptionCode.CLIENTID.value());
102 bb.putShort(OPT_CLIENT_ID_SIZE);
103 bb.putInt(OPT_CLIENT_ID);
104 byteHeader = bb.array();
105
106 DHCP6 dhcp6 = deserializer.deserialize(byteHeader, 0, byteHeader.length);
107 assertEquals(dhcp6.getMsgType(), DHCP6.MsgType.RELAY_FORW.value());
108 assertEquals(dhcp6.getHopCount(), HOP_COUNT);
109 assertArrayEquals(dhcp6.getLinkAddress(), LINK_ADDRESS.toOctets());
110 assertArrayEquals(dhcp6.getPeerAddress(), PEER_ADDRESS.toOctets());
111 assertEquals(dhcp6.getOptions().size(), 1);
112
113 DHCP6Option clientIdOption = dhcp6.getOptions().get(0);
114 assertEquals(clientIdOption.getCode(), DHCP6.OptionCode.CLIENTID.value());
115 assertArrayEquals(clientIdOption.getData(), OPT_CLIENT_ID_BYTE_ARR);
116 }
117
118 /**
119 * Serialize DHCPv6 header with default payload and options.
120 */
121 @Test
122 public void testSerializeDefaultPayload() throws Exception {
123 DHCP6 dhcp6 = new DHCP6();
124 dhcp6.setMsgType(DHCP6.MsgType.REQUEST.value());
125 dhcp6.setTransactionId(TRANSACTION_ID);
126
127 DHCP6Option opt1 = new DHCP6Option();
128 opt1.setCode(DHCP6.OptionCode.CLIENTID.value());
129 opt1.setLength(OPT_CLIENT_ID_SIZE);
130 opt1.setData(OPT_CLIENT_ID_BYTE_ARR);
131
132
133 DHCP6Option opt2 = new DHCP6Option();
134 opt2.setCode(DHCP6.OptionCode.AUTH.value());
135 opt2.setLength(OPT_AUTH_SIZE);
136 opt2.setData(OPT_AUTH_BYTE_AR);
137
138 dhcp6.setOptions(ImmutableList.of(opt1, opt2));
139
140 byte[] serialized = dhcp6.serialize();
141 ByteBuffer expected = ByteBuffer.allocate(20)
142 .put(DHCP6.MsgType.REQUEST.value())
143 .put(TRANSACTION_ID_BYTE_ARR)
144 .putShort(DHCP6.OptionCode.CLIENTID.value())
145 .putShort(OPT_CLIENT_ID_SIZE)
146 .putInt(OPT_CLIENT_ID)
147 .putShort(DHCP6.OptionCode.AUTH.value())
148 .putShort(OPT_AUTH_SIZE)
149 .putInt(OPT_AUTH);
150
151 assertArrayEquals(serialized, expected.array());
152 }
153
154 /**
155 * Serialize DHCPv6 header with relay agent payload and options.
156 */
157 @Test
158 public void testSerializeRelayAgent() throws Exception {
159 DHCP6 dhcp6 = new DHCP6();
160 dhcp6.setMsgType(DHCP6.MsgType.RELAY_FORW.value());
161 dhcp6.setHopCount(HOP_COUNT);
162 dhcp6.setLinkAddress(LINK_ADDRESS.toOctets());
163 dhcp6.setPeerAddress(PEER_ADDRESS.toOctets());
164
165 DHCP6Option opt1 = new DHCP6Option();
166 opt1.setCode(DHCP6.OptionCode.CLIENTID.value());
167 opt1.setLength(OPT_CLIENT_ID_SIZE);
168 opt1.setData(OPT_CLIENT_ID_BYTE_ARR);
169
170
171 DHCP6Option opt2 = new DHCP6Option();
172 opt2.setCode(DHCP6.OptionCode.AUTH.value());
173 opt2.setLength(OPT_AUTH_SIZE);
174 opt2.setData(OPT_AUTH_BYTE_AR);
175
176 dhcp6.setOptions(ImmutableList.of(opt1, opt2));
177
178 byte[] serialized = dhcp6.serialize();
179 ByteBuffer expected = ByteBuffer.allocate(50)
180 .put(DHCP6.MsgType.RELAY_FORW.value())
181 .put(HOP_COUNT)
182 .put(LINK_ADDRESS.toOctets())
183 .put(PEER_ADDRESS.toOctets())
184 .putShort(DHCP6.OptionCode.CLIENTID.value())
185 .putShort(OPT_CLIENT_ID_SIZE)
186 .putInt(OPT_CLIENT_ID)
187 .putShort(DHCP6.OptionCode.AUTH.value())
188 .putShort(OPT_AUTH_SIZE)
189 .putInt(OPT_AUTH);
190
191 assertArrayEquals(serialized, expected.array());
192 }
193}