blob: e01db2794002101439428389eb5d9dddf3c30655 [file] [log] [blame]
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +08003 *
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. Chanea5aa472015-01-03 13:40:39 +080016package org.onlab.packet.ndp;
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080017
18import org.junit.BeforeClass;
19import org.junit.Test;
Jonathan Hart2a655752015-04-07 16:46:33 -070020import org.onlab.packet.DeserializationException;
21import org.onlab.packet.Deserializer;
Charles Chan4b873892017-05-25 18:30:09 -070022import org.onlab.packet.Ethernet;
23import org.onlab.packet.ICMP6;
24import org.onlab.packet.IPv6;
25import org.onlab.packet.Ip6Address;
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080026import org.onlab.packet.MacAddress;
Charles Chan3599d632015-09-05 14:47:51 +080027import org.onlab.packet.PacketTestUtils;
Charles Chan4b873892017-05-25 18:30:09 -070028import org.onlab.packet.VlanId;
Charles Chan3599d632015-09-05 14:47:51 +080029
30import java.nio.ByteBuffer;
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080031
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080032import static org.hamcrest.Matchers.is;
Jian Li5fc14292015-12-04 11:30:46 -080033import static org.junit.Assert.*;
Charles Chan4b873892017-05-25 18:30:09 -070034import static org.onlab.packet.ndp.NeighborDiscoveryOptions.TYPE_SOURCE_LL_ADDRESS;
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080035
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080036/**
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080037 * Tests for class {@link NeighborSolicitation}.
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080038 */
39public class NeighborSolicitationTest {
Charles Chan4b873892017-05-25 18:30:09 -070040 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. Chan7fee36a2014-12-31 00:19:59 +080047 private static final byte[] TARGET_ADDRESS = {
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080048 (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. Chan7fee36a2014-12-31 00:19:59 +080052 };
53 private static final byte[] TARGET_ADDRESS2 = {
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080054 (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. Chan7fee36a2014-12-31 00:19:59 +080058 };
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080059 private static final MacAddress MAC_ADDRESS =
60 MacAddress.valueOf("11:22:33:44:55:66");
61
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080062 private static byte[] bytePacket;
63
Jonathan Hart2a655752015-04-07 16:46:33 -070064 private Deserializer<NeighborSolicitation> deserializer
65 = NeighborSolicitation.deserializer();
66
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080067 @BeforeClass
68 public static void setUpBeforeClass() throws Exception {
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080069 byte[] byteHeader = {
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080070 (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. Chan7fee36a2014-12-31 00:19:59 +080077 };
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080078 bytePacket = new byte[byteHeader.length];
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080079 System.arraycopy(byteHeader, 0, bytePacket, 0, byteHeader.length);
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080080 }
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 Radoslavova2626ef2015-02-18 18:33:25 -080089 ns.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS,
90 MAC_ADDRESS.toBytes());
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080091
92 assertArrayEquals(ns.serialize(), bytePacket);
93 }
94
Charles Chan3599d632015-09-05 14:47:51 +080095 @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. Chan7fee36a2014-12-31 00:19:59 +0800109 /**
110 * Tests deserialize and getters.
111 */
112 @Test
Jonathan Hart2a655752015-04-07 16:46:33 -0700113 public void testDeserialize() throws DeserializationException {
114 NeighborSolicitation ns = deserializer.deserialize(bytePacket, 0, bytePacket.length);
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800115
116 assertArrayEquals(ns.getTargetAddress(), TARGET_ADDRESS);
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800117
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. Chan7fee36a2014-12-31 00:19:59 +0800124 }
125
126 /**
127 * Tests comparator.
128 */
129 @Test
130 public void testEqual() {
131 NeighborSolicitation ns1 = new NeighborSolicitation();
132 ns1.setTargetAddress(TARGET_ADDRESS);
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800133 ns1.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS,
134 MAC_ADDRESS.toBytes());
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800135
136 NeighborSolicitation ns2 = new NeighborSolicitation();
137 ns2.setTargetAddress(TARGET_ADDRESS2);
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800138 ns2.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS,
139 MAC_ADDRESS.toBytes());
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800140
141 assertTrue(ns1.equals(ns1));
142 assertFalse(ns1.equals(ns2));
143 }
Jian Li5fc14292015-12-04 11:30:46 -0800144
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 Chan4b873892017-05-25 18:30:09 -0700155
156 /**
157 * Tests regular non-DAD neighbor solicitation.
158 */
159 @Test
160 public void testBuildNdpSolicit() throws Exception {
161 Ethernet ethPacket = NeighborSolicitation.buildNdpSolicit(TARGET_IP.toOctets(),
162 SRC_IP.toOctets(), DST_IP.toOctets(),
163 SRC_MAC.toBytes(), DST_MAC.toBytes(), VLAN_ID);
164 IPv6 ipPacket = (IPv6) ethPacket.getPayload();
165 ICMP6 icmp6Packet = (ICMP6) ipPacket.getPayload();
166 NeighborSolicitation nsPacket = (NeighborSolicitation) icmp6Packet.getPayload();
167
168 assertEquals("Non-DAD NS should have 1 option", 1, nsPacket.getOptions().size());
169 assertEquals("The option should be SRC_LL_ADDR type", TYPE_SOURCE_LL_ADDRESS,
170 nsPacket.getOptions().stream().findFirst().get().type());
171 }
172
173 /**
174 * Tests DAD neighbor solicitation.
175 * Source IP should be all-zero.
176 */
177 @Test
178 public void testBuildNdpSolicitDad() throws Exception {
179 Ethernet ethPacket = NeighborSolicitation.buildNdpSolicit(TARGET_IP.toOctets(),
180 Ip6Address.ZERO.toOctets(), DST_IP.toOctets(),
181 SRC_MAC.toBytes(), DST_MAC.toBytes(), VLAN_ID);
182 IPv6 ipPacket = (IPv6) ethPacket.getPayload();
183 ICMP6 icmp6Packet = (ICMP6) ipPacket.getPayload();
184 NeighborSolicitation nsPacket = (NeighborSolicitation) icmp6Packet.getPayload();
185
186 assertEquals("DAD NS should have no option", 0, nsPacket.getOptions().size());
187 }
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800188}