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