Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 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 | */ |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 16 | package org.onlab.packet.ndp; |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 17 | |
| 18 | import org.junit.BeforeClass; |
| 19 | import org.junit.Test; |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 20 | import org.onlab.packet.DeserializationException; |
| 21 | import org.onlab.packet.Deserializer; |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 22 | import org.onlab.packet.MacAddress; |
Charles Chan | 3599d63 | 2015-09-05 14:47:51 +0800 | [diff] [blame] | 23 | import org.onlab.packet.PacketTestUtils; |
| 24 | |
| 25 | import java.nio.ByteBuffer; |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 26 | |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 27 | import static org.hamcrest.Matchers.is; |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 28 | import static org.junit.Assert.*; |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 29 | |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 30 | /** |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 31 | * Tests for class {@link NeighborSolicitation}. |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 32 | */ |
| 33 | public class NeighborSolicitationTest { |
| 34 | private static final byte[] TARGET_ADDRESS = { |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 35 | (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, |
| 36 | (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15, |
| 37 | (byte) 0xca, (byte) 0x2a, (byte) 0x14, (byte) 0xff, |
| 38 | (byte) 0xfe, (byte) 0x35, (byte) 0x26, (byte) 0xce |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 39 | }; |
| 40 | private static final byte[] TARGET_ADDRESS2 = { |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 41 | (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, |
| 42 | (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15, |
| 43 | (byte) 0xe6, (byte) 0xce, (byte) 0x8f, (byte) 0xff, |
| 44 | (byte) 0xfe, (byte) 0x54, (byte) 0x37, (byte) 0xc8 |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 45 | }; |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 46 | private static final MacAddress MAC_ADDRESS = |
| 47 | MacAddress.valueOf("11:22:33:44:55:66"); |
| 48 | |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 49 | private static byte[] bytePacket; |
| 50 | |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 51 | private Deserializer<NeighborSolicitation> deserializer |
| 52 | = NeighborSolicitation.deserializer(); |
| 53 | |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 54 | @BeforeClass |
| 55 | public static void setUpBeforeClass() throws Exception { |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 56 | byte[] byteHeader = { |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 57 | (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, |
| 58 | (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, |
| 59 | (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15, |
| 60 | (byte) 0xca, (byte) 0x2a, (byte) 0x14, (byte) 0xff, |
| 61 | (byte) 0xfe, (byte) 0x35, (byte) 0x26, (byte) 0xce, |
| 62 | (byte) 0x02, (byte) 0x01, (byte) 0x11, (byte) 0x22, |
| 63 | (byte) 0x33, (byte) 0x44, (byte) 0x55, (byte) 0x66 |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 64 | }; |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 65 | bytePacket = new byte[byteHeader.length]; |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 66 | System.arraycopy(byteHeader, 0, bytePacket, 0, byteHeader.length); |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Tests serialize and setters. |
| 71 | */ |
| 72 | @Test |
| 73 | public void testSerialize() { |
| 74 | NeighborSolicitation ns = new NeighborSolicitation(); |
| 75 | ns.setTargetAddress(TARGET_ADDRESS); |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 76 | ns.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS, |
| 77 | MAC_ADDRESS.toBytes()); |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 78 | |
| 79 | assertArrayEquals(ns.serialize(), bytePacket); |
| 80 | } |
| 81 | |
Charles Chan | 3599d63 | 2015-09-05 14:47:51 +0800 | [diff] [blame] | 82 | @Test |
| 83 | public void testDeserializeBadInput() throws Exception { |
| 84 | PacketTestUtils.testDeserializeBadInput(NeighborSolicitation.deserializer()); |
| 85 | } |
| 86 | |
| 87 | @Test |
| 88 | public void testDeserializeTruncated() throws Exception { |
| 89 | // Run the truncation test only on the NeighborSolicitation header |
| 90 | byte[] nsHeader = new byte[NeighborSolicitation.HEADER_LENGTH]; |
| 91 | ByteBuffer.wrap(bytePacket).get(nsHeader); |
| 92 | |
| 93 | PacketTestUtils.testDeserializeTruncated(NeighborSolicitation.deserializer(), nsHeader); |
| 94 | } |
| 95 | |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 96 | /** |
| 97 | * Tests deserialize and getters. |
| 98 | */ |
| 99 | @Test |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 100 | public void testDeserialize() throws DeserializationException { |
| 101 | NeighborSolicitation ns = deserializer.deserialize(bytePacket, 0, bytePacket.length); |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 102 | |
| 103 | assertArrayEquals(ns.getTargetAddress(), TARGET_ADDRESS); |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 104 | |
| 105 | // Check the option(s) |
| 106 | assertThat(ns.getOptions().size(), is(1)); |
| 107 | NeighborDiscoveryOptions.Option option = ns.getOptions().get(0); |
| 108 | assertThat(option.type(), |
| 109 | is(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS)); |
| 110 | assertArrayEquals(option.data(), MAC_ADDRESS.toBytes()); |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Tests comparator. |
| 115 | */ |
| 116 | @Test |
| 117 | public void testEqual() { |
| 118 | NeighborSolicitation ns1 = new NeighborSolicitation(); |
| 119 | ns1.setTargetAddress(TARGET_ADDRESS); |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 120 | ns1.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS, |
| 121 | MAC_ADDRESS.toBytes()); |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 122 | |
| 123 | NeighborSolicitation ns2 = new NeighborSolicitation(); |
| 124 | ns2.setTargetAddress(TARGET_ADDRESS2); |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 125 | ns2.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS, |
| 126 | MAC_ADDRESS.toBytes()); |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 127 | |
| 128 | assertTrue(ns1.equals(ns1)); |
| 129 | assertFalse(ns1.equals(ns2)); |
| 130 | } |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 131 | |
| 132 | /** |
| 133 | * Tests toString. |
| 134 | */ |
| 135 | @Test |
| 136 | public void testToStringNS() throws Exception { |
| 137 | NeighborSolicitation ns = deserializer.deserialize(bytePacket, 0, bytePacket.length); |
| 138 | String str = ns.toString(); |
| 139 | |
| 140 | // TODO: need to handle TARGET_ADDRESS and Options |
| 141 | } |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 142 | } |