Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/net/www/protocol/jar/MultiReleaseJarURLConnection.java
41159 views
1
/*
2
* Copyright (c) 2015, 2020, 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
* @bug 8132734 8144062 8159785 8194070
27
* @summary Test that URL connections to multi-release jars can be runtime versioned
28
* @library /lib/testlibrary/java/util/jar /test/lib
29
* @modules jdk.compiler
30
* jdk.httpserver
31
* jdk.jartool
32
* @build CreateMultiReleaseTestJars
33
* jdk.test.lib.net.SimpleHttpServer
34
* jdk.test.lib.util.JarBuilder
35
* jdk.test.lib.compiler.Compiler
36
* @run testng MultiReleaseJarURLConnection
37
*/
38
39
import java.io.IOException;
40
import java.io.InputStream;
41
import java.lang.invoke.MethodHandle;
42
import java.lang.invoke.MethodHandles;
43
import java.lang.invoke.MethodType;
44
import java.net.InetAddress;
45
import java.net.InetSocketAddress;
46
import java.net.JarURLConnection;
47
import java.net.MalformedURLException;
48
import java.net.URI;
49
import java.net.URISyntaxException;
50
import java.net.URL;
51
import java.net.URLClassLoader;
52
import java.net.URLConnection;
53
import java.nio.file.Files;
54
import java.nio.file.Paths;
55
import java.util.Enumeration;
56
import java.util.jar.JarFile;
57
58
import jdk.test.lib.net.SimpleHttpServer;
59
import jdk.test.lib.net.URIBuilder;
60
import org.testng.Assert;
61
import org.testng.annotations.AfterClass;
62
import org.testng.annotations.BeforeClass;
63
import org.testng.annotations.DataProvider;
64
import org.testng.annotations.Test;
65
66
public class MultiReleaseJarURLConnection {
67
String userdir = System.getProperty("user.dir", ".");
68
String unversioned = userdir + "/unversioned.jar";
69
String unsigned = userdir + "/multi-release.jar";
70
String signed = userdir + "/signed-multi-release.jar";
71
static final String TESTCONTEXT = "/multi-release.jar";
72
SimpleHttpServer server;
73
74
@BeforeClass
75
public void initialize() throws Exception {
76
CreateMultiReleaseTestJars creator = new CreateMultiReleaseTestJars();
77
creator.compileEntries();
78
creator.buildUnversionedJar();
79
creator.buildMultiReleaseJar();
80
creator.buildSignedMultiReleaseJar();
81
server = new SimpleHttpServer(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), TESTCONTEXT, System.getProperty("user.dir", "."));
82
server.start();
83
}
84
85
@AfterClass
86
public void close() throws IOException {
87
// Windows requires server to stop before file is deleted
88
if (server != null)
89
server.stop();
90
Files.delete(Paths.get(unversioned));
91
Files.delete(Paths.get(unsigned));
92
Files.delete(Paths.get(signed));
93
}
94
95
@DataProvider(name = "data")
96
public Object[][] createData() {
97
return new Object[][]{
98
{"unversioned", unversioned},
99
{"unsigned", unsigned},
100
{"signed", signed}
101
};
102
}
103
104
@Test(dataProvider = "data")
105
public void testRuntimeVersioning(String style, String file) throws Exception {
106
String urlFile = "jar:file:" + file + "!/";
107
String baseUrlEntry = urlFile + "version/Version.java";
108
String rtreturn = "return " + Runtime.version().major();
109
110
Assert.assertTrue(readAndCompare(new URL(baseUrlEntry), "return 8"));
111
// #runtime is "magic" for a multi-release jar, but not for unversioned jar
112
Assert.assertTrue(readAndCompare(new URL(baseUrlEntry + "#runtime"),
113
style.equals("unversioned") ? "return 8" : rtreturn));
114
// #fragment or any other fragment is not magic
115
Assert.assertTrue(readAndCompare(new URL(baseUrlEntry + "#fragment"), "return 8"));
116
// cached entities not affected
117
Assert.assertTrue(readAndCompare(new URL(baseUrlEntry), "return 8"));
118
119
// the following tests will not work with unversioned jars
120
if (style.equals("unversioned"))
121
return;
122
123
// direct access to versioned entry
124
String versUrlEntry = urlFile + "META-INF/versions/" + Runtime.version().major()
125
+ "/version/Version.java";
126
Assert.assertTrue(readAndCompare(new URL(versUrlEntry), rtreturn));
127
// adding any fragment does not change things
128
Assert.assertTrue(readAndCompare(new URL(versUrlEntry + "#runtime"), rtreturn));
129
Assert.assertTrue(readAndCompare(new URL(versUrlEntry + "#fragment"), rtreturn));
130
}
131
132
@Test(dataProvider = "data")
133
public void testCachedJars(String style, String file) throws Exception {
134
String urlFile = "jar:file:" + file + "!/";
135
136
URL rootUrl = new URL(urlFile);
137
JarURLConnection juc = (JarURLConnection) rootUrl.openConnection();
138
JarFile rootJar = juc.getJarFile();
139
Runtime.Version root = rootJar.getVersion();
140
141
URL runtimeUrl = new URL(urlFile + "#runtime");
142
juc = (JarURLConnection) runtimeUrl.openConnection();
143
JarFile runtimeJar = juc.getJarFile();
144
Runtime.Version runtime = runtimeJar.getVersion();
145
if (style.equals("unversioned")) {
146
Assert.assertEquals(root, runtime);
147
} else {
148
Assert.assertNotEquals(root, runtime);
149
}
150
151
juc = (JarURLConnection) rootUrl.openConnection();
152
JarFile jar = juc.getJarFile();
153
Assert.assertEquals(jar.getVersion(), root);
154
Assert.assertEquals(jar, rootJar);
155
156
juc = (JarURLConnection) runtimeUrl.openConnection();
157
jar = juc.getJarFile();
158
Assert.assertEquals(jar.getVersion(), runtime);
159
Assert.assertEquals(jar, runtimeJar);
160
161
rootJar.close();
162
runtimeJar.close();
163
jar.close(); // probably not needed
164
}
165
166
@DataProvider(name = "resourcedata")
167
public Object[][] createResourceData() throws Exception {
168
return new Object[][]{
169
{"unversioned", Paths.get(unversioned).toUri().toURL()},
170
{"unsigned", Paths.get(unsigned).toUri().toURL()},
171
{"signed", Paths.get(signed).toUri().toURL()},
172
{"unversioned", new URL("file:" + unversioned)},
173
{"unsigned", new URL("file:" + unsigned)},
174
{"signed", new URL("file:" + signed)},
175
{"unversioned", new URL("jar:file:" + unversioned + "!/")},
176
{"unsigned", new URL("jar:file:" + unsigned + "!/")},
177
{"signed", new URL("jar:file:" + signed + "!/")},
178
// external jar received via http protocol
179
{"http", toHttpJarURL(server.getPort(), "/multi-release.jar", "!/")},
180
{"http", URIBuilder.newBuilder().scheme("http").port(server.getPort())
181
.loopback().path("/multi-release.jar").toURL()},
182
};
183
}
184
185
@Test(dataProvider = "resourcedata")
186
public void testResources(String style, URL url) throws Throwable {
187
// System.out.println(" testing " + style + " url: " + url);
188
URL[] urls = {url};
189
URLClassLoader cldr = new URLClassLoader(urls);
190
Class<?> vcls = cldr.loadClass("version.Version");
191
192
// verify we are loading a runtime versioned class
193
MethodType mt = MethodType.methodType(int.class);
194
MethodHandle mh = MethodHandles.lookup().findVirtual(vcls, "getVersion", mt);
195
Assert.assertEquals((int)mh.invoke(vcls.newInstance()),
196
style.equals("unversioned") ? 8 : Runtime.version().major());
197
198
// now get a resource and verify that we don't have a fragment attached
199
Enumeration<URL> vclsUrlEnum = cldr.getResources("version/Version.class");
200
Assert.assertTrue(vclsUrlEnum.hasMoreElements());
201
URL vclsUrls[] = new URL[] {
202
vcls.getResource("/version/Version.class"),
203
vcls.getResource("Version.class"),
204
cldr.getResource("version/Version.class"),
205
vclsUrlEnum.nextElement()
206
};
207
Assert.assertFalse(vclsUrlEnum.hasMoreElements());
208
for (URL vclsUrl : vclsUrls) {
209
String fragment = vclsUrl.getRef();
210
Assert.assertNull(fragment);
211
212
// and verify that the the url is a reified pointer to the runtime entry
213
String rep = vclsUrl.toString();
214
//System.out.println(" getResource(\"/version/Version.class\") returned: " + rep);
215
if (style.equals("http")) {
216
Assert.assertTrue(rep.startsWith("jar:http:"));
217
} else {
218
Assert.assertTrue(rep.startsWith("jar:file:"));
219
}
220
String suffix;
221
if (style.equals("unversioned")) {
222
suffix = ".jar!/version/Version.class";
223
} else {
224
suffix = ".jar!/META-INF/versions/" + Runtime.version().major()
225
+ "/version/Version.class";
226
}
227
Assert.assertTrue(rep.endsWith(suffix));
228
}
229
cldr.close();
230
}
231
232
private static URL toHttpJarURL(int port, String jar, String file)
233
throws MalformedURLException, URISyntaxException {
234
assert file.startsWith("!/");
235
URI httpURI = URIBuilder.newBuilder()
236
.scheme("http")
237
.loopback()
238
.port(port)
239
.path(jar)
240
.build();
241
return new URL("jar:" + httpURI + file);
242
}
243
244
private boolean readAndCompare(URL url, String match) throws Exception {
245
boolean result;
246
// necessary to do it this way, instead of openStream(), so we can
247
// close underlying JarFile, otherwise windows can't delete the file
248
URLConnection conn = url.openConnection();
249
try (InputStream is = conn.getInputStream()) {
250
byte[] bytes = is.readAllBytes();
251
result = (new String(bytes)).contains(match);
252
}
253
if (conn instanceof JarURLConnection) {
254
((JarURLConnection) conn).getJarFile().close();
255
}
256
return result;
257
}
258
}
259
260