3 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
5 * This program and the accompanying materials are made available under the
6 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7 * and is available at http://www.eclipse.org/legal/epl-v10.html
10 package org.opendaylight.controller.sal.implementation;
12 import org.junit.Assert;
13 import org.junit.Test;
14 import org.opendaylight.controller.sal.core.ConstructionException;
15 import org.opendaylight.controller.sal.implementation.internal.DataPacketService;
16 import org.opendaylight.controller.sal.packet.ARP;
17 import org.opendaylight.controller.sal.packet.Ethernet;
18 import org.opendaylight.controller.sal.packet.Packet;
19 import org.opendaylight.controller.sal.packet.RawPacket;
21 public class DataPacketServiceTest {
24 public void DataPacketServiceDecodeTest() throws ConstructionException, InstantiationException, IllegalAccessException {
26 DataPacketService dService = new DataPacketService();
27 RawPacket rawPkt = null;
29 Assert.assertTrue(dService.decodeDataPacket(rawPkt) == null);
31 byte[] data = { 10, 12, 14, 20, 55, 69, //DMAC
32 -90, -20, -100, -82, -78, -97, //SMAC
39 -90, -20, -100, -82, -78, -97, //src hw addr
40 9, 9, 9, 1, // src proto
41 0, 0, 0, 0, 0, 0, // target hw addr
42 9, 9, 9, -2 }; // target proto
44 rawPkt = new RawPacket(data);
46 Packet decodedPkt = dService.decodeDataPacket(rawPkt);
47 Class<? extends Packet> payloadClass = ARP.class;
48 Assert.assertTrue(payloadClass == decodedPkt.getPayload().getClass());
50 ARP arpPacket = (ARP) decodedPkt.getPayload();
52 Assert.assertTrue(arpPacket.getHardwareType() == (byte)0x1);
53 Assert.assertTrue(arpPacket.getProtocolType() == 2048);
54 Assert.assertTrue(arpPacket.getHardwareAddressLength() == (byte)0x6);
55 Assert.assertTrue(arpPacket.getProtocolAddressLength() == (byte)0x4);
56 Assert.assertTrue(arpPacket.getOpCode() == 1);
58 byte[] senderHwAddress = arpPacket.getSenderHardwareAddress();
59 byte[] senderProtocolAddress = arpPacket.getSenderProtocolAddress();
61 byte[] targetHwAddress = arpPacket.getTargetHardwareAddress();
62 byte[] targetProtocolAddress = arpPacket.getTargetProtocolAddress();
65 Assert.assertTrue(senderHwAddress[0] == (byte)0xA6);
66 Assert.assertTrue(senderHwAddress[1] == (byte)0xEC);
67 Assert.assertTrue(senderHwAddress[2] == (byte)0x9C);
68 Assert.assertTrue(senderHwAddress[3] == (byte)0xAE);
69 Assert.assertTrue(senderHwAddress[4] == (byte)0xB2);
70 Assert.assertTrue(senderHwAddress[5] == (byte)0x9F);
72 Assert.assertTrue(senderProtocolAddress[0] == (byte)0x9);
73 Assert.assertTrue(senderProtocolAddress[1] == (byte)0x9);
74 Assert.assertTrue(senderProtocolAddress[2] == (byte)0x9);
75 Assert.assertTrue(senderProtocolAddress[3] == (byte)0x1);
77 Assert.assertTrue(targetHwAddress[0] == (byte)0x0);
78 Assert.assertTrue(targetHwAddress[1] == (byte)0x0);
79 Assert.assertTrue(targetHwAddress[2] == (byte)0x0);
80 Assert.assertTrue(targetHwAddress[3] == (byte)0x0);
81 Assert.assertTrue(targetHwAddress[4] == (byte)0x0);
82 Assert.assertTrue(targetHwAddress[5] == (byte)0x0);
84 Assert.assertTrue(senderProtocolAddress[0] == (byte)0x9);
85 Assert.assertTrue(senderProtocolAddress[1] == (byte)0x9);
86 Assert.assertTrue(senderProtocolAddress[2] == (byte)0x9);
87 Assert.assertTrue(senderProtocolAddress[3] == (byte)0x1);
89 Assert.assertTrue(targetProtocolAddress[0] == (byte)0x9);
90 Assert.assertTrue(targetProtocolAddress[1] == (byte)0x9);
91 Assert.assertTrue(targetProtocolAddress[2] == (byte)0x9);
92 Assert.assertTrue(targetProtocolAddress[3] == (byte)0xFE);
96 public void DataPacketServiceEncodeTest() throws ConstructionException, InstantiationException, IllegalAccessException {
98 DataPacketService dService = new DataPacketService();
99 Ethernet eth = new Ethernet();
106 byte[] dMAC = { 10, 12, 14, 20, 55, 69 };
107 byte[] sMAC = { 82, 97, 109, 117, 127, -50 };
108 short etherType = 2054;
110 eth.setDestinationMACAddress(dMAC);
111 eth.setSourceMACAddress(sMAC);
112 eth.setEtherType(etherType);
114 arp.setHardwareType((short)1);
115 arp.setProtocolType((short)2048);
116 arp.setHardwareAddressLength((byte)0x6);
117 arp.setProtocolAddressLength((byte)0x4);
118 arp.setOpCode((byte)0x1);
120 byte[] senderHardwareAddress = {(byte)0xA6, (byte)0xEC, (byte)0x9C, (byte)0xAE,
121 (byte)0xB2, (byte)0x9F};
122 byte[] senderProtocolAddress = {(byte)0x09, (byte)0x09, (byte)0x09, (byte)0x01};
123 byte[] targetProtocolAddress = {(byte)0x09, (byte)0x09, (byte)0x09, (byte)0xFE};
124 byte[] targetHardwareAddress = {(byte)0x0, (byte)0x0, (byte)0x0, (byte)0x0, (byte)0x0, (byte)0x0};
125 arp.setSenderHardwareAddress(senderHardwareAddress);
126 arp.setSenderProtocolAddress(senderProtocolAddress);
127 arp.setTargetHardwareAddress(targetHardwareAddress);
128 arp.setTargetProtocolAddress(targetProtocolAddress);
133 rawPkt = dService.encodeDataPacket(eth);
134 data = rawPkt.getPacketData();
136 Assert.assertTrue(data[0] == (byte)0x0A);//Destination MAC
137 Assert.assertTrue(data[1] == (byte)0x0C);
138 Assert.assertTrue(data[2] == (byte)0x0E);
139 Assert.assertTrue(data[3] == (byte)0x14);
140 Assert.assertTrue(data[4] == (byte)0x37);
141 Assert.assertTrue(data[5] == (byte)0x45);
142 Assert.assertTrue(data[6] == (byte)0x52);//Source MAC
143 Assert.assertTrue(data[7] == (byte)0x61);
144 Assert.assertTrue(data[8] == (byte)0x6D);
145 Assert.assertTrue(data[9] == (byte)0x75);
146 Assert.assertTrue(data[10] == (byte)0x7F);
147 Assert.assertTrue(data[11] == (byte)0xCE);
148 Assert.assertTrue(data[12] == (byte)0x08);//EtherType
149 Assert.assertTrue(data[13] == (byte)0x06);
150 Assert.assertTrue(data[14] == (byte)0x00);//Hardware Type
151 Assert.assertTrue(data[15] == (byte)0x01);
152 Assert.assertTrue(data[16] == (byte)0x08);//Protocol Type
153 Assert.assertTrue(data[17] == (byte)0x0);
154 Assert.assertTrue(data[18] == (byte)0x6);//Hardware Address Length
155 Assert.assertTrue(data[19] == (byte)0x4);//Protocol Address Length
156 Assert.assertTrue(data[20] == (byte)0x0);//Opcode
157 Assert.assertTrue(data[21] == (byte)0x1);//Opcode
158 Assert.assertTrue(data[22] == (byte)0xA6);//Sender Hardware Address
159 Assert.assertTrue(data[23] == (byte)0xEC);
160 Assert.assertTrue(data[24] == (byte)0x9C);
161 Assert.assertTrue(data[25] == (byte)0xAE);
162 Assert.assertTrue(data[26] == (byte)0xB2);
163 Assert.assertTrue(data[27] == (byte)0x9F);
164 Assert.assertTrue(data[28] == (byte)0x09);//Sender Protocol Address
165 Assert.assertTrue(data[29] == (byte)0x09);
166 Assert.assertTrue(data[30] == (byte)0x09);
167 Assert.assertTrue(data[31] == (byte)0x01);//Target Hardware Address
168 Assert.assertTrue(data[32] == (byte)0x00);
169 Assert.assertTrue(data[33] == (byte)0x00);
170 Assert.assertTrue(data[34] == (byte)0x00);
171 Assert.assertTrue(data[35] == (byte)0x00);
172 Assert.assertTrue(data[36] == (byte)0x00);
173 Assert.assertTrue(data[37] == (byte)0x00);
174 Assert.assertTrue(data[38] == (byte)0x09);//Target Protocol Address
175 Assert.assertTrue(data[39] == (byte)0x09);
176 Assert.assertTrue(data[40] == (byte)0x09);
177 Assert.assertTrue(data[41] == (byte)0xFE);