Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present 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.Deserializer; |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 21 | import org.onlab.packet.MacAddress; |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 22 | import org.onlab.packet.PacketTestUtils; |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 23 | |
Charles Chan | 3599d63 | 2015-09-05 14:47:51 +0800 | [diff] [blame] | 24 | import java.nio.ByteBuffer; |
| 25 | |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 26 | import static org.hamcrest.Matchers.is; |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 27 | import static org.junit.Assert.*; |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 28 | |
| 29 | /** |
| 30 | * Tests for class {@link RouterSolicitation}. |
| 31 | */ |
| 32 | public class RouterSolicitationTest { |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 33 | private static final MacAddress MAC_ADDRESS1 = |
| 34 | MacAddress.valueOf("11:22:33:44:55:66"); |
| 35 | private static final MacAddress MAC_ADDRESS2 = |
| 36 | MacAddress.valueOf("11:22:33:44:55:00"); |
| 37 | |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 38 | private static byte[] bytePacket; |
| 39 | |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 40 | private Deserializer<RouterSolicitation> deserializer |
| 41 | = RouterSolicitation.deserializer(); |
| 42 | |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 43 | @BeforeClass |
| 44 | public static void setUpBeforeClass() throws Exception { |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 45 | byte[] byteHeader = { |
| 46 | (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, |
| 47 | (byte) 0x02, (byte) 0x01, (byte) 0x11, (byte) 0x22, |
| 48 | (byte) 0x33, (byte) 0x44, (byte) 0x55, (byte) 0x66 |
| 49 | }; |
| 50 | bytePacket = new byte[byteHeader.length]; |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 51 | System.arraycopy(byteHeader, 0, bytePacket, 0, byteHeader.length); |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Tests serialize and setters. |
| 56 | */ |
| 57 | @Test |
| 58 | public void testSerialize() { |
| 59 | RouterSolicitation rs = new RouterSolicitation(); |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 60 | rs.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS, |
| 61 | MAC_ADDRESS1.toBytes()); |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 62 | |
| 63 | assertArrayEquals(rs.serialize(), bytePacket); |
| 64 | } |
| 65 | |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 66 | @Test |
| 67 | public void testDeserializeBadInput() throws Exception { |
| 68 | PacketTestUtils.testDeserializeBadInput(RouterSolicitation.deserializer()); |
| 69 | } |
| 70 | |
| 71 | @Test |
| 72 | public void testDeserializeTruncated() throws Exception { |
Charles Chan | 3599d63 | 2015-09-05 14:47:51 +0800 | [diff] [blame] | 73 | // Run the truncation test only on the RouterSolicitation header |
| 74 | byte[] rsHeader = new byte[RouterSolicitation.HEADER_LENGTH]; |
| 75 | ByteBuffer.wrap(bytePacket).get(rsHeader); |
| 76 | |
| 77 | PacketTestUtils.testDeserializeTruncated(RouterSolicitation.deserializer(), rsHeader); |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 80 | /** |
| 81 | * Tests deserialize and getters. |
| 82 | */ |
| 83 | @Test |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 84 | public void testDeserialize() throws Exception { |
| 85 | RouterSolicitation rs = deserializer.deserialize(bytePacket, 0, bytePacket.length); |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 86 | |
| 87 | // Check the option(s) |
| 88 | assertThat(rs.getOptions().size(), is(1)); |
| 89 | NeighborDiscoveryOptions.Option option = rs.getOptions().get(0); |
| 90 | assertThat(option.type(), |
| 91 | is(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS)); |
| 92 | assertArrayEquals(option.data(), MAC_ADDRESS1.toBytes()); |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Tests comparator. |
| 97 | */ |
| 98 | @Test |
| 99 | public void testEqual() { |
| 100 | RouterSolicitation rs1 = new RouterSolicitation(); |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 101 | rs1.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS, |
| 102 | MAC_ADDRESS1.toBytes()); |
| 103 | |
| 104 | RouterSolicitation rs2 = new RouterSolicitation(); |
| 105 | rs2.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS, |
| 106 | MAC_ADDRESS2.toBytes()); |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 107 | |
| 108 | assertTrue(rs1.equals(rs1)); |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 109 | assertFalse(rs1.equals(rs2)); |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 110 | } |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 111 | |
| 112 | /** |
| 113 | * Tests toString. |
| 114 | */ |
| 115 | @Test |
| 116 | public void testToStringRS() throws Exception { |
| 117 | RouterSolicitation rs = deserializer.deserialize(bytePacket, 0, bytePacket.length); |
| 118 | String str = rs.toString(); |
| 119 | |
| 120 | // TODO: need to handle Options |
| 121 | } |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 122 | } |