Add INFO.yaml for GBP
[groupbasedpolicy.git] / renderers / ofoverlay / src / test / java / org / opendaylight / groupbasedpolicy / renderer / ofoverlay / arp / ArpTest.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.arp;
10
11 import java.net.InetAddress;
12 import java.net.UnknownHostException;
13 import java.util.Arrays;
14
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;
22
23 import com.google.common.primitives.Ints;
24
25 public class ArpTest {
26
27     private byte[] sha;
28     private byte[] spa;
29     private byte[] tha;
30     private byte[] tpa;
31     private int htype;
32     private int ptype;
33     private short hlen;
34     private short plen;
35     private int operation;
36     private Arp arp;
37
38     @Before
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();
44         htype = 2;
45         ptype = EtherTypes.IPv6.intValue();
46         hlen = 6;
47         plen = 4;
48         operation = 32;
49
50         arp = new Arp();
51
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);
61     }
62
63     @Test
64     public void ArpConstructionTest() throws Exception {
65
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());
75     }
76
77     @Test
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));
89     }
90
91     @Test
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());
106     }
107 }