Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/Socket/SocksConnectTimeout.java
41152 views
1
/*
2
* Copyright (c) 2010, 2019, 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
* @library /test/lib
28
* @summary Code hangs at connect call even when Timeout is specified
29
* @run main SocksConnectTimeout
30
* @run main/othervm -Djava.net.preferIPv4Stack=true SocksConnectTimeout
31
*/
32
33
import java.net.InetAddress;
34
import java.net.InetSocketAddress;
35
import java.net.Proxy;
36
import java.net.Socket;
37
import java.net.ServerSocket;
38
import java.net.SocketTimeoutException;
39
import java.io.IOException;
40
import java.io.Closeable;
41
import java.util.concurrent.Phaser;
42
import java.util.concurrent.TimeUnit;
43
import jdk.test.lib.net.IPSupport;
44
45
public class SocksConnectTimeout {
46
static ServerSocket serverSocket;
47
static final boolean debug = true;
48
static final Phaser startPhaser = new Phaser(2);
49
static final Phaser finishPhaser = new Phaser(2);
50
static int failed, passed;
51
52
public static void main(String[] args) {
53
IPSupport.throwSkippedExceptionIfNonOperational();
54
55
try {
56
serverSocket = new ServerSocket();
57
InetAddress localHost = InetAddress.getLocalHost();
58
serverSocket.bind(new InetSocketAddress(localHost, 0));
59
60
(new Thread() {
61
@Override
62
public void run() { serve(); }
63
}).start();
64
65
Proxy socksProxy = new Proxy(Proxy.Type.SOCKS,
66
new InetSocketAddress(localHost, serverSocket.getLocalPort()));
67
68
test(socksProxy);
69
} catch (IOException e) {
70
unexpected(e);
71
} finally {
72
close(serverSocket);
73
74
if (failed > 0)
75
throw new RuntimeException("Test Failed: passed:" + passed + ", failed:" + failed);
76
}
77
}
78
79
static void test(Proxy proxy) {
80
startPhaser.arriveAndAwaitAdvance();
81
Socket socket = null;
82
try {
83
socket = new Socket(proxy);
84
connectWithTimeout(socket);
85
failed("connected successfully!");
86
} catch (SocketTimeoutException socketTimeout) {
87
debug("Passed: Received: " + socketTimeout);
88
passed();
89
} catch (Exception exception) {
90
failed("Connect timeout test failed", exception);
91
} finally {
92
finishPhaser.arriveAndAwaitAdvance();
93
close(socket);
94
}
95
}
96
97
static void connectWithTimeout(Socket socket) throws IOException {
98
socket.connect(new InetSocketAddress(InetAddress.getLocalHost(), 1234), 500);
99
}
100
101
static void serve() {
102
Socket client = null;
103
try {
104
startPhaser.arriveAndAwaitAdvance();
105
client = serverSocket.accept();
106
finishPhaser.awaitAdvanceInterruptibly(finishPhaser.arrive(), 5, TimeUnit.SECONDS);
107
} catch (Exception e) {
108
unexpected(e);
109
} finally {
110
close(client);
111
}
112
}
113
114
static void debug(String message) {
115
if (debug)
116
System.out.println(message);
117
}
118
119
static void unexpected(Exception e ) {
120
System.out.println("Unexcepted Exception: " + e);
121
}
122
123
static void close(Closeable closeable) {
124
if (closeable != null) try { closeable.close(); } catch (IOException e) {unexpected(e);}
125
}
126
127
static void failed(String message) {
128
System.out.println(message);
129
failed++;
130
}
131
132
static void failed(String message, Exception e) {
133
System.out.println(message);
134
System.out.println(e);
135
failed++;
136
}
137
138
static void passed() { passed++; };
139
140
}
141
142