Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +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 | 7fee36a | 2014-12-31 00:19:59 +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 | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 24 | |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame^] | 25 | import static org.junit.Assert.assertArrayEquals; |
| 26 | import static org.junit.Assert.assertTrue; |
| 27 | import static org.junit.Assert.assertFalse; |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 28 | /** |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame^] | 29 | * Tests for class {@link NeighborSolicitation}. |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 30 | */ |
| 31 | public class NeighborSolicitationTest { |
| 32 | private static final byte[] TARGET_ADDRESS = { |
| 33 | (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15, |
| 34 | (byte) 0xca, (byte) 0x2a, (byte) 0x14, (byte) 0xff, (byte) 0xfe, (byte) 0x35, (byte) 0x26, (byte) 0xce |
| 35 | }; |
| 36 | private static final byte[] TARGET_ADDRESS2 = { |
| 37 | (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15, |
| 38 | (byte) 0xe6, (byte) 0xce, (byte) 0x8f, (byte) 0xff, (byte) 0xfe, (byte) 0x54, (byte) 0x37, (byte) 0xc8 |
| 39 | }; |
| 40 | private static Data data; |
| 41 | private static byte[] bytePacket; |
| 42 | |
| 43 | @BeforeClass |
| 44 | public static void setUpBeforeClass() throws Exception { |
| 45 | data = new Data(); |
| 46 | data.setData("".getBytes()); |
| 47 | |
| 48 | byte[] bytePayload = data.serialize(); |
| 49 | byte[] byteHeader = { |
| 50 | (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, |
| 51 | (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15, |
| 52 | (byte) 0xca, (byte) 0x2a, (byte) 0x14, (byte) 0xff, (byte) 0xfe, (byte) 0x35, (byte) 0x26, (byte) 0xce |
| 53 | }; |
| 54 | bytePacket = new byte[byteHeader.length + bytePayload.length]; |
| 55 | System.arraycopy(byteHeader, 0, bytePacket, 0, byteHeader.length); |
| 56 | System.arraycopy(bytePayload, 0, bytePacket, byteHeader.length, bytePayload.length); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Tests serialize and setters. |
| 61 | */ |
| 62 | @Test |
| 63 | public void testSerialize() { |
| 64 | NeighborSolicitation ns = new NeighborSolicitation(); |
| 65 | ns.setTargetAddress(TARGET_ADDRESS); |
Charles M.C. Chan | ea5aa47 | 2015-01-03 13:40:39 +0800 | [diff] [blame^] | 66 | ns.setPayload(data); |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 67 | |
| 68 | assertArrayEquals(ns.serialize(), bytePacket); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Tests deserialize and getters. |
| 73 | */ |
| 74 | @Test |
| 75 | public void testDeserialize() { |
| 76 | NeighborSolicitation ns = new NeighborSolicitation(); |
| 77 | ns.deserialize(bytePacket, 0, bytePacket.length); |
| 78 | |
| 79 | assertArrayEquals(ns.getTargetAddress(), TARGET_ADDRESS); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Tests comparator. |
| 84 | */ |
| 85 | @Test |
| 86 | public void testEqual() { |
| 87 | NeighborSolicitation ns1 = new NeighborSolicitation(); |
| 88 | ns1.setTargetAddress(TARGET_ADDRESS); |
| 89 | |
| 90 | NeighborSolicitation ns2 = new NeighborSolicitation(); |
| 91 | ns2.setTargetAddress(TARGET_ADDRESS2); |
| 92 | |
| 93 | assertTrue(ns1.equals(ns1)); |
| 94 | assertFalse(ns1.equals(ns2)); |
| 95 | } |
| 96 | } |