blob: d65dd51a734d13c0eae3a96831238fd91bb35c7d [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.DeserializationException;
21import org.onlab.packet.Deserializer;
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080022import org.onlab.packet.MacAddress;
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080023
24import static org.hamcrest.Matchers.is;
25import static org.junit.Assert.assertArrayEquals;
26import static org.junit.Assert.assertFalse;
27import static org.junit.Assert.assertThat;
28import static org.junit.Assert.assertTrue;
29
30/**
31 * Tests for class {@link RouterAdvertisement}.
32 */
33public class RouterAdvertisementTest {
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080034 private static final MacAddress MAC_ADDRESS =
35 MacAddress.valueOf("11:22:33:44:55:66");
36
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080037 private static byte[] bytePacket;
38
Jonathan Hart2a655752015-04-07 16:46:33 -070039 private Deserializer<RouterAdvertisement> deserializer
40 = RouterAdvertisement.deserializer();
41
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080042 @BeforeClass
43 public static void setUpBeforeClass() throws Exception {
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080044 byte[] byteHeader = {
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080045 (byte) 0x03, (byte) 0xc0, (byte) 0x02, (byte) 0x58,
46 (byte) 0x00, (byte) 0x00, (byte) 0x03, (byte) 0xe8,
47 (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0xf4,
48 (byte) 0x02, (byte) 0x01, (byte) 0x11, (byte) 0x22,
49 (byte) 0x33, (byte) 0x44, (byte) 0x55, (byte) 0x66
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080050 };
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080051 bytePacket = new byte[byteHeader.length];
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080052 System.arraycopy(byteHeader, 0, bytePacket, 0, byteHeader.length);
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080053 }
54
55 /**
56 * Tests serialize and setters.
57 */
58 @Test
59 public void testSerialize() {
60 RouterAdvertisement ra = new RouterAdvertisement();
61 ra.setCurrentHopLimit((byte) 3);
62 ra.setMFlag((byte) 1);
63 ra.setOFlag((byte) 1);
64 ra.setRouterLifetime((short) 0x258);
65 ra.setReachableTime(0x3e8);
66 ra.setRetransmitTimer(0x1f4);
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080067 ra.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS,
68 MAC_ADDRESS.toBytes());
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080069
70 assertArrayEquals(ra.serialize(), bytePacket);
71 }
72
73 /**
74 * Tests deserialize and getters.
75 */
76 @Test
Jonathan Hart2a655752015-04-07 16:46:33 -070077 public void testDeserialize() throws DeserializationException {
78 RouterAdvertisement ra = deserializer.deserialize(bytePacket, 0, bytePacket.length);
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080079
80 assertThat(ra.getCurrentHopLimit(), is((byte) 3));
81 assertThat(ra.getMFlag(), is((byte) 1));
82 assertThat(ra.getOFlag(), is((byte) 1));
83 assertThat(ra.getRouterLifetime(), is((short) 0x258));
84 assertThat(ra.getReachableTime(), is(0x3e8));
85 assertThat(ra.getRetransmitTimer(), is(0x1f4));
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080086
87 // Check the option(s)
88 assertThat(ra.getOptions().size(), is(1));
89 NeighborDiscoveryOptions.Option option = ra.getOptions().get(0);
90 assertThat(option.type(),
91 is(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS));
92 assertArrayEquals(option.data(), MAC_ADDRESS.toBytes());
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080093 }
94
95 /**
96 * Tests comparator.
97 */
98 @Test
99 public void testEqual() {
100 RouterAdvertisement ra1 = new RouterAdvertisement();
101 ra1.setCurrentHopLimit((byte) 3);
102 ra1.setMFlag((byte) 1);
103 ra1.setOFlag((byte) 1);
104 ra1.setRouterLifetime((short) 0x258);
105 ra1.setReachableTime(0x3e8);
106 ra1.setRetransmitTimer(0x1f4);
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800107 ra1.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS,
108 MAC_ADDRESS.toBytes());
Charles M.C. Chanea5aa472015-01-03 13:40:39 +0800109
110 RouterAdvertisement ra2 = new RouterAdvertisement();
111 ra2.setCurrentHopLimit((byte) 3);
112 ra2.setMFlag((byte) 0);
113 ra2.setOFlag((byte) 0);
114 ra2.setRouterLifetime((short) 0x1f4);
115 ra2.setReachableTime(0x3e8);
116 ra2.setRetransmitTimer(0x1f4);
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800117 ra2.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS,
118 MAC_ADDRESS.toBytes());
Charles M.C. Chanea5aa472015-01-03 13:40:39 +0800119
120 assertTrue(ra1.equals(ra1));
121 assertFalse(ra1.equals(ra2));
122 }
123}