blob: b562f38c96945f50e84b1ea2617bfae8f6482fb8 [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;
Jian Li5fc14292015-12-04 11:30:46 -080027import static org.junit.Assert.*;
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080028
29/**
30 * Tests for class {@link RouterSolicitation}.
31 */
32public class RouterSolicitationTest {
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080033 private static final MacAddress MAC_ADDRESS1 =
34 MacAddress.valueOf("11:22:33:44:55:66");
35 private static final MacAddress MAC_ADDRESS2 =
36 MacAddress.valueOf("11:22:33:44:55:00");
37
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080038 private static byte[] bytePacket;
39
Jonathan Hart2a655752015-04-07 16:46:33 -070040 private Deserializer<RouterSolicitation> deserializer
41 = RouterSolicitation.deserializer();
42
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080043 @BeforeClass
44 public static void setUpBeforeClass() throws Exception {
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080045 byte[] byteHeader = {
46 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
47 (byte) 0x02, (byte) 0x01, (byte) 0x11, (byte) 0x22,
48 (byte) 0x33, (byte) 0x44, (byte) 0x55, (byte) 0x66
49 };
50 bytePacket = new byte[byteHeader.length];
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080051 System.arraycopy(byteHeader, 0, bytePacket, 0, byteHeader.length);
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080052 }
53
54 /**
55 * Tests serialize and setters.
56 */
57 @Test
58 public void testSerialize() {
59 RouterSolicitation rs = new RouterSolicitation();
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080060 rs.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS,
61 MAC_ADDRESS1.toBytes());
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080062
63 assertArrayEquals(rs.serialize(), bytePacket);
64 }
65
Jonathan Hart2a655752015-04-07 16:46:33 -070066 @Test
67 public void testDeserializeBadInput() throws Exception {
68 PacketTestUtils.testDeserializeBadInput(RouterSolicitation.deserializer());
69 }
70
71 @Test
72 public void testDeserializeTruncated() throws Exception {
Charles Chan3599d632015-09-05 14:47:51 +080073 // Run the truncation test only on the RouterSolicitation header
74 byte[] rsHeader = new byte[RouterSolicitation.HEADER_LENGTH];
75 ByteBuffer.wrap(bytePacket).get(rsHeader);
76
77 PacketTestUtils.testDeserializeTruncated(RouterSolicitation.deserializer(), rsHeader);
Jonathan Hart2a655752015-04-07 16:46:33 -070078 }
79
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080080 /**
81 * Tests deserialize and getters.
82 */
83 @Test
Jonathan Hart2a655752015-04-07 16:46:33 -070084 public void testDeserialize() throws Exception {
85 RouterSolicitation rs = deserializer.deserialize(bytePacket, 0, bytePacket.length);
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080086
87 // Check the option(s)
88 assertThat(rs.getOptions().size(), is(1));
89 NeighborDiscoveryOptions.Option option = rs.getOptions().get(0);
90 assertThat(option.type(),
91 is(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS));
92 assertArrayEquals(option.data(), MAC_ADDRESS1.toBytes());
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080093 }
94
95 /**
96 * Tests comparator.
97 */
98 @Test
99 public void testEqual() {
100 RouterSolicitation rs1 = new RouterSolicitation();
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800101 rs1.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS,
102 MAC_ADDRESS1.toBytes());
103
104 RouterSolicitation rs2 = new RouterSolicitation();
105 rs2.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS,
106 MAC_ADDRESS2.toBytes());
Charles M.C. Chanea5aa472015-01-03 13:40:39 +0800107
108 assertTrue(rs1.equals(rs1));
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800109 assertFalse(rs1.equals(rs2));
Charles M.C. Chanea5aa472015-01-03 13:40:39 +0800110 }
Jian Li5fc14292015-12-04 11:30:46 -0800111
112 /**
113 * Tests toString.
114 */
115 @Test
116 public void testToStringRS() throws Exception {
117 RouterSolicitation rs = deserializer.deserialize(bytePacket, 0, bytePacket.length);
118 String str = rs.toString();
119
120 // TODO: need to handle Options
121 }
Charles M.C. Chanea5aa472015-01-03 13:40:39 +0800122}