Remove compatibility methods at BitBufferHelper
[l2switch.git] / packethandler / implementation / src / test / java / org / opendaylight / l2switch / packethandler / decoders / Ipv4DecoderTest.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.l2switch.packethandler.decoders;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertTrue;
14
15 import java.util.ArrayList;
16 import java.util.Arrays;
17 import org.junit.Test;
18 import org.mockito.Mockito;
19 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
20 import org.opendaylight.mdsal.binding.api.NotificationService;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.basepacket.rev140528.packet.chain.grp.PacketChain;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.basepacket.rev140528.packet.chain.grp.PacketChainBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.basepacket.rev140528.packet.chain.grp.packet.chain.packet.RawPacketBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.ethernet.rev140528.EthernetPacketReceivedBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.ethernet.rev140528.ethernet.packet.received.packet.chain.packet.EthernetPacketBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.ipv4.rev140528.Ipv4PacketReceived;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.ipv4.rev140528.KnownIpProtocols;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.ipv4.rev140528.ipv4.packet.received.packet.chain.packet.Ipv4Packet;
30
31 public class Ipv4DecoderTest {
32
33     @Test
34     public void testDecode() throws Exception {
35         byte[] ethPayload = {
36             0x01, 0x23, 0x66, 0x67, (byte) 0x89, (byte) 0xab,
37             (byte) 0xcd, (byte) 0xef, 0x01, 0x23, 0x45, 0x67,
38             0x08, 0x00,
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
50         };
51         NotificationPublishService npServiceMock = Mockito.mock(NotificationPublishService.class);
52         NotificationService mock2 = Mockito.mock(NotificationService.class);
53         ArrayList<PacketChain> packetChainList = new ArrayList<>();
54         packetChainList.add(new PacketChainBuilder().setPacket(new RawPacketBuilder().build()).build());
55         packetChainList.add(
56                 new PacketChainBuilder().setPacket(new EthernetPacketBuilder().setPayloadOffset(14).build()).build());
57
58         Ipv4PacketReceived notification = new Ipv4Decoder(npServiceMock, mock2).decode(
59                 new EthernetPacketReceivedBuilder().setPacketChain(packetChainList).setPayload(ethPayload).build());
60         Ipv4Packet ipv4Packet = (Ipv4Packet) notification.getPacketChain().get(2).getPacket();
61         assertEquals(4, ipv4Packet.getVersion().intValue());
62         assertEquals(5, ipv4Packet.getIhl().intValue());
63         assertEquals(30, ipv4Packet.getIpv4Length().intValue());
64         assertEquals(0, ipv4Packet.getDscp().getValue().intValue());
65         assertEquals(0, ipv4Packet.getEcn().intValue());
66         assertEquals(30, ipv4Packet.getIpv4Length().intValue());
67         assertEquals(286, ipv4Packet.getId().intValue());
68         assertFalse(ipv4Packet.getReservedFlag());
69         assertFalse(ipv4Packet.getDfFlag());
70         assertFalse(ipv4Packet.getMfFlag());
71         assertEquals(0, ipv4Packet.getFragmentOffset().intValue());
72         assertEquals(18, ipv4Packet.getTtl().intValue());
73         assertEquals(KnownIpProtocols.Udp, ipv4Packet.getProtocol());
74         assertEquals(0, ipv4Packet.getChecksum().intValue());
75
76         Ipv4Address srcAddress = new Ipv4Address("192.168.0.1");
77         Ipv4Address dstAddress = new Ipv4Address("1.2.3.4");
78         assertEquals(srcAddress, ipv4Packet.getSourceIpv4());
79         assertEquals(dstAddress, ipv4Packet.getDestinationIpv4());
80         assertEquals(10, ipv4Packet.getPayloadLength().intValue());
81         assertEquals(34, ipv4Packet.getPayloadOffset().intValue());
82         assertTrue(Arrays.equals(ethPayload, notification.getPayload()));
83     }
84
85     @Test
86     public void testDecode_WithDiffServAndFlagsAndOffset() throws Exception {
87         byte[] ethPayload = {
88             0x01, 0x23, 0x66, 0x67, (byte) 0x89, (byte) 0xab, //src mac
89             (byte) 0xcd, (byte) 0xef, 0x01, 0x23, 0x45, 0x67, //dst mac
90             (byte) 0x81, 0x00,
91             0x08, 0x00, // EtherType
92             0x45, // Version = 4,  IHL = 5
93             (byte) 0xff, // DSCP =63, ECN = 3
94             0x00, 0x1E, // Total Length -- 30
95             0x01, 0x1E, // Identification -- 286
96             (byte) 0xf0, 0x00, // Flags = all on & Fragment offset = 0
97             0x12, 0x06, // TTL = 18, Protocol = TCP
98             (byte) 0x00, 0x00, // Checksum = 0
99             (byte) 0xc0, (byte) 0xa8, 0x00, 0x01, // Src IP Address
100             0x01, 0x02, 0x03, 0x04, // Dest IP Address
101             0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11,
102             0x12, 0x13, // Data
103             (byte) 0x98, (byte) 0xfe, (byte) 0xdc, (byte) 0xba // CRC
104         };
105         NotificationPublishService npServiceMock = Mockito.mock(NotificationPublishService.class);
106         NotificationService mock2 = Mockito.mock(NotificationService.class);
107         ArrayList<PacketChain> packetChainList = new ArrayList<>();
108         packetChainList.add(new PacketChainBuilder().setPacket(new RawPacketBuilder().build()).build());
109         packetChainList.add(
110                 new PacketChainBuilder().setPacket(new EthernetPacketBuilder().setPayloadOffset(16).build()).build());
111
112         Ipv4PacketReceived notification = new Ipv4Decoder(npServiceMock, mock2).decode(
113                 new EthernetPacketReceivedBuilder().setPacketChain(packetChainList).setPayload(ethPayload).build());
114         Ipv4Packet ipv4Packet = (Ipv4Packet) notification.getPacketChain().get(2).getPacket();
115         assertEquals(4, ipv4Packet.getVersion().intValue());
116         assertEquals(5, ipv4Packet.getIhl().intValue());
117         assertEquals(30, ipv4Packet.getIpv4Length().intValue());
118         assertEquals(63, ipv4Packet.getDscp().getValue().intValue());
119         assertEquals(3, ipv4Packet.getEcn().intValue());
120         assertEquals(30, ipv4Packet.getIpv4Length().intValue());
121         assertEquals(286, ipv4Packet.getId().intValue());
122         assertTrue(ipv4Packet.getReservedFlag());
123         assertTrue(ipv4Packet.getDfFlag());
124         assertTrue(ipv4Packet.getMfFlag());
125         assertEquals(4096, ipv4Packet.getFragmentOffset().intValue());
126         assertEquals(18, ipv4Packet.getTtl().intValue());
127         assertEquals(KnownIpProtocols.Tcp, ipv4Packet.getProtocol());
128         assertEquals(0, ipv4Packet.getChecksum().intValue());
129
130         Ipv4Address srcAddress = new Ipv4Address("192.168.0.1");
131         Ipv4Address dstAddress = new Ipv4Address("1.2.3.4");
132         assertEquals(srcAddress, ipv4Packet.getSourceIpv4());
133         assertEquals(dstAddress, ipv4Packet.getDestinationIpv4());
134         assertEquals(13, ipv4Packet.getPayloadLength().intValue());
135         assertEquals(36, ipv4Packet.getPayloadOffset().intValue());
136         assertTrue(Arrays.equals(ethPayload, notification.getPayload()));
137     }
138
139     @Test
140     public void testDecode_AlternatingBits() throws Exception {
141         byte[] ethPayload = {
142             0x01, 0x23, 0x45, 0x67, (byte) 0x89, (byte) 0xab,
143             (byte) 0xcd, (byte) 0xef, 0x01, 0x23, 0x45, 0x67,
144             (byte) 0x81, 0x00,
145             (byte) 0xff, (byte) 0xff,
146             (byte) 0x86, (byte) 0xdd,
147             (byte) 0xf5, // Version = 15,  IHL = 5
148             (byte) 0x0f, // DSCP =3, ECN = 3
149             0x00, 0x00, // Total Length -- 30
150             (byte) 0xff, (byte) 0xff, // Identification -- 65535
151             (byte) 0x1f, (byte) 0xff, // Flags = all off & Fragment offset = 8191
152             0x00, 0x06, // TTL = 00, Protocol = TCP
153             (byte) 0xff, (byte) 0xff, // Checksum = 65535
154             (byte) 0x00, (byte) 0x00, 0x00, 0x00, // Src IP Address
155             (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
156             // Dest IP Address
157             0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, // Data
158             (byte) 0x98, (byte) 0xfe, (byte) 0xdc, (byte) 0xba // CRC
159         };
160         NotificationPublishService npServiceMock = Mockito.mock(NotificationPublishService.class);
161         NotificationService mock2 = Mockito.mock(NotificationService.class);
162         ArrayList<PacketChain> packetChainList = new ArrayList<>();
163         packetChainList.add(new PacketChainBuilder().setPacket(new RawPacketBuilder().build()).build());
164         packetChainList.add(
165                 new PacketChainBuilder().setPacket(new EthernetPacketBuilder().setPayloadOffset(18).build()).build());
166
167         Ipv4PacketReceived notification = new Ipv4Decoder(npServiceMock, mock2).decode(
168                 new EthernetPacketReceivedBuilder().setPacketChain(packetChainList).setPayload(ethPayload).build());
169         Ipv4Packet ipv4Packet = (Ipv4Packet) notification.getPacketChain().get(2).getPacket();
170         assertEquals(15, ipv4Packet.getVersion().intValue());
171         assertEquals(5, ipv4Packet.getIhl().intValue());
172         assertEquals(0, ipv4Packet.getIpv4Length().intValue());
173         assertEquals(3, ipv4Packet.getDscp().getValue().intValue());
174         assertEquals(3, ipv4Packet.getEcn().intValue());
175         assertEquals(0, ipv4Packet.getIpv4Length().intValue());
176         assertEquals(65535, ipv4Packet.getId().intValue());
177         assertFalse(ipv4Packet.getReservedFlag());
178         assertFalse(ipv4Packet.getDfFlag());
179         assertFalse(ipv4Packet.getMfFlag());
180         assertEquals(8191, ipv4Packet.getFragmentOffset().intValue());
181         assertEquals(0, ipv4Packet.getTtl().intValue());
182         assertEquals(KnownIpProtocols.Tcp, ipv4Packet.getProtocol());
183         assertEquals(65535, ipv4Packet.getChecksum().intValue());
184
185         Ipv4Address srcAddress = new Ipv4Address("0.0.0.0");
186         Ipv4Address dstAddress = new Ipv4Address("255.255.255.255");
187         assertEquals(srcAddress, ipv4Packet.getSourceIpv4());
188         assertEquals(dstAddress, ipv4Packet.getDestinationIpv4());
189         assertEquals(10, ipv4Packet.getPayloadLength().intValue());
190         assertEquals(38, ipv4Packet.getPayloadOffset().intValue());
191         assertTrue(Arrays.equals(ethPayload, notification.getPayload()));
192     }
193
194     // This test is from a Mininet VM, taken from a wireshark dump
195     @Test
196     public void testDecode_Udp() throws Exception {
197         byte[] ethPayload = {
198             // Eth start
199             (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, 0x06, 0x27, 0x25, 0x06, (byte)0x81,
200             (byte)0x81, 0x08, 0x00,
201             // Ipv4 start
202             0x45, 0x10, 0x01, 0x48, 0x00, 0x00, 0x00, 0x00, (byte)0x80, 0x11, 0x39, (byte)0x96, 0x00, 0x00, 0x00, 0x00,
203             (byte)0xff, (byte)0xff,
204             (byte)0xff, (byte)0xff,
205             // Udp start
206             0x00, 0x44, 0x00, 0x43, 0x01, 0x34, 0x2d, (byte)0xf5, 0x01, 0x01, 0x06, 0x00, (byte)0xdf, (byte)0xcc
207         };
208         NotificationPublishService npServiceMock = Mockito.mock(NotificationPublishService.class);
209         NotificationService mock2 = Mockito.mock(NotificationService.class);
210         ArrayList<PacketChain> packetChainList = new ArrayList<>();
211         packetChainList.add(new PacketChainBuilder().setPacket(new RawPacketBuilder().build()).build());
212         packetChainList.add(
213                 new PacketChainBuilder().setPacket(new EthernetPacketBuilder().setPayloadOffset(14).build()).build());
214
215         Ipv4PacketReceived notification = new Ipv4Decoder(npServiceMock, mock2).decode(
216                 new EthernetPacketReceivedBuilder().setPacketChain(packetChainList).setPayload(ethPayload).build());
217         Ipv4Packet ipv4Packet = (Ipv4Packet) notification.getPacketChain().get(2).getPacket();
218         assertEquals(4, ipv4Packet.getVersion().intValue());
219         assertEquals(5, ipv4Packet.getIhl().intValue());
220         assertEquals(4, ipv4Packet.getDscp().getValue().intValue());
221         assertEquals(0, ipv4Packet.getEcn().intValue());
222         assertEquals(328, ipv4Packet.getIpv4Length().intValue());
223         assertEquals(0, ipv4Packet.getId().intValue());
224         assertFalse(ipv4Packet.getReservedFlag());
225         assertFalse(ipv4Packet.getDfFlag());
226         assertFalse(ipv4Packet.getMfFlag());
227         assertEquals(0, ipv4Packet.getFragmentOffset().intValue());
228         assertEquals(128, ipv4Packet.getTtl().intValue());
229         assertEquals(KnownIpProtocols.Udp, ipv4Packet.getProtocol());
230         assertEquals(14742, ipv4Packet.getChecksum().intValue());
231
232         Ipv4Address srcAddress = new Ipv4Address("0.0.0.0");
233         Ipv4Address dstAddress = new Ipv4Address("255.255.255.255");
234         assertEquals(srcAddress, ipv4Packet.getSourceIpv4());
235         assertEquals(dstAddress, ipv4Packet.getDestinationIpv4());
236         assertEquals(34, ipv4Packet.getPayloadOffset().intValue());
237         // Not testing payloadLength because wireshark does not show crc
238         assertTrue(Arrays.equals(ethPayload, notification.getPayload()));
239     }
240 }