blob: e3b5686c7c5cc3cd5bac16918d8256ff17eca002 [file] [log] [blame]
Charles M.C. Chanea5aa472015-01-03 13:40:39 +08001/*
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -08002 * Copyright 2014-2015 Open Networking Laboratory
Charles M.C. Chanea5aa472015-01-03 13:40:39 +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;
17
18import org.junit.BeforeClass;
19import org.junit.Test;
Jonathan Hart2a655752015-04-07 16:46:33 -070020import org.onlab.packet.Deserializer;
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080021import org.onlab.packet.MacAddress;
Jonathan Hart2a655752015-04-07 16:46:33 -070022import org.onlab.packet.PacketTestUtils;
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080023
Charles Chan3599d632015-09-05 14:47:51 +080024import java.nio.ByteBuffer;
25
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080026import static org.hamcrest.Matchers.is;
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080027import static org.junit.Assert.assertArrayEquals;
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080028import static org.junit.Assert.assertFalse;
29import static org.junit.Assert.assertThat;
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080030import static org.junit.Assert.assertTrue;
31
32/**
33 * Tests for class {@link RouterSolicitation}.
34 */
35public class RouterSolicitationTest {
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080036 private static final MacAddress MAC_ADDRESS1 =
37 MacAddress.valueOf("11:22:33:44:55:66");
38 private static final MacAddress MAC_ADDRESS2 =
39 MacAddress.valueOf("11:22:33:44:55:00");
40
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080041 private static byte[] bytePacket;
42
Jonathan Hart2a655752015-04-07 16:46:33 -070043 private Deserializer<RouterSolicitation> deserializer
44 = RouterSolicitation.deserializer();
45
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080046 @BeforeClass
47 public static void setUpBeforeClass() throws Exception {
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080048 byte[] byteHeader = {
49 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
50 (byte) 0x02, (byte) 0x01, (byte) 0x11, (byte) 0x22,
51 (byte) 0x33, (byte) 0x44, (byte) 0x55, (byte) 0x66
52 };
53 bytePacket = new byte[byteHeader.length];
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080054 System.arraycopy(byteHeader, 0, bytePacket, 0, byteHeader.length);
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080055 }
56
57 /**
58 * Tests serialize and setters.
59 */
60 @Test
61 public void testSerialize() {
62 RouterSolicitation rs = new RouterSolicitation();
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080063 rs.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS,
64 MAC_ADDRESS1.toBytes());
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080065
66 assertArrayEquals(rs.serialize(), bytePacket);
67 }
68
Jonathan Hart2a655752015-04-07 16:46:33 -070069 @Test
70 public void testDeserializeBadInput() throws Exception {
71 PacketTestUtils.testDeserializeBadInput(RouterSolicitation.deserializer());
72 }
73
74 @Test
75 public void testDeserializeTruncated() throws Exception {
Charles Chan3599d632015-09-05 14:47:51 +080076 // Run the truncation test only on the RouterSolicitation header
77 byte[] rsHeader = new byte[RouterSolicitation.HEADER_LENGTH];
78 ByteBuffer.wrap(bytePacket).get(rsHeader);
79
80 PacketTestUtils.testDeserializeTruncated(RouterSolicitation.deserializer(), rsHeader);
Jonathan Hart2a655752015-04-07 16:46:33 -070081 }
82
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080083 /**
84 * Tests deserialize and getters.
85 */
86 @Test
Jonathan Hart2a655752015-04-07 16:46:33 -070087 public void testDeserialize() throws Exception {
88 RouterSolicitation rs = deserializer.deserialize(bytePacket, 0, bytePacket.length);
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080089
90 // Check the option(s)
91 assertThat(rs.getOptions().size(), is(1));
92 NeighborDiscoveryOptions.Option option = rs.getOptions().get(0);
93 assertThat(option.type(),
94 is(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS));
95 assertArrayEquals(option.data(), MAC_ADDRESS1.toBytes());
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080096 }
97
98 /**
99 * Tests comparator.
100 */
101 @Test
102 public void testEqual() {
103 RouterSolicitation rs1 = new RouterSolicitation();
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800104 rs1.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS,
105 MAC_ADDRESS1.toBytes());
106
107 RouterSolicitation rs2 = new RouterSolicitation();
108 rs2.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS,
109 MAC_ADDRESS2.toBytes());
Charles M.C. Chanea5aa472015-01-03 13:40:39 +0800110
111 assertTrue(rs1.equals(rs1));
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800112 assertFalse(rs1.equals(rs2));
Charles M.C. Chanea5aa472015-01-03 13:40:39 +0800113 }
114}