When SecurityManager doesn't allow the "lookup" operation, it returns
"localhost" instead of "127.0.0.1".  Otherwise, getHostName method will look
up the real hostname.


On 2/9/06, Geir Magnusson Jr <[EMAIL PROTECTED]> 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();
> >>     }
> >> }
> >
>

Reply via email to