blob: 7e120e5ab101fbbd3ad90bdac17bf1d4cd71bce8 [file] [log] [blame]
Jonathan Hart2a655752015-04-07 16:46:33 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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 {
43 deserializer = ICMP.deserializer();
44
45 ByteBuffer bb = ByteBuffer.allocate(ICMP.ICMP_HEADER_LENGTH);
46
47 bb.put(icmpType);
48 bb.put(icmpCode);
49 bb.putShort(checksum);
50
51 headerBytes = bb.array();
52 }
53
54 @Test
55 public void testDeserializeBadInput() throws Exception {
56 PacketTestUtils.testDeserializeBadInput(deserializer);
57 }
58
59 @Test
60 public void testDeserializeTruncated() throws Exception {
61 PacketTestUtils.testDeserializeTruncated(deserializer, headerBytes);
62 }
63
Jian Li5fc14292015-12-04 11:30:46 -080064 /**
65 * Tests deserialize and getters.
66 */
Jonathan Hart2a655752015-04-07 16:46:33 -070067 @Test
68 public void testDeserialize() throws Exception {
69 ICMP icmp = deserializer.deserialize(headerBytes, 0, headerBytes.length);
70
71 assertEquals(icmpType, icmp.getIcmpType());
72 assertEquals(icmpCode, icmp.getIcmpCode());
73 assertEquals(checksum, icmp.getChecksum());
74 }
Jian Li5fc14292015-12-04 11:30:46 -080075
76 /**
77 * Tests toString.
78 */
79 @Test
80 public void testToStringIcmp() throws Exception {
81 ICMP icmp = deserializer.deserialize(headerBytes, 0, headerBytes.length);
82 String str = icmp.toString();
83
84 assertTrue(StringUtils.contains(str, "icmpType=" + icmpType));
85 assertTrue(StringUtils.contains(str, "icmpCode=" + icmpCode));
86 assertTrue(StringUtils.contains(str, "checksum=" + checksum));
87 }
Jonathan Hart2a655752015-04-07 16:46:33 -070088}