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 org.junit.Before; |
| 20 | import org.junit.Test; |
| 21 | |
| 22 | import java.nio.ByteBuffer; |
| 23 | import java.util.Arrays; |
| 24 | |
| 25 | import static org.junit.Assert.assertEquals; |
| 26 | import static org.junit.Assert.assertTrue; |
| 27 | |
| 28 | /** |
| 29 | * Unit tests for the ARP class. |
| 30 | */ |
| 31 | public class ArpTest { |
| 32 | |
| 33 | private Deserializer<ARP> deserializer = ARP.deserializer(); |
| 34 | |
| 35 | private final byte hwAddressLength = 6; |
| 36 | private final byte protoAddressLength = 4; |
| 37 | |
| 38 | private MacAddress srcMac = MacAddress.valueOf(1); |
| 39 | private MacAddress targetMac = MacAddress.valueOf(2); |
| 40 | private Ip4Address srcIp = Ip4Address.valueOf(1); |
| 41 | private Ip4Address targetIp = Ip4Address.valueOf(2); |
| 42 | |
| 43 | private byte[] byteHeader; |
| 44 | |
| 45 | @Before |
| 46 | public void setUp() { |
| 47 | ByteBuffer bb = ByteBuffer.allocate(ARP.INITIAL_HEADER_LENGTH + |
| 48 | 2 * hwAddressLength + 2 * protoAddressLength); |
| 49 | bb.putShort(ARP.HW_TYPE_ETHERNET); |
| 50 | bb.putShort(ARP.PROTO_TYPE_IP); |
| 51 | bb.put(hwAddressLength); |
| 52 | bb.put(protoAddressLength); |
| 53 | bb.putShort(ARP.OP_REPLY); |
| 54 | |
| 55 | bb.put(srcMac.toBytes()); |
| 56 | bb.put(srcIp.toOctets()); |
| 57 | bb.put(targetMac.toBytes()); |
| 58 | bb.put(targetIp.toOctets()); |
| 59 | |
| 60 | byteHeader = bb.array(); |
| 61 | } |
| 62 | |
| 63 | @Test |
| 64 | public void testDeserializeBadInput() throws Exception { |
| 65 | PacketTestUtils.testDeserializeBadInput(deserializer); |
| 66 | } |
| 67 | |
| 68 | @Test |
| 69 | public void testDeserializeTruncated() throws Exception { |
| 70 | PacketTestUtils.testDeserializeTruncated(deserializer, byteHeader); |
| 71 | } |
| 72 | |
| 73 | @Test |
| 74 | public void testDeserialize() throws Exception { |
| 75 | ARP arp = deserializer.deserialize(byteHeader, 0, byteHeader.length); |
| 76 | |
| 77 | assertEquals(ARP.HW_TYPE_ETHERNET, arp.getHardwareType()); |
| 78 | assertEquals(ARP.PROTO_TYPE_IP, arp.getProtocolType()); |
| 79 | assertEquals(hwAddressLength, arp.getHardwareAddressLength()); |
| 80 | assertEquals(protoAddressLength, arp.getProtocolAddressLength()); |
| 81 | assertEquals(ARP.OP_REPLY, arp.getOpCode()); |
| 82 | |
| 83 | assertTrue(Arrays.equals(srcMac.toBytes(), arp.getSenderHardwareAddress())); |
| 84 | assertTrue(Arrays.equals(srcIp.toOctets(), arp.getSenderProtocolAddress())); |
| 85 | assertTrue(Arrays.equals(targetMac.toBytes(), arp.getTargetHardwareAddress())); |
| 86 | assertTrue(Arrays.equals(targetIp.toOctets(), arp.getTargetProtocolAddress())); |
| 87 | } |
| 88 | } |