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