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 org.junit.Assert;
16 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());
486 public void testFragment() throws Exception {
487 byte[] payload1 = new byte[0];
489 (byte)0x61, (byte)0xd1, (byte)0x3d, (byte)0x51,
490 (byte)0x1b, (byte)0x75, (byte)0xa7, (byte)0x83,
493 (byte)0xe7, (byte)0x0f, (byte)0x2d, (byte)0x7e,
494 (byte)0x15, (byte)0xba, (byte)0xe7, (byte)0x6d,
495 (byte)0xb5, (byte)0xc5, (byte)0xb5, (byte)0x37,
496 (byte)0x59, (byte)0xbc, (byte)0x91, (byte)0x43,
497 (byte)0xb5, (byte)0xb7, (byte)0xe4, (byte)0x28,
498 (byte)0xec, (byte)0x62, (byte)0x6b, (byte)0x6a,
499 (byte)0xd1, (byte)0xcb, (byte)0x79, (byte)0x1e,
500 (byte)0xfc, (byte)0x82, (byte)0xf5, (byte)0xb4,
503 // Ensure that the payload is not deserialized if the fragment offset
505 byte proto = IPProtocols.TCP.byteValue();
506 fragmentTest(payload1, proto, (short)0xf250);
507 fragmentTest(payload2, proto, (short)0xf248);
508 fragmentTest(payload3, proto, (short)0xf230);
510 proto = IPProtocols.UDP.byteValue();
511 fragmentTest(payload1, proto, (short)0xf245);
512 fragmentTest(payload2, proto, (short)0xf23d);
513 fragmentTest(payload3, proto, (short)0xf225);
515 proto = IPProtocols.ICMP.byteValue();
516 fragmentTest(payload1, proto, (short)0xf255);
517 fragmentTest(payload2, proto, (short)0xf24d);
518 fragmentTest(payload3, proto, (short)0xf235);
520 // Ensure that the protocol header in the first fragment is
522 proto = IPProtocols.TCP.byteValue();
524 tcp.setSourcePort((short)1234).setDestinationPort((short)32000).
525 setSequenceNumber((int)0xd541f5f8).setAckNumber((int)0x58da787d).
526 setDataOffset((byte)5).setReserved((byte)0).
527 setHeaderLenFlags((short)0x18).setWindowSize((short)0x40e8).
528 setUrgentPointer((short)0x15f7).setChecksum((short)0x0d4e);
529 firstFragmentTest(tcp, payload1, proto, (short)0xdfe6);
530 tcp.setChecksum((short)0xab2a);
531 firstFragmentTest(tcp, payload2, proto, (short)0xdfde);
532 tcp.setChecksum((short)0x1c75);
533 firstFragmentTest(tcp, payload3, proto, (short)0xdfc6);
535 proto = IPProtocols.UDP.byteValue();
537 udp.setSourcePort((short)53).setDestinationPort((short)45383).
538 setLength((short)(payload1.length + 8)).setChecksum((short)0);
539 firstFragmentTest(udp, payload1, proto, (short)0xdfe7);
540 udp.setLength((short)(payload2.length + 8));
541 firstFragmentTest(udp, payload2, proto, (short)0xdfdf);
542 udp.setLength((short)(payload3.length + 8));
543 firstFragmentTest(udp, payload3, proto, (short)0xdfc7);
545 proto = IPProtocols.ICMP.byteValue();
546 ICMP icmp = new ICMP();
547 icmp.setType((byte)8).setCode((byte)0).setIdentifier((short)0x3d1e).
548 setSequenceNumber((short)1);
549 firstFragmentTest(icmp, payload1, proto, (short)0xdff7);
550 firstFragmentTest(icmp, payload2, proto, (short)0xdfef);
551 firstFragmentTest(icmp, payload3, proto, (short)0xdfd7);
554 private void fragmentTest(byte[] payload, byte proto, short checksum)
556 // Construct a fragmented raw IPv4 packet.
558 byte[] rawIp = new byte[ipv4Len + payload.length];
563 byte tos = (byte)((dscp << 2) | ecn);
564 short totalLen = (short)rawIp.length;
568 byte[] srcIp = {(byte)0x0a, (byte)0x00, (byte)0x00, (byte)0x01};
569 byte[] dstIp = {(byte)0xc0, (byte)0xa9, (byte)0x66, (byte)0x23};
571 rawIp[0] = (byte)((ipVersion << 4) | (ipv4Len >> 2));
573 rawIp[2] = (byte)(totalLen >>> Byte.SIZE);
574 rawIp[3] = (byte)totalLen;
575 rawIp[4] = (byte)(id >>> Byte.SIZE);
577 rawIp[6] = (byte)(offset >>> Byte.SIZE);
578 rawIp[7] = (byte)offset;
581 rawIp[10] = (byte)(checksum >>> Byte.SIZE);
582 rawIp[11] = (byte)checksum;
583 System.arraycopy(srcIp, 0, rawIp, 12, srcIp.length);
584 System.arraycopy(dstIp, 0, rawIp, 16, srcIp.length);
585 System.arraycopy(payload, 0, rawIp, ipv4Len, payload.length);
588 IPv4 ipv4 = new IPv4();
589 ipv4.deserialize(rawIp, 0, rawIp.length * Byte.SIZE);
591 Assert.assertEquals(ipVersion, ipv4.getVersion());
592 Assert.assertEquals(ipv4Len, ipv4.getHeaderLen());
593 Assert.assertEquals(dscp, ipv4.getDiffServ());
594 Assert.assertEquals(ecn, ipv4.getECN());
595 Assert.assertEquals(totalLen, ipv4.getTotalLength());
596 Assert.assertEquals(id, ipv4.getIdentification());
597 Assert.assertEquals((byte)0, ipv4.getFlags());
598 Assert.assertEquals(offset, ipv4.getFragmentOffset());
599 Assert.assertEquals(ttl, ipv4.getTtl());
600 Assert.assertEquals(proto, ipv4.getProtocol());
601 Assert.assertEquals(checksum, ipv4.getChecksum());
602 Assert.assertEquals(NetUtils.byteArray4ToInt(srcIp),
603 ipv4.getSourceAddress());
604 Assert.assertEquals(NetUtils.byteArray4ToInt(dstIp),
605 ipv4.getDestinationAddress());
606 Assert.assertFalse(ipv4.isCorrupted());
608 // payloadClass should not be set if fragment offset is not zero.
609 Assert.assertEquals(null, ipv4.getPayload());
610 Assert.assertArrayEquals(payload, ipv4.getRawPayload());
613 private void firstFragmentTest(Packet payload, byte[] rawPayload,
614 byte proto, short checksum)
616 // Construct a raw IPv4 packet with MF flag.
618 payload.setRawPayload(rawPayload);
619 byte[] payloadBytes = payload.serialize();
620 byte[] rawIp = new byte[ipv4Len + payloadBytes.length];
625 byte tos = (byte)((dscp << 2) | ecn);
626 short totalLen = (short)rawIp.length;
630 short off = (short)(((short)flags << 13) | offset);
632 byte[] srcIp = {(byte)0xac, (byte)0x23, (byte)0x5b, (byte)0xfd};
633 byte[] dstIp = {(byte)0xc0, (byte)0xa8, (byte)0x64, (byte)0x71};
635 rawIp[0] = (byte)((ipVersion << 4) | (ipv4Len >> 2));
637 rawIp[2] = (byte)(totalLen >>> Byte.SIZE);
638 rawIp[3] = (byte)totalLen;
639 rawIp[4] = (byte)(id >>> Byte.SIZE);
641 rawIp[6] = (byte)(off >>> Byte.SIZE);
642 rawIp[7] = (byte)off;
645 rawIp[10] = (byte)(checksum >>> Byte.SIZE);
646 rawIp[11] = (byte)checksum;
647 System.arraycopy(srcIp, 0, rawIp, 12, srcIp.length);
648 System.arraycopy(dstIp, 0, rawIp, 16, srcIp.length);
649 System.arraycopy(payloadBytes, 0, rawIp, ipv4Len, payloadBytes.length);
652 IPv4 ipv4 = new IPv4();
653 ipv4.deserialize(rawIp, 0, rawIp.length * Byte.SIZE);
655 Assert.assertEquals(ipVersion, ipv4.getVersion());
656 Assert.assertEquals(ipv4Len, ipv4.getHeaderLen());
657 Assert.assertEquals(dscp, ipv4.getDiffServ());
658 Assert.assertEquals(ecn, ipv4.getECN());
659 Assert.assertEquals(totalLen, ipv4.getTotalLength());
660 Assert.assertEquals(id, ipv4.getIdentification());
661 Assert.assertEquals(flags, ipv4.getFlags());
662 Assert.assertEquals(offset, ipv4.getFragmentOffset());
663 Assert.assertEquals(ttl, ipv4.getTtl());
664 Assert.assertEquals(proto, ipv4.getProtocol());
665 Assert.assertEquals(checksum, ipv4.getChecksum());
666 Assert.assertEquals(NetUtils.byteArray4ToInt(srcIp),
667 ipv4.getSourceAddress());
668 Assert.assertEquals(NetUtils.byteArray4ToInt(dstIp),
669 ipv4.getDestinationAddress());
670 Assert.assertFalse(ipv4.isCorrupted());
672 // Protocol header in the first fragment should be deserialized.
673 Assert.assertEquals(null, ipv4.getRawPayload());
675 Packet desPayload = ipv4.getPayload();
676 Assert.assertEquals(payload, desPayload);
677 Assert.assertFalse(desPayload.isCorrupted());
678 Assert.assertArrayEquals(rawPayload, desPayload.getRawPayload());