Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/ProxySelector/B8035158.java
38812 views
1
/*
2
* Copyright (c) 2014, 2016, 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 8035158 8145732
26
* @run main/othervm B8035158
27
*/
28
29
import java.net.Proxy;
30
import java.net.ProxySelector;
31
import java.net.URI;
32
import java.util.*;
33
import java.util.concurrent.Callable;
34
35
public class B8035158 {
36
37
public static void main(String[] args) {
38
for (TestCase t : emptyNonProxiesHosts()) t.run();
39
for (TestCase t : nonEmptyNonProxiesHosts()) t.run();
40
for (TestCase t : misc()) t.run();
41
}
42
43
// Setting http.nonProxyHosts to an empty string has an effect of
44
// not including default hosts to the list of exceptions
45
// (i.e. if you want everything to be connected directly rather than
46
// through proxy, you should set this property to an empty string)
47
private static Collection<TestCase> emptyNonProxiesHosts() {
48
List<TestCase> tests = new LinkedList<>();
49
String[] loopbacks = {"localhost", "[::1]", "[::0]", "0.0.0.0",
50
"127.0.0.0", "127.0.0.1", "127.0.1.0", "127.0.1.1",
51
"127.1.0.0", "127.1.0.1", "127.1.1.0", "127.1.1.1"};
52
Map<String, String> properties = new HashMap<>();
53
properties.put("http.proxyHost", "http://proxy.example.com");
54
properties.put("http.nonProxyHosts", "");
55
for (String s : loopbacks) {
56
tests.add(new TestCase(properties, "http://" + s, true));
57
}
58
return tests;
59
}
60
61
// No matter what is set into the http.nonProxyHosts (as far as it is not
62
// an empty string) loopback address aliases must be always connected
63
// directly
64
private static Collection<TestCase> nonEmptyNonProxiesHosts() {
65
List<TestCase> tests = new LinkedList<>();
66
String[] nonProxyHosts = {
67
"google.com",
68
"localhost", "[::1]", "[::0]", "0.0.0.0",
69
"127.0.0.0", "127.0.0.1", "127.0.1.0", "127.0.1.1",
70
"127.1.0.0", "127.1.0.1", "127.1.1.0", "127.1.1.1"};
71
String[] loopbacks = {"localhost", "[::1]", "[::0]", "0.0.0.0",
72
"127.0.0.0", "127.0.0.1", "127.0.1.0", "127.0.1.1",
73
"127.1.0.0", "127.1.0.1", "127.1.1.0", "127.1.1.1"};
74
for (String h : nonProxyHosts) {
75
for (String s : loopbacks) {
76
Map<String, String> properties = new HashMap<>();
77
properties.put("http.proxyHost", "http://proxy.example.com");
78
properties.put("http.nonProxyHosts", h);
79
tests.add(new TestCase(properties, "http://" + s, false));
80
}
81
}
82
return tests;
83
}
84
85
// unsorted tests
86
private static Collection<TestCase> misc() {
87
List<TestCase> t = new LinkedList<>();
88
t.add(new TestCase("oracle.com", "http://137.254.16.101", true));
89
t.add(new TestCase("google.com", "http://74.125.200.101", true));
90
91
t.add(new TestCase("google.com|google.ie", "http://google.co.uk",
92
true));
93
t.add(new TestCase("google.com|google.ie", "http://google.com",
94
false));
95
t.add(new TestCase("google.com|google.ie", "http://google.ie",
96
false));
97
t.add(new TestCase("google.com|google.com|google.ie",
98
"http://google.ie", false));
99
100
t.add(new TestCase("google.com|bing.com|yahoo.com",
101
"http://127.0.0.1", false));
102
t.add(new TestCase("google.com|bing.com|yahoo.com",
103
"http://google.com", false));
104
t.add(new TestCase("google.com|bing.com|yahoo.com",
105
"http://bing.com", false));
106
t.add(new TestCase("google.com|bing.com|yahoo.com",
107
"http://yahoo.com", false));
108
109
t.add(new TestCase("google.com|bing.com", "http://google.com", false));
110
t.add(new TestCase("google.com|bing.com", "http://bing.com", false));
111
t.add(new TestCase("google.com|bing.com", "http://yahoo.com",
112
true));
113
t.add(new TestCase("google.com|bing.co*", "http://google.com", false));
114
t.add(new TestCase("google.com|bing.co*", "http://bing.com", false));
115
t.add(new TestCase("google.com|bing.co*", "http://yahoo.com",
116
true));
117
t.add(new TestCase("google.com|*ing.com", "http://google.com", false));
118
t.add(new TestCase("google.com|*ing.com", "http://bing.com", false));
119
t.add(new TestCase("google.com|*ing.com", "http://yahoo.com",
120
true));
121
t.add(new TestCase("google.co*|bing.com", "http://google.com", false));
122
t.add(new TestCase("google.co*|bing.com", "http://bing.com", false));
123
t.add(new TestCase("google.co*|bing.com", "http://yahoo.com",
124
true));
125
t.add(new TestCase("google.co*|bing.co*", "http://google.com", false));
126
t.add(new TestCase("google.co*|bing.co*", "http://bing.com", false));
127
t.add(new TestCase("google.co*|bing.co*", "http://yahoo.com",
128
true));
129
t.add(new TestCase("google.co*|*ing.com", "http://google.com", false));
130
t.add(new TestCase("google.co*|*ing.com", "http://bing.com", false));
131
t.add(new TestCase("google.co*|*ing.com", "http://yahoo.com",
132
true));
133
t.add(new TestCase("*oogle.com|bing.com", "http://google.com", false));
134
t.add(new TestCase("*oogle.com|bing.com", "http://bing.com", false));
135
t.add(new TestCase("*oogle.com|bing.com", "http://yahoo.com",
136
true));
137
t.add(new TestCase("*oogle.com|bing.co*", "http://google.com", false));
138
t.add(new TestCase("*oogle.com|bing.co*", "http://bing.com", false));
139
t.add(new TestCase("*oogle.com|bing.co*", "http://yahoo.com",
140
true));
141
t.add(new TestCase("*oogle.com|*ing.com", "http://google.com", false));
142
t.add(new TestCase("*oogle.com|*ing.com", "http://bing.com", false));
143
t.add(new TestCase("*oogle.com|*ing.com", "http://yahoo.com",
144
true));
145
146
t.add(new TestCase("google.com|bing.com|yahoo.com", "http://google.com", false));
147
t.add(new TestCase("google.com|bing.com|yahoo.com", "http://bing.com", false));
148
t.add(new TestCase("google.com|bing.com|yahoo.com", "http://yahoo.com", false));
149
t.add(new TestCase("google.com|bing.com|yahoo.com",
150
"http://duckduckgo.com", true));
151
152
t.add(new TestCase("p-proxy.com", "http://p-proxy.com", false));
153
t.add(new TestCase("google.co*|google.ie", "http://google.co.uk",
154
false));
155
156
t.add(new TestCase("*oracle.com", "http://my.oracle.com", false));
157
t.add(new TestCase("google.com|bing.com|yahoo.com", "http://127.0.0.1", false));
158
t.add(new TestCase("google.com|bing.com|yahoo.com", "http://yahoo.com", false));
159
160
// example from
161
// http://docs.oracle.com/javase/7/docs/technotes/guides/net/proxies.html
162
t.add(new TestCase("localhost|host.example.com", "http://localhost",
163
false));
164
t.add(new TestCase("localhost|host.example.com",
165
"http://host.example.com", false));
166
t.add(new TestCase("localhost|host.example.com",
167
"http://google.com", true));
168
return t;
169
}
170
171
172
private static <T> T withSystemPropertiesSet(
173
Map<String, String> localProperties,
174
Callable<? extends T> code) {
175
Map<String, String> backup = new HashMap<>();
176
try {
177
backupAndSetProperties(localProperties, backup);
178
return code.call();
179
} catch (Exception e) {
180
throw new RuntimeException(e);
181
} finally {
182
restoreProperties(backup);
183
}
184
}
185
186
private static void backupAndSetProperties(
187
Map<String, String> localProperties,
188
Map<String, String> oldProperties) {
189
for (Map.Entry<String, String> e : localProperties.entrySet()) {
190
String oldValue = System.setProperty(e.getKey(), e.getValue());
191
oldProperties.put(e.getKey(), oldValue);
192
}
193
}
194
195
private static void restoreProperties(Map<String, String> oldProperties) {
196
for (Map.Entry<String, String> e : oldProperties.entrySet()) {
197
String oldValue = e.getValue();
198
String key = e.getKey();
199
if (oldValue == null)
200
System.getProperties().remove(key);
201
else
202
System.setProperty(key, oldValue);
203
}
204
}
205
206
private static class TestCase {
207
208
final Map<String, String> localProperties;
209
final String urlhost;
210
final boolean expectedProxying;
211
212
TestCase(String nonProxyHosts, String urlhost,
213
boolean expectedProxying) {
214
this(nonProxyHosts, "proxy.example.com", urlhost,
215
expectedProxying);
216
}
217
218
TestCase(String nonProxyHosts, String proxyHost, String urlhost,
219
boolean expectedProxying) {
220
this(new HashMap<String, String>() {
221
{
222
put("http.nonProxyHosts", nonProxyHosts);
223
put("http.proxyHost", proxyHost);
224
}
225
}, urlhost, expectedProxying);
226
}
227
228
TestCase(Map<String, String> localProperties, String urlhost,
229
boolean expectedProxying) {
230
this.localProperties = localProperties;
231
this.urlhost = urlhost;
232
this.expectedProxying = expectedProxying;
233
}
234
235
void run() {
236
System.out.printf("urlhost=%s properties=%s: proxied? %s%n",
237
urlhost, localProperties, expectedProxying);
238
239
List<Proxy> proxies = withSystemPropertiesSet(localProperties,
240
() -> ProxySelector.getDefault().select(
241
URI.create(urlhost))
242
);
243
244
verify(proxies);
245
}
246
247
void verify(List<? extends Proxy> proxies) {
248
249
boolean actualProxying = !(proxies.size() == 1 &&
250
proxies.get(0).type() == Proxy.Type.DIRECT);
251
252
if (actualProxying != expectedProxying)
253
throw new AssertionError(String.format(
254
"Expected %s connection for %s (given " +
255
"properties=%s). Here's the list of proxies " +
256
"returned: %s",
257
expectedProxying ? "proxied" : "direct", urlhost,
258
localProperties, proxies
259
));
260
}
261
}
262
}
263
264