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 import org.junit.Test;
11 import org.mockito.Mockito;
13 import java.util.ArrayList;
14 import java.util.Arrays;
16 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.basepacket.rev140528.packet.chain.grp.PacketChain;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.basepacket.rev140528.packet.chain.grp.PacketChainBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.basepacket.rev140528.packet.chain.grp.packet.chain.packet.RawPacketBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.ethernet.rev140528.EthernetPacketReceivedBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.ethernet.rev140528.ethernet.packet.received.packet.chain.packet.EthernetPacketBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.ipv4.rev140528.Ipv4PacketReceived;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.ipv4.rev140528.KnownIpProtocols;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.ipv4.rev140528.ipv4.packet.received.packet.chain.packet.Ipv4Packet;
27 import static junit.framework.Assert.assertEquals;
28 import static junit.framework.Assert.assertFalse;
29 import static junit.framework.Assert.assertTrue;
31 public class Ipv4DecoderTest {
34 public void testDecode() throws Exception {
35 byte[] eth_payload = {
36 0x01, 0x23, 0x66, 0x67, (byte) 0x89, (byte) 0xab,
37 (byte) 0xcd, (byte) 0xef, 0x01, 0x23, 0x45, 0x67,
39 0x45, // Version = 4, IHL = 5
40 0x00, // DSCP =0, ECN = 0
41 0x00, 0x1E, // Total Length -- 30
42 0x01, 0x1E, // Identification -- 286
43 0x00, 0x00, // Flags = all off & Fragment offset = 0
44 0x12, 0x11, // TTL = 18, Protocol = UDP
45 0x00, 0x00, // Checksum = 0
46 (byte) 0xc0, (byte) 0xa8, 0x00, 0x01, // Src IP Address
47 0x01, 0x02, 0x03, 0x04, // Dest IP Address
48 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, // Data
49 (byte) 0x98, (byte) 0xfe, (byte) 0xdc, (byte) 0xba // CRC
51 NotificationProviderService npServiceMock = Mockito.mock(NotificationProviderService.class);
52 ArrayList<PacketChain> packetChainList = new ArrayList<PacketChain>();
53 packetChainList.add(new PacketChainBuilder()
54 .setPacket(new RawPacketBuilder().build())
56 packetChainList.add(new PacketChainBuilder()
57 .setPacket(new EthernetPacketBuilder().setPayloadOffset(14).build())
60 Ipv4PacketReceived notification = new Ipv4Decoder(npServiceMock).decode(new EthernetPacketReceivedBuilder()
61 .setPacketChain(packetChainList)
62 .setPayload(eth_payload)
64 Ipv4Packet ipv4Packet = (Ipv4Packet)notification.getPacketChain().get(2).getPacket();
65 assertEquals(4, ipv4Packet.getVersion().intValue());
66 assertEquals(5, ipv4Packet.getIhl().intValue());
67 assertEquals(30, ipv4Packet.getIpv4Length().intValue());
68 assertEquals(0, ipv4Packet.getDscp().getValue().intValue());
69 assertEquals(0, ipv4Packet.getEcn().intValue());
70 assertEquals(30, ipv4Packet.getIpv4Length().intValue());
71 assertEquals(286, ipv4Packet.getId().intValue());
72 assertFalse(ipv4Packet.isReservedFlag());
73 assertFalse(ipv4Packet.isDfFlag());
74 assertFalse(ipv4Packet.isMfFlag());
75 assertEquals(0, ipv4Packet.getFragmentOffset().intValue());
76 assertEquals(18, ipv4Packet.getTtl().intValue());
77 assertEquals(KnownIpProtocols.Udp, ipv4Packet.getProtocol());
78 assertEquals(0, ipv4Packet.getChecksum().intValue());
80 Ipv4Address src_address = new Ipv4Address("192.168.0.1");
81 Ipv4Address dst_address = new Ipv4Address("1.2.3.4");
82 assertEquals(src_address, ipv4Packet.getSourceIpv4());
83 assertEquals(dst_address, ipv4Packet.getDestinationIpv4());
84 assertEquals(10, ipv4Packet.getPayloadLength().intValue());
85 assertEquals(34, ipv4Packet.getPayloadOffset().intValue());
86 assertTrue(Arrays.equals(eth_payload, notification.getPayload()));
90 public void testDecode_WithDiffServAndFlagsAndOffset() throws Exception {
91 byte[] eth_payload = {
92 0x01, 0x23, 0x66, 0x67, (byte) 0x89, (byte) 0xab, //src mac
93 (byte) 0xcd, (byte) 0xef, 0x01, 0x23, 0x45, 0x67, //dst mac
95 0x08, 0x00, // EtherType
96 0x45, // Version = 4, IHL = 5
97 (byte) 0xff, // DSCP =63, ECN = 3
98 0x00, 0x1E, // Total Length -- 30
99 0x01, 0x1E, // Identification -- 286
100 (byte) 0xf0, 0x00, // Flags = all on & Fragment offset = 0
101 0x12, 0x06, // TTL = 18, Protocol = TCP
102 (byte) 0x00, 0x00, // Checksum = 0
103 (byte) 0xc0, (byte) 0xa8, 0x00, 0x01, // Src IP Address
104 0x01, 0x02, 0x03, 0x04, // Dest IP Address
105 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11,
107 (byte) 0x98, (byte) 0xfe, (byte) 0xdc, (byte) 0xba // CRC
109 NotificationProviderService npServiceMock = Mockito.mock(NotificationProviderService.class);
110 ArrayList<PacketChain> packetChainList = new ArrayList<PacketChain>();
111 packetChainList.add(new PacketChainBuilder()
112 .setPacket(new RawPacketBuilder().build())
114 packetChainList.add(new PacketChainBuilder()
115 .setPacket(new EthernetPacketBuilder().setPayloadOffset(16).build())
118 Ipv4PacketReceived notification = new Ipv4Decoder(npServiceMock).decode(new EthernetPacketReceivedBuilder()
119 .setPacketChain(packetChainList)
120 .setPayload(eth_payload)
122 Ipv4Packet ipv4Packet = (Ipv4Packet)notification.getPacketChain().get(2).getPacket();
123 assertEquals(4, ipv4Packet.getVersion().intValue());
124 assertEquals(5, ipv4Packet.getIhl().intValue());
125 assertEquals(30, ipv4Packet.getIpv4Length().intValue());
126 assertEquals(63, ipv4Packet.getDscp().getValue().intValue());
127 assertEquals(3, ipv4Packet.getEcn().intValue());
128 assertEquals(30, ipv4Packet.getIpv4Length().intValue());
129 assertEquals(286, ipv4Packet.getId().intValue());
130 assertTrue(ipv4Packet.isReservedFlag());
131 assertTrue(ipv4Packet.isDfFlag());
132 assertTrue(ipv4Packet.isMfFlag());
133 assertEquals(4096, ipv4Packet.getFragmentOffset().intValue());
134 assertEquals(18, ipv4Packet.getTtl().intValue());
135 assertEquals(KnownIpProtocols.Tcp, ipv4Packet.getProtocol());
136 assertEquals(0, ipv4Packet.getChecksum().intValue());
138 Ipv4Address src_address = new Ipv4Address("192.168.0.1");
139 Ipv4Address dst_address = new Ipv4Address("1.2.3.4");
140 assertEquals(src_address, ipv4Packet.getSourceIpv4());
141 assertEquals(dst_address, ipv4Packet.getDestinationIpv4());
142 assertEquals(13, ipv4Packet.getPayloadLength().intValue());
143 assertEquals(36, ipv4Packet.getPayloadOffset().intValue());
144 assertTrue(Arrays.equals(eth_payload, notification.getPayload()));
148 public void testDecode_AlternatingBits() throws Exception {
149 byte[] eth_payload = {
150 0x01, 0x23, 0x45, 0x67, (byte) 0x89, (byte) 0xab,
151 (byte) 0xcd, (byte) 0xef, 0x01, 0x23, 0x45, 0x67,
153 (byte) 0xff, (byte) 0xff,
154 (byte) 0x86, (byte) 0xdd,
155 (byte) 0xf5, // Version = 15, IHL = 5
156 (byte) 0x0f, // DSCP =3, ECN = 3
157 0x00, 0x00, // Total Length -- 30
158 (byte) 0xff, (byte) 0xff, // Identification -- 65535
159 (byte) 0x1f, (byte) 0xff, // Flags = all off & Fragment offset = 8191
160 0x00, 0x06, // TTL = 00, Protocol = TCP
161 (byte) 0xff, (byte) 0xff, // Checksum = 65535
162 (byte) 0x00, (byte) 0x00, 0x00, 0x00, // Src IP Address
163 (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
165 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, // Data
166 (byte) 0x98, (byte) 0xfe, (byte) 0xdc, (byte) 0xba // CRC
168 NotificationProviderService npServiceMock = Mockito.mock(NotificationProviderService.class);
169 ArrayList<PacketChain> packetChainList = new ArrayList<PacketChain>();
170 packetChainList.add(new PacketChainBuilder()
171 .setPacket(new RawPacketBuilder().build())
173 packetChainList.add(new PacketChainBuilder()
174 .setPacket(new EthernetPacketBuilder().setPayloadOffset(18).build())
177 Ipv4PacketReceived notification = new Ipv4Decoder(npServiceMock).decode(new EthernetPacketReceivedBuilder()
178 .setPacketChain(packetChainList)
179 .setPayload(eth_payload)
181 Ipv4Packet ipv4Packet = (Ipv4Packet)notification.getPacketChain().get(2).getPacket();
182 assertEquals(15, ipv4Packet.getVersion().intValue());
183 assertEquals(5, ipv4Packet.getIhl().intValue());
184 assertEquals(0, ipv4Packet.getIpv4Length().intValue());
185 assertEquals(3, ipv4Packet.getDscp().getValue().intValue());
186 assertEquals(3, ipv4Packet.getEcn().intValue());
187 assertEquals(0, ipv4Packet.getIpv4Length().intValue());
188 assertEquals(65535, ipv4Packet.getId().intValue());
189 assertFalse(ipv4Packet.isReservedFlag());
190 assertFalse(ipv4Packet.isDfFlag());
191 assertFalse(ipv4Packet.isMfFlag());
192 assertEquals(8191, ipv4Packet.getFragmentOffset().intValue());
193 assertEquals(0, ipv4Packet.getTtl().intValue());
194 assertEquals(KnownIpProtocols.Tcp, ipv4Packet.getProtocol());
195 assertEquals(65535, ipv4Packet.getChecksum().intValue());
197 Ipv4Address src_address = new Ipv4Address("0.0.0.0");
198 Ipv4Address dst_address = new Ipv4Address("255.255.255.255");
199 assertEquals(src_address, ipv4Packet.getSourceIpv4());
200 assertEquals(dst_address, ipv4Packet.getDestinationIpv4());
201 assertEquals(10, ipv4Packet.getPayloadLength().intValue());
202 assertEquals(38, ipv4Packet.getPayloadOffset().intValue());
203 assertTrue(Arrays.equals(eth_payload, notification.getPayload()));
206 // This test is from a Mininet VM, taken from a wireshark dump
208 public void testDecode_Udp() throws Exception {
209 byte[] eth_payload = {
211 (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, 0x06, 0x27, 0x25, 0x06, (byte)0x81, (byte)0x81, 0x08, 0x00,
213 0x45, 0x10, 0x01, 0x48, 0x00, 0x00, 0x00, 0x00, (byte)0x80, 0x11, 0x39, (byte)0x96, 0x00, 0x00, 0x00, 0x00, (byte)0xff, (byte)0xff,
214 (byte)0xff, (byte)0xff,
216 0x00, 0x44, 0x00, 0x43, 0x01, 0x34, 0x2d, (byte)0xf5, 0x01, 0x01, 0x06, 0x00, (byte)0xdf, (byte)0xcc
218 NotificationProviderService npServiceMock = Mockito.mock(NotificationProviderService.class);
219 ArrayList<PacketChain> packetChainList = new ArrayList<PacketChain>();
220 packetChainList.add(new PacketChainBuilder()
221 .setPacket(new RawPacketBuilder().build())
223 packetChainList.add(new PacketChainBuilder()
224 .setPacket(new EthernetPacketBuilder().setPayloadOffset(14).build())
227 Ipv4PacketReceived notification = new Ipv4Decoder(npServiceMock).decode(new EthernetPacketReceivedBuilder()
228 .setPacketChain(packetChainList)
229 .setPayload(eth_payload)
231 Ipv4Packet ipv4Packet = (Ipv4Packet)notification.getPacketChain().get(2).getPacket();
232 assertEquals(4, ipv4Packet.getVersion().intValue());
233 assertEquals(5, ipv4Packet.getIhl().intValue());
234 assertEquals(4, ipv4Packet.getDscp().getValue().intValue());
235 assertEquals(0, ipv4Packet.getEcn().intValue());
236 assertEquals(328, ipv4Packet.getIpv4Length().intValue());
237 assertEquals(0, ipv4Packet.getId().intValue());
238 assertFalse(ipv4Packet.isReservedFlag());
239 assertFalse(ipv4Packet.isDfFlag());
240 assertFalse(ipv4Packet.isMfFlag());
241 assertEquals(0, ipv4Packet.getFragmentOffset().intValue());
242 assertEquals(128, ipv4Packet.getTtl().intValue());
243 assertEquals(KnownIpProtocols.Udp, ipv4Packet.getProtocol());
244 assertEquals(14742, ipv4Packet.getChecksum().intValue());
246 Ipv4Address src_address = new Ipv4Address("0.0.0.0");
247 Ipv4Address dst_address = new Ipv4Address("255.255.255.255");
248 assertEquals(src_address, ipv4Packet.getSourceIpv4());
249 assertEquals(dst_address, ipv4Packet.getDestinationIpv4());
250 assertEquals(34, ipv4Packet.getPayloadOffset().intValue());
251 // Not testing payloadLength because wireshark does not show crc
252 assertTrue(Arrays.equals(eth_payload, notification.getPayload()));