Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/URLPermission/nstest/LookupTest.java
41152 views
1
/*
2
* Copyright (c) 2013, 2021, 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
* @summary A simple smoke test of the HttpURLPermission mechanism, which checks
27
* for either IOException (due to unknown host) or SecurityException
28
* due to lack of permission to connect
29
* @run main/othervm -Djava.security.manager=allow -Djdk.net.hosts.file=LookupTestHosts LookupTest
30
*/
31
32
import java.io.BufferedWriter;
33
import java.io.FilePermission;
34
import java.io.FileWriter;
35
import java.io.IOException;
36
import java.io.InputStream;
37
import java.io.OutputStream;
38
import java.io.PrintWriter;
39
import java.net.InetAddress;
40
import java.net.InetSocketAddress;
41
import java.net.NetPermission;
42
import java.net.ProxySelector;
43
import java.net.ServerSocket;
44
import java.net.Socket;
45
import java.net.SocketPermission;
46
import java.net.URL;
47
import java.net.URLConnection;
48
import java.net.URLPermission;
49
import java.security.CodeSource;
50
import java.security.Permission;
51
import java.security.PermissionCollection;
52
import java.security.Permissions;
53
import java.security.Policy;
54
import java.security.ProtectionDomain;
55
import static java.nio.charset.StandardCharsets.US_ASCII;
56
57
public class LookupTest {
58
59
static final Policy DEFAULT_POLICY = Policy.getPolicy();
60
static int port;
61
static volatile ServerSocket serverSocket;
62
63
static void test(String url,
64
boolean throwsSecException,
65
boolean throwsIOException) {
66
ProxySelector.setDefault(null);
67
URL u;
68
InputStream is = null;
69
try {
70
u = new URL(url);
71
System.err.println("Connecting to " + u);
72
URLConnection urlc = u.openConnection();
73
is = urlc.getInputStream();
74
} catch (SecurityException e) {
75
if (!throwsSecException) {
76
throw new RuntimeException("Unexpected SecurityException:", e);
77
}
78
return;
79
} catch (IOException e) {
80
if (!throwsIOException) {
81
System.err.println("Unexpected IOException:" + e.getMessage());
82
throw new RuntimeException(e);
83
}
84
return;
85
} finally {
86
if (is != null) {
87
try {
88
is.close();
89
} catch (IOException e) {
90
System.err.println("Unexpected IOException:" + e.getMessage());
91
throw new RuntimeException(e);
92
}
93
}
94
}
95
96
if (throwsSecException || throwsIOException) {
97
System.err.printf("was expecting a %s\n", throwsSecException
98
? "security exception" : "IOException");
99
throw new RuntimeException("was expecting an exception");
100
}
101
}
102
103
static final String HOSTS_FILE_NAME = System.getProperty("jdk.net.hosts.file");
104
105
public static void main(String args[]) throws Exception {
106
addMappingToHostsFile("allowedAndFound.com",
107
InetAddress.getLoopbackAddress().getHostAddress(),
108
HOSTS_FILE_NAME,
109
false);
110
addMappingToHostsFile("notAllowedButFound.com",
111
"99.99.99.99",
112
HOSTS_FILE_NAME,
113
true);
114
// name "notAllowedAndNotFound.com" is not in map
115
// name "allowedButNotfound.com" is not in map
116
Server server = new Server();
117
try {
118
Policy.setPolicy(new LookupTestPolicy());
119
System.setSecurityManager(new SecurityManager());
120
server.start();
121
test("http://allowedAndFound.com:" + port + "/foo", false, false);
122
test("http://notAllowedButFound.com:" + port + "/foo", true, false);
123
test("http://allowedButNotfound.com:" + port + "/foo", false, true);
124
test("http://notAllowedAndNotFound.com:" + port + "/foo", true, false);
125
} finally {
126
server.terminate();
127
}
128
}
129
130
static class Server extends Thread {
131
private volatile boolean done;
132
133
public Server() throws IOException {
134
InetAddress loopback = InetAddress.getLoopbackAddress();
135
serverSocket = new ServerSocket();
136
serverSocket.bind(new InetSocketAddress(loopback, 0));
137
port = serverSocket.getLocalPort();
138
}
139
140
public void run() {
141
try {
142
while (!done) {
143
try (Socket s = serverSocket.accept()) {
144
readOneRequest(s.getInputStream());
145
OutputStream o = s.getOutputStream();
146
String rsp = "HTTP/1.1 200 Ok\r\n" +
147
"Connection: close\r\n" +
148
"Content-length: 0\r\n\r\n";
149
o.write(rsp.getBytes(US_ASCII));
150
}
151
}
152
} catch (IOException e) {
153
if (!done)
154
e.printStackTrace();
155
}
156
}
157
158
void terminate() {
159
done = true;
160
try { serverSocket.close(); }
161
catch (IOException unexpected) { unexpected.printStackTrace(); }
162
}
163
164
static final byte[] requestEnd = new byte[] {'\r', '\n', '\r', '\n' };
165
166
// Read until the end of a HTTP request
167
void readOneRequest(InputStream is) throws IOException {
168
int requestEndCount = 0, r;
169
while ((r = is.read()) != -1) {
170
if (r == requestEnd[requestEndCount]) {
171
requestEndCount++;
172
if (requestEndCount == 4) {
173
break;
174
}
175
} else {
176
requestEndCount = 0;
177
}
178
}
179
}
180
}
181
182
private static void addMappingToHostsFile(String host,
183
String addr,
184
String hostsFileName,
185
boolean append)
186
throws IOException
187
{
188
String mapping = addr + " " + host;
189
try (FileWriter fr = new FileWriter(hostsFileName, append);
190
PrintWriter hfPWriter = new PrintWriter(new BufferedWriter(fr))) {
191
hfPWriter.println(mapping);
192
}
193
}
194
195
static class LookupTestPolicy extends Policy {
196
final PermissionCollection perms = new Permissions();
197
198
LookupTestPolicy() throws Exception {
199
perms.add(new NetPermission("setProxySelector"));
200
perms.add(new SocketPermission("localhost:1024-", "resolve,accept"));
201
perms.add(new URLPermission("http://allowedAndFound.com:" + port + "/-", "*:*"));
202
perms.add(new URLPermission("http://allowedButNotfound.com:" + port + "/-", "*:*"));
203
perms.add(new FilePermission("<<ALL FILES>>", "read,write,delete"));
204
//perms.add(new PropertyPermission("java.io.tmpdir", "read"));
205
}
206
207
public PermissionCollection getPermissions(ProtectionDomain domain) {
208
return perms;
209
}
210
211
public PermissionCollection getPermissions(CodeSource codesource) {
212
return perms;
213
}
214
215
public boolean implies(ProtectionDomain domain, Permission perm) {
216
return perms.implies(perm) || DEFAULT_POLICY.implies(domain, perm);
217
}
218
}
219
}
220
221