blob: ac00096290acd43d5c312f859e9ef701791f1370 [file] [log] [blame]
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present 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
Jian Li5fc14292015-12-04 11:30:46 -080018import org.apache.commons.lang3.StringUtils;
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080019import org.junit.BeforeClass;
20import org.junit.Test;
Jonathan Hart2a655752015-04-07 16:46:33 -070021import org.onlab.packet.DeserializationException;
22import org.onlab.packet.Deserializer;
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080023import org.onlab.packet.MacAddress;
Charles Chan3599d632015-09-05 14:47:51 +080024import org.onlab.packet.PacketTestUtils;
25
26import java.nio.ByteBuffer;
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080027
28import static org.hamcrest.Matchers.is;
Jian Li5fc14292015-12-04 11:30:46 -080029import static org.junit.Assert.*;
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080030
31/**
32 * Tests for class {@link NeighborAdvertisement}.
33 */
34public class NeighborAdvertisementTest {
35 private static final byte[] TARGET_ADDRESS = {
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080036 (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18,
37 (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15,
38 (byte) 0xca, (byte) 0x2a, (byte) 0x14, (byte) 0xff,
39 (byte) 0xfe, (byte) 0x35, (byte) 0x26, (byte) 0xce
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080040 };
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080041 private static final MacAddress MAC_ADDRESS =
42 MacAddress.valueOf("11:22:33:44:55:66");
43
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080044 private static byte[] bytePacket;
45
Jonathan Hart2a655752015-04-07 16:46:33 -070046 private Deserializer<NeighborAdvertisement> deserializer
47 = NeighborAdvertisement.deserializer();
48
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080049 @BeforeClass
50 public static void setUpBeforeClass() throws Exception {
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080051 byte[] byteHeader = {
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080052 (byte) 0xe0, (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. Chan93b7fb02014-12-28 03:59:36 +080059 };
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080060 bytePacket = new byte[byteHeader.length];
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080061 System.arraycopy(byteHeader, 0, bytePacket, 0, byteHeader.length);
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080062 }
63
64 /**
65 * Tests serialize and setters.
66 */
67 @Test
68 public void testSerialize() {
69 NeighborAdvertisement na = new NeighborAdvertisement();
70 na.setRouterFlag((byte) 1);
71 na.setSolicitedFlag((byte) 1);
72 na.setOverrideFlag((byte) 1);
73 na.setTargetAddress(TARGET_ADDRESS);
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080074 na.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS,
75 MAC_ADDRESS.toBytes());
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080076
77 assertArrayEquals(na.serialize(), bytePacket);
78 }
79
Charles Chan3599d632015-09-05 14:47:51 +080080 @Test
81 public void testDeserializeBadInput() throws Exception {
82 PacketTestUtils.testDeserializeBadInput(NeighborAdvertisement.deserializer());
83 }
84
85 @Test
86 public void testDeserializeTruncated() throws Exception {
87 // Run the truncation test only on the NeighborAdvertisement header
88 byte[] naHeader = new byte[NeighborAdvertisement.HEADER_LENGTH];
89 ByteBuffer.wrap(bytePacket).get(naHeader);
90
91 PacketTestUtils.testDeserializeTruncated(NeighborAdvertisement.deserializer(), naHeader);
92 }
93
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +080094 /**
95 * Tests deserialize and getters.
96 */
97 @Test
Jonathan Hart2a655752015-04-07 16:46:33 -070098 public void testDeserialize() throws DeserializationException {
99 NeighborAdvertisement na = deserializer.deserialize(bytePacket, 0, bytePacket.length);
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800100
101 assertThat(na.getRouterFlag(), is((byte) 1));
102 assertThat(na.getSolicitedFlag(), is((byte) 1));
103 assertThat(na.getOverrideFlag(), is((byte) 1));
104 assertArrayEquals(na.getTargetAddress(), TARGET_ADDRESS);
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800105
106 // Check the option(s)
107 assertThat(na.getOptions().size(), is(1));
108 NeighborDiscoveryOptions.Option option = na.getOptions().get(0);
109 assertThat(option.type(),
110 is(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS));
111 assertArrayEquals(option.data(), MAC_ADDRESS.toBytes());
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800112 }
113
114 /**
115 * Tests comparator.
116 */
117 @Test
118 public void testEqual() {
119 NeighborAdvertisement na1 = new NeighborAdvertisement();
120 na1.setRouterFlag((byte) 1);
121 na1.setSolicitedFlag((byte) 1);
122 na1.setOverrideFlag((byte) 1);
123 na1.setTargetAddress(TARGET_ADDRESS);
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800124 na1.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS,
125 MAC_ADDRESS.toBytes());
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800126
127 NeighborAdvertisement na2 = new NeighborAdvertisement();
128 na2.setRouterFlag((byte) 1);
129 na2.setSolicitedFlag((byte) 1);
130 na2.setOverrideFlag((byte) 0);
131 na2.setTargetAddress(TARGET_ADDRESS);
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800132 na2.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS,
133 MAC_ADDRESS.toBytes());
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800134
135 assertTrue(na1.equals(na1));
136 assertFalse(na1.equals(na2));
137 }
Jian Li5fc14292015-12-04 11:30:46 -0800138
139 /**
140 * Tests toString.
141 */
142 @Test
143 public void testToStringNA() throws Exception {
144 NeighborAdvertisement na = deserializer.deserialize(bytePacket, 0, bytePacket.length);
145 String str = na.toString();
146
147 assertTrue(StringUtils.contains(str, "routerFlag=" + (byte) 1));
148 assertTrue(StringUtils.contains(str, "solicitedFlag=" + (byte) 1));
149 assertTrue(StringUtils.contains(str, "overrideFlag=" + (byte) 1));
150 // TODO: need to handle TARGET_ADDRESS
151 }
Charles M.C. Chan93b7fb02014-12-28 03:59:36 +0800152}