Path: blob/master/test/jdk/java/net/Socks/SocksProxyVersion.java
41149 views
/*1* Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 6964547 5001942 812944426* @library /test/lib27* @run main/othervm SocksProxyVersion28* @summary test socksProxyVersion system property29*/3031import java.net.InetSocketAddress;32import java.net.ServerSocket;33import java.net.Socket;34import java.net.SocketException;35import java.io.IOException;36import java.net.InetAddress;37import java.net.Proxy;38import jdk.test.lib.net.IPSupport;3940public class SocksProxyVersion implements Runnable {41final ServerSocket ss;42volatile boolean failed;43volatile boolean stopped = false;44volatile int expected;4546public static void main(String[] args) throws Exception {47if (InetAddress.getLocalHost().isLoopbackAddress()) {48System.out.println("Test cannot run. getLocalHost returns a loopback address");49return;50}51new SocksProxyVersion();52}5354public SocksProxyVersion() throws Exception {55ss = new ServerSocket(0, 0, InetAddress.getLocalHost());56int port = ss.getLocalPort();57Thread serverThread = new Thread(this);58serverThread.start();59try (ServerSocket socket = ss) {60runTest(port);61} finally {62stopped = true;63}6465serverThread.join();66if (failed) {67throw new RuntimeException("socksProxyVersion not being set correctly");68}69}7071final void runTest(int port) throws Exception {72/*73* Retrieving the IP Address of the machine74* since "localhost" is bypassed as a non-proxy host75*/76String addr = InetAddress.getLocalHost().getHostAddress();7778System.setProperty("socksProxyHost", addr);79System.setProperty("socksProxyPort", Integer.toString(port));8081Proxy proxy = new Proxy(Proxy.Type.SOCKS,82new InetSocketAddress(addr, port));8384if (IPSupport.hasIPv4()) {85// SOCKS V4 (requires IPv4)86System.setProperty("socksProxyVersion", "4");87this.expected = 4;88check(new Socket(), addr, port);89check(new Socket(proxy), addr, port);90}9192// SOCKS V593System.setProperty("socksProxyVersion", "5");94this.expected = 5;95check(new Socket(), addr, port);96check(new Socket(proxy), addr, port);97}9899private void check(Socket socket, String addr, int port)100throws IOException101{102try (Socket s = socket) {103socket.connect(new InetSocketAddress(addr, port));104} catch (SocketException e) {105// java.net.SocketException: Malformed reply from SOCKS server106// This exception is OK, since the "server" does not implement107// the socks protocol. It just verifies the version and closes.108}109}110111@Override112public void run() {113int count = 0;114try {115while (!stopped) {116try (Socket s = ss.accept()) {117int version = (s.getInputStream()).read();118if (version != expected) {119System.out.printf("Iteration: %d, Got: %d, expected: %d%n",120count, version, expected);121failed = true;122}123}124count++;125}126} catch (IOException e) {127if (!ss.isClosed()) {128e.printStackTrace();129}130// ignore, server socket was closed131}132}133}134135136