1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
|
/*
* Copyright (c) 2015, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 8132734 8144062 8165723 8199172
* @summary Test the extended API and the aliasing additions in JarFile that
* support multi-release jar files
* @library /lib/testlibrary/java/util/jar /test/lib
* @build jdk.test.lib.RandomFactory
* CreateMultiReleaseTestJars
* jdk.test.lib.compiler.Compiler
* jdk.test.lib.util.JarBuilder
* @run testng MultiReleaseJarAPI
*/
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import jdk.test.lib.RandomFactory;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class MultiReleaseJarAPI {
private static final Random RANDOM = RandomFactory.getRandom();
String userdir = System.getProperty("user.dir",".");
CreateMultiReleaseTestJars creator = new CreateMultiReleaseTestJars();
File unversioned = new File(userdir, "unversioned.jar");
File multirelease = new File(userdir, "multi-release.jar");
File signedmultirelease = new File(userdir, "signed-multi-release.jar");
@BeforeClass
public void initialize() throws Exception {
creator.compileEntries();
creator.buildUnversionedJar();
creator.buildMultiReleaseJar();
creator.buildSignedMultiReleaseJar();
}
@AfterClass
public void close() throws IOException {
Files.delete(unversioned.toPath());
Files.delete(multirelease.toPath());
Files.delete(signedmultirelease.toPath());
}
@Test
public void isMultiReleaseJar() throws Exception {
try (JarFile jf = new JarFile(unversioned)) {
Assert.assertFalse(jf.isMultiRelease());
}
try (JarFile jf = new JarFile(unversioned, true, ZipFile.OPEN_READ, Runtime.version())) {
Assert.assertFalse(jf.isMultiRelease());
}
try (JarFile jf = new JarFile(multirelease)) {
Assert.assertTrue(jf.isMultiRelease());
}
try (JarFile jf = new JarFile(multirelease, true, ZipFile.OPEN_READ, Runtime.version())) {
Assert.assertTrue(jf.isMultiRelease());
}
testCustomMultiReleaseValue("true", true);
testCustomMultiReleaseValue("true\r\nOther: value", true);
testCustomMultiReleaseValue("true\nOther: value", true);
testCustomMultiReleaseValue("true\rOther: value", true);
testCustomMultiReleaseValue("false", false);
testCustomMultiReleaseValue(" true", false);
testCustomMultiReleaseValue("true ", false);
testCustomMultiReleaseValue("true\n true", false);
testCustomMultiReleaseValue("true\r true", false);
testCustomMultiReleaseValue("true\r\n true", false);
// "Multi-Release: true/false" not in main attributes
testCustomMultiReleaseValue("\r\n\r\nName: test\r\nMulti-Release: true\r\n",
false);
testCustomMultiReleaseValue("\n\nName: entryname\nMulti-Release: true\n",
false);
testCustomMultiReleaseValue("EndOfMainAttr: whatever\r\n" +
"\r\nName: entryname\r\nMulti-Release: true\r\n",
false);
testCustomMultiReleaseValue("EndOfMainAttr: whatever\r\n" +
"\nName: entryname\nMulti-Release: true\n",
false);
// generate "random" Strings to use as extra attributes, and
// verify that Multi-Release: true is always properly matched
for (int i = 0; i < 100; i++) {
byte[] keyBytes = new byte[RANDOM.nextInt(70) + 1];
Arrays.fill(keyBytes, (byte)('a' + RANDOM.nextInt(24)));
byte[] valueBytes = new byte[RANDOM.nextInt(70) + 1];
Arrays.fill(valueBytes, (byte)('a' + RANDOM.nextInt(24)));
String key = new String(keyBytes, StandardCharsets.UTF_8);
String value = new String(valueBytes, StandardCharsets.UTF_8);
// test that Multi-Release: true anywhere in the manifest always
// return true
testCustomMultiReleaseValue("true", Map.of(key, value), true);
// test that we don't get any false positives
testCustomMultiReleaseValue("false", Map.of(key, value), false);
}
}
private void testCustomMultiReleaseValue(String value, boolean expected)
throws Exception {
testCustomMultiReleaseValue(value, Map.of(), expected);
}
private static final AtomicInteger JAR_COUNT = new AtomicInteger(0);
private void testCustomMultiReleaseValue(String value,
Map<String, String> extraAttributes, boolean expected)
throws Exception {
String fileName = "custom-mr" + JAR_COUNT.incrementAndGet() + ".jar";
creator.buildCustomMultiReleaseJar(fileName, value, extraAttributes);
File custom = new File(userdir, fileName);
try (JarFile jf = new JarFile(custom, true, ZipFile.OPEN_READ, Runtime.version())) {
Assert.assertEquals(jf.isMultiRelease(), expected);
}
Files.delete(custom.toPath());
}
@DataProvider(name = "versions")
public Object[][] createVersionData() throws Exception {
return new Object[][]{
{JarFile.baseVersion(), 8},
{JarFile.runtimeVersion(), Runtime.version().major()},
{Runtime.version(), Runtime.version().major()},
{Runtime.Version.parse("7.1"), JarFile.baseVersion().major()},
{Runtime.Version.parse("9"), 9},
{Runtime.Version.parse("9.1.5-ea+200"), 9}
};
}
@Test(dataProvider="versions")
public void testVersioning(Runtime.Version value, int xpected) throws Exception {
Runtime.Version expected = Runtime.Version.parse(String.valueOf(xpected));
Runtime.Version base = JarFile.baseVersion();
// multi-release jar, opened as unversioned
try (JarFile jar = new JarFile(multirelease)) {
Assert.assertEquals(jar.getVersion(), base);
}
System.err.println("test versioning for Release " + value);
try (JarFile jf = new JarFile(multirelease, true, ZipFile.OPEN_READ, value)) {
Assert.assertEquals(jf.getVersion(), expected);
}
// regular, unversioned, jar
try (JarFile jf = new JarFile(unversioned, true, ZipFile.OPEN_READ, value)) {
Assert.assertEquals(jf.getVersion(), base);
}
}
@Test(dataProvider="versions")
public void testAliasing(Runtime.Version version, int xpected) throws Exception {
int n = Math.max(version.major(), JarFile.baseVersion().major());
Runtime.Version value = Runtime.Version.parse(String.valueOf(n));
System.err.println("test aliasing for Release " + version);
String prefix;
if (JarFile.baseVersion().equals(value)) {
prefix = "";
} else {
prefix = "META-INF/versions/" + value.major() + "/";
}
// test both multi-release jars
readAndCompare(multirelease, value, "README", prefix + "README");
readAndCompare(multirelease, value, "version/Version.class", prefix + "version/Version.class");
// and signed multi-release jars
readAndCompare(signedmultirelease, value, "README", prefix + "README");
readAndCompare(signedmultirelease, value, "version/Version.class", prefix + "version/Version.class");
}
private void readAndCompare(File jar, Runtime.Version version, String name, String realName) throws Exception {
byte[] baseBytes;
byte[] versionedBytes;
try (JarFile jf = new JarFile(jar, true, ZipFile.OPEN_READ, JarFile.baseVersion())) {
ZipEntry ze = jf.getEntry(realName);
try (InputStream is = jf.getInputStream(ze)) {
baseBytes = is.readAllBytes();
}
}
assert baseBytes.length > 0;
try (JarFile jf = new JarFile(jar, true, ZipFile.OPEN_READ, version)) {
ZipEntry ze = jf.getEntry(name);
try (InputStream is = jf.getInputStream(ze)) {
versionedBytes = is.readAllBytes();
}
}
assert versionedBytes.length > 0;
Assert.assertTrue(Arrays.equals(baseBytes, versionedBytes));
}
@Test
public void testNames() throws Exception {
String rname = "version/Version.class";
String vname = "META-INF/versions/9/version/Version.class";
ZipEntry ze1;
ZipEntry ze2;
try (JarFile jf = new JarFile(multirelease)) {
ze1 = jf.getEntry(vname);
}
Assert.assertEquals(ze1.getName(), vname);
try (JarFile jf = new JarFile(multirelease, true, ZipFile.OPEN_READ, Runtime.Version.parse("9"))) {
ze2 = jf.getEntry(rname);
}
Assert.assertEquals(ze2.getName(), rname);
Assert.assertNotEquals(ze1.getName(), ze2.getName());
}
}
|