blob: 8514aa5921889a04f18deeea0b492da86af8c666 [file] [log] [blame]
Jonathan Hart2a655752015-04-07 16:46:33 -07001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
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
19import org.junit.Before;
20import org.junit.Test;
21
22import java.nio.ByteBuffer;
23
24import static org.junit.Assert.assertEquals;
25
26/**
27 * Unit tests for the ICMP class.
28 */
29public class ICMPTest {
30
31 private Deserializer<ICMP> deserializer;
32
33 private byte icmpType = ICMP.TYPE_ECHO_REQUEST;
34 private byte icmpCode = 4;
35 private short checksum = 870;
36
37 private byte[] headerBytes;
38
39 @Before
40 public void setUp() throws Exception {
41 deserializer = ICMP.deserializer();
42
43 ByteBuffer bb = ByteBuffer.allocate(ICMP.ICMP_HEADER_LENGTH);
44
45 bb.put(icmpType);
46 bb.put(icmpCode);
47 bb.putShort(checksum);
48
49 headerBytes = bb.array();
50 }
51
52 @Test
53 public void testDeserializeBadInput() throws Exception {
54 PacketTestUtils.testDeserializeBadInput(deserializer);
55 }
56
57 @Test
58 public void testDeserializeTruncated() throws Exception {
59 PacketTestUtils.testDeserializeTruncated(deserializer, headerBytes);
60 }
61
62 @Test
63 public void testDeserialize() throws Exception {
64 ICMP icmp = deserializer.deserialize(headerBytes, 0, headerBytes.length);
65
66 assertEquals(icmpType, icmp.getIcmpType());
67 assertEquals(icmpCode, icmp.getIcmpCode());
68 assertEquals(checksum, icmp.getChecksum());
69 }
70}