ad13e2e4eb0b9c71c85e4aeaa215f2ebe4ddacc5
[l2switch.git] / arppacket / implementation / src / test / java / org / opendaylight / l2switch / arppacket / ArpDecoderTest.java
1 package org.opendaylight.l2switch.arppacket;
2
3 import org.junit.Test;
4 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.arp.rev140528.ArpPacket;
5 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.arp.rev140528.KnownHardwareType;
6 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.arp.rev140528.KnownOperation;
7 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.ethernet.rev140528.EthernetPacketBuilder;
8 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.ethernet.rev140528.KnownEtherType;
9
10 import static junit.framework.Assert.*;
11
12 public class ArpDecoderTest {
13   ArpDecoder arpDecoder = new ArpDecoder();
14
15   @Test
16   public void testDecode_RequestIPv4() throws Exception {
17     byte[] payload = {
18       0x00, 0x01, // Hardware Type -- Ethernet
19       0x08, 0x00, // Protocol Type -- Ipv4
20       0x06, // Hardware Length -- 6
21       0x04, // Protcool Length -- 4
22       0x00, 0x01, // Operator -- Request
23       0x01, 0x23, 0x45, 0x67, (byte) 0x89, (byte) 0xab, // Src Hardware Address
24       (byte)0xc0, (byte)0xa8, 0x00, 0x01, // Src Protocol Address
25       (byte) 0xcd, (byte) 0xef, 0x01, 0x23, 0x45, 0x67, // Dest Hardware Address
26       0x01, 0x02, 0x03, 0x04 // Dest Protocol Address
27     };
28     ArpPacket arp = (ArpPacket)arpDecoder.decode(new EthernetPacketBuilder().setEthernetPayload(payload).build());
29     assertEquals(KnownHardwareType.Ethernet, arp.getHardwareType());
30     assertEquals(KnownEtherType.Ipv4, arp.getProtocolType());
31     assertEquals(6, arp.getHardwareLength().intValue());
32     assertEquals(4, arp.getProtocolLength().intValue());
33     assertEquals(KnownOperation.Request, arp.getOperation());
34     assertEquals("01:23:45:67:89:ab", arp.getSourceHardwareAddress());
35     assertEquals("192.168.0.1", arp.getSourceProtocolAddress());
36     assertEquals("cd:ef:01:23:45:67", arp.getDestinationHardwareAddress());
37     assertEquals("1.2.3.4", arp.getDestinationProtocolAddress());
38   }
39
40   @Test
41   public void testDecode_ReplyIPv4() throws Exception {
42     byte[] payload = {
43       0x00, 0x01, // Hardware Type -- Ethernet
44       0x08, 0x00, // Protocol Type -- Ipv4
45       0x06, // Hardware Length -- 6
46       0x04, // Protcool Length -- 4
47       0x00, 0x02, // Operator -- Reply
48       0x01, 0x23, 0x45, 0x67, (byte) 0x89, (byte) 0xab, // Src Hardware Address
49       (byte)0xc0, (byte)0xa8, 0x00, 0x01, // Src Protocol Address
50       (byte) 0xcd, (byte) 0xef, 0x01, 0x23, 0x45, 0x67, // Dest Hardware Address
51       0x01, 0x02, 0x03, 0x04 // Dest Protocol Address
52     };
53     ArpPacket arp = (ArpPacket)arpDecoder.decode(new EthernetPacketBuilder().setEthernetPayload(payload).build());
54     assertEquals(KnownHardwareType.Ethernet, arp.getHardwareType());
55     assertEquals(KnownEtherType.Ipv4, arp.getProtocolType());
56     assertEquals(6, arp.getHardwareLength().intValue());
57     assertEquals(4, arp.getProtocolLength().intValue());
58     assertEquals(KnownOperation.Reply, arp.getOperation());
59     assertEquals("01:23:45:67:89:ab", arp.getSourceHardwareAddress());
60     assertEquals("192.168.0.1", arp.getSourceProtocolAddress());
61     assertEquals("cd:ef:01:23:45:67", arp.getDestinationHardwareAddress());
62     assertEquals("1.2.3.4", arp.getDestinationProtocolAddress());
63   }
64 }