Bug-5644: Fix checkstyle invocation for l2switch builds
[l2switch.git] / packethandler / implementation / src / test / java / org / opendaylight / l2switch / packethandler / decoders / EthernetDecoderTest.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 package org.opendaylight.l2switch.packethandler.decoders;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertNull;
13 import static org.junit.Assert.assertTrue;
14
15 import java.util.Arrays;
16
17 import org.junit.Test;
18 import org.mockito.Mockito;
19 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.ethernet.rev140528.EthernetPacketReceived;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.ethernet.rev140528.Header8021qType;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.ethernet.rev140528.KnownEtherType;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.ethernet.rev140528.ethernet.packet.received.packet.chain.packet.EthernetPacket;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceivedBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.packet.received.MatchBuilder;
26
27 public class EthernetDecoderTest {
28
29     @Test
30     public void testDecode_IPv4EtherType() throws Exception {
31         byte[] packet = {
32             0x01, 0x23, 0x45, 0x67, (byte) 0x89, (byte) 0xab,
33             (byte) 0xcd, (byte) 0xef, 0x01, 0x23, 0x45, 0x67,
34             0x08, 0x00,
35             0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11,
36             (byte)0x98, (byte)0xfe, (byte)0xdc, (byte)0xba
37         };
38         NotificationProviderService mock = Mockito.mock(NotificationProviderService.class);
39         EthernetPacketReceived notification = new EthernetDecoder(mock)
40                 .decode(new PacketReceivedBuilder().setPayload(packet).setMatch(new MatchBuilder().build()).build());
41         EthernetPacket ethernetPacket = (EthernetPacket) notification.getPacketChain().get(1).getPacket();
42         assertEquals(ethernetPacket.getEthertype(), KnownEtherType.Ipv4);
43         assertNull(ethernetPacket.getEthernetLength());
44         assertNull(ethernetPacket.getHeader8021q());
45         assertEquals(ethernetPacket.getDestinationMac().getValue(), "01:23:45:67:89:ab");
46         assertEquals(ethernetPacket.getSourceMac().getValue(), "cd:ef:01:23:45:67");
47         assertEquals(14, ethernetPacket.getPayloadOffset().intValue());
48         assertEquals(14, ethernetPacket.getPayloadLength().intValue());
49         assertEquals(2566839482L, ethernetPacket.getCrc().longValue());
50         assertTrue(Arrays.equals(packet, notification.getPayload()));
51     }
52
53     @Test
54     public void testDecode_Length() throws Exception {
55         byte[] packet = {
56             0x01, 0x23, 0x45, 0x67, (byte) 0x89, (byte) 0xab,
57             (byte) 0xcd, (byte) 0xef, 0x01, 0x23, 0x45, 0x67,
58             0x00, 0x0e,
59             0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22,
60             0x00, (byte)0xfe, (byte)0xdc, (byte)0xba
61         };
62         NotificationProviderService mock = Mockito.mock(NotificationProviderService.class);
63         EthernetPacketReceived notification = new EthernetDecoder(mock)
64                 .decode(new PacketReceivedBuilder().setPayload(packet).setMatch(new MatchBuilder().build()).build());
65         EthernetPacket ethernetPacket = (EthernetPacket) notification.getPacketChain().get(1).getPacket();
66         assertNull(ethernetPacket.getEthertype());
67         assertEquals(14, ethernetPacket.getEthernetLength().intValue());
68         assertNull(ethernetPacket.getHeader8021q());
69         assertEquals("01:23:45:67:89:ab", ethernetPacket.getDestinationMac().getValue());
70         assertEquals("cd:ef:01:23:45:67", ethernetPacket.getSourceMac().getValue());
71         assertEquals(14, ethernetPacket.getPayloadOffset().intValue());
72         assertEquals(13, ethernetPacket.getPayloadLength().intValue());
73         assertEquals(16702650L, ethernetPacket.getCrc().longValue());
74         assertTrue(Arrays.equals(packet, notification.getPayload()));
75     }
76
77     @Test
78     public void testDecode_IPv6EtherTypeWith8021qHeader() throws Exception {
79         byte[] packet = {
80             0x01, 0x23, 0x45, 0x67, (byte) 0x89, (byte) 0xab,
81             (byte) 0xcd, (byte) 0xef, 0x01, 0x23, 0x45, 0x67,
82             (byte) 0x81, 0x00,
83             (byte) 0xff, (byte) 0xff,
84             (byte) 0x86, (byte) 0xdd,
85             0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
86             0x00, (byte)0x00, (byte)0xdc, (byte)0xba
87         };
88         NotificationProviderService mock = Mockito.mock(NotificationProviderService.class);
89         EthernetPacketReceived notification = new EthernetDecoder(mock)
90                 .decode(new PacketReceivedBuilder().setPayload(packet).setMatch(new MatchBuilder().build()).build());
91         EthernetPacket ethernetPacket = (EthernetPacket) notification.getPacketChain().get(1).getPacket();
92         assertEquals(ethernetPacket.getEthertype(), KnownEtherType.Ipv6);
93         assertNull(ethernetPacket.getEthernetLength());
94         assertEquals(1, ethernetPacket.getHeader8021q().size());
95         assertEquals(Header8021qType.VlanTagged, ethernetPacket.getHeader8021q().get(0).getTPID());
96         assertEquals(7, ethernetPacket.getHeader8021q().get(0).getPriorityCode().intValue());
97         assertTrue(ethernetPacket.getHeader8021q().get(0).isDropEligible());
98         assertEquals(4095, ethernetPacket.getHeader8021q().get(0).getVlan().getValue().intValue());
99         assertEquals("01:23:45:67:89:ab", ethernetPacket.getDestinationMac().getValue());
100         assertEquals("cd:ef:01:23:45:67", ethernetPacket.getSourceMac().getValue());
101         assertEquals(18, ethernetPacket.getPayloadOffset().intValue());
102         assertEquals(8, ethernetPacket.getPayloadLength().intValue());
103         assertEquals(56506L, ethernetPacket.getCrc().longValue());
104         assertTrue(Arrays.equals(packet, notification.getPayload()));
105     }
106
107     @Test
108     public void testDecode_IPv6EtherTypeWithQinQ() throws Exception {
109         byte[] packet = {
110             0x01, 0x23, 0x45, 0x67, (byte) 0x89, (byte) 0xab,
111             (byte) 0xcd, (byte) 0xef, 0x01, 0x23, 0x45, 0x67,
112             (byte) 0x91, 0x00,
113             (byte) 0xff, (byte) 0xff,
114             (byte) 0x81, 0x00,
115             (byte) 0xa0, (byte) 0x0a,
116             (byte) 0x86, (byte) 0xdd,
117             0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11,
118             (byte)0x0a, (byte)0x0b, (byte)0x0c, (byte)0x0d
119         };
120         NotificationProviderService mock = Mockito.mock(NotificationProviderService.class);
121         EthernetPacketReceived notification = new EthernetDecoder(mock)
122                 .decode(new PacketReceivedBuilder().setPayload(packet).setMatch(new MatchBuilder().build()).build());
123         EthernetPacket ethernetPacket = (EthernetPacket) notification.getPacketChain().get(1).getPacket();
124         assertEquals(ethernetPacket.getEthertype(), KnownEtherType.Ipv6);
125         assertNull(ethernetPacket.getEthernetLength());
126         assertEquals(2, ethernetPacket.getHeader8021q().size());
127         assertEquals(Header8021qType.QInQ, ethernetPacket.getHeader8021q().get(0).getTPID());
128         assertEquals(7, ethernetPacket.getHeader8021q().get(0).getPriorityCode().intValue());
129         assertTrue(ethernetPacket.getHeader8021q().get(0).isDropEligible());
130         assertEquals(4095, ethernetPacket.getHeader8021q().get(0).getVlan().getValue().intValue());
131         assertEquals(Header8021qType.VlanTagged, ethernetPacket.getHeader8021q().get(1).getTPID());
132         assertEquals(5, ethernetPacket.getHeader8021q().get(1).getPriorityCode().intValue());
133         assertFalse(ethernetPacket.getHeader8021q().get(1).isDropEligible());
134         assertEquals(10, ethernetPacket.getHeader8021q().get(1).getVlan().getValue().intValue());
135         assertEquals("01:23:45:67:89:ab", ethernetPacket.getDestinationMac().getValue());
136         assertEquals("cd:ef:01:23:45:67", ethernetPacket.getSourceMac().getValue());
137         assertEquals(22, ethernetPacket.getPayloadOffset().intValue());
138         assertEquals(14, ethernetPacket.getPayloadLength().intValue());
139         assertEquals(168496141L, ethernetPacket.getCrc().longValue());
140         assertTrue(Arrays.equals(packet, notification.getPayload()));
141     }
142
143     // This test is from a Mininet VM, taken from a wireshark dump
144     @Test
145     public void testDecode_Ipv6Udp() throws Exception {
146         byte[] packet = {
147             // Ethernet start
148             0x33, 0x33, 0x00, 0x00, 0x00, (byte)0xfb, (byte)0xa2, (byte)0xe6, (byte)0xda, 0x67, (byte)0xef, (byte)0x95,
149             (byte)0x86, (byte)0xdd,
150             // IPv6 packet start
151             0x60, 0x00, 0x00, 0x00, 0x00, 0x35, 0x11, (byte)0xff, (byte)0xfe, (byte)0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte)0xa0, (byte)0xe6,
152             (byte)0xda, (byte)0xff, (byte)0xfe, 0x67, (byte)0xef, (byte)0x95, (byte)0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
153             0x00, 0x00, 0x00, 0x00, 0x00, (byte)0xfb,
154             // UDP start
155             0x14, (byte)0xe9, 0x14, (byte)0xe9, 0x00, 0x35, 0x6b, (byte)0xd4, 0x00, 0x00,
156             0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x5f, 0x69, 0x70, 0x70, 0x73,
157             0x04, 0x5f, 0x74, 0x63, 0x70, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x00, 0x00, 0x0c, 0x00, 0x01,
158             0x04, 0x5f, 0x69, 0x70, 0x70, (byte)0xc0, 0x12, 0x00, 0x0c, 0x00, 0x01
159         };
160         NotificationProviderService mock = Mockito.mock(NotificationProviderService.class);
161         EthernetPacketReceived notification = new EthernetDecoder(mock)
162                 .decode(new PacketReceivedBuilder().setPayload(packet).setMatch(new MatchBuilder().build()).build());
163         EthernetPacket ethernetPacket = (EthernetPacket) notification.getPacketChain().get(1).getPacket();
164         assertEquals(ethernetPacket.getEthertype(), KnownEtherType.Ipv6);
165         assertNull(ethernetPacket.getEthernetLength());
166         assertNull(ethernetPacket.getHeader8021q());
167         assertEquals("33:33:00:00:00:fb", ethernetPacket.getDestinationMac().getValue());
168         assertEquals("a2:e6:da:67:ef:95", ethernetPacket.getSourceMac().getValue());
169         assertEquals(14, ethernetPacket.getPayloadOffset().intValue());
170         // Wirehshark didn't include a CRC, so not testing for length & crc
171         // fields
172         assertTrue(Arrays.equals(packet, notification.getPayload()));
173     }
174 }