jdk/test/java/net/Socks/SocksProxyVersion.java
author chegar
Thu, 13 Jan 2011 13:24:58 +0000
changeset 7982 65f5328a67a2
child 8160 4257e268578e
permissions -rw-r--r--
6964547: Impossible to set useV4 in SocksSocketImpl Summary: Add socksProxyVersion property Reviewed-by: alanb, michaelm
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 {
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    39
    ServerSocket ss;
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
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    46
    public SocksProxyVersion() throws Exception {
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    47
        ss = new ServerSocket(0);
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    48
        int port = ss.getLocalPort();
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    49
        Thread serverThread = new Thread(this);
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    50
        serverThread.start();
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
        System.setProperty("socksProxyHost", "localhost");
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    53
        System.setProperty("socksProxyPort", Integer.toString(port));
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    54
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    55
        // SOCKS V4
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    56
        System.setProperty("socksProxyVersion", Integer.toString(4));
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    57
        try (Socket s = new Socket()) {
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    58
            s.connect(new InetSocketAddress("localhost", port));
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    59
        } catch (SocketException e) {
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    60
            // java.net.SocketException: Malformed reply from SOCKS server
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    61
            // This exception is OK, since the "server" does not implement
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    62
            // the socks protocol. It just verifies the version and closes.
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    63
        }
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    64
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    65
        // SOCKS V5
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    66
        System.setProperty("socksProxyVersion", Integer.toString(5));
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    67
        try (Socket s = new Socket()) {
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    68
            s.connect(new InetSocketAddress("localhost", port));
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    69
        } catch (SocketException e) { /* OK */ }
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
        serverThread.join();
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    72
        if (failed) {
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    73
            throw new RuntimeException("socksProxyVersion not being set correctly");
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    74
        }
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    75
    }
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    76
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    77
    public void run() {
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    78
        try (ss) {
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    79
            Socket s = ss.accept();
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    80
            int version = (s.getInputStream()).read();
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    81
            if (version != 4) {
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    82
                System.out.println("Got " + version + ", expected 4");
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    83
                failed = true;
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    84
            }
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    85
            s.close();
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    86
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    87
            s = ss.accept();
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    88
            version = (s.getInputStream()).read();
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    89
            if (version != 5) {
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    90
                System.out.println("Got " + version + ", expected 5");
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    91
                failed = true;
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    92
            }
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    93
            s.close();
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    94
        } catch (IOException e) {
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    95
            e.printStackTrace();
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    96
        }
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    97
    }
65f5328a67a2 6964547: Impossible to set useV4 in SocksSocketImpl
chegar
parents:
diff changeset
    98
}