Charles M.C. Chan | 94f3737 | 2015-01-10 17:53:42 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014-2015 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 | package org.onlab.packet.ipv6; |
| 18 | |
| 19 | import org.junit.BeforeClass; |
| 20 | import org.junit.Test; |
| 21 | import org.onlab.packet.Data; |
| 22 | import org.onlab.packet.IPv6; |
| 23 | import org.onlab.packet.UDP; |
| 24 | |
| 25 | import static org.hamcrest.Matchers.is; |
| 26 | import static org.junit.Assert.assertArrayEquals; |
| 27 | import static org.junit.Assert.assertFalse; |
| 28 | import static org.junit.Assert.assertThat; |
| 29 | import static org.junit.Assert.assertTrue; |
| 30 | |
| 31 | /** |
| 32 | * Tests for class {@link BaseOptions}. |
| 33 | */ |
| 34 | public class BaseOptionsTest { |
| 35 | private static Data data; |
| 36 | private static UDP udp; |
| 37 | private static byte[] options = { |
| 38 | (byte) 0x00, (byte) 0x03, |
| 39 | (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0x00 |
| 40 | }; |
| 41 | private static byte[] bytePacket; |
| 42 | |
| 43 | @BeforeClass |
| 44 | public static void setUpBeforeClass() throws Exception { |
| 45 | data = new Data(); |
| 46 | data.setData("testSerialize".getBytes()); |
| 47 | udp = new UDP(); |
| 48 | udp.setPayload(data); |
| 49 | |
| 50 | byte[] bytePayload = udp.serialize(); |
| 51 | byte[] byteHeader = { |
| 52 | (byte) 0x11, (byte) 0x00, (byte) 0x00, (byte) 0x03, |
| 53 | (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0x00 |
| 54 | }; |
| 55 | bytePacket = new byte[byteHeader.length + bytePayload.length]; |
| 56 | System.arraycopy(byteHeader, 0, bytePacket, 0, byteHeader.length); |
| 57 | System.arraycopy(bytePayload, 0, bytePacket, byteHeader.length, bytePayload.length); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Tests serialize and setters. |
| 62 | */ |
| 63 | @Test |
| 64 | public void testSerialize() { |
| 65 | BaseOptions baseopt = new BaseOptions(); |
| 66 | baseopt.setNextHeader((byte) 0x11); |
| 67 | baseopt.setHeaderExtLength((byte) 0x00); |
| 68 | baseopt.setOptions(options); |
| 69 | baseopt.setPayload(udp); |
| 70 | |
| 71 | assertArrayEquals(baseopt.serialize(), bytePacket); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Tests deserialize and getters. |
| 76 | */ |
| 77 | @Test |
| 78 | public void testDeserialize() { |
| 79 | BaseOptions baseopt = new BaseOptions(); |
| 80 | baseopt.deserialize(bytePacket, 0, bytePacket.length); |
| 81 | |
| 82 | assertThat(baseopt.getNextHeader(), is((byte) 0x11)); |
| 83 | assertThat(baseopt.getHeaderExtLength(), is((byte) 0x00)); |
| 84 | assertArrayEquals(baseopt.getOptions(), options); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Tests comparator. |
| 89 | */ |
| 90 | @Test |
| 91 | public void testEqual() { |
| 92 | BaseOptions baseopt1 = new BaseOptions(); |
| 93 | baseopt1.setNextHeader((byte) 0x11); |
| 94 | baseopt1.setHeaderExtLength((byte) 0x00); |
| 95 | baseopt1.setOptions(options); |
| 96 | baseopt1.setType(IPv6.PROTOCOL_HOPOPT); |
| 97 | |
| 98 | BaseOptions baseopt2 = new BaseOptions(); |
| 99 | baseopt2.setNextHeader((byte) 0x11); |
| 100 | baseopt2.setHeaderExtLength((byte) 0x00); |
| 101 | baseopt2.setOptions(options); |
| 102 | baseopt1.setType(IPv6.PROTOCOL_DSTOPT); |
| 103 | |
| 104 | assertTrue(baseopt1.equals(baseopt1)); |
| 105 | assertFalse(baseopt1.equals(baseopt2)); |
| 106 | } |
| 107 | } |