Path: blob/master/test/jdk/java/net/Socket/SocksConnectTimeout.java
41152 views
/*1* Copyright (c) 2010, 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 622363526* @library /test/lib27* @summary Code hangs at connect call even when Timeout is specified28* @run main SocksConnectTimeout29* @run main/othervm -Djava.net.preferIPv4Stack=true SocksConnectTimeout30*/3132import java.net.InetAddress;33import java.net.InetSocketAddress;34import java.net.Proxy;35import java.net.Socket;36import java.net.ServerSocket;37import java.net.SocketTimeoutException;38import java.io.IOException;39import java.io.Closeable;40import java.util.concurrent.Phaser;41import java.util.concurrent.TimeUnit;42import jdk.test.lib.net.IPSupport;4344public class SocksConnectTimeout {45static ServerSocket serverSocket;46static final boolean debug = true;47static final Phaser startPhaser = new Phaser(2);48static final Phaser finishPhaser = new Phaser(2);49static int failed, passed;5051public static void main(String[] args) {52IPSupport.throwSkippedExceptionIfNonOperational();5354try {55serverSocket = new ServerSocket();56InetAddress localHost = InetAddress.getLocalHost();57serverSocket.bind(new InetSocketAddress(localHost, 0));5859(new Thread() {60@Override61public void run() { serve(); }62}).start();6364Proxy socksProxy = new Proxy(Proxy.Type.SOCKS,65new InetSocketAddress(localHost, serverSocket.getLocalPort()));6667test(socksProxy);68} catch (IOException e) {69unexpected(e);70} finally {71close(serverSocket);7273if (failed > 0)74throw new RuntimeException("Test Failed: passed:" + passed + ", failed:" + failed);75}76}7778static void test(Proxy proxy) {79startPhaser.arriveAndAwaitAdvance();80Socket socket = null;81try {82socket = new Socket(proxy);83connectWithTimeout(socket);84failed("connected successfully!");85} catch (SocketTimeoutException socketTimeout) {86debug("Passed: Received: " + socketTimeout);87passed();88} catch (Exception exception) {89failed("Connect timeout test failed", exception);90} finally {91finishPhaser.arriveAndAwaitAdvance();92close(socket);93}94}9596static void connectWithTimeout(Socket socket) throws IOException {97socket.connect(new InetSocketAddress(InetAddress.getLocalHost(), 1234), 500);98}99100static void serve() {101Socket client = null;102try {103startPhaser.arriveAndAwaitAdvance();104client = serverSocket.accept();105finishPhaser.awaitAdvanceInterruptibly(finishPhaser.arrive(), 5, TimeUnit.SECONDS);106} catch (Exception e) {107unexpected(e);108} finally {109close(client);110}111}112113static void debug(String message) {114if (debug)115System.out.println(message);116}117118static void unexpected(Exception e ) {119System.out.println("Unexcepted Exception: " + e);120}121122static void close(Closeable closeable) {123if (closeable != null) try { closeable.close(); } catch (IOException e) {unexpected(e);}124}125126static void failed(String message) {127System.out.println(message);128failed++;129}130131static void failed(String message, Exception e) {132System.out.println(message);133System.out.println(e);134failed++;135}136137static void passed() { passed++; };138139}140141142