blob: d537251cd41d098ece3b226b46a8efedc00a949c [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;
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. Chan93b7fb02014-12-28 03:59:36 +080026
27import 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;
30import static org.junit.Assert.assertThat;
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080031import static org.junit.Assert.assertTrue;
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080032
33/**
34 * Tests for class {@link NeighborAdvertisement}.
35 */
36public class NeighborAdvertisementTest {
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. Chan93b7fb02014-12-28 03:59:36 +080042 };
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080043 private static final MacAddress MAC_ADDRESS =
44 MacAddress.valueOf("11:22:33:44:55:66");
45
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080046 private static byte[] bytePacket;
47
Jonathan Hart2a655752015-04-07 16:46:33 -070048 private Deserializer<NeighborAdvertisement> deserializer
49 = NeighborAdvertisement.deserializer();
50
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080051 @BeforeClass
52 public static void setUpBeforeClass() throws Exception {
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080053 byte[] byteHeader = {
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080054 (byte) 0xe0, (byte) 0x00, (byte) 0x00, (byte) 0x00,
55 (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18,
56 (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15,
57 (byte) 0xca, (byte) 0x2a, (byte) 0x14, (byte) 0xff,
58 (byte) 0xfe, (byte) 0x35, (byte) 0x26, (byte) 0xce,
59 (byte) 0x02, (byte) 0x01, (byte) 0x11, (byte) 0x22,
60 (byte) 0x33, (byte) 0x44, (byte) 0x55, (byte) 0x66
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080061 };
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080062 bytePacket = new byte[byteHeader.length];
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080063 System.arraycopy(byteHeader, 0, bytePacket, 0, byteHeader.length);
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080064 }
65
66 /**
67 * Tests serialize and setters.
68 */
69 @Test
70 public void testSerialize() {
71 NeighborAdvertisement na = new NeighborAdvertisement();
72 na.setRouterFlag((byte) 1);
73 na.setSolicitedFlag((byte) 1);
74 na.setOverrideFlag((byte) 1);
75 na.setTargetAddress(TARGET_ADDRESS);
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080076 na.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS,
77 MAC_ADDRESS.toBytes());
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080078
79 assertArrayEquals(na.serialize(), bytePacket);
80 }
81
Charles Chan3599d632015-09-05 14:47:51 +080082 @Test
83 public void testDeserializeBadInput() throws Exception {
84 PacketTestUtils.testDeserializeBadInput(NeighborAdvertisement.deserializer());
85 }
86
87 @Test
88 public void testDeserializeTruncated() throws Exception {
89 // Run the truncation test only on the NeighborAdvertisement header
90 byte[] naHeader = new byte[NeighborAdvertisement.HEADER_LENGTH];
91 ByteBuffer.wrap(bytePacket).get(naHeader);
92
93 PacketTestUtils.testDeserializeTruncated(NeighborAdvertisement.deserializer(), naHeader);
94 }
95
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080096 /**
97 * Tests deserialize and getters.
98 */
99 @Test
Jonathan Hart2a655752015-04-07 16:46:33 -0700100 public void testDeserialize() throws DeserializationException {
101 NeighborAdvertisement na = deserializer.deserialize(bytePacket, 0, bytePacket.length);
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800102
103 assertThat(na.getRouterFlag(), is((byte) 1));
104 assertThat(na.getSolicitedFlag(), is((byte) 1));
105 assertThat(na.getOverrideFlag(), is((byte) 1));
106 assertArrayEquals(na.getTargetAddress(), TARGET_ADDRESS);
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800107
108 // Check the option(s)
109 assertThat(na.getOptions().size(), is(1));
110 NeighborDiscoveryOptions.Option option = na.getOptions().get(0);
111 assertThat(option.type(),
112 is(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS));
113 assertArrayEquals(option.data(), MAC_ADDRESS.toBytes());
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800114 }
115
116 /**
117 * Tests comparator.
118 */
119 @Test
120 public void testEqual() {
121 NeighborAdvertisement na1 = new NeighborAdvertisement();
122 na1.setRouterFlag((byte) 1);
123 na1.setSolicitedFlag((byte) 1);
124 na1.setOverrideFlag((byte) 1);
125 na1.setTargetAddress(TARGET_ADDRESS);
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800126 na1.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS,
127 MAC_ADDRESS.toBytes());
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800128
129 NeighborAdvertisement na2 = new NeighborAdvertisement();
130 na2.setRouterFlag((byte) 1);
131 na2.setSolicitedFlag((byte) 1);
132 na2.setOverrideFlag((byte) 0);
133 na2.setTargetAddress(TARGET_ADDRESS);
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800134 na2.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS,
135 MAC_ADDRESS.toBytes());
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800136
137 assertTrue(na1.equals(na1));
138 assertFalse(na1.equals(na2));
139 }
140}