Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -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; |
| 18 | |
| 19 | import com.google.common.base.Charsets; |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 20 | import org.apache.commons.lang3.StringUtils; |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 21 | import org.junit.Before; |
| 22 | import org.junit.Test; |
Yi Tseng | c7403c2 | 2017-06-19 16:23:22 -0700 | [diff] [blame] | 23 | import org.onlab.packet.dhcp.DhcpOption; |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 24 | |
| 25 | import java.nio.ByteBuffer; |
Yi Tseng | c7403c2 | 2017-06-19 16:23:22 -0700 | [diff] [blame] | 26 | import java.util.ArrayList; |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 27 | import java.util.Arrays; |
Yi Tseng | c7403c2 | 2017-06-19 16:23:22 -0700 | [diff] [blame] | 28 | import java.util.List; |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 29 | |
Yi Tseng | c7403c2 | 2017-06-19 16:23:22 -0700 | [diff] [blame] | 30 | import static org.junit.Assert.assertArrayEquals; |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 31 | import static org.junit.Assert.assertEquals; |
| 32 | import static org.junit.Assert.assertTrue; |
| 33 | |
| 34 | /** |
| 35 | * Unit tests for DHCP class. |
| 36 | */ |
| 37 | public class DhcpTest { |
| 38 | |
Yi Tseng | c7403c2 | 2017-06-19 16:23:22 -0700 | [diff] [blame] | 39 | // For serialize test |
| 40 | private static final int TRANSACTION_ID = 1000; |
| 41 | private static final MacAddress CLIENT1_HOST_MAC = MacAddress.valueOf("1a:1a:1a:1a:1a:1a"); |
| 42 | private static final Ip4Address REQ_IP = Ip4Address.valueOf("10.2.0.2"); |
| 43 | private static final byte[] EXPECTED_SERIALIZED = ByteBuffer.allocate(300) |
| 44 | .put((byte) 0x01) // op code |
| 45 | .put((byte) 0x01) // hardware type |
| 46 | .put((byte) 0x06) // hardware address len |
| 47 | .put((byte) 0x00) // hops |
| 48 | .putInt(0x3e8) // transaction id |
| 49 | .putShort((short) 0x0) // seconds |
| 50 | .putShort((short) 0x0) // flags |
| 51 | .putInt(0) // client ip |
| 52 | .putInt(0) // your ip |
| 53 | .putInt(0) // server ip |
| 54 | .putInt(0) // gateway ip |
| 55 | .put(CLIENT1_HOST_MAC.toBytes()) // client hardware address |
| 56 | .put(new byte[10]) // pad |
| 57 | .put(new byte[64]) // server name |
| 58 | .put(new byte[128]) // boot file name |
| 59 | .putInt(0x63825363) // magic cookie |
| 60 | .put(new byte[]{0x35, 0x1, 0x3}) // msg type |
| 61 | .put(new byte[]{0x32, 0x4, 0xa, 0x2, 0x0, 0x2}) // requested ip |
| 62 | .put((byte) 0xff) // end of options |
| 63 | .put(new byte[50]) // pad |
| 64 | .array(); |
| 65 | |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 66 | private Deserializer<DHCP> deserializer = DHCP.deserializer(); |
| 67 | |
| 68 | private byte opCode = 1; |
| 69 | private byte hardwareType = 1; |
| 70 | private byte hardwareAddressLength = Ethernet.DATALAYER_ADDRESS_LENGTH; |
| 71 | private byte hops = 0; |
| 72 | private int transactionId = 0x2ed4eb50; |
| 73 | private short seconds = 0; |
| 74 | private short flags = 0; |
| 75 | private int clientIpAddress = 1; |
| 76 | private int yourIpAddress = 2; |
| 77 | private int serverIpAddress = 3; |
| 78 | private int gatewayIpAddress = 4; |
| 79 | private byte[] clientHardwareAddress = MacAddress.valueOf(500).toBytes(); |
| 80 | private String serverName = "test-server"; |
| 81 | private String bootFileName = "test-file"; |
| 82 | |
| 83 | private String hostName = "test-host"; |
Yi Tseng | c7403c2 | 2017-06-19 16:23:22 -0700 | [diff] [blame] | 84 | private DhcpOption hostNameOption = new DhcpOption(); |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 85 | |
| 86 | private byte[] byteHeader; |
| 87 | |
| 88 | @Before |
| 89 | public void setUp() { |
| 90 | hostNameOption.setCode((byte) 55); |
| 91 | hostNameOption.setLength((byte) hostName.length()); |
| 92 | hostNameOption.setData(hostName.getBytes(Charsets.US_ASCII)); |
| 93 | |
| 94 | // Packet length is the fixed DHCP header plus option length plus an |
| 95 | // extra byte to indicate 'end of options'. |
| 96 | ByteBuffer bb = ByteBuffer.allocate(DHCP.MIN_HEADER_LENGTH + |
| 97 | 2 + hostNameOption.getLength() + 1); |
| 98 | |
| 99 | bb.put(opCode); |
| 100 | bb.put(hardwareType); |
| 101 | bb.put(hardwareAddressLength); |
| 102 | bb.put(hops); |
| 103 | bb.putInt(transactionId); |
| 104 | bb.putShort(seconds); |
| 105 | bb.putShort(flags); |
| 106 | bb.putInt(clientIpAddress); |
| 107 | bb.putInt(yourIpAddress); |
| 108 | bb.putInt(serverIpAddress); |
| 109 | bb.putInt(gatewayIpAddress); |
| 110 | bb.put(clientHardwareAddress); |
| 111 | |
| 112 | // need 16 bytes of zeros to pad out the client hardware address field |
| 113 | bb.put(new byte[16 - hardwareAddressLength]); |
| 114 | |
| 115 | // Put server name and pad out to 64 bytes |
| 116 | bb.put(serverName.getBytes(Charsets.US_ASCII)); |
| 117 | bb.put(new byte[64 - serverName.length()]); |
| 118 | |
| 119 | // Put boot file name and pad out to 128 bytes |
| 120 | bb.put(bootFileName.getBytes(Charsets.US_ASCII)); |
| 121 | bb.put(new byte[128 - bootFileName.length()]); |
| 122 | |
| 123 | // Magic cookie |
| 124 | bb.put("DHCP".getBytes(Charsets.US_ASCII)); |
| 125 | |
| 126 | bb.put(hostNameOption.getCode()); |
| 127 | bb.put(hostNameOption.getLength()); |
| 128 | bb.put(hostNameOption.getData()); |
| 129 | |
| 130 | // End of options marker |
Yi Tseng | c7403c2 | 2017-06-19 16:23:22 -0700 | [diff] [blame] | 131 | bb.put((DHCP.DHCPOptionCode.OptionCode_END.getValue())); |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 132 | |
| 133 | byteHeader = bb.array(); |
| 134 | } |
| 135 | |
| 136 | @Test |
| 137 | public void testDeserializeBadInput() throws Exception { |
| 138 | PacketTestUtils.testDeserializeBadInput(deserializer); |
| 139 | } |
| 140 | |
| 141 | @Test |
| 142 | public void testDeserializeTruncated() throws Exception { |
| 143 | PacketTestUtils.testDeserializeTruncated(deserializer, byteHeader); |
| 144 | } |
| 145 | |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 146 | /** |
| 147 | * Tests deserialize and getters. |
| 148 | */ |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 149 | @Test |
| 150 | public void testDeserialize() throws Exception { |
| 151 | DHCP dhcp = deserializer.deserialize(byteHeader, 0, byteHeader.length); |
| 152 | |
| 153 | assertEquals(opCode, dhcp.opCode); |
| 154 | assertEquals(hardwareType, dhcp.hardwareType); |
| 155 | assertEquals(hardwareAddressLength, dhcp.hardwareAddressLength); |
| 156 | assertEquals(hops, dhcp.hops); |
| 157 | assertEquals(transactionId, dhcp.transactionId); |
| 158 | assertEquals(seconds, dhcp.seconds); |
| 159 | assertEquals(flags, dhcp.flags); |
| 160 | assertEquals(clientIpAddress, dhcp.clientIPAddress); |
| 161 | assertEquals(yourIpAddress, dhcp.yourIPAddress); |
| 162 | assertEquals(serverIpAddress, dhcp.serverIPAddress); |
| 163 | assertEquals(gatewayIpAddress, dhcp.gatewayIPAddress); |
| 164 | assertTrue(Arrays.equals(clientHardwareAddress, dhcp.clientHardwareAddress)); |
| 165 | |
| 166 | assertEquals(serverName, dhcp.serverName); |
| 167 | assertEquals(bootFileName, dhcp.bootFileName); |
alshabib | 0588e57 | 2015-09-04 12:00:42 -0700 | [diff] [blame] | 168 | assertEquals(2, dhcp.options.size()); |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 169 | assertEquals(hostNameOption, dhcp.options.get(0)); |
| 170 | } |
| 171 | |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 172 | /** |
| 173 | * Tests toString. |
| 174 | */ |
| 175 | @Test |
| 176 | public void testToStringDhcp() throws Exception { |
| 177 | DHCP dhcp = deserializer.deserialize(byteHeader, 0, byteHeader.length); |
| 178 | String str = dhcp.toString(); |
| 179 | |
| 180 | assertTrue(StringUtils.contains(str, "opCode=" + opCode)); |
| 181 | assertTrue(StringUtils.contains(str, "hardwareType=" + hardwareType)); |
| 182 | assertTrue(StringUtils.contains(str, "hardwareAddressLength=" + hardwareAddressLength)); |
| 183 | assertTrue(StringUtils.contains(str, "hops=" + hops)); |
| 184 | assertTrue(StringUtils.contains(str, "transactionId=" + transactionId)); |
| 185 | assertTrue(StringUtils.contains(str, "seconds=" + seconds)); |
| 186 | assertTrue(StringUtils.contains(str, "flags=" + flags)); |
| 187 | assertTrue(StringUtils.contains(str, "clientIPAddress=" + clientIpAddress)); |
| 188 | assertTrue(StringUtils.contains(str, "yourIPAddress=" + yourIpAddress)); |
| 189 | assertTrue(StringUtils.contains(str, "serverIPAddress=" + serverIpAddress)); |
| 190 | assertTrue(StringUtils.contains(str, "gatewayIPAddress=" + gatewayIpAddress)); |
| 191 | assertTrue(StringUtils.contains(str, "clientHardwareAddress=" + Arrays.toString(clientHardwareAddress))); |
| 192 | assertTrue(StringUtils.contains(str, "serverName=" + serverName)); |
| 193 | assertTrue(StringUtils.contains(str, "bootFileName=" + bootFileName)); |
| 194 | // TODO: add option unit test |
| 195 | } |
Yi Tseng | c7403c2 | 2017-06-19 16:23:22 -0700 | [diff] [blame] | 196 | |
| 197 | |
| 198 | |
| 199 | @Test |
| 200 | public void testSerialize() throws Exception { |
| 201 | DHCP dhcpReply = new DHCP(); |
| 202 | dhcpReply.setOpCode(DHCP.OPCODE_REQUEST); |
| 203 | |
| 204 | dhcpReply.setYourIPAddress(0); |
| 205 | dhcpReply.setServerIPAddress(0); |
| 206 | |
| 207 | dhcpReply.setTransactionId(TRANSACTION_ID); |
| 208 | dhcpReply.setClientHardwareAddress(CLIENT1_HOST_MAC.toBytes()); |
| 209 | dhcpReply.setHardwareType(DHCP.HWTYPE_ETHERNET); |
| 210 | dhcpReply.setHardwareAddressLength((byte) 6); |
| 211 | |
| 212 | // DHCP Options. |
| 213 | DhcpOption option = new DhcpOption(); |
| 214 | List<DhcpOption> optionList = new ArrayList<>(); |
| 215 | |
| 216 | // DHCP Message Type. |
| 217 | option.setCode(DHCP.DHCPOptionCode.OptionCode_MessageType.getValue()); |
| 218 | option.setLength((byte) 1); |
| 219 | byte[] optionData = {(byte) DHCP.MsgType.DHCPREQUEST.getValue()}; |
| 220 | option.setData(optionData); |
| 221 | optionList.add(option); |
| 222 | |
| 223 | // DHCP Requested IP. |
| 224 | option = new DhcpOption(); |
| 225 | option.setCode(DHCP.DHCPOptionCode.OptionCode_RequestedIP.getValue()); |
| 226 | option.setLength((byte) 4); |
| 227 | optionData = REQ_IP.toOctets(); |
| 228 | option.setData(optionData); |
| 229 | optionList.add(option); |
| 230 | |
| 231 | // End Option. |
| 232 | option = new DhcpOption(); |
| 233 | option.setCode(DHCP.DHCPOptionCode.OptionCode_END.getValue()); |
| 234 | option.setLength((byte) 1); |
| 235 | optionList.add(option); |
| 236 | |
| 237 | dhcpReply.setOptions(optionList); |
| 238 | |
| 239 | assertArrayEquals(EXPECTED_SERIALIZED, dhcpReply.serialize()); |
| 240 | } |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 241 | } |