Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 19 | package org.onlab.packet; |
| 20 | |
| 21 | import org.junit.Test; |
| 22 | |
| 23 | import static org.hamcrest.Matchers.is; |
| 24 | import static org.hamcrest.Matchers.not; |
| 25 | import static org.junit.Assert.assertThat; |
| 26 | import static org.junit.Assert.assertTrue; |
| 27 | import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable; |
| 28 | |
| 29 | /** |
| 30 | * Tests for class {@link Ip6Address}. |
| 31 | */ |
| 32 | public class Ip6AddressTest { |
| 33 | /** |
| 34 | * Tests the immutability of {@link Ip6Address}. |
| 35 | */ |
| 36 | @Test |
| 37 | public void testImmutable() { |
| 38 | assertThatClassIsImmutable(Ip6Address.class); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Tests the length of the address in bytes (octets). |
| 43 | */ |
| 44 | @Test |
| 45 | public void testAddrBytelen() { |
| 46 | assertThat(Ip6Address.BYTE_LENGTH, is(16)); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Tests the length of the address in bits. |
| 51 | */ |
| 52 | @Test |
| 53 | public void testAddrBitlen() { |
| 54 | assertThat(Ip6Address.BIT_LENGTH, is(128)); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Tests default class constructor. |
| 59 | */ |
| 60 | @Test |
| 61 | public void testDefaultConstructor() { |
| 62 | Ip6Address ip6Address = new Ip6Address(); |
| 63 | assertThat(ip6Address.toString(), is("::")); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Tests valid class copy constructor. |
| 68 | */ |
| 69 | @Test |
| 70 | public void testCopyConstructor() { |
| 71 | Ip6Address fromAddr = |
| 72 | new Ip6Address("1111:2222:3333:4444:5555:6666:7777:8888"); |
| 73 | Ip6Address ip6Address = new Ip6Address(fromAddr); |
| 74 | assertThat(ip6Address.toString(), |
| 75 | is("1111:2222:3333:4444:5555:6666:7777:8888")); |
| 76 | |
| 77 | fromAddr = new Ip6Address("::"); |
| 78 | ip6Address = new Ip6Address(fromAddr); |
| 79 | assertThat(ip6Address.toString(), is("::")); |
| 80 | |
| 81 | fromAddr = new Ip6Address("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"); |
| 82 | ip6Address = new Ip6Address(fromAddr); |
| 83 | assertThat(ip6Address.toString(), |
| 84 | is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Tests invalid class copy constructor for a null object to copy from. |
| 89 | */ |
| 90 | @Test(expected = NullPointerException.class) |
| 91 | public void testInvalidConstructorNullObject() { |
| 92 | Ip6Address fromAddr = null; |
| 93 | Ip6Address ip6Address = new Ip6Address(fromAddr); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Tests valid class constructor for integer values. |
| 98 | */ |
| 99 | @Test |
| 100 | public void testConstructorForInteger() { |
| 101 | Ip6Address ip6Address = |
| 102 | new Ip6Address(0x1111222233334444L, 0x5555666677778888L); |
| 103 | assertThat(ip6Address.toString(), |
| 104 | is("1111:2222:3333:4444:5555:6666:7777:8888")); |
| 105 | |
| 106 | ip6Address = new Ip6Address(0L, 0L); |
| 107 | assertThat(ip6Address.toString(), is("::")); |
| 108 | |
| 109 | ip6Address = new Ip6Address(-1L, -1L); |
| 110 | assertThat(ip6Address.toString(), |
| 111 | is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Tests valid class constructor for an array value. |
| 116 | */ |
| 117 | @Test |
| 118 | public void testConstructorForArray() { |
| 119 | final byte[] value1 = new byte[] {0x11, 0x11, 0x22, 0x22, |
| 120 | 0x33, 0x33, 0x44, 0x44, |
| 121 | 0x55, 0x55, 0x66, 0x66, |
| 122 | 0x77, 0x77, |
| 123 | (byte) 0x88, (byte) 0x88}; |
| 124 | Ip6Address ip6Address = new Ip6Address(value1); |
| 125 | assertThat(ip6Address.toString(), |
| 126 | is("1111:2222:3333:4444:5555:6666:7777:8888")); |
| 127 | |
| 128 | final byte[] value2 = new byte[] {0x00, 0x00, 0x00, 0x00, |
| 129 | 0x00, 0x00, 0x00, 0x00, |
| 130 | 0x00, 0x00, 0x00, 0x00, |
| 131 | 0x00, 0x00, 0x00, 0x00}; |
| 132 | ip6Address = new Ip6Address(value2); |
| 133 | assertThat(ip6Address.toString(), is("::")); |
| 134 | |
| 135 | final byte[] value3 = new byte[] {(byte) 0xff, (byte) 0xff, |
| 136 | (byte) 0xff, (byte) 0xff, |
| 137 | (byte) 0xff, (byte) 0xff, |
| 138 | (byte) 0xff, (byte) 0xff, |
| 139 | (byte) 0xff, (byte) 0xff, |
| 140 | (byte) 0xff, (byte) 0xff, |
| 141 | (byte) 0xff, (byte) 0xff, |
| 142 | (byte) 0xff, (byte) 0xff}; |
| 143 | ip6Address = new Ip6Address(value3); |
| 144 | assertThat(ip6Address.toString(), |
| 145 | is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Tests valid class constructor for an array value and an offset. |
| 150 | */ |
| 151 | @Test |
| 152 | public void testConstructorForArrayAndOffset() { |
| 153 | final byte[] value1 = new byte[] {11, 22, 33, // Preamble |
| 154 | 0x11, 0x11, 0x22, 0x22, |
| 155 | 0x33, 0x33, 0x44, 0x44, |
| 156 | 0x55, 0x55, 0x66, 0x66, |
| 157 | 0x77, 0x77, |
| 158 | (byte) 0x88, (byte) 0x88, |
| 159 | 44, 55}; // Extra bytes |
| 160 | Ip6Address ip6Address = new Ip6Address(value1, 3); |
| 161 | assertThat(ip6Address.toString(), |
| 162 | is("1111:2222:3333:4444:5555:6666:7777:8888")); |
| 163 | |
| 164 | final byte[] value2 = new byte[] {11, 22, // Preamble |
| 165 | 0x00, 0x00, 0x00, 0x00, |
| 166 | 0x00, 0x00, 0x00, 0x00, |
| 167 | 0x00, 0x00, 0x00, 0x00, |
| 168 | 0x00, 0x00, 0x00, 0x00, |
| 169 | 33}; // Extra bytes |
| 170 | ip6Address = new Ip6Address(value2, 2); |
| 171 | assertThat(ip6Address.toString(), is("::")); |
| 172 | |
| 173 | final byte[] value3 = new byte[] {11, 22, // Preamble |
| 174 | (byte) 0xff, (byte) 0xff, |
| 175 | (byte) 0xff, (byte) 0xff, |
| 176 | (byte) 0xff, (byte) 0xff, |
| 177 | (byte) 0xff, (byte) 0xff, |
| 178 | (byte) 0xff, (byte) 0xff, |
| 179 | (byte) 0xff, (byte) 0xff, |
| 180 | (byte) 0xff, (byte) 0xff, |
| 181 | (byte) 0xff, (byte) 0xff, |
| 182 | 33}; // Extra bytes |
| 183 | ip6Address = new Ip6Address(value3, 2); |
| 184 | assertThat(ip6Address.toString(), |
| 185 | is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")); |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Tests invalid class constructor for a null array. |
| 190 | */ |
| 191 | @Test(expected = NullPointerException.class) |
| 192 | public void testInvalidConstructorNullArray() { |
| 193 | final byte[] fromArray = null; |
| 194 | Ip6Address ip6Address = new Ip6Address(fromArray); |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Tests invalid class constructor for an array that is too short. |
| 199 | */ |
| 200 | @Test(expected = IllegalArgumentException.class) |
| 201 | public void testInvalidConstructorShortArray() { |
| 202 | final byte[] fromArray = new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9}; |
| 203 | Ip6Address ip6Address = new Ip6Address(fromArray); |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Tests invalid class constructor for an array and an invalid offset. |
| 208 | */ |
| 209 | @Test(expected = IllegalArgumentException.class) |
| 210 | public void testInvalidConstructorArrayInvalidOffset() { |
| 211 | final byte[] value1 = new byte[] {11, 22, 33, // Preamble |
| 212 | 0x11, 0x11, 0x22, 0x22, |
| 213 | 0x33, 0x33, 0x44, 0x44, |
| 214 | 0x55, 0x55, 0x66, 0x66, |
| 215 | 0x77, 0x77, |
| 216 | (byte) 0x88, (byte) 0x88, |
| 217 | 44, 55}; // Extra bytes |
| 218 | Ip6Address ip6Address = new Ip6Address(value1, 6); |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Tests valid class constructor for a string. |
| 223 | */ |
| 224 | @Test |
| 225 | public void testConstructorForString() { |
| 226 | Ip6Address ip6Address = |
| 227 | new Ip6Address("1111:2222:3333:4444:5555:6666:7777:8888"); |
| 228 | assertThat(ip6Address.toString(), |
| 229 | is("1111:2222:3333:4444:5555:6666:7777:8888")); |
| 230 | |
| 231 | ip6Address = new Ip6Address("::"); |
| 232 | assertThat(ip6Address.toString(), is("::")); |
| 233 | |
| 234 | ip6Address = new Ip6Address("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"); |
| 235 | assertThat(ip6Address.toString(), |
| 236 | is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")); |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Tests invalid class constructor for a null string. |
| 241 | */ |
| 242 | @Test(expected = NullPointerException.class) |
| 243 | public void testInvalidConstructorNullString() { |
| 244 | String fromString = null; |
| 245 | Ip6Address ip6Address = new Ip6Address(fromString); |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * Tests invalid class constructor for an empty string. |
| 250 | */ |
| 251 | @Test(expected = IllegalArgumentException.class) |
| 252 | public void testInvalidConstructors() { |
| 253 | // Check constructor for invalid ID: empty string |
| 254 | Ip6Address ip6Address = new Ip6Address(""); |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Tests returning the address as a byte array. |
| 259 | */ |
| 260 | @Test |
| 261 | public void testAddressToOctets() { |
| 262 | final byte[] value1 = new byte[] {0x11, 0x11, 0x22, 0x22, |
| 263 | 0x33, 0x33, 0x44, 0x44, |
| 264 | 0x55, 0x55, 0x66, 0x66, |
| 265 | 0x77, 0x77, |
| 266 | (byte) 0x88, (byte) 0x88}; |
| 267 | Ip6Address ip6Address = |
| 268 | new Ip6Address("1111:2222:3333:4444:5555:6666:7777:8888"); |
| 269 | assertThat(ip6Address.toOctets(), is(value1)); |
| 270 | |
| 271 | final byte[] value2 = new byte[] {0x00, 0x00, 0x00, 0x00, |
| 272 | 0x00, 0x00, 0x00, 0x00, |
| 273 | 0x00, 0x00, 0x00, 0x00, |
| 274 | 0x00, 0x00, 0x00, 0x00}; |
| 275 | ip6Address = new Ip6Address("::"); |
| 276 | assertThat(ip6Address.toOctets(), is(value2)); |
| 277 | |
| 278 | final byte[] value3 = new byte[] {(byte) 0xff, (byte) 0xff, |
| 279 | (byte) 0xff, (byte) 0xff, |
| 280 | (byte) 0xff, (byte) 0xff, |
| 281 | (byte) 0xff, (byte) 0xff, |
| 282 | (byte) 0xff, (byte) 0xff, |
| 283 | (byte) 0xff, (byte) 0xff, |
| 284 | (byte) 0xff, (byte) 0xff, |
| 285 | (byte) 0xff, (byte) 0xff}; |
| 286 | ip6Address = new Ip6Address("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"); |
| 287 | assertThat(ip6Address.toOctets(), is(value3)); |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * Tests making a mask prefix for a given prefix length. |
| 292 | */ |
| 293 | @Test |
| 294 | public void testMakeMaskPrefix() { |
| 295 | Ip6Address ip6Address = Ip6Address.makeMaskPrefix(8); |
| 296 | assertThat(ip6Address.toString(), is("ff00::")); |
| 297 | |
| 298 | ip6Address = Ip6Address.makeMaskPrefix(120); |
| 299 | assertThat(ip6Address.toString(), |
| 300 | is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ff00")); |
| 301 | |
| 302 | ip6Address = Ip6Address.makeMaskPrefix(0); |
| 303 | assertThat(ip6Address.toString(), is("::")); |
| 304 | |
| 305 | ip6Address = Ip6Address.makeMaskPrefix(128); |
| 306 | assertThat(ip6Address.toString(), |
| 307 | is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")); |
| 308 | |
| 309 | ip6Address = Ip6Address.makeMaskPrefix(64); |
| 310 | assertThat(ip6Address.toString(), is("ffff:ffff:ffff:ffff::")); |
| 311 | } |
| 312 | |
| 313 | /** |
| 314 | * Tests making of a masked address. |
| 315 | */ |
| 316 | @Test |
| 317 | public void testMakeMaskedAddress() { |
| 318 | Ip6Address ip6Address = |
| 319 | new Ip6Address("1111:2222:3333:4444:5555:6666:7777:8885"); |
| 320 | Ip6Address ip6AddressMasked = |
| 321 | Ip6Address.makeMaskedAddress(ip6Address, 8); |
| 322 | assertThat(ip6AddressMasked.toString(), is("1100::")); |
| 323 | |
| 324 | ip6AddressMasked = Ip6Address.makeMaskedAddress(ip6Address, 120); |
| 325 | assertThat(ip6AddressMasked.toString(), |
| 326 | is("1111:2222:3333:4444:5555:6666:7777:8800")); |
| 327 | |
| 328 | ip6AddressMasked = Ip6Address.makeMaskedAddress(ip6Address, 0); |
| 329 | assertThat(ip6AddressMasked.toString(), is("::")); |
| 330 | |
| 331 | ip6AddressMasked = Ip6Address.makeMaskedAddress(ip6Address, 128); |
| 332 | assertThat(ip6AddressMasked.toString(), |
| 333 | is("1111:2222:3333:4444:5555:6666:7777:8885")); |
| 334 | |
| 335 | ip6AddressMasked = Ip6Address.makeMaskedAddress(ip6Address, 64); |
| 336 | assertThat(ip6AddressMasked.toString(), is("1111:2222:3333:4444::")); |
| 337 | } |
| 338 | |
| 339 | /** |
| 340 | * Tests getting the value of an address. |
| 341 | */ |
| 342 | @Test |
| 343 | public void testGetValue() { |
| 344 | Ip6Address ip6Address = |
| 345 | new Ip6Address("1111:2222:3333:4444:5555:6666:7777:8888"); |
| 346 | assertThat(ip6Address.getValueHigh(), is(0x1111222233334444L)); |
| 347 | assertThat(ip6Address.getValueLow(), is(0x5555666677778888L)); |
| 348 | |
| 349 | ip6Address = new Ip6Address(0, 0); |
| 350 | assertThat(ip6Address.getValueHigh(), is(0L)); |
| 351 | assertThat(ip6Address.getValueLow(), is(0L)); |
| 352 | |
| 353 | ip6Address = new Ip6Address(-1L, -1L); |
| 354 | assertThat(ip6Address.getValueHigh(), is(-1L)); |
| 355 | assertThat(ip6Address.getValueLow(), is(-1L)); |
| 356 | } |
| 357 | |
| 358 | /** |
| 359 | * Tests equality of {@link Ip6Address}. |
| 360 | */ |
| 361 | @Test |
| 362 | public void testEquality() { |
| 363 | Ip6Address addr1 = |
| 364 | new Ip6Address("1111:2222:3333:4444:5555:6666:7777:8888"); |
| 365 | Ip6Address addr2 = |
| 366 | new Ip6Address("1111:2222:3333:4444:5555:6666:7777:8888"); |
| 367 | assertThat(addr1, is(addr2)); |
| 368 | |
| 369 | addr1 = new Ip6Address("::"); |
| 370 | addr2 = new Ip6Address("::"); |
| 371 | assertThat(addr1, is(addr2)); |
| 372 | |
| 373 | addr1 = new Ip6Address("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"); |
| 374 | addr2 = new Ip6Address("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"); |
| 375 | assertThat(addr1, is(addr2)); |
| 376 | } |
| 377 | |
| 378 | /** |
| 379 | * Tests non-equality of {@link Ip6Address}. |
| 380 | */ |
| 381 | @Test |
| 382 | public void testNonEquality() { |
| 383 | Ip6Address addr1 = |
| 384 | new Ip6Address("1111:2222:3333:4444:5555:6666:7777:8888"); |
| 385 | Ip6Address addr2 = |
| 386 | new Ip6Address("1111:2222:3333:4444:5555:6666:7777:888A"); |
| 387 | Ip6Address addr3 = new Ip6Address("::"); |
| 388 | Ip6Address addr4 = |
| 389 | new Ip6Address("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"); |
| 390 | assertThat(addr1, is(not(addr2))); |
| 391 | assertThat(addr3, is(not(addr2))); |
| 392 | assertThat(addr4, is(not(addr2))); |
| 393 | } |
| 394 | |
| 395 | /** |
| 396 | * Tests comparison of {@link Ip6Address}. |
| 397 | */ |
| 398 | @Test |
| 399 | public void testComparison() { |
| 400 | Ip6Address addr1 = |
| 401 | new Ip6Address("1111:2222:3333:4444:5555:6666:7777:8888"); |
| 402 | Ip6Address addr2 = |
| 403 | new Ip6Address("1111:2222:3333:4444:5555:6666:7777:8888"); |
| 404 | Ip6Address addr3 = |
| 405 | new Ip6Address("1111:2222:3333:4444:5555:6666:7777:8887"); |
| 406 | Ip6Address addr4 = |
| 407 | new Ip6Address("1111:2222:3333:4444:5555:6666:7777:8889"); |
| 408 | assertTrue(addr1.compareTo(addr2) == 0); |
| 409 | assertTrue(addr1.compareTo(addr3) > 0); |
| 410 | assertTrue(addr1.compareTo(addr4) < 0); |
| 411 | |
| 412 | addr1 = new Ip6Address("ffff:2222:3333:4444:5555:6666:7777:8888"); |
| 413 | addr2 = new Ip6Address("ffff:2222:3333:4444:5555:6666:7777:8888"); |
| 414 | addr3 = new Ip6Address("ffff:2222:3333:4444:5555:6666:7777:8887"); |
| 415 | addr4 = new Ip6Address("ffff:2222:3333:4444:5555:6666:7777:8889"); |
| 416 | assertTrue(addr1.compareTo(addr2) == 0); |
| 417 | assertTrue(addr1.compareTo(addr3) > 0); |
| 418 | assertTrue(addr1.compareTo(addr4) < 0); |
| 419 | |
| 420 | addr1 = new Ip6Address("ffff:2222:3333:4444:5555:6666:7777:8888"); |
| 421 | addr2 = new Ip6Address("ffff:2222:3333:4444:5555:6666:7777:8888"); |
| 422 | addr3 = new Ip6Address("ffff:2222:3333:4443:5555:6666:7777:8888"); |
| 423 | addr4 = new Ip6Address("ffff:2222:3333:4445:5555:6666:7777:8888"); |
| 424 | assertTrue(addr1.compareTo(addr2) == 0); |
| 425 | assertTrue(addr1.compareTo(addr3) > 0); |
| 426 | assertTrue(addr1.compareTo(addr4) < 0); |
| 427 | } |
| 428 | |
| 429 | /** |
| 430 | * Tests object string representation. |
| 431 | */ |
| 432 | @Test |
| 433 | public void testToString() { |
| 434 | Ip6Address ip6Address = |
| 435 | new Ip6Address("1111:2222:3333:4444:5555:6666:7777:8888"); |
| 436 | assertThat(ip6Address.toString(), |
| 437 | is("1111:2222:3333:4444:5555:6666:7777:8888")); |
| 438 | |
| 439 | ip6Address = new Ip6Address("1111::8888"); |
| 440 | assertThat(ip6Address.toString(), is("1111::8888")); |
| 441 | |
| 442 | ip6Address = new Ip6Address("1111::"); |
| 443 | assertThat(ip6Address.toString(), is("1111::")); |
| 444 | |
| 445 | ip6Address = new Ip6Address("::8888"); |
| 446 | assertThat(ip6Address.toString(), is("::8888")); |
| 447 | |
| 448 | ip6Address = new Ip6Address("::"); |
| 449 | assertThat(ip6Address.toString(), is("::")); |
| 450 | |
| 451 | ip6Address = new Ip6Address("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"); |
| 452 | assertThat(ip6Address.toString(), |
| 453 | is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")); |
| 454 | } |
| 455 | } |