Path: blob/jdk8u272-b10-aarch32-20201026/jdk/test/java/net/Socks/SocksProxyVersion.java
48795 views
/*1* Copyright (c) 2011, 2013, 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 500194226* @run main/othervm SocksProxyVersion27* @summary test socksProxyVersion system property28*/2930import java.net.InetSocketAddress;31import java.net.ServerSocket;32import java.net.Socket;33import java.net.SocketException;34import java.io.DataInputStream;35import java.io.IOException;36import java.net.InetAddress;3738public class SocksProxyVersion implements Runnable {39final ServerSocket ss;40volatile boolean failed;4142public static void main(String[] args) throws Exception {43if (InetAddress.getLocalHost().isLoopbackAddress()) {44System.out.println("Test cannot run. getLocalHost returns a loopback address");45return;46}47new SocksProxyVersion();48}4950@SuppressWarnings("try")51public SocksProxyVersion() throws Exception {52ss = new ServerSocket(0);53try (ServerSocket socket = ss) {54runTest();55}56}5758void runTest() throws Exception {59int port = ss.getLocalPort();60Thread serverThread = new Thread(this);61serverThread.start();6263/*64* Retreving the IP Address of the machine65* since "localhost" is bypassed as a non-proxy host66*/67String addr = InetAddress.getLocalHost().getHostAddress();6869System.setProperty("socksProxyHost", addr);70System.setProperty("socksProxyPort", Integer.toString(port));7172// SOCKS V473System.setProperty("socksProxyVersion", Integer.toString(4));74try (Socket s = new Socket()) {75s.connect(new InetSocketAddress(addr, port));76} catch (SocketException e) {77// java.net.SocketException: Malformed reply from SOCKS server78// This exception is OK, since the "server" does not implement79// the socks protocol. It just verifies the version and closes.80}8182// SOCKS V583System.setProperty("socksProxyVersion", Integer.toString(5));84try (Socket s = new Socket()) {85s.connect(new InetSocketAddress(addr, port));86} catch (SocketException e) { /* OK */ }8788serverThread.join();89if (failed) {90throw new RuntimeException("socksProxyVersion not being set correctly");91}92}9394public void run() {95try {96try (Socket s = ss.accept()) {97int version = (s.getInputStream()).read();98if (version != 4) {99System.out.println("Got " + version + ", expected 4");100failed = true;101}102}103try (Socket s = ss.accept()) {104int version = (s.getInputStream()).read();105if (version != 5) {106System.out.println("Got " + version + ", expected 5");107failed = true;108}109}110} catch (IOException e) {111e.printStackTrace();112}113}114}115116117