1 /* 2 * Copyright (c) 2015, 2019, 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 8144355 8144062 8176709 8194070 8193802 8231093 27 * @summary Test aliasing additions to ZipFileSystem for multi-release jar files 28 * @library /lib/testlibrary/java/util/jar 29 * @modules jdk.compiler 30 * jdk.jartool 31 * jdk.zipfs 32 * @build Compiler JarBuilder CreateMultiReleaseTestJars 33 * @run testng MultiReleaseJarTest 34 */ 35 36 import java.io.IOException; 37 import java.lang.invoke.MethodHandle; 38 import java.lang.invoke.MethodHandles; 39 import java.lang.invoke.MethodType; 40 import java.lang.Runtime.Version; 41 import java.net.URI; 42 import java.nio.file.*; 43 import java.util.HashMap; 44 import java.util.Map; 45 import java.util.concurrent.atomic.AtomicInteger; 46 47 import org.testng.Assert; 48 import org.testng.annotations.*; 49 50 public class MultiReleaseJarTest { 51 final private int MAJOR_VERSION = Runtime.version().feature(); 52 53 final private String userdir = System.getProperty("user.dir","."); 54 final private CreateMultiReleaseTestJars creator = new CreateMultiReleaseTestJars(); 55 final private Map<String,String> stringEnv = new HashMap<>(); 56 final private Map<String,Integer> integerEnv = new HashMap<>(); 57 final private Map<String,Version> versionEnv = new HashMap<>(); 58 final private String className = "version.Version"; 59 final private MethodType mt = MethodType.methodType(int.class); 60 61 private String entryName; 62 private URI uvuri; 63 private URI mruri; 64 private URI smruri; 65 66 @BeforeClass 67 public void initialize() throws Exception { 68 creator.compileEntries(); 69 creator.buildUnversionedJar(); 70 creator.buildMultiReleaseJar(); 71 creator.buildShortMultiReleaseJar(); 72 String ssp = Paths.get(userdir, "unversioned.jar").toUri().toString(); 73 uvuri = new URI("jar", ssp , null); 74 ssp = Paths.get(userdir, "multi-release.jar").toUri().toString(); 75 mruri = new URI("jar", ssp, null); 76 ssp = Paths.get(userdir, "short-multi-release.jar").toUri().toString(); 77 smruri = new URI("jar", ssp, null); 78 entryName = className.replace('.', '/') + ".class"; 79 } 80 81 public void close() throws IOException { 82 Files.delete(Paths.get(userdir, "unversioned.jar")); 83 Files.delete(Paths.get(userdir, "multi-release.jar")); 84 Files.delete(Paths.get(userdir, "short-multi-release.jar")); 85 } 86 87 @DataProvider(name="strings") 88 public Object[][] createStrings() { 89 return new Object[][]{ 90 {"runtime", MAJOR_VERSION}, 91 {null, 8}, 92 {"8", 8}, 93 {"9", 9}, 94 {Integer.toString(MAJOR_VERSION), MAJOR_VERSION}, 95 {Integer.toString(MAJOR_VERSION+1), MAJOR_VERSION}, 96 {"50", MAJOR_VERSION} 97 }; 98 } 99 100 @DataProvider(name="integers") 101 public Object[][] createIntegers() { 102 return new Object[][] { 103 {null, 8}, 104 {Integer.valueOf(8), 8}, 105 {Integer.valueOf(9), 9}, 106 {Integer.valueOf(MAJOR_VERSION), MAJOR_VERSION}, 107 {Integer.valueOf(MAJOR_VERSION + 1), MAJOR_VERSION}, 108 {Integer.valueOf(100), MAJOR_VERSION} 109 }; 110 } 111 112 @DataProvider(name="versions") 113 public Object[][] createVersions() { 114 return new Object[][] { 115 {null, 8}, 116 {Version.parse("8"), 8}, 117 {Version.parse("9"), 9}, 118 {Version.parse(Integer.toString(MAJOR_VERSION)), MAJOR_VERSION}, 119 {Version.parse(Integer.toString(MAJOR_VERSION) + 1), MAJOR_VERSION}, 120 {Version.parse("100"), MAJOR_VERSION} 121 }; 122 } 123 124 @DataProvider(name="invalidVersions") 125 public Object[][] invalidVersions() { 126 return new Object[][] { 127 {Map.of("releaseVersion", "")}, 128 {Map.of("releaseVersion", "invalid")}, 129 {Map.of("releaseVersion", "0")}, 130 {Map.of("releaseVersion", "-1")}, 131 {Map.of("releaseVersion", "11.0.1")}, 132 {Map.of("releaseVersion",Integer.valueOf(0))}, 133 {Map.of("releaseVersion",Integer.valueOf(-1))} 134 }; 135 } 136 137 // Not the best test but all I can do since ZipFileSystem and JarFileSystem 138 // are not public, so I can't use (fs instanceof ...) 139 @Test 140 public void testNewFileSystem() throws Exception { 141 Map<String,String> env = new HashMap<>(); 142 // no configuration, treat multi-release jar as unversioned 143 try (FileSystem fs = FileSystems.newFileSystem(mruri, env)) { 144 Assert.assertTrue(readAndCompare(fs, 8)); 145 } 146 env.put("releaseVersion", "runtime"); 147 // a configuration and jar file is multi-release 148 try (FileSystem fs = FileSystems.newFileSystem(mruri, env)) { 149 Assert.assertTrue(readAndCompare(fs, MAJOR_VERSION)); 150 } 151 // a configuration but jar file is unversioned 152 try (FileSystem fs = FileSystems.newFileSystem(uvuri, env)) { 153 Assert.assertTrue(readAndCompare(fs, 8)); 154 } 155 } 156 157 private boolean readAndCompare(FileSystem fs, int expected) throws IOException { 158 Path path = fs.getPath("version/Version.java"); 159 String src = new String(Files.readAllBytes(path)); 160 return src.contains("return " + expected); 161 } 162 163 @Test(dataProvider="strings") 164 public void testStrings(String value, int expected) throws Throwable { 165 stringEnv.put("releaseVersion", value); 166 runTest(stringEnv, expected); 167 } 168 169 @Test(dataProvider="integers") 170 public void testIntegers(Integer value, int expected) throws Throwable { 171 integerEnv.put("releaseVersion", value); 172 runTest(integerEnv, expected); 173 } 174 175 @Test(dataProvider="versions") 176 public void testVersions(Version value, int expected) throws Throwable { 177 versionEnv.put("releaseVersion", value); 178 runTest(versionEnv, expected); 179 } 180 181 @Test 182 public void testShortJar() throws Throwable { 183 integerEnv.put("releaseVersion", Integer.valueOf(MAJOR_VERSION)); 184 runTest(smruri, integerEnv, MAJOR_VERSION); 185 integerEnv.put("releaseVersion", Integer.valueOf(9)); 186 runTest(smruri, integerEnv, 8); 187 } 188 189 /** 190 * Validate that an invalid value for the "releaseVersion" property throws 191 * an {@code IllegalArgumentException} 192 * @param env Zip FS map 193 * @throws Throwable Exception thrown for anything other than the expected 194 * IllegalArgumentException 195 */ 196 @Test(dataProvider="invalidVersions") 197 public void testInvalidVersions(Map<String,?> env) throws Throwable { 198 Assert.assertThrows(IllegalArgumentException.class, () -> 199 FileSystems.newFileSystem(Path.of(userdir, 200 "multi-release.jar"), env)); 201 } 202 203 // The following tests are for backwards compatibility to validate that 204 // the original property still works 205 @Test(dataProvider="strings") 206 public void testMRStrings(String value, int expected) throws Throwable { 207 stringEnv.clear(); 208 stringEnv.put("multi-release", value); 209 runTest(stringEnv, expected); 210 } 211 212 @Test(dataProvider="integers") 213 public void testMRIntegers(Integer value, int expected) throws Throwable { 214 integerEnv.clear(); 215 integerEnv.put("multi-release", value); 216 runTest(integerEnv, expected); 217 } 218 219 @Test(dataProvider="versions") 220 public void testMRVersions(Version value, int expected) throws Throwable { 221 versionEnv.clear(); 222 versionEnv.put("multi-release", value); 223 runTest(versionEnv, expected); 224 } 225 226 private void runTest(Map<String,?> env, int expected) throws Throwable { 227 runTest(mruri, env, expected); 228 } 229 230 private void runTest(URI uri, Map<String,?> env, int expected) throws Throwable { 231 try (FileSystem fs = FileSystems.newFileSystem(uri, env)) { 232 Path version = fs.getPath(entryName); 233 byte [] bytes = Files.readAllBytes(version); 234 Class<?> vcls = (new ByteArrayClassLoader(fs)).defineClass(className, bytes); 235 MethodHandle mh = MethodHandles.lookup().findVirtual(vcls, "getVersion", mt); 236 Assert.assertEquals((int)mh.invoke(vcls.getDeclaredConstructor().newInstance()), expected); 237 } 238 } 239 240 @Test 241 public void testIsMultiReleaseJar() throws Exception { 242 // Re-examine commented out tests as part of JDK-8176843 243 testCustomMultiReleaseValue("true", true); 244 testCustomMultiReleaseValue("true\r\nOther: value", true); 245 testCustomMultiReleaseValue("true\nOther: value", true); 246 //testCustomMultiReleaseValue("true\rOther: value", true); 247 248 testCustomMultiReleaseValue("false", false); 249 testCustomMultiReleaseValue(" true", false); 250 testCustomMultiReleaseValue("true ", false); 251 //testCustomMultiReleaseValue("true\n ", false); 252 //testCustomMultiReleaseValue("true\r ", false); 253 //testCustomMultiReleaseValue("true\n true", false); 254 //testCustomMultiReleaseValue("true\r\n true", false); 255 } 256 257 @Test 258 public void testMultiReleaseJarWithNonVersionDir() throws Exception { 259 String jfname = "multi-release-non-ver.jar"; 260 Path jfpath = Paths.get(jfname); 261 URI uri = new URI("jar", jfpath.toUri().toString() , null); 262 JarBuilder jb = new JarBuilder(jfname); 263 jb.addAttribute("Multi-Release", "true"); 264 jb.build(); 265 Map<String,String> env = Map.of("releaseVersion", "runtime"); 266 try (FileSystem fs = FileSystems.newFileSystem(uri, env)) { 267 Assert.assertTrue(true); 268 } 269 Files.delete(jfpath); 270 } 271 272 private static final AtomicInteger JAR_COUNT = new AtomicInteger(0); 273 274 private void testCustomMultiReleaseValue(String value, boolean expected) 275 throws Exception { 276 String fileName = "custom-mr" + JAR_COUNT.incrementAndGet() + ".jar"; 277 creator.buildCustomMultiReleaseJar(fileName, value, Map.of(), 278 /*addEntries*/true); 279 280 Map<String,String> env = Map.of("releaseVersion", "runtime"); 281 Path filePath = Paths.get(userdir, fileName); 282 String ssp = filePath.toUri().toString(); 283 URI customJar = new URI("jar", ssp , null); 284 try (FileSystem fs = FileSystems.newFileSystem(customJar, env)) { 285 if (expected) { 286 Assert.assertTrue(readAndCompare(fs, MAJOR_VERSION)); 287 } else { 288 Assert.assertTrue(readAndCompare(fs, 8)); 289 } 290 } 291 Files.delete(filePath); 292 } 293 294 private static class ByteArrayClassLoader extends ClassLoader { 295 final private FileSystem fs; 296 297 ByteArrayClassLoader(FileSystem fs) { 298 super(null); 299 this.fs = fs; 300 } 301 302 @Override 303 public Class<?> loadClass(String name) throws ClassNotFoundException { 304 try { 305 return super.loadClass(name); 306 } catch (ClassNotFoundException x) {} 307 Path cls = fs.getPath(name.replace('.', '/') + ".class"); 308 try { 309 byte[] bytes = Files.readAllBytes(cls); 310 return defineClass(name, bytes); 311 } catch (IOException x) { 312 throw new ClassNotFoundException(x.getMessage()); 313 } 314 } 315 316 public Class<?> defineClass(String name, byte[] bytes) throws ClassNotFoundException { 317 if (bytes == null) throw new ClassNotFoundException("No bytes for " + name); 318 return defineClass(name, bytes, 0, bytes.length); 319 } 320 } 321 }