blob: 9c087e392e01e4edfb0e75823710208a0ab7590b [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;
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080020import org.onlab.packet.MacAddress;
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080021
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080022import static org.hamcrest.Matchers.is;
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080023import static org.junit.Assert.assertArrayEquals;
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080024import static org.junit.Assert.assertFalse;
25import static org.junit.Assert.assertThat;
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080026import static org.junit.Assert.assertTrue;
27
28/**
29 * Tests for class {@link RouterSolicitation}.
30 */
31public class RouterSolicitationTest {
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080032 private static final MacAddress MAC_ADDRESS1 =
33 MacAddress.valueOf("11:22:33:44:55:66");
34 private static final MacAddress MAC_ADDRESS2 =
35 MacAddress.valueOf("11:22:33:44:55:00");
36
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080037 private static byte[] bytePacket;
38
39 @BeforeClass
40 public static void setUpBeforeClass() throws Exception {
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080041 byte[] byteHeader = {
42 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
43 (byte) 0x02, (byte) 0x01, (byte) 0x11, (byte) 0x22,
44 (byte) 0x33, (byte) 0x44, (byte) 0x55, (byte) 0x66
45 };
46 bytePacket = new byte[byteHeader.length];
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080047 System.arraycopy(byteHeader, 0, bytePacket, 0, byteHeader.length);
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080048 }
49
50 /**
51 * Tests serialize and setters.
52 */
53 @Test
54 public void testSerialize() {
55 RouterSolicitation rs = new RouterSolicitation();
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080056 rs.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS,
57 MAC_ADDRESS1.toBytes());
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080058
59 assertArrayEquals(rs.serialize(), bytePacket);
60 }
61
62 /**
63 * Tests deserialize and getters.
64 */
65 @Test
66 public void testDeserialize() {
67 RouterSolicitation rs = new RouterSolicitation();
68 rs.deserialize(bytePacket, 0, bytePacket.length);
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080069
70 // Check the option(s)
71 assertThat(rs.getOptions().size(), is(1));
72 NeighborDiscoveryOptions.Option option = rs.getOptions().get(0);
73 assertThat(option.type(),
74 is(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS));
75 assertArrayEquals(option.data(), MAC_ADDRESS1.toBytes());
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080076 }
77
78 /**
79 * Tests comparator.
80 */
81 @Test
82 public void testEqual() {
83 RouterSolicitation rs1 = new RouterSolicitation();
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080084 rs1.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS,
85 MAC_ADDRESS1.toBytes());
86
87 RouterSolicitation rs2 = new RouterSolicitation();
88 rs2.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS,
89 MAC_ADDRESS2.toBytes());
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080090
91 assertTrue(rs1.equals(rs1));
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080092 assertFalse(rs1.equals(rs2));
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080093 }
94}