Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-aarch32-jdk8u
Path: blob/jdk8u272-b10-aarch32-20201026/jdk/test/java/net/Socks/SocksProxyVersion.java
48795 views
1
/*
2
* Copyright (c) 2011, 2013, 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 6964547 5001942
27
* @run main/othervm SocksProxyVersion
28
* @summary test socksProxyVersion system property
29
*/
30
31
import java.net.InetSocketAddress;
32
import java.net.ServerSocket;
33
import java.net.Socket;
34
import java.net.SocketException;
35
import java.io.DataInputStream;
36
import java.io.IOException;
37
import java.net.InetAddress;
38
39
public class SocksProxyVersion implements Runnable {
40
final ServerSocket ss;
41
volatile boolean failed;
42
43
public static void main(String[] args) throws Exception {
44
if (InetAddress.getLocalHost().isLoopbackAddress()) {
45
System.out.println("Test cannot run. getLocalHost returns a loopback address");
46
return;
47
}
48
new SocksProxyVersion();
49
}
50
51
@SuppressWarnings("try")
52
public SocksProxyVersion() throws Exception {
53
ss = new ServerSocket(0);
54
try (ServerSocket socket = ss) {
55
runTest();
56
}
57
}
58
59
void runTest() throws Exception {
60
int port = ss.getLocalPort();
61
Thread serverThread = new Thread(this);
62
serverThread.start();
63
64
/*
65
* Retreving the IP Address of the machine
66
* since "localhost" is bypassed as a non-proxy host
67
*/
68
String addr = InetAddress.getLocalHost().getHostAddress();
69
70
System.setProperty("socksProxyHost", addr);
71
System.setProperty("socksProxyPort", Integer.toString(port));
72
73
// SOCKS V4
74
System.setProperty("socksProxyVersion", Integer.toString(4));
75
try (Socket s = new Socket()) {
76
s.connect(new InetSocketAddress(addr, port));
77
} catch (SocketException e) {
78
// java.net.SocketException: Malformed reply from SOCKS server
79
// This exception is OK, since the "server" does not implement
80
// the socks protocol. It just verifies the version and closes.
81
}
82
83
// SOCKS V5
84
System.setProperty("socksProxyVersion", Integer.toString(5));
85
try (Socket s = new Socket()) {
86
s.connect(new InetSocketAddress(addr, port));
87
} catch (SocketException e) { /* OK */ }
88
89
serverThread.join();
90
if (failed) {
91
throw new RuntimeException("socksProxyVersion not being set correctly");
92
}
93
}
94
95
public void run() {
96
try {
97
try (Socket s = ss.accept()) {
98
int version = (s.getInputStream()).read();
99
if (version != 4) {
100
System.out.println("Got " + version + ", expected 4");
101
failed = true;
102
}
103
}
104
try (Socket s = ss.accept()) {
105
int version = (s.getInputStream()).read();
106
if (version != 5) {
107
System.out.println("Got " + version + ", expected 5");
108
failed = true;
109
}
110
}
111
} catch (IOException e) {
112
e.printStackTrace();
113
}
114
}
115
}
116
117