001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.luni.tests.java.net;
019:
020: import java.io.Serializable;
021: import java.net.Inet6Address;
022: import java.net.InetAddress;
023: import java.net.NetworkInterface;
024: import java.net.UnknownHostException;
025:
026: import org.apache.harmony.testframework.serialization.SerializationTest;
027: import org.apache.harmony.testframework.serialization.SerializationTest.SerializableAssert;
028:
029: public class Inet6AddressTest extends junit.framework.TestCase {
030:
031: /**
032: * @tests java.net.Inet6Address#isMulticastAddress()
033: */
034: public void test_isMulticastAddress() throws Exception {
035:
036: String addrName = "";
037: InetAddress addr = null;
038:
039: // IP V6 regular multicast and non-multicast tests
040: //
041: // Create 2 IP v6 addresses and call "isMulticastAddress()"
042: // A prefix of "11111111" means that the address is multicast
043: // The first one will be one with the prefix the second without
044:
045: addrName = "FFFF::42:42"; // 11111111 = FFFF
046: addr = InetAddress.getByName(addrName);
047: assertTrue("Multicast address " + addrName + " not detected.",
048: addr.isMulticastAddress());
049:
050: addrName = "42::42:42"; // an non-multicast address
051: addr = InetAddress.getByName(addrName);
052: assertTrue("Non multicast address " + addrName
053: + " reporting as a multicast address.", !addr
054: .isMulticastAddress());
055:
056: // IPv4-compatible IPv6 address tests
057: //
058: // Now create 2 IP v6 addresses that are IP v4 compatable
059: // to IP v6 addresses. The address prefix for a multicast ip v4
060: // address is 1110 for the last 16 bits ::d.d.d.d
061: // We expect these to be false
062:
063: addrName = "::224.42.42.42"; // an ipv4 multicast addr 1110 = 224
064: addr = InetAddress.getByName(addrName);
065: assertTrue("IPv4 compatable address " + addrName
066: + " reported incorrectly as multicast.", !addr
067: .isMulticastAddress());
068:
069: addrName = "::42.42.42.42"; // an ipv4 non-multicast address
070: addr = InetAddress.getByName(addrName);
071: assertTrue("IPv4 compatable address " + addrName
072: + " reported incorrectly as multicast.", !addr
073: .isMulticastAddress());
074:
075: // IPv4-mapped IPv6 address tests
076: //
077: // Now create 2 IP v6 addresses that are IP v4 compatable
078: // to IP v6 addresses. The address prefix for a multicast ip v4
079: // address is 1110 for the last 16 bits ::FFFF:d.d.d.d
080:
081: addrName = "::FFFF:224.42.42.42"; // an ipv4 multicast addr 1110 =
082: // 224
083: addr = InetAddress.getByName(addrName);
084: assertTrue("IPv4-mapped IPv6 multicast address " + addrName
085: + " not detected.", addr.isMulticastAddress());
086:
087: addrName = "::FFFF:42.42.42.42"; // an ipv4 non-multicast address
088: addr = InetAddress.getByName(addrName);
089: assertTrue("IPv4-mapped IPv6 non-multicast address " + addrName
090: + " reporting as a multicast address.", !addr
091: .isMulticastAddress());
092: }
093:
094: /**
095: * @tests java.net.Inet6Address#isAnyLocalAddress()
096: */
097: public void test_isAnyLocalAddress() throws Exception {
098:
099: String addrName = "";
100: InetAddress addr = null;
101:
102: // test to ensure that the unspecified address returns tru
103: addrName = "::0"; // The unspecified address
104: addr = InetAddress.getByName(addrName);
105: assertTrue(
106: "The unspecified (also known as wildcard and any local address) "
107: + addrName + " not detected.", addr
108: .isAnyLocalAddress());
109:
110: addrName = "::"; // another form of the unspecified address
111: addr = InetAddress.getByName(addrName);
112: assertTrue(
113: "The unspecified (also known as wildcard and any local address) "
114: + addrName + " not detected.", addr
115: .isAnyLocalAddress());
116:
117: addrName = "::1"; // The loopback address
118: addr = InetAddress.getByName(addrName);
119: assertTrue("The addresses " + addrName
120: + " incorrectly reporting an the unspecified address.",
121: !addr.isAnyLocalAddress());
122: }
123:
124: /**
125: * @tests java.net.Inet6Address#isLoopbackAddress()
126: */
127: public void test_isLoopbackAddress() throws Exception {
128:
129: String addrName = "";
130: // IP V6 regular address tests for loopback
131: // The loopback address for IPv6 is ::1
132:
133: addrName = "::1";
134: InetAddress addr = InetAddress.getByName(addrName);
135: assertTrue("IPv6 loopback address " + addrName
136: + " not detected.", addr.isLoopbackAddress());
137:
138: addrName = "::2";
139: addr = InetAddress.getByName(addrName);
140: assertTrue("IPv6 address incorrectly " + addrName
141: + " detected as a loopback address.", !addr
142: .isLoopbackAddress());
143:
144: // a loopback address should be 127.d.d.d
145: addrName = "42:42::42:42";
146: addr = InetAddress.getByName(addrName);
147: assertTrue("IPv6 address incorrectly " + addrName
148: + " detected as a loopback address.", !addr
149: .isLoopbackAddress());
150:
151: // IPv4-compatible IPv6 address tests
152: //
153: // Now create 2 IP v6 addresses that are IP v4 compatable
154: // to IP v6 addresses. The address prefix for a multicast ip v4
155: // address is 1110 for the last 16 bits ::d.d.d.d
156: // We expect these to be false, as they are not IPv4 addresses
157:
158: // a loopback address should be 127.d.d.d
159: addrName = "::127.0.0.0";
160: addr = InetAddress.getByName(addrName);
161: assertTrue("IPv4-compatible IPv6 address " + addrName
162: + " detected incorrectly as a loopback.", !addr
163: .isLoopbackAddress());
164:
165: addrName = "::127.42.42.42"; // a loopback address should be
166: // 127.d.d.d
167: addr = InetAddress.getByName(addrName);
168: assertTrue("IPv4-compatible IPv6 address " + addrName
169: + " detected incorrectly as a loopback.", !addr
170: .isLoopbackAddress());
171:
172: // a loopback address should be 127.d.d.d
173: addrName = "::42.42.42.42";
174: addr = InetAddress.getByName(addrName);
175: assertTrue("IPv4-compatible IPv6 address " + addrName
176: + " detected incorrectly as a loopback.", !addr
177: .isLoopbackAddress());
178:
179: // IPv4-mapped IPv6 address tests
180: //
181: // Now create 2 IP v6 addresses that are IP v4 compatable
182: // to IP v6 addresses. The address prefix for a multicast ip v4
183: // address is 1110 for the last 16 bits ::FFFF:d.d.d.d
184:
185: // a loopback address should be 127.d.d.d
186: addrName = "::FFFF:127.0.0.0";
187: addr = InetAddress.getByName(addrName);
188: assertTrue("IPv4-compatible IPv6 loopback address " + addrName
189: + " not detected.", addr.isLoopbackAddress());
190:
191: // a loopback address should be 127.d.d.d
192: addrName = "::FFFF:127.42.42.42";
193: addr = InetAddress.getByName(addrName);
194: assertTrue("IPv4-compatible IPv6 loopback address " + addrName
195: + " not detected.", addr.isLoopbackAddress());
196:
197: // a loopback address should be 127.d.d.d
198: addrName = "::FFFF:42.42.42.42";
199: addr = InetAddress.getByName(addrName);
200: assertTrue("IPv4-compatible IPv6 address incorrectly "
201: + addrName + " detected as a loopback address.", !addr
202: .isLoopbackAddress());
203: }
204:
205: /**
206: * @tests java.net.Inet6Address#isLinkLocalAddress()
207: */
208: public void test_isLinkLocalAddress() throws Exception {
209:
210: String addrName = "";
211: // IP V6 regular address tests for link local addresses
212: //
213: // Link local addresses are FE80:: -
214: // FEBF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF
215:
216: addrName = "FE80::0";
217: InetAddress addr = InetAddress.getByName(addrName);
218: assertTrue("IPv6 link local address " + addrName
219: + " not detected.", addr.isLinkLocalAddress());
220:
221: addrName = "FEBF::FFFF:FFFF:FFFF:FFFF";
222: addr = InetAddress.getByName(addrName);
223: assertTrue("IPv6 link local address " + addrName
224: + " not detected.", addr.isLinkLocalAddress());
225:
226: addrName = "FEC0::1";
227: addr = InetAddress.getByName(addrName);
228: assertTrue("IPv6 address " + addrName
229: + " detected incorrectly as a link local address.",
230: !addr.isLinkLocalAddress());
231:
232: addrName = "FD80::1:FFFF:FFFF:FFFF:FFFF";
233: addr = InetAddress.getByName(addrName);
234: assertTrue("IPv6 address " + addrName
235: + " detected incorrectly as a link local address.",
236: !addr.isLinkLocalAddress());
237:
238: addrName = "FE7F::FFFF:FFFF:FFFF:FFFF";
239: addr = InetAddress.getByName(addrName);
240: assertTrue("IPv6 address " + addrName
241: + " detected incorrectly as a link local address.",
242: !addr.isLinkLocalAddress());
243: }
244:
245: /**
246: * @tests java.net.Inet6Address#isSiteLocalAddress()
247: */
248: public void test_isSiteLocalAddress() throws Exception {
249: String addrName = "";
250:
251: // IP V6 regular address tests for link local addresses
252: //
253: // Link local addresses are FEC0::0 through to
254: // FEFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF
255:
256: addrName = "FEC0::0";
257: InetAddress addr = InetAddress.getByName(addrName);
258: assertTrue("IPv6 site local address " + addrName
259: + " not detected.", addr.isSiteLocalAddress());
260:
261: addrName = "FEFF::FFFF:FFFF:FFFF:FFFF:FFFF";
262: addr = InetAddress.getByName(addrName);
263: assertTrue("IPv6 site local address " + addrName
264: + " not detected.", addr.isSiteLocalAddress());
265:
266: addrName = "FEBF::FFFF:FFFF:FFFF:FFFF:FFFF";
267: addr = InetAddress.getByName(addrName);
268: assertTrue("IPv6 address " + addrName
269: + " detected incorrectly as a site local address.",
270: !addr.isSiteLocalAddress());
271:
272: addrName = "FFC0::0";
273: addr = InetAddress.getByName(addrName);
274: assertTrue("IPv6 address " + addrName
275: + " detected incorrectly as a site local address.",
276: !addr.isSiteLocalAddress());
277: }
278:
279: /**
280: * @tests java.net.Inet6Address#isMCGlobal()
281: */
282: public void test_isMCGlobal() throws Exception {
283: String addrName = "";
284: // IP V6 regular address tests for Mulitcase Global addresses
285: //
286: // Multicast global addresses are FFxE:/112 where x is
287: // a set of flags, and the addition 112 bits make up
288: // the global address space
289:
290: addrName = "FF0E::0";
291: InetAddress addr = InetAddress.getByName(addrName);
292: assertTrue("IPv6 global mutlicast address " + addrName
293: + " not detected.", addr.isMCGlobal());
294:
295: addrName = "FF0E:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF";
296: addr = InetAddress.getByName(addrName);
297: assertTrue("IPv6 global multicast address " + addrName
298: + " not detected.", addr.isMCGlobal());
299:
300: // a currently invalid address as the prefix FFxE
301: // is only valid for x = {1,0} as the rest are reserved
302: addrName = "FFFE::0";
303: addr = InetAddress.getByName(addrName);
304: assertTrue("IPv6 global mutlicast address " + addrName
305: + " not detected.", addr.isMCGlobal());
306:
307: // a currently invalid address as the prefix FFxE
308: // is only valid for x = {1,0} as the rest are reserved
309: addrName = "FFFE:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF";
310: addr = InetAddress.getByName(addrName);
311: assertTrue("IPv6 global multicast address " + addrName
312: + " not detected.", addr.isMCGlobal());
313:
314: // a sample MC organizational address
315: addrName = "FF08:42:42:42:42:42:42:42";
316: addr = InetAddress.getByName(addrName);
317: assertTrue("IPv6 mulitcast organizational " + addrName
318: + " incorrectly indicated as a global address.", !addr
319: .isMCGlobal());
320:
321: // a sample MC site address
322: addrName = "FF05:42:42:42:42:42:42:42";
323: addr = InetAddress.getByName(addrName);
324: assertTrue("IPv6 mulitcast site address " + addrName
325: + " incorrectly indicated as a global address.", !addr
326: .isMCGlobal());
327:
328: // a sample MC link address
329: addrName = "FF02:42:42:42:42:42:42:42";
330: addr = InetAddress.getByName(addrName);
331: assertTrue("IPv6 mulitcast link address " + addrName
332: + " incorrectly indicated as a global address.", !addr
333: .isMCGlobal());
334:
335: // a sample MC Node
336: addrName = "FF01:42:42:42:42:42:42:42";
337: addr = InetAddress.getByName(addrName);
338: assertTrue("IPv6 mulitcast node address " + addrName
339: + " incorrectly indicated as a global address.", !addr
340: .isMCGlobal());
341:
342: // IPv4-mapped IPv6 address tests
343: addrName = "::FFFF:224.0.1.0";
344: addr = InetAddress.getByName(addrName);
345: assertTrue("IPv4 global multicast address " + addrName
346: + " not identified as a global multicast address.",
347: addr.isMCGlobal());
348:
349: addrName = "::FFFF:238.255.255.255";
350: addr = InetAddress.getByName(addrName);
351: assertTrue("IPv4 global multicast address " + addrName
352: + " not identified as a global multicast address.",
353: addr.isMCGlobal());
354: }
355:
356: /**
357: * @tests java.net.Inet6Address#isMCNodeLocal()
358: */
359: public void test_isMCNodeLocal() throws Exception {
360: String addrName = "";
361: // IP V6 regular address tests for Mulitcase node local addresses
362: //
363: // Multicast node local addresses are FFx1:/112 where x is
364: // a set of flags, and the addition 112 bits make up
365: // the global address space
366:
367: addrName = "FF01::0";
368: InetAddress addr = InetAddress.getByName(addrName);
369: assertTrue("IPv6 node-local mutlicast address " + addrName
370: + " not detected.", addr.isMCNodeLocal());
371:
372: addrName = "FF01:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF";
373: addr = InetAddress.getByName(addrName);
374: assertTrue("IPv6 node-local multicast address " + addrName
375: + " not detected.", addr.isMCNodeLocal());
376:
377: // a currently invalid address as the prefix FFxE
378: // is only valid for x = {1,0} as the rest are reserved
379: addrName = "FFF1::0";
380: addr = InetAddress.getByName(addrName);
381: assertTrue("IPv6 node-local mutlicast address " + addrName
382: + " not detected.", addr.isMCNodeLocal());
383:
384: // a currently invalid address as the prefix FFxE
385: // is only valid for x = {1,0} as the rest are reserved
386: addrName = "FFF1:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF";
387: addr = InetAddress.getByName(addrName);
388: assertTrue("IPv6 node-local multicast address " + addrName
389: + " not detected.", addr.isMCNodeLocal());
390:
391: // a sample MC organizational address
392: addrName = "FF08:42:42:42:42:42:42:42";
393: addr = InetAddress.getByName(addrName);
394: assertTrue("IPv6 mulitcast organizational address " + addrName
395: + " incorrectly indicated as a node-local address.",
396: !addr.isMCNodeLocal());
397:
398: // a sample MC site address
399: addrName = "FF05:42:42:42:42:42:42:42";
400: addr = InetAddress.getByName(addrName);
401: assertTrue("IPv6 mulitcast site address " + addrName
402: + " incorrectly indicated as a node-local address.",
403: !addr.isMCNodeLocal());
404:
405: // a sample MC link address
406: addrName = "FF02:42:42:42:42:42:42:42";
407: addr = InetAddress.getByName(addrName);
408: assertTrue("IPv6 mulitcast link address " + addrName
409: + " incorrectly indicated as a node-local address.",
410: !addr.isMCNodeLocal());
411:
412: // a sample MC global address
413: addrName = "FF0E:42:42:42:42:42:42:42";
414: addr = InetAddress.getByName(addrName);
415: assertTrue("IPv6 mulitcast node address " + addrName
416: + " incorrectly indicated as a node-local address.",
417: !addr.isMCNodeLocal());
418: }
419:
420: /**
421: * @tests java.net.Inet6Address#isMCLinkLocal()
422: */
423: public void test_isMCLinkLocal() throws Exception {
424: String addrName = "";
425: // IP V6 regular address tests for Mulitcase link local addresses
426: //
427: // Multicast link local addresses are FFx2:/112 where x is
428: // a set of flags, and the addition 112 bits make up
429: // the global address space
430:
431: addrName = "FF02::0";
432: InetAddress addr = InetAddress.getByName(addrName);
433: assertTrue("IPv6 link local multicast address " + addrName
434: + " not detected.", addr.isMCLinkLocal());
435:
436: addrName = "FF02:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF";
437: addr = InetAddress.getByName(addrName);
438: assertTrue("IPv6 link local multicast address " + addrName
439: + " not detected.", addr.isMCLinkLocal());
440:
441: // a currently invalid address as the prefix FFxE
442: // is only valid for x = {1,0} as the rest are reserved
443: addrName = "FFF2::0";
444: addr = InetAddress.getByName(addrName);
445: assertTrue("IPv6 link local multicast address " + addrName
446: + " not detected.", addr.isMCLinkLocal());
447:
448: // a currently invalid address as the prefix FFxE
449: // is only valid for x = {1,0} as the rest are reserved
450: addrName = "FFF2:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF";
451: addr = InetAddress.getByName(addrName);
452: assertTrue("IPv6 link local multicast address " + addrName
453: + " not detected.", addr.isMCLinkLocal());
454:
455: // a sample MC organizational address
456: addrName = "FF08:42:42:42:42:42:42:42";
457: addr = InetAddress.getByName(addrName);
458: assertTrue(
459: "IPv6 organization multicast address "
460: + addrName
461: + " incorrectly indicated as a link-local mulitcast address.",
462: !addr.isMCLinkLocal());
463:
464: // a sample MC site address
465: addrName = "FF05:42:42:42:42:42:42:42";
466: addr = InetAddress.getByName(addrName);
467: assertTrue(
468: "IPv6 site-local mulitcast address "
469: + addrName
470: + " incorrectly indicated as a link-local mulitcast address.",
471: !addr.isMCLinkLocal());
472:
473: // a sample MC global address
474: addrName = "FF0E:42:42:42:42:42:42:42";
475: addr = InetAddress.getByName(addrName);
476: assertTrue(
477: "IPv6 global multicast address "
478: + addrName
479: + " incorrectly indicated as a link-local mulitcast address.",
480: !addr.isMCLinkLocal());
481:
482: // a sample MC Node
483: addrName = "FF01:42:42:42:42:42:42:42";
484: addr = InetAddress.getByName(addrName);
485: assertTrue(
486: "IPv6 mulitcast node address "
487: + addrName
488: + " incorrectly indicated as a link-local mulitcast address.",
489: !addr.isMCLinkLocal());
490:
491: // Ipv4-mapped IPv6 addresses
492:
493: addrName = "::FFFF:224.0.0.0"; // a multicast addr 1110
494: addr = InetAddress.getByName(addrName);
495: assertTrue("IPv4 link-local multicast address " + addrName
496: + " not identified as a link-local multicast address.",
497: addr.isMCLinkLocal());
498:
499: addrName = "::FFFF:224.0.0.255"; // a multicast addr 1110
500: addr = InetAddress.getByName(addrName);
501: assertTrue("IPv4 link-local multicast address " + addrName
502: + " not identified as a link-local multicast address.",
503: addr.isMCLinkLocal());
504: }
505:
506: /**
507: * @tests java.net.Inet6Address#isMCSiteLocal()
508: */
509: public void test_isMCSiteLocal() throws Exception {
510: String addrName = "";
511: // IP V6 regular address tests for Multicast site-local addresses
512: //
513: // Multicast global addresses are FFx5:/112 where x is
514: // a set of flags, and the addition 112 bits make up
515: // the global address space
516:
517: addrName = "FF05::0";
518: InetAddress addr = InetAddress.getByName(addrName);
519: assertTrue("IPv6 site-local mutlicast address " + addrName
520: + " not detected.", addr.isMCSiteLocal());
521:
522: addrName = "FF05:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF";
523: addr = InetAddress.getByName(addrName);
524: assertTrue("IPv6 site-local multicast address " + addrName
525: + " not detected.", addr.isMCSiteLocal());
526:
527: // a currently invalid address as the prefix FFxE
528: // is only valid for x = {1,0} as the rest are reserved
529: addrName = "FFF5::0";
530: addr = InetAddress.getByName(addrName);
531: assertTrue("IPv6 site-local mutlicast address " + addrName
532: + " not detected.", addr.isMCSiteLocal());
533:
534: // a currently invalid address as the prefix FFxE
535: // is only valid for x = {1,0} as the rest are reserved
536: addrName = "FFF5:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF";
537: addr = InetAddress.getByName(addrName);
538: assertTrue("IPv6 site-local multicast address " + addrName
539: + " not detected.", addr.isMCSiteLocal());
540:
541: // a sample MC organizational address
542: addrName = "FF08:42:42:42:42:42:42:42";
543: addr = InetAddress.getByName(addrName);
544: assertTrue(
545: "IPv6 organization multicast address "
546: + addrName
547: + " incorrectly indicated as a site-local mulitcast address.",
548: !addr.isMCSiteLocal());
549:
550: // a sample MC global address
551: addrName = "FF0E:42:42:42:42:42:42:42";
552: addr = InetAddress.getByName(addrName);
553: assertTrue(
554: "IPv6 global mulitcast address "
555: + addrName
556: + " incorrectly indicated as a site-local mulitcast address.",
557: !addr.isMCSiteLocal());
558:
559: // a sample MC link address
560: addrName = "FF02:42:42:42:42:42:42:42";
561: addr = InetAddress.getByName(addrName);
562: assertTrue(
563: "IPv6 link-local multicast address "
564: + addrName
565: + " incorrectly indicated as a site-local mulitcast address.",
566: !addr.isMCSiteLocal());
567:
568: // a sample MC Node
569: addrName = "FF01:42:42:42:42:42:42:42";
570: addr = InetAddress.getByName(addrName);
571: assertTrue(
572: "IPv6 mulitcast node address "
573: + addrName
574: + " incorrectly indicated as a site-local mulitcast address.",
575: !addr.isMCSiteLocal());
576:
577: // IPv4-mapped IPv6 addresses
578: addrName = "::FFFF:239.255.0.0";
579: addr = InetAddress.getByName(addrName);
580: assertTrue("IPv4 site-local multicast address " + addrName
581: + " not identified as a site-local multicast address.",
582: addr.isMCSiteLocal());
583:
584: addrName = "::FFFF:239.255.255.255";
585: addr = InetAddress.getByName(addrName);
586: assertTrue("IPv4 site-local multicast address " + addrName
587: + " not identified as a site-local multicast address.",
588: addr.isMCSiteLocal());
589: }
590:
591: /**
592: * @tests java.net.Inet6Address#isMCOrgLocal()
593: */
594: public void test_isMCOrgLocal() throws Exception {
595: String addrName = "";
596: // IP V6 regular address tests for Mulitcase organization-local
597: // addresses
598: //
599: // Multicast global addresses are FFxE:/112 where x is
600: // a set of flags, and the addition 112 bits make up
601: // the global address space
602:
603: addrName = "FF08::0";
604: InetAddress addr = InetAddress.getByName(addrName);
605: assertTrue("IPv6 organization-local mutlicast address "
606: + addrName + " not detected.", addr.isMCOrgLocal());
607:
608: addrName = "FF08:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF";
609: addr = InetAddress.getByName(addrName);
610: assertTrue("IPv6 organization-local multicast address "
611: + addrName + " not detected.", addr.isMCOrgLocal());
612:
613: // a currently invalid address as the prefix FFxE
614: // is only valid for x = {1,0} as the rest are reserved
615: addrName = "FFF8::0";
616: addr = InetAddress.getByName(addrName);
617: assertTrue("IPv6 organization-local mutlicast address "
618: + addrName + " not detected.", addr.isMCOrgLocal());
619:
620: // a currently invalid address as the prefix FFxE
621: // is only valid for x = {1,0} as the rest are reserved
622: addrName = "FFF8:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF";
623: addr = InetAddress.getByName(addrName);
624: assertTrue("IPv6 organization-local multicast address "
625: + addrName + " not detected.", addr.isMCOrgLocal());
626:
627: // a sample MC global address
628: addrName = "FF0E:42:42:42:42:42:42:42";
629: addr = InetAddress.getByName(addrName);
630: assertTrue(
631: "IPv6 global multicast address "
632: + addrName
633: + " incorrectly indicated as an organization-local mulitcast address.",
634: !addr.isMCOrgLocal());
635:
636: // a sample MC site address
637: addrName = "FF05:42:42:42:42:42:42:42";
638: addr = InetAddress.getByName(addrName);
639: assertTrue(
640: "IPv6 site-local mulitcast address "
641: + addrName
642: + " incorrectly indicated as an organization-local mulitcast address.",
643: !addr.isMCOrgLocal());
644:
645: // a sample MC link address
646: addrName = "FF02:42:42:42:42:42:42:42";
647: addr = InetAddress.getByName(addrName);
648: assertTrue(
649: "IPv6 link-local multicast address "
650: + addrName
651: + " incorrectly indicated as an organization-local mulitcast address.",
652: !addr.isMCOrgLocal());
653:
654: // a sample MC Node
655: addrName = "FF01:42:42:42:42:42:42:42";
656: addr = InetAddress.getByName(addrName);
657: assertTrue(
658: "IPv6 mulitcast node address "
659: + addrName
660: + " incorrectly indicated as an organization-local mulitcast address.",
661: !addr.isMCOrgLocal());
662:
663: // IPv4-mapped IPv6 addresses
664:
665: addrName = "::FFFF:239.192.0.0";
666: addr = InetAddress.getByName(addrName);
667: assertTrue("IPv4 org-local multicast address " + addrName
668: + " not identified as a org-local multicast address.",
669: addr.isMCOrgLocal());
670:
671: addrName = "::FFFF:239.195.255.255";
672: addr = InetAddress.getByName(addrName);
673: assertTrue("IPv4 org-local multicast address " + addrName
674: + " not identified as a org-local multicast address.",
675: addr.isMCOrgLocal());
676: }
677:
678: /**
679: * @tests java.net.Inet6Address#isIPv4CompatibleAddress()
680: */
681: public void test_isIPv4CompatibleAddress() throws Exception {
682: String addrName = "";
683: Inet6Address addr = null;
684:
685: // Tests a number of addresses to see if they are compatable with
686: // IPv6 addresses
687:
688: addrName = "FFFF::42:42"; // 11111111 = FFFF
689: addr = (Inet6Address) InetAddress.getByName(addrName);
690: assertTrue(
691: "A non-compatable IPv6 address "
692: + addrName
693: + " incorrectly identified as a IPv4 compatable address.",
694: !addr.isIPv4CompatibleAddress());
695:
696: // IPv4-compatible IPv6 address tests
697: //
698: // Now create 2 IP v6 addresses that are IP v4 compatable
699: // to IP v6 addresses.
700:
701: addrName = "::0.0.0.0";
702: addr = (Inet6Address) InetAddress.getByName(addrName);
703: assertTrue("IPv4 compatable address " + addrName
704: + " not detected correctly.", addr
705: .isIPv4CompatibleAddress());
706:
707: addrName = "::255.255.255.255"; // an ipv4 non-multicast address
708: addr = (Inet6Address) InetAddress.getByName(addrName);
709: assertTrue("IPv4 compatable address " + addrName
710: + " not detected correctly.", addr
711: .isIPv4CompatibleAddress());
712: }
713:
714: /**
715: * @tests java.net.Inet6Address#getAddress()
716: */
717: public void test_getAddress() {
718: // TODO : Implementation
719: }
720:
721: /**
722: * @tests java.net.Inet6Address#getByName(java.lang.String)
723: */
724: public void test_getByNameLjava_lang_String() throws Exception {
725: // ones to add "::255.255.255.255", "::FFFF:0.0.0.0",
726: // "0.0.0.0.0.0::255.255.255.255", "F:F:F:F:F:F:F:F",
727: // "[F:F:F:F:F:F:F:F]"
728: String validIPAddresses[] = { "::1.2.3.4", "::", "::", "1::0",
729: "1::", "::1", "0", /* jdk1.5 accepts 0 as valid */
730: "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF",
731: "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:255.255.255.255",
732: "0:0:0:0:0:0:0:0", "0:0:0:0:0:0:0.0.0.0" };
733:
734: String invalidIPAddresses[] = { "FFFF:FFFF" };
735:
736: for (int i = 0; i < validIPAddresses.length; i++) {
737:
738: InetAddress.getByName(validIPAddresses[i]);
739:
740: //exercise positive cache
741: InetAddress.getByName(validIPAddresses[i]);
742:
743: if (!validIPAddresses[i].equals("0")) {
744: String tempIPAddress = "[" + validIPAddresses[i] + "]";
745: InetAddress.getByName(tempIPAddress);
746: }
747: }
748:
749: for (int i = 0; i < invalidIPAddresses.length; i++) {
750: try {
751: InetAddress.getByName(invalidIPAddresses[i]);
752: fail("Invalid IP address incorrectly recognized as valid: "
753: + invalidIPAddresses[i]);
754: } catch (Exception e) {
755: }
756:
757: //exercise negative cache
758: try {
759: InetAddress.getByName(invalidIPAddresses[i]);
760: fail("Invalid IP address incorrectly recognized as valid: "
761: + invalidIPAddresses[i]);
762: } catch (Exception e) {
763: }
764: }
765: }
766:
767: /**
768: * @tests java.net.Inet6Address#getByAddress(String, byte[], int)
769: */
770: public void test_getByAddressLString$BI()
771: throws UnknownHostException {
772: try {
773: Inet6Address.getByAddress("123", null, 0);
774: fail("should throw UnknownHostException");
775: } catch (UnknownHostException uhe) {
776: // expected
777: }
778: byte[] addr1 = { (byte) 127, 0, 0, 1 };
779: try {
780: Inet6Address.getByAddress("123", addr1, 0);
781: fail("should throw UnknownHostException");
782: } catch (UnknownHostException uhe) {
783: // expected
784: }
785:
786: byte[] addr2 = { (byte) 0xFE, (byte) 0x80, 0, 0, 0, 0, 0, 0,
787: 0x02, 0x11, 0x25, (byte) 0xFF, (byte) 0xFE,
788: (byte) 0xF8, (byte) 0x7C, (byte) 0xB2 };
789:
790: // should not throw any exception
791: Inet6Address.getByAddress("123", addr2, 3);
792: Inet6Address.getByAddress("123", addr2, 0);
793: Inet6Address.getByAddress("123", addr2, -1);
794: }
795:
796: /**
797: * @tests java.net.Inet6Address#getByAddress(String, byte[],
798: * NetworkInterface)
799: */
800: public void test_getByAddressLString$BLNetworkInterface()
801: throws UnknownHostException {
802: NetworkInterface nif = null;
803: try {
804: Inet6Address.getByAddress("123", null, nif);
805: fail("should throw UnknownHostException");
806: } catch (UnknownHostException uhe) {
807: // expected
808: }
809: byte[] addr1 = { (byte) 127, 0, 0, 1 };
810: try {
811: Inet6Address.getByAddress("123", addr1, nif);
812: fail("should throw UnknownHostException");
813: } catch (UnknownHostException uhe) {
814: // expected
815: }
816: byte[] addr2 = { (byte) 0xFE, (byte) 0x80, 0, 0, 0, 0, 0, 0,
817: 0x02, 0x11, 0x25, (byte) 0xFF, (byte) 0xFE,
818: (byte) 0xF8, (byte)
819:
820: 0x7C, (byte) 0xB2 };
821: // should not throw any exception
822: Inet6Address.getByAddress("123", addr2, nif);
823: }
824:
825: /**
826: * @throws UnknownHostException
827: * @tests java.net.Inet6Address#getScopeID()
828: */
829: public void test_getScopeID() throws UnknownHostException {
830: Inet6Address v6ia;
831: byte[] addr = { (byte) 0xFE, (byte) 0x80, 0, 0, 0, 0, 0, 0,
832: 0x02, 0x11, 0x25, (byte) 0xFF, (byte) 0xFE,
833: (byte) 0xF8, (byte) 0x7C, (byte) 0xB2 };
834:
835: v6ia = Inet6Address.getByAddress("123", addr, 3);
836: assertEquals(3, v6ia.getScopeId());
837:
838: v6ia = Inet6Address.getByAddress("123", addr, 0);
839: assertEquals(0, v6ia.getScopeId());
840:
841: v6ia = Inet6Address.getByAddress("123", addr, -1);
842: assertEquals(0, v6ia.getScopeId());
843: }
844:
845: /**
846: * @tests java.net.Inet6Address#getScopedInterface()
847: */
848: public void test_getScopedInterface() throws UnknownHostException {
849: byte[] addr = { (byte) 0xFE, (byte) 0x80, (byte) 0x09,
850: (byte) 0xb5, (byte) 0x6b, (byte) 0xa4, 0, 0, 0, 0, 0,
851: 0, (byte) 0x09, (byte) 0xb5, (byte) 0x6b, (byte) 0xa4 };
852: Inet6Address v6Addr;
853: v6Addr = Inet6Address.getByAddress("123", addr, null);
854: assertNull(v6Addr.getScopedInterface());
855: }
856:
857: int bytesToInt(byte bytes[], int start) {
858:
859: int byteMask = 255;
860: int value = ((bytes[start + 3] & byteMask))
861: | ((bytes[start + 2] & byteMask) << 8)
862: | ((bytes[start + 1] & byteMask) << 16)
863: | ((bytes[start] & byteMask) << 24);
864: return value;
865:
866: }
867:
868: String byteArrayToHexString(byte bytes[], boolean leadingZeros) {
869:
870: String fullString = "";
871: int times = bytes.length / 4;
872: int intArray[] = new int[times];
873: for (int i = 0; i < times; i++) {
874: intArray[i] = bytesToInt(bytes, i * 4);
875: }
876:
877: return intArrayToHexString(intArray, leadingZeros);
878: }
879:
880: void intToBytes(int value, byte bytes[], int start) {
881:
882: int byteMask = 255;
883: bytes[start + 3] = (byte) (value & byteMask);
884: bytes[start + 2] = (byte) ((value >> 8) & byteMask);
885: bytes[start + 1] = (byte) ((value >> 16) & byteMask);
886: bytes[start] = (byte) ((value >> 24) & byteMask);
887: }
888:
889: String intArrayToHexString(int ints[], boolean leadingZeros) {
890:
891: String fullString = "";
892: String tempString;
893: int intsLength = ints.length;
894: for (int i = 0; i < intsLength; i++) {
895: tempString = Integer.toHexString(ints[i]);
896: while (tempString.length() < 4 && leadingZeros) {
897: tempString = "0" + tempString;
898: }
899: if (i + 1 < intsLength) {
900: tempString += ":";
901: }
902: fullString += tempString;
903: }
904:
905: return fullString.toUpperCase();
906: }
907:
908: // comparator for Inet6Address objects
909: private static final SerializableAssert COMPARATOR = new SerializableAssert() {
910: public void assertDeserialized(Serializable initial,
911: Serializable deserialized) {
912:
913: Inet6Address initAddr = (Inet6Address) initial;
914: Inet6Address desrAddr = (Inet6Address) deserialized;
915:
916: byte[] iaAddresss = initAddr.getAddress();
917: byte[] deIAAddresss = desrAddr.getAddress();
918: for (int i = 0; i < iaAddresss.length; i++) {
919: assertEquals(iaAddresss[i], deIAAddresss[i]);
920: }
921: assertEquals(initAddr.getScopeId(), desrAddr.getScopeId());
922: assertEquals(initAddr.getScopedInterface(), desrAddr
923: .getScopedInterface());
924: }
925: };
926:
927: /**
928: * @tests serialization/deserialization compatibility.
929: */
930: public void testSerializationSelf() throws Exception {
931:
932: byte[] localv6 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
933: 1 };
934:
935: SerializationTest.verifySelf(InetAddress.getByAddress(localv6),
936: COMPARATOR);
937: }
938:
939: /**
940: * @tests serialization/deserialization compatibility with RI.
941: */
942: public void testSerializationCompatibility() throws Exception {
943:
944: byte[] localv6 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
945: 1 };
946:
947: Object[] addresses = { InetAddress.getByAddress(localv6),
948: // Regression for Harmony-1039: ser-form has
949: // null interface name
950: InetAddress.getByAddress(localv6) };
951:
952: SerializationTest.verifyGolden(this, addresses, COMPARATOR);
953: }
954: }
|