blob: 09e011777e82688c8a7922e3e2015fc495e09b55 [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;
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080020import org.onlab.packet.MacAddress;
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080021
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080022import static org.hamcrest.Matchers.is;
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080023import static org.junit.Assert.assertArrayEquals;
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080024import static org.junit.Assert.assertFalse;
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080025import static org.junit.Assert.assertThat;
26import static org.junit.Assert.assertTrue;
27
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080028/**
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080029 * Tests for class {@link NeighborSolicitation}.
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080030 */
31public class NeighborSolicitationTest {
32 private static final byte[] TARGET_ADDRESS = {
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080033 (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18,
34 (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15,
35 (byte) 0xca, (byte) 0x2a, (byte) 0x14, (byte) 0xff,
36 (byte) 0xfe, (byte) 0x35, (byte) 0x26, (byte) 0xce
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080037 };
38 private static final byte[] TARGET_ADDRESS2 = {
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080039 (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18,
40 (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15,
41 (byte) 0xe6, (byte) 0xce, (byte) 0x8f, (byte) 0xff,
42 (byte) 0xfe, (byte) 0x54, (byte) 0x37, (byte) 0xc8
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080043 };
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080044 private static final MacAddress MAC_ADDRESS =
45 MacAddress.valueOf("11:22:33:44:55:66");
46
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080047 private static byte[] bytePacket;
48
49 @BeforeClass
50 public static void setUpBeforeClass() throws Exception {
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080051 byte[] byteHeader = {
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080052 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
53 (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18,
54 (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15,
55 (byte) 0xca, (byte) 0x2a, (byte) 0x14, (byte) 0xff,
56 (byte) 0xfe, (byte) 0x35, (byte) 0x26, (byte) 0xce,
57 (byte) 0x02, (byte) 0x01, (byte) 0x11, (byte) 0x22,
58 (byte) 0x33, (byte) 0x44, (byte) 0x55, (byte) 0x66
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080059 };
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080060 bytePacket = new byte[byteHeader.length];
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080061 System.arraycopy(byteHeader, 0, bytePacket, 0, byteHeader.length);
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080062 }
63
64 /**
65 * Tests serialize and setters.
66 */
67 @Test
68 public void testSerialize() {
69 NeighborSolicitation ns = new NeighborSolicitation();
70 ns.setTargetAddress(TARGET_ADDRESS);
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080071 ns.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS,
72 MAC_ADDRESS.toBytes());
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080073
74 assertArrayEquals(ns.serialize(), bytePacket);
75 }
76
77 /**
78 * Tests deserialize and getters.
79 */
80 @Test
81 public void testDeserialize() {
82 NeighborSolicitation ns = new NeighborSolicitation();
83 ns.deserialize(bytePacket, 0, bytePacket.length);
84
85 assertArrayEquals(ns.getTargetAddress(), TARGET_ADDRESS);
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080086
87 // Check the option(s)
88 assertThat(ns.getOptions().size(), is(1));
89 NeighborDiscoveryOptions.Option option = ns.getOptions().get(0);
90 assertThat(option.type(),
91 is(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS));
92 assertArrayEquals(option.data(), MAC_ADDRESS.toBytes());
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +080093 }
94
95 /**
96 * Tests comparator.
97 */
98 @Test
99 public void testEqual() {
100 NeighborSolicitation ns1 = new NeighborSolicitation();
101 ns1.setTargetAddress(TARGET_ADDRESS);
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800102 ns1.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS,
103 MAC_ADDRESS.toBytes());
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800104
105 NeighborSolicitation ns2 = new NeighborSolicitation();
106 ns2.setTargetAddress(TARGET_ADDRESS2);
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800107 ns2.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS,
108 MAC_ADDRESS.toBytes());
Charles M.C. Chan7fee36a2014-12-31 00:19:59 +0800109
110 assertTrue(ns1.equals(ns1));
111 assertFalse(ns1.equals(ns2));
112 }
113}