Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
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 | |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 18 | import org.apache.commons.lang3.StringUtils; |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 19 | import org.junit.BeforeClass; |
| 20 | import org.junit.Test; |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 21 | import org.onlab.packet.DeserializationException; |
| 22 | import org.onlab.packet.Deserializer; |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 23 | import org.onlab.packet.MacAddress; |
Charles Chan | 3599d63 | 2015-09-05 14:47:51 +0800 | [diff] [blame] | 24 | import org.onlab.packet.PacketTestUtils; |
| 25 | |
| 26 | import java.nio.ByteBuffer; |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 27 | |
| 28 | import static org.hamcrest.Matchers.is; |
| 29 | import static org.junit.Assert.assertArrayEquals; |
| 30 | import static org.junit.Assert.assertFalse; |
| 31 | import static org.junit.Assert.assertThat; |
| 32 | import static org.junit.Assert.assertTrue; |
| 33 | |
| 34 | /** |
| 35 | * Tests for class {@link RouterAdvertisement}. |
| 36 | */ |
| 37 | public class RouterAdvertisementTest { |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 38 | private static final MacAddress MAC_ADDRESS = |
| 39 | MacAddress.valueOf("11:22:33:44:55:66"); |
| 40 | |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 41 | private static byte[] bytePacket; |
| 42 | |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 43 | private Deserializer<RouterAdvertisement> deserializer |
| 44 | = RouterAdvertisement.deserializer(); |
| 45 | |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 46 | @BeforeClass |
| 47 | public static void setUpBeforeClass() throws Exception { |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 48 | byte[] byteHeader = { |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 49 | (byte) 0x03, (byte) 0xc0, (byte) 0x02, (byte) 0x58, |
| 50 | (byte) 0x00, (byte) 0x00, (byte) 0x03, (byte) 0xe8, |
| 51 | (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0xf4, |
| 52 | (byte) 0x02, (byte) 0x01, (byte) 0x11, (byte) 0x22, |
| 53 | (byte) 0x33, (byte) 0x44, (byte) 0x55, (byte) 0x66 |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 54 | }; |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 55 | bytePacket = new byte[byteHeader.length]; |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 56 | System.arraycopy(byteHeader, 0, bytePacket, 0, byteHeader.length); |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Tests serialize and setters. |
| 61 | */ |
| 62 | @Test |
| 63 | public void testSerialize() { |
| 64 | RouterAdvertisement ra = new RouterAdvertisement(); |
| 65 | ra.setCurrentHopLimit((byte) 3); |
| 66 | ra.setMFlag((byte) 1); |
| 67 | ra.setOFlag((byte) 1); |
| 68 | ra.setRouterLifetime((short) 0x258); |
| 69 | ra.setReachableTime(0x3e8); |
| 70 | ra.setRetransmitTimer(0x1f4); |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 71 | ra.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS, |
| 72 | MAC_ADDRESS.toBytes()); |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 73 | |
| 74 | assertArrayEquals(ra.serialize(), bytePacket); |
| 75 | } |
| 76 | |
Charles Chan | 3599d63 | 2015-09-05 14:47:51 +0800 | [diff] [blame] | 77 | @Test |
| 78 | public void testDeserializeBadInput() throws Exception { |
| 79 | PacketTestUtils.testDeserializeBadInput(RouterAdvertisement.deserializer()); |
| 80 | } |
| 81 | |
| 82 | @Test |
| 83 | public void testDeserializeTruncated() throws Exception { |
| 84 | // Run the truncation test only on the RouterAdvertisement header |
| 85 | byte[] raHeader = new byte[RouterAdvertisement.HEADER_LENGTH]; |
| 86 | ByteBuffer.wrap(bytePacket).get(raHeader); |
| 87 | |
| 88 | PacketTestUtils.testDeserializeTruncated(RouterAdvertisement.deserializer(), raHeader); |
| 89 | } |
| 90 | |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 91 | /** |
| 92 | * Tests deserialize and getters. |
| 93 | */ |
| 94 | @Test |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 95 | public void testDeserialize() throws DeserializationException { |
| 96 | RouterAdvertisement ra = deserializer.deserialize(bytePacket, 0, bytePacket.length); |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 97 | |
| 98 | assertThat(ra.getCurrentHopLimit(), is((byte) 3)); |
| 99 | assertThat(ra.getMFlag(), is((byte) 1)); |
| 100 | assertThat(ra.getOFlag(), is((byte) 1)); |
| 101 | assertThat(ra.getRouterLifetime(), is((short) 0x258)); |
| 102 | assertThat(ra.getReachableTime(), is(0x3e8)); |
| 103 | assertThat(ra.getRetransmitTimer(), is(0x1f4)); |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 104 | |
| 105 | // Check the option(s) |
| 106 | assertThat(ra.getOptions().size(), is(1)); |
| 107 | NeighborDiscoveryOptions.Option option = ra.getOptions().get(0); |
| 108 | assertThat(option.type(), |
| 109 | is(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS)); |
| 110 | assertArrayEquals(option.data(), MAC_ADDRESS.toBytes()); |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Tests comparator. |
| 115 | */ |
| 116 | @Test |
| 117 | public void testEqual() { |
| 118 | RouterAdvertisement ra1 = new RouterAdvertisement(); |
| 119 | ra1.setCurrentHopLimit((byte) 3); |
| 120 | ra1.setMFlag((byte) 1); |
| 121 | ra1.setOFlag((byte) 1); |
| 122 | ra1.setRouterLifetime((short) 0x258); |
| 123 | ra1.setReachableTime(0x3e8); |
| 124 | ra1.setRetransmitTimer(0x1f4); |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 125 | ra1.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS, |
| 126 | MAC_ADDRESS.toBytes()); |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 127 | |
| 128 | RouterAdvertisement ra2 = new RouterAdvertisement(); |
| 129 | ra2.setCurrentHopLimit((byte) 3); |
| 130 | ra2.setMFlag((byte) 0); |
| 131 | ra2.setOFlag((byte) 0); |
| 132 | ra2.setRouterLifetime((short) 0x1f4); |
| 133 | ra2.setReachableTime(0x3e8); |
| 134 | ra2.setRetransmitTimer(0x1f4); |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 135 | ra2.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS, |
| 136 | MAC_ADDRESS.toBytes()); |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 137 | |
| 138 | assertTrue(ra1.equals(ra1)); |
| 139 | assertFalse(ra1.equals(ra2)); |
| 140 | } |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 141 | |
| 142 | /** |
| 143 | * Tests toString. |
| 144 | */ |
| 145 | @Test |
| 146 | public void testToStringRA() throws Exception { |
| 147 | RouterAdvertisement ra = deserializer.deserialize(bytePacket, 0, bytePacket.length); |
| 148 | String str = ra.toString(); |
| 149 | |
| 150 | assertTrue(StringUtils.contains(str, "currentHopLimit=" + (byte) 3)); |
| 151 | assertTrue(StringUtils.contains(str, "mFlag=" + (byte) 1)); |
| 152 | assertTrue(StringUtils.contains(str, "oFlag=" + (byte) 1)); |
| 153 | assertTrue(StringUtils.contains(str, "routerLifetime=" + (short) 0x258)); |
| 154 | assertTrue(StringUtils.contains(str, "reachableTime=" + 0x3e8)); |
| 155 | assertTrue(StringUtils.contains(str, "retransmitTimer=" + 0x1f4)); |
| 156 | |
| 157 | // TODO: need to handle options |
| 158 | } |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 159 | } |