3393N/A/*
3393N/A * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
3393N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3393N/A *
3393N/A * This code is free software; you can redistribute it and/or modify it
3393N/A * under the terms of the GNU General Public License version 2 only, as
3393N/A * published by the Free Software Foundation.
3393N/A *
3393N/A * This code is distributed in the hope that it will be useful, but WITHOUT
3393N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3393N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
3393N/A * version 2 for more details (a copy is included in the LICENSE file that
3393N/A * accompanied this code).
3393N/A *
3393N/A * You should have received a copy of the GNU General Public License version
3393N/A * 2 along with this work; if not, write to the Free Software Foundation,
3393N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3393N/A *
3393N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3393N/A * or visit www.oracle.com if you need additional information or have any
3393N/A * questions.
3393N/A */
3393N/A
3393N/A/*
3393N/A * @test
3393N/A * @bug 6964547
3393N/A * @run main/othervm SocksProxyVersion
3393N/A * @summary test socksProxyVersion system property
3393N/A */
3393N/A
3393N/Aimport java.net.InetSocketAddress;
3393N/Aimport java.net.ServerSocket;
3393N/Aimport java.net.Socket;
3393N/Aimport java.net.SocketException;
3393N/Aimport java.io.DataInputStream;
3393N/Aimport java.io.IOException;
3393N/A
3393N/Apublic class SocksProxyVersion implements Runnable {
3473N/A final ServerSocket ss;
3393N/A volatile boolean failed;
3393N/A
3393N/A public static void main(String[] args) throws Exception {
3393N/A new SocksProxyVersion();
3393N/A }
3393N/A
3473N/A @SuppressWarnings("try")
3393N/A public SocksProxyVersion() throws Exception {
3393N/A ss = new ServerSocket(0);
3473N/A try (ServerSocket socket = ss) {
3473N/A runTest();
3473N/A }
3473N/A }
3473N/A
3473N/A void runTest() throws Exception {
3393N/A int port = ss.getLocalPort();
3393N/A Thread serverThread = new Thread(this);
3393N/A serverThread.start();
3393N/A
3393N/A System.setProperty("socksProxyHost", "localhost");
3393N/A System.setProperty("socksProxyPort", Integer.toString(port));
3393N/A
3393N/A // SOCKS V4
3393N/A System.setProperty("socksProxyVersion", Integer.toString(4));
3393N/A try (Socket s = new Socket()) {
3393N/A s.connect(new InetSocketAddress("localhost", port));
3393N/A } catch (SocketException e) {
3393N/A // java.net.SocketException: Malformed reply from SOCKS server
3393N/A // This exception is OK, since the "server" does not implement
3393N/A // the socks protocol. It just verifies the version and closes.
3393N/A }
3393N/A
3393N/A // SOCKS V5
3393N/A System.setProperty("socksProxyVersion", Integer.toString(5));
3393N/A try (Socket s = new Socket()) {
3393N/A s.connect(new InetSocketAddress("localhost", port));
3393N/A } catch (SocketException e) { /* OK */ }
3393N/A
3393N/A serverThread.join();
3393N/A if (failed) {
3393N/A throw new RuntimeException("socksProxyVersion not being set correctly");
3393N/A }
3393N/A }
3393N/A
3393N/A public void run() {
3473N/A try {
3473N/A try (Socket s = ss.accept()) {
3473N/A int version = (s.getInputStream()).read();
3473N/A if (version != 4) {
3473N/A System.out.println("Got " + version + ", expected 4");
3473N/A failed = true;
3473N/A }
3393N/A }
3473N/A try (Socket s = ss.accept()) {
3473N/A int version = (s.getInputStream()).read();
3473N/A if (version != 5) {
3473N/A System.out.println("Got " + version + ", expected 5");
3473N/A failed = true;
3473N/A }
3393N/A }
3393N/A } catch (IOException e) {
3393N/A e.printStackTrace();
3393N/A }
3393N/A }
3393N/A}