jdk/test/java/net/Socks/SocksProxyVersion.java
author asmotrak
Fri, 03 Jul 2015 08:00:33 +0100
changeset 31529 31d7d82b39ff
parent 23010 6dadb192ad81
permissions -rw-r--r--
8129444: socksProxyVersion system property ignored for Socket(Proxy) Reviewed-by: chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7982
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
     1
/*
31529
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 23010
diff changeset
     2
 * Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.
7982
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
     4
 *
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
     8
 *
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    13
 * accompanied this code).
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    14
 *
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    18
 *
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    21
 * questions.
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    22
 */
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    23
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    24
/*
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    25
 * @test
31529
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 23010
diff changeset
    26
 * @bug 6964547 5001942 8129444
7982
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    27
 * @run main/othervm SocksProxyVersion
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    28
 * @summary test socksProxyVersion system property
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    29
 */
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    30
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    31
import java.net.InetSocketAddress;
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    32
import java.net.ServerSocket;
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    33
import java.net.Socket;
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    34
import java.net.SocketException;
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    35
import java.io.IOException;
16737
8f36190e097a 5001942: Missings SOCKS support for direct connections
khazra
parents: 8160
diff changeset
    36
import java.net.InetAddress;
31529
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 23010
diff changeset
    37
import java.net.Proxy;
7982
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    38
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    39
public class SocksProxyVersion implements Runnable {
8160
4257e268578e 7015410: test/java/net/Socks/SocksProxyVersion.java needs to be updated due to 7013420
alanb
parents: 7982
diff changeset
    40
    final ServerSocket ss;
7982
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    41
    volatile boolean failed;
31529
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 23010
diff changeset
    42
    volatile boolean stopped = false;
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 23010
diff changeset
    43
    volatile int expected;
7982
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    44
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    45
    public static void main(String[] args) throws Exception {
20783
c0806cb8d4ae 8023555: test/java/net/Socks/SocksProxyVersion.java fails when machine name is localhost
tyan
parents: 16737
diff changeset
    46
        if (InetAddress.getLocalHost().isLoopbackAddress()) {
c0806cb8d4ae 8023555: test/java/net/Socks/SocksProxyVersion.java fails when machine name is localhost
tyan
parents: 16737
diff changeset
    47
            System.out.println("Test cannot run. getLocalHost returns a loopback address");
c0806cb8d4ae 8023555: test/java/net/Socks/SocksProxyVersion.java fails when machine name is localhost
tyan
parents: 16737
diff changeset
    48
            return;
c0806cb8d4ae 8023555: test/java/net/Socks/SocksProxyVersion.java fails when machine name is localhost
tyan
parents: 16737
diff changeset
    49
        }
7982
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    50
        new SocksProxyVersion();
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    51
    }
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    52
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    53
    public SocksProxyVersion() throws Exception {
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    54
        ss = new ServerSocket(0);
31529
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 23010
diff changeset
    55
        int port = ss.getLocalPort();
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 23010
diff changeset
    56
        Thread serverThread = new Thread(this);
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 23010
diff changeset
    57
        serverThread.start();
8160
4257e268578e 7015410: test/java/net/Socks/SocksProxyVersion.java needs to be updated due to 7013420
alanb
parents: 7982
diff changeset
    58
        try (ServerSocket socket = ss) {
31529
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 23010
diff changeset
    59
            runTest(port);
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 23010
diff changeset
    60
        } finally {
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 23010
diff changeset
    61
            stopped = true;
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 23010
diff changeset
    62
        }
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 23010
diff changeset
    63
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 23010
diff changeset
    64
        serverThread.join();
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 23010
diff changeset
    65
        if (failed) {
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 23010
diff changeset
    66
            throw new RuntimeException("socksProxyVersion not being set correctly");
8160
4257e268578e 7015410: test/java/net/Socks/SocksProxyVersion.java needs to be updated due to 7013420
alanb
parents: 7982
diff changeset
    67
        }
4257e268578e 7015410: test/java/net/Socks/SocksProxyVersion.java needs to be updated due to 7013420
alanb
parents: 7982
diff changeset
    68
    }
4257e268578e 7015410: test/java/net/Socks/SocksProxyVersion.java needs to be updated due to 7013420
alanb
parents: 7982
diff changeset
    69
31529
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 23010
diff changeset
    70
    final void runTest(int port) throws Exception {
16737
8f36190e097a 5001942: Missings SOCKS support for direct connections
khazra
parents: 8160
diff changeset
    71
        /*
31529
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 23010
diff changeset
    72
         * Retrieving the IP Address of the machine
16737
8f36190e097a 5001942: Missings SOCKS support for direct connections
khazra
parents: 8160
diff changeset
    73
         * since "localhost" is bypassed as a non-proxy host
8f36190e097a 5001942: Missings SOCKS support for direct connections
khazra
parents: 8160
diff changeset
    74
         */
8f36190e097a 5001942: Missings SOCKS support for direct connections
khazra
parents: 8160
diff changeset
    75
        String addr = InetAddress.getLocalHost().getHostAddress();
8f36190e097a 5001942: Missings SOCKS support for direct connections
khazra
parents: 8160
diff changeset
    76
8f36190e097a 5001942: Missings SOCKS support for direct connections
khazra
parents: 8160
diff changeset
    77
        System.setProperty("socksProxyHost", addr);
7982
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    78
        System.setProperty("socksProxyPort", Integer.toString(port));
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    79
31529
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 23010
diff changeset
    80
        Proxy proxy = new Proxy(Proxy.Type.SOCKS,
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 23010
diff changeset
    81
                                new InetSocketAddress(addr, port));
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 23010
diff changeset
    82
7982
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    83
        // SOCKS V4
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    84
        System.setProperty("socksProxyVersion", Integer.toString(4));
31529
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 23010
diff changeset
    85
        this.expected = 4;
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 23010
diff changeset
    86
        check(new Socket(), addr, port);
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 23010
diff changeset
    87
        check(new Socket(proxy), addr, port);
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 23010
diff changeset
    88
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 23010
diff changeset
    89
        // SOCKS V5
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 23010
diff changeset
    90
        System.setProperty("socksProxyVersion", Integer.toString(5));
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 23010
diff changeset
    91
        this.expected = 5;
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 23010
diff changeset
    92
        check(new Socket(), addr, port);
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 23010
diff changeset
    93
        check(new Socket(proxy), addr, port);
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 23010
diff changeset
    94
    }
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 23010
diff changeset
    95
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 23010
diff changeset
    96
    private void check(Socket socket, String addr, int port)
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 23010
diff changeset
    97
        throws IOException
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 23010
diff changeset
    98
    {
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 23010
diff changeset
    99
        try (Socket s = socket) {
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 23010
diff changeset
   100
            socket.connect(new InetSocketAddress(addr, port));
7982
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
   101
        } catch (SocketException e) {
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
   102
            // java.net.SocketException: Malformed reply from SOCKS server
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
   103
            // This exception is OK, since the "server" does not implement
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
   104
            // the socks protocol. It just verifies the version and closes.
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
   105
        }
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
   106
    }
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
   107
31529
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 23010
diff changeset
   108
    @Override
7982
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
   109
    public void run() {
31529
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 23010
diff changeset
   110
        int count = 0;
8160
4257e268578e 7015410: test/java/net/Socks/SocksProxyVersion.java needs to be updated due to 7013420
alanb
parents: 7982
diff changeset
   111
        try {
31529
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 23010
diff changeset
   112
            while (!stopped) {
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 23010
diff changeset
   113
                try (Socket s = ss.accept()) {
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 23010
diff changeset
   114
                    int version = (s.getInputStream()).read();
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 23010
diff changeset
   115
                    if (version != expected) {
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 23010
diff changeset
   116
                        System.out.printf("Iteration: %d, Got: %d, expected: %d%n",
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 23010
diff changeset
   117
                                          count, version, expected);
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 23010
diff changeset
   118
                        failed = true;
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 23010
diff changeset
   119
                    }
8160
4257e268578e 7015410: test/java/net/Socks/SocksProxyVersion.java needs to be updated due to 7013420
alanb
parents: 7982
diff changeset
   120
                }
31529
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 23010
diff changeset
   121
                count++;
7982
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
   122
            }
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
   123
        } catch (IOException e) {
31529
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 23010
diff changeset
   124
            if (!ss.isClosed()) {
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 23010
diff changeset
   125
                e.printStackTrace();
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 23010
diff changeset
   126
            }
31d7d82b39ff 8129444: socksProxyVersion system property ignored for Socket(Proxy)
asmotrak
parents: 23010
diff changeset
   127
            // ignore, server socket was closed
7982
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
   128
        }
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
   129
    }
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
   130
}