blob: 0c95c6f2d16b52b0c7524c8af7f0c9b06078f7be [file] [log] [blame]
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +08001/*
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -08002 * Copyright 2014-2015 Open Networking Laboratory
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +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. Chan93b7fb02014-12-28 03:59:36 +080017
18import org.junit.BeforeClass;
19import org.junit.Test;
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080020import org.onlab.packet.MacAddress;
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080021
22import 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;
25import static org.junit.Assert.assertThat;
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080026import static org.junit.Assert.assertTrue;
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080027
28/**
29 * Tests for class {@link NeighborAdvertisement}.
30 */
31public class NeighborAdvertisementTest {
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. Chan93b7fb02014-12-28 03:59:36 +080037 };
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080038 private static final MacAddress MAC_ADDRESS =
39 MacAddress.valueOf("11:22:33:44:55:66");
40
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080041 private static byte[] bytePacket;
42
43 @BeforeClass
44 public static void setUpBeforeClass() throws Exception {
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080045 byte[] byteHeader = {
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080046 (byte) 0xe0, (byte) 0x00, (byte) 0x00, (byte) 0x00,
47 (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18,
48 (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15,
49 (byte) 0xca, (byte) 0x2a, (byte) 0x14, (byte) 0xff,
50 (byte) 0xfe, (byte) 0x35, (byte) 0x26, (byte) 0xce,
51 (byte) 0x02, (byte) 0x01, (byte) 0x11, (byte) 0x22,
52 (byte) 0x33, (byte) 0x44, (byte) 0x55, (byte) 0x66
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080053 };
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080054 bytePacket = new byte[byteHeader.length];
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080055 System.arraycopy(byteHeader, 0, bytePacket, 0, byteHeader.length);
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080056 }
57
58 /**
59 * Tests serialize and setters.
60 */
61 @Test
62 public void testSerialize() {
63 NeighborAdvertisement na = new NeighborAdvertisement();
64 na.setRouterFlag((byte) 1);
65 na.setSolicitedFlag((byte) 1);
66 na.setOverrideFlag((byte) 1);
67 na.setTargetAddress(TARGET_ADDRESS);
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080068 na.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS,
69 MAC_ADDRESS.toBytes());
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080070
71 assertArrayEquals(na.serialize(), bytePacket);
72 }
73
74 /**
75 * Tests deserialize and getters.
76 */
77 @Test
78 public void testDeserialize() {
79 NeighborAdvertisement na = new NeighborAdvertisement();
80 na.deserialize(bytePacket, 0, bytePacket.length);
81
82 assertThat(na.getRouterFlag(), is((byte) 1));
83 assertThat(na.getSolicitedFlag(), is((byte) 1));
84 assertThat(na.getOverrideFlag(), is((byte) 1));
85 assertArrayEquals(na.getTargetAddress(), TARGET_ADDRESS);
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080086
87 // Check the option(s)
88 assertThat(na.getOptions().size(), is(1));
89 NeighborDiscoveryOptions.Option option = na.getOptions().get(0);
90 assertThat(option.type(),
91 is(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS));
92 assertArrayEquals(option.data(), MAC_ADDRESS.toBytes());
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080093 }
94
95 /**
96 * Tests comparator.
97 */
98 @Test
99 public void testEqual() {
100 NeighborAdvertisement na1 = new NeighborAdvertisement();
101 na1.setRouterFlag((byte) 1);
102 na1.setSolicitedFlag((byte) 1);
103 na1.setOverrideFlag((byte) 1);
104 na1.setTargetAddress(TARGET_ADDRESS);
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800105 na1.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS,
106 MAC_ADDRESS.toBytes());
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800107
108 NeighborAdvertisement na2 = new NeighborAdvertisement();
109 na2.setRouterFlag((byte) 1);
110 na2.setSolicitedFlag((byte) 1);
111 na2.setOverrideFlag((byte) 0);
112 na2.setTargetAddress(TARGET_ADDRESS);
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800113 na2.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS,
114 MAC_ADDRESS.toBytes());
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800115
116 assertTrue(na1.equals(na1));
117 assertFalse(na1.equals(na2));
118 }
119}