Source Code Cross Referenced for InetAddressTest.java in  » Apache-Harmony-Java-SE » org-package » org » apache » harmony » luni » tests » java » net » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Apache Harmony Java SE » org package » org.apache.harmony.luni.tests.java.net 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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.IOException;
021:        import java.io.Serializable;
022:        import java.net.DatagramSocket;
023:        import java.net.Inet4Address;
024:        import java.net.InetAddress;
025:        import java.net.NetworkInterface;
026:        import java.net.UnknownHostException;
027:        import java.security.Permission;
028:        import java.util.ArrayList;
029:        import java.util.Enumeration;
030:
031:        import org.apache.harmony.testframework.serialization.SerializationTest;
032:        import org.apache.harmony.testframework.serialization.SerializationTest.SerializableAssert;
033:
034:        import tests.support.Support_Configuration;
035:
036:        public class InetAddressTest extends junit.framework.TestCase {
037:
038:            private static boolean someoneDone[] = new boolean[2];
039:
040:            protected static boolean threadedTestSucceeded;
041:
042:            protected static String threadedTestErrorString;
043:
044:            /**
045:             * This class is used to test inet_ntoa, gethostbyaddr and gethostbyname
046:             * functions in the VM to make sure they're threadsafe. getByName will cause
047:             * the gethostbyname function to be called. getHostName will cause the
048:             * gethostbyaddr to be called. getHostAddress will cause inet_ntoa to be
049:             * called.
050:             */
051:            static class threadsafeTestThread extends Thread {
052:                private String lookupName;
053:
054:                private InetAddress testAddress;
055:
056:                private int testType;
057:
058:                /*
059:                 * REP_NUM can be adjusted if desired. Since this error is
060:                 * non-deterministic it may not always occur. Setting REP_NUM higher,
061:                 * increases the chances of an error being detected, but causes the test
062:                 * to take longer. Because the Java threads spend a lot of time
063:                 * performing operations other than running the native code that may not
064:                 * be threadsafe, it is quite likely that several thousand iterations
065:                 * will elapse before the first error is detected.
066:                 */
067:                private static final int REP_NUM = 20000;
068:
069:                public threadsafeTestThread(String name, String lookupName,
070:                        InetAddress testAddress, int type) {
071:                    super (name);
072:                    this .lookupName = lookupName;
073:                    this .testAddress = testAddress;
074:                    testType = type;
075:                }
076:
077:                public void run() {
078:                    try {
079:                        String correctName = testAddress.getHostName();
080:                        String correctAddress = testAddress.getHostAddress();
081:                        long startTime = System.currentTimeMillis();
082:
083:                        synchronized (someoneDone) {
084:                        }
085:
086:                        for (int i = 0; i < REP_NUM; i++) {
087:                            if (someoneDone[testType]) {
088:                                break;
089:                            } else if ((i % 25) == 0
090:                                    && System.currentTimeMillis() - startTime > 240000) {
091:                                System.out
092:                                        .println("Exiting due to time limitation after "
093:                                                + i + " iterations");
094:                                break;
095:                            }
096:
097:                            InetAddress ia = InetAddress.getByName(lookupName);
098:                            String hostName = ia.getHostName();
099:                            String hostAddress = ia.getHostAddress();
100:
101:                            // Intentionally not looking for exact name match so that 
102:                            // the test works across different platforms that may or 
103:                            // may not include a domain suffix on the hostname
104:                            if (!hostName.startsWith(correctName)) {
105:                                threadedTestSucceeded = false;
106:                                threadedTestErrorString = (testType == 0 ? "gethostbyname"
107:                                        : "gethostbyaddr")
108:                                        + ": getHostName() returned "
109:                                        + hostName
110:                                        + " instead of "
111:                                        + correctName;
112:                                break;
113:                            }
114:                            // IP addresses should match exactly
115:                            if (!correctAddress.equals(hostAddress)) {
116:                                threadedTestSucceeded = false;
117:                                threadedTestErrorString = (testType == 0 ? "gethostbyname"
118:                                        : "gethostbyaddr")
119:                                        + ": getHostName() returned "
120:                                        + hostAddress
121:                                        + " instead of "
122:                                        + correctAddress;
123:                                break;
124:                            }
125:
126:                        }
127:                        someoneDone[testType] = true;
128:                    } catch (Exception e) {
129:                        threadedTestSucceeded = false;
130:                        threadedTestErrorString = e.toString();
131:                    }
132:                }
133:            }
134:
135:            /**
136:             * @tests java.net.InetAddress#equals(java.lang.Object)
137:             */
138:            public void test_equalsLjava_lang_Object() throws Exception {
139:                // Test for method boolean java.net.InetAddress.equals(java.lang.Object)
140:                InetAddress ia1 = InetAddress
141:                        .getByName(Support_Configuration.InetTestAddress);
142:                InetAddress ia2 = InetAddress
143:                        .getByName(Support_Configuration.InetTestIP);
144:                assertTrue("Equals returned incorrect result - " + ia1 + " != "
145:                        + ia2, ia1.equals(ia2));
146:            }
147:
148:            /**
149:             * @tests java.net.InetAddress#getAddress()
150:             */
151:            public void test_getAddress() throws UnknownHostException {
152:                // Test for method byte [] java.net.InetAddress.getAddress()
153:                try {
154:                    InetAddress ia = InetAddress
155:                            .getByName(Support_Configuration.InetTestIP);
156:                    byte[] caddr = Support_Configuration.InetTestCaddr;
157:                    byte[] addr = ia.getAddress();
158:                    for (int i = 0; i < addr.length; i++)
159:                        assertTrue("Incorrect address returned",
160:                                caddr[i] == addr[i]);
161:                } catch (java.net.UnknownHostException e) {
162:                }
163:
164:                byte[] origBytes = new byte[] { 0, 1, 2, 3 };
165:                InetAddress address = InetAddress.getByAddress(origBytes);
166:                origBytes[0] = -1;
167:                byte[] newBytes = address.getAddress();
168:                assertSame((byte) 0, newBytes[0]);
169:            }
170:
171:            /**
172:             * @tests java.net.InetAddress#getAllByName(java.lang.String)
173:             */
174:            public void test_getAllByNameLjava_lang_String() throws Exception {
175:                // Test for method java.net.InetAddress []
176:                // java.net.InetAddress.getAllByName(java.lang.String)
177:                InetAddress[] all = InetAddress
178:                        .getAllByName(Support_Configuration.SpecialInetTestAddress);
179:                assertNotNull(all);
180:                // Number of aliases depends on individual test machine
181:                assertTrue(all.length >= 1);
182:                for (InetAddress alias : all) {
183:                    // Check that each alias has the same hostname. Intentionally not
184:                    // checking for exact string match.
185:                    assertTrue(alias.getHostName().startsWith(
186:                            Support_Configuration.SpecialInetTestAddress));
187:                }// end for all aliases
188:
189:                // check the getByName if there is a security manager.
190:                SecurityManager oldman = System.getSecurityManager();
191:                System.setSecurityManager(new MockSecurityManager());
192:                try {
193:                    boolean exception = false;
194:                    try {
195:                        InetAddress.getByName("3d.com");
196:                    } catch (SecurityException ex) {
197:                        exception = true;
198:                    }
199:                    assertTrue("expected SecurityException", exception);
200:                } finally {
201:                    System.setSecurityManager(oldman);
202:                }
203:
204:                //Regression for HARMONY-56
205:                InetAddress[] ia = InetAddress.getAllByName(null);
206:                assertEquals("Assert 0: No loopback address", 1, ia.length);
207:                assertTrue("Assert 1: getAllByName(null) not loopback", ia[0]
208:                        .isLoopbackAddress());
209:
210:                ia = InetAddress.getAllByName("");
211:                assertEquals("Assert 2: No loopback address", 1, ia.length);
212:                assertTrue("Assert 3: getAllByName(\"\") not loopback", ia[0]
213:                        .isLoopbackAddress());
214:            }
215:
216:            /**
217:             * @tests java.net.InetAddress#getByName(java.lang.String)
218:             */
219:            public void test_getByNameLjava_lang_String() throws Exception {
220:                // Test for method java.net.InetAddress
221:                // java.net.InetAddress.getByName(java.lang.String)
222:                InetAddress ia2 = InetAddress
223:                        .getByName(Support_Configuration.InetTestIP);
224:
225:                // Intentionally not testing for exact string match
226:                /* FIXME: comment the assertion below because it is platform/configuration dependent
227:                 * Please refer to HARMONY-1664 (https://issues.apache.org/jira/browse/HARMONY-1664)
228:                 * for details
229:                 */
230:                //        assertTrue(
231:                //      "Expected " + Support_Configuration.InetTestAddress + "*",
232:                //          ia2.getHostName().startsWith(Support_Configuration.InetTestAddress));
233:                // TODO : Test to ensure all the address formats are recognized
234:                InetAddress i = InetAddress.getByName("1.2.3");
235:                assertEquals("1.2.0.3", i.getHostAddress());
236:                i = InetAddress.getByName("1.2");
237:                assertEquals("1.0.0.2", i.getHostAddress());
238:                i = InetAddress.getByName(String.valueOf(0xffffffffL));
239:                assertEquals("255.255.255.255", i.getHostAddress());
240:                String s = "222.222.222.222....";
241:                i = InetAddress.getByName(s);
242:                assertEquals("222.222.222.222", i.getHostAddress());
243:            }
244:
245:            /**
246:             * @tests java.net.InetAddress#getHostAddress()
247:             */
248:            public void test_getHostAddress() throws Exception {
249:                // Test for method java.lang.String
250:                // java.net.InetAddress.getHostAddress()
251:                InetAddress ia2 = InetAddress
252:                        .getByName(Support_Configuration.InetTestAddress);
253:                assertTrue("getHostAddress returned incorrect result: "
254:                        + ia2.getHostAddress() + " != "
255:                        + Support_Configuration.InetTestIP, ia2
256:                        .getHostAddress().equals(
257:                                Support_Configuration.InetTestIP));
258:            }
259:
260:            /**
261:             * @tests java.net.InetAddress#getHostName()
262:             */
263:            public void test_getHostName() throws Exception {
264:                // Test for method java.lang.String java.net.InetAddress.getHostName()
265:                InetAddress ia = InetAddress
266:                        .getByName(Support_Configuration.InetTestIP);
267:
268:                // Intentionally not testing for exact string match
269:                /* FIXME: comment the assertion below because it is platform/configuration dependent
270:                 * Please refer to HARMONY-1664 (https://issues.apache.org/jira/browse/HARMONY-1664)
271:                 * for details
272:                 */
273:                //        assertTrue(
274:                //      "Expected " + Support_Configuration.InetTestAddress + "*",
275:                //      ia.getHostName().startsWith(Support_Configuration.InetTestAddress));
276:                // Test for any of the host lookups, where the default SecurityManager
277:                // is installed.
278:                SecurityManager oldman = System.getSecurityManager();
279:                try {
280:                    String exp = Support_Configuration.InetTestIP;
281:                    System.setSecurityManager(new MockSecurityManager());
282:                    ia = InetAddress.getByName(exp);
283:                    String ans = ia.getHostName();
284:                    /* FIXME: comment the assertion below because it is platform/configuration dependent
285:                     * Please refer to HARMONY-1664 (https://issues.apache.org/jira/browse/HARMONY-1664)
286:                     * for details
287:                     */
288:                    //    assertEquals(Support_Configuration.InetTestIP, ans);
289:                } finally {
290:                    System.setSecurityManager(oldman);
291:                }
292:
293:                // Make sure there is no caching
294:                String originalPropertyValue = System
295:                        .getProperty("networkaddress.cache.ttl");
296:                System.setProperty("networkaddress.cache.ttl", "0");
297:
298:                // Test for threadsafety
299:                try {
300:                    InetAddress lookup1 = InetAddress
301:                            .getByName(Support_Configuration.InetTestAddress);
302:                    assertTrue(lookup1 + " expected "
303:                            + Support_Configuration.InetTestIP,
304:                            Support_Configuration.InetTestIP.equals(lookup1
305:                                    .getHostAddress()));
306:                    InetAddress lookup2 = InetAddress
307:                            .getByName(Support_Configuration.InetTestAddress2);
308:                    assertTrue(lookup2 + " expected "
309:                            + Support_Configuration.InetTestIP2,
310:                            Support_Configuration.InetTestIP2.equals(lookup2
311:                                    .getHostAddress()));
312:                    threadsafeTestThread thread1 = new threadsafeTestThread(
313:                            "1", lookup1.getHostName(), lookup1, 0);
314:                    threadsafeTestThread thread2 = new threadsafeTestThread(
315:                            "2", lookup2.getHostName(), lookup2, 0);
316:                    threadsafeTestThread thread3 = new threadsafeTestThread(
317:                            "3", lookup1.getHostAddress(), lookup1, 1);
318:                    threadsafeTestThread thread4 = new threadsafeTestThread(
319:                            "4", lookup2.getHostAddress(), lookup2, 1);
320:
321:                    // initialize the flags
322:                    threadedTestSucceeded = true;
323:                    synchronized (someoneDone) {
324:                        thread1.start();
325:                        thread2.start();
326:                        thread3.start();
327:                        thread4.start();
328:                    }
329:                    thread1.join();
330:                    thread2.join();
331:                    thread3.join();
332:                    thread4.join();
333:                    /* FIXME: comment the assertion below because it is platform/configuration dependent
334:                     * Please refer to HARMONY-1664 (https://issues.apache.org/jira/browse/HARMONY-1664)
335:                     * for details
336:                     */
337:                    //            assertTrue(threadedTestErrorString, threadedTestSucceeded);
338:                } finally {
339:                    // restore the old value of the property
340:                    if (originalPropertyValue == null)
341:                        // setting the property to -1 has the same effect as having the
342:                        // property be null
343:                        System.setProperty("networkaddress.cache.ttl", "-1");
344:                    else
345:                        System.setProperty("networkaddress.cache.ttl",
346:                                originalPropertyValue);
347:                }
348:            }
349:
350:            /**
351:             * @tests java.net.InetAddress#getLocalHost()
352:             */
353:            public void test_getLocalHost() throws Exception {
354:                // Test for method java.net.InetAddress
355:                // java.net.InetAddress.getLocalHost()
356:
357:                // We don't know the host name or ip of the machine
358:                // running the test, so we can't build our own address
359:                DatagramSocket dg = new DatagramSocket(0, InetAddress
360:                        .getLocalHost());
361:                assertTrue("Incorrect host returned", InetAddress
362:                        .getLocalHost().equals(dg.getLocalAddress()));
363:                dg.close();
364:            }
365:
366:            /**
367:             * @tests java.net.InetAddress#hashCode()
368:             */
369:            public void test_hashCode() {
370:                // Test for method int java.net.InetAddress.hashCode()
371:                try {
372:                    InetAddress host = InetAddress
373:                            .getByName(Support_Configuration.InetTestAddress);
374:                    int hashcode = host.hashCode();
375:                    assertTrue("Incorrect hash returned: " + hashcode
376:                            + " from host: " + host,
377:                            hashcode == Support_Configuration.InetTestHashcode);
378:                } catch (java.net.UnknownHostException e) {
379:                    fail("Exception during test : " + e.getMessage());
380:                }
381:            }
382:
383:            /**
384:             * @tests java.net.InetAddress#isMulticastAddress()
385:             */
386:            public void test_isMulticastAddress() throws UnknownHostException {
387:                InetAddress ia2 = InetAddress.getByName("239.255.255.255");
388:                assertTrue(ia2.isMulticastAddress());
389:                ia2 = InetAddress.getByName("localhost");
390:                assertFalse(ia2.isMulticastAddress());
391:            }
392:
393:            /**
394:             * @tests java.net.InetAddress#isAnyLocalAddress()
395:             */
396:            public void test_isAnyLocalAddress() throws UnknownHostException {
397:                InetAddress ia2 = InetAddress.getByName("239.255.255.255");
398:                assertFalse(ia2.isAnyLocalAddress());
399:                ia2 = InetAddress.getByName("localhost");
400:                assertFalse(ia2.isAnyLocalAddress());
401:            }
402:
403:            /**
404:             * @tests java.net.InetAddress#isLinkLocalAddress()
405:             */
406:            public void test_isLinkLocalAddress() throws UnknownHostException {
407:                InetAddress ia2 = InetAddress.getByName("239.255.255.255");
408:                assertFalse(ia2.isLinkLocalAddress());
409:                ia2 = InetAddress.getByName("localhost");
410:                assertFalse(ia2.isLinkLocalAddress());
411:            }
412:
413:            /**
414:             * @tests java.net.InetAddress#isLoopbackAddress()
415:             */
416:            public void test_isLoopbackAddress() throws UnknownHostException {
417:                InetAddress ia2 = InetAddress.getByName("239.255.255.255");
418:                assertFalse(ia2.isLoopbackAddress());
419:                ia2 = InetAddress.getByName("localhost");
420:                assertTrue(ia2.isLoopbackAddress());
421:                ia2 = InetAddress.getByName("127.0.0.2");
422:                assertTrue(ia2.isLoopbackAddress());
423:            }
424:
425:            /**
426:             * @tests java.net.InetAddress#isLoopbackAddress()
427:             */
428:            public void test_isSiteLocalAddress() throws UnknownHostException {
429:                InetAddress ia2 = InetAddress.getByName("239.255.255.255");
430:                assertFalse(ia2.isSiteLocalAddress());
431:                ia2 = InetAddress.getByName("localhost");
432:                assertFalse(ia2.isSiteLocalAddress());
433:                ia2 = InetAddress.getByName("127.0.0.2");
434:                assertFalse(ia2.isSiteLocalAddress());
435:                ia2 = InetAddress.getByName("243.243.45.3");
436:                assertFalse(ia2.isSiteLocalAddress());
437:                ia2 = InetAddress.getByName("10.0.0.2");
438:                assertTrue(ia2.isSiteLocalAddress());
439:            }
440:
441:            /**
442:             * @tests java.net.InetAddress#isMCGlobal()/isMCLinkLocal/isMCNodeLocal/isMCOrgLocal/isMCSiteLocal
443:             */
444:            public void test_isMCVerify() throws UnknownHostException {
445:                InetAddress ia2 = InetAddress.getByName("239.255.255.255");
446:                assertFalse(ia2.isMCGlobal());
447:                assertFalse(ia2.isMCLinkLocal());
448:                assertFalse(ia2.isMCNodeLocal());
449:                assertFalse(ia2.isMCOrgLocal());
450:                assertTrue(ia2.isMCSiteLocal());
451:                ia2 = InetAddress.getByName("243.243.45.3");
452:                assertFalse(ia2.isMCGlobal());
453:                assertFalse(ia2.isMCLinkLocal());
454:                assertFalse(ia2.isMCNodeLocal());
455:                assertFalse(ia2.isMCOrgLocal());
456:                assertFalse(ia2.isMCSiteLocal());
457:                ia2 = InetAddress.getByName("250.255.255.254");
458:                assertFalse(ia2.isMCGlobal());
459:                assertFalse(ia2.isMCLinkLocal());
460:                assertFalse(ia2.isMCNodeLocal());
461:                assertFalse(ia2.isMCOrgLocal());
462:                assertFalse(ia2.isMCSiteLocal());
463:                ia2 = InetAddress.getByName("10.0.0.2");
464:                assertFalse(ia2.isMCGlobal());
465:                assertFalse(ia2.isMCLinkLocal());
466:                assertFalse(ia2.isMCNodeLocal());
467:                assertFalse(ia2.isMCOrgLocal());
468:                assertFalse(ia2.isMCSiteLocal());
469:            }
470:
471:            /**
472:             * @tests java.net.InetAddress#toString()
473:             */
474:            public void test_toString() throws Exception {
475:                // Test for method java.lang.String java.net.InetAddress.toString()
476:                InetAddress ia2 = InetAddress
477:                        .getByName(Support_Configuration.InetTestIP);
478:                assertEquals("/" + Support_Configuration.InetTestIP, ia2
479:                        .toString());
480:                // Regression for HARMONY-84
481:                InetAddress addr = InetAddress.getByName("localhost");
482:                assertEquals("Assert 0: wrong string from name",
483:                        "localhost/127.0.0.1", addr.toString());
484:                InetAddress addr2 = InetAddress.getByAddress(new byte[] { 127,
485:                        0, 0, 1 });
486:                assertEquals("Assert 1: wrong string from address",
487:                        "/127.0.0.1", addr2.toString());
488:            }
489:
490:            /**
491:             * @tests java.net.InetAddress#getByAddress(java.lang.String, byte[])
492:             */
493:            public void test_getByAddressLjava_lang_String$B() {
494:                // Check an IPv4 address with an IPv6 hostname
495:                byte ipAddress[] = { 127, 0, 0, 1 };
496:                String addressStr = "::1";
497:                try {
498:                    InetAddress addr = InetAddress.getByAddress(addressStr,
499:                            ipAddress);
500:                    addr = InetAddress.getByAddress(ipAddress);
501:                } catch (UnknownHostException e) {
502:                    fail("Unexpected problem creating IP Address "
503:                            + ipAddress.length);
504:                }
505:
506:                byte ipAddress2[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1,
507:                        127, 0, 0, 1 };
508:                addressStr = "::1";
509:                try {
510:                    InetAddress addr = InetAddress.getByAddress(addressStr,
511:                            ipAddress2);
512:                    addr = InetAddress.getByAddress(ipAddress);
513:                } catch (UnknownHostException e) {
514:                    fail("Unexpected problem creating IP Address "
515:                            + ipAddress.length);
516:                }
517:            }
518:
519:            /**
520:             * @tests java.net.InetAddress#getCanonicalHostName()
521:             */
522:            public void test_getCanonicalHostName() throws Exception {
523:                InetAddress theAddress = null;
524:                theAddress = InetAddress.getLocalHost();
525:                assertTrue(
526:                        "getCanonicalHostName returned a zero length string ",
527:                        theAddress.getCanonicalHostName().length() != 0);
528:                assertTrue("getCanonicalHostName returned an empty string ",
529:                        !theAddress.equals(""));
530:
531:                // test against an expected value
532:                InetAddress ia = InetAddress
533:                        .getByName(Support_Configuration.InetTestIP);
534:
535:                // Intentionally not testing for exact string match
536:                /* FIXME: comment the assertion below because it is platform/configuration dependent
537:                 * Please refer to HARMONY-1664 (https://issues.apache.org/jira/browse/HARMONY-1664)
538:                 * for details
539:                 */
540:                //        assertTrue(
541:                //           "Expected " + Support_Configuration.InetTestAddress + "*", 
542:                //           ia.getCanonicalHostName().startsWith(Support_Configuration.InetTestAddress));
543:            }
544:
545:            /**
546:             * @tests java.net.InetAddress#isReachableI
547:             */
548:            public void test_isReachableI() throws Exception {
549:                InetAddress ia = Inet4Address.getByName("127.0.0.1");
550:                assertTrue(ia.isReachable(10000));
551:                ia = Inet4Address.getByName("127.0.0.1");
552:                try {
553:                    ia.isReachable(-1);
554:                    fail("Should throw IllegalArgumentException");
555:                } catch (IllegalArgumentException e) {
556:                    // correct
557:                }
558:            }
559:
560:            /**
561:             * @tests java.net.InetAddress#isReachableLjava_net_NetworkInterfaceII
562:             */
563:            public void test_isReachableLjava_net_NetworkInterfaceII()
564:                    throws Exception {
565:                // tests local address
566:                InetAddress ia = Inet4Address.getByName("127.0.0.1");
567:                assertTrue(ia.isReachable(null, 0, 10000));
568:                ia = Inet4Address.getByName("127.0.0.1");
569:                try {
570:                    ia.isReachable(null, -1, 10000);
571:                    fail("Should throw IllegalArgumentException");
572:                } catch (IllegalArgumentException e) {
573:                    // correct
574:                }
575:                try {
576:                    ia.isReachable(null, 0, -1);
577:                    fail("Should throw IllegalArgumentException");
578:                } catch (IllegalArgumentException e) {
579:                    // correct
580:                }
581:                try {
582:                    ia.isReachable(null, -1, -1);
583:                    fail("Should throw IllegalArgumentException");
584:                } catch (IllegalArgumentException e) {
585:                    // correct
586:                }
587:                // tests nowhere
588:                ia = Inet4Address.getByName("1.1.1.1");
589:                assertFalse(ia.isReachable(1000));
590:                assertFalse(ia.isReachable(null, 0, 1000));
591:
592:                // Regression test for HARMONY-1842.
593:                ia = InetAddress.getByName("localhost"); //$NON-NLS-1$
594:                Enumeration<NetworkInterface> nif = NetworkInterface
595:                        .getNetworkInterfaces();
596:                NetworkInterface netif;
597:                while (nif.hasMoreElements()) {
598:                    netif = nif.nextElement();
599:                    ia.isReachable(netif, 10, 1000);
600:                }
601:            }
602:
603:            // comparator for InetAddress objects
604:            private static final SerializableAssert COMPARATOR = new SerializableAssert() {
605:                public void assertDeserialized(Serializable initial,
606:                        Serializable deserialized) {
607:
608:                    InetAddress initAddr = (InetAddress) initial;
609:                    InetAddress desrAddr = (InetAddress) deserialized;
610:
611:                    byte[] iaAddresss = initAddr.getAddress();
612:                    byte[] deIAAddresss = desrAddr.getAddress();
613:                    for (int i = 0; i < iaAddresss.length; i++) {
614:                        assertEquals(iaAddresss[i], deIAAddresss[i]);
615:                    }
616:                    assertEquals(initAddr.getHostName(), desrAddr.getHostName());
617:                }
618:            };
619:
620:            // Regression Test for Harmony-2290
621:            public void test_isReachableLjava_net_NetworkInterfaceII_loopbackInterface()
622:                    throws IOException {
623:                final int TTL = 20;
624:                final int TIME_OUT = 3000;
625:
626:                NetworkInterface loopbackInterface = null;
627:                ArrayList<InetAddress> localAddresses = new ArrayList<InetAddress>();
628:                Enumeration<NetworkInterface> networkInterfaces = NetworkInterface
629:                        .getNetworkInterfaces();
630:                while (networkInterfaces.hasMoreElements()) {
631:                    NetworkInterface networkInterface = networkInterfaces
632:                            .nextElement();
633:                    Enumeration<InetAddress> addresses = networkInterface
634:                            .getInetAddresses();
635:                    while (addresses.hasMoreElements()) {
636:                        InetAddress address = addresses.nextElement();
637:                        if (address.isLoopbackAddress()) {
638:                            loopbackInterface = networkInterface;
639:                        } else {
640:                            localAddresses.add(address);
641:                        }
642:                    }
643:                }
644:
645:                //loopbackInterface can reach local address
646:                if (null != loopbackInterface) {
647:                    for (InetAddress destAddress : localAddresses) {
648:                        assertTrue(destAddress.isReachable(loopbackInterface,
649:                                TTL, TIME_OUT));
650:                    }
651:                }
652:
653:                //loopback Interface cannot reach outside address
654:                InetAddress destAddress = InetAddress
655:                        .getByName("www.google.com");
656:                assertFalse(destAddress.isReachable(loopbackInterface, TTL,
657:                        TIME_OUT));
658:            }
659:
660:            /**
661:             * @tests serialization/deserialization compatibility.
662:             */
663:            public void testSerializationSelf() throws Exception {
664:
665:                SerializationTest.verifySelf(
666:                        InetAddress.getByName("localhost"), COMPARATOR);
667:            }
668:
669:            /**
670:             * @tests serialization/deserialization compatibility with RI.
671:             */
672:            public void testSerializationCompatibility() throws Exception {
673:
674:                SerializationTest.verifyGolden(this , InetAddress
675:                        .getByName("localhost"), COMPARATOR);
676:            }
677:
678:            /**
679:             * @tests java.net.InetAddress#getByAddress(byte[])
680:             */
681:            public void test_getByAddress() {
682:                // Regression for HARMONY-61
683:                try {
684:                    InetAddress.getByAddress(null);
685:                    fail("Assert 0: UnknownHostException must be thrown");
686:                } catch (UnknownHostException e) {
687:                    // Expected
688:                }
689:            }
690:
691:            class MockSecurityManager extends SecurityManager {
692:                public void checkPermission(Permission permission) {
693:                    if (permission.getName().equals("setSecurityManager")) {
694:                        return;
695:                    }
696:                    if (permission.getName().equals("3d.com")) {
697:                        throw new SecurityException();
698:                    }
699:                    super.checkPermission(permission);
700:                }
701:            }
702:        }
w___w_w__._j__a_v__a___2___s_.__c__om__ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.