blob: 39ddc24c8fcea1e86ec86e5c24ce2dc8db625415 [file] [log] [blame]
Charles M.C. Chan197a0122015-04-08 18:15:34 +08001/*
2 * Copyright 2014-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
17
18
19package org.onlab.packet;
20
21import org.junit.BeforeClass;
22import org.junit.Test;
23
24import static org.hamcrest.Matchers.is;
25import static org.junit.Assert.assertArrayEquals;
26import static org.junit.Assert.assertFalse;
27import static org.junit.Assert.assertThat;
28import static org.junit.Assert.assertTrue;
29
30/**
31 * Tests for class {@link ICMP6}.
32 */
33public class ICMP6Test {
34 private static final byte[] IPV6_SOURCE_ADDRESS = {
35 (byte) 0xfe, (byte) 0x80, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
36 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01
37 };
38 private static final byte[] IPV6_DESTINATION_ADDRESS = {
39 (byte) 0xfe, (byte) 0x80, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
40 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x02
41 };
42
43 private static IPv6 ipv6 = new IPv6();
44 private static byte[] bytePacket = {
45 ICMP6.ECHO_REQUEST, // type
46 (byte) 0x00, // code
47 (byte) 0x82, (byte) 0xbc, // checksum
48 };
49
50 @BeforeClass
51 public static void setUpBeforeClass() throws Exception {
52 ipv6.setSourceAddress(IPV6_SOURCE_ADDRESS);
53 ipv6.setDestinationAddress(IPV6_DESTINATION_ADDRESS);
54 ipv6.setNextHeader(IPv6.PROTOCOL_ICMP6);
55 }
56
57 /**
58 * Tests serialize and setters.
59 */
60 @Test
61 public void testSerialize() {
62 ICMP6 icmp6 = new ICMP6();
63 icmp6.setIcmpType(ICMP6.ECHO_REQUEST);
64 icmp6.setIcmpCode((byte) 0);
65 icmp6.setParent(ipv6);
66
67 assertArrayEquals(bytePacket, icmp6.serialize());
68 }
69
Jonathan Hart2a655752015-04-07 16:46:33 -070070 @Test
71 public void testDeserializeBadInput() throws Exception {
72 PacketTestUtils.testDeserializeBadInput(ICMP6.deserializer());
73 }
74
75 @Test
76 public void testDeserializeTruncated() throws Exception {
77 PacketTestUtils.testDeserializeTruncated(ICMP6.deserializer(), bytePacket);
78 }
79
Charles M.C. Chan197a0122015-04-08 18:15:34 +080080 /**
81 * Tests deserialize and getters.
82 */
83 @Test
Jonathan Hart2a655752015-04-07 16:46:33 -070084 public void testDeserialize() throws Exception {
85 ICMP6 icmp6 = ICMP6.deserializer().deserialize(bytePacket, 0, bytePacket.length);
Charles M.C. Chan197a0122015-04-08 18:15:34 +080086
87 assertThat(icmp6.getIcmpType(), is(ICMP6.ECHO_REQUEST));
88 assertThat(icmp6.getIcmpCode(), is((byte) 0x00));
89 assertThat(icmp6.getChecksum(), is((short) 0x82bc));
90 }
91
92 /**
93 * Tests comparator.
94 */
95 @Test
96 public void testEqual() {
97 ICMP6 icmp61 = new ICMP6();
98 icmp61.setIcmpType(ICMP6.ECHO_REQUEST);
99 icmp61.setIcmpCode((byte) 0);
100 icmp61.setChecksum((short) 0);
101
102 ICMP6 icmp62 = new ICMP6();
103 icmp62.setIcmpType(ICMP6.ECHO_REPLY);
104 icmp62.setIcmpCode((byte) 0);
105 icmp62.setChecksum((short) 0);
106
107 assertTrue(icmp61.equals(icmp61));
108 assertFalse(icmp61.equals(icmp62));
109 }
110}