UT: ofoverlay-arp
[groupbasedpolicy.git] / renderers / ofoverlay / src / test / java / org / opendaylight / groupbasedpolicy / renderer / ofoverlay / arp / ArpResolverUtilsTest.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.Rule;
15 import org.junit.Test;
16 import org.junit.rules.ExpectedException;
17 import org.opendaylight.controller.liblldp.EtherTypes;
18 import org.opendaylight.controller.liblldp.Ethernet;
19 import org.opendaylight.controller.liblldp.HexEncode;
20 import org.opendaylight.controller.liblldp.PacketException;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceivedBuilder;
23
24 public class ArpResolverUtilsTest {
25
26     @Test
27     public void getArpFromTest() throws Exception {
28
29         Arp arp = new Arp();
30         byte[] sha = HexEncode.bytesFromHexString("00:00:00:00:00:01");
31         byte[] spa = InetAddress.getByName("192.168.0.1").getAddress();
32         byte[] tha = HexEncode.bytesFromHexString("00:00:00:00:00:02");
33         byte[] tpa = InetAddress.getByName("192.168.0.2").getAddress();
34         int htype = 1;
35         int ptype = EtherTypes.IPv4.intValue();
36         short hlen = 6;
37         short plen = 4;
38         int operation = 1;
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 arpFrame = new Ethernet().setSourceMACAddress(sha)
51             .setDestinationMACAddress(tha)
52             .setEtherType(EtherTypes.ARP.shortValue());
53         arpFrame.setPayload(arp);
54         PacketReceived packet = new PacketReceivedBuilder().setPayload(arpFrame.serialize()).build();
55         Arp arpOut = ArpResolverUtils.getArpFrom(packet);
56         Assert.assertEquals(arp, arpOut);
57     }
58
59     @Rule public ExpectedException e = ExpectedException.none();
60     @Test
61     public void getArpFromTest_notArpPacket() throws PacketException {
62         byte[] payload = {0xb, 0xe, 0xe, 0xf};
63         PacketReceived packet = new PacketReceivedBuilder().setPayload(payload).build();
64         e.expect(PacketException.class);
65         ArpResolverUtils.getArpFrom(packet);
66     }
67 }