Refactored decoder models as per new design. Also modified abstract decoder to regist...
[l2switch.git] / packethandler / implementation / src / test / java / org / opendaylight / l2switch / packethandler / decoders / Ipv4DecoderTest.java
1 package org.opendaylight.l2switch.packethandler.decoders;
2
3 public class Ipv4DecoderTest {
4   /*Ipv4Decoder ipv4Decoder = new Ipv4Decoder();
5
6   @Test
7   public void testDecode() throws Exception {
8     byte[] payload = {
9       0x45, // Version = 4,  IHL = 5
10       0x00, // DSCP =0, ECN = 0
11       0x00, 0x1E, // Total Length -- 30
12       0x01, 0x1E, // Identification -- 286
13       0x00, 0x00, // Flags = all off & Fragment offset = 0
14       0x12, 0x11, // TTL = 18, Protocol = UDP
15       0x00, 0x00, // Checksum = 0
16       (byte)0xc0, (byte)0xa8, 0x00, 0x01, // Src IP Address
17       0x01, 0x02, 0x03, 0x04, // Dest IP Address
18       0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10 // Data
19     };
20     Ipv4Packet ipv4packet = (Ipv4Packet)ipv4Decoder.decode(new EthernetPacketBuilder().setEthernetPayload(payload).build());
21     assertEquals(4, ipv4packet.getVersion().intValue());
22     assertEquals(5, ipv4packet.getIhl().intValue());
23     assertEquals(30, ipv4packet.getIpv4Length().intValue());
24     assertEquals(0, ipv4packet.getDscp().intValue());
25     assertEquals(0, ipv4packet.getEcn().intValue());
26     assertEquals(30, ipv4packet.getIpv4Length().intValue());
27     assertEquals(286, ipv4packet.getId().intValue());
28     assertFalse(ipv4packet.isReservedFlag());
29     assertFalse(ipv4packet.isDfFlag());
30     assertFalse(ipv4packet.isMfFlag());
31     assertEquals(0, ipv4packet.getFragmentOffset().intValue());
32     assertEquals(18, ipv4packet.getTtl().intValue());
33     assertEquals(KnownIpProtocols.Udp, ipv4packet.getProtocol());
34     assertEquals(0, ipv4packet.getChecksum().intValue());
35     assertEquals("192.168.0.1", ipv4packet.getSourceIpv4());
36     assertEquals("1.2.3.4", ipv4packet.getDestinationIpv4());
37     assertTrue(Arrays.equals(ipv4packet.getIpv4Payload(), Arrays.copyOfRange(payload, 20, payload.length)));
38   }
39
40   @Test
41   public void testDecode_WithDiffServAndFlagsAndOffset() throws Exception {
42     byte[] payload = {
43       0x45, // Version = 4,  IHL = 5
44       (byte)0xff, // DSCP =63, ECN = 3
45       0x00, 0x1E, // Total Length -- 30
46       0x01, 0x1E, // Identification -- 286
47       (byte)0xf0, 0x00, // Flags = all on & Fragment offset = 0
48       0x12, 0x06, // TTL = 18, Protocol = TCP
49       (byte)0x00, 0x00, // Checksum = 0
50       (byte)0xc0, (byte)0xa8, 0x00, 0x01, // Src IP Address
51       0x01, 0x02, 0x03, 0x04, // Dest IP Address
52       0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10 // Data
53     };
54     Ipv4Packet ipv4packet = (Ipv4Packet)ipv4Decoder.decode(new EthernetPacketBuilder().setEthernetPayload(payload).build());
55     assertEquals(4, ipv4packet.getVersion().intValue());
56     assertEquals(5, ipv4packet.getIhl().intValue());
57     assertEquals(30, ipv4packet.getIpv4Length().intValue());
58     assertEquals(63, ipv4packet.getDscp().intValue());
59     assertEquals(3, ipv4packet.getEcn().intValue());
60     assertEquals(30, ipv4packet.getIpv4Length().intValue());
61     assertEquals(286, ipv4packet.getId().intValue());
62     assertTrue(ipv4packet.isReservedFlag());
63     assertTrue(ipv4packet.isDfFlag());
64     assertTrue(ipv4packet.isMfFlag());
65     assertEquals(4096, ipv4packet.getFragmentOffset().intValue());
66     assertEquals(18, ipv4packet.getTtl().intValue());
67     assertEquals(KnownIpProtocols.Tcp, ipv4packet.getProtocol());
68     assertEquals(0, ipv4packet.getChecksum().intValue());
69     assertEquals("192.168.0.1", ipv4packet.getSourceIpv4());
70     assertEquals("1.2.3.4", ipv4packet.getDestinationIpv4());
71     assertTrue(Arrays.equals(ipv4packet.getIpv4Payload(), Arrays.copyOfRange(payload, 20, payload.length)));
72   }
73
74   @Test
75   public void testDecode_AlternatingBits() throws Exception {
76     byte[] payload = {
77       (byte)0xf5, // Version = 15,  IHL = 5
78       (byte)0x0f, // DSCP =3, ECN = 3
79       0x00, 0x00, // Total Length -- 30
80       (byte)0xff, (byte)0xff, // Identification -- 65535
81       (byte)0x1f, (byte)0xff, // Flags = all off & Fragment offset = 8191
82       0x00, 0x06, // TTL = 00, Protocol = TCP
83       (byte)0xff, (byte)0xff, // Checksum = 65535
84       (byte)0x00, (byte)0x00, 0x00, 0x00, // Src IP Address
85       (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, // Dest IP Address
86       0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10 // Data
87     };
88     Ipv4Packet ipv4packet = (Ipv4Packet)ipv4Decoder.decode(new EthernetPacketBuilder().setEthernetPayload(payload).build());
89     assertEquals(15, ipv4packet.getVersion().intValue());
90     assertEquals(5, ipv4packet.getIhl().intValue());
91     assertEquals(0, ipv4packet.getIpv4Length().intValue());
92     assertEquals(3, ipv4packet.getDscp().intValue());
93     assertEquals(3, ipv4packet.getEcn().intValue());
94     assertEquals(0, ipv4packet.getIpv4Length().intValue());
95     assertEquals(65535, ipv4packet.getId().intValue());
96     assertFalse(ipv4packet.isReservedFlag());
97     assertFalse(ipv4packet.isDfFlag());
98     assertFalse(ipv4packet.isMfFlag());
99     assertEquals(8191, ipv4packet.getFragmentOffset().intValue());
100     assertEquals(0, ipv4packet.getTtl().intValue());
101     assertEquals(KnownIpProtocols.Tcp, ipv4packet.getProtocol());
102     assertEquals(65535, ipv4packet.getChecksum().intValue());
103     assertEquals("0.0.0.0", ipv4packet.getSourceIpv4());
104     assertEquals("255.255.255.255", ipv4packet.getDestinationIpv4());
105     assertTrue(Arrays.equals(ipv4packet.getIpv4Payload(), Arrays.copyOfRange(payload, 20, payload.length)));
106   }
107   */
108 }