Charles M.C. Chan | 197a012 | 2015-04-08 18:15:34 +0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 19 | package org.onlab.packet; |
| 20 | |
| 21 | import org.junit.BeforeClass; |
| 22 | import org.junit.Test; |
| 23 | |
| 24 | import static org.hamcrest.Matchers.is; |
| 25 | import static org.junit.Assert.assertArrayEquals; |
| 26 | import static org.junit.Assert.assertFalse; |
| 27 | import static org.junit.Assert.assertThat; |
| 28 | import static org.junit.Assert.assertTrue; |
| 29 | |
| 30 | /** |
| 31 | * Tests for class {@link ICMP6}. |
| 32 | */ |
| 33 | public 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 Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 70 | @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. Chan | 197a012 | 2015-04-08 18:15:34 +0800 | [diff] [blame] | 80 | /** |
| 81 | * Tests deserialize and getters. |
| 82 | */ |
| 83 | @Test |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 84 | public void testDeserialize() throws Exception { |
| 85 | ICMP6 icmp6 = ICMP6.deserializer().deserialize(bytePacket, 0, bytePacket.length); |
Charles M.C. Chan | 197a012 | 2015-04-08 18:15:34 +0800 | [diff] [blame] | 86 | |
| 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 | } |