blob: b69d14281d4ee7be8f913f74d0f59c63e7367b63 [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
22import static org.hamcrest.Matchers.is;
23import static org.junit.Assert.assertArrayEquals;
24import static org.junit.Assert.assertFalse;
25import static org.junit.Assert.assertThat;
26import static org.junit.Assert.assertTrue;
27
28/**
29 * Tests for class {@link RouterAdvertisement}.
30 */
31public class RouterAdvertisementTest {
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080032 private static final MacAddress MAC_ADDRESS =
33 MacAddress.valueOf("11:22:33:44:55:66");
34
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080035 private static byte[] bytePacket;
36
37 @BeforeClass
38 public static void setUpBeforeClass() throws Exception {
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080039 byte[] byteHeader = {
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080040 (byte) 0x03, (byte) 0xc0, (byte) 0x02, (byte) 0x58,
41 (byte) 0x00, (byte) 0x00, (byte) 0x03, (byte) 0xe8,
42 (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0xf4,
43 (byte) 0x02, (byte) 0x01, (byte) 0x11, (byte) 0x22,
44 (byte) 0x33, (byte) 0x44, (byte) 0x55, (byte) 0x66
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080045 };
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080046 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 RouterAdvertisement ra = new RouterAdvertisement();
56 ra.setCurrentHopLimit((byte) 3);
57 ra.setMFlag((byte) 1);
58 ra.setOFlag((byte) 1);
59 ra.setRouterLifetime((short) 0x258);
60 ra.setReachableTime(0x3e8);
61 ra.setRetransmitTimer(0x1f4);
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080062 ra.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS,
63 MAC_ADDRESS.toBytes());
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080064
65 assertArrayEquals(ra.serialize(), bytePacket);
66 }
67
68 /**
69 * Tests deserialize and getters.
70 */
71 @Test
72 public void testDeserialize() {
73 RouterAdvertisement ra = new RouterAdvertisement();
74 ra.deserialize(bytePacket, 0, bytePacket.length);
75
76 assertThat(ra.getCurrentHopLimit(), is((byte) 3));
77 assertThat(ra.getMFlag(), is((byte) 1));
78 assertThat(ra.getOFlag(), is((byte) 1));
79 assertThat(ra.getRouterLifetime(), is((short) 0x258));
80 assertThat(ra.getReachableTime(), is(0x3e8));
81 assertThat(ra.getRetransmitTimer(), is(0x1f4));
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080082
83 // Check the option(s)
84 assertThat(ra.getOptions().size(), is(1));
85 NeighborDiscoveryOptions.Option option = ra.getOptions().get(0);
86 assertThat(option.type(),
87 is(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS));
88 assertArrayEquals(option.data(), MAC_ADDRESS.toBytes());
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080089 }
90
91 /**
92 * Tests comparator.
93 */
94 @Test
95 public void testEqual() {
96 RouterAdvertisement ra1 = new RouterAdvertisement();
97 ra1.setCurrentHopLimit((byte) 3);
98 ra1.setMFlag((byte) 1);
99 ra1.setOFlag((byte) 1);
100 ra1.setRouterLifetime((short) 0x258);
101 ra1.setReachableTime(0x3e8);
102 ra1.setRetransmitTimer(0x1f4);
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800103 ra1.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS,
104 MAC_ADDRESS.toBytes());
Charles M.C. Chanea5aa472015-01-03 13:40:39 +0800105
106 RouterAdvertisement ra2 = new RouterAdvertisement();
107 ra2.setCurrentHopLimit((byte) 3);
108 ra2.setMFlag((byte) 0);
109 ra2.setOFlag((byte) 0);
110 ra2.setRouterLifetime((short) 0x1f4);
111 ra2.setReachableTime(0x3e8);
112 ra2.setRetransmitTimer(0x1f4);
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800113 ra2.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS,
114 MAC_ADDRESS.toBytes());
Charles M.C. Chanea5aa472015-01-03 13:40:39 +0800115
116 assertTrue(ra1.equals(ra1));
117 assertFalse(ra1.equals(ra2));
118 }
119}