IpAddressTest.java
32.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
/*
* Copyright 2014-2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onlab.packet;
import java.net.InetAddress;
import org.junit.Test;
import com.google.common.net.InetAddresses;
import com.google.common.testing.EqualsTester;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutableBaseClass;
/**
* Tests for class {@link IpAddress}.
*/
public class IpAddressTest {
/**
* Tests the immutability of {@link IpAddress}.
*/
@Test
public void testImmutable() {
assertThatClassIsImmutableBaseClass(IpAddress.class);
}
/**
* Tests the length of the address in bytes (octets).
*/
@Test
public void testAddrByteLength() {
assertThat(IpAddress.INET_BYTE_LENGTH, is(4));
assertThat(IpAddress.INET6_BYTE_LENGTH, is(16));
assertThat(IpAddress.byteLength(IpAddress.Version.INET), is(4));
assertThat(IpAddress.byteLength(IpAddress.Version.INET6), is(16));
}
/**
* Tests the length of the address in bits.
*/
@Test
public void testAddrBitLength() {
assertThat(IpAddress.INET_BIT_LENGTH, is(32));
assertThat(IpAddress.INET6_BIT_LENGTH, is(128));
}
/**
* Tests returning the IP address version.
*/
@Test
public void testVersion() {
IpAddress ipAddress;
// IPv4
ipAddress = IpAddress.valueOf("0.0.0.0");
assertThat(ipAddress.version(), is(IpAddress.Version.INET));
// IPv6
ipAddress = IpAddress.valueOf("::");
assertThat(ipAddress.version(), is(IpAddress.Version.INET6));
}
/**
* Tests whether the IP version of an address is IPv4.
*/
@Test
public void testIsIp4() {
IpAddress ipAddress;
// IPv4
ipAddress = IpAddress.valueOf("0.0.0.0");
assertTrue(ipAddress.isIp4());
// IPv6
ipAddress = IpAddress.valueOf("::");
assertFalse(ipAddress.isIp4());
}
/**
* Tests whether the IP version of an address is IPv6.
*/
@Test
public void testIsIp6() {
IpAddress ipAddress;
// IPv4
ipAddress = IpAddress.valueOf("0.0.0.0");
assertFalse(ipAddress.isIp6());
// IPv6
ipAddress = IpAddress.valueOf("::");
assertTrue(ipAddress.isIp6());
}
/**
* Tests getting the Ip4Address and Ip6Address view of the IP address.
*/
@Test
public void testGetIp4AndIp6AddressView() {
IpAddress ipAddress;
Ip4Address ip4Address;
Ip6Address ip6Address;
// Pure IPv4 IpAddress
ipAddress = IpAddress.valueOf("1.2.3.4");
ip4Address = ipAddress.getIp4Address();
ip6Address = ipAddress.getIp6Address();
assertThat(ip4Address.toString(), is("1.2.3.4"));
assertNull(ip6Address);
// IPv4 IpAddress that is Ip4Address
ipAddress = Ip4Address.valueOf("1.2.3.4");
ip4Address = ipAddress.getIp4Address();
ip6Address = ipAddress.getIp6Address();
assertThat(ip4Address.toString(), is("1.2.3.4"));
assertNull(ip6Address);
// Pure IPv6 IpAddress
ipAddress = IpAddress.valueOf("1111:2222::");
ip4Address = ipAddress.getIp4Address();
ip6Address = ipAddress.getIp6Address();
assertNull(ip4Address);
assertThat(ip6Address.toString(), is("1111:2222::"));
// IPv6 IpAddress that is Ip6Address
ipAddress = Ip6Address.valueOf("1111:2222::");
ip4Address = ipAddress.getIp4Address();
ip6Address = ipAddress.getIp6Address();
assertNull(ip4Address);
assertThat(ip6Address.toString(), is("1111:2222::"));
}
/**
* Tests returning an IPv4 address as a byte array.
*/
@Test
public void testAddressToOctetsIPv4() {
IpAddress ipAddress;
byte[] value;
value = new byte[] {1, 2, 3, 4};
ipAddress = IpAddress.valueOf("1.2.3.4");
assertThat(ipAddress.toOctets(), is(value));
value = new byte[] {0, 0, 0, 0};
ipAddress = IpAddress.valueOf("0.0.0.0");
assertThat(ipAddress.toOctets(), is(value));
value = new byte[] {(byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff};
ipAddress = IpAddress.valueOf("255.255.255.255");
assertThat(ipAddress.toOctets(), is(value));
}
/**
* Tests returning an IPv6 address as a byte array.
*/
@Test
public void testAddressToOctetsIPv6() {
IpAddress ipAddress;
byte[] value;
value = new byte[] {0x11, 0x11, 0x22, 0x22,
0x33, 0x33, 0x44, 0x44,
0x55, 0x55, 0x66, 0x66,
0x77, 0x77,
(byte) 0x88, (byte) 0x88};
ipAddress =
IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888");
assertThat(ipAddress.toOctets(), is(value));
value = new byte[] {0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00};
ipAddress = IpAddress.valueOf("::");
assertThat(ipAddress.toOctets(), is(value));
value = new byte[] {(byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff};
ipAddress =
IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
assertThat(ipAddress.toOctets(), is(value));
}
/**
* Tests valueOf() converter for IPv4 integer value.
*/
@Test
public void testValueOfForIntegerIPv4() {
IpAddress ipAddress;
ipAddress = IpAddress.valueOf(0x01020304);
assertThat(ipAddress.toString(), is("1.2.3.4"));
ipAddress = IpAddress.valueOf(0);
assertThat(ipAddress.toString(), is("0.0.0.0"));
ipAddress = IpAddress.valueOf(0xffffffff);
assertThat(ipAddress.toString(), is("255.255.255.255"));
}
/**
* Tests valueOf() converter for IPv4 byte array.
*/
@Test
public void testValueOfByteArrayIPv4() {
IpAddress ipAddress;
byte[] value;
value = new byte[] {1, 2, 3, 4};
ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value);
assertThat(ipAddress.toString(), is("1.2.3.4"));
value = new byte[] {0, 0, 0, 0};
ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value);
assertThat(ipAddress.toString(), is("0.0.0.0"));
value = new byte[] {(byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff};
ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value);
assertThat(ipAddress.toString(), is("255.255.255.255"));
}
/**
* Tests valueOf() converter for IPv6 byte array.
*/
@Test
public void testValueOfByteArrayIPv6() {
IpAddress ipAddress;
byte[] value;
value = new byte[] {0x11, 0x11, 0x22, 0x22,
0x33, 0x33, 0x44, 0x44,
0x55, 0x55, 0x66, 0x66,
0x77, 0x77,
(byte) 0x88, (byte) 0x88};
ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value);
assertThat(ipAddress.toString(),
is("1111:2222:3333:4444:5555:6666:7777:8888"));
value = new byte[] {0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00};
ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value);
assertThat(ipAddress.toString(), is("::"));
value = new byte[] {(byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff};
ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value);
assertThat(ipAddress.toString(),
is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"));
}
/**
* Tests invalid valueOf() converter for a null array for IPv4.
*/
@Test(expected = NullPointerException.class)
public void testInvalidValueOfNullArrayIPv4() {
IpAddress ipAddress;
byte[] value;
value = null;
ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value);
}
/**
* Tests invalid valueOf() converter for a null array for IPv6.
*/
@Test(expected = NullPointerException.class)
public void testInvalidValueOfNullArrayIPv6() {
IpAddress ipAddress;
byte[] value;
value = null;
ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value);
}
/**
* Tests invalid valueOf() converger for an array that is too short for
* IPv4.
*/
@Test(expected = IllegalArgumentException.class)
public void testInvalidValueOfShortArrayIPv4() {
IpAddress ipAddress;
byte[] value;
value = new byte[] {1, 2, 3};
ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value);
}
/**
* Tests invalid valueOf() converger for an array that is too short for
* IPv6.
*/
@Test(expected = IllegalArgumentException.class)
public void testInvalidValueOfShortArrayIPv6() {
IpAddress ipAddress;
byte[] value;
value = new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9};
ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value);
}
/**
* Tests valueOf() converter for IPv4 byte array and an offset.
*/
@Test
public void testValueOfByteArrayOffsetIPv4() {
IpAddress ipAddress;
byte[] value;
value = new byte[] {11, 22, 33, // Preamble
1, 2, 3, 4,
44, 55}; // Extra bytes
ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value, 3);
assertThat(ipAddress.toString(), is("1.2.3.4"));
value = new byte[] {11, 22, // Preamble
0, 0, 0, 0,
33}; // Extra bytes
ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value, 2);
assertThat(ipAddress.toString(), is("0.0.0.0"));
value = new byte[] {11, 22, // Preamble
(byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff,
33}; // Extra bytes
ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value, 2);
assertThat(ipAddress.toString(), is("255.255.255.255"));
}
/**
* Tests valueOf() converter for IPv6 byte array and an offset.
*/
@Test
public void testValueOfByteArrayOffsetIPv6() {
IpAddress ipAddress;
byte[] value;
value = new byte[] {11, 22, 33, // Preamble
0x11, 0x11, 0x22, 0x22,
0x33, 0x33, 0x44, 0x44,
0x55, 0x55, 0x66, 0x66,
0x77, 0x77,
(byte) 0x88, (byte) 0x88,
44, 55}; // Extra bytes
ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value, 3);
assertThat(ipAddress.toString(),
is("1111:2222:3333:4444:5555:6666:7777:8888"));
value = new byte[] {11, 22, // Preamble
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
33}; // Extra bytes
ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value, 2);
assertThat(ipAddress.toString(), is("::"));
value = new byte[] {11, 22, // Preamble
(byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff,
33}; // Extra bytes
ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value, 2);
assertThat(ipAddress.toString(),
is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"));
}
/**
* Tests invalid valueOf() converger for an array and an invalid offset
* for IPv4.
*/
@Test(expected = IllegalArgumentException.class)
public void testInvalidValueOfArrayInvalidOffsetIPv4() {
IpAddress ipAddress;
byte[] value;
value = new byte[] {11, 22, 33, // Preamble
1, 2, 3, 4,
44, 55}; // Extra bytes
ipAddress = IpAddress.valueOf(IpAddress.Version.INET, value, 6);
}
/**
* Tests invalid valueOf() converger for an array and an invalid offset
* for IPv6.
*/
@Test(expected = IllegalArgumentException.class)
public void testInvalidValueOfArrayInvalidOffsetIPv6() {
IpAddress ipAddress;
byte[] value;
value = new byte[] {11, 22, 33, // Preamble
0x11, 0x11, 0x22, 0x22,
0x33, 0x33, 0x44, 0x44,
0x55, 0x55, 0x66, 0x66,
0x77, 0x77,
(byte) 0x88, (byte) 0x88,
44, 55}; // Extra bytes
ipAddress = IpAddress.valueOf(IpAddress.Version.INET6, value, 6);
}
/**
* Tests valueOf() converter for IPv4 InetAddress.
*/
@Test
public void testValueOfInetAddressIPv4() {
IpAddress ipAddress;
InetAddress inetAddress;
inetAddress = InetAddresses.forString("1.2.3.4");
ipAddress = IpAddress.valueOf(inetAddress);
assertThat(ipAddress.toString(), is("1.2.3.4"));
inetAddress = InetAddresses.forString("0.0.0.0");
ipAddress = IpAddress.valueOf(inetAddress);
assertThat(ipAddress.toString(), is("0.0.0.0"));
inetAddress = InetAddresses.forString("255.255.255.255");
ipAddress = IpAddress.valueOf(inetAddress);
assertThat(ipAddress.toString(), is("255.255.255.255"));
}
/**
* Tests valueOf() converter for IPv6 InetAddress.
*/
@Test
public void testValueOfInetAddressIPv6() {
IpAddress ipAddress;
InetAddress inetAddress;
inetAddress =
InetAddresses.forString("1111:2222:3333:4444:5555:6666:7777:8888");
ipAddress = IpAddress.valueOf(inetAddress);
assertThat(ipAddress.toString(),
is("1111:2222:3333:4444:5555:6666:7777:8888"));
inetAddress = InetAddresses.forString("::");
ipAddress = IpAddress.valueOf(inetAddress);
assertThat(ipAddress.toString(), is("::"));
inetAddress =
InetAddresses.forString("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
ipAddress = IpAddress.valueOf(inetAddress);
assertThat(ipAddress.toString(),
is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"));
}
/**
* Tests valueOf() converter for IPv4 string.
*/
@Test
public void testValueOfStringIPv4() {
IpAddress ipAddress;
ipAddress = IpAddress.valueOf("1.2.3.4");
assertThat(ipAddress.toString(), is("1.2.3.4"));
ipAddress = IpAddress.valueOf("0.0.0.0");
assertThat(ipAddress.toString(), is("0.0.0.0"));
ipAddress = IpAddress.valueOf("255.255.255.255");
assertThat(ipAddress.toString(), is("255.255.255.255"));
}
/**
* Tests valueOf() converter for IPv6 string.
*/
@Test
public void testValueOfStringIPv6() {
IpAddress ipAddress;
ipAddress =
IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888");
assertThat(ipAddress.toString(),
is("1111:2222:3333:4444:5555:6666:7777:8888"));
ipAddress = IpAddress.valueOf("::");
assertThat(ipAddress.toString(), is("::"));
ipAddress =
IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
assertThat(ipAddress.toString(),
is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"));
}
/**
* Tests invalid valueOf() converter for a null string.
*/
@Test(expected = NullPointerException.class)
public void testInvalidValueOfNullString() {
IpAddress ipAddress;
String fromString = null;
ipAddress = IpAddress.valueOf(fromString);
}
/**
* Tests invalid valueOf() converter for an empty string.
*/
@Test(expected = IllegalArgumentException.class)
public void testInvalidValueOfEmptyString() {
IpAddress ipAddress;
String fromString = "";
ipAddress = IpAddress.valueOf(fromString);
}
/**
* Tests invalid valueOf() converter for an incorrect string.
*/
@Test(expected = IllegalArgumentException.class)
public void testInvalidValueOfIncorrectString() {
IpAddress ipAddress;
String fromString = "NoSuchIpAddress";
ipAddress = IpAddress.valueOf(fromString);
}
/**
* Tests making a mask prefix for a given prefix length for IPv4.
*/
@Test
public void testMakeMaskPrefixIPv4() {
IpAddress ipAddress;
ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET, 25);
assertThat(ipAddress.toString(), is("255.255.255.128"));
ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET, 0);
assertThat(ipAddress.toString(), is("0.0.0.0"));
ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET, 32);
assertThat(ipAddress.toString(), is("255.255.255.255"));
}
/**
* Tests making a mask prefix for a given prefix length for IPv6.
*/
@Test
public void testMakeMaskPrefixIPv6() {
IpAddress ipAddress;
ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET6, 8);
assertThat(ipAddress.toString(), is("ff00::"));
ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET6, 120);
assertThat(ipAddress.toString(),
is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ff00"));
ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET6, 0);
assertThat(ipAddress.toString(), is("::"));
ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET6, 128);
assertThat(ipAddress.toString(),
is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"));
ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET6, 64);
assertThat(ipAddress.toString(), is("ffff:ffff:ffff:ffff::"));
}
/**
* Tests making a mask prefix for an invalid prefix length for IPv4:
* negative prefix length.
*/
@Test(expected = IllegalArgumentException.class)
public void testInvalidMakeNegativeMaskPrefixIPv4() {
IpAddress ipAddress;
ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET, -1);
}
/**
* Tests making a mask prefix for an invalid prefix length for IPv6:
* negative prefix length.
*/
@Test(expected = IllegalArgumentException.class)
public void testInvalidMakeNegativeMaskPrefixIPv6() {
IpAddress ipAddress;
ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET6, -1);
}
/**
* Tests making a mask prefix for an invalid prefix length for IPv4:
* too long prefix length.
*/
@Test(expected = IllegalArgumentException.class)
public void testInvalidMakeTooLongMaskPrefixIPv4() {
IpAddress ipAddress;
ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET, 33);
}
/**
* Tests making a mask prefix for an invalid prefix length for IPv6:
* too long prefix length.
*/
@Test(expected = IllegalArgumentException.class)
public void testInvalidMakeTooLongMaskPrefixIPv6() {
IpAddress ipAddress;
ipAddress = IpAddress.makeMaskPrefix(IpAddress.Version.INET6, 129);
}
/**
* Tests making of a masked address for IPv4.
*/
@Test
public void testMakeMaskedAddressIPv4() {
IpAddress ipAddress = IpAddress.valueOf("1.2.3.5");
IpAddress ipAddressMasked;
ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 24);
assertThat(ipAddressMasked.toString(), is("1.2.3.0"));
ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 0);
assertThat(ipAddressMasked.toString(), is("0.0.0.0"));
ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 32);
assertThat(ipAddressMasked.toString(), is("1.2.3.5"));
}
/**
* Tests making of a masked address for IPv6.
*/
@Test
public void testMakeMaskedAddressIPv6() {
IpAddress ipAddress =
IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8885");
IpAddress ipAddressMasked;
ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 8);
assertThat(ipAddressMasked.toString(), is("1100::"));
ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 120);
assertThat(ipAddressMasked.toString(),
is("1111:2222:3333:4444:5555:6666:7777:8800"));
ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 0);
assertThat(ipAddressMasked.toString(), is("::"));
ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 128);
assertThat(ipAddressMasked.toString(),
is("1111:2222:3333:4444:5555:6666:7777:8885"));
ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 64);
assertThat(ipAddressMasked.toString(), is("1111:2222:3333:4444::"));
}
/**
* Tests making of a masked address for invalid prefix length for IPv4:
* negative prefix length.
*/
@Test(expected = IllegalArgumentException.class)
public void testInvalidMakeNegativeMaskedAddressIPv4() {
IpAddress ipAddress = IpAddress.valueOf("1.2.3.5");
IpAddress ipAddressMasked;
ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, -1);
}
/**
* Tests making of a masked address for invalid prefix length for IPv6:
* negative prefix length.
*/
@Test(expected = IllegalArgumentException.class)
public void testInvalidMakeNegativeMaskedAddressIPv6() {
IpAddress ipAddress =
IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8885");
IpAddress ipAddressMasked;
ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, -1);
}
/**
* Tests making of a masked address for an invalid prefix length for IPv4:
* too long prefix length.
*/
@Test(expected = IllegalArgumentException.class)
public void testInvalidMakeTooLongMaskedAddressIPv4() {
IpAddress ipAddress = IpAddress.valueOf("1.2.3.5");
IpAddress ipAddressMasked;
ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 33);
}
/**
* Tests making of a masked address for an invalid prefix length for IPv6:
* too long prefix length.
*/
@Test(expected = IllegalArgumentException.class)
public void testInvalidMakeTooLongMaskedAddressIPv6() {
IpAddress ipAddress =
IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8885");
IpAddress ipAddressMasked;
ipAddressMasked = IpAddress.makeMaskedAddress(ipAddress, 129);
}
/**
* Tests if address is zero for IPv4.
*/
@Test
public void testIsZeroIPv4() {
IpAddress normalIP = IpAddress.valueOf("10.0.0.1");
IpAddress zeroIP = IpAddress.valueOf("0.0.0.0");
assertFalse(normalIP.isZero());
assertTrue(zeroIP.isZero());
}
/**
* Tests if address is zero for IPv6.
*/
@Test
public void testIsZeroIPv6() {
IpAddress normalIP = IpAddress.valueOf("fe80::1");
IpAddress zeroIP = IpAddress.valueOf("::");
assertFalse(normalIP.isZero());
assertTrue(zeroIP.isZero());
}
/**
* Tests if address is self-assigned for IPv4.
*/
@Test
public void testIsSelfAssignedIpv4() {
IpAddress normalIP = IpAddress.valueOf("10.0.0.1");
IpAddress selfAssignedIP = IpAddress.valueOf("169.1.2.3");
assertFalse(normalIP.isSelfAssigned());
assertTrue(selfAssignedIP.isSelfAssigned());
}
/**
* Tests if the address is a multicast address.
*/
@Test
public void testIsMulticast() {
IpAddress v4Unicast = IpAddress.valueOf("10.0.0.1");
IpAddress v4Multicast = IpAddress.valueOf("224.0.0.1");
IpAddress v6Unicast = IpAddress.valueOf("1000::1");
IpAddress v6Multicast = IpAddress.valueOf("ff02::1");
assertFalse(v4Unicast.isMulticast());
assertTrue(v4Multicast.isMulticast());
assertFalse(v6Unicast.isMulticast());
assertTrue(v6Multicast.isMulticast());
}
/**
* Tests comparison of {@link IpAddress} for IPv4.
*/
@Test
public void testComparisonIPv4() {
IpAddress addr1, addr2, addr3, addr4;
addr1 = IpAddress.valueOf("1.2.3.4");
addr2 = IpAddress.valueOf("1.2.3.4");
addr3 = IpAddress.valueOf("1.2.3.3");
addr4 = IpAddress.valueOf("1.2.3.5");
assertTrue(addr1.compareTo(addr2) == 0);
assertTrue(addr1.compareTo(addr3) > 0);
assertTrue(addr1.compareTo(addr4) < 0);
addr1 = IpAddress.valueOf("255.2.3.4");
addr2 = IpAddress.valueOf("255.2.3.4");
addr3 = IpAddress.valueOf("255.2.3.3");
addr4 = IpAddress.valueOf("255.2.3.5");
assertTrue(addr1.compareTo(addr2) == 0);
assertTrue(addr1.compareTo(addr3) > 0);
assertTrue(addr1.compareTo(addr4) < 0);
}
/**
* Tests comparison of {@link IpAddress} for IPv6.
*/
@Test
public void testComparisonIPv6() {
IpAddress addr1, addr2, addr3, addr4;
addr1 = IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888");
addr2 = IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888");
addr3 = IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8887");
addr4 = IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8889");
assertTrue(addr1.compareTo(addr2) == 0);
assertTrue(addr1.compareTo(addr3) > 0);
assertTrue(addr1.compareTo(addr4) < 0);
addr1 = IpAddress.valueOf("ffff:2222:3333:4444:5555:6666:7777:8888");
addr2 = IpAddress.valueOf("ffff:2222:3333:4444:5555:6666:7777:8888");
addr3 = IpAddress.valueOf("ffff:2222:3333:4444:5555:6666:7777:8887");
addr4 = IpAddress.valueOf("ffff:2222:3333:4444:5555:6666:7777:8889");
assertTrue(addr1.compareTo(addr2) == 0);
assertTrue(addr1.compareTo(addr3) > 0);
assertTrue(addr1.compareTo(addr4) < 0);
addr1 = IpAddress.valueOf("ffff:2222:3333:4444:5555:6666:7777:8888");
addr2 = IpAddress.valueOf("ffff:2222:3333:4444:5555:6666:7777:8888");
addr3 = IpAddress.valueOf("ffff:2222:3333:4443:5555:6666:7777:8888");
addr4 = IpAddress.valueOf("ffff:2222:3333:4445:5555:6666:7777:8888");
assertTrue(addr1.compareTo(addr2) == 0);
assertTrue(addr1.compareTo(addr3) > 0);
assertTrue(addr1.compareTo(addr4) < 0);
}
/**
* Tests equality of {@link IpAddress} for IPv4.
*/
@Test
public void testEqualityIPv4() {
new EqualsTester()
.addEqualityGroup(IpAddress.valueOf("1.2.3.4"),
IpAddress.valueOf("1.2.3.4"))
.addEqualityGroup(IpAddress.valueOf("1.2.3.5"),
IpAddress.valueOf("1.2.3.5"))
.addEqualityGroup(IpAddress.valueOf("0.0.0.0"),
IpAddress.valueOf("0.0.0.0"))
.addEqualityGroup(IpAddress.valueOf("255.255.255.255"),
IpAddress.valueOf("255.255.255.255"))
.testEquals();
}
/**
* Tests equality of {@link IpAddress} for IPv6.
*/
@Test
public void testEqualityIPv6() {
new EqualsTester()
.addEqualityGroup(
IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888"),
IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888"))
.addEqualityGroup(
IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:888a"),
IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:888a"))
.addEqualityGroup(
IpAddress.valueOf("::"),
IpAddress.valueOf("::"))
.addEqualityGroup(
IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"),
IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"))
.testEquals();
}
/**
* Tests object string representation for IPv4.
*/
@Test
public void testToStringIPv4() {
IpAddress ipAddress;
ipAddress = IpAddress.valueOf("1.2.3.4");
assertThat(ipAddress.toString(), is("1.2.3.4"));
ipAddress = IpAddress.valueOf("0.0.0.0");
assertThat(ipAddress.toString(), is("0.0.0.0"));
ipAddress = IpAddress.valueOf("255.255.255.255");
assertThat(ipAddress.toString(), is("255.255.255.255"));
}
/**
* Tests object string representation for IPv6.
*/
@Test
public void testToStringIPv6() {
IpAddress ipAddress;
ipAddress =
IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888");
assertThat(ipAddress.toString(),
is("1111:2222:3333:4444:5555:6666:7777:8888"));
ipAddress = IpAddress.valueOf("1111::8888");
assertThat(ipAddress.toString(), is("1111::8888"));
ipAddress = IpAddress.valueOf("1111::");
assertThat(ipAddress.toString(), is("1111::"));
ipAddress = IpAddress.valueOf("::8888");
assertThat(ipAddress.toString(), is("::8888"));
ipAddress = IpAddress.valueOf("::");
assertThat(ipAddress.toString(), is("::"));
ipAddress =
IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
assertThat(ipAddress.toString(),
is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"));
ipAddress =
IpAddress.valueOf("::1111:2222");
assertThat(ipAddress.toString(),
is("::1111:2222"));
ipAddress =
IpAddress.valueOf("1:0:0:1:0:0:2:3");
assertThat(ipAddress.toString(),
is("1::1:0:0:2:3"));
ipAddress =
IpAddress.valueOf("::0123:0004");
assertThat(ipAddress.toString(),
is("::123:4"));
ipAddress =
IpAddress.valueOf("0:0:1:1:0:0:1:1");
assertThat(ipAddress.toString(),
is("::1:1:0:0:1:1"));
ipAddress =
IpAddress.valueOf("1:1a2b::");
assertThat(ipAddress.toString(),
is("1:1a2b::"));
ipAddress =
IpAddress.valueOf("0:0:00:00:0000:00:00:000");
assertThat(ipAddress.toString(),
is("::"));
ipAddress =
IpAddress.valueOf("0:0:0:1:0:0:0:0");
assertThat(ipAddress.toString(),
is("0:0:0:1::"));
}
}