2 * Copyright (c) 2013 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.utils.EtherTypes;
19 import org.opendaylight.controller.sal.utils.IPProtocols;
20 import org.opendaylight.controller.sal.utils.NetUtils;
22 public class IPv4Test {
25 public void testGetVersion() {
27 byte[] ipVersion = { (byte) 4 };
28 ip.hdrFieldsMap.put("Version", ipVersion);
29 byte version = ip.getVersion();
30 Assert.assertTrue(version == (byte) 4);
34 public void testGetHeaderLength() {
36 byte[] ipHeaderLength = { 5 };
37 ip.hdrFieldsMap.put("HeaderLength", ipHeaderLength);
38 byte headerLength = (byte) ip.getHeaderLen();
39 Assert.assertTrue(headerLength == 20);
43 public void testGetDiffServ() {
45 byte[] ipDiffServ = { 20 };
46 ip.hdrFieldsMap.put("DiffServ", ipDiffServ);
47 byte diffServ = ip.getDiffServ();
48 Assert.assertTrue(diffServ == 20);
52 public void testGetTotalLength() {
54 byte[] iptotLength = { 3, -24 };
55 ip.hdrFieldsMap.put("TotalLength", iptotLength);
56 short totalLength = ip.getTotalLength();
57 Assert.assertTrue(totalLength == 1000);
61 public void testGetIdentification() {
63 byte[] ipIdentification = { 7, -48 };
64 ip.hdrFieldsMap.put("Identification", ipIdentification);
65 short identification = ip.getIdentification();
66 Assert.assertTrue(identification == 2000);
70 public void testGetFlags() {
72 byte[] ipFlags = { 7 };
73 ip.hdrFieldsMap.put("Flags", ipFlags);
74 byte flags = ip.getFlags();
75 Assert.assertTrue(flags == 7);
79 public void testGetTtl() {
81 byte[] ipTtl = { 100 };
82 ip.hdrFieldsMap.put("TTL", ipTtl);
83 byte ttl = ip.getTtl();
84 Assert.assertTrue(ttl == 100);
88 public void testGetProtocol() {
90 byte[] ipProtocol = { 1 };
91 ip.hdrFieldsMap.put("Protocol", ipProtocol);
92 byte protocol = ip.getProtocol();
93 Assert.assertTrue(protocol == 1);
95 Class<? extends Packet> clazz = IPv4.protocolClassMap.get(protocol);
96 Assert.assertTrue(clazz == ICMP.class);
100 public void testGetFragmentOffset() {
101 IPv4 ip = new IPv4();
102 byte[] ipFragmentOffset = { 6, -35 };
103 ip.hdrFieldsMap.put("FragmentOffset", ipFragmentOffset);
104 short fragmentOffset = ip.getFragmentOffset();
105 Assert.assertTrue(fragmentOffset == 1757);
109 public void testGetSourceAddress() {
110 IPv4 ip = new IPv4();
111 byte[] ipSourceAddress = { 10, 110, 31, 55 };
112 ip.hdrFieldsMap.put("SourceIPAddress", ipSourceAddress);
113 int sourceAddress = ip.getSourceAddress();
114 Assert.assertTrue(sourceAddress == 174989111);
118 public void testGetDestinationAddress() {
119 IPv4 ip = new IPv4();
120 byte[] ipDestinationAddress = { 20, 55, 62, 110 };
121 ip.hdrFieldsMap.put("DestinationIPAddress", ipDestinationAddress);
122 int destinationAddress = ip.getDestinationAddress();
123 Assert.assertTrue(destinationAddress == 339164782);
127 public void testSetVersion() {
128 IPv4 ip = new IPv4();
129 byte ipVersion = (byte) 4;
130 ip.setVersion(ipVersion);
131 byte[] version = ip.hdrFieldsMap.get("Version");
132 Assert.assertTrue(version[0] == (byte) 4);
136 public void testSetHeaderLength() {
137 IPv4 ip = new IPv4();
138 byte ipHeaderLength = 5;
139 ip.setHeaderLength(ipHeaderLength);
140 byte[] headerLength = ip.hdrFieldsMap.get("HeaderLength");
141 Assert.assertTrue(headerLength[0] == 5);
145 public void testSetDiffServ() {
146 IPv4 ip = new IPv4();
147 byte ipDiffServ = 20;
148 ip.setDiffServ(ipDiffServ);
149 byte[] diffServ = ip.hdrFieldsMap.get("DiffServ");
150 Assert.assertTrue(diffServ[0] == 20);
154 public void testSetTotalLength() {
155 IPv4 ip = new IPv4();
156 short iptotLength = 1000;
157 ip.setTotalLength(iptotLength);
158 byte[] totalLength = ip.hdrFieldsMap.get("TotalLength");
159 Assert.assertTrue(totalLength[0] == 3);
160 Assert.assertTrue(totalLength[1] == -24);
162 ip.setTotalLength((short)84);
163 totalLength = ip.hdrFieldsMap.get("TotalLength");
164 Assert.assertTrue(totalLength[0] == 0);
165 Assert.assertTrue(totalLength[1] == 84);
169 public void testSetIdentification() {
170 IPv4 ip = new IPv4();
171 short ipIdentification = 2000;
172 ip.setIdentification(ipIdentification);
173 byte[] identification = ip.hdrFieldsMap.get("Identification");
174 Assert.assertTrue(identification[0] == 7);
175 Assert.assertTrue(identification[1] == -48);
179 public void testSetFlags() {
180 IPv4 ip = new IPv4();
182 ip.setFlags(ipFlags);
183 byte[] flags = ip.hdrFieldsMap.get("Flags");
184 Assert.assertTrue(flags[0] == 7);
188 public void testSetTtl() {
189 IPv4 ip = new IPv4();
192 byte[] ttl = ip.hdrFieldsMap.get("TTL");
193 Assert.assertTrue(ttl[0] == 100);
197 public void testSetProtocol() {
198 IPv4 ip = new IPv4();
199 byte ipProtocol = 11;
200 ip.setProtocol(ipProtocol);
201 byte[] protocol = ip.hdrFieldsMap.get("Protocol");
202 Assert.assertTrue(protocol[0] == 11);
206 public void testSetFragmentOffset() {
207 IPv4 ip = new IPv4();
208 short ipFragmentOffset = 1757;
209 ip.setFragmentOffset(ipFragmentOffset);
210 byte[] fragmentOffset = ip.hdrFieldsMap.get("FragmentOffset");
211 Assert.assertTrue(fragmentOffset[0] == 6);
212 Assert.assertTrue(fragmentOffset[1] == -35);
216 public void testSetDestinationAddress() {
217 IPv4 ip = new IPv4();
218 int ipDestinationAddress = 339164782;
219 ip.setDestinationAddress(ipDestinationAddress);
220 byte[] destinationAddress = ip.hdrFieldsMap.get("DestinationIPAddress");
221 Assert.assertTrue(destinationAddress[0] == 20);
222 Assert.assertTrue(destinationAddress[1] == 55);
223 Assert.assertTrue(destinationAddress[2] == 62);
224 Assert.assertTrue(destinationAddress[3] == 110);
228 public void testOptions() throws Exception {
229 IPv4 ip = new IPv4();
230 Assert.assertEquals(20, ip.getHeaderLen());
231 Assert.assertEquals(160, ip.getHeaderSize());
232 Assert.assertEquals(0, ip.getfieldnumBits("Options"));
239 (byte)0x01, (byte)0x02,
242 (byte)0x01, (byte)0x02, (byte)0x03,
245 (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04,
249 (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04,
253 (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04,
254 (byte)0x05, (byte)0x06,
257 (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04,
258 (byte)0x05, (byte)0x06, (byte)0x07,
261 (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04,
262 (byte)0x05, (byte)0x06, (byte)0x07, (byte)0x08,
267 byte[][] expected = {
269 (byte)0x01, (byte)0x00, (byte)0x00, (byte)0x00,
272 (byte)0x01, (byte)0x02, (byte)0x00, (byte)0x00,
275 (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x00,
278 (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04,
282 (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04,
283 (byte)0x05, (byte)0x00, (byte)0x00, (byte)0x00,
286 (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04,
287 (byte)0x05, (byte)0x06, (byte)0x00, (byte)0x00,
290 (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04,
291 (byte)0x05, (byte)0x06, (byte)0x07, (byte)0x00,
294 (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04,
295 (byte)0x05, (byte)0x06, (byte)0x07, (byte)0x08,
301 (byte)0x11, (byte)0x22, (byte)0x33, (byte)0x44,
302 (byte)0x55, (byte)0x66, (byte)0x77, (byte)0x88,
303 (byte)0x99, (byte)0xaa,
305 ICMP icmp = new ICMP();
306 icmp.setType((byte)8);
307 icmp.setCode((byte)0);
308 icmp.setIdentifier((short)0xabcd);
309 icmp.setSequenceNumber((short)7777);
310 icmp.setRawPayload(echo);
312 ip.setSourceAddress(InetAddress.getByName("192.168.10.20"));
313 ip.setDestinationAddress(InetAddress.getByName("192.168.30.40"));
314 ip.setProtocol(IPProtocols.ICMP.byteValue());
316 for (int i = 0; i < options.length; i++) {
317 byte[] opts = options[i];
318 byte[] exp = expected[i];
330 Assert.assertTrue(Arrays.equals(exp, ip.getOptions()));
331 Assert.assertEquals(hlen, ip.getHeaderLen());
332 Assert.assertEquals(hlen * 8, ip.getHeaderSize());
333 Assert.assertEquals(optlen * 8, ip.getfieldnumBits("Options"));
335 // Serialize/Deserialize test.
338 byte[] raw = ip.serialize();
339 IPv4 newip = new IPv4();
340 newip.deserialize(raw, 0, raw.length * 8);
341 Assert.assertEquals(ip, newip);
342 Assert.assertEquals(icmp, newip.getPayload());
343 Assert.assertTrue(Arrays.equals(exp, newip.getOptions()));
348 public void testChecksum() {
349 byte header[] = { (byte) 0x45, 00, 00, (byte) 0x3c, (byte) 0x1c,
350 (byte) 0x46, (byte) 0x40, 00, (byte) 0x40, 06, (byte) 0xb1,
351 (byte) 0xe6, (byte) 0xac, (byte) 0x10, (byte) 0x0a,
352 (byte) 0x63, (byte) 0xac, (byte) 0x10, (byte) 0x0a, (byte) 0x0c };
353 byte header2[] = { (byte) 0x45, 00, 00, (byte) 0x73, 00, 00,
354 (byte) 0x40, 00, (byte) 0x40, (byte) 0x11, (byte) 0xb8,
355 (byte) 0x61, (byte) 0xc0, (byte) 0xa8, 00, 01, (byte) 0xc0,
356 (byte) 0xa8, 00, (byte) 0xc7 };
357 byte header3[] = { (byte) 0x45, 00, 00, (byte) 0x47, (byte) 0x73,
358 (byte) 0x88, (byte) 0x40, 00, (byte) 0x40, 06, (byte) 0xA2,
359 (byte) 0xC4, (byte) 0x83, (byte) 0x9F, (byte) 0x0E,
360 (byte) 0x85, (byte) 0x83, (byte) 0x9F, (byte) 0x0E, (byte) 0xA1 };
361 byte header4[] = { (byte) 0x45, 00, 00, (byte) 0x54, 00, 00,
362 (byte) 0x40, 00, (byte) 0x40, 01, (byte) 0xf0, (byte) 0x8e,
363 (byte) 0xc0, (byte) 0xa8, (byte) 0x64, (byte) 0x65,
364 (byte) 0xc0, (byte) 0xa8, (byte) 0x64, (byte) 0x64 };
365 byte header5[] = { (byte) 0x45, 00, 00, (byte) 0x54, 00, 00,
366 (byte) 0x40, 00, (byte) 0x40, 01, (byte) 0xef, (byte) 0x8d,
367 (byte) 0xc0, (byte) 0xa8, (byte) 0x64, (byte) 0x65,
368 (byte) 0xc0, (byte) 0xa8, (byte) 0x65, (byte) 0x65 };
369 byte header6[] = { (byte) 0x45, 00, 00, (byte) 0x54, 00, 00,
370 (byte) 0x40, 00, (byte) 0x40, 01, (byte) 0x0b, (byte) 0x92,
371 (byte) 0xc0, (byte) 0xa8, (byte) 0x64, (byte) 0x65, (byte) 0x9,
372 (byte) 0x9, (byte) 0x1, (byte) 0x1 };
373 byte header7[] = { (byte) 0x45, 00, 00, (byte) 0x54, 00, 00,
374 (byte) 0x40, 00, (byte) 0x40, 01, (byte) 0, (byte) 0,
375 (byte) 0xc0, (byte) 0xa8, (byte) 0x64, (byte) 0x65, (byte) 0x9,
376 (byte) 0x9, (byte) 0x2, (byte) 0x2 };
378 IPv4 ip = new IPv4();
380 Assert.assertTrue(NetUtils.getUnsignedShort(ip.computeChecksum(header,
382 Assert.assertTrue(NetUtils.getUnsignedShort(ip.computeChecksum(header2,
384 Assert.assertTrue(NetUtils.getUnsignedShort(ip.computeChecksum(header3,
386 Assert.assertTrue(NetUtils.getUnsignedShort(ip.computeChecksum(header4,
388 Assert.assertTrue(NetUtils.getUnsignedShort(ip.computeChecksum(header5,
390 Assert.assertTrue(NetUtils.getUnsignedShort(ip.computeChecksum(header6,
392 Assert.assertTrue(NetUtils.getUnsignedShort(ip.computeChecksum(header7,
397 public void testFullIP() throws UnknownHostException, PacketException {
398 byte[] icmpRawPayload = new byte[] { (byte) 0x38, (byte) 0x26,
399 (byte) 0x9e, (byte) 0x51, (byte) 0x00, (byte) 0x00,
400 (byte) 0x00, (byte) 0x00, (byte) 0x2e, (byte) 0x6a,
401 (byte) 0x08, (byte) 0x00, (byte) 0x00, (byte) 0x00,
402 (byte) 0x00, (byte) 0x00, (byte) 0x10, (byte) 0x11,
403 (byte) 0x12, (byte) 0x13, (byte) 0x14, (byte) 0x15,
404 (byte) 0x16, (byte) 0x17, (byte) 0x18, (byte) 0x19,
405 (byte) 0x1a, (byte) 0x1b, (byte) 0x1c, (byte) 0x1d,
406 (byte) 0x1e, (byte) 0x1f, (byte) 0x20, (byte) 0x21,
407 (byte) 0x22, (byte) 0x23, (byte) 0x24, (byte) 0x25,
408 (byte) 0x26, (byte) 0x27, (byte) 0x28, (byte) 0x29,
409 (byte) 0x2a, (byte) 0x2b, (byte) 0x2c, (byte) 0x2d,
410 (byte) 0x2e, (byte) 0x2f, (byte) 0x30, (byte) 0x31,
411 (byte) 0x32, (byte) 0x33, (byte) 0x34, (byte) 0x35,
412 (byte) 0x36, (byte) 0x37 };
413 ICMP icmp = new ICMP();
414 icmp.setType((byte) 8);
415 icmp.setCode((byte) 0);
416 icmp.setIdentifier((short) 0x46f5);
417 icmp.setSequenceNumber((short) 2);
418 icmp.setRawPayload(icmpRawPayload);
420 IPv4 ip = new IPv4();
421 ip.setVersion((byte) 4);
422 ip.setIdentification((short) 5);
423 ip.setDiffServ((byte) 0);
425 ip.setTotalLength((short) 84);
426 ip.setFlags((byte) 2);
427 ip.setFragmentOffset((short) 0);
428 ip.setTtl((byte) 64);
429 ip.setProtocol(IPProtocols.ICMP.byteValue());
430 ip.setDestinationAddress(InetAddress.getByName("192.168.100.100"));
431 ip.setSourceAddress(InetAddress.getByName("192.168.100.101"));
434 Ethernet eth = new Ethernet();
435 eth.setDestinationMACAddress(new byte[] { (byte) 0x98, (byte) 0xfc,
436 (byte) 0x11, (byte) 0x93, (byte) 0x5c, (byte) 0xb8 });
437 eth.setSourceMACAddress(new byte[] { (byte) 0x00, (byte) 0x24,
438 (byte) 0xd7, (byte) 0xa9, (byte) 0xa3, (byte) 0x50 });
439 eth.setEtherType(EtherTypes.IPv4.shortValue());
442 byte[] stream = eth.serialize();
444 Ethernet decEth = new Ethernet();
445 decEth.deserialize(stream, 0, stream.length * NetUtils.NumBitsInAByte);
447 IPv4 decIp = (IPv4) decEth.getPayload();
448 Assert.assertFalse(decIp.isCorrupted());
449 Assert.assertTrue(ip.equals(decIp));
451 ICMP decIcmp = (ICMP) decIp.getPayload();
452 Assert.assertFalse(decIcmp.isCorrupted());
453 Assert.assertTrue(Arrays.equals(icmpRawPayload, decIcmp.getRawPayload()));