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