harmony-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Geir Magnusson Jr <g...@pobox.com>
Subject Re: [jira] Updated: (HARMONY-73) java.net.InetAddress.getLocalHost() returns wrong host name for loopback address
Date Fri, 10 Feb 2006 12:14:06 GMT
Ok.

<aside>
The behavior strikes me as dumb.  Why return a textual representation? 
What good does that do anyone?  The caller has no clue there was a 
problem...
</aside>

geir

Paulex Yang wrote:
> As I commented in the JIRA, further study shows some interesting facts,
> 
> first, my prior patch is redundant, the actual bug is 
> http://issues.apache.org/jira/browse/HARMONY-84.
> 
> second, there is another different behavior of getHostName() between RI 
> and Harmony, but I consider it as RI's bug:
> 
> The java spec of getHostName() says:
> <spec>*
> Returns: *the host name for this IP address, or if the operation is not 
> allowed by the security check, the textual representation of the IP 
> address.
> </spec>
> 
> But the following test shows RI will return host name instead of IP 
> address if the host name can be got without reverse name lookup.
> 
> <code>
> public class HostNameTest{
>    public static void main(String[] args) throws Exception{
>        InetAddress addr = InetAddress.getByName("localhost");
>        InetAddress addr2 = InetAddress.getByAddress(new byte[]{127, 0, 
> 0, 1});
>        SecurityManager sm = new Inet_SecurityManager();
>        System.setSecurityManager(sm);
>        System.out.println(addr.getHostName());
>        System.out.println(addr2.getHostName());
>    }
>      static class Inet_SecurityManager extends SecurityManager {
>        public void checkConnect(String host, int port) {
>         super.checkConnect(host,port);
>        throw new SecurityException();
>        }
>    }
> }
> </code>
> 
> RI outputs:
> localhost
> 127.0.0.1
> 
> Harmony outputs:
> 127.0.0.1
> 127.0.0.1
> 
> This time, Harmony win :-D .
> 
> Geir Magnusson Jr wrote:
>> isn't "localhost" actually something in the local hosts file?  Don't 
>> you want to look this up?
>>
>> Paulex Yang (JIRA) wrote:
>>>      [ http://issues.apache.org/jira/browse/HARMONY-73?page=all ]
>>>
>>> Paulex Yang updated HARMONY-73:
>>> -------------------------------
>>>
>>>     Attachment: InetAddress.patch
>>>
>>> A suggested modification is to add these two lines before Ln. 296 of 
>>> java.net.InetAddress,
>>> if(this == LOOPBACK){
>>>      return "localhost";
>>> }
>>>
>>> So that the getHostName() will return "localhost" instead of 
>>> "127.0.0.1".
>>>
>>> The patch is attached.
>>>>  java.net.InetAddress.getLocalHost() returns wrong host name for 
>>>> loopback address
>>>> ---------------------------------------------------------------------------------

>>>>
>>>>
>>>>          Key: HARMONY-73
>>>>          URL: http://issues.apache.org/jira/browse/HARMONY-73
>>>>      Project: Harmony
>>>>         Type: Bug
>>>>   Components: Classlib
>>>>     Reporter: Svetlana Samoilenko
>>>>     Priority: Minor
>>>>  Attachments: InetAddress.patch
>>>>
>>>> J2se 1.4.2 and 5.0 specifications for 
>>>> java.net.InetAddress.getLocalHost() read, that if there is a 
>>>> security manager, its checkConnect method is called with the local 
>>>> host name and -1 as its arguments to see if the operation is 
>>>> allowed. If the operation is not allowed, an InetAddress 
>>>> representing the loopback address is returned as 
>>>> hostname/hostaddress (as followed from toString() specification).
>>>> The test listed below shows that the returned loopback address has 
>>>> wrong hostname, "127.0.0.1" instead of "loopback".
>>>> Inet_SecurityManager class is called twice and therefore host name 
>>>> is substituted with hostaddress address.
>>>> Code to reproduce: import java.net.*; public class test2 {     
>>>> public static void main(String[] args) {         try {             
>>>> System.setSecurityManager(new Inet_SecurityManager());
>>>>             System.out.println("Loopback address = " + 
>>>> InetAddress.getLocalHost());
>>>>         } catch (Exception e){             
>>>> System.out.println("Unexpected exception = " + e);         };     } 
>>>> } class Inet_SecurityManager extends SecurityManager {     public 
>>>> void checkConnect(String host, int port) {         
>>>> super.checkConnect(host,port);         throw new SecurityException();
>>>>     }
>>>> }
>>>> Steps to Reproduce: 1. Build Harmony (check-out on 2006-01-30) j2se 
>>>> subset as described in README.txt. 2. Compile test2.java using BEA 
>>>> 1.4 javac
>>>>> javac -d . test2.java 
>>>> 3. Run java using compatible VM (J9)
>>>>> java -showversion test2 
>>>> Output: C:\tmp>C:\jrockit-j2sdk1.4.2_04\bin\java.exe -showversion 
>>>> test2 java version "1.4.2_04" Java(TM) 2 Runtime Environment, 
>>>> Standard Edition (build 1.4.2_04-b05) BEA WebLogic JRockit(TM) 
>>>> 1.4.2_04 JVM (build ari-31788-20040616-1132-win-ia32, Native 
>>>> Threads, GC strategy: parallel) Inet_SecurityManager :host= nswssamoil1
>>>> Inet_SecurityManager :port= -1
>>>> Loopback address = localhost/127.0.0.1
>>>> C:\tmp>C:\harmony\trunk\deploy\jre\bin\java -showversion test2 (c) 
>>>> Copyright 1991, 2005 The Apache Software Foundation or its 
>>>> licensors, as applicable. Inet_SecurityManager :host= nswssamoil1
>>>> Inet_SecurityManager :port= -1
>>>> Inet_SecurityManager :host= localhost
>>>> Inet_SecurityManager :port= -1
>>>> Loopback address = 127.0.0.1/127.0.0.1
>>>> Suggested junit test case:
>>>> ------------------------ InetAddressTest.java 
>>>> ------------------------------------------------- import java.net.*; 
>>>> import junit.framework.*; public class InetAddressTest extends 
>>>> TestCase {     public static void main(String[] args) {         
>>>> junit.textui.TestRunner.run(InetAddress.class);     }     public 
>>>> void test_getLocalHost() {         try{                        
>>>> System.setSecurityManager(new Inet_SecurityManager());             
>>>> String hostname=InetAddress.getLocalHost().getHostName();           
>>>>             assertEquals("localhost", hostname);
>>>>         } catch (Exception e){             fail("Unexpected 
>>>> exception = " + e);         };              } }
>>>> class Inet_SecurityManager extends SecurityManager {     public void 
>>>> checkConnect(String host, int port) {         
>>>> super.checkConnect(host,port);         throw new SecurityException();
>>>>     }
>>>> } 
>>>
>>
> 
> 

Mime
View raw message