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 static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNull;
14 import java.net.InetAddress;
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;
23 public class ArpUtilsTest {
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");
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();
35 int ptype = EtherTypes.IPv6.intValue();
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);
50 Ethernet eth = new Ethernet().setEtherType(EtherTypes.IPv4.shortValue())
51 .setDestinationMACAddress(ArpUtils.macToBytes(destMac))
52 .setSourceMACAddress(ArpUtils.macToBytes(srcMac));
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()));
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)));
69 result.contains("getSenderProtocolAddress()=" + InetAddress.getByAddress(spa).getHostAddress()));
71 result.contains("getTargetProtocolAddress()=" + InetAddress.getByAddress(tpa).getHostAddress()));
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));