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