blob: 2c7237bcf49fde77532675e23c34976d14338b0b [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
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
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080027import static org.hamcrest.Matchers.is;
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080028import static org.junit.Assert.assertArrayEquals;
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080029import static org.junit.Assert.assertFalse;
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080030import static org.junit.Assert.assertThat;
31import static org.junit.Assert.assertTrue;
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080032
33/**
34 * Tests for class {@link Redirect}.
35 */
36public class RedirectTest {
37 private static final byte[] TARGET_ADDRESS = {
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080038 (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18,
39 (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15,
40 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
41 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080042 };
43 private static final byte[] DESTINATION_ADDRESS = {
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080044 (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18,
45 (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15,
46 (byte) 0xca, (byte) 0x2a, (byte) 0x14, (byte) 0xff,
47 (byte) 0xfe, (byte) 0x35, (byte) 0x26, (byte) 0xce
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080048 };
49 private static final byte[] DESTINATION_ADDRESS2 = {
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080050 (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18,
51 (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15,
52 (byte) 0xe6, (byte) 0xce, (byte) 0x8f, (byte) 0xff,
53 (byte) 0xfe, (byte) 0x54, (byte) 0x37, (byte) 0xc8
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080054 };
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080055 private static final MacAddress MAC_ADDRESS =
56 MacAddress.valueOf("11:22:33:44:55:66");
57
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080058 private static byte[] bytePacket;
59
Jonathan Hart2a655752015-04-07 16:46:33 -070060 private Deserializer<Redirect> deserializer = Redirect.deserializer();
61
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080062 @BeforeClass
63 public static void setUpBeforeClass() throws Exception {
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080064 byte[] byteHeader = {
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080065 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
66 (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18,
67 (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15,
68 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
69 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
70 (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18,
71 (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15,
72 (byte) 0xca, (byte) 0x2a, (byte) 0x14, (byte) 0xff,
73 (byte) 0xfe, (byte) 0x35, (byte) 0x26, (byte) 0xce,
74 (byte) 0x02, (byte) 0x01, (byte) 0x11, (byte) 0x22,
75 (byte) 0x33, (byte) 0x44, (byte) 0x55, (byte) 0x66
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080076 };
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080077 bytePacket = new byte[byteHeader.length];
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080078 System.arraycopy(byteHeader, 0, bytePacket, 0, byteHeader.length);
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080079 }
80
81 /**
82 * Tests serialize and setters.
83 */
84 @Test
85 public void testSerialize() {
86 Redirect rd = new Redirect();
87 rd.setTargetAddress(TARGET_ADDRESS);
88 rd.setDestinationAddress(DESTINATION_ADDRESS);
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080089 rd.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS,
90 MAC_ADDRESS.toBytes());
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080091
92 assertArrayEquals(rd.serialize(), bytePacket);
93 }
94
Charles Chan3599d632015-09-05 14:47:51 +080095 @Test
96 public void testDeserializeBadInput() throws Exception {
97 PacketTestUtils.testDeserializeBadInput(Redirect.deserializer());
98 }
99
100 @Test
101 public void testDeserializeTruncated() throws Exception {
102 // Run the truncation test only on the Redirect header
103 byte[] rdHeader = new byte[Redirect.HEADER_LENGTH];
104 ByteBuffer.wrap(bytePacket).get(rdHeader);
105
106 PacketTestUtils.testDeserializeTruncated(Redirect.deserializer(), rdHeader);
107 }
108
Charles M.C. Chanea5aa472015-01-03 13:40:39 +0800109 /**
110 * Tests deserialize and getters.
111 */
112 @Test
Jonathan Hart2a655752015-04-07 16:46:33 -0700113 public void testDeserialize() throws DeserializationException {
114 Redirect rd = deserializer.deserialize(bytePacket, 0, bytePacket.length);
Charles M.C. Chanea5aa472015-01-03 13:40:39 +0800115
116 assertArrayEquals(rd.getTargetAddress(), TARGET_ADDRESS);
117 assertArrayEquals(rd.getDestinationAddress(), DESTINATION_ADDRESS);
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800118
119 // Check the option(s)
120 assertThat(rd.getOptions().size(), is(1));
121 NeighborDiscoveryOptions.Option option = rd.getOptions().get(0);
122 assertThat(option.type(),
123 is(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS));
124 assertArrayEquals(option.data(), MAC_ADDRESS.toBytes());
Charles M.C. Chanea5aa472015-01-03 13:40:39 +0800125 }
126
127 /**
128 * Tests comparator.
129 */
130 @Test
131 public void testEqual() {
132 Redirect rd1 = new Redirect();
133 rd1.setTargetAddress(TARGET_ADDRESS);
134 rd1.setDestinationAddress(DESTINATION_ADDRESS);
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800135 rd1.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS,
136 MAC_ADDRESS.toBytes());
Charles M.C. Chanea5aa472015-01-03 13:40:39 +0800137
138 Redirect rd2 = new Redirect();
139 rd2.setTargetAddress(TARGET_ADDRESS);
140 rd2.setDestinationAddress(DESTINATION_ADDRESS2);
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800141 rd2.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS,
142 MAC_ADDRESS.toBytes());
Charles M.C. Chanea5aa472015-01-03 13:40:39 +0800143
144 assertTrue(rd1.equals(rd1));
145 assertFalse(rd1.equals(rd2));
146 }
Jian Li5fc14292015-12-04 11:30:46 -0800147
148 /**
149 * Tests toString.
150 */
151 @Test
152 public void testToStringRedirect() throws Exception {
153 Redirect rd = deserializer.deserialize(bytePacket, 0, bytePacket.length);
154 String str = rd.toString();
155
156 // TODO: need to handle TARGET_ADDRESS and DESTINATION_ADDRESS
157 }
Charles M.C. Chanea5aa472015-01-03 13:40:39 +0800158}