Pavlin Radoslavov | d0e32d7 | 2014-10-31 18:11:43 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2014 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 | package org.onlab.packet; |
| 17 | |
| 18 | import org.junit.Test; |
| 19 | |
| 20 | import static org.hamcrest.Matchers.is; |
| 21 | import static org.hamcrest.Matchers.not; |
| 22 | import static org.junit.Assert.assertThat; |
| 23 | import static org.junit.Assert.assertTrue; |
| 24 | import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable; |
| 25 | |
| 26 | /** |
| 27 | * Tests for class {@link IpAddress}. |
| 28 | */ |
| 29 | public class IpAddressTest { |
| 30 | /** |
| 31 | * Tests the immutability of {@link IpAddress}. |
| 32 | */ |
| 33 | @Test |
| 34 | public void testImmutable() { |
| 35 | assertThatClassIsImmutable(IpAddress.class); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Tests the length of the address in bytes (octets). |
| 40 | */ |
| 41 | @Test |
| 42 | public void testAddrByteLength() { |
| 43 | assertThat(IpAddress.INET_BYTE_LENGTH, is(4)); |
| 44 | assertThat(IpAddress.INET6_BYTE_LENGTH, is(16)); |
| 45 | assertThat(IpAddress.byteLength(IpAddress.Version.INET), is(4)); |
| 46 | assertThat(IpAddress.byteLength(IpAddress.Version.INET6), is(16)); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Tests the length of the address in bits. |
| 51 | */ |
| 52 | @Test |
| 53 | public void testAddrBitLength() { |
| 54 | assertThat(IpAddress.INET_BIT_LENGTH, is(32)); |
| 55 | assertThat(IpAddress.INET6_BIT_LENGTH, is(128)); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Tests returning the IP address version. |
| 60 | */ |
| 61 | @Test |
| 62 | public void testVersion() { |
| 63 | IpAddress ipAddress; |
| 64 | |
| 65 | // IPv4 |
| 66 | ipAddress = IpAddress.valueOf("0.0.0.0"); |
| 67 | assertThat(ipAddress.version(), is(IpAddress.Version.INET)); |
| 68 | |
| 69 | // IPv6 |
| 70 | ipAddress = IpAddress.valueOf("::"); |
| 71 | assertThat(ipAddress.version(), is(IpAddress.Version.INET6)); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Tests returning an IPv4 address as a byte array. |
| 76 | */ |
| 77 | @Test |
| 78 | public void testAddressToOctetsIPv4() { |
| 79 | IpAddress ipAddress; |
| 80 | |
| 81 | final byte[] value1 = new byte[] {1, 2, 3, 4}; |
| 82 | ipAddress = IpAddress.valueOf("1.2.3.4"); |
| 83 | assertThat(ipAddress.toOctets(), is(value1)); |
| 84 | |
| 85 | final byte[] value2 = new byte[] {0, 0, 0, 0}; |
| 86 | ipAddress = IpAddress.valueOf("0.0.0.0"); |
| 87 | assertThat(ipAddress.toOctets(), is(value2)); |
| 88 | |
| 89 | final byte[] value3 = new byte[] {(byte) 0xff, (byte) 0xff, |
| 90 | (byte) 0xff, (byte) 0xff}; |
| 91 | ipAddress = IpAddress.valueOf("255.255.255.255"); |
| 92 | assertThat(ipAddress.toOctets(), is(value3)); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Tests returning an IPv6 address as a byte array. |
| 97 | */ |
| 98 | @Test |
| 99 | public void testAddressToOctetsIPv6() { |
| 100 | IpAddress ipAddress; |
| 101 | |
| 102 | final byte[] value1 = new byte[] {0x11, 0x11, 0x22, 0x22, |
| 103 | 0x33, 0x33, 0x44, 0x44, |
| 104 | 0x55, 0x55, 0x66, 0x66, |
| 105 | 0x77, 0x77, |
| 106 | (byte) 0x88, (byte) 0x88}; |
| 107 | ipAddress = |
| 108 | IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888"); |
| 109 | assertThat(ipAddress.toOctets(), is(value1)); |
| 110 | |
| 111 | final byte[] value2 = new byte[] {0x00, 0x00, 0x00, 0x00, |
| 112 | 0x00, 0x00, 0x00, 0x00, |
| 113 | 0x00, 0x00, 0x00, 0x00, |
| 114 | 0x00, 0x00, 0x00, 0x00}; |
| 115 | ipAddress = IpAddress.valueOf("::"); |
| 116 | assertThat(ipAddress.toOctets(), is(value2)); |
| 117 | |
| 118 | final byte[] value3 = new byte[] {(byte) 0xff, (byte) 0xff, |
| 119 | (byte) 0xff, (byte) 0xff, |
| 120 | (byte) 0xff, (byte) 0xff, |
| 121 | (byte) 0xff, (byte) 0xff, |
| 122 | (byte) 0xff, (byte) 0xff, |
| 123 | (byte) 0xff, (byte) 0xff, |
| 124 | (byte) 0xff, (byte) 0xff, |
| 125 | (byte) 0xff, (byte) 0xff}; |
| 126 | ipAddress = |
| 127 | IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"); |
| 128 | assertThat(ipAddress.toOctets(), is(value3)); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Tests returning an IPv4 address asn an integer. |
| 133 | */ |
| 134 | @Test |
| 135 | public void testToint() { |
| 136 | IpAddress ipAddress; |
| 137 | |
| 138 | ipAddress = IpAddress.valueOf("1.2.3.4"); |
| 139 | assertThat(ipAddress.toInt(), is(0x01020304)); |
| 140 | |
| 141 | ipAddress = IpAddress.valueOf("0.0.0.0"); |
| 142 | assertThat(ipAddress.toInt(), is(0)); |
| 143 | |
| 144 | ipAddress = IpAddress.valueOf("255.255.255.255"); |
| 145 | assertThat(ipAddress.toInt(), is(-1)); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Tests valueOf() converter for an integer value. |
| 150 | */ |
| 151 | @Test |
| 152 | public void testValueOfForInteger() { |
| 153 | IpAddress ipAddress; |
| 154 | |
| 155 | ipAddress = IpAddress.valueOf(0x01020304); |
| 156 | assertThat(ipAddress.toString(), is("1.2.3.4")); |
| 157 | |
| 158 | ipAddress = IpAddress.valueOf(0); |
| 159 | assertThat(ipAddress.toString(), is("0.0.0.0")); |
| 160 | |
| 161 | ipAddress = IpAddress.valueOf(0xffffffff); |
| 162 | assertThat(ipAddress.toString(), is("255.255.255.255")); |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Tests valueOf() converter for IPv4 byte array. |
| 167 | */ |
| 168 | @Test |
| 169 | public void testValueOfByteArrayIPv4() { |
| 170 | IpAddress ipAddress; |
| 171 | |
| 172 | final byte[] value1 = new byte[] {1, 2, 3, 4}; |
| 173 | ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value1); |
| 174 | assertThat(ipAddress.toString(), is("1.2.3.4")); |
| 175 | |
| 176 | final byte[] value2 = new byte[] {0, 0, 0, 0}; |
| 177 | ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value2); |
| 178 | assertThat(ipAddress.toString(), is("0.0.0.0")); |
| 179 | |
| 180 | final byte[] value3 = new byte[] {(byte) 0xff, (byte) 0xff, |
| 181 | (byte) 0xff, (byte) 0xff}; |
| 182 | ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value3); |
| 183 | assertThat(ipAddress.toString(), is("255.255.255.255")); |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Tests valueOf() converter for IPv6 byte array. |
| 188 | */ |
| 189 | @Test |
| 190 | public void testValueOfByteArrayIPv6() { |
| 191 | IpAddress ipAddress; |
| 192 | |
| 193 | final byte[] value1 = new byte[] {0x11, 0x11, 0x22, 0x22, |
| 194 | 0x33, 0x33, 0x44, 0x44, |
| 195 | 0x55, 0x55, 0x66, 0x66, |
| 196 | 0x77, 0x77, |
| 197 | (byte) 0x88, (byte) 0x88}; |
| 198 | ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value1); |
| 199 | assertThat(ipAddress.toString(), |
| 200 | is("1111:2222:3333:4444:5555:6666:7777:8888")); |
| 201 | |
| 202 | final byte[] value2 = new byte[] {0x00, 0x00, 0x00, 0x00, |
| 203 | 0x00, 0x00, 0x00, 0x00, |
| 204 | 0x00, 0x00, 0x00, 0x00, |
| 205 | 0x00, 0x00, 0x00, 0x00}; |
| 206 | ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value2); |
| 207 | assertThat(ipAddress.toString(), is("::")); |
| 208 | |
| 209 | final byte[] value3 = new byte[] {(byte) 0xff, (byte) 0xff, |
| 210 | (byte) 0xff, (byte) 0xff, |
| 211 | (byte) 0xff, (byte) 0xff, |
| 212 | (byte) 0xff, (byte) 0xff, |
| 213 | (byte) 0xff, (byte) 0xff, |
| 214 | (byte) 0xff, (byte) 0xff, |
| 215 | (byte) 0xff, (byte) 0xff, |
| 216 | (byte) 0xff, (byte) 0xff}; |
| 217 | ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value3); |
| 218 | assertThat(ipAddress.toString(), |
| 219 | is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")); |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Tests invalid valueOf() converter for a null array for IPv4. |
| 224 | */ |
| 225 | @Test(expected = NullPointerException.class) |
| 226 | public void testInvalidValueOfNullArrayIPv4() { |
| 227 | IpAddress ipAddress; |
| 228 | |
| 229 | final byte[] fromArray = null; |
| 230 | ipAddress = IpAddress.valueOf(IpAddress.Version.INET, fromArray); |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Tests invalid valueOf() converter for a null array for IPv6. |
| 235 | */ |
| 236 | @Test(expected = NullPointerException.class) |
| 237 | public void testInvalidValueOfNullArrayIPv6() { |
| 238 | IpAddress ipAddress; |
| 239 | |
| 240 | final byte[] fromArray = null; |
| 241 | ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, fromArray); |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Tests invalid valueOf() converger for an array that is too short for |
| 246 | * IPv4. |
| 247 | */ |
| 248 | @Test(expected = IllegalArgumentException.class) |
| 249 | public void testInvalidValueOfShortArrayIPv4() { |
| 250 | IpAddress ipAddress; |
| 251 | |
| 252 | final byte[] fromArray = new byte[] {1, 2, 3}; |
| 253 | ipAddress = IpAddress.valueOf(IpAddress.Version.INET, fromArray); |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Tests invalid valueOf() converger for an array that is too short for |
| 258 | * IPv6. |
| 259 | */ |
| 260 | @Test(expected = IllegalArgumentException.class) |
| 261 | public void testInvalidValueOfShortArrayIPv6() { |
| 262 | IpAddress ipAddress; |
| 263 | |
| 264 | final byte[] fromArray = new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9}; |
| 265 | ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, fromArray); |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Tests valueOf() converter for IPv4 byte array and an offset. |
| 270 | */ |
| 271 | @Test |
| 272 | public void testValueOfByteArrayOffsetIPv4() { |
| 273 | IpAddress ipAddress; |
| 274 | |
| 275 | final byte[] value1 = new byte[] {11, 22, 33, // Preamble |
| 276 | 1, 2, 3, 4, |
| 277 | 44, 55}; // Extra bytes |
| 278 | ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value1, 3); |
| 279 | assertThat(ipAddress.toString(), is("1.2.3.4")); |
| 280 | |
| 281 | final byte[] value2 = new byte[] {11, 22, // Preamble |
| 282 | 0, 0, 0, 0, |
| 283 | 33}; // Extra bytes |
| 284 | ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value2, 2); |
| 285 | assertThat(ipAddress.toString(), is("0.0.0.0")); |
| 286 | |
| 287 | final byte[] value3 = new byte[] {11, 22, // Preamble |
| 288 | (byte) 0xff, (byte) 0xff, |
| 289 | (byte) 0xff, (byte) 0xff, |
| 290 | 33}; // Extra bytes |
| 291 | ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value3, 2); |
| 292 | assertThat(ipAddress.toString(), is("255.255.255.255")); |
| 293 | } |
| 294 | |
| 295 | /** |
| 296 | * Tests valueOf() converter for IPv6 byte array and an offset. |
| 297 | */ |
| 298 | @Test |
| 299 | public void testValueOfByteArrayOffsetIPv6() { |
| 300 | IpAddress ipAddress; |
| 301 | |
| 302 | final byte[] value1 = new byte[] {11, 22, 33, // Preamble |
| 303 | 0x11, 0x11, 0x22, 0x22, |
| 304 | 0x33, 0x33, 0x44, 0x44, |
| 305 | 0x55, 0x55, 0x66, 0x66, |
| 306 | 0x77, 0x77, |
| 307 | (byte) 0x88, (byte) 0x88, |
| 308 | 44, 55}; // Extra bytes |
| 309 | ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value1, 3); |
| 310 | assertThat(ipAddress.toString(), |
| 311 | is("1111:2222:3333:4444:5555:6666:7777:8888")); |
| 312 | |
| 313 | final byte[] value2 = new byte[] {11, 22, // Preamble |
| 314 | 0x00, 0x00, 0x00, 0x00, |
| 315 | 0x00, 0x00, 0x00, 0x00, |
| 316 | 0x00, 0x00, 0x00, 0x00, |
| 317 | 0x00, 0x00, 0x00, 0x00, |
| 318 | 33}; // Extra bytes |
| 319 | ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value2, 2); |
| 320 | assertThat(ipAddress.toString(), is("::")); |
| 321 | |
| 322 | final byte[] value3 = new byte[] {11, 22, // Preamble |
| 323 | (byte) 0xff, (byte) 0xff, |
| 324 | (byte) 0xff, (byte) 0xff, |
| 325 | (byte) 0xff, (byte) 0xff, |
| 326 | (byte) 0xff, (byte) 0xff, |
| 327 | (byte) 0xff, (byte) 0xff, |
| 328 | (byte) 0xff, (byte) 0xff, |
| 329 | (byte) 0xff, (byte) 0xff, |
| 330 | (byte) 0xff, (byte) 0xff, |
| 331 | 33}; // Extra bytes |
| 332 | ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value3, 2); |
| 333 | assertThat(ipAddress.toString(), |
| 334 | is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")); |
| 335 | } |
| 336 | |
| 337 | /** |
| 338 | * Tests invalid valueOf() converger for an array and an invalid offset |
| 339 | * for IPv4. |
| 340 | */ |
| 341 | @Test(expected = IllegalArgumentException.class) |
| 342 | public void testInvalidValueOfArrayInvalidOffsetIPv4() { |
| 343 | IpAddress ipAddress; |
| 344 | |
| 345 | final byte[] value1 = new byte[] {11, 22, 33, // Preamble |
| 346 | 1, 2, 3, 4, |
| 347 | 44, 55}; // Extra bytes |
| 348 | ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value1, 6); |
| 349 | } |
| 350 | |
| 351 | /** |
| 352 | * Tests invalid valueOf() converger for an array and an invalid offset |
| 353 | * for IPv6. |
| 354 | */ |
| 355 | @Test(expected = IllegalArgumentException.class) |
| 356 | public void testInvalidValueOfArrayInvalidOffsetIPv6() { |
| 357 | IpAddress ipAddress; |
| 358 | |
| 359 | final byte[] value1 = new byte[] {11, 22, 33, // Preamble |
| 360 | 0x11, 0x11, 0x22, 0x22, |
| 361 | 0x33, 0x33, 0x44, 0x44, |
| 362 | 0x55, 0x55, 0x66, 0x66, |
| 363 | 0x77, 0x77, |
| 364 | (byte) 0x88, (byte) 0x88, |
| 365 | 44, 55}; // Extra bytes |
| 366 | ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value1, 6); |
| 367 | } |
| 368 | |
| 369 | /** |
| 370 | * Tests valueOf() converter for IPv4 string. |
| 371 | */ |
| 372 | @Test |
| 373 | public void testValueOfStringIPv4() { |
| 374 | IpAddress ipAddress; |
| 375 | |
| 376 | ipAddress = IpAddress.valueOf("1.2.3.4"); |
| 377 | assertThat(ipAddress.toString(), is("1.2.3.4")); |
| 378 | |
| 379 | ipAddress = IpAddress.valueOf("0.0.0.0"); |
| 380 | assertThat(ipAddress.toString(), is("0.0.0.0")); |
| 381 | |
| 382 | ipAddress = IpAddress.valueOf("255.255.255.255"); |
| 383 | assertThat(ipAddress.toString(), is("255.255.255.255")); |
| 384 | } |
| 385 | |
| 386 | /** |
| 387 | * Tests valueOf() converter for IPv6 string. |
| 388 | */ |
| 389 | @Test |
| 390 | public void testValueOfStringIPv6() { |
| 391 | IpAddress ipAddress; |
| 392 | |
| 393 | ipAddress = |
| 394 | IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888"); |
| 395 | assertThat(ipAddress.toString(), |
| 396 | is("1111:2222:3333:4444:5555:6666:7777:8888")); |
| 397 | |
| 398 | ipAddress = IpAddress.valueOf("::"); |
| 399 | assertThat(ipAddress.toString(), is("::")); |
| 400 | |
| 401 | ipAddress = |
| 402 | IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"); |
| 403 | assertThat(ipAddress.toString(), |
| 404 | is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")); |
| 405 | } |
| 406 | |
| 407 | /** |
| 408 | * Tests invalid valueOf() converter for a null string. |
| 409 | */ |
| 410 | @Test(expected = NullPointerException.class) |
| 411 | public void testInvalidValueOfNullString() { |
| 412 | IpAddress ipAddress; |
| 413 | |
| 414 | String fromString = null; |
| 415 | ipAddress = IpAddress.valueOf(fromString); |
| 416 | } |
| 417 | |
| 418 | /** |
| 419 | * Tests invalid valueOf() converter for an empty string. |
| 420 | */ |
| 421 | @Test(expected = IllegalArgumentException.class) |
| 422 | public void testInvalidValueOfEmptyString() { |
| 423 | IpAddress ipAddress; |
| 424 | |
| 425 | String fromString = ""; |
| 426 | ipAddress = IpAddress.valueOf(fromString); |
| 427 | } |
| 428 | |
| 429 | /** |
| 430 | * Tests invalid valueOf() converter for an incorrect string. |
| 431 | */ |
| 432 | @Test(expected = IllegalArgumentException.class) |
| 433 | public void testInvalidValueOfIncorrectString() { |
| 434 | IpAddress ipAddress; |
| 435 | |
| 436 | String fromString = "NoSuchIpAddress"; |
| 437 | ipAddress = IpAddress.valueOf(fromString); |
| 438 | } |
| 439 | |
| 440 | /** |
| 441 | * Tests making a mask prefix for a given prefix length for IPv4. |
| 442 | */ |
| 443 | @Test |
| 444 | public void testMakeMaskPrefixIPv4() { |
| 445 | IpAddress ipAddress; |
| 446 | |
| 447 | ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET, 25); |
| 448 | assertThat(ipAddress.toString(), is("255.255.255.128")); |
| 449 | |
| 450 | ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET, 0); |
| 451 | assertThat(ipAddress.toString(), is("0.0.0.0")); |
| 452 | |
| 453 | ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET, 32); |
| 454 | assertThat(ipAddress.toString(), is("255.255.255.255")); |
| 455 | } |
| 456 | |
| 457 | /** |
| 458 | * Tests making a mask prefix for a given prefix length for IPv6. |
| 459 | */ |
| 460 | @Test |
| 461 | public void testMakeMaskPrefixIPv6() { |
| 462 | IpAddress ipAddress; |
| 463 | |
| 464 | ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET6, 8); |
| 465 | assertThat(ipAddress.toString(), is("ff00::")); |
| 466 | |
| 467 | ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET6, 120); |
| 468 | assertThat(ipAddress.toString(), |
| 469 | is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ff00")); |
| 470 | |
| 471 | ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET6, 0); |
| 472 | assertThat(ipAddress.toString(), is("::")); |
| 473 | |
| 474 | ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET6, 128); |
| 475 | assertThat(ipAddress.toString(), |
| 476 | is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")); |
| 477 | |
| 478 | ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET6, 64); |
| 479 | assertThat(ipAddress.toString(), is("ffff:ffff:ffff:ffff::")); |
| 480 | } |
| 481 | |
| 482 | /** |
| 483 | * Tests making a mask prefix for an invalid prefix length for IPv4: |
| 484 | * negative prefix length. |
| 485 | */ |
| 486 | @Test(expected = IllegalArgumentException.class) |
| 487 | public void testInvalidMakeNegativeMaskPrefixIPv4() { |
| 488 | IpAddress ipAddress; |
| 489 | |
| 490 | ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET, -1); |
| 491 | } |
| 492 | |
| 493 | /** |
| 494 | * Tests making a mask prefix for an invalid prefix length for IPv6: |
| 495 | * negative prefix length. |
| 496 | */ |
| 497 | @Test(expected = IllegalArgumentException.class) |
| 498 | public void testInvalidMakeNegativeMaskPrefixIPv6() { |
| 499 | IpAddress ipAddress; |
| 500 | |
| 501 | ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET6, -1); |
| 502 | } |
| 503 | |
| 504 | /** |
| 505 | * Tests making a mask prefix for an invalid prefix length for IPv4: |
| 506 | * too long prefix length. |
| 507 | */ |
| 508 | @Test(expected = IllegalArgumentException.class) |
| 509 | public void testInvalidMakeTooLongMaskPrefixIPv4() { |
| 510 | IpAddress ipAddress; |
| 511 | |
| 512 | ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET, 33); |
| 513 | } |
| 514 | |
| 515 | /** |
| 516 | * Tests making a mask prefix for an invalid prefix length for IPv6: |
| 517 | * too long prefix length. |
| 518 | */ |
| 519 | @Test(expected = IllegalArgumentException.class) |
| 520 | public void testInvalidMakeTooLongMaskPrefixIPv6() { |
| 521 | IpAddress ipAddress; |
| 522 | |
| 523 | ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET6, 129); |
| 524 | } |
| 525 | |
| 526 | /** |
| 527 | * Tests making of a masked address for IPv4. |
| 528 | */ |
| 529 | @Test |
| 530 | public void testMakeMaskedAddressIPv4() { |
| 531 | IpAddress ipAddress = IpAddress.valueOf("1.2.3.5"); |
| 532 | IpAddress ipAddressMasked; |
| 533 | |
| 534 | ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 24); |
| 535 | assertThat(ipAddressMasked.toString(), is("1.2.3.0")); |
| 536 | |
| 537 | ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 0); |
| 538 | assertThat(ipAddressMasked.toString(), is("0.0.0.0")); |
| 539 | |
| 540 | ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 32); |
| 541 | assertThat(ipAddressMasked.toString(), is("1.2.3.5")); |
| 542 | } |
| 543 | |
| 544 | /** |
| 545 | * Tests making of a masked address for IPv6. |
| 546 | */ |
| 547 | @Test |
| 548 | public void testMakeMaskedAddressIPv6() { |
| 549 | IpAddress ipAddress = |
| 550 | IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8885"); |
| 551 | IpAddress ipAddressMasked; |
| 552 | |
| 553 | ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 8); |
| 554 | assertThat(ipAddressMasked.toString(), is("1100::")); |
| 555 | |
| 556 | ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 120); |
| 557 | assertThat(ipAddressMasked.toString(), |
| 558 | is("1111:2222:3333:4444:5555:6666:7777:8800")); |
| 559 | |
| 560 | ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 0); |
| 561 | assertThat(ipAddressMasked.toString(), is("::")); |
| 562 | |
| 563 | ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 128); |
| 564 | assertThat(ipAddressMasked.toString(), |
| 565 | is("1111:2222:3333:4444:5555:6666:7777:8885")); |
| 566 | |
| 567 | ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 64); |
| 568 | assertThat(ipAddressMasked.toString(), is("1111:2222:3333:4444::")); |
| 569 | } |
| 570 | |
| 571 | /** |
| 572 | * Tests making of a masked address for invalid prefix length for IPv4: |
| 573 | * negative prefix length. |
| 574 | */ |
| 575 | @Test(expected = IllegalArgumentException.class) |
| 576 | public void testInvalidMakeNegativeMaskedAddressIPv4() { |
| 577 | IpAddress ipAddress = IpAddress.valueOf("1.2.3.5"); |
| 578 | IpAddress ipAddressMasked; |
| 579 | |
| 580 | ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, -1); |
| 581 | } |
| 582 | |
| 583 | /** |
| 584 | * Tests making of a masked address for invalid prefix length for IPv6: |
| 585 | * negative prefix length. |
| 586 | */ |
| 587 | @Test(expected = IllegalArgumentException.class) |
| 588 | public void testInvalidMakeNegativeMaskedAddressIPv6() { |
| 589 | IpAddress ipAddress = |
| 590 | IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8885"); |
| 591 | IpAddress ipAddressMasked; |
| 592 | |
| 593 | ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, -1); |
| 594 | } |
| 595 | |
| 596 | /** |
| 597 | * Tests making of a masked address for an invalid prefix length for IPv4: |
| 598 | * too long prefix length. |
| 599 | */ |
| 600 | @Test(expected = IllegalArgumentException.class) |
| 601 | public void testInvalidMakeTooLongMaskedAddressIPv4() { |
| 602 | IpAddress ipAddress = IpAddress.valueOf("1.2.3.5"); |
| 603 | IpAddress ipAddressMasked; |
| 604 | |
| 605 | ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 33); |
| 606 | } |
| 607 | |
| 608 | /** |
| 609 | * Tests making of a masked address for an invalid prefix length for IPv6: |
| 610 | * too long prefix length. |
| 611 | */ |
| 612 | @Test(expected = IllegalArgumentException.class) |
| 613 | public void testInvalidMakeTooLongMaskedAddressIPv6() { |
| 614 | IpAddress ipAddress = |
| 615 | IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8885"); |
| 616 | IpAddress ipAddressMasked; |
| 617 | |
| 618 | ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 129); |
| 619 | } |
| 620 | |
| 621 | /** |
| 622 | * Tests comparison of {@link IpAddress} for IPv4. |
| 623 | */ |
| 624 | @Test |
| 625 | public void testComparisonIPv4() { |
| 626 | IpAddress addr1, addr2, addr3, addr4; |
| 627 | |
| 628 | addr1 = IpAddress.valueOf("1.2.3.4"); |
| 629 | addr2 = IpAddress.valueOf("1.2.3.4"); |
| 630 | addr3 = IpAddress.valueOf("1.2.3.3"); |
| 631 | addr4 = IpAddress.valueOf("1.2.3.5"); |
| 632 | assertTrue(addr1.compareTo(addr2) == 0); |
| 633 | assertTrue(addr1.compareTo(addr3) > 0); |
| 634 | assertTrue(addr1.compareTo(addr4) < 0); |
| 635 | |
| 636 | addr1 = IpAddress.valueOf("255.2.3.4"); |
| 637 | addr2 = IpAddress.valueOf("255.2.3.4"); |
| 638 | addr3 = IpAddress.valueOf("255.2.3.3"); |
| 639 | addr4 = IpAddress.valueOf("255.2.3.5"); |
| 640 | assertTrue(addr1.compareTo(addr2) == 0); |
| 641 | assertTrue(addr1.compareTo(addr3) > 0); |
| 642 | assertTrue(addr1.compareTo(addr4) < 0); |
| 643 | } |
| 644 | |
| 645 | /** |
| 646 | * Tests comparison of {@link IpAddress} for IPv6. |
| 647 | */ |
| 648 | @Test |
| 649 | public void testComparisonIPv6() { |
| 650 | IpAddress addr1, addr2, addr3, addr4; |
| 651 | |
| 652 | addr1 = IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888"); |
| 653 | addr2 = IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888"); |
| 654 | addr3 = IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8887"); |
| 655 | addr4 = IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8889"); |
| 656 | assertTrue(addr1.compareTo(addr2) == 0); |
| 657 | assertTrue(addr1.compareTo(addr3) > 0); |
| 658 | assertTrue(addr1.compareTo(addr4) < 0); |
| 659 | |
| 660 | addr1 = IpAddress.valueOf("ffff:2222:3333:4444:5555:6666:7777:8888"); |
| 661 | addr2 = IpAddress.valueOf("ffff:2222:3333:4444:5555:6666:7777:8888"); |
| 662 | addr3 = IpAddress.valueOf("ffff:2222:3333:4444:5555:6666:7777:8887"); |
| 663 | addr4 = IpAddress.valueOf("ffff:2222:3333:4444:5555:6666:7777:8889"); |
| 664 | assertTrue(addr1.compareTo(addr2) == 0); |
| 665 | assertTrue(addr1.compareTo(addr3) > 0); |
| 666 | assertTrue(addr1.compareTo(addr4) < 0); |
| 667 | |
| 668 | addr1 = IpAddress.valueOf("ffff:2222:3333:4444:5555:6666:7777:8888"); |
| 669 | addr2 = IpAddress.valueOf("ffff:2222:3333:4444:5555:6666:7777:8888"); |
| 670 | addr3 = IpAddress.valueOf("ffff:2222:3333:4443:5555:6666:7777:8888"); |
| 671 | addr4 = IpAddress.valueOf("ffff:2222:3333:4445:5555:6666:7777:8888"); |
| 672 | assertTrue(addr1.compareTo(addr2) == 0); |
| 673 | assertTrue(addr1.compareTo(addr3) > 0); |
| 674 | assertTrue(addr1.compareTo(addr4) < 0); |
| 675 | } |
| 676 | |
| 677 | /** |
| 678 | * Tests equality of {@link IpAddress} for IPv4. |
| 679 | */ |
| 680 | @Test |
| 681 | public void testEqualityIPv4() { |
| 682 | IpAddress addr1, addr2; |
| 683 | |
| 684 | addr1 = IpAddress.valueOf("1.2.3.4"); |
| 685 | addr2 = IpAddress.valueOf("1.2.3.4"); |
| 686 | assertThat(addr1, is(addr2)); |
| 687 | |
| 688 | addr1 = IpAddress.valueOf("0.0.0.0"); |
| 689 | addr2 = IpAddress.valueOf("0.0.0.0"); |
| 690 | assertThat(addr1, is(addr2)); |
| 691 | |
| 692 | addr1 = IpAddress.valueOf("255.255.255.255"); |
| 693 | addr2 = IpAddress.valueOf("255.255.255.255"); |
| 694 | assertThat(addr1, is(addr2)); |
| 695 | } |
| 696 | |
| 697 | /** |
| 698 | * Tests equality of {@link IpAddress} for IPv6. |
| 699 | */ |
| 700 | @Test |
| 701 | public void testEqualityIPv6() { |
| 702 | IpAddress addr1, addr2; |
| 703 | |
| 704 | addr1 = IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888"); |
| 705 | addr2 = IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888"); |
| 706 | assertThat(addr1, is(addr2)); |
| 707 | |
| 708 | addr1 = IpAddress.valueOf("::"); |
| 709 | addr2 = IpAddress.valueOf("::"); |
| 710 | assertThat(addr1, is(addr2)); |
| 711 | |
| 712 | addr1 = IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"); |
| 713 | addr2 = IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"); |
| 714 | assertThat(addr1, is(addr2)); |
| 715 | } |
| 716 | |
| 717 | /** |
| 718 | * Tests non-equality of {@link IpAddress} for IPv4. |
| 719 | */ |
| 720 | @Test |
| 721 | public void testNonEqualityIPv4() { |
| 722 | IpAddress addr1, addr2, addr3, addr4; |
| 723 | |
| 724 | addr1 = IpAddress.valueOf("1.2.3.4"); |
| 725 | addr2 = IpAddress.valueOf("1.2.3.5"); |
| 726 | addr3 = IpAddress.valueOf("0.0.0.0"); |
| 727 | addr4 = IpAddress.valueOf("255.255.255.255"); |
| 728 | assertThat(addr1, is(not(addr2))); |
| 729 | assertThat(addr3, is(not(addr2))); |
| 730 | assertThat(addr4, is(not(addr2))); |
| 731 | } |
| 732 | |
| 733 | /** |
| 734 | * Tests non-equality of {@link IpAddress} for IPv6. |
| 735 | */ |
| 736 | @Test |
| 737 | public void testNonEqualityIPv6() { |
| 738 | IpAddress addr1, addr2, addr3, addr4; |
| 739 | |
| 740 | addr1 = IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888"); |
| 741 | addr2 = IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:888A"); |
| 742 | addr3 = IpAddress.valueOf("::"); |
| 743 | addr4 = IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"); |
| 744 | assertThat(addr1, is(not(addr2))); |
| 745 | assertThat(addr3, is(not(addr2))); |
| 746 | assertThat(addr4, is(not(addr2))); |
| 747 | } |
| 748 | |
| 749 | /** |
| 750 | * Tests object string representation for IPv4. |
| 751 | */ |
| 752 | @Test |
| 753 | public void testToStringIPv4() { |
| 754 | IpAddress ipAddress; |
| 755 | |
| 756 | ipAddress = IpAddress.valueOf("1.2.3.4"); |
| 757 | assertThat(ipAddress.toString(), is("1.2.3.4")); |
| 758 | |
| 759 | ipAddress = IpAddress.valueOf("0.0.0.0"); |
| 760 | assertThat(ipAddress.toString(), is("0.0.0.0")); |
| 761 | |
| 762 | ipAddress = IpAddress.valueOf("255.255.255.255"); |
| 763 | assertThat(ipAddress.toString(), is("255.255.255.255")); |
| 764 | } |
| 765 | |
| 766 | /** |
| 767 | * Tests object string representation for IPv6. |
| 768 | */ |
| 769 | @Test |
| 770 | public void testToStringIPv6() { |
| 771 | IpAddress ipAddress; |
| 772 | |
| 773 | ipAddress = |
| 774 | IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888"); |
| 775 | assertThat(ipAddress.toString(), |
| 776 | is("1111:2222:3333:4444:5555:6666:7777:8888")); |
| 777 | |
| 778 | ipAddress = IpAddress.valueOf("1111::8888"); |
| 779 | assertThat(ipAddress.toString(), is("1111::8888")); |
| 780 | |
| 781 | ipAddress = IpAddress.valueOf("1111::"); |
| 782 | assertThat(ipAddress.toString(), is("1111::")); |
| 783 | |
| 784 | ipAddress = IpAddress.valueOf("::8888"); |
| 785 | assertThat(ipAddress.toString(), is("::8888")); |
| 786 | |
| 787 | ipAddress = IpAddress.valueOf("::"); |
| 788 | assertThat(ipAddress.toString(), is("::")); |
| 789 | |
| 790 | ipAddress = |
| 791 | IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"); |
| 792 | assertThat(ipAddress.toString(), |
| 793 | is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")); |
| 794 | } |
| 795 | } |