blob: c6afe60fa88e12ad9fbba102f2131052702828bf [file] [log] [blame]
Jonathan Hart2a655752015-04-07 16:46:33 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Jonathan Hart2a655752015-04-07 16:46:33 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onlab.packet;
18
Jian Li5fc14292015-12-04 11:30:46 -080019import org.apache.commons.lang3.StringUtils;
Jonathan Hart2a655752015-04-07 16:46:33 -070020import org.junit.Before;
21import org.junit.Test;
22
23import java.nio.ByteBuffer;
24
25import static org.junit.Assert.assertEquals;
Jian Li5fc14292015-12-04 11:30:46 -080026import static org.junit.Assert.assertTrue;
Jonathan Hart2a655752015-04-07 16:46:33 -070027
28/**
29 * Unit tests for the ICMP class.
30 */
31public class ICMPTest {
32
33 private Deserializer<ICMP> deserializer;
34
35 private byte icmpType = ICMP.TYPE_ECHO_REQUEST;
36 private byte icmpCode = 4;
37 private short checksum = 870;
38
39 private byte[] headerBytes;
40
41 @Before
42 public void setUp() throws Exception {
Daniel Parkd27dcda2018-08-07 01:40:40 +090043 ICMPEcho icmpEcho = new ICMPEcho();
44 icmpEcho.setIdentifier((short) 0)
45 .setSequenceNum((short) 0);
46
47 ByteBuffer byteBufferIcmpEcho = ByteBuffer.wrap(icmpEcho.serialize());
48
Jonathan Hart2a655752015-04-07 16:46:33 -070049 deserializer = ICMP.deserializer();
50
Daniel Parkd27dcda2018-08-07 01:40:40 +090051 ByteBuffer bb = ByteBuffer.allocate(ICMP.ICMP_HEADER_LENGTH + ICMPEcho.ICMP_ECHO_HEADER_LENGTH);
Jonathan Hart2a655752015-04-07 16:46:33 -070052
53 bb.put(icmpType);
54 bb.put(icmpCode);
55 bb.putShort(checksum);
Daniel Parkd27dcda2018-08-07 01:40:40 +090056 bb.put(byteBufferIcmpEcho);
Jonathan Hart2a655752015-04-07 16:46:33 -070057
58 headerBytes = bb.array();
59 }
60
61 @Test
62 public void testDeserializeBadInput() throws Exception {
63 PacketTestUtils.testDeserializeBadInput(deserializer);
64 }
65
66 @Test
67 public void testDeserializeTruncated() throws Exception {
68 PacketTestUtils.testDeserializeTruncated(deserializer, headerBytes);
69 }
70
Jian Li5fc14292015-12-04 11:30:46 -080071 /**
72 * Tests deserialize and getters.
73 */
Jonathan Hart2a655752015-04-07 16:46:33 -070074 @Test
75 public void testDeserialize() throws Exception {
76 ICMP icmp = deserializer.deserialize(headerBytes, 0, headerBytes.length);
77
78 assertEquals(icmpType, icmp.getIcmpType());
79 assertEquals(icmpCode, icmp.getIcmpCode());
80 assertEquals(checksum, icmp.getChecksum());
81 }
Jian Li5fc14292015-12-04 11:30:46 -080082
83 /**
84 * Tests toString.
85 */
86 @Test
87 public void testToStringIcmp() throws Exception {
88 ICMP icmp = deserializer.deserialize(headerBytes, 0, headerBytes.length);
89 String str = icmp.toString();
90
91 assertTrue(StringUtils.contains(str, "icmpType=" + icmpType));
92 assertTrue(StringUtils.contains(str, "icmpCode=" + icmpCode));
93 assertTrue(StringUtils.contains(str, "checksum=" + checksum));
94 }
Jonathan Hart2a655752015-04-07 16:46:33 -070095}