blob: ede1574791350ca20d5b59dc8f8148deba8c121b [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 Chan3599d632015-09-05 14:47:51 +080023import org.onlab.packet.PacketTestUtils;
24
25import java.nio.ByteBuffer;
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080026
27import static org.hamcrest.Matchers.is;
28import static org.junit.Assert.assertArrayEquals;
29import static org.junit.Assert.assertFalse;
30import static org.junit.Assert.assertThat;
31import static org.junit.Assert.assertTrue;
32
33/**
34 * Tests for class {@link RouterAdvertisement}.
35 */
36public class RouterAdvertisementTest {
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080037 private static final MacAddress MAC_ADDRESS =
38 MacAddress.valueOf("11:22:33:44:55:66");
39
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080040 private static byte[] bytePacket;
41
Jonathan Hart2a655752015-04-07 16:46:33 -070042 private Deserializer<RouterAdvertisement> deserializer
43 = RouterAdvertisement.deserializer();
44
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080045 @BeforeClass
46 public static void setUpBeforeClass() throws Exception {
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080047 byte[] byteHeader = {
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080048 (byte) 0x03, (byte) 0xc0, (byte) 0x02, (byte) 0x58,
49 (byte) 0x00, (byte) 0x00, (byte) 0x03, (byte) 0xe8,
50 (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0xf4,
51 (byte) 0x02, (byte) 0x01, (byte) 0x11, (byte) 0x22,
52 (byte) 0x33, (byte) 0x44, (byte) 0x55, (byte) 0x66
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080053 };
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080054 bytePacket = new byte[byteHeader.length];
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080055 System.arraycopy(byteHeader, 0, bytePacket, 0, byteHeader.length);
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080056 }
57
58 /**
59 * Tests serialize and setters.
60 */
61 @Test
62 public void testSerialize() {
63 RouterAdvertisement ra = new RouterAdvertisement();
64 ra.setCurrentHopLimit((byte) 3);
65 ra.setMFlag((byte) 1);
66 ra.setOFlag((byte) 1);
67 ra.setRouterLifetime((short) 0x258);
68 ra.setReachableTime(0x3e8);
69 ra.setRetransmitTimer(0x1f4);
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080070 ra.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS,
71 MAC_ADDRESS.toBytes());
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080072
73 assertArrayEquals(ra.serialize(), bytePacket);
74 }
75
Charles Chan3599d632015-09-05 14:47:51 +080076 @Test
77 public void testDeserializeBadInput() throws Exception {
78 PacketTestUtils.testDeserializeBadInput(RouterAdvertisement.deserializer());
79 }
80
81 @Test
82 public void testDeserializeTruncated() throws Exception {
83 // Run the truncation test only on the RouterAdvertisement header
84 byte[] raHeader = new byte[RouterAdvertisement.HEADER_LENGTH];
85 ByteBuffer.wrap(bytePacket).get(raHeader);
86
87 PacketTestUtils.testDeserializeTruncated(RouterAdvertisement.deserializer(), raHeader);
88 }
89
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080090 /**
91 * Tests deserialize and getters.
92 */
93 @Test
Jonathan Hart2a655752015-04-07 16:46:33 -070094 public void testDeserialize() throws DeserializationException {
95 RouterAdvertisement ra = deserializer.deserialize(bytePacket, 0, bytePacket.length);
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080096
97 assertThat(ra.getCurrentHopLimit(), is((byte) 3));
98 assertThat(ra.getMFlag(), is((byte) 1));
99 assertThat(ra.getOFlag(), is((byte) 1));
100 assertThat(ra.getRouterLifetime(), is((short) 0x258));
101 assertThat(ra.getReachableTime(), is(0x3e8));
102 assertThat(ra.getRetransmitTimer(), is(0x1f4));
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800103
104 // Check the option(s)
105 assertThat(ra.getOptions().size(), is(1));
106 NeighborDiscoveryOptions.Option option = ra.getOptions().get(0);
107 assertThat(option.type(),
108 is(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS));
109 assertArrayEquals(option.data(), MAC_ADDRESS.toBytes());
Charles M.C. Chanea5aa472015-01-03 13:40:39 +0800110 }
111
112 /**
113 * Tests comparator.
114 */
115 @Test
116 public void testEqual() {
117 RouterAdvertisement ra1 = new RouterAdvertisement();
118 ra1.setCurrentHopLimit((byte) 3);
119 ra1.setMFlag((byte) 1);
120 ra1.setOFlag((byte) 1);
121 ra1.setRouterLifetime((short) 0x258);
122 ra1.setReachableTime(0x3e8);
123 ra1.setRetransmitTimer(0x1f4);
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800124 ra1.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS,
125 MAC_ADDRESS.toBytes());
Charles M.C. Chanea5aa472015-01-03 13:40:39 +0800126
127 RouterAdvertisement ra2 = new RouterAdvertisement();
128 ra2.setCurrentHopLimit((byte) 3);
129 ra2.setMFlag((byte) 0);
130 ra2.setOFlag((byte) 0);
131 ra2.setRouterLifetime((short) 0x1f4);
132 ra2.setReachableTime(0x3e8);
133 ra2.setRetransmitTimer(0x1f4);
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800134 ra2.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS,
135 MAC_ADDRESS.toBytes());
Charles M.C. Chanea5aa472015-01-03 13:40:39 +0800136
137 assertTrue(ra1.equals(ra1));
138 assertFalse(ra1.equals(ra2));
139 }
140}