1 package org.opendaylight.l2switch.ipv4decoder;
4 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.ethernet.rev140528.EthernetPacketBuilder;
5 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.ipv4.rev140528.Ipv4Packet;
6 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.ipv4.rev140528.KnownIpProtocols;
8 import java.util.Arrays;
10 import static junit.framework.Assert.*;
12 public class Ipv4DecoderTest {
13 Ipv4Decoder ipv4Decoder = new Ipv4Decoder();
16 public void testDecode() throws Exception {
18 0x45, // Version = 4, IHL = 5
19 0x00, // DSCP =0, ECN = 0
20 0x00, 0x1E, // Total Length -- 30
21 0x01, 0x1E, // Identification -- 286
22 0x00, 0x00, // Flags = all off & Fragment offset = 0
23 0x12, 0x11, // TTL = 18, Protocol = UDP
24 0x00, 0x00, // Checksum = 0
25 (byte)0xc0, (byte)0xa8, 0x00, 0x01, // Src IP Address
26 0x01, 0x02, 0x03, 0x04, // Dest IP Address
27 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10 // Data
29 Ipv4Packet ipv4packet = (Ipv4Packet)ipv4Decoder.decode(new EthernetPacketBuilder().setEthernetPayload(payload).build());
30 assertEquals(4, ipv4packet.getVersion().intValue());
31 assertEquals(5, ipv4packet.getIhl().intValue());
32 assertEquals(30, ipv4packet.getIpv4Length().intValue());
33 assertEquals(0, ipv4packet.getDscp().intValue());
34 assertEquals(0, ipv4packet.getEcn().intValue());
35 assertEquals(30, ipv4packet.getIpv4Length().intValue());
36 assertEquals(286, ipv4packet.getId().intValue());
37 assertFalse(ipv4packet.isReservedFlag());
38 assertFalse(ipv4packet.isDfFlag());
39 assertFalse(ipv4packet.isMfFlag());
40 assertEquals(0, ipv4packet.getFragmentOffset().intValue());
41 assertEquals(18, ipv4packet.getTtl().intValue());
42 assertEquals(KnownIpProtocols.Udp, ipv4packet.getProtocol());
43 assertEquals(0, ipv4packet.getChecksum().intValue());
44 assertEquals("192.168.0.1", ipv4packet.getSourceIpv4());
45 assertEquals("1.2.3.4", ipv4packet.getDestinationIpv4());
46 assertTrue(Arrays.equals(ipv4packet.getIpv4Payload(), Arrays.copyOfRange(payload, 20, payload.length)));
50 public void testDecode_WithDiffServAndFlagsAndOffset() throws Exception {
52 0x45, // Version = 4, IHL = 5
53 (byte)0xff, // DSCP =63, ECN = 3
54 0x00, 0x1E, // Total Length -- 30
55 0x01, 0x1E, // Identification -- 286
56 (byte)0xf0, 0x00, // Flags = all on & Fragment offset = 0
57 0x12, 0x06, // TTL = 18, Protocol = TCP
58 (byte)0x00, 0x00, // Checksum = 0
59 (byte)0xc0, (byte)0xa8, 0x00, 0x01, // Src IP Address
60 0x01, 0x02, 0x03, 0x04, // Dest IP Address
61 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10 // Data
63 Ipv4Packet ipv4packet = (Ipv4Packet)ipv4Decoder.decode(new EthernetPacketBuilder().setEthernetPayload(payload).build());
64 assertEquals(4, ipv4packet.getVersion().intValue());
65 assertEquals(5, ipv4packet.getIhl().intValue());
66 assertEquals(30, ipv4packet.getIpv4Length().intValue());
67 assertEquals(63, ipv4packet.getDscp().intValue());
68 assertEquals(3, ipv4packet.getEcn().intValue());
69 assertEquals(30, ipv4packet.getIpv4Length().intValue());
70 assertEquals(286, ipv4packet.getId().intValue());
71 assertTrue(ipv4packet.isReservedFlag());
72 assertTrue(ipv4packet.isDfFlag());
73 assertTrue(ipv4packet.isMfFlag());
74 assertEquals(4096, ipv4packet.getFragmentOffset().intValue());
75 assertEquals(18, ipv4packet.getTtl().intValue());
76 assertEquals(KnownIpProtocols.Tcp, ipv4packet.getProtocol());
77 assertEquals(0, ipv4packet.getChecksum().intValue());
78 assertEquals("192.168.0.1", ipv4packet.getSourceIpv4());
79 assertEquals("1.2.3.4", ipv4packet.getDestinationIpv4());
80 assertTrue(Arrays.equals(ipv4packet.getIpv4Payload(), Arrays.copyOfRange(payload, 20, payload.length)));
84 public void testDecode_AlternatingBits() throws Exception {
86 (byte)0xf5, // Version = 15, IHL = 5
87 (byte)0x0f, // DSCP =3, ECN = 3
88 0x00, 0x00, // Total Length -- 30
89 (byte)0xff, (byte)0xff, // Identification -- 65535
90 (byte)0x1f, (byte)0xff, // Flags = all off & Fragment offset = 8191
91 0x00, 0x06, // TTL = 00, Protocol = TCP
92 (byte)0xff, (byte)0xff, // Checksum = 65535
93 (byte)0x00, (byte)0x00, 0x00, 0x00, // Src IP Address
94 (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, // Dest IP Address
95 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10 // Data
97 Ipv4Packet ipv4packet = (Ipv4Packet)ipv4Decoder.decode(new EthernetPacketBuilder().setEthernetPayload(payload).build());
98 assertEquals(15, ipv4packet.getVersion().intValue());
99 assertEquals(5, ipv4packet.getIhl().intValue());
100 assertEquals(0, ipv4packet.getIpv4Length().intValue());
101 assertEquals(3, ipv4packet.getDscp().intValue());
102 assertEquals(3, ipv4packet.getEcn().intValue());
103 assertEquals(0, ipv4packet.getIpv4Length().intValue());
104 assertEquals(65535, ipv4packet.getId().intValue());
105 assertFalse(ipv4packet.isReservedFlag());
106 assertFalse(ipv4packet.isDfFlag());
107 assertFalse(ipv4packet.isMfFlag());
108 assertEquals(8191, ipv4packet.getFragmentOffset().intValue());
109 assertEquals(0, ipv4packet.getTtl().intValue());
110 assertEquals(KnownIpProtocols.Tcp, ipv4packet.getProtocol());
111 assertEquals(65535, ipv4packet.getChecksum().intValue());
112 assertEquals("0.0.0.0", ipv4packet.getSourceIpv4());
113 assertEquals("255.255.255.255", ipv4packet.getDestinationIpv4());
114 assertTrue(Arrays.equals(ipv4packet.getIpv4Payload(), Arrays.copyOfRange(payload, 20, payload.length)));