Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/Socks/SocksIPv6Test.java
41149 views
1
/*
2
* Copyright (c) 2013, 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
/* @test
25
* @bug 7100957
26
* @modules jdk.httpserver
27
* @library /test/lib
28
* @summary Java doesn't correctly handle the SOCKS protocol when used over IPv6.
29
* @run testng SocksIPv6Test
30
*/
31
32
import java.io.BufferedReader;
33
import java.io.IOException;
34
import java.io.InputStreamReader;
35
import java.io.OutputStreamWriter;
36
import java.net.Authenticator;
37
import java.net.InetSocketAddress;
38
import java.net.URL;
39
import java.net.Proxy;
40
import java.lang.Override;
41
import java.net.InetAddress;
42
import java.net.Inet6Address;
43
import java.net.ServerSocket;
44
import java.net.SocketException;
45
import java.net.NetworkInterface;
46
import java.util.Collections;
47
import java.util.List;
48
import com.sun.net.httpserver.*;
49
import java.io.BufferedWriter;
50
import org.testng.SkipException;
51
import org.testng.annotations.AfterClass;
52
import org.testng.annotations.BeforeClass;
53
import org.testng.annotations.Test;
54
55
import jdk.test.lib.NetworkConfiguration;
56
57
import static org.testng.Assert.*;
58
59
public class SocksIPv6Test {
60
61
private HttpServer server;
62
private SocksServer socks;
63
private String response = "Hello.";
64
65
@BeforeClass
66
public void setUp() throws Exception {
67
if (!ensureInet6AddressFamily() || !ensureIPv6OnLoopback()) {
68
NetworkConfiguration.printSystemConfiguration(System.out);
69
throw new SkipException("Host does not support IPv6");
70
}
71
72
server = HttpServer.create(new InetSocketAddress("::1", 0), 0);
73
server.createContext("/", ex -> {
74
ex.sendResponseHeaders(200, response.length());
75
try (BufferedWriter writer = new BufferedWriter(
76
new OutputStreamWriter(ex.getResponseBody(), "UTF-8"))) {
77
writer.write(response);
78
}
79
ex.close();
80
});
81
server.start();
82
83
socks = new SocksServer(InetAddress.getByName("::1"), 0, false);
84
socks.addUser("user", "pass");
85
socks.start();
86
87
Authenticator.setDefault(new Authenticator() {
88
@Override
89
protected java.net.PasswordAuthentication getPasswordAuthentication() {
90
return new java.net.PasswordAuthentication(
91
"user", "pass".toCharArray());
92
}
93
});
94
}
95
96
private boolean ensureIPv6OnLoopback() throws Exception {
97
boolean ipv6 = false;
98
99
List<NetworkInterface> nics = Collections.list(NetworkInterface.getNetworkInterfaces());
100
for (NetworkInterface nic : nics) {
101
if (!nic.isLoopback()) {
102
continue;
103
}
104
List<InetAddress> addrs = Collections.list(nic.getInetAddresses());
105
for (InetAddress addr : addrs) {
106
if (addr instanceof Inet6Address) {
107
ipv6 = true;
108
break;
109
}
110
}
111
}
112
if (!ipv6)
113
System.out.println("IPv6 is not enabled on loopback. Skipping test suite.");
114
return ipv6;
115
}
116
117
private boolean ensureInet6AddressFamily() throws IOException {
118
try (ServerSocket s = new ServerSocket()) {
119
s.bind(new InetSocketAddress("::1", 0));
120
return true;
121
} catch (SocketException e) {
122
System.out.println("Inet 6 address family is not available. Skipping test suite.");
123
}
124
return false;
125
}
126
127
@Test(groups = "unit")
128
public void testSocksOverIPv6() throws Exception {
129
Proxy proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("::1",
130
socks.getPort()));
131
URL url = new URL("http://[::1]:" + server.getAddress().getPort());
132
java.net.URLConnection conn = url.openConnection(proxy);
133
String actual = "";
134
try (BufferedReader reader = new BufferedReader(
135
new InputStreamReader(conn.getInputStream()))) {
136
actual = reader.readLine();
137
}
138
assertEquals(actual, response);
139
}
140
141
@Test(groups = "unit")
142
public void testSocksOverIPv6Hostname() throws Exception {
143
InetAddress ipv6Loopback = InetAddress.getByName("::1");
144
String ipv6Hostname = ipv6Loopback.getHostName();
145
String ipv6HostAddress = ipv6Loopback.getHostAddress();
146
InetAddress ipv4Loopback;
147
String ipv4Hostname;
148
String ipv4HostAddress;
149
try {
150
ipv4Loopback = InetAddress.getByName("127.0.0.1");
151
ipv4Hostname = ipv4Loopback == null ? null : ipv4Loopback.getHostName();
152
ipv4HostAddress = ipv4Loopback == null ? null : ipv4Loopback.getHostAddress();
153
} catch (IOException io) {
154
ipv4Hostname = null;
155
ipv4HostAddress = null;
156
}
157
158
System.out.println("ipv6Hostname: " + ipv6Hostname + " / " + ipv6HostAddress);
159
System.out.println("ipv4Hostname: " + ipv4Hostname + " / " + ipv4HostAddress);
160
161
if (ipv6Hostname.equals(ipv6HostAddress)) {
162
System.out.println("Unable to get the hostname of the IPv6 loopback "
163
+ "address. Skipping test case.");
164
return;
165
}
166
167
if (ipv4Hostname != null && ipv6Hostname.equals(ipv4Hostname)) {
168
System.out.println("IPv6 and IPv4 loopback addresses map to the"
169
+ " same hostname. Skipping test case.");
170
return;
171
}
172
173
if (!InetAddress.getByName(ipv6Hostname).getHostAddress()
174
.equals(ipv6HostAddress)) {
175
System.out.println(ipv6Hostname + " resolves to \""
176
+ InetAddress.getByName(ipv6Hostname).getHostAddress()
177
+ "\", not \"" + ipv6HostAddress +
178
"\". Skipping test case.");
179
return;
180
}
181
182
Proxy proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(ipv6Hostname,
183
socks.getPort()));
184
URL url = new URL("http://" + ipv6Hostname + ":" + server.getAddress().getPort());
185
java.net.URLConnection conn = url.openConnection(proxy);
186
String actual = "";
187
try (BufferedReader reader = new BufferedReader(
188
new InputStreamReader(conn.getInputStream()))) {
189
actual = reader.readLine();
190
}
191
assertEquals(actual, response);
192
}
193
194
@AfterClass
195
public void tearDown() {
196
if (server != null) {
197
server.stop(1);
198
}
199
if (socks != null) {
200
socks.close();
201
}
202
}
203
}
204
205