2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 package org.apache.harmony.luni.tests.java.net;
20 import dalvik.annotation.TestTargetClass;
21 import dalvik.annotation.TestLevel;
22 import dalvik.annotation.TestTargetNew;
24 import java.io.Serializable;
25 import java.net.Inet6Address;
26 import java.net.InetAddress;
27 import java.net.NetworkInterface;
28 import java.net.UnknownHostException;
30 import java.security.Permission;
32 import org.apache.harmony.luni.tests.java.net.InetAddressTest.MockSecurityManager;
33 import org.apache.harmony.testframework.serialization.SerializationTest;
34 import org.apache.harmony.testframework.serialization.SerializationTest.SerializableAssert;
36 @TestTargetClass(Inet6Address.class)
37 public class Inet6AddressTest extends junit.framework.TestCase {
40 * @tests java.net.Inet6Address#isMulticastAddress()
43 level = TestLevel.COMPLETE,
45 method = "isMulticastAddress",
48 public void test_isMulticastAddress() {
51 InetAddress addr = null;
55 // IP V6 regular multicast and non-multicast tests
57 // Create 2 IP v6 addresses and call "isMulticastAddress()"
58 // A prefix of "11111111" means that the address is multicast
59 // The first one will be one with the prefix the second without
61 addrName = "FFFF::42:42"; // 11111111 = FFFF
62 addr = Inet6Address.getByName(addrName);
63 assertTrue("Multicast address " + addrName + " not detected.", addr
64 .isMulticastAddress());
66 addrName = "42::42:42"; // an non-multicast address
67 addr = Inet6Address.getByName(addrName);
68 assertTrue("Non multicast address " + addrName
69 + " reporting as a multicast address.", !addr
70 .isMulticastAddress());
72 // IPv4-compatible IPv6 address tests
74 // Now create 2 IP v6 addresses that are IP v4 compatable
75 // to IP v6 addresses. The address prefix for a multicast ip v4
76 // address is 1110 for the last 16 bits ::d.d.d.d
77 // We expect these to be false
79 addrName = "::224.42.42.42"; // an ipv4 multicast addr 1110 = 224
80 addr = Inet6Address.getByName(addrName);
81 assertTrue("IPv4 compatable address " + addrName
82 + " reported incorrectly as multicast.", !addr
83 .isMulticastAddress());
85 addrName = "::42.42.42.42"; // an ipv4 non-multicast address
86 addr = Inet6Address.getByName(addrName);
87 assertTrue("IPv4 compatable address " + addrName
88 + " reported incorrectly as multicast.", !addr
89 .isMulticastAddress());
91 // IPv4-mapped IPv6 address tests
93 // Now create 2 IP v6 addresses that are IP v4 compatable
94 // to IP v6 addresses. The address prefix for a multicast ip v4
95 // address is 1110 for the last 16 bits ::FFFF:d.d.d.d
97 addrName = "::FFFF:224.42.42.42"; // an ipv4 multicast addr 1110 =
99 addr = Inet6Address.getByName(addrName);
100 assertTrue("IPv4-mapped IPv6 multicast address " + addrName
101 + " not detected.", addr.isMulticastAddress());
103 addrName = "::FFFF:42.42.42.42"; // an ipv4 non-multicast address
104 addr = Inet6Address.getByName(addrName);
105 assertTrue("IPv4-mapped IPv6 non-multicast address " + addrName
106 + " reporting as a multicast address.", !addr
107 .isMulticastAddress());
108 } catch (Exception e) {
109 fail("Unknown address : " + addrName);
114 * @tests java.net.Inet6Address#isAnyLocalAddress()
117 level = TestLevel.COMPLETE,
119 method = "isAnyLocalAddress",
122 public void test_isAnyLocalAddress() {
124 String addrName = "";
125 InetAddress addr = null;
129 // test to ensure that the unspecified address returns tru
130 addrName = "::0"; // The unspecified address
131 addr = InetAddress.getByName(addrName);
133 "The unspecified (also known as wildcard and any local address) "
134 + addrName + " not detected.", addr
135 .isAnyLocalAddress());
137 addrName = "::"; // another form of the unspecified address
138 addr = InetAddress.getByName(addrName);
140 "The unspecified (also known as wildcard and any local address) "
141 + addrName + " not detected.", addr
142 .isAnyLocalAddress());
144 addrName = "::1"; // The loopback address
145 addr = InetAddress.getByName(addrName);
146 assertTrue("The addresses " + addrName
147 + " incorrectly reporting an the unspecified address.",
148 !addr.isAnyLocalAddress());
150 } catch (Exception e) {
151 fail("Unknown address : " + addrName);
156 * @tests java.net.Inet6Address#isLoopbackAddress()
159 level = TestLevel.COMPLETE,
161 method = "isLoopbackAddress",
164 public void test_isLoopbackAddress() {
166 String addrName = "";
169 // IP V6 regular address tests for loopback
170 // The loopback address for IPv6 is ::1
173 InetAddress addr = Inet6Address.getByName(addrName);
174 assertTrue("IPv6 loopback address " + addrName + " not detected.",
175 addr.isLoopbackAddress());
178 addr = Inet6Address.getByName(addrName);
179 assertTrue("IPv6 address incorrectly " + addrName
180 + " detected as a loopback address.", !addr
181 .isLoopbackAddress());
183 // a loopback address should be 127.d.d.d
184 addrName = "42:42::42:42";
185 addr = Inet6Address.getByName(addrName);
186 assertTrue("IPv6 address incorrectly " + addrName
187 + " detected as a loopback address.", !addr
188 .isLoopbackAddress());
190 // IPv4-compatible IPv6 address tests
192 // Now create 2 IP v6 addresses that are IP v4 compatable
193 // to IP v6 addresses. The address prefix for a multicast ip v4
194 // address is 1110 for the last 16 bits ::d.d.d.d
195 // We expect these to be false, as they are not IPv4 addresses
197 // a loopback address should be 127.d.d.d
198 addrName = "::127.0.0.0";
199 addr = Inet6Address.getByName(addrName);
200 assertTrue("IPv4-compatible IPv6 address " + addrName
201 + " detected incorrectly as a loopback.", !addr
202 .isLoopbackAddress());
204 addrName = "::127.42.42.42"; // a loopback address should be
206 addr = Inet6Address.getByName(addrName);
207 assertTrue("IPv4-compatible IPv6 address " + addrName
208 + " detected incorrectly as a loopback.", !addr
209 .isLoopbackAddress());
211 // a loopback address should be 127.d.d.d
212 addrName = "::42.42.42.42";
213 addr = Inet6Address.getByName(addrName);
214 assertTrue("IPv4-compatible IPv6 address " + addrName
215 + " detected incorrectly as a loopback.", !addr
216 .isLoopbackAddress());
218 // IPv4-mapped IPv6 address tests
220 // Now create 2 IP v6 addresses that are IP v4 compatable
221 // to IP v6 addresses. The address prefix for a multicast ip v4
222 // address is 1110 for the last 16 bits ::FFFF:d.d.d.d
224 // a loopback address should be 127.d.d.d
225 addrName = "::FFFF:127.0.0.0";
226 addr = Inet6Address.getByName(addrName);
227 assertTrue("IPv4-compatible IPv6 loopback address " + addrName
228 + " not detected.", addr.isLoopbackAddress());
230 // a loopback address should be 127.d.d.d
231 addrName = "::FFFF:127.42.42.42";
232 addr = Inet6Address.getByName(addrName);
233 assertTrue("IPv4-compatible IPv6 loopback address " + addrName
234 + " not detected.", addr.isLoopbackAddress());
236 // a loopback address should be 127.d.d.d
237 addrName = "::FFFF:42.42.42.42";
238 addr = Inet6Address.getByName(addrName);
239 assertTrue("IPv4-compatible IPv6 address incorrectly " + addrName
240 + " detected as a loopback address.", !addr
241 .isLoopbackAddress());
243 } catch (UnknownHostException e) {
244 fail("Unknown address : " + addrName);
249 * @tests java.net.Inet6Address#isLinkLocalAddress()
252 level = TestLevel.COMPLETE,
254 method = "isLinkLocalAddress",
257 public void test_isLinkLocalAddress() {
259 String addrName = "";
261 // IP V6 regular address tests for link local addresses
263 // Link local addresses are FE80:: -
264 // FEBF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF
266 addrName = "FE80::0";
267 InetAddress addr = Inet6Address.getByName(addrName);
269 "IPv6 link local address " + addrName + " not detected.",
270 addr.isLinkLocalAddress());
272 addrName = "FEBF::FFFF:FFFF:FFFF:FFFF";
273 addr = Inet6Address.getByName(addrName);
275 "IPv6 link local address " + addrName + " not detected.",
276 addr.isLinkLocalAddress());
278 addrName = "FEC0::1";
279 addr = Inet6Address.getByName(addrName);
280 assertTrue("IPv6 address " + addrName
281 + " detected incorrectly as a link local address.", !addr
282 .isLinkLocalAddress());
284 addrName = "FD80::1:FFFF:FFFF:FFFF:FFFF";
285 addr = Inet6Address.getByName(addrName);
286 assertTrue("IPv6 address " + addrName
287 + " detected incorrectly as a link local address.", !addr
288 .isLinkLocalAddress());
290 addrName = "FE7F::FFFF:FFFF:FFFF:FFFF";
291 addr = Inet6Address.getByName(addrName);
292 assertTrue("IPv6 address " + addrName
293 + " detected incorrectly as a link local address.", !addr
294 .isLinkLocalAddress());
295 } catch (Exception e) {
296 fail("Unknown address : " + addrName);
302 * @tests java.net.Inet6Address#isSiteLocalAddress()
305 level = TestLevel.COMPLETE,
307 method = "isSiteLocalAddress",
310 public void test_isSiteLocalAddress() {
311 String addrName = "";
313 // IP V6 regular address tests for link local addresses
315 // Link local addresses are FEC0::0 through to
316 // FEFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF
318 addrName = "FEC0::0";
319 InetAddress addr = Inet6Address.getByName(addrName);
321 "IPv6 site local address " + addrName + " not detected.",
322 addr.isSiteLocalAddress());
324 addrName = "FEFF::FFFF:FFFF:FFFF:FFFF:FFFF";
325 addr = Inet6Address.getByName(addrName);
327 "IPv6 site local address " + addrName + " not detected.",
328 addr.isSiteLocalAddress());
330 addrName = "FEBF::FFFF:FFFF:FFFF:FFFF:FFFF";
331 addr = Inet6Address.getByName(addrName);
332 assertTrue("IPv6 address " + addrName
333 + " detected incorrectly as a site local address.", !addr
334 .isSiteLocalAddress());
336 addrName = "FFC0::0";
337 addr = Inet6Address.getByName(addrName);
338 assertTrue("IPv6 address " + addrName
339 + " detected incorrectly as a site local address.", !addr
340 .isSiteLocalAddress());
342 } catch (Exception e) {
343 fail("Unknown address : " + addrName);
348 * @tests java.net.Inet6Address#isMCGlobal()
351 level = TestLevel.COMPLETE,
353 method = "isMCGlobal",
356 public void test_isMCGlobal() {
357 String addrName = "";
359 // IP V6 regular address tests for Mulitcase Global addresses
361 // Multicast global addresses are FFxE:/112 where x is
362 // a set of flags, and the addition 112 bits make up
363 // the global address space
365 addrName = "FF0E::0";
366 InetAddress addr = Inet6Address.getByName(addrName);
367 assertTrue("IPv6 global mutlicast address " + addrName
368 + " not detected.", addr.isMCGlobal());
370 addrName = "FF0E:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF";
371 addr = Inet6Address.getByName(addrName);
372 assertTrue("IPv6 global multicast address " + addrName
373 + " not detected.", addr.isMCGlobal());
375 // a currently invalid address as the prefix FFxE
376 // is only valid for x = {1,0} as the rest are reserved
377 addrName = "FFFE::0";
378 addr = Inet6Address.getByName(addrName);
379 assertTrue("IPv6 global mutlicast address " + addrName
380 + " not detected.", addr.isMCGlobal());
382 // a currently invalid address as the prefix FFxE
383 // is only valid for x = {1,0} as the rest are reserved
384 addrName = "FFFE:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF";
385 addr = Inet6Address.getByName(addrName);
386 assertTrue("IPv6 global multicast address " + addrName
387 + " not detected.", addr.isMCGlobal());
389 // a sample MC organizational address
390 addrName = "FF08:42:42:42:42:42:42:42";
391 addr = Inet6Address.getByName(addrName);
392 assertTrue("IPv6 mulitcast organizational " + addrName
393 + " incorrectly indicated as a global address.", !addr
396 // a sample MC site address
397 addrName = "FF05:42:42:42:42:42:42:42";
398 addr = Inet6Address.getByName(addrName);
399 assertTrue("IPv6 mulitcast site address " + addrName
400 + " incorrectly indicated as a global address.", !addr
403 // a sample MC link address
404 addrName = "FF02:42:42:42:42:42:42:42";
405 addr = Inet6Address.getByName(addrName);
406 assertTrue("IPv6 mulitcast link address " + addrName
407 + " incorrectly indicated as a global address.", !addr
411 addrName = "FF01:42:42:42:42:42:42:42";
412 addr = Inet6Address.getByName(addrName);
413 assertTrue("IPv6 mulitcast node address " + addrName
414 + " incorrectly indicated as a global address.", !addr
417 // IPv4-mapped IPv6 address tests
418 addrName = "::FFFF:224.0.1.0";
419 addr = Inet6Address.getByName(addrName);
420 assertTrue("IPv4 global multicast address " + addrName
421 + " not identified as a global multicast address.", addr
424 addrName = "::FFFF:238.255.255.255";
425 addr = Inet6Address.getByName(addrName);
426 assertTrue("IPv4 global multicast address " + addrName
427 + " not identified as a global multicast address.", addr
430 } catch (Exception e) {
431 fail("Unknown address : " + addrName);
436 * @tests java.net.Inet6Address#isMCNodeLocal()
439 level = TestLevel.COMPLETE,
441 method = "isMCNodeLocal",
444 public void test_isMCNodeLocal() {
445 String addrName = "";
447 // IP V6 regular address tests for Mulitcase node local addresses
449 // Multicast node local addresses are FFx1:/112 where x is
450 // a set of flags, and the addition 112 bits make up
451 // the global address space
453 addrName = "FF01::0";
454 InetAddress addr = Inet6Address.getByName(addrName);
455 assertTrue("IPv6 node-local mutlicast address " + addrName
456 + " not detected.", addr.isMCNodeLocal());
458 addrName = "FF01:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF";
459 addr = Inet6Address.getByName(addrName);
460 assertTrue("IPv6 node-local multicast address " + addrName
461 + " not detected.", addr.isMCNodeLocal());
463 // a currently invalid address as the prefix FFxE
464 // is only valid for x = {1,0} as the rest are reserved
465 addrName = "FFF1::0";
466 addr = Inet6Address.getByName(addrName);
467 assertTrue("IPv6 node-local mutlicast address " + addrName
468 + " not detected.", addr.isMCNodeLocal());
470 // a currently invalid address as the prefix FFxE
471 // is only valid for x = {1,0} as the rest are reserved
472 addrName = "FFF1:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF";
473 addr = Inet6Address.getByName(addrName);
474 assertTrue("IPv6 node-local multicast address " + addrName
475 + " not detected.", addr.isMCNodeLocal());
477 // a sample MC organizational address
478 addrName = "FF08:42:42:42:42:42:42:42";
479 addr = Inet6Address.getByName(addrName);
480 assertTrue("IPv6 mulitcast organizational address " + addrName
481 + " incorrectly indicated as a node-local address.", !addr
484 // a sample MC site address
485 addrName = "FF05:42:42:42:42:42:42:42";
486 addr = Inet6Address.getByName(addrName);
487 assertTrue("IPv6 mulitcast site address " + addrName
488 + " incorrectly indicated as a node-local address.", !addr
491 // a sample MC link address
492 addrName = "FF02:42:42:42:42:42:42:42";
493 addr = Inet6Address.getByName(addrName);
494 assertTrue("IPv6 mulitcast link address " + addrName
495 + " incorrectly indicated as a node-local address.", !addr
498 // a sample MC global address
499 addrName = "FF0E:42:42:42:42:42:42:42";
500 addr = Inet6Address.getByName(addrName);
501 assertTrue("IPv6 mulitcast node address " + addrName
502 + " incorrectly indicated as a node-local address.", !addr
505 } catch (Exception e) {
506 fail("Unknown address : " + addrName);
511 * @tests java.net.Inet6Address#isMCLinkLocal()
514 level = TestLevel.COMPLETE,
516 method = "isMCLinkLocal",
519 public void test_isMCLinkLocal() {
520 String addrName = "";
522 // IP V6 regular address tests for Mulitcase link local addresses
524 // Multicast link local addresses are FFx2:/112 where x is
525 // a set of flags, and the addition 112 bits make up
526 // the global address space
528 addrName = "FF02::0";
529 InetAddress addr = Inet6Address.getByName(addrName);
530 assertTrue("IPv6 link local multicast address " + addrName
531 + " not detected.", addr.isMCLinkLocal());
533 addrName = "FF02:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF";
534 addr = Inet6Address.getByName(addrName);
535 assertTrue("IPv6 link local multicast address " + addrName
536 + " not detected.", addr.isMCLinkLocal());
538 // a currently invalid address as the prefix FFxE
539 // is only valid for x = {1,0} as the rest are reserved
540 addrName = "FFF2::0";
541 addr = Inet6Address.getByName(addrName);
542 assertTrue("IPv6 link local multicast address " + addrName
543 + " not detected.", addr.isMCLinkLocal());
545 // a currently invalid address as the prefix FFxE
546 // is only valid for x = {1,0} as the rest are reserved
547 addrName = "FFF2:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF";
548 addr = Inet6Address.getByName(addrName);
549 assertTrue("IPv6 link local multicast address " + addrName
550 + " not detected.", addr.isMCLinkLocal());
552 // a sample MC organizational address
553 addrName = "FF08:42:42:42:42:42:42:42";
554 addr = Inet6Address.getByName(addrName);
556 "IPv6 organization multicast address "
558 + " incorrectly indicated as a link-local mulitcast address.",
559 !addr.isMCLinkLocal());
561 // a sample MC site address
562 addrName = "FF05:42:42:42:42:42:42:42";
563 addr = Inet6Address.getByName(addrName);
565 "IPv6 site-local mulitcast address "
567 + " incorrectly indicated as a link-local mulitcast address.",
568 !addr.isMCLinkLocal());
570 // a sample MC global address
571 addrName = "FF0E:42:42:42:42:42:42:42";
572 addr = Inet6Address.getByName(addrName);
574 "IPv6 global multicast address "
576 + " incorrectly indicated as a link-local mulitcast address.",
577 !addr.isMCLinkLocal());
580 addrName = "FF01:42:42:42:42:42:42:42";
581 addr = Inet6Address.getByName(addrName);
583 "IPv6 mulitcast node address "
585 + " incorrectly indicated as a link-local mulitcast address.",
586 !addr.isMCLinkLocal());
588 // Ipv4-mapped IPv6 addresses
590 addrName = "::FFFF:224.0.0.0"; // a multicast addr 1110
591 addr = Inet6Address.getByName(addrName);
592 assertTrue("IPv4 link-local multicast address " + addrName
593 + " not identified as a link-local multicast address.",
594 addr.isMCLinkLocal());
596 addrName = "::FFFF:224.0.0.255"; // a multicast addr 1110
597 addr = Inet6Address.getByName(addrName);
598 assertTrue("IPv4 link-local multicast address " + addrName
599 + " not identified as a link-local multicast address.",
600 addr.isMCLinkLocal());
602 } catch (Exception e) {
603 fail("Unknown address : " + addrName);
608 * @tests java.net.Inet6Address#isMCSiteLocal()
611 level = TestLevel.COMPLETE,
613 method = "isMCSiteLocal",
616 public void test_isMCSiteLocal() {
617 String addrName = "";
619 // IP V6 regular address tests for Multicast site-local addresses
621 // Multicast global addresses are FFx5:/112 where x is
622 // a set of flags, and the addition 112 bits make up
623 // the global address space
625 addrName = "FF05::0";
626 InetAddress addr = Inet6Address.getByName(addrName);
627 assertTrue("IPv6 site-local mutlicast address " + addrName
628 + " not detected.", addr.isMCSiteLocal());
630 addrName = "FF05:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF";
631 addr = Inet6Address.getByName(addrName);
632 assertTrue("IPv6 site-local multicast address " + addrName
633 + " not detected.", addr.isMCSiteLocal());
635 // a currently invalid address as the prefix FFxE
636 // is only valid for x = {1,0} as the rest are reserved
637 addrName = "FFF5::0";
638 addr = Inet6Address.getByName(addrName);
639 assertTrue("IPv6 site-local mutlicast address " + addrName
640 + " not detected.", addr.isMCSiteLocal());
642 // a currently invalid address as the prefix FFxE
643 // is only valid for x = {1,0} as the rest are reserved
644 addrName = "FFF5:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF";
645 addr = Inet6Address.getByName(addrName);
646 assertTrue("IPv6 site-local multicast address " + addrName
647 + " not detected.", addr.isMCSiteLocal());
649 // a sample MC organizational address
650 addrName = "FF08:42:42:42:42:42:42:42";
651 addr = Inet6Address.getByName(addrName);
653 "IPv6 organization multicast address "
655 + " incorrectly indicated as a site-local mulitcast address.",
656 !addr.isMCSiteLocal());
658 // a sample MC global address
659 addrName = "FF0E:42:42:42:42:42:42:42";
660 addr = Inet6Address.getByName(addrName);
662 "IPv6 global mulitcast address "
664 + " incorrectly indicated as a site-local mulitcast address.",
665 !addr.isMCSiteLocal());
667 // a sample MC link address
668 addrName = "FF02:42:42:42:42:42:42:42";
669 addr = Inet6Address.getByName(addrName);
671 "IPv6 link-local multicast address "
673 + " incorrectly indicated as a site-local mulitcast address.",
674 !addr.isMCSiteLocal());
677 addrName = "FF01:42:42:42:42:42:42:42";
678 addr = Inet6Address.getByName(addrName);
680 "IPv6 mulitcast node address "
682 + " incorrectly indicated as a site-local mulitcast address.",
683 !addr.isMCSiteLocal());
685 // IPv4-mapped IPv6 addresses
686 addrName = "::FFFF:239.255.0.0";
687 addr = Inet6Address.getByName(addrName);
688 assertTrue("IPv4 site-local multicast address " + addrName
689 + " not identified as a site-local multicast address.",
690 addr.isMCSiteLocal());
692 addrName = "::FFFF:239.255.255.255";
693 addr = Inet6Address.getByName(addrName);
694 assertTrue("IPv4 site-local multicast address " + addrName
695 + " not identified as a site-local multicast address.",
696 addr.isMCSiteLocal());
698 } catch (Exception e) {
699 fail("Unknown address : " + addrName);
704 * @tests java.net.Inet6Address#isMCOrgLocal()
707 level = TestLevel.COMPLETE,
709 method = "isMCOrgLocal",
712 public void test_isMCOrgLocal() {
713 String addrName = "";
715 // IP V6 regular address tests for Mulitcase organization-local
718 // Multicast global addresses are FFxE:/112 where x is
719 // a set of flags, and the addition 112 bits make up
720 // the global address space
722 addrName = "FF08::0";
723 InetAddress addr = Inet6Address.getByName(addrName);
724 assertTrue("IPv6 organization-local mutlicast address " + addrName
725 + " not detected.", addr.isMCOrgLocal());
727 addrName = "FF08:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF";
728 addr = Inet6Address.getByName(addrName);
729 assertTrue("IPv6 organization-local multicast address " + addrName
730 + " not detected.", addr.isMCOrgLocal());
732 // a currently invalid address as the prefix FFxE
733 // is only valid for x = {1,0} as the rest are reserved
734 addrName = "FFF8::0";
735 addr = Inet6Address.getByName(addrName);
736 assertTrue("IPv6 organization-local mutlicast address " + addrName
737 + " not detected.", addr.isMCOrgLocal());
739 // a currently invalid address as the prefix FFxE
740 // is only valid for x = {1,0} as the rest are reserved
741 addrName = "FFF8:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF";
742 addr = Inet6Address.getByName(addrName);
743 assertTrue("IPv6 organization-local multicast address " + addrName
744 + " not detected.", addr.isMCOrgLocal());
746 // a sample MC global address
747 addrName = "FF0E:42:42:42:42:42:42:42";
748 addr = Inet6Address.getByName(addrName);
750 "IPv6 global multicast address "
752 + " incorrectly indicated as an organization-local mulitcast address.",
753 !addr.isMCOrgLocal());
755 // a sample MC site address
756 addrName = "FF05:42:42:42:42:42:42:42";
757 addr = Inet6Address.getByName(addrName);
759 "IPv6 site-local mulitcast address "
761 + " incorrectly indicated as an organization-local mulitcast address.",
762 !addr.isMCOrgLocal());
764 // a sample MC link address
765 addrName = "FF02:42:42:42:42:42:42:42";
766 addr = Inet6Address.getByName(addrName);
768 "IPv6 link-local multicast address "
770 + " incorrectly indicated as an organization-local mulitcast address.",
771 !addr.isMCOrgLocal());
774 addrName = "FF01:42:42:42:42:42:42:42";
775 addr = Inet6Address.getByName(addrName);
777 "IPv6 mulitcast node address "
779 + " incorrectly indicated as an organization-local mulitcast address.",
780 !addr.isMCOrgLocal());
782 // IPv4-mapped IPv6 addresses
784 addrName = "::FFFF:239.192.0.0";
785 addr = Inet6Address.getByName(addrName);
786 assertTrue("IPv4 org-local multicast address " + addrName
787 + " not identified as a org-local multicast address.", addr
790 addrName = "::FFFF:239.195.255.255";
791 addr = Inet6Address.getByName(addrName);
792 assertTrue("IPv4 org-local multicast address " + addrName
793 + " not identified as a org-local multicast address.", addr
796 } catch (Exception e) {
797 fail("Unknown address : " + addrName);
802 * @tests java.net.Inet6Address#isIPv4CompatibleAddress()
805 level = TestLevel.COMPLETE,
807 method = "isIPv4CompatibleAddress",
810 public void test_isIPv4CompatibleAddress() {
811 String addrName = "";
812 Inet6Address addr = null;
816 // Tests a number of addresses to see if they are compatable with
819 addrName = "FFFF::42:42"; // 11111111 = FFFF
820 addr = (Inet6Address) InetAddress.getByName(addrName);
821 assertTrue("A non-compatable IPv6 address " + addrName
822 + " incorrectly identified as a IPv4 compatable address.",
823 !addr.isIPv4CompatibleAddress());
825 // IPv4-compatible IPv6 address tests
827 // Now create 2 IP v6 addresses that are IP v4 compatable
828 // to IP v6 addresses.
830 addrName = "::0.0.0.0";
831 addr = (Inet6Address) InetAddress.getByName(addrName);
832 assertTrue("IPv4 compatable address " + addrName
833 + " not detected correctly.", addr
834 .isIPv4CompatibleAddress());
836 addrName = "::255.255.255.255"; // an ipv4 non-multicast address
837 addr = (Inet6Address) InetAddress.getByName(addrName);
838 assertTrue("IPv4 compatable address " + addrName
839 + " not detected correctly.", addr
840 .isIPv4CompatibleAddress());
842 } catch (Exception e) {
844 fail("Unknown address : " + addrName);
849 * @tests java.net.Inet6Address#getByName(java.lang.String)
852 level = TestLevel.PARTIAL_COMPLETE,
854 method = "getByName",
855 args = {java.lang.String.class}
857 public void test_getByNameLjava_lang_String() throws Exception {
858 // ones to add "::255.255.255.255", "::FFFF:0.0.0.0",
859 // "0.0.0.0.0.0::255.255.255.255", "F:F:F:F:F:F:F:F",
860 // "[F:F:F:F:F:F:F:F]"
861 String validIPAddresses[] = { "::1.2.3.4", "::", "::", "1::0", "1::",
862 "::1", "0", /* jdk1.5 accepts 0 as valid */
863 "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF",
864 "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:255.255.255.255",
865 "0:0:0:0:0:0:0:0", "0:0:0:0:0:0:0.0.0.0" };
867 String invalidIPAddresses[] = { "FFFF:FFFF" };
869 for (int i = 0; i < validIPAddresses.length; i++) {
871 InetAddress.getByName(validIPAddresses[i]);
873 //exercise positive cache
874 InetAddress.getByName(validIPAddresses[i]);
876 if (!validIPAddresses[i].equals("0")) {
877 String tempIPAddress = "[" + validIPAddresses[i] + "]";
878 InetAddress.getByName(tempIPAddress);
882 for (int i = 0; i < invalidIPAddresses.length; i++) {
884 InetAddress.getByName(invalidIPAddresses[i]);
885 fail("Invalid IP address incorrectly recognized as valid: "
886 + invalidIPAddresses[i]);
887 } catch (Exception e) {
890 //exercise negative cache
892 InetAddress.getByName(invalidIPAddresses[i]);
893 fail("Invalid IP address incorrectly recognized as valid: "
894 + invalidIPAddresses[i]);
895 } catch (Exception e) {
901 * @tests java.net.Inet6Address#getByAddress(String, byte[], int)
904 level = TestLevel.COMPLETE,
906 method = "getByAddress",
907 args = {java.lang.String.class, byte[].class, int.class}
909 public void test_getByAddressLString$BI() throws UnknownHostException{
911 Inet6Address.getByAddress("123", null, 0);
912 fail("should throw UnknownHostException");
913 } catch (UnknownHostException uhe) {
916 byte[] addr1 = { (byte) 127, 0, 0, 1 };
918 Inet6Address.getByAddress("123", addr1, 0);
919 fail("should throw UnknownHostException");
920 } catch (UnknownHostException uhe) {
924 byte[] addr2 = { (byte) 0xFE, (byte) 0x80, 0, 0, 0, 0, 0, 0, 0x02,
925 0x11, 0x25, (byte) 0xFF, (byte) 0xFE, (byte) 0xF8, (byte) 0x7C,
928 // should not throw any exception
929 Inet6Address.getByAddress("123", addr2, 3);
930 Inet6Address.getByAddress("123", addr2, 0);
931 Inet6Address.getByAddress("123", addr2, -1);
935 * @tests java.net.Inet6Address#getByAddress(String, byte[],
939 level = TestLevel.COMPLETE,
941 method = "getByAddress",
942 args = {java.lang.String.class, byte[].class, java.net.NetworkInterface.class}
944 public void test_getByAddressLString$BLNetworkInterface()
945 throws UnknownHostException {
946 NetworkInterface nif = null;
948 Inet6Address.getByAddress("123", null, nif);
949 fail("should throw UnknownHostException");
950 } catch (UnknownHostException uhe) {
953 byte[] addr1 = { (byte) 127, 0, 0, 1 };
955 Inet6Address.getByAddress("123", addr1, nif);
956 fail("should throw UnknownHostException");
957 } catch (UnknownHostException uhe) {
960 byte[] addr2 = { (byte) 0xFE, (byte) 0x80, 0, 0, 0, 0, 0, 0, 0x02,
961 0x11, 0x25, (byte) 0xFF, (byte) 0xFE, (byte) 0xF8, (byte)
964 // should not throw any exception
965 Inet6Address.getByAddress("123", addr2, nif);
969 * @throws UnknownHostException
970 * @tests java.net.Inet6Address#getScopeID()
973 level = TestLevel.COMPLETE,
975 method = "getScopeId",
978 public void test_getScopeID() throws UnknownHostException {
980 byte[] addr = { (byte) 0xFE, (byte) 0x80, 0, 0, 0, 0, 0, 0, 0x02, 0x11,
981 0x25, (byte) 0xFF, (byte) 0xFE, (byte) 0xF8, (byte) 0x7C,
984 v6ia = Inet6Address.getByAddress("123", addr, 3);
985 assertEquals(3, v6ia.getScopeId());
987 v6ia = Inet6Address.getByAddress("123", addr, 0);
988 assertEquals(0, v6ia.getScopeId());
990 v6ia = Inet6Address.getByAddress("123", addr, -1);
991 assertEquals(0, v6ia.getScopeId());
995 * @tests java.net.Inet6Address#getScopedInterface()
998 level = TestLevel.COMPLETE,
1000 method = "getScopedInterface",
1003 public void test_getScopedInterface() throws UnknownHostException {
1004 byte[] addr = { (byte) 0xFE, (byte) 0x80, (byte) 0x09, (byte) 0xb5,
1005 (byte) 0x6b, (byte) 0xa4, 0, 0, 0, 0, 0, 0, (byte) 0x09,
1006 (byte) 0xb5, (byte) 0x6b, (byte) 0xa4 };
1007 Inet6Address v6Addr;
1008 v6Addr = Inet6Address.getByAddress("123", addr, null);
1009 assertNull(v6Addr.getScopedInterface());
1013 int bytesToInt(byte bytes[], int start) {
1016 int value = ((bytes[start + 3] & byteMask))
1017 | ((bytes[start + 2] & byteMask) << 8)
1018 | ((bytes[start + 1] & byteMask) << 16)
1019 | ((bytes[start] & byteMask) << 24);
1024 String byteArrayToHexString(byte bytes[], boolean leadingZeros) {
1026 String fullString = "";
1027 int times = bytes.length / 4;
1028 int intArray[] = new int[times];
1029 for (int i = 0; i < times; i++) {
1030 intArray[i] = bytesToInt(bytes, i * 4);
1033 return intArrayToHexString(intArray, leadingZeros);
1036 void intToBytes(int value, byte bytes[], int start) {
1039 bytes[start + 3] = (byte) (value & byteMask);
1040 bytes[start + 2] = (byte) ((value >> 8) & byteMask);
1041 bytes[start + 1] = (byte) ((value >> 16) & byteMask);
1042 bytes[start] = (byte) ((value >> 24) & byteMask);
1045 String intArrayToHexString(int ints[], boolean leadingZeros) {
1047 String fullString = "";
1049 int intsLength = ints.length;
1050 for (int i = 0; i < intsLength; i++) {
1051 tempString = Integer.toHexString(ints[i]);
1052 while (tempString.length() < 4 && leadingZeros) {
1053 tempString = "0" + tempString;
1055 if (i + 1 < intsLength) {
1058 fullString += tempString;
1061 return fullString.toUpperCase();
1064 // comparator for Inet6Address objects
1065 private static final SerializableAssert COMPARATOR = new SerializableAssert() {
1066 public void assertDeserialized(Serializable initial,
1067 Serializable deserialized) {
1069 Inet6Address initAddr = (Inet6Address) initial;
1070 Inet6Address desrAddr = (Inet6Address) deserialized;
1072 byte[] iaAddresss = initAddr.getAddress();
1073 byte[] deIAAddresss = desrAddr.getAddress();
1074 for (int i = 0; i < iaAddresss.length; i++) {
1075 assertEquals(iaAddresss[i], deIAAddresss[i]);
1077 assertEquals(initAddr.getScopeId(), desrAddr.getScopeId());
1078 assertEquals(initAddr.getScopedInterface(), desrAddr
1079 .getScopedInterface());
1084 * @tests serialization/deserialization compatibility.
1087 level = TestLevel.COMPLETE,
1088 notes = "Checks serialization",
1089 method = "!SerializationSelf",
1092 public void testSerializationSelf() throws Exception {
1094 byte[] localv6 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
1096 SerializationTest.verifySelf(InetAddress.getByAddress(localv6),
1101 * @tests serialization/deserialization compatibility with RI.
1104 level = TestLevel.COMPLETE,
1105 notes = "Checks serialization",
1106 method = "!SerializationGolden",
1109 public void testSerializationCompatibility() throws Exception {
1111 byte[] localv6 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
1113 Object[] addresses = { InetAddress.getByAddress(localv6),
1114 // Regression for Harmony-1039: ser-form has
1115 // null interface name
1116 InetAddress.getByAddress(localv6) };
1118 SerializationTest.verifyGolden(this, addresses, COMPARATOR);
1122 level = TestLevel.COMPLETE,
1125 args = {java.lang.Object.class}
1127 public void test_equals() throws Exception {
1128 InetAddress addr = Inet6Address.getByName("239.191.255.255");
1129 assertTrue(addr.equals(addr));
1130 InetAddress addr1 = Inet6Address.getByName("127.0.0.1");
1131 InetAddress addr2 = Inet6Address.getByName("localhost");
1132 assertTrue(addr1.equals(addr2));
1133 assertFalse(addr.equals(addr1));
1135 InetAddress addr3 = Inet6Address.getByName("127.0.0");
1136 assertFalse(addr1.equals(addr3));
1140 level = TestLevel.COMPLETE,
1142 method = "getHostAddress",
1145 public void test_getHostAddress() throws Exception {
1146 InetAddress aAddr = Inet6Address.getByName("localhost");
1147 assertEquals("127.0.0.1", aAddr.getHostAddress());
1149 aAddr = Inet6Address.getByName("127.0.0.1");
1150 assertEquals("127.0.0.1", aAddr.getHostAddress());
1152 aAddr = Inet6Address.getByName("224.0.0.0");
1153 assertEquals("224.0.0.0", aAddr.getHostAddress());
1155 aAddr = Inet6Address.getByName("1");
1156 assertEquals("0.0.0.1", aAddr.getHostAddress());
1158 aAddr = Inet6Address.getByName("1.1");
1159 assertEquals("1.0.0.1", aAddr.getHostAddress());
1161 aAddr = Inet6Address.getByName("1.1.1");
1162 assertEquals("1.1.0.1", aAddr.getHostAddress());
1164 byte[] bAddr = { (byte) 0xFE, (byte) 0x80, 0, 0, 0, 0, 0, 0, 0x02, 0x11,
1165 0x25, (byte) 0xFF, (byte) 0xFE, (byte) 0xF8, (byte) 0x7C,
1167 aAddr = Inet6Address.getByAddress(bAddr);
1168 String aString = aAddr.getHostAddress();
1169 assertTrue(aString.equals("fe80:0:0:0:211:25ff:fef8:7cb2") ||
1170 aString.equals("fe80::211:25ff:fef8:7cb2"));
1172 byte[] cAddr = { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
1173 (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
1174 (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
1175 (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF};
1176 aAddr = Inet6Address.getByAddress(cAddr);
1177 assertEquals("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", aAddr.getHostAddress());
1179 byte[] dAddr = { (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
1180 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
1181 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
1182 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00};
1183 aAddr = Inet6Address.getByAddress(dAddr);
1184 aString = aAddr.getHostAddress();
1185 assertTrue(aString.equals("0:0:0:0:0:0:0:0") ||
1186 aString.equals("::"));
1188 byte[] eAddr = { (byte) 0x00, (byte) 0x01, (byte) 0x02, (byte) 0x03,
1189 (byte) 0x04, (byte) 0x05, (byte) 0x06, (byte) 0x07,
1190 (byte) 0x08, (byte) 0x09, (byte) 0x0a, (byte) 0x0b,
1191 (byte) 0x0c, (byte) 0x0d, (byte) 0x0e, (byte) 0x0f};
1192 aAddr = Inet6Address.getByAddress(eAddr);
1193 assertEquals("1:203:405:607:809:a0b:c0d:e0f", aAddr.getHostAddress());
1195 byte[] fAddr = { (byte) 0x00, (byte) 0x10, (byte) 0x20, (byte) 0x30,
1196 (byte) 0x40, (byte) 0x50, (byte) 0x60, (byte) 0x70,
1197 (byte) 0x80, (byte) 0x90, (byte) 0xa0, (byte) 0xb0,
1198 (byte) 0xc0, (byte) 0xd0, (byte) 0xe0, (byte) 0xf0};
1199 aAddr = Inet6Address.getByAddress(fAddr);
1200 assertEquals("10:2030:4050:6070:8090:a0b0:c0d0:e0f0", aAddr.getHostAddress());
1204 level = TestLevel.COMPLETE,
1206 method = "hashCode",
1209 public void test_hashCode() throws Exception {
1210 InetAddress addr1 = Inet6Address.getByName("1.1");
1211 InetAddress addr2 = Inet6Address.getByName("1.1.1");
1212 assertFalse(addr1.hashCode() == addr2.hashCode());
1214 addr2 = Inet6Address.getByName("1.0.0.1");
1215 assertTrue(addr1.hashCode() == addr2.hashCode());
1217 addr1 = Inet6Address.getByName("127.0.0.1");
1218 addr2 = Inet6Address.getByName("localhost");
1219 assertTrue(addr1.hashCode() == addr2.hashCode());
1223 level = TestLevel.COMPLETE,
1225 method = "toString",
1228 public void test_toString() throws Exception {
1229 String validIPAddresses[] = { "::1.2.3.4", "::", "::", "1::0", "1::",
1230 "::1", "0", /* jdk1.5 accepts 0 as valid */
1231 "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF",
1232 "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:255.255.255.255",
1233 "0:0:0:0:0:0:0:0", "0:0:0:0:0:0:0.0.0.0" };
1235 String [] resultStrings = {"/0:0:0:0:0:0:102:304", "/0:0:0:0:0:0:0:0",
1236 "/0:0:0:0:0:0:0:0", "/1:0:0:0:0:0:0:0", "/1:0:0:0:0:0:0:0",
1238 "/0.0.0.0", "/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
1239 "/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", "/0:0:0:0:0:0:0:0",
1240 "/0:0:0:0:0:0:0:0"};
1242 for(int i = 0; i < validIPAddresses.length; i++) {
1243 InetAddress ia = Inet6Address.getByName(validIPAddresses[i]);
1244 String result = ia.toString();
1245 assertNotNull(result);
1246 //assertEquals("toString method returns incorrect value: " +
1247 // result, resultStrings[i], result);