5067458: Loopback SSLSocketImpl createSocket is throwing an exception
authorxuelei
Mon, 23 Feb 2009 17:32:52 +0800
changeset 2068 cdbc5929b91e
parent 2067 6f9db5f305cd
child 2069 2cd4a0aa917f
5067458: Loopback SSLSocketImpl createSocket is throwing an exception Summary: A null hostname should be regarded as a loopback address. Reviewed-by: weijun
jdk/src/share/classes/sun/security/ssl/SSLSocketImpl.java
jdk/test/sun/security/ssl/com/sun/net/ssl/internal/ssl/SSLSocketImpl/LoopbackSSLSocket.java
--- a/jdk/src/share/classes/sun/security/ssl/SSLSocketImpl.java	Mon Feb 23 10:05:55 2009 +0800
+++ b/jdk/src/share/classes/sun/security/ssl/SSLSocketImpl.java	Mon Feb 23 17:32:52 2009 +0800
@@ -1,5 +1,5 @@
 /*
- * Copyright 1996-2008 Sun Microsystems, Inc.  All Rights Reserved.
+ * Copyright 1996-2009 Sun Microsystems, Inc.  All Rights Reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -368,7 +368,9 @@
         super();
         this.host = host;
         init(context, false);
-        SocketAddress socketAddress = new InetSocketAddress(host, port);
+        SocketAddress socketAddress =
+               host != null ? new InetSocketAddress(host, port) :
+               new InetSocketAddress(InetAddress.getByName(null), port);
         connect(socketAddress, 0);
     }
 
@@ -409,7 +411,9 @@
         this.host = host;
         init(context, false);
         bind(new InetSocketAddress(localAddr, localPort));
-        SocketAddress socketAddress = new InetSocketAddress(host, port);
+        SocketAddress socketAddress =
+               host != null ? new InetSocketAddress(host, port) :
+               new InetSocketAddress(InetAddress.getByName(null), port);
         connect(socketAddress, 0);
     }
 
@@ -1829,7 +1833,8 @@
     }
 
     synchronized String getHost() {
-        if (host == null) {
+        // Note that the host may be null or empty for localhost.
+        if (host == null || host.length() == 0) {
             host = getInetAddress().getHostName();
         }
         return host;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/sun/security/ssl/com/sun/net/ssl/internal/ssl/SSLSocketImpl/LoopbackSSLSocket.java	Mon Feb 23 17:32:52 2009 +0800
@@ -0,0 +1,50 @@
+/*
+ * Copyright 2009 Sun Microsystems, Inc.  All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test
+ * @bug 5067458
+ * @summary Loopback SSLSocketImpl createSocket is throwing an exception.
+ * @author Xuelei Fan
+ */
+
+import java.io.*;
+import java.net.*;
+
+import javax.net.ssl.*;
+
+public class LoopbackSSLSocket {
+
+    public static void main(String[] args) throws Exception {
+        SSLSocketFactory sf = (SSLSocketFactory)SSLSocketFactory.getDefault();
+        // we won't expect a IllegalArgumentException: hostname can't be null.
+        try {
+            SSLSocket s = (SSLSocket)sf.createSocket((String)null, 0);
+            s.close();
+        } catch (IOException ioe) {
+            // would catch a IOException because there is no listener on
+            // that socket.
+        }
+    }
+
+}