blob: 03e57f5e1736e187cfee3180f5ab598b69efdf71 [file] [log] [blame]
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +08001/*
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -08002 * Copyright 2014-2015 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;
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080022import org.onlab.packet.MacAddress;
Charles Chan3599d632015-09-05 14:47:51 +080023import org.onlab.packet.PacketTestUtils;
24
25import java.nio.ByteBuffer;
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080026
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080027import static org.hamcrest.Matchers.is;
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080028import static org.junit.Assert.assertArrayEquals;
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080029import static org.junit.Assert.assertFalse;
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080030import static org.junit.Assert.assertThat;
31import static org.junit.Assert.assertTrue;
32
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080033/**
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080034 * Tests for class {@link NeighborSolicitation}.
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080035 */
36public class NeighborSolicitationTest {
37 private static final byte[] TARGET_ADDRESS = {
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080038 (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18,
39 (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15,
40 (byte) 0xca, (byte) 0x2a, (byte) 0x14, (byte) 0xff,
41 (byte) 0xfe, (byte) 0x35, (byte) 0x26, (byte) 0xce
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080042 };
43 private static final byte[] TARGET_ADDRESS2 = {
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080044 (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18,
45 (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15,
46 (byte) 0xe6, (byte) 0xce, (byte) 0x8f, (byte) 0xff,
47 (byte) 0xfe, (byte) 0x54, (byte) 0x37, (byte) 0xc8
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080048 };
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080049 private static final MacAddress MAC_ADDRESS =
50 MacAddress.valueOf("11:22:33:44:55:66");
51
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080052 private static byte[] bytePacket;
53
Jonathan Hart2a655752015-04-07 16:46:33 -070054 private Deserializer<NeighborSolicitation> deserializer
55 = NeighborSolicitation.deserializer();
56
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080057 @BeforeClass
58 public static void setUpBeforeClass() throws Exception {
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080059 byte[] byteHeader = {
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080060 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
61 (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18,
62 (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15,
63 (byte) 0xca, (byte) 0x2a, (byte) 0x14, (byte) 0xff,
64 (byte) 0xfe, (byte) 0x35, (byte) 0x26, (byte) 0xce,
65 (byte) 0x02, (byte) 0x01, (byte) 0x11, (byte) 0x22,
66 (byte) 0x33, (byte) 0x44, (byte) 0x55, (byte) 0x66
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080067 };
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080068 bytePacket = new byte[byteHeader.length];
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080069 System.arraycopy(byteHeader, 0, bytePacket, 0, byteHeader.length);
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080070 }
71
72 /**
73 * Tests serialize and setters.
74 */
75 @Test
76 public void testSerialize() {
77 NeighborSolicitation ns = new NeighborSolicitation();
78 ns.setTargetAddress(TARGET_ADDRESS);
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080079 ns.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS,
80 MAC_ADDRESS.toBytes());
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080081
82 assertArrayEquals(ns.serialize(), bytePacket);
83 }
84
Charles Chan3599d632015-09-05 14:47:51 +080085 @Test
86 public void testDeserializeBadInput() throws Exception {
87 PacketTestUtils.testDeserializeBadInput(NeighborSolicitation.deserializer());
88 }
89
90 @Test
91 public void testDeserializeTruncated() throws Exception {
92 // Run the truncation test only on the NeighborSolicitation header
93 byte[] nsHeader = new byte[NeighborSolicitation.HEADER_LENGTH];
94 ByteBuffer.wrap(bytePacket).get(nsHeader);
95
96 PacketTestUtils.testDeserializeTruncated(NeighborSolicitation.deserializer(), nsHeader);
97 }
98
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080099 /**
100 * Tests deserialize and getters.
101 */
102 @Test
Jonathan Hart2a655752015-04-07 16:46:33 -0700103 public void testDeserialize() throws DeserializationException {
104 NeighborSolicitation ns = deserializer.deserialize(bytePacket, 0, bytePacket.length);
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800105
106 assertArrayEquals(ns.getTargetAddress(), TARGET_ADDRESS);
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800107
108 // Check the option(s)
109 assertThat(ns.getOptions().size(), is(1));
110 NeighborDiscoveryOptions.Option option = ns.getOptions().get(0);
111 assertThat(option.type(),
112 is(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS));
113 assertArrayEquals(option.data(), MAC_ADDRESS.toBytes());
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800114 }
115
116 /**
117 * Tests comparator.
118 */
119 @Test
120 public void testEqual() {
121 NeighborSolicitation ns1 = new NeighborSolicitation();
122 ns1.setTargetAddress(TARGET_ADDRESS);
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800123 ns1.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS,
124 MAC_ADDRESS.toBytes());
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800125
126 NeighborSolicitation ns2 = new NeighborSolicitation();
127 ns2.setTargetAddress(TARGET_ADDRESS2);
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800128 ns2.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS,
129 MAC_ADDRESS.toBytes());
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800130
131 assertTrue(ns1.equals(ns1));
132 assertFalse(ns1.equals(ns2));
133 }
134}