Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Laboratory |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +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 | |
| 18 | |
| 19 | package org.onlab.packet; |
| 20 | |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 21 | import org.apache.commons.lang3.StringUtils; |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 22 | import org.junit.Before; |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 23 | import org.junit.BeforeClass; |
| 24 | import org.junit.Test; |
| 25 | |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 26 | import java.nio.ByteBuffer; |
Pier Ventre | 78e73f6 | 2016-12-02 19:59:28 -0800 | [diff] [blame] | 27 | import java.util.Arrays; |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 28 | |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 29 | import static org.hamcrest.Matchers.is; |
Pier Ventre | 78e73f6 | 2016-12-02 19:59:28 -0800 | [diff] [blame] | 30 | import static org.junit.Assert.*; |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 31 | |
| 32 | /** |
| 33 | * Tests for class {@link IPv6}. |
| 34 | */ |
| 35 | public class IPv6Test { |
| 36 | private static final byte[] SOURCE_ADDRESS = { |
| 37 | (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15, |
| 38 | (byte) 0xca, (byte) 0x2a, (byte) 0x14, (byte) 0xff, (byte) 0xfe, (byte) 0x35, (byte) 0x26, (byte) 0xce |
| 39 | }; |
| 40 | private static final byte[] DESTINATION_ADDRESS = { |
| 41 | (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15, |
| 42 | (byte) 0xe6, (byte) 0xce, (byte) 0x8f, (byte) 0xff, (byte) 0xfe, (byte) 0x54, (byte) 0x37, (byte) 0xc8 |
| 43 | }; |
Pier Ventre | 78e73f6 | 2016-12-02 19:59:28 -0800 | [diff] [blame] | 44 | private static final byte[] SOLICITATION_NODE_ADDRESS = { |
| 45 | (byte) 0xff, (byte) 0x02, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, |
| 46 | (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0xff, (byte) 0x54, (byte) 0x37, (byte) 0xc8 |
| 47 | }; |
| 48 | private static final byte[] MULTICAST_ADDRESS = { |
| 49 | (byte) 0x33, (byte) 0x33, (byte) 0xfe, (byte) 0x54, (byte) 0x37, (byte) 0xc8 |
| 50 | }; |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 51 | private static Data data; |
| 52 | private static UDP udp; |
| 53 | private static byte[] bytePacket; |
| 54 | |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 55 | private Deserializer<IPv6> deserializer; |
| 56 | |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 57 | @BeforeClass |
| 58 | public static void setUpBeforeClass() throws Exception { |
| 59 | data = new Data(); |
| 60 | data.setData("testSerialize".getBytes()); |
| 61 | udp = new UDP(); |
| 62 | udp.setPayload(data); |
| 63 | |
| 64 | byte[] bytePayload = udp.serialize(); |
| 65 | byte[] byteHeader = { |
| 66 | (byte) 0x69, (byte) 0x31, (byte) 0x35, (byte) 0x79, |
| 67 | (byte) (bytePayload.length >> 8 & 0xff), (byte) (bytePayload.length & 0xff), |
| 68 | (byte) 0x11, (byte) 0x20, |
| 69 | (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15, |
| 70 | (byte) 0xca, (byte) 0x2a, (byte) 0x14, (byte) 0xff, (byte) 0xfe, (byte) 0x35, (byte) 0x26, (byte) 0xce, |
| 71 | (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15, |
| 72 | (byte) 0xe6, (byte) 0xce, (byte) 0x8f, (byte) 0xff, (byte) 0xfe, (byte) 0x54, (byte) 0x37, (byte) 0xc8, |
| 73 | }; |
| 74 | bytePacket = new byte[byteHeader.length + bytePayload.length]; |
| 75 | System.arraycopy(byteHeader, 0, bytePacket, 0, byteHeader.length); |
| 76 | System.arraycopy(bytePayload, 0, bytePacket, byteHeader.length, bytePayload.length); |
| 77 | } |
| 78 | |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 79 | @Before |
| 80 | public void setUp() { |
| 81 | deserializer = IPv6.deserializer(); |
| 82 | } |
| 83 | |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 84 | /** |
| 85 | * Tests serialize and setters. |
| 86 | */ |
| 87 | @Test |
| 88 | public void testSerialize() { |
| 89 | IPv6 ipv6 = new IPv6(); |
| 90 | ipv6.setPayload(udp); |
| 91 | ipv6.setVersion((byte) 6); |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 92 | ipv6.setTrafficClass((byte) 0x93); |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 93 | ipv6.setFlowLabel(0x13579); |
| 94 | ipv6.setNextHeader(IPv6.PROTOCOL_UDP); |
| 95 | ipv6.setHopLimit((byte) 32); |
| 96 | ipv6.setSourceAddress(SOURCE_ADDRESS); |
| 97 | ipv6.setDestinationAddress(DESTINATION_ADDRESS); |
| 98 | |
| 99 | assertArrayEquals(ipv6.serialize(), bytePacket); |
| 100 | } |
| 101 | |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 102 | @Test |
| 103 | public void testDeserializeBadInput() throws Exception { |
| 104 | PacketTestUtils.testDeserializeBadInput(deserializer); |
| 105 | } |
| 106 | |
| 107 | @Test |
| 108 | public void testDeserializeTruncated() throws Exception { |
| 109 | // Run the truncation test only on the IPv6 header |
| 110 | byte[] ipv6Header = new byte[IPv6.FIXED_HEADER_LENGTH]; |
| 111 | ByteBuffer.wrap(bytePacket).get(ipv6Header); |
| 112 | |
| 113 | PacketTestUtils.testDeserializeTruncated(deserializer, ipv6Header); |
| 114 | } |
| 115 | |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 116 | /** |
| 117 | * Tests deserialize and getters. |
| 118 | */ |
| 119 | @Test |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 120 | public void testDeserialize() throws DeserializationException { |
| 121 | IPv6 ipv6 = deserializer.deserialize(bytePacket, 0, bytePacket.length); |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 122 | |
| 123 | assertThat(ipv6.getVersion(), is((byte) 6)); |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 124 | assertThat(ipv6.getTrafficClass(), is((byte) 0x93)); |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 125 | assertThat(ipv6.getFlowLabel(), is(0x13579)); |
| 126 | assertThat(ipv6.getNextHeader(), is(IPv6.PROTOCOL_UDP)); |
| 127 | assertThat(ipv6.getHopLimit(), is((byte) 32)); |
| 128 | assertArrayEquals(ipv6.getSourceAddress(), SOURCE_ADDRESS); |
| 129 | assertArrayEquals(ipv6.getDestinationAddress(), DESTINATION_ADDRESS); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Tests comparator. |
| 134 | */ |
| 135 | @Test |
| 136 | public void testEqual() { |
| 137 | IPv6 packet1 = new IPv6(); |
| 138 | packet1.setPayload(udp); |
| 139 | packet1.setVersion((byte) 6); |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 140 | packet1.setTrafficClass((byte) 0x93); |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 141 | packet1.setFlowLabel(0x13579); |
| 142 | packet1.setNextHeader(IPv6.PROTOCOL_UDP); |
| 143 | packet1.setHopLimit((byte) 32); |
| 144 | packet1.setSourceAddress(SOURCE_ADDRESS); |
| 145 | packet1.setDestinationAddress(DESTINATION_ADDRESS); |
| 146 | |
| 147 | IPv6 packet2 = new IPv6(); |
| 148 | packet2.setPayload(udp); |
| 149 | packet2.setVersion((byte) 6); |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 150 | packet2.setTrafficClass((byte) 0x93); |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 151 | packet2.setFlowLabel(0x13579); |
| 152 | packet2.setNextHeader(IPv6.PROTOCOL_UDP); |
| 153 | packet2.setHopLimit((byte) 32); |
| 154 | packet2.setSourceAddress(DESTINATION_ADDRESS); |
| 155 | packet2.setDestinationAddress(SOURCE_ADDRESS); |
| 156 | |
| 157 | assertTrue(packet1.equals(packet1)); |
| 158 | assertFalse(packet1.equals(packet2)); |
| 159 | } |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 160 | |
| 161 | /** |
| 162 | * Tests toString. |
| 163 | */ |
| 164 | @Test |
| 165 | public void testToStringIPv6() throws Exception { |
| 166 | IPv6 ipv6 = deserializer.deserialize(bytePacket, 0, bytePacket.length); |
| 167 | String str = ipv6.toString(); |
| 168 | |
| 169 | assertTrue(StringUtils.contains(str, "version=" + (byte) 6)); |
| 170 | assertTrue(StringUtils.contains(str, "trafficClass=" + (byte) 0x93)); |
| 171 | assertTrue(StringUtils.contains(str, "flowLabel=" + 0x13579)); |
| 172 | assertTrue(StringUtils.contains(str, "nextHeader=" + IPv6.PROTOCOL_UDP)); |
| 173 | assertTrue(StringUtils.contains(str, "hopLimit=" + (byte) 32)); |
| 174 | // TODO: test IPv6 source and destination address |
| 175 | } |
Pier Ventre | 78e73f6 | 2016-12-02 19:59:28 -0800 | [diff] [blame] | 176 | |
| 177 | /** |
| 178 | * Tests the proper operation of the solicitationNodeAddress function. |
| 179 | */ |
| 180 | @Test |
| 181 | public void testSolicitationNodeAddress() { |
| 182 | assertTrue(Arrays.equals(SOLICITATION_NODE_ADDRESS, IPv6.solicitationNodeAddress(DESTINATION_ADDRESS))); |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Tests the proper operation of the multicastAddress function. |
| 187 | */ |
| 188 | @Test |
| 189 | public void testMulticastAddress() { |
| 190 | assertTrue(Arrays.equals(MULTICAST_ADDRESS, IPv6.multicastMacAddress(DESTINATION_ADDRESS))); |
| 191 | } |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 192 | } |