1/*
2 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * @test
26 * @bug 6223635
27 * @summary Code hangs at connect call even when Timeout is specified
28 */
29
30import java.net.InetAddress;
31import java.net.InetSocketAddress;
32import java.net.Proxy;
33import java.net.Socket;
34import java.net.ServerSocket;
35import java.net.SocketTimeoutException;
36import java.io.IOException;
37import java.io.Closeable;
38import java.util.concurrent.Phaser;
39import java.util.concurrent.TimeUnit;
40
41public class SocksConnectTimeout {
42    static ServerSocket serverSocket;
43    static final boolean debug = true;
44    static final Phaser startPhaser = new Phaser(2);
45    static final Phaser finishPhaser = new Phaser(2);
46    static int failed, passed;
47
48    public static void main(String[] args) {
49        try {
50            serverSocket = new ServerSocket(0);
51
52            (new Thread() {
53                @Override
54                public void run() { serve(); }
55            }).start();
56
57            Proxy socksProxy = new Proxy(Proxy.Type.SOCKS,
58                new InetSocketAddress(InetAddress.getLocalHost(), serverSocket.getLocalPort()));
59
60            test(socksProxy);
61        } catch (IOException e) {
62            unexpected(e);
63        } finally {
64            close(serverSocket);
65
66            if (failed > 0)
67                throw new RuntimeException("Test Failed: passed:" + passed + ", failed:" + failed);
68        }
69    }
70
71    static void test(Proxy proxy) {
72        startPhaser.arriveAndAwaitAdvance();
73        Socket socket = null;
74        try {
75            socket = new Socket(proxy);
76            connectWithTimeout(socket);
77            failed("connected successfully!");
78        } catch (SocketTimeoutException socketTimeout) {
79            debug("Passed: Received: " + socketTimeout);
80            passed();
81        } catch (Exception exception) {
82            failed("Connect timeout test failed", exception);
83        } finally {
84            finishPhaser.arriveAndAwaitAdvance();
85            close(socket);
86        }
87    }
88
89    static void connectWithTimeout(Socket socket) throws IOException {
90        socket.connect(new InetSocketAddress(InetAddress.getLocalHost(), 1234), 500);
91    }
92
93    static void serve() {
94        Socket client = null;
95        try {
96            startPhaser.arriveAndAwaitAdvance();
97            client = serverSocket.accept();
98            finishPhaser.awaitAdvanceInterruptibly(finishPhaser.arrive(), 5, TimeUnit.SECONDS);
99        } catch (Exception e) {
100            unexpected(e);
101        } finally {
102            close(client);
103        }
104    }
105
106    static void debug(String message) {
107        if (debug)
108            System.out.println(message);
109    }
110
111    static void unexpected(Exception e ) {
112        System.out.println("Unexcepted Exception: " + e);
113    }
114
115    static void close(Closeable closeable) {
116        if (closeable != null) try { closeable.close(); } catch (IOException e) {unexpected(e);}
117    }
118
119    static void failed(String message) {
120        System.out.println(message);
121        failed++;
122    }
123
124    static void failed(String message, Exception e) {
125        System.out.println(message);
126        System.out.println(e);
127        failed++;
128    }
129
130    static void passed() { passed++; };
131
132}
133