harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lian...@apache.org
Subject svn commit: r480867 - in /harmony/enhanced/classlib/trunk/modules/luni/src: main/java/java/net/InetAddress.java test/java/org/apache/harmony/luni/tests/java/net/InetAddressTest.java
Date Thu, 30 Nov 2006 07:30:30 GMT
Author: liangyx
Date: Wed Nov 29 23:30:28 2006
New Revision: 480867

URL: http://svn.apache.org/viewvc?view=rev&rev=480867
Log:
Apply patch for HARMONY-2290 ([classlib][luni]InetAddress.isReachable(NetworkInterface,int,int)
throws NullPointerExeption when the networkInterface is not bind to a network address)

Modified:
    harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/InetAddress.java
    harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/net/InetAddressTest.java

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/InetAddress.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/InetAddress.java?view=diff&rev=480867&r1=480866&r2=480867
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/InetAddress.java (original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/InetAddress.java Wed
Nov 29 23:30:28 2006
@@ -792,6 +792,10 @@
                 reachable = isReachableByTCP(this, null, timeout);
             }
         } else {
+            //Not Bind to any address
+            if (null == netif.addresses) {
+                return false;
+            }                               
             // binds to all address on this NetworkInterface, tries ICMP ping
             // first
             reachable = isReachableByICMPUseMultiThread(netif, ttl, timeout);
@@ -813,20 +817,42 @@
         Enumeration<InetAddress> addresses = netif.getInetAddresses();
         reached = false;
         addrCount = netif.addresses.length;
+        boolean needWait = false;
         while (addresses.hasMoreElements()) {
             final InetAddress addr = addresses.nextElement();
+            
+            //loopback interface can only reach to local addresses
+            if(addr.isLoopbackAddress())
+            {
+                Enumeration<NetworkInterface> NetworkInterfaces = NetworkInterface.getNetworkInterfaces();
+                while(NetworkInterfaces.hasMoreElements())
+                {
+                    NetworkInterface networkInterface = NetworkInterfaces.nextElement();
+                    Enumeration<InetAddress> localAddresses = networkInterface.getInetAddresses();
+                    while(localAddresses.hasMoreElements())
+                    {
+                        if(InetAddress.this.equals(localAddresses.nextElement()))
+                        {
+                            return true;
+                        }
+                    }                    
+                }
+                continue;
+            }
+            
+            needWait = true;
             new Thread() {
                 @Override
                 public void run() {
                     boolean threadReached = false;
                     // if isICMP, tries ICMP ping, else TCP echo
                     if (isICMP) {
-                        threadReached = NETIMPL.isReachableByICMP(addr,
-                                InetAddress.this, ttl, timeout);
+                        threadReached = NETIMPL.isReachableByICMP(
+                                InetAddress.this, addr, ttl, timeout);
                     } else {
                         try {
-                            threadReached = isReachableByTCP(addr,
-                                    InetAddress.this, timeout);
+                            threadReached = isReachableByTCP(
+                                    InetAddress.this, addr, timeout);
                         } catch (IOException e) {
                             // do nothing
                         }
@@ -850,14 +876,20 @@
                 }
             }.start();
         }
-        synchronized (waitReachable) {
-            try {
-                // wait for notification
-                waitReachable.wait();
-            } catch (InterruptedException e) {
-                // do nothing
+        
+        if (needWait) {
+            synchronized (waitReachable) {
+                try {
+                    // wait for notification
+                    waitReachable.wait();
+                } catch (InterruptedException e) {
+                    // do nothing
+                }
+                return reached;
             }
-            return reached;
+        }
+        else {
+            return false;
         }
     }    
 

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/net/InetAddressTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/net/InetAddressTest.java?view=diff&rev=480867&r1=480866&r2=480867
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/net/InetAddressTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/net/InetAddressTest.java
Wed Nov 29 23:30:28 2006
@@ -17,12 +17,16 @@
 
 package org.apache.harmony.luni.tests.java.net;
 
+import java.io.IOException;
 import java.io.Serializable;
 import java.net.DatagramSocket;
 import java.net.Inet4Address;
 import java.net.InetAddress;
+import java.net.NetworkInterface;
 import java.net.UnknownHostException;
 import java.security.Permission;
+import java.util.ArrayList;
+import java.util.Enumeration;
 
 import org.apache.harmony.testframework.serialization.SerializationTest;
 import org.apache.harmony.testframework.serialization.SerializationTest.SerializableAssert;
@@ -500,6 +504,41 @@
             assertEquals(initAddr.getHostName(), desrAddr.getHostName());
         }
     };
+    
+    // Regression Test for Harmony-2290
+    public void test_isReachableLjava_net_NetworkInterfaceII_loopbackInterface() throws IOException
{
+        final int TTL = 20;
+        final int TIME_OUT = 3000;
+        
+        NetworkInterface loopbackInterface = null;
+        ArrayList<InetAddress> localAddresses = new ArrayList<InetAddress>();
+        Enumeration<NetworkInterface> networkInterfaces = NetworkInterface
+                .getNetworkInterfaces();
+        while (networkInterfaces.hasMoreElements()) {
+            NetworkInterface networkInterface = networkInterfaces.nextElement();
+            Enumeration<InetAddress> addresses = networkInterface
+                    .getInetAddresses();
+            while (addresses.hasMoreElements()) {
+                InetAddress address = addresses.nextElement();
+                if (address.isLoopbackAddress()) {
+                    loopbackInterface = networkInterface;
+                } else {
+                    localAddresses.add(address);
+                }
+            }
+        }
+
+        //loopbackInterface can reach local address
+        if (null != loopbackInterface) {
+            for (InetAddress destAddress : localAddresses) {
+                assertTrue(destAddress.isReachable(loopbackInterface, TTL, TIME_OUT));
+            }
+        }
+
+        //loopback Interface cannot reach outside address
+        InetAddress destAddress = InetAddress.getByName("www.google.com");
+        assertFalse(destAddress.isReachable(loopbackInterface, TTL, TIME_OUT));
+    }
 
     /**
      * @tests serialization/deserialization compatibility.



Mime
View raw message