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.util.Arrays;
14 import org.junit.Assert;
16 import org.junit.Test;
18 public class ICMPTest {
21 public void testSetTypeCode() {
22 ICMP icmp = new ICMP();
24 icmp.setType(icmpType);
25 byte[] typeCode = icmp.hdrFieldsMap.get("Type");
26 Assert.assertTrue(typeCode[0] == 2);
31 public void testSetChecksum() {
32 ICMP icmp = new ICMP();
33 short icmpChecksum = 200;
34 icmp.setChecksum(icmpChecksum);
35 byte[] checksum = icmp.hdrFieldsMap.get("Checksum");
36 Assert.assertTrue(checksum[0] == 0);
37 Assert.assertTrue(checksum[1] == -56);
42 public void testSetIdentifier() {
43 ICMP icmp = new ICMP();
44 short icmpIdentifier = 1201;
45 icmp.setIdentifier(icmpIdentifier);
46 byte[] identifier = icmp.hdrFieldsMap.get("Identifier");
47 Assert.assertTrue(identifier[0] == 4);
48 Assert.assertTrue(identifier[1] == -79);
53 public void testSetSequenceNumber() {
54 ICMP icmp = new ICMP();
55 short icmpSequenceNumber = 5000;
56 icmp.setSequenceNumber(icmpSequenceNumber);
57 byte[] sequenceNumber = icmp.hdrFieldsMap.get("SequenceNumber");
58 Assert.assertTrue(sequenceNumber[0] == 19);
59 Assert.assertTrue(sequenceNumber[1] == -120);
64 public void testSerialization() throws PacketException {
65 byte icmpRawPayload[] = { (byte) 0x38, (byte) 0x26, (byte) 0x9e,
66 (byte) 0x51, (byte) 0x00, (byte) 0x00, (byte) 0x00,
67 (byte) 0x00, (byte) 0x2e, (byte) 0x6a, (byte) 0x08,
68 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
69 (byte) 0x00, (byte) 0x10, (byte) 0x11, (byte) 0x12,
70 (byte) 0x13, (byte) 0x14, (byte) 0x15, (byte) 0x16,
71 (byte) 0x17, (byte) 0x18, (byte) 0x19, (byte) 0x1a,
72 (byte) 0x1b, (byte) 0x1c, (byte) 0x1d, (byte) 0x1e,
73 (byte) 0x1f, (byte) 0x20, (byte) 0x21, (byte) 0x22,
74 (byte) 0x23, (byte) 0x24, (byte) 0x25, (byte) 0x26,
75 (byte) 0x27, (byte) 0x28, (byte) 0x29, (byte) 0x2a,
76 (byte) 0x2b, (byte) 0x2c, (byte) 0x2d, (byte) 0x2e,
77 (byte) 0x2f, (byte) 0x30, (byte) 0x31, (byte) 0x32,
78 (byte) 0x33, (byte) 0x34, (byte) 0x35, (byte) 0x36, (byte) 0x37 };
79 serializeTest(icmpRawPayload, (short)0xe553);
81 serializeTest(null, (short)0xb108);
82 serializeTest(new byte[0], (short)0xb108);
85 (byte)0xba, (byte)0xd4, (byte)0xc7, (byte)0x53,
86 (byte)0xf8, (byte)0x59, (byte)0x68, (byte)0x77,
87 (byte)0xfd, (byte)0x27, (byte)0xe0, (byte)0x5b,
88 (byte)0xd0, (byte)0x2e, (byte)0x28, (byte)0x41,
89 (byte)0xa3, (byte)0x48, (byte)0x5d, (byte)0x2e,
90 (byte)0x7d, (byte)0x5b, (byte)0xd3, (byte)0x60,
91 (byte)0xb3, (byte)0x88, (byte)0x8d, (byte)0x0f,
92 (byte)0x1d, (byte)0x87, (byte)0x51, (byte)0x0f,
93 (byte)0x6a, (byte)0xff, (byte)0xf7, (byte)0xd4,
94 (byte)0x40, (byte)0x35, (byte)0x4e, (byte)0x01,
97 serializeTest(odd, (short)0xd0ad);
99 // Large payload that causes 16-bit checksum overflow more than
101 byte[] largeEven = new byte[1024];
102 Arrays.fill(largeEven, (byte)0xff);
103 serializeTest(largeEven, (short)0xb108);
105 byte[] largeOdd = new byte[1021];
106 Arrays.fill(largeOdd, (byte)0xff);
107 serializeTest(largeOdd, (short)0xb207);
110 private void serializeTest(byte[] payload, short checksum)
111 throws PacketException {
112 ICMP icmp = new ICMP();
113 icmp.setType((byte)8).setCode((byte)0).
114 setIdentifier((short)0x46f5).setSequenceNumber((short)2);
116 if (payload != null) {
117 icmp.setRawPayload(payload);
118 payloadSize = payload.length;
122 byte[] data = icmp.serialize();
123 Assert.assertEquals(payloadSize + 8, data.length);
126 ICMP icmpDes = new ICMP();
127 icmpDes.deserialize(data, 0, data.length);
129 Assert.assertFalse(icmpDes.isCorrupted());
130 Assert.assertEquals(checksum, icmpDes.getChecksum());
131 Assert.assertEquals(icmp, icmpDes);