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/URLPermission/nstest/LookupTest.java
47490 views
1
/*
2
* Copyright (c) 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
* This is a simple smoke test of the HttpURLPermission mechanism, which
26
* checks for either IOException (due to unknown host) or SecurityException
27
* due to lack of permission to connect
28
*/
29
30
import java.net.*;
31
import java.io.*;
32
import jdk.testlibrary.Utils;
33
34
public class LookupTest {
35
36
static void test(
37
String url, boolean throwsSecException, boolean throwsIOException)
38
{
39
try {
40
URL u = new URL(url);
41
System.err.println ("Connecting to " + u);
42
URLConnection urlc = u.openConnection();
43
InputStream is = urlc.getInputStream();
44
} catch (SecurityException e) {
45
if (!throwsSecException) {
46
throw new RuntimeException ("(1) was not expecting ", e);
47
}
48
return;
49
} catch (IOException ioe) {
50
if (!throwsIOException) {
51
throw new RuntimeException ("(2) was not expecting ", ioe);
52
}
53
return;
54
}
55
if (throwsSecException || throwsIOException) {
56
System.err.printf ("was expecting a %s\n", throwsSecException ?
57
"security exception" : "IOException");
58
throw new RuntimeException("was expecting an exception");
59
}
60
}
61
62
static int port;
63
static ServerSocket serverSocket;
64
65
public static void main(String args[]) throws Exception {
66
String cmd = args[0];
67
if (cmd.equals("-getport")) {
68
port = Utils.getFreePort();
69
System.out.print(port);
70
} else if (cmd.equals("-runtest")) {
71
port = Integer.parseInt(args[1]);
72
SimpleNameService.put("allowedAndFound.com", "127.0.0.1");
73
SimpleNameService.put("notAllowedButFound.com", "99.99.99.99");
74
// name "notAllowedAndNotFound.com" is not in map
75
// name "allowedButNotfound.com" is not in map
76
try {
77
startServer();
78
79
System.setSecurityManager(new SecurityManager());
80
81
test("http://allowedAndFound.com:" + port + "/foo", false, false);
82
83
test("http://notAllowedButFound.com:" + port + "/foo", true, false);
84
85
test("http://allowedButNotfound.com:" + port + "/foo", false, true);
86
87
test("http://notAllowedAndNotFound.com:" + port + "/foo", true, false);
88
} finally {
89
serverSocket.close();
90
}
91
} else {
92
throw new RuntimeException("Bad invocation: " + cmd);
93
}
94
}
95
96
static Thread server;
97
98
static class Server extends Thread {
99
public void run() {
100
byte[] buf = new byte[1000];
101
try {
102
while (true) {
103
Socket s = serverSocket.accept();
104
InputStream i = s.getInputStream();
105
i.read(buf);
106
OutputStream o = s.getOutputStream();
107
String rsp = "HTTP/1.1 200 Ok\r\n" +
108
"Connection: close\r\nContent-length: 0\r\n\r\n";
109
o.write(rsp.getBytes());
110
o.close();
111
}
112
} catch (IOException e) {
113
return;
114
}
115
}
116
}
117
118
static void startServer() {
119
try {
120
serverSocket = new ServerSocket(port);
121
server = new Server();
122
server.start();
123
} catch (Exception e) {
124
throw new RuntimeException ("Test failed to initialize", e);
125
}
126
}
127
}
128
129