2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.arp;
11 import java.net.InetAddress;
12 import java.net.UnknownHostException;
13 import java.util.Arrays;
15 import org.junit.Assert;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.opendaylight.controller.liblldp.EtherTypes;
19 import org.opendaylight.controller.liblldp.HexEncode;
20 import org.opendaylight.controller.liblldp.Packet;
21 import org.opendaylight.controller.liblldp.PacketException;
23 import com.google.common.primitives.Ints;
25 public class ArpTest {
35 private int operation;
39 public void init() throws UnknownHostException {
40 sha = HexEncode.bytesFromHexString("00:00:00:00:00:01");
41 spa = InetAddress.getByName("192.168.0.1").getAddress();
42 tha = HexEncode.bytesFromHexString("00:00:00:00:00:02");
43 tpa = InetAddress.getByName("192.168.0.2").getAddress();
45 ptype = EtherTypes.IPv6.intValue();
52 arp.setSenderHardwareAddress(sha);
53 arp.setSenderProtocolAddress(spa);
54 arp.setTargetHardwareAddress(tha);
55 arp.setTargetProtocolAddress(tpa);
56 arp.setOperation(operation);
57 arp.setHardwareLength(hlen);
58 arp.setProtocolLength(plen);
59 arp.setHardwareType(htype);
60 arp.setProtocolType(ptype);
64 public void ArpConstructionTest() throws Exception {
66 Assert.assertArrayEquals(sha, arp.getSenderHardwareAddress());
67 Assert.assertArrayEquals(tha, arp.getTargetHardwareAddress());
68 Assert.assertArrayEquals(spa, arp.getSenderProtocolAddress());
69 Assert.assertArrayEquals(tpa, arp.getTargetProtocolAddress());
70 Assert.assertEquals(operation, arp.getOperation());
71 Assert.assertEquals(hlen, arp.getHardwareLength());
72 Assert.assertEquals(plen, arp.getProtocolLength());
73 Assert.assertEquals(htype, arp.getHardwareType());
74 Assert.assertEquals(ptype, arp.getProtocolType());
78 public void serializeTest() throws PacketException {
79 byte[] output = arp.serialize();
80 Assert.assertEquals(htype, Ints.fromBytes((byte) 0, (byte) 0, output[0], output[1]));
81 Assert.assertEquals(ptype, Ints.fromBytes((byte) 0, (byte) 0, output[2], output[3]));
82 Assert.assertEquals(hlen, output[4]);
83 Assert.assertEquals(plen, output[5]);
84 Assert.assertEquals(operation, Ints.fromBytes((byte) 0, (byte) 0, output[6], output[7]));
85 Assert.assertArrayEquals(sha, Arrays.copyOfRange(output, 8, 14));
86 Assert.assertArrayEquals(spa, Arrays.copyOfRange(output, 14, 18));
87 Assert.assertArrayEquals(tha, Arrays.copyOfRange(output, 18, 24));
88 Assert.assertArrayEquals(tpa, Arrays.copyOfRange(output, 24, 28));
92 public void deserializeTest() throws PacketException {
93 byte[] output = arp.serialize();
94 Packet packet = arp.deserialize(output, 0, 28);
95 Assert.assertTrue(packet instanceof Arp);
96 Arp newArp = (Arp) packet;
97 Assert.assertArrayEquals(sha, newArp.getSenderHardwareAddress());
98 Assert.assertArrayEquals(tha, newArp.getTargetHardwareAddress());
99 Assert.assertArrayEquals(spa, newArp.getSenderProtocolAddress());
100 Assert.assertArrayEquals(tpa, newArp.getTargetProtocolAddress());
101 Assert.assertEquals(operation, newArp.getOperation());
102 Assert.assertEquals(hlen, newArp.getHardwareLength());
103 Assert.assertEquals(plen, newArp.getProtocolLength());
104 Assert.assertEquals(htype, newArp.getHardwareType());
105 Assert.assertEquals(ptype, newArp.getProtocolType());