blob: 1a6308ebf6e9fd24315af595fc4bcf1d52430e9a [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
70 /**
71 * Tests deserialize and getters.
72 */
73 @Test
74 public void testDeserialize() {
75 ICMP6 icmp6 = new ICMP6();
76 icmp6.deserialize(bytePacket, 0, bytePacket.length);
77
78 assertThat(icmp6.getIcmpType(), is(ICMP6.ECHO_REQUEST));
79 assertThat(icmp6.getIcmpCode(), is((byte) 0x00));
80 assertThat(icmp6.getChecksum(), is((short) 0x82bc));
81 }
82
83 /**
84 * Tests comparator.
85 */
86 @Test
87 public void testEqual() {
88 ICMP6 icmp61 = new ICMP6();
89 icmp61.setIcmpType(ICMP6.ECHO_REQUEST);
90 icmp61.setIcmpCode((byte) 0);
91 icmp61.setChecksum((short) 0);
92
93 ICMP6 icmp62 = new ICMP6();
94 icmp62.setIcmpType(ICMP6.ECHO_REPLY);
95 icmp62.setIcmpCode((byte) 0);
96 icmp62.setChecksum((short) 0);
97
98 assertTrue(icmp61.equals(icmp61));
99 assertFalse(icmp61.equals(icmp62));
100 }
101}