blob: 8616b482b24bbadc0a204758bd60dab99043d881 [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
Jian Li5fc14292015-12-04 11:30:46 -080021import org.apache.commons.lang3.StringUtils;
Charles M.C. Chan197a0122015-04-08 18:15:34 +080022import org.junit.BeforeClass;
23import org.junit.Test;
24
25import static org.hamcrest.Matchers.is;
26import static org.junit.Assert.assertArrayEquals;
27import static org.junit.Assert.assertFalse;
28import static org.junit.Assert.assertThat;
29import static org.junit.Assert.assertTrue;
30
31/**
32 * Tests for class {@link ICMP6}.
33 */
34public class ICMP6Test {
35 private static final byte[] IPV6_SOURCE_ADDRESS = {
36 (byte) 0xfe, (byte) 0x80, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
37 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01
38 };
39 private static final byte[] IPV6_DESTINATION_ADDRESS = {
40 (byte) 0xfe, (byte) 0x80, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
41 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x02
42 };
43
44 private static IPv6 ipv6 = new IPv6();
45 private static byte[] bytePacket = {
46 ICMP6.ECHO_REQUEST, // type
47 (byte) 0x00, // code
48 (byte) 0x82, (byte) 0xbc, // checksum
49 };
50
51 @BeforeClass
52 public static void setUpBeforeClass() throws Exception {
53 ipv6.setSourceAddress(IPV6_SOURCE_ADDRESS);
54 ipv6.setDestinationAddress(IPV6_DESTINATION_ADDRESS);
55 ipv6.setNextHeader(IPv6.PROTOCOL_ICMP6);
56 }
57
58 /**
59 * Tests serialize and setters.
60 */
61 @Test
62 public void testSerialize() {
63 ICMP6 icmp6 = new ICMP6();
64 icmp6.setIcmpType(ICMP6.ECHO_REQUEST);
65 icmp6.setIcmpCode((byte) 0);
66 icmp6.setParent(ipv6);
67
68 assertArrayEquals(bytePacket, icmp6.serialize());
69 }
70
Jonathan Hart2a655752015-04-07 16:46:33 -070071 @Test
72 public void testDeserializeBadInput() throws Exception {
73 PacketTestUtils.testDeserializeBadInput(ICMP6.deserializer());
74 }
75
76 @Test
77 public void testDeserializeTruncated() throws Exception {
78 PacketTestUtils.testDeserializeTruncated(ICMP6.deserializer(), bytePacket);
79 }
80
Charles M.C. Chan197a0122015-04-08 18:15:34 +080081 /**
82 * Tests deserialize and getters.
83 */
84 @Test
Jonathan Hart2a655752015-04-07 16:46:33 -070085 public void testDeserialize() throws Exception {
86 ICMP6 icmp6 = ICMP6.deserializer().deserialize(bytePacket, 0, bytePacket.length);
Charles M.C. Chan197a0122015-04-08 18:15:34 +080087
88 assertThat(icmp6.getIcmpType(), is(ICMP6.ECHO_REQUEST));
89 assertThat(icmp6.getIcmpCode(), is((byte) 0x00));
90 assertThat(icmp6.getChecksum(), is((short) 0x82bc));
91 }
92
93 /**
94 * Tests comparator.
95 */
96 @Test
97 public void testEqual() {
98 ICMP6 icmp61 = new ICMP6();
99 icmp61.setIcmpType(ICMP6.ECHO_REQUEST);
100 icmp61.setIcmpCode((byte) 0);
101 icmp61.setChecksum((short) 0);
102
103 ICMP6 icmp62 = new ICMP6();
104 icmp62.setIcmpType(ICMP6.ECHO_REPLY);
105 icmp62.setIcmpCode((byte) 0);
106 icmp62.setChecksum((short) 0);
107
108 assertTrue(icmp61.equals(icmp61));
109 assertFalse(icmp61.equals(icmp62));
110 }
Jian Li5fc14292015-12-04 11:30:46 -0800111
112 /**
113 * Tests toString.
114 */
115 @Test
116 public void testToStringIcmp6() throws Exception {
117 ICMP6 icmp6 = ICMP6.deserializer().deserialize(bytePacket, 0, bytePacket.length);
118 String str = icmp6.toString();
119
120 assertTrue(StringUtils.contains(str, "icmpType=" + ICMP6.ECHO_REQUEST));
121 assertTrue(StringUtils.contains(str, "icmpCode=" + (byte) 0x00));
122 assertTrue(StringUtils.contains(str, "checksum=" + (short) 0x82bc));
123 }
Charles M.C. Chan197a0122015-04-08 18:15:34 +0800124}