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 | |
| 24 | import static org.hamcrest.Matchers.is; |
| 25 | import static org.junit.Assert.assertArrayEquals; |
| 26 | import static org.junit.Assert.assertFalse; |
| 27 | import static org.junit.Assert.assertThat; |
| 28 | import static org.junit.Assert.assertTrue; |
| 29 | |
| 30 | /** |
| 31 | * Tests for class {@link RouterAdvertisement}. |
| 32 | */ |
| 33 | public class RouterAdvertisementTest { |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 34 | private static final MacAddress MAC_ADDRESS = |
| 35 | MacAddress.valueOf("11:22:33:44:55:66"); |
| 36 | |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 37 | private static byte[] bytePacket; |
| 38 | |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 39 | private Deserializer<RouterAdvertisement> deserializer |
| 40 | = RouterAdvertisement.deserializer(); |
| 41 | |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 42 | @BeforeClass |
| 43 | public static void setUpBeforeClass() throws Exception { |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 44 | byte[] byteHeader = { |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 45 | (byte) 0x03, (byte) 0xc0, (byte) 0x02, (byte) 0x58, |
| 46 | (byte) 0x00, (byte) 0x00, (byte) 0x03, (byte) 0xe8, |
| 47 | (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0xf4, |
| 48 | (byte) 0x02, (byte) 0x01, (byte) 0x11, (byte) 0x22, |
| 49 | (byte) 0x33, (byte) 0x44, (byte) 0x55, (byte) 0x66 |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 50 | }; |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 51 | bytePacket = new byte[byteHeader.length]; |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 52 | System.arraycopy(byteHeader, 0, bytePacket, 0, byteHeader.length); |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Tests serialize and setters. |
| 57 | */ |
| 58 | @Test |
| 59 | public void testSerialize() { |
| 60 | RouterAdvertisement ra = new RouterAdvertisement(); |
| 61 | ra.setCurrentHopLimit((byte) 3); |
| 62 | ra.setMFlag((byte) 1); |
| 63 | ra.setOFlag((byte) 1); |
| 64 | ra.setRouterLifetime((short) 0x258); |
| 65 | ra.setReachableTime(0x3e8); |
| 66 | ra.setRetransmitTimer(0x1f4); |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 67 | ra.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS, |
| 68 | MAC_ADDRESS.toBytes()); |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 69 | |
| 70 | assertArrayEquals(ra.serialize(), bytePacket); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Tests deserialize and getters. |
| 75 | */ |
| 76 | @Test |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 77 | public void testDeserialize() throws DeserializationException { |
| 78 | RouterAdvertisement ra = deserializer.deserialize(bytePacket, 0, bytePacket.length); |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 79 | |
| 80 | assertThat(ra.getCurrentHopLimit(), is((byte) 3)); |
| 81 | assertThat(ra.getMFlag(), is((byte) 1)); |
| 82 | assertThat(ra.getOFlag(), is((byte) 1)); |
| 83 | assertThat(ra.getRouterLifetime(), is((short) 0x258)); |
| 84 | assertThat(ra.getReachableTime(), is(0x3e8)); |
| 85 | assertThat(ra.getRetransmitTimer(), is(0x1f4)); |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 86 | |
| 87 | // Check the option(s) |
| 88 | assertThat(ra.getOptions().size(), is(1)); |
| 89 | NeighborDiscoveryOptions.Option option = ra.getOptions().get(0); |
| 90 | assertThat(option.type(), |
| 91 | is(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS)); |
| 92 | assertArrayEquals(option.data(), MAC_ADDRESS.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 | RouterAdvertisement ra1 = new RouterAdvertisement(); |
| 101 | ra1.setCurrentHopLimit((byte) 3); |
| 102 | ra1.setMFlag((byte) 1); |
| 103 | ra1.setOFlag((byte) 1); |
| 104 | ra1.setRouterLifetime((short) 0x258); |
| 105 | ra1.setReachableTime(0x3e8); |
| 106 | ra1.setRetransmitTimer(0x1f4); |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 107 | ra1.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS, |
| 108 | MAC_ADDRESS.toBytes()); |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 109 | |
| 110 | RouterAdvertisement ra2 = new RouterAdvertisement(); |
| 111 | ra2.setCurrentHopLimit((byte) 3); |
| 112 | ra2.setMFlag((byte) 0); |
| 113 | ra2.setOFlag((byte) 0); |
| 114 | ra2.setRouterLifetime((short) 0x1f4); |
| 115 | ra2.setReachableTime(0x3e8); |
| 116 | ra2.setRetransmitTimer(0x1f4); |
Pavlin Radoslavov | a2626ef | 2015-02-18 18:33:25 -0800 | [diff] [blame] | 117 | ra2.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS, |
| 118 | MAC_ADDRESS.toBytes()); |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame] | 119 | |
| 120 | assertTrue(ra1.equals(ra1)); |
| 121 | assertFalse(ra1.equals(ra2)); |
| 122 | } |
| 123 | } |