ICMP fix and Packet class should store and provide access to the raw payload in case...
[controller.git] / opendaylight / sal / api / src / test / java / org / opendaylight / controller / sal / packet / ICMPTest.java
1
2 /*
3  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
4  *
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
8  */
9
10 package org.opendaylight.controller.sal.packet;
11
12 import junit.framework.Assert;
13
14 import org.junit.Test;
15
16 public class ICMPTest {
17
18     @Test
19     public void testSetTypeCode() {
20         ICMP icmp = new ICMP();
21         byte icmpType = 2;
22         icmp.setType(icmpType);
23         byte[] typeCode = icmp.hdrFieldsMap.get("Type");
24         Assert.assertTrue(typeCode[0] == 2);
25
26     }
27
28     @Test
29     public void testSetChecksum() {
30         ICMP icmp = new ICMP();
31         short icmpChecksum = 200;
32         icmp.setChecksum(icmpChecksum);
33         byte[] checksum = icmp.hdrFieldsMap.get("Checksum");
34         Assert.assertTrue(checksum[0] == 0);
35         Assert.assertTrue(checksum[1] == -56);
36
37     }
38
39     @Test
40     public void testSetIdentifier() {
41         ICMP icmp = new ICMP();
42         short icmpIdentifier = 1201;
43         icmp.setIdentifier(icmpIdentifier);
44         byte[] identifier = icmp.hdrFieldsMap.get("Identifier");
45         Assert.assertTrue(identifier[0] == 4);
46         Assert.assertTrue(identifier[1] == -79);
47
48     }
49
50     @Test
51     public void testSetSequenceNumber() {
52         ICMP icmp = new ICMP();
53         short icmpSequenceNumber = 5000;
54         icmp.setSequenceNumber(icmpSequenceNumber);
55         byte[] sequenceNumber = icmp.hdrFieldsMap.get("SequenceNumber");
56         Assert.assertTrue(sequenceNumber[0] == 19);
57         Assert.assertTrue(sequenceNumber[1] == -120);
58
59     }
60
61     @Test
62     public void testSerialization() throws PacketException {
63         byte icmpRawPayload[] = { (byte) 0x38, (byte) 0x26, (byte) 0x9e,
64                 (byte) 0x51, (byte) 0x00, (byte) 0x00, (byte) 0x00,
65                 (byte) 0x00, (byte) 0x2e, (byte) 0x6a, (byte) 0x08,
66                 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
67                 (byte) 0x00, (byte) 0x10, (byte) 0x11, (byte) 0x12,
68                 (byte) 0x13, (byte) 0x14, (byte) 0x15, (byte) 0x16,
69                 (byte) 0x17, (byte) 0x18, (byte) 0x19, (byte) 0x1a,
70                 (byte) 0x1b, (byte) 0x1c, (byte) 0x1d, (byte) 0x1e,
71                 (byte) 0x1f, (byte) 0x20, (byte) 0x21, (byte) 0x22,
72                 (byte) 0x23, (byte) 0x24, (byte) 0x25, (byte) 0x26,
73                 (byte) 0x27, (byte) 0x28, (byte) 0x29, (byte) 0x2a,
74                 (byte) 0x2b, (byte) 0x2c, (byte) 0x2d, (byte) 0x2e,
75                 (byte) 0x2f, (byte) 0x30, (byte) 0x31, (byte) 0x32,
76                 (byte) 0x33, (byte) 0x34, (byte) 0x35, (byte) 0x36, (byte) 0x37 };
77
78         short checksum = (short)0xe553;
79
80         // Create ICMP object
81         ICMP icmp = new ICMP();
82         icmp.setType((byte)8);
83         icmp.setCode((byte)0);
84         icmp.setIdentifier((short) 0x46f5);
85         icmp.setSequenceNumber((short) 2);
86         icmp.setRawPayload(icmpRawPayload);
87         //icmp.setChecksum(checksum);
88
89         // Serialize
90         byte[] stream = icmp.serialize();
91         Assert.assertTrue(stream.length == 64);
92
93         // Deserialize
94         ICMP icmpDes = new ICMP();
95         icmpDes.deserialize(stream, 0, stream.length);
96
97         Assert.assertFalse(icmpDes.isCorrupted());
98         Assert.assertTrue(icmpDes.getChecksum() == checksum);
99         Assert.assertTrue(icmp.equals(icmpDes));
100     }
101 }