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