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; |
Charles Chan | 4b87389 | 2017-05-25 18:30:09 -0700 | [diff] [blame] | 22 | import org.onlab.packet.Ethernet; |
| 23 | import org.onlab.packet.ICMP6; |
| 24 | import org.onlab.packet.IPv6; |
| 25 | import org.onlab.packet.Ip6Address; |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 26 | import org.onlab.packet.MacAddress; |
Charles Chan | 3599d63 | 2015-09-05 14:47:51 +0800 | [diff] [blame] | 27 | import org.onlab.packet.PacketTestUtils; |
Charles Chan | 4b87389 | 2017-05-25 18:30:09 -0700 | [diff] [blame] | 28 | import org.onlab.packet.VlanId; |
Charles Chan | 3599d63 | 2015-09-05 14:47:51 +0800 | [diff] [blame] | 29 | |
| 30 | import java.nio.ByteBuffer; |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 31 | |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 32 | import static org.hamcrest.Matchers.is; |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 33 | import static org.junit.Assert.*; |
Charles Chan | 4b87389 | 2017-05-25 18:30:09 -0700 | [diff] [blame] | 34 | import static org.onlab.packet.ndp.NeighborDiscoveryOptions.TYPE_SOURCE_LL_ADDRESS; |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 35 | |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 36 | /** |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 37 | * Tests for class {@link NeighborSolicitation}. |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 38 | */ |
| 39 | public class NeighborSolicitationTest { |
Charles Chan | 4b87389 | 2017-05-25 18:30:09 -0700 | [diff] [blame] | 40 | private static final Ip6Address TARGET_IP = Ip6Address.valueOf("2000::1"); |
| 41 | private static final Ip6Address SRC_IP = Ip6Address.valueOf("2000::f"); |
| 42 | private static final Ip6Address DST_IP = Ip6Address.valueOf("2000::1"); |
| 43 | private static final MacAddress SRC_MAC = MacAddress.valueOf("00:00:00:00:00:0f"); |
| 44 | private static final MacAddress DST_MAC = MacAddress.valueOf("00:00:00:00:00:01"); |
| 45 | private static final VlanId VLAN_ID = VlanId.NONE; |
| 46 | |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 47 | private static final byte[] TARGET_ADDRESS = { |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 48 | (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, |
| 49 | (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15, |
| 50 | (byte) 0xca, (byte) 0x2a, (byte) 0x14, (byte) 0xff, |
| 51 | (byte) 0xfe, (byte) 0x35, (byte) 0x26, (byte) 0xce |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 52 | }; |
| 53 | private static final byte[] TARGET_ADDRESS2 = { |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 54 | (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, |
| 55 | (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15, |
| 56 | (byte) 0xe6, (byte) 0xce, (byte) 0x8f, (byte) 0xff, |
| 57 | (byte) 0xfe, (byte) 0x54, (byte) 0x37, (byte) 0xc8 |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 58 | }; |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 59 | private static final MacAddress MAC_ADDRESS = |
| 60 | MacAddress.valueOf("11:22:33:44:55:66"); |
| 61 | |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 62 | private static byte[] bytePacket; |
| 63 | |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 64 | private Deserializer<NeighborSolicitation> deserializer |
| 65 | = NeighborSolicitation.deserializer(); |
| 66 | |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 67 | @BeforeClass |
| 68 | public static void setUpBeforeClass() throws Exception { |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 69 | byte[] byteHeader = { |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 70 | (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, |
| 71 | (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, |
| 72 | (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15, |
| 73 | (byte) 0xca, (byte) 0x2a, (byte) 0x14, (byte) 0xff, |
| 74 | (byte) 0xfe, (byte) 0x35, (byte) 0x26, (byte) 0xce, |
| 75 | (byte) 0x02, (byte) 0x01, (byte) 0x11, (byte) 0x22, |
| 76 | (byte) 0x33, (byte) 0x44, (byte) 0x55, (byte) 0x66 |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 77 | }; |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 78 | bytePacket = new byte[byteHeader.length]; |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 79 | System.arraycopy(byteHeader, 0, bytePacket, 0, byteHeader.length); |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Tests serialize and setters. |
| 84 | */ |
| 85 | @Test |
| 86 | public void testSerialize() { |
| 87 | NeighborSolicitation ns = new NeighborSolicitation(); |
| 88 | ns.setTargetAddress(TARGET_ADDRESS); |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 89 | ns.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS, |
| 90 | MAC_ADDRESS.toBytes()); |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 91 | |
| 92 | assertArrayEquals(ns.serialize(), bytePacket); |
| 93 | } |
| 94 | |
Charles Chan | 3599d63 | 2015-09-05 14:47:51 +0800 | [diff] [blame] | 95 | @Test |
| 96 | public void testDeserializeBadInput() throws Exception { |
| 97 | PacketTestUtils.testDeserializeBadInput(NeighborSolicitation.deserializer()); |
| 98 | } |
| 99 | |
| 100 | @Test |
| 101 | public void testDeserializeTruncated() throws Exception { |
| 102 | // Run the truncation test only on the NeighborSolicitation header |
| 103 | byte[] nsHeader = new byte[NeighborSolicitation.HEADER_LENGTH]; |
| 104 | ByteBuffer.wrap(bytePacket).get(nsHeader); |
| 105 | |
| 106 | PacketTestUtils.testDeserializeTruncated(NeighborSolicitation.deserializer(), nsHeader); |
| 107 | } |
| 108 | |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 109 | /** |
| 110 | * Tests deserialize and getters. |
| 111 | */ |
| 112 | @Test |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 113 | public void testDeserialize() throws DeserializationException { |
| 114 | NeighborSolicitation ns = deserializer.deserialize(bytePacket, 0, bytePacket.length); |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 115 | |
| 116 | assertArrayEquals(ns.getTargetAddress(), TARGET_ADDRESS); |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 117 | |
| 118 | // Check the option(s) |
| 119 | assertThat(ns.getOptions().size(), is(1)); |
| 120 | NeighborDiscoveryOptions.Option option = ns.getOptions().get(0); |
| 121 | assertThat(option.type(), |
| 122 | is(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS)); |
| 123 | assertArrayEquals(option.data(), MAC_ADDRESS.toBytes()); |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Tests comparator. |
| 128 | */ |
| 129 | @Test |
| 130 | public void testEqual() { |
| 131 | NeighborSolicitation ns1 = new NeighborSolicitation(); |
| 132 | ns1.setTargetAddress(TARGET_ADDRESS); |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 133 | ns1.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS, |
| 134 | MAC_ADDRESS.toBytes()); |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 135 | |
| 136 | NeighborSolicitation ns2 = new NeighborSolicitation(); |
| 137 | ns2.setTargetAddress(TARGET_ADDRESS2); |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 138 | ns2.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS, |
| 139 | MAC_ADDRESS.toBytes()); |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 140 | |
| 141 | assertTrue(ns1.equals(ns1)); |
| 142 | assertFalse(ns1.equals(ns2)); |
| 143 | } |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 144 | |
| 145 | /** |
| 146 | * Tests toString. |
| 147 | */ |
| 148 | @Test |
| 149 | public void testToStringNS() throws Exception { |
| 150 | NeighborSolicitation ns = deserializer.deserialize(bytePacket, 0, bytePacket.length); |
| 151 | String str = ns.toString(); |
| 152 | |
| 153 | // TODO: need to handle TARGET_ADDRESS and Options |
| 154 | } |
Charles Chan | 4b87389 | 2017-05-25 18:30:09 -0700 | [diff] [blame] | 155 | |
| 156 | /** |
| 157 | * Tests regular non-DAD neighbor solicitation. |
| 158 | */ |
| 159 | @Test |
| 160 | public void testBuildNdpSolicit() throws Exception { |
Bartlomiej Goluszka | 08537a8 | 2017-06-12 15:18:32 +0200 | [diff] [blame] | 161 | final Ethernet ethPacket = NeighborSolicitation.buildNdpSolicit(TARGET_IP, |
| 162 | SRC_IP, DST_IP, SRC_MAC, DST_MAC, VLAN_ID); |
| 163 | |
| 164 | assertTrue(ethPacket.getDestinationMAC().equals(DST_MAC)); |
| 165 | assertTrue(ethPacket.getSourceMAC().equals(SRC_MAC)); |
| 166 | assertTrue(ethPacket.getEtherType() == Ethernet.TYPE_IPV6); |
| 167 | assertTrue(ethPacket.getVlanID() == VLAN_ID.id()); |
| 168 | |
| 169 | final IPv6 ipPacket = (IPv6) ethPacket.getPayload(); |
| 170 | |
| 171 | assertArrayEquals(ipPacket.getSourceAddress(), SRC_IP.toOctets()); |
| 172 | assertArrayEquals(ipPacket.getDestinationAddress(), DST_IP.toOctets()); |
| 173 | |
| 174 | final ICMP6 icmp6Packet = (ICMP6) ipPacket.getPayload(); |
| 175 | final NeighborSolicitation nsPacket = (NeighborSolicitation) icmp6Packet.getPayload(); |
| 176 | |
| 177 | assertArrayEquals(nsPacket.getTargetAddress(), TARGET_IP.toOctets()); |
Charles Chan | 4b87389 | 2017-05-25 18:30:09 -0700 | [diff] [blame] | 178 | |
| 179 | assertEquals("Non-DAD NS should have 1 option", 1, nsPacket.getOptions().size()); |
| 180 | assertEquals("The option should be SRC_LL_ADDR type", TYPE_SOURCE_LL_ADDRESS, |
| 181 | nsPacket.getOptions().stream().findFirst().get().type()); |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Tests DAD neighbor solicitation. |
| 186 | * Source IP should be all-zero. |
| 187 | */ |
| 188 | @Test |
| 189 | public void testBuildNdpSolicitDad() throws Exception { |
Bartlomiej Goluszka | 08537a8 | 2017-06-12 15:18:32 +0200 | [diff] [blame] | 190 | Ethernet ethPacket = NeighborSolicitation.buildNdpSolicit(TARGET_IP, |
| 191 | Ip6Address.ZERO, DST_IP, SRC_MAC, DST_MAC, VLAN_ID); |
Charles Chan | 4b87389 | 2017-05-25 18:30:09 -0700 | [diff] [blame] | 192 | IPv6 ipPacket = (IPv6) ethPacket.getPayload(); |
| 193 | ICMP6 icmp6Packet = (ICMP6) ipPacket.getPayload(); |
| 194 | NeighborSolicitation nsPacket = (NeighborSolicitation) icmp6Packet.getPayload(); |
| 195 | |
| 196 | assertEquals("DAD NS should have no option", 0, nsPacket.getOptions().size()); |
| 197 | } |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 198 | } |