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