changeset 12324:cce0dee6d995

8129444: socksProxyVersion system property ignored for Socket(Proxy) Reviewed-by: chegar
author asmotrak
date Fri, 03 Jul 2015 08:00:33 +0100
parents f376824d4940
children 4124b1992789
files src/java.base/share/classes/java/net/SocksSocketImpl.java src/java.base/share/classes/sun/net/spi/DefaultProxySelector.java test/java/net/Socks/SocksProxyVersion.java
diffstat 3 files changed, 76 insertions(+), 51 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.base/share/classes/java/net/SocksSocketImpl.java	Thu Jul 02 16:08:47 2015 -0700
+++ b/src/java.base/share/classes/java/net/SocksSocketImpl.java	Fri Jul 03 08:00:33 2015 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2000, 2015, Oracle and/or its affiliates. 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
@@ -31,6 +31,7 @@
 import java.security.PrivilegedAction;
 import java.security.PrivilegedExceptionAction;
 import sun.net.SocksProxy;
+import sun.net.spi.DefaultProxySelector;
 import sun.net.www.ParseUtil;
 /* import org.ietf.jgss.*; */
 
@@ -69,12 +70,21 @@
             server = ad.getHostString();
             serverPort = ad.getPort();
         }
+        useV4 = useV4(proxy);
     }
 
     void setV4() {
         useV4 = true;
     }
 
+    private static boolean useV4(Proxy proxy) {
+        if (proxy instanceof SocksProxy
+            && ((SocksProxy)proxy).protocolVersion() == 4) {
+            return true;
+        }
+        return DefaultProxySelector.socksProxyVersion() == 4;
+    }
+
     private synchronized void privilegedConnect(final String host,
                                               final int port,
                                               final int timeout)
@@ -398,11 +408,7 @@
                 // Use getHostString() to avoid reverse lookups
                 server = ((InetSocketAddress) p.address()).getHostString();
                 serverPort = ((InetSocketAddress) p.address()).getPort();
-                if (p instanceof SocksProxy) {
-                    if (((SocksProxy)p).protocolVersion() == 4) {
-                        useV4 = true;
-                    }
-                }
+                useV4 = useV4(p);
 
                 // Connects to the SOCKS server
                 try {
@@ -715,11 +721,7 @@
                 // Use getHostString() to avoid reverse lookups
                 server = ((InetSocketAddress) p.address()).getHostString();
                 serverPort = ((InetSocketAddress) p.address()).getPort();
-                if (p instanceof SocksProxy) {
-                    if (((SocksProxy)p).protocolVersion() == 4) {
-                        useV4 = true;
-                    }
-                }
+                useV4 = useV4(p);
 
                 // Connects to the SOCKS server
                 try {
--- a/src/java.base/share/classes/sun/net/spi/DefaultProxySelector.java	Thu Jul 02 16:08:47 2015 -0700
+++ b/src/java.base/share/classes/sun/net/spi/DefaultProxySelector.java	Fri Jul 03 08:00:33 2015 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2003, 2015, Oracle and/or its affiliates. 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
@@ -106,6 +106,15 @@
         }
     }
 
+    public static int socksProxyVersion() {
+        return AccessController.doPrivileged(
+                new PrivilegedAction<Integer>() {
+                    @Override public Integer run() {
+                        return NetProperties.getInteger(SOCKS_PROXY_VERSION, 5);
+                    }
+                });
+    }
+
     /**
      * How to deal with "non proxy hosts":
      * since we do have to generate a pattern we don't want to do that if
@@ -302,8 +311,7 @@
                             saddr = InetSocketAddress.createUnresolved(phost, pport);
                             // Socks is *always* the last on the list.
                             if (j == (props[i].length - 1)) {
-                                int version = NetProperties.getInteger(SOCKS_PROXY_VERSION, 5).intValue();
-                                return SocksProxy.create(saddr, version);
+                                return SocksProxy.create(saddr, socksProxyVersion());
                             } else {
                                 return new Proxy(Proxy.Type.HTTP, saddr);
                             }
--- a/test/java/net/Socks/SocksProxyVersion.java	Thu Jul 02 16:08:47 2015 -0700
+++ b/test/java/net/Socks/SocksProxyVersion.java	Fri Jul 03 08:00:33 2015 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2011, 2015, Oracle and/or its affiliates. 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
@@ -23,7 +23,7 @@
 
 /*
  * @test
- * @bug 6964547 5001942
+ * @bug 6964547 5001942 8129444
  * @run main/othervm SocksProxyVersion
  * @summary test socksProxyVersion system property
  */
@@ -32,13 +32,15 @@
 import java.net.ServerSocket;
 import java.net.Socket;
 import java.net.SocketException;
-import java.io.DataInputStream;
 import java.io.IOException;
 import java.net.InetAddress;
+import java.net.Proxy;
 
 public class SocksProxyVersion implements Runnable {
     final ServerSocket ss;
     volatile boolean failed;
+    volatile boolean stopped = false;
+    volatile int expected;
 
     public static void main(String[] args) throws Exception {
         if (InetAddress.getLocalHost().isLoopbackAddress()) {
@@ -48,21 +50,26 @@
         new SocksProxyVersion();
     }
 
-    @SuppressWarnings("try")
     public SocksProxyVersion() throws Exception {
         ss = new ServerSocket(0);
+        int port = ss.getLocalPort();
+        Thread serverThread = new Thread(this);
+        serverThread.start();
         try (ServerSocket socket = ss) {
-            runTest();
+            runTest(port);
+        } finally {
+            stopped = true;
+        }
+
+        serverThread.join();
+        if (failed) {
+            throw new RuntimeException("socksProxyVersion not being set correctly");
         }
     }
 
-    void runTest() throws Exception {
-        int port = ss.getLocalPort();
-        Thread serverThread = new Thread(this);
-        serverThread.start();
-
+    final void runTest(int port) throws Exception {
         /*
-         * Retreving the IP Address of the machine
+         * Retrieving the IP Address of the machine
          * since "localhost" is bypassed as a non-proxy host
          */
         String addr = InetAddress.getLocalHost().getHostAddress();
@@ -70,46 +77,54 @@
         System.setProperty("socksProxyHost", addr);
         System.setProperty("socksProxyPort", Integer.toString(port));
 
+        Proxy proxy = new Proxy(Proxy.Type.SOCKS,
+                                new InetSocketAddress(addr, port));
+
         // SOCKS V4
         System.setProperty("socksProxyVersion", Integer.toString(4));
-        try (Socket s = new Socket()) {
-            s.connect(new InetSocketAddress(addr, port));
+        this.expected = 4;
+        check(new Socket(), addr, port);
+        check(new Socket(proxy), addr, port);
+
+        // SOCKS V5
+        System.setProperty("socksProxyVersion", Integer.toString(5));
+        this.expected = 5;
+        check(new Socket(), addr, port);
+        check(new Socket(proxy), addr, port);
+    }
+
+    private void check(Socket socket, String addr, int port)
+        throws IOException
+    {
+        try (Socket s = socket) {
+            socket.connect(new InetSocketAddress(addr, port));
         } catch (SocketException e) {
             // java.net.SocketException: Malformed reply from SOCKS server
             // This exception is OK, since the "server" does not implement
             // the socks protocol. It just verifies the version and closes.
         }
-
-        // SOCKS V5
-        System.setProperty("socksProxyVersion", Integer.toString(5));
-        try (Socket s = new Socket()) {
-            s.connect(new InetSocketAddress(addr, port));
-        } catch (SocketException e) { /* OK */ }
-
-        serverThread.join();
-        if (failed) {
-            throw new RuntimeException("socksProxyVersion not being set correctly");
-        }
     }
 
+    @Override
     public void run() {
+        int count = 0;
         try {
-            try (Socket s = ss.accept()) {
-                int version = (s.getInputStream()).read();
-                if (version != 4) {
-                    System.out.println("Got " + version + ", expected 4");
-                    failed = true;
+            while (!stopped) {
+                try (Socket s = ss.accept()) {
+                    int version = (s.getInputStream()).read();
+                    if (version != expected) {
+                        System.out.printf("Iteration: %d, Got: %d, expected: %d%n",
+                                          count, version, expected);
+                        failed = true;
+                    }
                 }
-            }
-            try (Socket s = ss.accept()) {
-                int version = (s.getInputStream()).read();
-                if (version != 5) {
-                    System.out.println("Got " + version + ", expected 5");
-                    failed = true;
-                }
+                count++;
             }
         } catch (IOException e) {
-            e.printStackTrace();
+            if (!ss.isClosed()) {
+                e.printStackTrace();
+            }
+            // ignore, server socket was closed
         }
     }
 }