2 * Copyright (c) 2013-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
9 package org.opendaylight.controller.sal.packet;
11 import java.net.InetAddress;
12 import java.net.UnknownHostException;
13 import java.util.Arrays;
15 import junit.framework.Assert;
17 import org.junit.Test;
18 import org.opendaylight.controller.sal.match.Match;
19 import org.opendaylight.controller.sal.match.MatchType;
20 import org.opendaylight.controller.sal.utils.EtherTypes;
21 import org.opendaylight.controller.sal.utils.IPProtocols;
22 import org.opendaylight.controller.sal.utils.NetUtils;
24 public class IPv4Test {
27 public void testGetVersion() {
29 byte[] ipVersion = { (byte) 4 };
30 ip.hdrFieldsMap.put("Version", ipVersion);
31 byte version = ip.getVersion();
32 Assert.assertTrue(version == (byte) 4);
36 public void testGetHeaderLength() {
38 byte[] ipHeaderLength = { 5 };
39 ip.hdrFieldsMap.put("HeaderLength", ipHeaderLength);
40 byte headerLength = (byte) ip.getHeaderLen();
41 Assert.assertTrue(headerLength == 20);
45 public void testGetDiffServ() {
47 byte[] ipDiffServ = { 20 };
48 ip.hdrFieldsMap.put("DiffServ", ipDiffServ);
49 byte diffServ = ip.getDiffServ();
50 Assert.assertTrue(diffServ == 20);
54 public void testGetTotalLength() {
56 byte[] iptotLength = { 3, -24 };
57 ip.hdrFieldsMap.put("TotalLength", iptotLength);
58 short totalLength = ip.getTotalLength();
59 Assert.assertTrue(totalLength == 1000);
63 public void testGetIdentification() {
65 byte[] ipIdentification = { 7, -48 };
66 ip.hdrFieldsMap.put("Identification", ipIdentification);
67 short identification = ip.getIdentification();
68 Assert.assertTrue(identification == 2000);
72 public void testGetFlags() {
74 byte[] ipFlags = { 7 };
75 ip.hdrFieldsMap.put("Flags", ipFlags);
76 byte flags = ip.getFlags();
77 Assert.assertTrue(flags == 7);
81 public void testGetTtl() {
83 byte[] ipTtl = { 100 };
84 ip.hdrFieldsMap.put("TTL", ipTtl);
85 byte ttl = ip.getTtl();
86 Assert.assertTrue(ttl == 100);
90 public void testGetProtocol() {
92 byte[] ipProtocol = { 1 };
93 ip.hdrFieldsMap.put("Protocol", ipProtocol);
94 byte protocol = ip.getProtocol();
95 Assert.assertTrue(protocol == 1);
97 Class<? extends Packet> clazz = IPv4.protocolClassMap.get(protocol);
98 Assert.assertTrue(clazz == ICMP.class);
102 public void testGetFragmentOffset() {
103 IPv4 ip = new IPv4();
104 byte[] ipFragmentOffset = { 6, -35 };
105 ip.hdrFieldsMap.put("FragmentOffset", ipFragmentOffset);
106 short fragmentOffset = ip.getFragmentOffset();
107 Assert.assertTrue(fragmentOffset == 1757);
111 public void testGetSourceAddress() {
112 IPv4 ip = new IPv4();
113 byte[] ipSourceAddress = { 10, 110, 31, 55 };
114 ip.hdrFieldsMap.put("SourceIPAddress", ipSourceAddress);
115 int sourceAddress = ip.getSourceAddress();
116 Assert.assertTrue(sourceAddress == 174989111);
120 public void testGetDestinationAddress() {
121 IPv4 ip = new IPv4();
122 byte[] ipDestinationAddress = { 20, 55, 62, 110 };
123 ip.hdrFieldsMap.put("DestinationIPAddress", ipDestinationAddress);
124 int destinationAddress = ip.getDestinationAddress();
125 Assert.assertTrue(destinationAddress == 339164782);
129 public void testSetVersion() {
130 IPv4 ip = new IPv4();
131 byte ipVersion = (byte) 4;
132 ip.setVersion(ipVersion);
133 byte[] version = ip.hdrFieldsMap.get("Version");
134 Assert.assertTrue(version[0] == (byte) 4);
138 public void testSetHeaderLength() {
139 IPv4 ip = new IPv4();
140 byte ipHeaderLength = 5;
141 ip.setHeaderLength(ipHeaderLength);
142 byte[] headerLength = ip.hdrFieldsMap.get("HeaderLength");
143 Assert.assertTrue(headerLength[0] == 5);
147 public void testSetDiffServ() {
148 IPv4 ip = new IPv4();
149 byte ipDiffServ = 20;
150 ip.setDiffServ(ipDiffServ);
151 byte[] diffServ = ip.hdrFieldsMap.get("DiffServ");
152 Assert.assertTrue(diffServ[0] == 20);
156 public void testSetTotalLength() {
157 IPv4 ip = new IPv4();
158 short iptotLength = 1000;
159 ip.setTotalLength(iptotLength);
160 byte[] totalLength = ip.hdrFieldsMap.get("TotalLength");
161 Assert.assertTrue(totalLength[0] == 3);
162 Assert.assertTrue(totalLength[1] == -24);
164 ip.setTotalLength((short)84);
165 totalLength = ip.hdrFieldsMap.get("TotalLength");
166 Assert.assertTrue(totalLength[0] == 0);
167 Assert.assertTrue(totalLength[1] == 84);
171 public void testSetIdentification() {
172 IPv4 ip = new IPv4();
173 short ipIdentification = 2000;
174 ip.setIdentification(ipIdentification);
175 byte[] identification = ip.hdrFieldsMap.get("Identification");
176 Assert.assertTrue(identification[0] == 7);
177 Assert.assertTrue(identification[1] == -48);
181 public void testSetFlags() {
182 IPv4 ip = new IPv4();
184 ip.setFlags(ipFlags);
185 byte[] flags = ip.hdrFieldsMap.get("Flags");
186 Assert.assertTrue(flags[0] == 7);
190 public void testSetTtl() {
191 IPv4 ip = new IPv4();
194 byte[] ttl = ip.hdrFieldsMap.get("TTL");
195 Assert.assertTrue(ttl[0] == 100);
199 public void testSetProtocol() {
200 IPv4 ip = new IPv4();
201 byte ipProtocol = 11;
202 ip.setProtocol(ipProtocol);
203 byte[] protocol = ip.hdrFieldsMap.get("Protocol");
204 Assert.assertTrue(protocol[0] == 11);
208 public void testSetFragmentOffset() {
209 IPv4 ip = new IPv4();
210 short ipFragmentOffset = 1757;
211 ip.setFragmentOffset(ipFragmentOffset);
212 byte[] fragmentOffset = ip.hdrFieldsMap.get("FragmentOffset");
213 Assert.assertTrue(fragmentOffset[0] == 6);
214 Assert.assertTrue(fragmentOffset[1] == -35);
218 public void testSetDestinationAddress() {
219 IPv4 ip = new IPv4();
220 int ipDestinationAddress = 339164782;
221 ip.setDestinationAddress(ipDestinationAddress);
222 byte[] destinationAddress = ip.hdrFieldsMap.get("DestinationIPAddress");
223 Assert.assertTrue(destinationAddress[0] == 20);
224 Assert.assertTrue(destinationAddress[1] == 55);
225 Assert.assertTrue(destinationAddress[2] == 62);
226 Assert.assertTrue(destinationAddress[3] == 110);
230 public void testOptions() throws Exception {
231 IPv4 ip = new IPv4();
232 Assert.assertEquals(20, ip.getHeaderLen());
233 Assert.assertEquals(160, ip.getHeaderSize());
234 Assert.assertEquals(0, ip.getfieldnumBits("Options"));
241 (byte)0x01, (byte)0x02,
244 (byte)0x01, (byte)0x02, (byte)0x03,
247 (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04,
251 (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04,
255 (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04,
256 (byte)0x05, (byte)0x06,
259 (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04,
260 (byte)0x05, (byte)0x06, (byte)0x07,
263 (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04,
264 (byte)0x05, (byte)0x06, (byte)0x07, (byte)0x08,
269 byte[][] expected = {
271 (byte)0x01, (byte)0x00, (byte)0x00, (byte)0x00,
274 (byte)0x01, (byte)0x02, (byte)0x00, (byte)0x00,
277 (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x00,
280 (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04,
284 (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04,
285 (byte)0x05, (byte)0x00, (byte)0x00, (byte)0x00,
288 (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04,
289 (byte)0x05, (byte)0x06, (byte)0x00, (byte)0x00,
292 (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04,
293 (byte)0x05, (byte)0x06, (byte)0x07, (byte)0x00,
296 (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04,
297 (byte)0x05, (byte)0x06, (byte)0x07, (byte)0x08,
303 (byte)0x11, (byte)0x22, (byte)0x33, (byte)0x44,
304 (byte)0x55, (byte)0x66, (byte)0x77, (byte)0x88,
305 (byte)0x99, (byte)0xaa,
307 ICMP icmp = new ICMP();
308 icmp.setType((byte)8);
309 icmp.setCode((byte)0);
310 icmp.setIdentifier((short)0xabcd);
311 icmp.setSequenceNumber((short)7777);
312 icmp.setRawPayload(echo);
314 ip.setSourceAddress(InetAddress.getByName("192.168.10.20"));
315 ip.setDestinationAddress(InetAddress.getByName("192.168.30.40"));
316 ip.setProtocol(IPProtocols.ICMP.byteValue());
318 for (int i = 0; i < options.length; i++) {
319 byte[] opts = options[i];
320 byte[] exp = expected[i];
332 Assert.assertTrue(Arrays.equals(exp, ip.getOptions()));
333 Assert.assertEquals(hlen, ip.getHeaderLen());
334 Assert.assertEquals(hlen * 8, ip.getHeaderSize());
335 Assert.assertEquals(optlen * 8, ip.getfieldnumBits("Options"));
337 // Serialize/Deserialize test.
340 byte[] raw = ip.serialize();
341 IPv4 newip = new IPv4();
342 newip.deserialize(raw, 0, raw.length * 8);
343 Assert.assertEquals(ip, newip);
344 Assert.assertEquals(icmp, newip.getPayload());
345 Assert.assertTrue(Arrays.equals(exp, newip.getOptions()));
350 public void testChecksum() {
351 byte header[] = { (byte) 0x45, 00, 00, (byte) 0x3c, (byte) 0x1c,
352 (byte) 0x46, (byte) 0x40, 00, (byte) 0x40, 06, (byte) 0xb1,
353 (byte) 0xe6, (byte) 0xac, (byte) 0x10, (byte) 0x0a,
354 (byte) 0x63, (byte) 0xac, (byte) 0x10, (byte) 0x0a, (byte) 0x0c };
355 byte header2[] = { (byte) 0x45, 00, 00, (byte) 0x73, 00, 00,
356 (byte) 0x40, 00, (byte) 0x40, (byte) 0x11, (byte) 0xb8,
357 (byte) 0x61, (byte) 0xc0, (byte) 0xa8, 00, 01, (byte) 0xc0,
358 (byte) 0xa8, 00, (byte) 0xc7 };
359 byte header3[] = { (byte) 0x45, 00, 00, (byte) 0x47, (byte) 0x73,
360 (byte) 0x88, (byte) 0x40, 00, (byte) 0x40, 06, (byte) 0xA2,
361 (byte) 0xC4, (byte) 0x83, (byte) 0x9F, (byte) 0x0E,
362 (byte) 0x85, (byte) 0x83, (byte) 0x9F, (byte) 0x0E, (byte) 0xA1 };
363 byte header4[] = { (byte) 0x45, 00, 00, (byte) 0x54, 00, 00,
364 (byte) 0x40, 00, (byte) 0x40, 01, (byte) 0xf0, (byte) 0x8e,
365 (byte) 0xc0, (byte) 0xa8, (byte) 0x64, (byte) 0x65,
366 (byte) 0xc0, (byte) 0xa8, (byte) 0x64, (byte) 0x64 };
367 byte header5[] = { (byte) 0x45, 00, 00, (byte) 0x54, 00, 00,
368 (byte) 0x40, 00, (byte) 0x40, 01, (byte) 0xef, (byte) 0x8d,
369 (byte) 0xc0, (byte) 0xa8, (byte) 0x64, (byte) 0x65,
370 (byte) 0xc0, (byte) 0xa8, (byte) 0x65, (byte) 0x65 };
371 byte header6[] = { (byte) 0x45, 00, 00, (byte) 0x54, 00, 00,
372 (byte) 0x40, 00, (byte) 0x40, 01, (byte) 0x0b, (byte) 0x92,
373 (byte) 0xc0, (byte) 0xa8, (byte) 0x64, (byte) 0x65, (byte) 0x9,
374 (byte) 0x9, (byte) 0x1, (byte) 0x1 };
375 byte header7[] = { (byte) 0x45, 00, 00, (byte) 0x54, 00, 00,
376 (byte) 0x40, 00, (byte) 0x40, 01, (byte) 0, (byte) 0,
377 (byte) 0xc0, (byte) 0xa8, (byte) 0x64, (byte) 0x65, (byte) 0x9,
378 (byte) 0x9, (byte) 0x2, (byte) 0x2 };
380 IPv4 ip = new IPv4();
382 Assert.assertTrue(NetUtils.getUnsignedShort(ip.computeChecksum(header,
384 Assert.assertTrue(NetUtils.getUnsignedShort(ip.computeChecksum(header2,
386 Assert.assertTrue(NetUtils.getUnsignedShort(ip.computeChecksum(header3,
388 Assert.assertTrue(NetUtils.getUnsignedShort(ip.computeChecksum(header4,
390 Assert.assertTrue(NetUtils.getUnsignedShort(ip.computeChecksum(header5,
392 Assert.assertTrue(NetUtils.getUnsignedShort(ip.computeChecksum(header6,
394 Assert.assertTrue(NetUtils.getUnsignedShort(ip.computeChecksum(header7,
399 public void testFullIP() throws UnknownHostException, PacketException {
400 byte[] icmpRawPayload = new byte[] { (byte) 0x38, (byte) 0x26,
401 (byte) 0x9e, (byte) 0x51, (byte) 0x00, (byte) 0x00,
402 (byte) 0x00, (byte) 0x00, (byte) 0x2e, (byte) 0x6a,
403 (byte) 0x08, (byte) 0x00, (byte) 0x00, (byte) 0x00,
404 (byte) 0x00, (byte) 0x00, (byte) 0x10, (byte) 0x11,
405 (byte) 0x12, (byte) 0x13, (byte) 0x14, (byte) 0x15,
406 (byte) 0x16, (byte) 0x17, (byte) 0x18, (byte) 0x19,
407 (byte) 0x1a, (byte) 0x1b, (byte) 0x1c, (byte) 0x1d,
408 (byte) 0x1e, (byte) 0x1f, (byte) 0x20, (byte) 0x21,
409 (byte) 0x22, (byte) 0x23, (byte) 0x24, (byte) 0x25,
410 (byte) 0x26, (byte) 0x27, (byte) 0x28, (byte) 0x29,
411 (byte) 0x2a, (byte) 0x2b, (byte) 0x2c, (byte) 0x2d,
412 (byte) 0x2e, (byte) 0x2f, (byte) 0x30, (byte) 0x31,
413 (byte) 0x32, (byte) 0x33, (byte) 0x34, (byte) 0x35,
414 (byte) 0x36, (byte) 0x37 };
415 ICMP icmp = new ICMP();
416 icmp.setType((byte) 8);
417 icmp.setCode((byte) 0);
418 icmp.setIdentifier((short) 0x46f5);
419 icmp.setSequenceNumber((short) 2);
420 icmp.setRawPayload(icmpRawPayload);
422 IPv4 ip = new IPv4();
423 ip.setVersion((byte) 4);
424 ip.setIdentification((short) 5);
425 ip.setDiffServ((byte) 0);
427 ip.setTotalLength((short) 84);
428 ip.setFlags((byte) 2);
429 ip.setFragmentOffset((short) 0);
430 ip.setTtl((byte) 64);
431 ip.setProtocol(IPProtocols.ICMP.byteValue());
432 ip.setDestinationAddress(InetAddress.getByName("192.168.100.100"));
433 ip.setSourceAddress(InetAddress.getByName("192.168.100.101"));
436 Ethernet eth = new Ethernet();
437 eth.setDestinationMACAddress(new byte[] { (byte) 0x98, (byte) 0xfc,
438 (byte) 0x11, (byte) 0x93, (byte) 0x5c, (byte) 0xb8 });
439 eth.setSourceMACAddress(new byte[] { (byte) 0x00, (byte) 0x24,
440 (byte) 0xd7, (byte) 0xa9, (byte) 0xa3, (byte) 0x50 });
441 eth.setEtherType(EtherTypes.IPv4.shortValue());
444 byte[] stream = eth.serialize();
446 Ethernet decEth = new Ethernet();
447 decEth.deserialize(stream, 0, stream.length * NetUtils.NumBitsInAByte);
449 IPv4 decIp = (IPv4) decEth.getPayload();
450 Assert.assertFalse(decIp.isCorrupted());
451 Assert.assertTrue(ip.equals(decIp));
453 ICMP decIcmp = (ICMP) decIp.getPayload();
454 Assert.assertFalse(decIcmp.isCorrupted());
455 Assert.assertTrue(Arrays.equals(icmpRawPayload, decIcmp.getRawPayload()));
459 public void testGetMatch() throws Exception {
460 IPv4 ip = new IPv4();
461 InetAddress sourceAddress = InetAddress.getByName("172.168.190.15");
462 InetAddress destintationAddress = InetAddress.getByName("23.128.0.11");
463 byte protocol = IPProtocols.TCP.byteValue();
465 ip.setVersion((byte) 4);
466 ip.setIdentification((short) 5);
469 ip.setTotalLength((short) 84);
470 ip.setFlags((byte) 2);
471 ip.setFragmentOffset((short) 0);
472 ip.setTtl((byte) 64);
473 ip.setProtocol(protocol);
474 ip.setDestinationAddress(destintationAddress);
475 ip.setSourceAddress(sourceAddress);
477 Match match = ip.getMatch();
479 Assert.assertEquals(sourceAddress, match.getField(MatchType.NW_SRC).getValue());
480 Assert.assertEquals(destintationAddress, match.getField(MatchType.NW_DST).getValue());
481 Assert.assertEquals(protocol, (byte) match.getField(MatchType.NW_PROTO).getValue());
482 Assert.assertEquals(tos, (byte) match.getField(MatchType.NW_TOS).getValue());