Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 1 | /* |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 3 | * |
| 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. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 16 | package org.onlab.packet.ndp; |
| 17 | |
| 18 | import org.junit.BeforeClass; |
| 19 | import org.junit.Test; |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 20 | import org.onlab.packet.DeserializationException; |
| 21 | import org.onlab.packet.Deserializer; |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 22 | import org.onlab.packet.MacAddress; |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 23 | |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 24 | import static org.hamcrest.Matchers.is; |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 25 | import static org.junit.Assert.assertArrayEquals; |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 26 | import static org.junit.Assert.assertFalse; |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 27 | import static org.junit.Assert.assertThat; |
| 28 | import static org.junit.Assert.assertTrue; |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * Tests for class {@link Redirect}. |
| 32 | */ |
| 33 | public class RedirectTest { |
| 34 | private static final byte[] TARGET_ADDRESS = { |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 35 | (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, |
| 36 | (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15, |
| 37 | (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, |
| 38 | (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 39 | }; |
| 40 | private static final byte[] DESTINATION_ADDRESS = { |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 41 | (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, |
| 42 | (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15, |
| 43 | (byte) 0xca, (byte) 0x2a, (byte) 0x14, (byte) 0xff, |
| 44 | (byte) 0xfe, (byte) 0x35, (byte) 0x26, (byte) 0xce |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 45 | }; |
| 46 | private static final byte[] DESTINATION_ADDRESS2 = { |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 47 | (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, |
| 48 | (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15, |
| 49 | (byte) 0xe6, (byte) 0xce, (byte) 0x8f, (byte) 0xff, |
| 50 | (byte) 0xfe, (byte) 0x54, (byte) 0x37, (byte) 0xc8 |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 51 | }; |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 52 | private static final MacAddress MAC_ADDRESS = |
| 53 | MacAddress.valueOf("11:22:33:44:55:66"); |
| 54 | |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 55 | private static byte[] bytePacket; |
| 56 | |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 57 | private Deserializer<Redirect> deserializer = Redirect.deserializer(); |
| 58 | |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 59 | @BeforeClass |
| 60 | public static void setUpBeforeClass() throws Exception { |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 61 | byte[] byteHeader = { |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 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) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, |
| 66 | (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, |
| 67 | (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, |
| 68 | (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15, |
| 69 | (byte) 0xca, (byte) 0x2a, (byte) 0x14, (byte) 0xff, |
| 70 | (byte) 0xfe, (byte) 0x35, (byte) 0x26, (byte) 0xce, |
| 71 | (byte) 0x02, (byte) 0x01, (byte) 0x11, (byte) 0x22, |
| 72 | (byte) 0x33, (byte) 0x44, (byte) 0x55, (byte) 0x66 |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 73 | }; |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 74 | bytePacket = new byte[byteHeader.length]; |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 75 | System.arraycopy(byteHeader, 0, bytePacket, 0, byteHeader.length); |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Tests serialize and setters. |
| 80 | */ |
| 81 | @Test |
| 82 | public void testSerialize() { |
| 83 | Redirect rd = new Redirect(); |
| 84 | rd.setTargetAddress(TARGET_ADDRESS); |
| 85 | rd.setDestinationAddress(DESTINATION_ADDRESS); |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 86 | rd.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS, |
| 87 | MAC_ADDRESS.toBytes()); |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 88 | |
| 89 | assertArrayEquals(rd.serialize(), bytePacket); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Tests deserialize and getters. |
| 94 | */ |
| 95 | @Test |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 96 | public void testDeserialize() throws DeserializationException { |
| 97 | Redirect rd = deserializer.deserialize(bytePacket, 0, bytePacket.length); |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 98 | |
| 99 | assertArrayEquals(rd.getTargetAddress(), TARGET_ADDRESS); |
| 100 | assertArrayEquals(rd.getDestinationAddress(), DESTINATION_ADDRESS); |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 101 | |
| 102 | // Check the option(s) |
| 103 | assertThat(rd.getOptions().size(), is(1)); |
| 104 | NeighborDiscoveryOptions.Option option = rd.getOptions().get(0); |
| 105 | assertThat(option.type(), |
| 106 | is(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS)); |
| 107 | assertArrayEquals(option.data(), MAC_ADDRESS.toBytes()); |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Tests comparator. |
| 112 | */ |
| 113 | @Test |
| 114 | public void testEqual() { |
| 115 | Redirect rd1 = new Redirect(); |
| 116 | rd1.setTargetAddress(TARGET_ADDRESS); |
| 117 | rd1.setDestinationAddress(DESTINATION_ADDRESS); |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 118 | rd1.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS, |
| 119 | MAC_ADDRESS.toBytes()); |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 120 | |
| 121 | Redirect rd2 = new Redirect(); |
| 122 | rd2.setTargetAddress(TARGET_ADDRESS); |
| 123 | rd2.setDestinationAddress(DESTINATION_ADDRESS2); |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 124 | rd2.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS, |
| 125 | MAC_ADDRESS.toBytes()); |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 126 | |
| 127 | assertTrue(rd1.equals(rd1)); |
| 128 | assertFalse(rd1.equals(rd2)); |
| 129 | } |
| 130 | } |