jdk/test/java/net/Socks/SocksProxyVersion.java
author amurillo
Wed, 15 Aug 2012 16:49:38 -0700
changeset 13465 d3fc5d192448
parent 8160 4257e268578e
child 16737 8f36190e097a
permissions -rw-r--r--
7191765: make jdk8 the default jprt release for hs24 Reviewed-by: jcoomes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7982
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
     1
/*
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
     2
 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
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
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    26
 * @bug 6964547
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.DataInputStream;
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    36
import java.io.IOException;
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    37
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    38
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
    39
    final ServerSocket ss;
7982
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    40
    volatile boolean failed;
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    41
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    42
    public static void main(String[] args) throws Exception {
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    43
        new SocksProxyVersion();
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
8160
4257e268578e 7015410: test/java/net/Socks/SocksProxyVersion.java needs to be updated due to 7013420
alanb
parents: 7982
diff changeset
    46
    @SuppressWarnings("try")
7982
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    47
    public SocksProxyVersion() throws Exception {
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    48
        ss = new ServerSocket(0);
8160
4257e268578e 7015410: test/java/net/Socks/SocksProxyVersion.java needs to be updated due to 7013420
alanb
parents: 7982
diff changeset
    49
        try (ServerSocket socket = ss) {
4257e268578e 7015410: test/java/net/Socks/SocksProxyVersion.java needs to be updated due to 7013420
alanb
parents: 7982
diff changeset
    50
            runTest();
4257e268578e 7015410: test/java/net/Socks/SocksProxyVersion.java needs to be updated due to 7013420
alanb
parents: 7982
diff changeset
    51
        }
4257e268578e 7015410: test/java/net/Socks/SocksProxyVersion.java needs to be updated due to 7013420
alanb
parents: 7982
diff changeset
    52
    }
4257e268578e 7015410: test/java/net/Socks/SocksProxyVersion.java needs to be updated due to 7013420
alanb
parents: 7982
diff changeset
    53
4257e268578e 7015410: test/java/net/Socks/SocksProxyVersion.java needs to be updated due to 7013420
alanb
parents: 7982
diff changeset
    54
    void runTest() throws Exception {
7982
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    55
        int port = ss.getLocalPort();
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    56
        Thread serverThread = new Thread(this);
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    57
        serverThread.start();
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    58
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    59
        System.setProperty("socksProxyHost", "localhost");
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    60
        System.setProperty("socksProxyPort", Integer.toString(port));
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    61
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    62
        // SOCKS V4
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    63
        System.setProperty("socksProxyVersion", Integer.toString(4));
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    64
        try (Socket s = new Socket()) {
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    65
            s.connect(new InetSocketAddress("localhost", port));
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    66
        } catch (SocketException e) {
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    67
            // java.net.SocketException: Malformed reply from SOCKS server
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    68
            // This exception is OK, since the "server" does not implement
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    69
            // the socks protocol. It just verifies the version and closes.
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    70
        }
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    71
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    72
        // SOCKS V5
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    73
        System.setProperty("socksProxyVersion", Integer.toString(5));
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    74
        try (Socket s = new Socket()) {
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    75
            s.connect(new InetSocketAddress("localhost", port));
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    76
        } catch (SocketException e) { /* OK */ }
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    77
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    78
        serverThread.join();
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    79
        if (failed) {
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    80
            throw new RuntimeException("socksProxyVersion not being set correctly");
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    81
        }
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    82
    }
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    83
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    84
    public void run() {
8160
4257e268578e 7015410: test/java/net/Socks/SocksProxyVersion.java needs to be updated due to 7013420
alanb
parents: 7982
diff changeset
    85
        try {
4257e268578e 7015410: test/java/net/Socks/SocksProxyVersion.java needs to be updated due to 7013420
alanb
parents: 7982
diff changeset
    86
            try (Socket s = ss.accept()) {
4257e268578e 7015410: test/java/net/Socks/SocksProxyVersion.java needs to be updated due to 7013420
alanb
parents: 7982
diff changeset
    87
                int version = (s.getInputStream()).read();
4257e268578e 7015410: test/java/net/Socks/SocksProxyVersion.java needs to be updated due to 7013420
alanb
parents: 7982
diff changeset
    88
                if (version != 4) {
4257e268578e 7015410: test/java/net/Socks/SocksProxyVersion.java needs to be updated due to 7013420
alanb
parents: 7982
diff changeset
    89
                    System.out.println("Got " + version + ", expected 4");
4257e268578e 7015410: test/java/net/Socks/SocksProxyVersion.java needs to be updated due to 7013420
alanb
parents: 7982
diff changeset
    90
                    failed = true;
4257e268578e 7015410: test/java/net/Socks/SocksProxyVersion.java needs to be updated due to 7013420
alanb
parents: 7982
diff changeset
    91
                }
7982
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    92
            }
8160
4257e268578e 7015410: test/java/net/Socks/SocksProxyVersion.java needs to be updated due to 7013420
alanb
parents: 7982
diff changeset
    93
            try (Socket s = ss.accept()) {
4257e268578e 7015410: test/java/net/Socks/SocksProxyVersion.java needs to be updated due to 7013420
alanb
parents: 7982
diff changeset
    94
                int version = (s.getInputStream()).read();
4257e268578e 7015410: test/java/net/Socks/SocksProxyVersion.java needs to be updated due to 7013420
alanb
parents: 7982
diff changeset
    95
                if (version != 5) {
4257e268578e 7015410: test/java/net/Socks/SocksProxyVersion.java needs to be updated due to 7013420
alanb
parents: 7982
diff changeset
    96
                    System.out.println("Got " + version + ", expected 5");
4257e268578e 7015410: test/java/net/Socks/SocksProxyVersion.java needs to be updated due to 7013420
alanb
parents: 7982
diff changeset
    97
                    failed = true;
4257e268578e 7015410: test/java/net/Socks/SocksProxyVersion.java needs to be updated due to 7013420
alanb
parents: 7982
diff changeset
    98
                }
7982
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    99
            }
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
   100
        } catch (IOException e) {
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
   101
            e.printStackTrace();
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
   102
        }
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
   103
    }
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
   104
}