3 * Copyright (c) 2013-2014 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.packet;
12 import java.net.InetAddress;
13 import java.util.Arrays;
16 import junit.framework.Assert;
18 import org.junit.Test;
19 import org.opendaylight.controller.sal.match.Match;
20 import org.opendaylight.controller.sal.match.MatchType;
21 import org.opendaylight.controller.sal.utils.EtherTypes;
22 import org.opendaylight.controller.sal.utils.IPProtocols;
24 public class PacketTest {
27 public void testDeserialize() throws NoSuchFieldException, Exception {
28 short startOffset, numBits;
30 Ethernet eth = new Ethernet();
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
46 eth.deserialize(data, startOffset, numBits);
48 byte[] dMAC = eth.getDestinationMACAddress();
49 byte[] sMAC = eth.getSourceMACAddress();
50 short etherType = eth.getEtherType();
52 Assert.assertTrue(dMAC[0] == 10);
53 Assert.assertTrue(dMAC[1] == 12);
54 Assert.assertTrue(dMAC[2] == 14);
55 Assert.assertTrue(dMAC[3] == 20);
56 Assert.assertTrue(dMAC[4] == 55);
57 Assert.assertTrue(dMAC[5] == 69);
59 Assert.assertTrue(sMAC[0] == -90);
60 Assert.assertTrue(sMAC[1] == -20);
61 Assert.assertTrue(sMAC[2] == -100);
62 Assert.assertTrue(sMAC[3] == -82);
63 Assert.assertTrue(sMAC[4] == -78);
64 Assert.assertTrue(sMAC[5] == -97);
66 Assert.assertTrue(etherType == 0x806);
68 ARP arpPacket = (ARP) eth.getPayload();
70 Assert.assertTrue(arpPacket.getHardwareType() == (byte)0x1);
71 Assert.assertTrue(arpPacket.getProtocolType() == 2048);
72 Assert.assertTrue(arpPacket.getHardwareAddressLength() == (byte)0x6);
73 Assert.assertTrue(arpPacket.getProtocolAddressLength() == (byte)0x4);
74 Assert.assertTrue(arpPacket.getOpCode() == 1);
76 byte[] senderHwAddress = arpPacket.getSenderHardwareAddress();
77 byte[] senderProtocolAddress = arpPacket.getSenderProtocolAddress();
79 byte[] targetHwAddress = arpPacket.getTargetHardwareAddress();
80 byte[] targetProtocolAddress = arpPacket.getTargetProtocolAddress();
83 Assert.assertTrue(senderHwAddress[0] == (byte)0xA6);
84 Assert.assertTrue(senderHwAddress[1] == (byte)0xEC);
85 Assert.assertTrue(senderHwAddress[2] == (byte)0x9C);
86 Assert.assertTrue(senderHwAddress[3] == (byte)0xAE);
87 Assert.assertTrue(senderHwAddress[4] == (byte)0xB2);
88 Assert.assertTrue(senderHwAddress[5] == (byte)0x9F);
90 Assert.assertTrue(senderProtocolAddress[0] == (byte)0x9);
91 Assert.assertTrue(senderProtocolAddress[1] == (byte)0x9);
92 Assert.assertTrue(senderProtocolAddress[2] == (byte)0x9);
93 Assert.assertTrue(senderProtocolAddress[3] == (byte)0x1);
95 Assert.assertTrue(targetHwAddress[0] == (byte)0x0);
96 Assert.assertTrue(targetHwAddress[1] == (byte)0x0);
97 Assert.assertTrue(targetHwAddress[2] == (byte)0x0);
98 Assert.assertTrue(targetHwAddress[3] == (byte)0x0);
99 Assert.assertTrue(targetHwAddress[4] == (byte)0x0);
100 Assert.assertTrue(targetHwAddress[5] == (byte)0x0);
102 Assert.assertTrue(senderProtocolAddress[0] == (byte)0x9);
103 Assert.assertTrue(senderProtocolAddress[1] == (byte)0x9);
104 Assert.assertTrue(senderProtocolAddress[2] == (byte)0x9);
105 Assert.assertTrue(senderProtocolAddress[3] == (byte)0x1);
107 Assert.assertTrue(targetProtocolAddress[0] == (byte)0x9);
108 Assert.assertTrue(targetProtocolAddress[1] == (byte)0x9);
109 Assert.assertTrue(targetProtocolAddress[2] == (byte)0x9);
110 Assert.assertTrue(targetProtocolAddress[3] == (byte)0xFE);
114 public void testSerialize() throws NoSuchFieldException, Exception {
115 Ethernet eth = new Ethernet();
116 Map<String, byte[]> fCValues = eth.hdrFieldsMap;
118 byte[] dMAC = { 10, 12, 14, 20, 55, 69 };
119 byte[] sMAC = { 82, 97, 109, 117, 127, -50 };
120 short etherType = 2054;
122 byte[] dMACdata, sMACdata, etherTypedata;
123 byte[] data = new byte[20];
125 eth.setDestinationMACAddress(dMAC);
126 eth.setSourceMACAddress(sMAC);
127 eth.setEtherType(etherType);
129 dMACdata = fCValues.get("DestinationMACAddress");
130 sMACdata = fCValues.get("SourceMACAddress");
131 etherTypedata = fCValues.get("EtherType");
133 Assert.assertTrue(dMACdata[0] == 10);
134 Assert.assertTrue(dMACdata[1] == 12);
135 Assert.assertTrue(dMACdata[2] == 14);
136 Assert.assertTrue(dMACdata[3] == 20);
137 Assert.assertTrue(dMACdata[4] == 55);
138 Assert.assertTrue(dMACdata[5] == 69);
140 Assert.assertTrue(sMACdata[0] == 82);
141 Assert.assertTrue(sMACdata[1] == 97);
142 Assert.assertTrue(sMACdata[2] == 109);
143 Assert.assertTrue(sMACdata[3] == 117);
144 Assert.assertTrue(sMACdata[4] == 127);
145 Assert.assertTrue(sMACdata[5] == -50);
147 Assert.assertTrue(etherTypedata[0] == 8);
148 Assert.assertTrue(etherTypedata[1] == 6);
149 data = eth.serialize();
151 Assert.assertTrue(data[0] == 10);
152 Assert.assertTrue(data[1] == 12);
153 Assert.assertTrue(data[2] == 14);
154 Assert.assertTrue(data[3] == 20);
155 Assert.assertTrue(data[4] == 55);
156 Assert.assertTrue(data[5] == 69);
158 Assert.assertTrue(data[6] == 82);
159 Assert.assertTrue(data[7] == 97);
160 Assert.assertTrue(data[8] == 109);
161 Assert.assertTrue(data[9] == 117);
162 Assert.assertTrue(data[10] == 127);
163 Assert.assertTrue(data[11] == -50);
165 Assert.assertTrue(data[12] == 8);
166 Assert.assertTrue(data[13] == 6);
171 public void testGetMatch() throws Exception {
173 short sport = (short) 11093;
174 short dport = (short) 23;
175 tcp.setSourcePort(sport);
176 tcp.setDestinationPort(dport);
178 IPv4 ip = new IPv4();
179 InetAddress sourceAddress = InetAddress.getByName("192.168.100.100");
180 InetAddress destintationAddress = InetAddress.getByName("192.168.100.101");
181 byte protocol = IPProtocols.TCP.byteValue();
183 ip.setVersion((byte) 4);
184 ip.setIdentification((short) 5);
187 ip.setTotalLength((short) 84);
188 ip.setFlags((byte) 2);
189 ip.setFragmentOffset((short) 0);
190 ip.setTtl((byte) 64);
191 ip.setProtocol(protocol);
192 ip.setDestinationAddress(destintationAddress);
193 ip.setSourceAddress(sourceAddress);
196 IEEE8021Q dot1q = new IEEE8021Q();
199 short ethType = EtherTypes.IPv4.shortValue();
200 dot1q.setPcp(priority);
201 dot1q.setVid(vlanId);
202 dot1q.setEtherType(ethType);
203 dot1q.setPayload(ip);
205 Ethernet eth = new Ethernet();
206 byte smac[] = { (byte) 0xf0, (byte) 0xde, (byte) 0xf1, (byte) 0x71, (byte) 0x72, (byte) 0x8d };
207 byte dmac[] = { (byte) 0xde, (byte) 0x28, (byte) 0xdb, (byte) 0xb3, (byte) 0x7c, (byte) 0xf8 };
208 eth.setDestinationMACAddress(dmac);
209 eth.setSourceMACAddress(smac);
210 eth.setEtherType(EtherTypes.VLANTAGGED.shortValue());
211 eth.setPayload(dot1q);
213 Match match = eth.getMatch();
215 Assert.assertTrue(Arrays.equals(smac, (byte[]) match.getField(MatchType.DL_SRC).getValue()));
216 Assert.assertTrue(Arrays.equals(dmac, (byte[]) match.getField(MatchType.DL_DST).getValue()));
217 Assert.assertEquals(priority, (byte) match.getField(MatchType.DL_VLAN_PR).getValue());
218 Assert.assertEquals(vlanId, (short) match.getField(MatchType.DL_VLAN).getValue());
219 Assert.assertEquals(ethType, (short) match.getField(MatchType.DL_TYPE).getValue());
220 Assert.assertEquals(sourceAddress, match.getField(MatchType.NW_SRC).getValue());
221 Assert.assertEquals(destintationAddress, match.getField(MatchType.NW_DST).getValue());
222 Assert.assertEquals(protocol, (byte) match.getField(MatchType.NW_PROTO).getValue());
223 Assert.assertEquals(tos, (byte) match.getField(MatchType.NW_TOS).getValue());
224 Assert.assertEquals(sport, (short) match.getField(MatchType.TP_SRC).getValue());
225 Assert.assertEquals(dport, (short) match.getField(MatchType.TP_DST).getValue());