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