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; |
| 27 | |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 28 | import static org.hamcrest.Matchers.is; |
Pier Ventre | 78e73f6 | 2016-12-02 19:59:28 -0800 | [diff] [blame] | 29 | import static org.junit.Assert.*; |
Pier Luigi | 37b687b | 2017-01-16 22:46:33 -0800 | [diff] [blame] | 30 | import static org.onlab.packet.IPv6.*; |
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 | }; |
Pier Luigi | 37b687b | 2017-01-16 22:46:33 -0800 | [diff] [blame] | 51 | private static final byte[] LINK_LOCAL_ADDRESS_1 = { |
| 52 | (byte) 0xfe, (byte) 0x80, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, |
| 53 | (byte) 0x50, (byte) 0x74, (byte) 0xf2, (byte) 0xff, (byte) 0xfe, (byte) 0xb1, (byte) 0xa8, (byte) 0x7f |
| 54 | }; |
| 55 | private static final byte[] MAC_ADDRESS_1 = { |
| 56 | (byte) 0x52, (byte) 0x74, (byte) 0xf2, (byte) 0xb1, (byte) 0xa8, (byte) 0x7f |
| 57 | }; |
| 58 | private static final byte[] LINK_LOCAL_ADDRESS_2 = { |
| 59 | (byte) 0xfe, (byte) 0x80, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, |
| 60 | (byte) 0x02, (byte) 0x0c, (byte) 0x29, (byte) 0xff, (byte) 0xfe, (byte) 0x8f, (byte) 0x99, (byte) 0xd2 |
| 61 | }; |
| 62 | private static final byte[] MAC_ADDRESS_2 = { |
| 63 | (byte) 0x00, (byte) 0x0c, (byte) 0x29, (byte) 0x8f, (byte) 0x99, (byte) 0xd2 |
| 64 | }; |
| 65 | |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 66 | private static Data data; |
| 67 | private static UDP udp; |
| 68 | private static byte[] bytePacket; |
| 69 | |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 70 | private Deserializer<IPv6> deserializer; |
| 71 | |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 72 | @BeforeClass |
| 73 | public static void setUpBeforeClass() throws Exception { |
| 74 | data = new Data(); |
| 75 | data.setData("testSerialize".getBytes()); |
| 76 | udp = new UDP(); |
| 77 | udp.setPayload(data); |
| 78 | |
| 79 | byte[] bytePayload = udp.serialize(); |
| 80 | byte[] byteHeader = { |
| 81 | (byte) 0x69, (byte) 0x31, (byte) 0x35, (byte) 0x79, |
| 82 | (byte) (bytePayload.length >> 8 & 0xff), (byte) (bytePayload.length & 0xff), |
| 83 | (byte) 0x11, (byte) 0x20, |
| 84 | (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15, |
| 85 | (byte) 0xca, (byte) 0x2a, (byte) 0x14, (byte) 0xff, (byte) 0xfe, (byte) 0x35, (byte) 0x26, (byte) 0xce, |
| 86 | (byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15, |
| 87 | (byte) 0xe6, (byte) 0xce, (byte) 0x8f, (byte) 0xff, (byte) 0xfe, (byte) 0x54, (byte) 0x37, (byte) 0xc8, |
| 88 | }; |
| 89 | bytePacket = new byte[byteHeader.length + bytePayload.length]; |
| 90 | System.arraycopy(byteHeader, 0, bytePacket, 0, byteHeader.length); |
| 91 | System.arraycopy(bytePayload, 0, bytePacket, byteHeader.length, bytePayload.length); |
| 92 | } |
| 93 | |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 94 | @Before |
| 95 | public void setUp() { |
Pier Luigi | 37b687b | 2017-01-16 22:46:33 -0800 | [diff] [blame] | 96 | deserializer = deserializer(); |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 99 | /** |
| 100 | * Tests serialize and setters. |
| 101 | */ |
| 102 | @Test |
| 103 | public void testSerialize() { |
| 104 | IPv6 ipv6 = new IPv6(); |
| 105 | ipv6.setPayload(udp); |
| 106 | ipv6.setVersion((byte) 6); |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 107 | ipv6.setTrafficClass((byte) 0x93); |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 108 | ipv6.setFlowLabel(0x13579); |
Pier Luigi | 37b687b | 2017-01-16 22:46:33 -0800 | [diff] [blame] | 109 | ipv6.setNextHeader(PROTOCOL_UDP); |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 110 | ipv6.setHopLimit((byte) 32); |
| 111 | ipv6.setSourceAddress(SOURCE_ADDRESS); |
| 112 | ipv6.setDestinationAddress(DESTINATION_ADDRESS); |
| 113 | |
| 114 | assertArrayEquals(ipv6.serialize(), bytePacket); |
| 115 | } |
| 116 | |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 117 | @Test |
| 118 | public void testDeserializeBadInput() throws Exception { |
| 119 | PacketTestUtils.testDeserializeBadInput(deserializer); |
| 120 | } |
| 121 | |
| 122 | @Test |
| 123 | public void testDeserializeTruncated() throws Exception { |
| 124 | // Run the truncation test only on the IPv6 header |
Pier Luigi | 37b687b | 2017-01-16 22:46:33 -0800 | [diff] [blame] | 125 | byte[] ipv6Header = new byte[FIXED_HEADER_LENGTH]; |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 126 | ByteBuffer.wrap(bytePacket).get(ipv6Header); |
| 127 | |
| 128 | PacketTestUtils.testDeserializeTruncated(deserializer, ipv6Header); |
| 129 | } |
| 130 | |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 131 | /** |
| 132 | * Tests deserialize and getters. |
| 133 | */ |
| 134 | @Test |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 135 | public void testDeserialize() throws DeserializationException { |
| 136 | IPv6 ipv6 = deserializer.deserialize(bytePacket, 0, bytePacket.length); |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 137 | |
| 138 | assertThat(ipv6.getVersion(), is((byte) 6)); |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 139 | assertThat(ipv6.getTrafficClass(), is((byte) 0x93)); |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 140 | assertThat(ipv6.getFlowLabel(), is(0x13579)); |
Pier Luigi | 37b687b | 2017-01-16 22:46:33 -0800 | [diff] [blame] | 141 | assertThat(ipv6.getNextHeader(), is(PROTOCOL_UDP)); |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 142 | assertThat(ipv6.getHopLimit(), is((byte) 32)); |
| 143 | assertArrayEquals(ipv6.getSourceAddress(), SOURCE_ADDRESS); |
| 144 | assertArrayEquals(ipv6.getDestinationAddress(), DESTINATION_ADDRESS); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Tests comparator. |
| 149 | */ |
| 150 | @Test |
| 151 | public void testEqual() { |
| 152 | IPv6 packet1 = new IPv6(); |
| 153 | packet1.setPayload(udp); |
| 154 | packet1.setVersion((byte) 6); |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 155 | packet1.setTrafficClass((byte) 0x93); |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 156 | packet1.setFlowLabel(0x13579); |
Pier Luigi | 37b687b | 2017-01-16 22:46:33 -0800 | [diff] [blame] | 157 | packet1.setNextHeader(PROTOCOL_UDP); |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 158 | packet1.setHopLimit((byte) 32); |
| 159 | packet1.setSourceAddress(SOURCE_ADDRESS); |
| 160 | packet1.setDestinationAddress(DESTINATION_ADDRESS); |
| 161 | |
| 162 | IPv6 packet2 = new IPv6(); |
| 163 | packet2.setPayload(udp); |
| 164 | packet2.setVersion((byte) 6); |
Charles M.C. Chan | 7fee36a | 2014-12-31 00:19:59 +0800 | [diff] [blame] | 165 | packet2.setTrafficClass((byte) 0x93); |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 166 | packet2.setFlowLabel(0x13579); |
Pier Luigi | 37b687b | 2017-01-16 22:46:33 -0800 | [diff] [blame] | 167 | packet2.setNextHeader(PROTOCOL_UDP); |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 168 | packet2.setHopLimit((byte) 32); |
| 169 | packet2.setSourceAddress(DESTINATION_ADDRESS); |
| 170 | packet2.setDestinationAddress(SOURCE_ADDRESS); |
| 171 | |
| 172 | assertTrue(packet1.equals(packet1)); |
| 173 | assertFalse(packet1.equals(packet2)); |
| 174 | } |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 175 | |
| 176 | /** |
| 177 | * Tests toString. |
| 178 | */ |
| 179 | @Test |
| 180 | public void testToStringIPv6() throws Exception { |
| 181 | IPv6 ipv6 = deserializer.deserialize(bytePacket, 0, bytePacket.length); |
| 182 | String str = ipv6.toString(); |
| 183 | |
| 184 | assertTrue(StringUtils.contains(str, "version=" + (byte) 6)); |
| 185 | assertTrue(StringUtils.contains(str, "trafficClass=" + (byte) 0x93)); |
| 186 | assertTrue(StringUtils.contains(str, "flowLabel=" + 0x13579)); |
Pier Luigi | 37b687b | 2017-01-16 22:46:33 -0800 | [diff] [blame] | 187 | assertTrue(StringUtils.contains(str, "nextHeader=" + PROTOCOL_UDP)); |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 188 | assertTrue(StringUtils.contains(str, "hopLimit=" + (byte) 32)); |
| 189 | // TODO: test IPv6 source and destination address |
| 190 | } |
Pier Ventre | 78e73f6 | 2016-12-02 19:59:28 -0800 | [diff] [blame] | 191 | |
| 192 | /** |
| 193 | * Tests the proper operation of the solicitationNodeAddress function. |
| 194 | */ |
| 195 | @Test |
| 196 | public void testSolicitationNodeAddress() { |
Pier Luigi | 37b687b | 2017-01-16 22:46:33 -0800 | [diff] [blame] | 197 | assertArrayEquals(SOLICITATION_NODE_ADDRESS, getSolicitNodeAddress(DESTINATION_ADDRESS)); |
Pier Ventre | 78e73f6 | 2016-12-02 19:59:28 -0800 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Tests the proper operation of the multicastAddress function. |
| 202 | */ |
| 203 | @Test |
| 204 | public void testMulticastAddress() { |
Pier Luigi | 37b687b | 2017-01-16 22:46:33 -0800 | [diff] [blame] | 205 | assertArrayEquals(MULTICAST_ADDRESS, getMCastMacAddress(DESTINATION_ADDRESS)); |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Tests the proper operation of the isLinkLocalAddress function. |
| 210 | */ |
| 211 | @Test |
| 212 | public void testIsLinkLocalAddress() { |
| 213 | assertFalse(isLinkLocalAddress(SOURCE_ADDRESS)); |
| 214 | assertFalse(isLinkLocalAddress(DESTINATION_ADDRESS)); |
| 215 | assertFalse(isLinkLocalAddress(SOLICITATION_NODE_ADDRESS)); |
| 216 | assertTrue(isLinkLocalAddress(LINK_LOCAL_ADDRESS_1)); |
| 217 | assertTrue(isLinkLocalAddress(LINK_LOCAL_ADDRESS_2)); |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Tests the proper operation of the linkLocalAddress function. |
| 222 | */ |
| 223 | @Test |
| 224 | public void testLinkLocalAddress() { |
| 225 | assertArrayEquals(getLinkLocalAddress(MAC_ADDRESS_1), LINK_LOCAL_ADDRESS_1); |
| 226 | assertArrayEquals(getLinkLocalAddress(MAC_ADDRESS_2), LINK_LOCAL_ADDRESS_2); |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Tests the proper operation of the macAddress function. |
| 231 | */ |
| 232 | @Test |
| 233 | public void testMacAddress() { |
| 234 | assertArrayEquals(getMacAddress(LINK_LOCAL_ADDRESS_1), MAC_ADDRESS_1); |
| 235 | assertArrayEquals(getMacAddress(LINK_LOCAL_ADDRESS_2), MAC_ADDRESS_2); |
Pier Ventre | 78e73f6 | 2016-12-02 19:59:28 -0800 | [diff] [blame] | 236 | } |
Charles M.C. Chan | 93b7fb0 | 2014-12-28 03:59:36 +0800 | [diff] [blame] | 237 | } |