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