Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +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 | 93b7fb0 | 2014-12-28 03:59:36 +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; |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 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 | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 23 | |
| 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; |
| 27 | import static org.junit.Assert.assertThat; |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 28 | import static org.junit.Assert.assertTrue; |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * Tests for class {@link NeighborAdvertisement}. |
| 32 | */ |
| 33 | public class NeighborAdvertisementTest { |
| 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) 0xca, (byte) 0x2a, (byte) 0x14, (byte) 0xff, |
| 38 | (byte) 0xfe, (byte) 0x35, (byte) 0x26, (byte) 0xce |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 39 | }; |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 40 | private static final MacAddress MAC_ADDRESS = |
| 41 | MacAddress.valueOf("11:22:33:44:55:66"); |
| 42 | |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 43 | private static byte[] bytePacket; |
| 44 | |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 45 | private Deserializer<NeighborAdvertisement> deserializer |
| 46 | = NeighborAdvertisement.deserializer(); |
| 47 | |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 48 | @BeforeClass |
| 49 | public static void setUpBeforeClass() throws Exception { |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 50 | byte[] byteHeader = { |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 51 | (byte) 0xe0, (byte) 0x00, (byte) 0x00, (byte) 0x00, |
| 52 | (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, |
| 53 | (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15, |
| 54 | (byte) 0xca, (byte) 0x2a, (byte) 0x14, (byte) 0xff, |
| 55 | (byte) 0xfe, (byte) 0x35, (byte) 0x26, (byte) 0xce, |
| 56 | (byte) 0x02, (byte) 0x01, (byte) 0x11, (byte) 0x22, |
| 57 | (byte) 0x33, (byte) 0x44, (byte) 0x55, (byte) 0x66 |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 58 | }; |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 59 | bytePacket = new byte[byteHeader.length]; |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 60 | System.arraycopy(byteHeader, 0, bytePacket, 0, byteHeader.length); |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Tests serialize and setters. |
| 65 | */ |
| 66 | @Test |
| 67 | public void testSerialize() { |
| 68 | NeighborAdvertisement na = new NeighborAdvertisement(); |
| 69 | na.setRouterFlag((byte) 1); |
| 70 | na.setSolicitedFlag((byte) 1); |
| 71 | na.setOverrideFlag((byte) 1); |
| 72 | na.setTargetAddress(TARGET_ADDRESS); |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 73 | na.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS, |
| 74 | MAC_ADDRESS.toBytes()); |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 75 | |
| 76 | assertArrayEquals(na.serialize(), bytePacket); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Tests deserialize and getters. |
| 81 | */ |
| 82 | @Test |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 83 | public void testDeserialize() throws DeserializationException { |
| 84 | NeighborAdvertisement na = deserializer.deserialize(bytePacket, 0, bytePacket.length); |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 85 | |
| 86 | assertThat(na.getRouterFlag(), is((byte) 1)); |
| 87 | assertThat(na.getSolicitedFlag(), is((byte) 1)); |
| 88 | assertThat(na.getOverrideFlag(), is((byte) 1)); |
| 89 | assertArrayEquals(na.getTargetAddress(), TARGET_ADDRESS); |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 90 | |
| 91 | // Check the option(s) |
| 92 | assertThat(na.getOptions().size(), is(1)); |
| 93 | NeighborDiscoveryOptions.Option option = na.getOptions().get(0); |
| 94 | assertThat(option.type(), |
| 95 | is(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS)); |
| 96 | assertArrayEquals(option.data(), MAC_ADDRESS.toBytes()); |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Tests comparator. |
| 101 | */ |
| 102 | @Test |
| 103 | public void testEqual() { |
| 104 | NeighborAdvertisement na1 = new NeighborAdvertisement(); |
| 105 | na1.setRouterFlag((byte) 1); |
| 106 | na1.setSolicitedFlag((byte) 1); |
| 107 | na1.setOverrideFlag((byte) 1); |
| 108 | na1.setTargetAddress(TARGET_ADDRESS); |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 109 | na1.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS, |
| 110 | MAC_ADDRESS.toBytes()); |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 111 | |
| 112 | NeighborAdvertisement na2 = new NeighborAdvertisement(); |
| 113 | na2.setRouterFlag((byte) 1); |
| 114 | na2.setSolicitedFlag((byte) 1); |
| 115 | na2.setOverrideFlag((byte) 0); |
| 116 | na2.setTargetAddress(TARGET_ADDRESS); |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 117 | na2.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS, |
| 118 | MAC_ADDRESS.toBytes()); |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 119 | |
| 120 | assertTrue(na1.equals(na1)); |
| 121 | assertFalse(na1.equals(na2)); |
| 122 | } |
| 123 | } |