blob: e67424e7a7b2f9854aedaba13c44827aab6ed8e1 [file] [log] [blame]
Jonathan Hart2a655752015-04-07 16:46:33 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Jonathan Hart2a655752015-04-07 16:46:33 -07003 *
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
Jian Li5fc14292015-12-04 11:30:46 -080019import org.apache.commons.lang3.StringUtils;
Jonathan Hart2a655752015-04-07 16:46:33 -070020import 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 the ARP class.
31 */
32public class ArpTest {
33
34 private Deserializer<ARP> deserializer = ARP.deserializer();
35
36 private final byte hwAddressLength = 6;
37 private final byte protoAddressLength = 4;
38
39 private MacAddress srcMac = MacAddress.valueOf(1);
40 private MacAddress targetMac = MacAddress.valueOf(2);
41 private Ip4Address srcIp = Ip4Address.valueOf(1);
42 private Ip4Address targetIp = Ip4Address.valueOf(2);
43
44 private byte[] byteHeader;
45
46 @Before
47 public void setUp() {
48 ByteBuffer bb = ByteBuffer.allocate(ARP.INITIAL_HEADER_LENGTH +
Jian Li5fc14292015-12-04 11:30:46 -080049 2 * hwAddressLength + 2 * protoAddressLength);
Jonathan Hart2a655752015-04-07 16:46:33 -070050 bb.putShort(ARP.HW_TYPE_ETHERNET);
51 bb.putShort(ARP.PROTO_TYPE_IP);
52 bb.put(hwAddressLength);
53 bb.put(protoAddressLength);
54 bb.putShort(ARP.OP_REPLY);
55
56 bb.put(srcMac.toBytes());
57 bb.put(srcIp.toOctets());
58 bb.put(targetMac.toBytes());
59 bb.put(targetIp.toOctets());
60
61 byteHeader = bb.array();
62 }
63
64 @Test
65 public void testDeserializeBadInput() throws Exception {
66 PacketTestUtils.testDeserializeBadInput(deserializer);
67 }
68
69 @Test
70 public void testDeserializeTruncated() throws Exception {
71 PacketTestUtils.testDeserializeTruncated(deserializer, byteHeader);
72 }
73
Jian Li5fc14292015-12-04 11:30:46 -080074 /**
75 * Tests deserialize and getters.
76 */
Jonathan Hart2a655752015-04-07 16:46:33 -070077 @Test
78 public void testDeserialize() throws Exception {
79 ARP arp = deserializer.deserialize(byteHeader, 0, byteHeader.length);
80
81 assertEquals(ARP.HW_TYPE_ETHERNET, arp.getHardwareType());
82 assertEquals(ARP.PROTO_TYPE_IP, arp.getProtocolType());
83 assertEquals(hwAddressLength, arp.getHardwareAddressLength());
84 assertEquals(protoAddressLength, arp.getProtocolAddressLength());
85 assertEquals(ARP.OP_REPLY, arp.getOpCode());
86
87 assertTrue(Arrays.equals(srcMac.toBytes(), arp.getSenderHardwareAddress()));
88 assertTrue(Arrays.equals(srcIp.toOctets(), arp.getSenderProtocolAddress()));
89 assertTrue(Arrays.equals(targetMac.toBytes(), arp.getTargetHardwareAddress()));
90 assertTrue(Arrays.equals(targetIp.toOctets(), arp.getTargetProtocolAddress()));
91 }
Jian Li5fc14292015-12-04 11:30:46 -080092
93 /**
94 * Tests toString.
95 */
96 @Test
97 public void testToStringArp() throws Exception {
98 ARP arp = deserializer.deserialize(byteHeader, 0, byteHeader.length);
99 String str = arp.toString();
100 assertTrue(StringUtils.contains(str, "hardwareAddressLength=" + hwAddressLength));
101 assertTrue(StringUtils.contains(str, "protocolAddressLength=" + protoAddressLength));
Charles Chand2edd472016-10-17 18:03:37 -0700102 assertTrue(StringUtils.contains(str, "senderHardwareAddress=" + srcMac));
103 assertTrue(StringUtils.contains(str, "senderProtocolAddress=" + srcIp));
104 assertTrue(StringUtils.contains(str, "targetHardwareAddress=" + targetMac));
105 assertTrue(StringUtils.contains(str, "targetProtocolAddress=" + targetIp));
Jian Li5fc14292015-12-04 11:30:46 -0800106 }
Jonathan Hart2a655752015-04-07 16:46:33 -0700107}