blob: aac81412e2cfd74453a08a29917ffecf3575b268 [file] [log] [blame]
Jonathan Hart2a655752015-04-07 16:46:33 -07001/*
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
17package org.onlab.packet;
18
19import com.google.common.base.Charsets;
20import org.junit.Before;
21import org.junit.Test;
22
23import java.nio.ByteBuffer;
24import java.util.Arrays;
25
26import static org.junit.Assert.assertEquals;
27import static org.junit.Assert.assertTrue;
28
29/**
30 * Unit tests for DHCP class.
31 */
32public class DhcpTest {
33
34 private Deserializer<DHCP> deserializer = DHCP.deserializer();
35
36 private byte opCode = 1;
37 private byte hardwareType = 1;
38 private byte hardwareAddressLength = Ethernet.DATALAYER_ADDRESS_LENGTH;
39 private byte hops = 0;
40 private int transactionId = 0x2ed4eb50;
41 private short seconds = 0;
42 private short flags = 0;
43 private int clientIpAddress = 1;
44 private int yourIpAddress = 2;
45 private int serverIpAddress = 3;
46 private int gatewayIpAddress = 4;
47 private byte[] clientHardwareAddress = MacAddress.valueOf(500).toBytes();
48 private String serverName = "test-server";
49 private String bootFileName = "test-file";
50
51 private String hostName = "test-host";
52 private DHCPOption hostNameOption = new DHCPOption();
53
54 private byte[] byteHeader;
55
56 @Before
57 public void setUp() {
58 hostNameOption.setCode((byte) 55);
59 hostNameOption.setLength((byte) hostName.length());
60 hostNameOption.setData(hostName.getBytes(Charsets.US_ASCII));
61
62 // Packet length is the fixed DHCP header plus option length plus an
63 // extra byte to indicate 'end of options'.
64 ByteBuffer bb = ByteBuffer.allocate(DHCP.MIN_HEADER_LENGTH +
65 2 + hostNameOption.getLength() + 1);
66
67 bb.put(opCode);
68 bb.put(hardwareType);
69 bb.put(hardwareAddressLength);
70 bb.put(hops);
71 bb.putInt(transactionId);
72 bb.putShort(seconds);
73 bb.putShort(flags);
74 bb.putInt(clientIpAddress);
75 bb.putInt(yourIpAddress);
76 bb.putInt(serverIpAddress);
77 bb.putInt(gatewayIpAddress);
78 bb.put(clientHardwareAddress);
79
80 // need 16 bytes of zeros to pad out the client hardware address field
81 bb.put(new byte[16 - hardwareAddressLength]);
82
83 // Put server name and pad out to 64 bytes
84 bb.put(serverName.getBytes(Charsets.US_ASCII));
85 bb.put(new byte[64 - serverName.length()]);
86
87 // Put boot file name and pad out to 128 bytes
88 bb.put(bootFileName.getBytes(Charsets.US_ASCII));
89 bb.put(new byte[128 - bootFileName.length()]);
90
91 // Magic cookie
92 bb.put("DHCP".getBytes(Charsets.US_ASCII));
93
94 bb.put(hostNameOption.getCode());
95 bb.put(hostNameOption.getLength());
96 bb.put(hostNameOption.getData());
97
98 // End of options marker
99 bb.put((byte) (0xff & 255));
100
101 byteHeader = bb.array();
102 }
103
104 @Test
105 public void testDeserializeBadInput() throws Exception {
106 PacketTestUtils.testDeserializeBadInput(deserializer);
107 }
108
109 @Test
110 public void testDeserializeTruncated() throws Exception {
111 PacketTestUtils.testDeserializeTruncated(deserializer, byteHeader);
112 }
113
114 @Test
115 public void testDeserialize() throws Exception {
116 DHCP dhcp = deserializer.deserialize(byteHeader, 0, byteHeader.length);
117
118 assertEquals(opCode, dhcp.opCode);
119 assertEquals(hardwareType, dhcp.hardwareType);
120 assertEquals(hardwareAddressLength, dhcp.hardwareAddressLength);
121 assertEquals(hops, dhcp.hops);
122 assertEquals(transactionId, dhcp.transactionId);
123 assertEquals(seconds, dhcp.seconds);
124 assertEquals(flags, dhcp.flags);
125 assertEquals(clientIpAddress, dhcp.clientIPAddress);
126 assertEquals(yourIpAddress, dhcp.yourIPAddress);
127 assertEquals(serverIpAddress, dhcp.serverIPAddress);
128 assertEquals(gatewayIpAddress, dhcp.gatewayIPAddress);
129 assertTrue(Arrays.equals(clientHardwareAddress, dhcp.clientHardwareAddress));
130
131 assertEquals(serverName, dhcp.serverName);
132 assertEquals(bootFileName, dhcp.bootFileName);
alshabib0588e572015-09-04 12:00:42 -0700133 assertEquals(2, dhcp.options.size());
Jonathan Hart2a655752015-04-07 16:46:33 -0700134 assertEquals(hostNameOption, dhcp.options.get(0));
135 }
136
137}