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; |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame^] | 20 | import org.onlab.packet.MacAddress; |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 21 | |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame^] | 22 | import static org.hamcrest.Matchers.is; |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 23 | import static org.junit.Assert.assertArrayEquals; |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame^] | 24 | import static org.junit.Assert.assertFalse; |
| 25 | import static org.junit.Assert.assertThat; |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 26 | import static org.junit.Assert.assertTrue; |
| 27 | |
| 28 | /** |
| 29 | * Tests for class {@link RouterSolicitation}. |
| 30 | */ |
| 31 | public class RouterSolicitationTest { |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame^] | 32 | private static final MacAddress MAC_ADDRESS1 = |
| 33 | MacAddress.valueOf("11:22:33:44:55:66"); |
| 34 | private static final MacAddress MAC_ADDRESS2 = |
| 35 | MacAddress.valueOf("11:22:33:44:55:00"); |
| 36 | |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 37 | private static byte[] bytePacket; |
| 38 | |
| 39 | @BeforeClass |
| 40 | public static void setUpBeforeClass() throws Exception { |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame^] | 41 | byte[] byteHeader = { |
| 42 | (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, |
| 43 | (byte) 0x02, (byte) 0x01, (byte) 0x11, (byte) 0x22, |
| 44 | (byte) 0x33, (byte) 0x44, (byte) 0x55, (byte) 0x66 |
| 45 | }; |
| 46 | bytePacket = new byte[byteHeader.length]; |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 47 | System.arraycopy(byteHeader, 0, bytePacket, 0, byteHeader.length); |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Tests serialize and setters. |
| 52 | */ |
| 53 | @Test |
| 54 | public void testSerialize() { |
| 55 | RouterSolicitation rs = new RouterSolicitation(); |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame^] | 56 | rs.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS, |
| 57 | MAC_ADDRESS1.toBytes()); |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 58 | |
| 59 | assertArrayEquals(rs.serialize(), bytePacket); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Tests deserialize and getters. |
| 64 | */ |
| 65 | @Test |
| 66 | public void testDeserialize() { |
| 67 | RouterSolicitation rs = new RouterSolicitation(); |
| 68 | rs.deserialize(bytePacket, 0, bytePacket.length); |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame^] | 69 | |
| 70 | // Check the option(s) |
| 71 | assertThat(rs.getOptions().size(), is(1)); |
| 72 | NeighborDiscoveryOptions.Option option = rs.getOptions().get(0); |
| 73 | assertThat(option.type(), |
| 74 | is(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS)); |
| 75 | assertArrayEquals(option.data(), MAC_ADDRESS1.toBytes()); |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Tests comparator. |
| 80 | */ |
| 81 | @Test |
| 82 | public void testEqual() { |
| 83 | RouterSolicitation rs1 = new RouterSolicitation(); |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame^] | 84 | rs1.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS, |
| 85 | MAC_ADDRESS1.toBytes()); |
| 86 | |
| 87 | RouterSolicitation rs2 = new RouterSolicitation(); |
| 88 | rs2.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS, |
| 89 | MAC_ADDRESS2.toBytes()); |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 90 | |
| 91 | assertTrue(rs1.equals(rs1)); |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame^] | 92 | assertFalse(rs1.equals(rs2)); |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 93 | } |
| 94 | } |