UT: ofoverlay-arp
[groupbasedpolicy.git] / renderers / ofoverlay / src / test / java / org / opendaylight / groupbasedpolicy / renderer / ofoverlay / arp / ArpUtilsTest.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
13 import org.junit.Assert;
14 import org.junit.Test;
15 import org.opendaylight.controller.liblldp.EtherTypes;
16 import org.opendaylight.controller.liblldp.Ethernet;
17 import org.opendaylight.controller.liblldp.HexEncode;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
19
20 public class ArpUtilsTest {
21
22     @Test
23     public void getArpFrameToStringFormatTest() throws Exception {
24         MacAddress destMac = new MacAddress("00:00:00:00:00:01");
25         MacAddress srcMac = new MacAddress("00:00:00:00:00:02");
26         Arp arp = new Arp();
27         byte[] sha = HexEncode.bytesFromHexString(srcMac.getValue());
28         byte[] spa = InetAddress.getByName("192.168.0.1").getAddress();
29         byte[] tha = HexEncode.bytesFromHexString(destMac.getValue());
30         byte[] tpa = InetAddress.getByName("192.168.0.2").getAddress();
31         int htype = 2;
32         int ptype = EtherTypes.IPv6.intValue();
33         short hlen = 8;
34         short plen = 8;
35         int operation = 32;
36
37         arp.setSenderHardwareAddress(sha);
38         arp.setSenderProtocolAddress(spa);
39         arp.setTargetHardwareAddress(tha);
40         arp.setTargetProtocolAddress(tpa);
41         arp.setOperation(operation);
42         arp.setHardwareLength(hlen);
43         arp.setProtocolLength(plen);
44         arp.setHardwareType(htype);
45         arp.setProtocolType(ptype);
46
47         Ethernet eth = new Ethernet().setEtherType(EtherTypes.IPv4.shortValue())
48             .setDestinationMACAddress(ArpUtils.macToBytes(destMac))
49             .setSourceMACAddress(ArpUtils.macToBytes(srcMac));
50         eth.setPayload(arp);
51
52         String daco = InetAddress.getByAddress(spa).getHostAddress();
53         String result = ArpUtils.getArpFrameToStringFormat(eth);
54         Assert.assertTrue(result.contains("getSourceMACAddress()=" + srcMac.getValue()));
55         Assert.assertTrue(result.contains("getDestinationMACAddress()=" + destMac.getValue()));
56         Assert.assertTrue(
57                 result.contains("getEtherType()=" + EtherTypes.loadFromString(String.valueOf(eth.getEtherType()))));
58         Assert.assertTrue(result.contains("getHardwareType()=" + htype));
59         Assert.assertTrue(result.contains("getProtocolType()=" + ptype));
60         Assert.assertTrue(result.contains("getHardwareLength()=" + hlen));
61         Assert.assertTrue(result.contains("getProtocolLength()=" + plen));
62         Assert.assertTrue(result.contains("getOperation()=" + ArpOperation.loadFromInt(arp.getOperation())));
63         Assert.assertTrue(result.contains("getSenderHardwareAddress()=" + HexEncode.bytesToHexStringFormat(sha)));
64         Assert.assertTrue(result.contains("getTargetHardwareAddress()=" + HexEncode.bytesToHexStringFormat(tha)));
65         Assert.assertTrue(
66                 result.contains("getSenderProtocolAddress()=" + InetAddress.getByAddress(spa).getHostAddress()));
67         Assert.assertTrue(
68                 result.contains("getTargetProtocolAddress()=" + InetAddress.getByAddress(tpa).getHostAddress()));
69     }
70 }