blob: 1ae77dc3257495a1581cb17dc5a9998ada6e8e2c [file] [log] [blame]
Charles M.C. Chanea5aa472015-01-03 13:40:39 +08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present 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
Jian Li5fc14292015-12-04 11:30:46 -080018import org.apache.commons.lang3.StringUtils;
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080019import org.junit.BeforeClass;
20import org.junit.Test;
Jonathan Hart2a655752015-04-07 16:46:33 -070021import org.onlab.packet.DeserializationException;
22import org.onlab.packet.Deserializer;
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080023import org.onlab.packet.MacAddress;
Charles Chan3599d632015-09-05 14:47:51 +080024import org.onlab.packet.PacketTestUtils;
25
26import java.nio.ByteBuffer;
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080027
28import static org.hamcrest.Matchers.is;
29import static org.junit.Assert.assertArrayEquals;
30import static org.junit.Assert.assertFalse;
31import static org.junit.Assert.assertThat;
32import static org.junit.Assert.assertTrue;
33
34/**
35 * Tests for class {@link RouterAdvertisement}.
36 */
37public class RouterAdvertisementTest {
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080038 private static final MacAddress MAC_ADDRESS =
39 MacAddress.valueOf("11:22:33:44:55:66");
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<RouterAdvertisement> deserializer
44 = RouterAdvertisement.deserializer();
45
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080046 @BeforeClass
47 public static void setUpBeforeClass() throws Exception {
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080048 byte[] byteHeader = {
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080049 (byte) 0x03, (byte) 0xc0, (byte) 0x02, (byte) 0x58,
50 (byte) 0x00, (byte) 0x00, (byte) 0x03, (byte) 0xe8,
51 (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0xf4,
52 (byte) 0x02, (byte) 0x01, (byte) 0x11, (byte) 0x22,
53 (byte) 0x33, (byte) 0x44, (byte) 0x55, (byte) 0x66
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080054 };
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080055 bytePacket = new byte[byteHeader.length];
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080056 System.arraycopy(byteHeader, 0, bytePacket, 0, byteHeader.length);
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080057 }
58
59 /**
60 * Tests serialize and setters.
61 */
62 @Test
63 public void testSerialize() {
64 RouterAdvertisement ra = new RouterAdvertisement();
65 ra.setCurrentHopLimit((byte) 3);
66 ra.setMFlag((byte) 1);
67 ra.setOFlag((byte) 1);
68 ra.setRouterLifetime((short) 0x258);
69 ra.setReachableTime(0x3e8);
70 ra.setRetransmitTimer(0x1f4);
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080071 ra.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS,
72 MAC_ADDRESS.toBytes());
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080073
74 assertArrayEquals(ra.serialize(), bytePacket);
75 }
76
Charles Chan3599d632015-09-05 14:47:51 +080077 @Test
78 public void testDeserializeBadInput() throws Exception {
79 PacketTestUtils.testDeserializeBadInput(RouterAdvertisement.deserializer());
80 }
81
82 @Test
83 public void testDeserializeTruncated() throws Exception {
84 // Run the truncation test only on the RouterAdvertisement header
85 byte[] raHeader = new byte[RouterAdvertisement.HEADER_LENGTH];
86 ByteBuffer.wrap(bytePacket).get(raHeader);
87
88 PacketTestUtils.testDeserializeTruncated(RouterAdvertisement.deserializer(), raHeader);
89 }
90
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080091 /**
92 * Tests deserialize and getters.
93 */
94 @Test
Jonathan Hart2a655752015-04-07 16:46:33 -070095 public void testDeserialize() throws DeserializationException {
96 RouterAdvertisement ra = deserializer.deserialize(bytePacket, 0, bytePacket.length);
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080097
98 assertThat(ra.getCurrentHopLimit(), is((byte) 3));
99 assertThat(ra.getMFlag(), is((byte) 1));
100 assertThat(ra.getOFlag(), is((byte) 1));
101 assertThat(ra.getRouterLifetime(), is((short) 0x258));
102 assertThat(ra.getReachableTime(), is(0x3e8));
103 assertThat(ra.getRetransmitTimer(), is(0x1f4));
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800104
105 // Check the option(s)
106 assertThat(ra.getOptions().size(), is(1));
107 NeighborDiscoveryOptions.Option option = ra.getOptions().get(0);
108 assertThat(option.type(),
109 is(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS));
110 assertArrayEquals(option.data(), MAC_ADDRESS.toBytes());
Charles M.C. Chanea5aa472015-01-03 13:40:39 +0800111 }
112
113 /**
114 * Tests comparator.
115 */
116 @Test
117 public void testEqual() {
118 RouterAdvertisement ra1 = new RouterAdvertisement();
119 ra1.setCurrentHopLimit((byte) 3);
120 ra1.setMFlag((byte) 1);
121 ra1.setOFlag((byte) 1);
122 ra1.setRouterLifetime((short) 0x258);
123 ra1.setReachableTime(0x3e8);
124 ra1.setRetransmitTimer(0x1f4);
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800125 ra1.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS,
126 MAC_ADDRESS.toBytes());
Charles M.C. Chanea5aa472015-01-03 13:40:39 +0800127
128 RouterAdvertisement ra2 = new RouterAdvertisement();
129 ra2.setCurrentHopLimit((byte) 3);
130 ra2.setMFlag((byte) 0);
131 ra2.setOFlag((byte) 0);
132 ra2.setRouterLifetime((short) 0x1f4);
133 ra2.setReachableTime(0x3e8);
134 ra2.setRetransmitTimer(0x1f4);
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800135 ra2.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS,
136 MAC_ADDRESS.toBytes());
Charles M.C. Chanea5aa472015-01-03 13:40:39 +0800137
138 assertTrue(ra1.equals(ra1));
139 assertFalse(ra1.equals(ra2));
140 }
Jian Li5fc14292015-12-04 11:30:46 -0800141
142 /**
143 * Tests toString.
144 */
145 @Test
146 public void testToStringRA() throws Exception {
147 RouterAdvertisement ra = deserializer.deserialize(bytePacket, 0, bytePacket.length);
148 String str = ra.toString();
149
150 assertTrue(StringUtils.contains(str, "currentHopLimit=" + (byte) 3));
151 assertTrue(StringUtils.contains(str, "mFlag=" + (byte) 1));
152 assertTrue(StringUtils.contains(str, "oFlag=" + (byte) 1));
153 assertTrue(StringUtils.contains(str, "routerLifetime=" + (short) 0x258));
154 assertTrue(StringUtils.contains(str, "reachableTime=" + 0x3e8));
155 assertTrue(StringUtils.contains(str, "retransmitTimer=" + 0x1f4));
156
157 // TODO: need to handle options
158 }
Charles M.C. Chanea5aa472015-01-03 13:40:39 +0800159}