Charles M.C. Chan | 94f3737 | 2015-01-10 17:53:42 +0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame^] | 2 | * Copyright 2015-present Open Networking Foundation |
Charles M.C. Chan | 94f3737 | 2015-01-10 17:53:42 +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 | */ |
| 16 | |
| 17 | package org.onlab.packet.ipv6; |
| 18 | |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 19 | import org.apache.commons.lang3.StringUtils; |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 20 | import org.junit.Before; |
Charles M.C. Chan | 94f3737 | 2015-01-10 17:53:42 +0800 | [diff] [blame] | 21 | import org.junit.BeforeClass; |
| 22 | import org.junit.Test; |
| 23 | import org.onlab.packet.Data; |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 24 | import org.onlab.packet.Deserializer; |
Charles M.C. Chan | 94f3737 | 2015-01-10 17:53:42 +0800 | [diff] [blame] | 25 | import org.onlab.packet.IPv6; |
| 26 | import org.onlab.packet.UDP; |
| 27 | |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 28 | import java.util.Arrays; |
| 29 | |
Charles M.C. Chan | 94f3737 | 2015-01-10 17:53:42 +0800 | [diff] [blame] | 30 | import static org.hamcrest.Matchers.is; |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 31 | import static org.junit.Assert.*; |
Charles M.C. Chan | 94f3737 | 2015-01-10 17:53:42 +0800 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * Tests for class {@link BaseOptions}. |
| 35 | */ |
| 36 | public class BaseOptionsTest { |
| 37 | private static Data data; |
| 38 | private static UDP udp; |
| 39 | private static byte[] options = { |
| 40 | (byte) 0x00, (byte) 0x03, |
| 41 | (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0x00 |
| 42 | }; |
| 43 | private static byte[] bytePacket; |
| 44 | |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 45 | private Deserializer<BaseOptions> deserializer; |
| 46 | |
Charles M.C. Chan | 94f3737 | 2015-01-10 17:53:42 +0800 | [diff] [blame] | 47 | @BeforeClass |
| 48 | public static void setUpBeforeClass() throws Exception { |
| 49 | data = new Data(); |
| 50 | data.setData("testSerialize".getBytes()); |
| 51 | udp = new UDP(); |
| 52 | udp.setPayload(data); |
| 53 | |
| 54 | byte[] bytePayload = udp.serialize(); |
| 55 | byte[] byteHeader = { |
| 56 | (byte) 0x11, (byte) 0x00, (byte) 0x00, (byte) 0x03, |
| 57 | (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0x00 |
| 58 | }; |
| 59 | bytePacket = new byte[byteHeader.length + bytePayload.length]; |
| 60 | System.arraycopy(byteHeader, 0, bytePacket, 0, byteHeader.length); |
| 61 | System.arraycopy(bytePayload, 0, bytePacket, byteHeader.length, bytePayload.length); |
| 62 | } |
| 63 | |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 64 | @Before |
| 65 | public void setUp() { |
| 66 | deserializer = BaseOptions.deserializer(); |
| 67 | } |
| 68 | |
Charles M.C. Chan | 94f3737 | 2015-01-10 17:53:42 +0800 | [diff] [blame] | 69 | /** |
| 70 | * Tests serialize and setters. |
| 71 | */ |
| 72 | @Test |
| 73 | public void testSerialize() { |
| 74 | BaseOptions baseopt = new BaseOptions(); |
| 75 | baseopt.setNextHeader((byte) 0x11); |
| 76 | baseopt.setHeaderExtLength((byte) 0x00); |
| 77 | baseopt.setOptions(options); |
| 78 | baseopt.setPayload(udp); |
| 79 | |
| 80 | assertArrayEquals(baseopt.serialize(), bytePacket); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Tests deserialize and getters. |
| 85 | */ |
| 86 | @Test |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 87 | public void testDeserialize() throws Exception { |
| 88 | BaseOptions baseopt = deserializer.deserialize(bytePacket, 0, bytePacket.length); |
Charles M.C. Chan | 94f3737 | 2015-01-10 17:53:42 +0800 | [diff] [blame] | 89 | |
| 90 | assertThat(baseopt.getNextHeader(), is((byte) 0x11)); |
| 91 | assertThat(baseopt.getHeaderExtLength(), is((byte) 0x00)); |
| 92 | assertArrayEquals(baseopt.getOptions(), options); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Tests comparator. |
| 97 | */ |
| 98 | @Test |
| 99 | public void testEqual() { |
| 100 | BaseOptions baseopt1 = new BaseOptions(); |
| 101 | baseopt1.setNextHeader((byte) 0x11); |
| 102 | baseopt1.setHeaderExtLength((byte) 0x00); |
| 103 | baseopt1.setOptions(options); |
| 104 | baseopt1.setType(IPv6.PROTOCOL_HOPOPT); |
| 105 | |
| 106 | BaseOptions baseopt2 = new BaseOptions(); |
| 107 | baseopt2.setNextHeader((byte) 0x11); |
| 108 | baseopt2.setHeaderExtLength((byte) 0x00); |
| 109 | baseopt2.setOptions(options); |
| 110 | baseopt1.setType(IPv6.PROTOCOL_DSTOPT); |
| 111 | |
| 112 | assertTrue(baseopt1.equals(baseopt1)); |
| 113 | assertFalse(baseopt1.equals(baseopt2)); |
| 114 | } |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 115 | |
| 116 | /** |
| 117 | * Tests toString. |
| 118 | */ |
| 119 | @Test |
| 120 | public void testToStringBaseOptions() throws Exception { |
| 121 | BaseOptions baseopt = deserializer.deserialize(bytePacket, 0, bytePacket.length); |
| 122 | String str = baseopt.toString(); |
| 123 | |
| 124 | assertTrue(StringUtils.contains(str, "nextHeader=" + (byte) 0x11)); |
| 125 | assertTrue(StringUtils.contains(str, "headerExtLength=" + (byte) 0x00)); |
| 126 | assertTrue(StringUtils.contains(str, "options=" + Arrays.toString(options))); |
| 127 | } |
Charles M.C. Chan | 94f3737 | 2015-01-10 17:53:42 +0800 | [diff] [blame] | 128 | } |