Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Open Networking Laboratory |
| 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 | */ |
| 16 | |
| 17 | |
| 18 | |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame^] | 19 | package org.onlab.packet.ndp; |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 20 | |
| 21 | import org.junit.BeforeClass; |
| 22 | import org.junit.Test; |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame^] | 23 | import org.onlab.packet.Data; |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 24 | |
| 25 | import static org.hamcrest.Matchers.is; |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame^] | 26 | import static org.junit.Assert.assertArrayEquals; |
| 27 | import static org.junit.Assert.assertTrue; |
| 28 | import static org.junit.Assert.assertFalse; |
| 29 | import static org.junit.Assert.assertThat; |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 30 | |
| 31 | /** |
| 32 | * Tests for class {@link NeighborAdvertisement}. |
| 33 | */ |
| 34 | public class NeighborAdvertisementTest { |
| 35 | private static final byte[] TARGET_ADDRESS = { |
| 36 | (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15, |
| 37 | (byte) 0xca, (byte) 0x2a, (byte) 0x14, (byte) 0xff, (byte) 0xfe, (byte) 0x35, (byte) 0x26, (byte) 0xce |
| 38 | }; |
| 39 | private static Data data; |
| 40 | private static byte[] bytePacket; |
| 41 | |
| 42 | @BeforeClass |
| 43 | public static void setUpBeforeClass() throws Exception { |
| 44 | data = new Data(); |
| 45 | data.setData("".getBytes()); |
| 46 | |
| 47 | byte[] bytePayload = data.serialize(); |
| 48 | byte[] byteHeader = { |
| 49 | (byte) 0xe0, (byte) 0x00, (byte) 0x00, (byte) 0x00, |
| 50 | (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15, |
| 51 | (byte) 0xca, (byte) 0x2a, (byte) 0x14, (byte) 0xff, (byte) 0xfe, (byte) 0x35, (byte) 0x26, (byte) 0xce |
| 52 | }; |
| 53 | bytePacket = new byte[byteHeader.length + bytePayload.length]; |
| 54 | System.arraycopy(byteHeader, 0, bytePacket, 0, byteHeader.length); |
| 55 | System.arraycopy(bytePayload, 0, bytePacket, byteHeader.length, bytePayload.length); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Tests serialize and setters. |
| 60 | */ |
| 61 | @Test |
| 62 | public void testSerialize() { |
| 63 | NeighborAdvertisement na = new NeighborAdvertisement(); |
| 64 | na.setRouterFlag((byte) 1); |
| 65 | na.setSolicitedFlag((byte) 1); |
| 66 | na.setOverrideFlag((byte) 1); |
| 67 | na.setTargetAddress(TARGET_ADDRESS); |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame^] | 68 | na.setPayload(data); |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 69 | |
| 70 | assertArrayEquals(na.serialize(), bytePacket); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Tests deserialize and getters. |
| 75 | */ |
| 76 | @Test |
| 77 | public void testDeserialize() { |
| 78 | NeighborAdvertisement na = new NeighborAdvertisement(); |
| 79 | na.deserialize(bytePacket, 0, bytePacket.length); |
| 80 | |
| 81 | assertThat(na.getRouterFlag(), is((byte) 1)); |
| 82 | assertThat(na.getSolicitedFlag(), is((byte) 1)); |
| 83 | assertThat(na.getOverrideFlag(), is((byte) 1)); |
| 84 | assertArrayEquals(na.getTargetAddress(), TARGET_ADDRESS); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Tests comparator. |
| 89 | */ |
| 90 | @Test |
| 91 | public void testEqual() { |
| 92 | NeighborAdvertisement na1 = new NeighborAdvertisement(); |
| 93 | na1.setRouterFlag((byte) 1); |
| 94 | na1.setSolicitedFlag((byte) 1); |
| 95 | na1.setOverrideFlag((byte) 1); |
| 96 | na1.setTargetAddress(TARGET_ADDRESS); |
| 97 | |
| 98 | NeighborAdvertisement na2 = new NeighborAdvertisement(); |
| 99 | na2.setRouterFlag((byte) 1); |
| 100 | na2.setSolicitedFlag((byte) 1); |
| 101 | na2.setOverrideFlag((byte) 0); |
| 102 | na2.setTargetAddress(TARGET_ADDRESS); |
| 103 | |
| 104 | assertTrue(na1.equals(na1)); |
| 105 | assertFalse(na1.equals(na2)); |
| 106 | } |
| 107 | } |