1/*
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
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
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.
16 */
17
18package org.apache.harmony.tests.java.net;
19
20import java.io.InvalidObjectException;
21import java.io.Serializable;
22import java.net.Inet6Address;
23import java.net.InetAddress;
24import java.net.NetworkInterface;
25import java.net.UnknownHostException;
26import java.util.Locale;
27
28import org.apache.harmony.testframework.serialization.SerializationTest;
29import org.apache.harmony.testframework.serialization.SerializationTest.SerializableAssert;
30
31public class Inet6AddressTest extends junit.framework.TestCase {
32    public void test_isMulticastAddress() throws Exception {
33
34        String addrName = "";
35        InetAddress addr = null;
36
37        // IP V6 regular multicast and non-multicast tests
38        //
39        // Create 2 IP v6 addresses and call "isMulticastAddress()"
40        // A prefix of "11111111" means that the address is multicast
41        // The first one will be one with the prefix the second without
42
43        addrName = "FFFF::42:42"; // 11111111 = FFFF
44        addr = InetAddress.getByName(addrName);
45        assertTrue("Multicast address " + addrName + " not detected.", addr
46                .isMulticastAddress());
47
48        addrName = "42::42:42"; // an non-multicast address
49        addr = InetAddress.getByName(addrName);
50        assertTrue("Non multicast address " + addrName
51                + " reporting as a multicast address.", !addr
52                .isMulticastAddress());
53
54        // IPv4-compatible IPv6 address tests
55        //
56        // Now create 2 IP v6 addresses that are IP v4 compatable
57        // to IP v6 addresses. The address prefix for a multicast ip v4
58        // address is 1110 for the last 16 bits ::d.d.d.d
59        // We expect these to be false
60
61        addrName = "::224.42.42.42"; // an ipv4 multicast addr 1110 = 224
62        addr = InetAddress.getByName(addrName);
63        assertTrue("IPv4 compatable address " + addrName
64                + " reported incorrectly as multicast.", !addr
65                .isMulticastAddress());
66
67        addrName = "::42.42.42.42"; // an ipv4 non-multicast address
68        addr = InetAddress.getByName(addrName);
69        assertTrue("IPv4 compatable address " + addrName
70                + " reported incorrectly as multicast.", !addr
71                .isMulticastAddress());
72
73        // IPv4-mapped IPv6 address tests
74        //
75        // Now create 2 IP v6 addresses that are IP v4 compatable
76        // to IP v6 addresses. The address prefix for a multicast ip v4
77        // address is 1110 for the last 16 bits ::FFFF:d.d.d.d
78
79        addrName = "::FFFF:224.42.42.42"; // an ipv4 multicast addr 1110 =
80        // 224
81        addr = InetAddress.getByName(addrName);
82        assertTrue("IPv4-mapped IPv6 multicast address " + addrName
83                + " not detected.", addr.isMulticastAddress());
84
85        addrName = "::FFFF:42.42.42.42"; // an ipv4 non-multicast address
86        addr = InetAddress.getByName(addrName);
87        assertTrue("IPv4-mapped IPv6 non-multicast address " + addrName
88                + " reporting as a multicast address.", !addr
89                .isMulticastAddress());
90    }
91
92    public void test_isAnyLocalAddress() throws Exception {
93
94        String addrName = "";
95        InetAddress addr = null;
96
97        // test to ensure that the unspecified address returns tru
98        addrName = "::0"; // The unspecified address
99        addr = InetAddress.getByName(addrName);
100        assertTrue(
101                "The unspecified (also known as wildcard and any local address) "
102                        + addrName + " not detected.", addr
103                .isAnyLocalAddress());
104
105        addrName = "::"; // another form of the unspecified address
106        addr = InetAddress.getByName(addrName);
107        assertTrue(
108                "The unspecified (also known as wildcard and any local address) "
109                        + addrName + " not detected.", addr
110                .isAnyLocalAddress());
111
112        addrName = "::1"; // The loopback address
113        addr = InetAddress.getByName(addrName);
114        assertTrue("The addresses " + addrName
115                + " incorrectly reporting an the unspecified address.",
116                !addr.isAnyLocalAddress());
117    }
118
119    public void test_isLoopbackAddress() throws Exception {
120
121        String addrName = "";
122        // IP V6 regular address tests for loopback
123        // The loopback address for IPv6 is ::1
124
125        addrName = "::1";
126        InetAddress addr = InetAddress.getByName(addrName);
127        assertTrue("IPv6 loopback address " + addrName + " not detected.",
128                addr.isLoopbackAddress());
129
130        addrName = "::2";
131        addr = InetAddress.getByName(addrName);
132        assertTrue("IPv6 address incorrectly " + addrName
133                + " detected as a loopback address.", !addr
134                .isLoopbackAddress());
135
136        // a loopback address should be 127.d.d.d
137        addrName = "42:42::42:42";
138        addr = InetAddress.getByName(addrName);
139        assertTrue("IPv6 address incorrectly " + addrName
140                + " detected as a loopback address.", !addr
141                .isLoopbackAddress());
142
143        // IPv4-compatible IPv6 address tests
144        //
145        // Now create 2 IP v6 addresses that are IP v4 compatable
146        // to IP v6 addresses. The address prefix for a multicast ip v4
147        // address is 1110 for the last 16 bits ::d.d.d.d
148        // We expect these to be false, as they are not IPv4 addresses
149
150        // a loopback address should be 127.d.d.d
151        addrName = "::127.0.0.0";
152        addr = InetAddress.getByName(addrName);
153        assertTrue("IPv4-compatible IPv6 address " + addrName
154                + " detected incorrectly as a loopback.", !addr
155                .isLoopbackAddress());
156
157        addrName = "::127.42.42.42"; // a loopback address should be
158        // 127.d.d.d
159        addr = InetAddress.getByName(addrName);
160        assertTrue("IPv4-compatible IPv6 address " + addrName
161                + " detected incorrectly as a loopback.", !addr
162                .isLoopbackAddress());
163
164        // a loopback address should be 127.d.d.d
165        addrName = "::42.42.42.42";
166        addr = InetAddress.getByName(addrName);
167        assertTrue("IPv4-compatible IPv6 address " + addrName
168                + " detected incorrectly as a loopback.", !addr
169                .isLoopbackAddress());
170
171        // IPv4-mapped IPv6 address tests
172        //
173        // Now create 2 IP v6 addresses that are IP v4 compatable
174        // to IP v6 addresses. The address prefix for a multicast ip v4
175        // address is 1110 for the last 16 bits ::FFFF:d.d.d.d
176
177        // a loopback address should be 127.d.d.d
178        addrName = "::FFFF:127.0.0.0";
179        addr = InetAddress.getByName(addrName);
180        assertTrue("IPv4-compatible IPv6 loopback address " + addrName
181                + " not detected.", addr.isLoopbackAddress());
182
183        // a loopback address should be 127.d.d.d
184        addrName = "::FFFF:127.42.42.42";
185        addr = InetAddress.getByName(addrName);
186        assertTrue("IPv4-compatible IPv6 loopback address " + addrName
187                + " not detected.", addr.isLoopbackAddress());
188
189        // a loopback address should be 127.d.d.d
190        addrName = "::FFFF:42.42.42.42";
191        addr = InetAddress.getByName(addrName);
192        assertTrue("IPv4-compatible IPv6 address incorrectly " + addrName
193                + " detected as a loopback address.", !addr
194                .isLoopbackAddress());
195    }
196
197    public void test_isLinkLocalAddress() throws Exception {
198
199        String addrName = "";
200        // IP V6 regular address tests for link local addresses
201        //
202        // Link local addresses are FE80:: -
203        // FEBF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF
204
205        addrName = "FE80::0";
206        InetAddress addr = InetAddress.getByName(addrName);
207        assertTrue(
208                "IPv6 link local address " + addrName + " not detected.",
209                addr.isLinkLocalAddress());
210
211        addrName = "FEBF::FFFF:FFFF:FFFF:FFFF";
212        addr = InetAddress.getByName(addrName);
213        assertTrue(
214                "IPv6 link local address " + addrName + " not detected.",
215                addr.isLinkLocalAddress());
216
217        addrName = "FEC0::1";
218        addr = InetAddress.getByName(addrName);
219        assertTrue("IPv6 address " + addrName
220                + " detected incorrectly as a link local address.", !addr
221                .isLinkLocalAddress());
222
223        addrName = "FD80::1:FFFF:FFFF:FFFF:FFFF";
224        addr = InetAddress.getByName(addrName);
225        assertTrue("IPv6 address " + addrName
226                + " detected incorrectly as a link local address.", !addr
227                .isLinkLocalAddress());
228
229        addrName = "FE7F::FFFF:FFFF:FFFF:FFFF";
230        addr = InetAddress.getByName(addrName);
231        assertTrue("IPv6 address " + addrName
232                + " detected incorrectly as a link local address.", !addr
233                .isLinkLocalAddress());
234    }
235
236    public void test_isSiteLocalAddress() throws Exception {
237        String addrName = "";
238
239        // IP V6 regular address tests for link local addresses
240        //
241        // Link local addresses are FEC0::0 through to
242        // FEFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF
243
244        addrName = "FEC0::0";
245        InetAddress addr = InetAddress.getByName(addrName);
246        assertTrue(
247                "IPv6 site local address " + addrName + " not detected.",
248                addr.isSiteLocalAddress());
249
250        addrName = "FEFF::FFFF:FFFF:FFFF:FFFF:FFFF";
251        addr = InetAddress.getByName(addrName);
252        assertTrue(
253                "IPv6 site local address " + addrName + " not detected.",
254                addr.isSiteLocalAddress());
255
256        addrName = "FEBF::FFFF:FFFF:FFFF:FFFF:FFFF";
257        addr = InetAddress.getByName(addrName);
258        assertTrue("IPv6 address " + addrName
259                + " detected incorrectly as a site local address.", !addr
260                .isSiteLocalAddress());
261
262        addrName = "FFC0::0";
263        addr = InetAddress.getByName(addrName);
264        assertTrue("IPv6 address " + addrName
265                + " detected incorrectly as a site local address.", !addr
266                .isSiteLocalAddress());
267    }
268
269    public void test_isMCGlobal() throws Exception {
270        String addrName = "";
271        // IP V6 regular address tests for Mulitcase Global addresses
272        //
273        // Multicast global addresses are FFxE:/112 where x is
274        // a set of flags, and the addition 112 bits make up
275        // the global address space
276
277        addrName = "FF0E::0";
278        InetAddress addr = InetAddress.getByName(addrName);
279        assertTrue("IPv6 global mutlicast address " + addrName
280                + " not detected.", addr.isMCGlobal());
281
282        addrName = "FF0E:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF";
283        addr = InetAddress.getByName(addrName);
284        assertTrue("IPv6 global multicast address " + addrName
285                + " not detected.", addr.isMCGlobal());
286
287        // a currently invalid address as the prefix FFxE
288        // is only valid for x = {1,0} as the rest are reserved
289        addrName = "FFFE::0";
290        addr = InetAddress.getByName(addrName);
291        assertTrue("IPv6 global mutlicast address " + addrName
292                + " not detected.", addr.isMCGlobal());
293
294        // a currently invalid address as the prefix FFxE
295        // is only valid for x = {1,0} as the rest are reserved
296        addrName = "FFFE:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF";
297        addr = InetAddress.getByName(addrName);
298        assertTrue("IPv6 global multicast address " + addrName
299                + " not detected.", addr.isMCGlobal());
300
301        // a sample MC organizational address
302        addrName = "FF08:42:42:42:42:42:42:42";
303        addr = InetAddress.getByName(addrName);
304        assertTrue("IPv6 mulitcast organizational " + addrName
305                + " incorrectly indicated as a global address.", !addr
306                .isMCGlobal());
307
308        // a sample MC site address
309        addrName = "FF05:42:42:42:42:42:42:42";
310        addr = InetAddress.getByName(addrName);
311        assertTrue("IPv6 mulitcast site address " + addrName
312                + " incorrectly indicated as a global address.", !addr
313                .isMCGlobal());
314
315        // a sample MC link address
316        addrName = "FF02:42:42:42:42:42:42:42";
317        addr = InetAddress.getByName(addrName);
318        assertTrue("IPv6 mulitcast link address " + addrName
319                + " incorrectly indicated as a global address.", !addr
320                .isMCGlobal());
321
322        // a sample MC Node
323        addrName = "FF01:42:42:42:42:42:42:42";
324        addr = InetAddress.getByName(addrName);
325        assertTrue("IPv6 mulitcast node address " + addrName
326                + " incorrectly indicated as a global address.", !addr
327                .isMCGlobal());
328
329        // IPv4-mapped IPv6 address tests
330        addrName = "::FFFF:224.0.1.0";
331        addr = InetAddress.getByName(addrName);
332        assertTrue("IPv4 global multicast address " + addrName
333                + " not identified as a global multicast address.", addr
334                .isMCGlobal());
335
336        addrName = "::FFFF:238.255.255.255";
337        addr = InetAddress.getByName(addrName);
338        assertTrue("IPv4 global multicast address " + addrName
339                + " not identified as a global multicast address.", addr
340                .isMCGlobal());
341    }
342
343    public void test_isMCNodeLocal() throws Exception {
344        String addrName = "";
345        // IP V6 regular address tests for Mulitcase node local addresses
346        //
347        // Multicast node local addresses are FFx1:/112 where x is
348        // a set of flags, and the addition 112 bits make up
349        // the global address space
350
351        addrName = "FF01::0";
352        InetAddress addr = InetAddress.getByName(addrName);
353        assertTrue("IPv6 node-local mutlicast address " + addrName
354                + " not detected.", addr.isMCNodeLocal());
355
356        addrName = "FF01:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF";
357        addr = InetAddress.getByName(addrName);
358        assertTrue("IPv6 node-local multicast address " + addrName
359                + " not detected.", addr.isMCNodeLocal());
360
361        // a currently invalid address as the prefix FFxE
362        // is only valid for x = {1,0} as the rest are reserved
363        addrName = "FFF1::0";
364        addr = InetAddress.getByName(addrName);
365        assertTrue("IPv6 node-local mutlicast address " + addrName
366                + " not detected.", addr.isMCNodeLocal());
367
368        // a currently invalid address as the prefix FFxE
369        // is only valid for x = {1,0} as the rest are reserved
370        addrName = "FFF1:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF";
371        addr = InetAddress.getByName(addrName);
372        assertTrue("IPv6 node-local multicast address " + addrName
373                + " not detected.", addr.isMCNodeLocal());
374
375        // a sample MC organizational address
376        addrName = "FF08:42:42:42:42:42:42:42";
377        addr = InetAddress.getByName(addrName);
378        assertTrue("IPv6 mulitcast organizational address " + addrName
379                + " incorrectly indicated as a node-local address.", !addr
380                .isMCNodeLocal());
381
382        // a sample MC site address
383        addrName = "FF05:42:42:42:42:42:42:42";
384        addr = InetAddress.getByName(addrName);
385        assertTrue("IPv6 mulitcast site address " + addrName
386                + " incorrectly indicated as a node-local address.", !addr
387                .isMCNodeLocal());
388
389        // a sample MC link address
390        addrName = "FF02:42:42:42:42:42:42:42";
391        addr = InetAddress.getByName(addrName);
392        assertTrue("IPv6 mulitcast link address " + addrName
393                + " incorrectly indicated as a node-local address.", !addr
394                .isMCNodeLocal());
395
396        // a sample MC global address
397        addrName = "FF0E:42:42:42:42:42:42:42";
398        addr = InetAddress.getByName(addrName);
399        assertTrue("IPv6 mulitcast node address " + addrName
400                + " incorrectly indicated as a node-local address.", !addr
401                .isMCNodeLocal());
402    }
403
404    public void test_isMCLinkLocal() throws Exception {
405        String addrName = "";
406        // IP V6 regular address tests for Mulitcase link local addresses
407        //
408        // Multicast link local addresses are FFx2:/112 where x is
409        // a set of flags, and the addition 112 bits make up
410        // the global address space
411
412        addrName = "FF02::0";
413        InetAddress addr = InetAddress.getByName(addrName);
414        assertTrue("IPv6 link local multicast address " + addrName
415                + " not detected.", addr.isMCLinkLocal());
416
417        addrName = "FF02:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF";
418        addr = InetAddress.getByName(addrName);
419        assertTrue("IPv6 link local multicast address " + addrName
420                + " not detected.", addr.isMCLinkLocal());
421
422        // a currently invalid address as the prefix FFxE
423        // is only valid for x = {1,0} as the rest are reserved
424        addrName = "FFF2::0";
425        addr = InetAddress.getByName(addrName);
426        assertTrue("IPv6 link local multicast address " + addrName
427                + " not detected.", addr.isMCLinkLocal());
428
429        // a currently invalid address as the prefix FFxE
430        // is only valid for x = {1,0} as the rest are reserved
431        addrName = "FFF2:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF";
432        addr = InetAddress.getByName(addrName);
433        assertTrue("IPv6 link local multicast address " + addrName
434                + " not detected.", addr.isMCLinkLocal());
435
436        // a sample MC organizational address
437        addrName = "FF08:42:42:42:42:42:42:42";
438        addr = InetAddress.getByName(addrName);
439        assertTrue(
440                "IPv6 organization multicast address "
441                        + addrName
442                        + " incorrectly indicated as a link-local mulitcast address.",
443                !addr.isMCLinkLocal());
444
445        // a sample MC site address
446        addrName = "FF05:42:42:42:42:42:42:42";
447        addr = InetAddress.getByName(addrName);
448        assertTrue(
449                "IPv6 site-local mulitcast address "
450                        + addrName
451                        + " incorrectly indicated as a link-local mulitcast address.",
452                !addr.isMCLinkLocal());
453
454        // a sample MC global address
455        addrName = "FF0E:42:42:42:42:42:42:42";
456        addr = InetAddress.getByName(addrName);
457        assertTrue(
458                "IPv6 global multicast address "
459                        + addrName
460                        + " incorrectly indicated as a link-local mulitcast address.",
461                !addr.isMCLinkLocal());
462
463        // a sample MC Node
464        addrName = "FF01:42:42:42:42:42:42:42";
465        addr = InetAddress.getByName(addrName);
466        assertTrue(
467                "IPv6 mulitcast node address "
468                        + addrName
469                        + " incorrectly indicated as a link-local mulitcast address.",
470                !addr.isMCLinkLocal());
471
472        // Ipv4-mapped IPv6 addresses
473
474        addrName = "::FFFF:224.0.0.0"; // a multicast addr 1110
475        addr = InetAddress.getByName(addrName);
476        assertTrue("IPv4 link-local multicast address " + addrName
477                + " not identified as a link-local multicast address.",
478                addr.isMCLinkLocal());
479
480        addrName = "::FFFF:224.0.0.255"; // a multicast addr 1110
481        addr = InetAddress.getByName(addrName);
482        assertTrue("IPv4 link-local multicast address " + addrName
483                + " not identified as a link-local multicast address.",
484                addr.isMCLinkLocal());
485    }
486
487    public void test_isMCSiteLocal() throws Exception {
488        String addrName = "";
489        // IP V6 regular address tests for Multicast site-local addresses
490        //
491        // Multicast global addresses are FFx5:/112 where x is
492        // a set of flags, and the addition 112 bits make up
493        // the global address space
494
495        addrName = "FF05::0";
496        InetAddress addr = InetAddress.getByName(addrName);
497        assertTrue("IPv6 site-local mutlicast address " + addrName
498                + " not detected.", addr.isMCSiteLocal());
499
500        addrName = "FF05:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF";
501        addr = InetAddress.getByName(addrName);
502        assertTrue("IPv6 site-local multicast address " + addrName
503                + " not detected.", addr.isMCSiteLocal());
504
505        // a currently invalid address as the prefix FFxE
506        // is only valid for x = {1,0} as the rest are reserved
507        addrName = "FFF5::0";
508        addr = InetAddress.getByName(addrName);
509        assertTrue("IPv6 site-local mutlicast address " + addrName
510                + " not detected.", addr.isMCSiteLocal());
511
512        // a currently invalid address as the prefix FFxE
513        // is only valid for x = {1,0} as the rest are reserved
514        addrName = "FFF5:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF";
515        addr = InetAddress.getByName(addrName);
516        assertTrue("IPv6 site-local multicast address " + addrName
517                + " not detected.", addr.isMCSiteLocal());
518
519        // a sample MC organizational address
520        addrName = "FF08:42:42:42:42:42:42:42";
521        addr = InetAddress.getByName(addrName);
522        assertTrue(
523                "IPv6 organization multicast address "
524                        + addrName
525                        + " incorrectly indicated as a site-local mulitcast address.",
526                !addr.isMCSiteLocal());
527
528        // a sample MC global address
529        addrName = "FF0E:42:42:42:42:42:42:42";
530        addr = InetAddress.getByName(addrName);
531        assertTrue(
532                "IPv6 global mulitcast address "
533                        + addrName
534                        + " incorrectly indicated as a site-local mulitcast address.",
535                !addr.isMCSiteLocal());
536
537        // a sample MC link address
538        addrName = "FF02:42:42:42:42:42:42:42";
539        addr = InetAddress.getByName(addrName);
540        assertTrue(
541                "IPv6 link-local multicast address "
542                        + addrName
543                        + " incorrectly indicated as a site-local mulitcast address.",
544                !addr.isMCSiteLocal());
545
546        // a sample MC Node
547        addrName = "FF01:42:42:42:42:42:42:42";
548        addr = InetAddress.getByName(addrName);
549        assertTrue(
550                "IPv6 mulitcast node address "
551                        + addrName
552                        + " incorrectly indicated as a site-local mulitcast address.",
553                !addr.isMCSiteLocal());
554
555        // IPv4-mapped IPv6 addresses
556        addrName = "::FFFF:239.255.0.0";
557        addr = InetAddress.getByName(addrName);
558        assertTrue("IPv4 site-local multicast address " + addrName
559                + " not identified as a site-local multicast address.",
560                addr.isMCSiteLocal());
561
562        addrName = "::FFFF:239.255.255.255";
563        addr = InetAddress.getByName(addrName);
564        assertTrue("IPv4 site-local multicast address " + addrName
565                + " not identified as a site-local multicast address.",
566                addr.isMCSiteLocal());
567    }
568
569    public void test_isMCOrgLocal() throws Exception {
570        String addrName = "";
571        // IP V6 regular address tests for Mulitcase organization-local
572        // addresses
573        //
574        // Multicast global addresses are FFxE:/112 where x is
575        // a set of flags, and the addition 112 bits make up
576        // the global address space
577
578        addrName = "FF08::0";
579        InetAddress addr = InetAddress.getByName(addrName);
580        assertTrue("IPv6 organization-local mutlicast address " + addrName
581                + " not detected.", addr.isMCOrgLocal());
582
583        addrName = "FF08:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF";
584        addr = InetAddress.getByName(addrName);
585        assertTrue("IPv6 organization-local multicast address " + addrName
586                + " not detected.", addr.isMCOrgLocal());
587
588        // a currently invalid address as the prefix FFxE
589        // is only valid for x = {1,0} as the rest are reserved
590        addrName = "FFF8::0";
591        addr = InetAddress.getByName(addrName);
592        assertTrue("IPv6 organization-local mutlicast address " + addrName
593                + " not detected.", addr.isMCOrgLocal());
594
595        // a currently invalid address as the prefix FFxE
596        // is only valid for x = {1,0} as the rest are reserved
597        addrName = "FFF8:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF";
598        addr = InetAddress.getByName(addrName);
599        assertTrue("IPv6 organization-local multicast address " + addrName
600                + " not detected.", addr.isMCOrgLocal());
601
602        // a sample MC global address
603        addrName = "FF0E:42:42:42:42:42:42:42";
604        addr = InetAddress.getByName(addrName);
605        assertTrue(
606                "IPv6 global multicast address "
607                        + addrName
608                        + " incorrectly indicated as an organization-local mulitcast address.",
609                !addr.isMCOrgLocal());
610
611        // a sample MC site address
612        addrName = "FF05:42:42:42:42:42:42:42";
613        addr = InetAddress.getByName(addrName);
614        assertTrue(
615                "IPv6 site-local mulitcast address "
616                        + addrName
617                        + " incorrectly indicated as an organization-local mulitcast address.",
618                !addr.isMCOrgLocal());
619
620        // a sample MC link address
621        addrName = "FF02:42:42:42:42:42:42:42";
622        addr = InetAddress.getByName(addrName);
623        assertTrue(
624                "IPv6 link-local multicast address "
625                        + addrName
626                        + " incorrectly indicated as an organization-local mulitcast address.",
627                !addr.isMCOrgLocal());
628
629        // a sample MC Node
630        addrName = "FF01:42:42:42:42:42:42:42";
631        addr = InetAddress.getByName(addrName);
632        assertTrue(
633                "IPv6 mulitcast node address "
634                        + addrName
635                        + " incorrectly indicated as an organization-local mulitcast address.",
636                !addr.isMCOrgLocal());
637
638        // IPv4-mapped IPv6 addresses
639
640        addrName = "::FFFF:239.192.0.0";
641        addr = InetAddress.getByName(addrName);
642        assertTrue("IPv4 org-local multicast address " + addrName
643                + " not identified as a org-local multicast address.", addr
644                .isMCOrgLocal());
645
646        addrName = "::FFFF:239.195.255.255";
647        addr = InetAddress.getByName(addrName);
648        assertTrue("IPv4 org-local multicast address " + addrName
649                + " not identified as a org-local multicast address.", addr
650                .isMCOrgLocal());
651    }
652
653    public void test_isIPv4CompatibleAddress() throws Exception {
654        String addrName = "";
655        Inet6Address addr = null;
656
657        // Tests a number of addresses to see if they are compatable with
658        // IPv6 addresses
659
660        addrName = "FFFF::42:42"; // 11111111 = FFFF
661        addr = (Inet6Address) InetAddress.getByName(addrName);
662        assertTrue("A non-compatable IPv6 address " + addrName
663                + " incorrectly identified as a IPv4 compatable address.",
664                !addr.isIPv4CompatibleAddress());
665
666        // IPv4-compatible IPv6 address tests
667        //
668        // Now create 2 IP v6 addresses that are IP v4 compatable
669        // to IP v6 addresses.
670
671        addrName = "::0.0.0.0";
672        addr = (Inet6Address) InetAddress.getByName(addrName);
673        assertTrue("IPv4 compatable address " + addrName
674                + " not detected correctly.", addr
675                .isIPv4CompatibleAddress());
676
677        addrName = "::255.255.255.255"; // an ipv4 non-multicast address
678        addr = (Inet6Address) InetAddress.getByName(addrName);
679        assertTrue("IPv4 compatable address " + addrName
680                + " not detected correctly.", addr
681                .isIPv4CompatibleAddress());
682    }
683
684    public void test_getByAddressLString$BI() throws UnknownHostException {
685        try {
686            Inet6Address.getByAddress("123", null, 0);
687            fail("should throw UnknownHostException");
688        } catch (UnknownHostException uhe) {
689            // expected
690        }
691        byte[] addr1 = { (byte) 127, 0, 0, 1 };
692        try {
693            Inet6Address.getByAddress("123", addr1, 0);
694            fail("should throw UnknownHostException");
695        } catch (UnknownHostException uhe) {
696            // expected
697        }
698
699        byte[] addr2 = { (byte) 0xFE, (byte) 0x80, 0, 0, 0, 0, 0, 0, 0x02,
700                0x11, 0x25, (byte) 0xFF, (byte) 0xFE, (byte) 0xF8, (byte) 0x7C,
701                (byte) 0xB2 };
702
703        // should not throw any exception
704        Inet6Address.getByAddress("123", addr2, 3);
705        Inet6Address.getByAddress("123", addr2, 0);
706        Inet6Address.getByAddress("123", addr2, -1);
707    }
708
709    public void test_getByAddressLString$BLNetworkInterface()
710            throws UnknownHostException {
711        NetworkInterface nif = null;
712        try {
713            Inet6Address.getByAddress("123", null, nif);
714            fail("should throw UnknownHostException");
715        } catch (UnknownHostException uhe) {
716            // expected
717        }
718        byte[] addr1 = { (byte) 127, 0, 0, 1 };
719        try {
720            Inet6Address.getByAddress("123", addr1, nif);
721            fail("should throw UnknownHostException");
722        } catch (UnknownHostException uhe) {
723            // expected
724        }
725        byte[] addr2 = { (byte) 0xFE, (byte) 0x80, 0, 0, 0, 0, 0, 0, 0x02,
726                0x11, 0x25, (byte) 0xFF, (byte) 0xFE, (byte) 0xF8, (byte)
727
728                0x7C, (byte) 0xB2 };
729        // should not throw any exception
730        Inet6Address.getByAddress("123", addr2, nif);
731    }
732
733    public void test_getHostAddress_() throws Exception {
734        byte[] ipAddress = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
735        InetAddress ia = InetAddress.getByAddress(ipAddress);
736        assertEquals("::1", ia.getHostAddress().toLowerCase(Locale.US));
737
738        ipAddress = new byte[] { -2, -128, 0, 0, 0, 0, 0, 0, 2, 17, 37, -1, -2, -8, 124, -79 };
739        ia = InetAddress.getByAddress(ipAddress);
740        assertEquals("fe80::211:25ff:fef8:7cb1", ia.getHostAddress().toLowerCase(Locale.US));
741    }
742
743    public void test_getScopeID() throws UnknownHostException {
744        Inet6Address v6ia;
745        byte[] addr = { (byte) 0xFE, (byte) 0x80, 0, 0, 0, 0, 0, 0, 0x02, 0x11,
746                0x25, (byte) 0xFF, (byte) 0xFE, (byte) 0xF8, (byte) 0x7C,
747                (byte) 0xB2 };
748
749        v6ia = Inet6Address.getByAddress("123", addr, 3);
750        assertEquals(3, v6ia.getScopeId());
751
752        v6ia = Inet6Address.getByAddress("123", addr, 0);
753        assertEquals(0, v6ia.getScopeId());
754
755        v6ia = Inet6Address.getByAddress("123", addr, -1);
756        assertEquals(0, v6ia.getScopeId());
757    }
758
759    public void test_getScopedInterface() throws UnknownHostException {
760        byte[] addr = { (byte) 0xFE, (byte) 0x80, (byte) 0x09, (byte) 0xb5,
761                (byte) 0x6b, (byte) 0xa4, 0, 0, 0, 0, 0, 0, (byte) 0x09,
762                (byte) 0xb5, (byte) 0x6b, (byte) 0xa4 };
763        Inet6Address v6Addr;
764        v6Addr = Inet6Address.getByAddress("123", addr, null);
765        assertNull(v6Addr.getScopedInterface());
766    }
767
768    public void test_hashCode() throws UnknownHostException {
769        byte[] addr = { (byte) 0xFE, (byte) 0x80, 0, 0, 0, 0, 0, 0, 0x02, 0x11,
770                0x25, (byte) 0xFF, (byte) 0xFE, (byte) 0xF8, (byte) 0x7C,
771                (byte) 0xB2 };
772        Inet6Address address1, address2;
773
774        address1 = Inet6Address.getByAddress("123", addr, 0);
775        address2 = Inet6Address.getByAddress("1234", addr, 0);
776        assertEquals(address1.hashCode(), address2.hashCode());
777    }
778
779    int bytesToInt(byte bytes[], int start) {
780
781        int byteMask = 255;
782        int value = ((bytes[start + 3] & byteMask))
783                | ((bytes[start + 2] & byteMask) << 8)
784                | ((bytes[start + 1] & byteMask) << 16)
785                | ((bytes[start] & byteMask) << 24);
786        return value;
787
788    }
789
790    String byteArrayToHexString(byte bytes[], boolean leadingZeros) {
791
792        String fullString = "";
793        int times = bytes.length / 4;
794        int intArray[] = new int[times];
795        for (int i = 0; i < times; i++) {
796            intArray[i] = bytesToInt(bytes, i * 4);
797        }
798
799        return intArrayToHexString(intArray, leadingZeros);
800    }
801
802    void intToBytes(int value, byte bytes[], int start) {
803
804        int byteMask = 255;
805        bytes[start + 3] = (byte) (value & byteMask);
806        bytes[start + 2] = (byte) ((value >> 8) & byteMask);
807        bytes[start + 1] = (byte) ((value >> 16) & byteMask);
808        bytes[start] = (byte) ((value >> 24) & byteMask);
809    }
810
811    String intArrayToHexString(int ints[], boolean leadingZeros) {
812
813        String fullString = "";
814        String tempString;
815        int intsLength = ints.length;
816        for (int i = 0; i < intsLength; i++) {
817            tempString = Integer.toHexString(ints[i]);
818            while (tempString.length() < 4 && leadingZeros) {
819                tempString = "0" + tempString;
820            }
821            if (i + 1 < intsLength) {
822                tempString += ":";
823            }
824            fullString += tempString;
825        }
826
827        return fullString.toUpperCase();
828    }
829
830    // comparator for Inet6Address objects
831    private static final SerializableAssert COMPARATOR = new SerializableAssert() {
832        public void assertDeserialized(Serializable initial,
833                Serializable deserialized) {
834
835            Inet6Address initAddr = (Inet6Address) initial;
836            Inet6Address desrAddr = (Inet6Address) deserialized;
837
838            byte[] iaAddresss = initAddr.getAddress();
839            byte[] deIAAddresss = desrAddr.getAddress();
840            for (int i = 0; i < iaAddresss.length; i++) {
841                assertEquals(iaAddresss[i], deIAAddresss[i]);
842            }
843            assertEquals(initAddr.getScopeId(), desrAddr.getScopeId());
844            assertEquals(initAddr.getScopedInterface(), desrAddr
845                    .getScopedInterface());
846        }
847    };
848
849    /**
850     * Tests serialization/deserialization compatibility with ourselves.
851     */
852    public void testSerializationSelf() throws Exception {
853
854        byte[] localv6 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
855
856        SerializationTest.verifySelf(InetAddress.getByAddress(localv6),
857                COMPARATOR);
858    }
859
860    /**
861     * Tests serialization/deserialization compatibility with RI.
862     */
863    public void testSerializationCompatibility() throws Exception {
864
865        byte[] localv6 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
866
867        Object[] addresses = { InetAddress.getByAddress(localv6),
868                // Regression for Harmony-1039: ser-form has
869                // null interface name
870                InetAddress.getByAddress(localv6) };
871
872        try {
873            SerializationTest.verifyGolden(this, addresses, COMPARATOR);
874            fail();
875        } catch (InvalidObjectException expected) {
876            // We use different Inet6Address scheme from OpenJDK,
877            // deserialization is not supported and should throw an exception.
878        }
879    }
880}
881