Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | 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; |
| 23 | |
| 24 | import java.nio.ByteBuffer; |
| 25 | import java.util.Arrays; |
| 26 | |
| 27 | import static org.junit.Assert.assertEquals; |
| 28 | import static org.junit.Assert.assertTrue; |
| 29 | |
| 30 | /** |
| 31 | * Unit tests for DHCP class. |
| 32 | */ |
| 33 | public class DhcpTest { |
| 34 | |
| 35 | private Deserializer<DHCP> deserializer = DHCP.deserializer(); |
| 36 | |
| 37 | private byte opCode = 1; |
| 38 | private byte hardwareType = 1; |
| 39 | private byte hardwareAddressLength = Ethernet.DATALAYER_ADDRESS_LENGTH; |
| 40 | private byte hops = 0; |
| 41 | private int transactionId = 0x2ed4eb50; |
| 42 | private short seconds = 0; |
| 43 | private short flags = 0; |
| 44 | private int clientIpAddress = 1; |
| 45 | private int yourIpAddress = 2; |
| 46 | private int serverIpAddress = 3; |
| 47 | private int gatewayIpAddress = 4; |
| 48 | private byte[] clientHardwareAddress = MacAddress.valueOf(500).toBytes(); |
| 49 | private String serverName = "test-server"; |
| 50 | private String bootFileName = "test-file"; |
| 51 | |
| 52 | private String hostName = "test-host"; |
| 53 | private DHCPOption hostNameOption = new DHCPOption(); |
| 54 | |
| 55 | private byte[] byteHeader; |
| 56 | |
| 57 | @Before |
| 58 | public void setUp() { |
| 59 | hostNameOption.setCode((byte) 55); |
| 60 | hostNameOption.setLength((byte) hostName.length()); |
| 61 | hostNameOption.setData(hostName.getBytes(Charsets.US_ASCII)); |
| 62 | |
| 63 | // Packet length is the fixed DHCP header plus option length plus an |
| 64 | // extra byte to indicate 'end of options'. |
| 65 | ByteBuffer bb = ByteBuffer.allocate(DHCP.MIN_HEADER_LENGTH + |
| 66 | 2 + hostNameOption.getLength() + 1); |
| 67 | |
| 68 | bb.put(opCode); |
| 69 | bb.put(hardwareType); |
| 70 | bb.put(hardwareAddressLength); |
| 71 | bb.put(hops); |
| 72 | bb.putInt(transactionId); |
| 73 | bb.putShort(seconds); |
| 74 | bb.putShort(flags); |
| 75 | bb.putInt(clientIpAddress); |
| 76 | bb.putInt(yourIpAddress); |
| 77 | bb.putInt(serverIpAddress); |
| 78 | bb.putInt(gatewayIpAddress); |
| 79 | bb.put(clientHardwareAddress); |
| 80 | |
| 81 | // need 16 bytes of zeros to pad out the client hardware address field |
| 82 | bb.put(new byte[16 - hardwareAddressLength]); |
| 83 | |
| 84 | // Put server name and pad out to 64 bytes |
| 85 | bb.put(serverName.getBytes(Charsets.US_ASCII)); |
| 86 | bb.put(new byte[64 - serverName.length()]); |
| 87 | |
| 88 | // Put boot file name and pad out to 128 bytes |
| 89 | bb.put(bootFileName.getBytes(Charsets.US_ASCII)); |
| 90 | bb.put(new byte[128 - bootFileName.length()]); |
| 91 | |
| 92 | // Magic cookie |
| 93 | bb.put("DHCP".getBytes(Charsets.US_ASCII)); |
| 94 | |
| 95 | bb.put(hostNameOption.getCode()); |
| 96 | bb.put(hostNameOption.getLength()); |
| 97 | bb.put(hostNameOption.getData()); |
| 98 | |
| 99 | // End of options marker |
| 100 | bb.put((byte) (0xff & 255)); |
| 101 | |
| 102 | byteHeader = bb.array(); |
| 103 | } |
| 104 | |
| 105 | @Test |
| 106 | public void testDeserializeBadInput() throws Exception { |
| 107 | PacketTestUtils.testDeserializeBadInput(deserializer); |
| 108 | } |
| 109 | |
| 110 | @Test |
| 111 | public void testDeserializeTruncated() throws Exception { |
| 112 | PacketTestUtils.testDeserializeTruncated(deserializer, byteHeader); |
| 113 | } |
| 114 | |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 115 | /** |
| 116 | * Tests deserialize and getters. |
| 117 | */ |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 118 | @Test |
| 119 | public void testDeserialize() throws Exception { |
| 120 | DHCP dhcp = deserializer.deserialize(byteHeader, 0, byteHeader.length); |
| 121 | |
| 122 | assertEquals(opCode, dhcp.opCode); |
| 123 | assertEquals(hardwareType, dhcp.hardwareType); |
| 124 | assertEquals(hardwareAddressLength, dhcp.hardwareAddressLength); |
| 125 | assertEquals(hops, dhcp.hops); |
| 126 | assertEquals(transactionId, dhcp.transactionId); |
| 127 | assertEquals(seconds, dhcp.seconds); |
| 128 | assertEquals(flags, dhcp.flags); |
| 129 | assertEquals(clientIpAddress, dhcp.clientIPAddress); |
| 130 | assertEquals(yourIpAddress, dhcp.yourIPAddress); |
| 131 | assertEquals(serverIpAddress, dhcp.serverIPAddress); |
| 132 | assertEquals(gatewayIpAddress, dhcp.gatewayIPAddress); |
| 133 | assertTrue(Arrays.equals(clientHardwareAddress, dhcp.clientHardwareAddress)); |
| 134 | |
| 135 | assertEquals(serverName, dhcp.serverName); |
| 136 | assertEquals(bootFileName, dhcp.bootFileName); |
alshabib | 0588e57 | 2015-09-04 12:00:42 -0700 | [diff] [blame] | 137 | assertEquals(2, dhcp.options.size()); |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 138 | assertEquals(hostNameOption, dhcp.options.get(0)); |
| 139 | } |
| 140 | |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 141 | /** |
| 142 | * Tests toString. |
| 143 | */ |
| 144 | @Test |
| 145 | public void testToStringDhcp() throws Exception { |
| 146 | DHCP dhcp = deserializer.deserialize(byteHeader, 0, byteHeader.length); |
| 147 | String str = dhcp.toString(); |
| 148 | |
| 149 | assertTrue(StringUtils.contains(str, "opCode=" + opCode)); |
| 150 | assertTrue(StringUtils.contains(str, "hardwareType=" + hardwareType)); |
| 151 | assertTrue(StringUtils.contains(str, "hardwareAddressLength=" + hardwareAddressLength)); |
| 152 | assertTrue(StringUtils.contains(str, "hops=" + hops)); |
| 153 | assertTrue(StringUtils.contains(str, "transactionId=" + transactionId)); |
| 154 | assertTrue(StringUtils.contains(str, "seconds=" + seconds)); |
| 155 | assertTrue(StringUtils.contains(str, "flags=" + flags)); |
| 156 | assertTrue(StringUtils.contains(str, "clientIPAddress=" + clientIpAddress)); |
| 157 | assertTrue(StringUtils.contains(str, "yourIPAddress=" + yourIpAddress)); |
| 158 | assertTrue(StringUtils.contains(str, "serverIPAddress=" + serverIpAddress)); |
| 159 | assertTrue(StringUtils.contains(str, "gatewayIPAddress=" + gatewayIpAddress)); |
| 160 | assertTrue(StringUtils.contains(str, "clientHardwareAddress=" + Arrays.toString(clientHardwareAddress))); |
| 161 | assertTrue(StringUtils.contains(str, "serverName=" + serverName)); |
| 162 | assertTrue(StringUtils.contains(str, "bootFileName=" + bootFileName)); |
| 163 | // TODO: add option unit test |
| 164 | } |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 165 | } |