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 | |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 19 | import org.apache.commons.lang3.StringUtils; |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 20 | import org.junit.Before; |
| 21 | import org.junit.Test; |
| 22 | |
| 23 | import java.nio.ByteBuffer; |
| 24 | import java.util.Arrays; |
| 25 | |
| 26 | import static org.junit.Assert.assertEquals; |
| 27 | import static org.junit.Assert.assertTrue; |
| 28 | |
| 29 | /** |
| 30 | * Unit tests for the ARP class. |
| 31 | */ |
| 32 | public 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 Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 49 | 2 * hwAddressLength + 2 * protoAddressLength); |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 50 | 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 Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 74 | /** |
| 75 | * Tests deserialize and getters. |
| 76 | */ |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 77 | @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 Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 92 | |
| 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)); |
| 102 | assertTrue(StringUtils.contains(str, "senderHardwareAddress=" + Arrays.toString(srcMac.toBytes()))); |
| 103 | assertTrue(StringUtils.contains(str, "senderProtocolAddress=" + Arrays.toString(srcIp.toOctets()))); |
| 104 | assertTrue(StringUtils.contains(str, "targetHardwareAddress=" + Arrays.toString(targetMac.toBytes()))); |
| 105 | assertTrue(StringUtils.contains(str, "targetProtocolAddress=" + Arrays.toString(targetIp.toOctets()))); |
| 106 | } |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 107 | } |