1 /* 2 * Copyright (c) 2016, 2018, 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 * @library /lib/testlibrary 27 * @modules jdk.compiler 28 * @build AddExportsAndOpensInManifest Test2 JarUtils jdk.testlibrary.* 29 * @compile --add-exports=java.base/jdk.internal.misc=ALL-UNNAMED Test1.java 30 * @run testng AddExportsAndOpensInManifest 31 * @summary Basic test for Add-Exports and Add-Opens attributes in the 32 * manifest of a main application JAR 33 */ 34 35 import java.lang.reflect.Method; 36 import java.nio.file.Files; 37 import java.nio.file.Path; 38 import java.nio.file.Paths; 39 import java.util.jar.Attributes; 40 import java.util.jar.Manifest; 41 42 import jdk.testlibrary.OutputAnalyzer; 43 import jdk.testlibrary.ProcessTools; 44 45 import org.testng.annotations.BeforeMethod; 46 import org.testng.annotations.Test; 47 import static org.testng.Assert.*; 48 49 50 @Test 51 public class AddExportsAndOpensInManifest { 52 53 private String testName; 54 private int testCaseNum; 55 56 @BeforeMethod 57 public void getTestName(Method m){ 58 testName = m.getName(); 59 testCaseNum = 0; 60 } 61 62 /** 63 * Package Test1 and Test2 into a JAR file with the given attributes 64 * in the JAR manifest, then execute the JAR file with `java -jar`. 65 */ 66 private OutputAnalyzer runTest(String attributes) throws Exception { 67 testCaseNum++; 68 Manifest man = new Manifest(); 69 Attributes attrs = man.getMainAttributes(); 70 attrs.put(Attributes.Name.MANIFEST_VERSION, "1.0"); 71 72 for (String nameAndValue : attributes.split(",")) { 73 String[] s = nameAndValue.split("="); 74 if (s.length != 2) 75 throw new RuntimeException("Malformed: " + nameAndValue); 76 String name = s[0]; 77 String value = s[1]; 78 attrs.put(new Attributes.Name(name), value); 79 } 80 81 // create the JAR file with Test1 and Test2 82 Path jarfile = Paths.get(String.format("%s-%s.jar", testName, testCaseNum)); 83 Files.deleteIfExists(jarfile); 84 85 Path classes = Paths.get(System.getProperty("test.classes", "")); 86 JarUtils.createJarFile(jarfile, man, classes, 87 Paths.get("Test1.class"), Paths.get("Test2.class")); 88 89 // java -jar test.jar 90 return ProcessTools.executeTestJava("--illegal-access=deny", 91 "-jar", jarfile.toString()) 92 .outputTo(System.out) 93 .errorTo(System.out); 94 } 95 96 /** 97 * Run test with the given JAR attributes, expecting the test to pass 98 */ 99 private void runExpectingPass(String attrs) throws Exception { 100 int exitValue = runTest(attrs).getExitValue(); 101 assertTrue(exitValue == 0); 102 } 103 104 /** 105 * Run test with the given JAR attributes, expecting the test to fail 106 * with at least the given output 107 */ 108 private void runExpectingFail(String attrs, String errorString) throws Exception { 109 int exitValue = runTest(attrs).shouldContain(errorString).getExitValue(); 110 assertTrue(exitValue != 0); 111 } 112 113 114 /** 115 * Run tests to make sure that they fail in the expected way. 116 */ 117 public void testSanity() throws Exception { 118 runExpectingFail("Main-Class=Test1", "IllegalAccessError"); 119 runExpectingFail("Main-Class=Test2", "InaccessibleObjectException"); 120 } 121 122 /** 123 * Run tests with the Add-Exports attribute in the main manifest. 124 */ 125 public void testWithAddExports() throws Exception { 126 runExpectingPass("Main-Class=Test1,Add-Exports=java.base/jdk.internal.misc"); 127 runExpectingFail("Main-Class=Test2,Add-Exports=java.base/jdk.internal.misc", 128 "InaccessibleObjectException"); 129 130 // run with leading and trailing spaces 131 runExpectingPass("Main-Class=Test1,Add-Exports= java.base/jdk.internal.misc"); 132 runExpectingPass("Main-Class=Test1,Add-Exports=java.base/jdk.internal.misc "); 133 134 // run with multiple values 135 runExpectingPass("Main-Class=Test1,Add-Exports=java.base/jdk.internal.misc" 136 + " java.base/jdk.internal.loader"); 137 runExpectingPass("Main-Class=Test1,Add-Exports=java.base/jdk.internal.loader" 138 + " java.base/jdk.internal.misc"); 139 140 // run with duplicate values 141 runExpectingPass("Main-Class=Test1,Add-Exports=java.base/jdk.internal.misc" 142 + " java.base/jdk.internal.misc"); 143 } 144 145 /** 146 * Run tests with the Add-Opens attribute in the main manifest. 147 */ 148 public void testWithAddOpens() throws Exception { 149 runExpectingPass("Main-Class=Test1,Add-Opens=java.base/jdk.internal.misc"); 150 runExpectingPass("Main-Class=Test2,Add-Opens=java.base/jdk.internal.misc"); 151 152 // run with leading and trailing spaces 153 runExpectingPass("Main-Class=Test1,Add-Opens= java.base/jdk.internal.misc"); 154 runExpectingPass("Main-Class=Test1,Add-Opens=java.base/jdk.internal.misc "); 155 156 // run with multiple values 157 runExpectingPass("Main-Class=Test1,Add-Opens=java.base/jdk.internal.misc" 158 + " java.base/jdk.internal.loader"); 159 runExpectingPass("Main-Class=Test1,Add-Opens=java.base/jdk.internal.loader" 160 + " java.base/jdk.internal.misc"); 161 162 // run with duplicate values 163 runExpectingPass("Main-Class=Test1,Add-Opens=java.base/jdk.internal.misc" 164 + " java.base/jdk.internal.misc"); 165 } 166 167 /** 168 * Run tests a bad module or package name 169 */ 170 public void testWithBadModuleOrPackage() throws Exception { 171 // Add-Exports with bad module name 172 String attrs = "Main-Class=Test1,Add-Exports=java.DoesNotExist/jdk.internal.misc"; 173 runExpectingFail(attrs, "IllegalAccessError"); 174 175 // Add-Exports with bad package name 176 attrs = "Main-Class=Test1,Add-Exports=java.base/jdk.internal.DoesNotExit"; 177 runExpectingFail(attrs, "IllegalAccessError"); 178 179 // Add-Opens with bad module name 180 attrs = "Main-Class=Test1,Add-Opens=java.DoesNotExist/jdk.internal.misc"; 181 runExpectingFail(attrs, "IllegalAccessError"); 182 183 // Add-Opens with bad package name 184 attrs = "Main-Class=Test1,Add-Opens=java.base/jdk.internal.DoesNotExit"; 185 runExpectingFail(attrs, "IllegalAccessError"); 186 } 187 }