blob: 865f03a2dac94e71cb0fe4180005267b9941f910 [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
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080022import static org.hamcrest.Matchers.is;
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080023import static org.junit.Assert.assertArrayEquals;
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080024import static org.junit.Assert.assertFalse;
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080025import static org.junit.Assert.assertThat;
26import static org.junit.Assert.assertTrue;
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080027
28/**
29 * Tests for class {@link Redirect}.
30 */
31public class RedirectTest {
32 private static final byte[] TARGET_ADDRESS = {
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080033 (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18,
34 (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15,
35 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
36 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080037 };
38 private static final byte[] DESTINATION_ADDRESS = {
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080039 (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18,
40 (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15,
41 (byte) 0xca, (byte) 0x2a, (byte) 0x14, (byte) 0xff,
42 (byte) 0xfe, (byte) 0x35, (byte) 0x26, (byte) 0xce
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080043 };
44 private static final byte[] DESTINATION_ADDRESS2 = {
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080045 (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18,
46 (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15,
47 (byte) 0xe6, (byte) 0xce, (byte) 0x8f, (byte) 0xff,
48 (byte) 0xfe, (byte) 0x54, (byte) 0x37, (byte) 0xc8
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080049 };
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080050 private static final MacAddress MAC_ADDRESS =
51 MacAddress.valueOf("11:22:33:44:55:66");
52
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080053 private static byte[] bytePacket;
54
55 @BeforeClass
56 public static void setUpBeforeClass() throws Exception {
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080057 byte[] byteHeader = {
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080058 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
59 (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18,
60 (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15,
61 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
62 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
63 (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18,
64 (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15,
65 (byte) 0xca, (byte) 0x2a, (byte) 0x14, (byte) 0xff,
66 (byte) 0xfe, (byte) 0x35, (byte) 0x26, (byte) 0xce,
67 (byte) 0x02, (byte) 0x01, (byte) 0x11, (byte) 0x22,
68 (byte) 0x33, (byte) 0x44, (byte) 0x55, (byte) 0x66
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080069 };
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080070 bytePacket = new byte[byteHeader.length];
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080071 System.arraycopy(byteHeader, 0, bytePacket, 0, byteHeader.length);
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080072 }
73
74 /**
75 * Tests serialize and setters.
76 */
77 @Test
78 public void testSerialize() {
79 Redirect rd = new Redirect();
80 rd.setTargetAddress(TARGET_ADDRESS);
81 rd.setDestinationAddress(DESTINATION_ADDRESS);
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080082 rd.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS,
83 MAC_ADDRESS.toBytes());
Charles M.C. Chanea5aa472015-01-03 13:40:39 +080084
85 assertArrayEquals(rd.serialize(), bytePacket);
86 }
87
88 /**
89 * Tests deserialize and getters.
90 */
91 @Test
92 public void testDeserialize() {
93 Redirect rd = new Redirect();
94 rd.deserialize(bytePacket, 0, bytePacket.length);
95
96 assertArrayEquals(rd.getTargetAddress(), TARGET_ADDRESS);
97 assertArrayEquals(rd.getDestinationAddress(), DESTINATION_ADDRESS);
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080098
99 // Check the option(s)
100 assertThat(rd.getOptions().size(), is(1));
101 NeighborDiscoveryOptions.Option option = rd.getOptions().get(0);
102 assertThat(option.type(),
103 is(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS));
104 assertArrayEquals(option.data(), MAC_ADDRESS.toBytes());
Charles M.C. Chanea5aa472015-01-03 13:40:39 +0800105 }
106
107 /**
108 * Tests comparator.
109 */
110 @Test
111 public void testEqual() {
112 Redirect rd1 = new Redirect();
113 rd1.setTargetAddress(TARGET_ADDRESS);
114 rd1.setDestinationAddress(DESTINATION_ADDRESS);
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800115 rd1.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS,
116 MAC_ADDRESS.toBytes());
Charles M.C. Chanea5aa472015-01-03 13:40:39 +0800117
118 Redirect rd2 = new Redirect();
119 rd2.setTargetAddress(TARGET_ADDRESS);
120 rd2.setDestinationAddress(DESTINATION_ADDRESS2);
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800121 rd2.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS,
122 MAC_ADDRESS.toBytes());
Charles M.C. Chanea5aa472015-01-03 13:40:39 +0800123
124 assertTrue(rd1.equals(rd1));
125 assertFalse(rd1.equals(rd2));
126 }
127}