harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ndbe...@apache.org
Subject svn commit: r529472 - in /harmony/enhanced/classlib/trunk/modules/luni/src: main/java/org/apache/harmony/luni/net/PlainSocketImpl.java test/java/tests/api/java/net/SocketTest.java
Date Tue, 17 Apr 2007 02:30:12 GMT
Author: ndbeyer
Date: Mon Apr 16 19:30:11 2007
New Revision: 529472

URL: http://svn.apache.org/viewvc?view=rev&rev=529472
Log:
Apply patch for HARMONY-2503 - [classlib][net]ServerSocket refuses connection on 0.0.0.0 after
successful connection on localhost

Modified:
    harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/PlainSocketImpl.java
    harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/SocketTest.java

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/PlainSocketImpl.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/PlainSocketImpl.java?view=diff&rev=529472&r1=529471&r2=529472
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/PlainSocketImpl.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/PlainSocketImpl.java
Mon Apr 16 19:30:11 2007
@@ -207,9 +207,8 @@
      */
     private void connect(InetAddress anAddr, int aPort, int timeout)
             throws IOException {
-        InetAddress address = anAddr.isAnyLocalAddress() ? InetAddress
-                .getByName("localhost") //$NON-NLS-1$
-                : anAddr;
+        InetAddress normalAddr = anAddr.isAnyLocalAddress() ? InetAddress
+                .getLocalHost() : anAddr;
 
         try {
             if (streaming) {
@@ -217,20 +216,20 @@
                     socksConnect(anAddr, aPort, 0);
                 } else {
                     if (timeout == 0) {
-                        netImpl.connect(fd, trafficClass, address, aPort);
+                        netImpl.connect(fd, trafficClass, normalAddr, aPort);
                     } else {
                         netImpl.connectStreamWithTimeoutSocket(fd, aPort,
-                                timeout, trafficClass, address);
+                                timeout, trafficClass, normalAddr);
                     }
                 }
             } else {
-            	netImpl.connectDatagram(fd, aPort, trafficClass, address);
+            	netImpl.connectDatagram(fd, aPort, trafficClass, normalAddr);
             }
         } catch (ConnectException e) {
             throw new ConnectException(anAddr + ":" + aPort + " - "
                     + e.getMessage());
         }
-        super.address = address;
+        super.address = normalAddr;
         super.port = aPort;
     }
 

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/SocketTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/SocketTest.java?view=diff&rev=529472&r1=529471&r2=529472
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/SocketTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/SocketTest.java
Mon Apr 16 19:30:11 2007
@@ -48,6 +48,12 @@
 	Thread t;
 
 	boolean interrupted = false;
+	
+	private static final int port = 8080;
+
+	private static String host = "localhost";
+    
+    private static String failureMsg = null;
 
 	class SServer extends Thread implements Runnable {
 		Socket s1 = null;
@@ -73,6 +79,91 @@
 			}
 		}
 	}
+	
+	static class ServerThread implements Runnable {
+		public boolean ready = false;
+
+		private int serverSocketConstructor = 0;
+		
+		private static final int FIRST_TIME = 1;
+
+		private static final int SECOND_TIME = 2;
+		
+		private int backlog = 10;
+
+		public void run() {
+			try {
+
+				ServerSocket socket = null;
+				switch (serverSocketConstructor) {
+				case FIRST_TIME:
+					socket = new ServerSocket(port, backlog,
+							new InetSocketAddress(host, port).getAddress());
+					break;
+				case SECOND_TIME:
+					socket = new ServerSocket(port, backlog);
+					host = socket.getInetAddress().getHostName();
+					break;
+				default:
+					socket = new ServerSocket();
+					break;
+				}
+
+				synchronized (this) {
+					ready = true;
+					this.notifyAll();
+				}
+
+				socket.setSoTimeout(5000);
+				socket.accept();				
+
+				socket.close();
+			} catch (IOException e) {
+				e.printStackTrace();
+			} catch (Throwable e) {
+				e.printStackTrace();
+			}
+		}
+
+		public synchronized void waitCreated() throws Exception{
+			while (!ready) {				
+				this.wait();
+			}
+		}
+	}
+	
+	static class ClientThread implements Runnable {
+
+		public void run() {
+			try {
+				Socket socket = new Socket();
+				InetSocketAddress addr = new InetSocketAddress(host, port);				
+				socket.connect(addr);
+
+				socket.close();
+			}catch (Exception e) {
+                failureMsg = e.getMessage();
+			} 
+		}
+	}
+	
+	private static void connectTestImpl(int ssConsType) throws Exception {
+		ServerThread server = new ServerThread();
+		server.serverSocketConstructor = ssConsType;
+		Thread serverThread = new Thread(server);
+		serverThread.start();
+		server.waitCreated();
+
+		ClientThread client = new ClientThread();
+		Thread clientThread = new Thread(client);
+		clientThread.start();
+		try {
+			serverThread.join();
+			clientThread.join();
+		} catch (InterruptedException e) {
+			e.printStackTrace();
+		}
+	}	
 
 	/**
 	 * @tests java.net.Socket#Socket()
@@ -2446,6 +2537,19 @@
             fail("Should throw UnknownHostException");
         } catch (UnknownHostException e) {
             // expected
+        }
+    }
+    
+    /**
+     * Regression for Harmony-2503 
+     */
+    public void test_connectLjava_net_SocketAddress_AnyAddress()
+            throws Exception {
+        connectTestImpl(ServerThread.FIRST_TIME);
+        connectTestImpl(ServerThread.SECOND_TIME);
+        if (failureMsg != null) {
+            failureMsg = null;
+            fail(failureMsg);
         }
     }
     



Mime
View raw message