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