1 /*
2 * Copyright (c) 2016, 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.nio.file.Files;
36 import java.nio.file.Path;
37 import java.nio.file.Paths;
38 import java.util.jar.Attributes;
39 import java.util.jar.Manifest;
40
41 import jdk.testlibrary.OutputAnalyzer;
42 import jdk.testlibrary.ProcessTools;
43
44 import org.testng.annotations.Test;
45 import static org.testng.Assert.*;
46
47
48 @Test
49 public class AddExportsAndOpensInManifest {
50
51 /**
52 * Package Test1 and Test2 into a JAR file with the given attributes
53 * in the JAR manifest, then execute the JAR file with `java -jar`.
54 */
55 private OutputAnalyzer runTest(String attributes) throws Exception {
56 Manifest man = new Manifest();
57 Attributes attrs = man.getMainAttributes();
58 attrs.put(Attributes.Name.MANIFEST_VERSION, "1.0");
59
60 for (String nameAndValue : attributes.split(",")) {
61 String[] s = nameAndValue.split("=");
62 if (s.length != 2)
63 throw new RuntimeException("Malformed: " + nameAndValue);
64 String name = s[0];
65 String value = s[1];
66 attrs.put(new Attributes.Name(name), value);
67 }
68
69 // create the JAR file with Test1 and Test2
70 Path jarfile = Paths.get("test.jar");
71 Files.deleteIfExists(jarfile);
72
73 Path classes = Paths.get(System.getProperty("test.classes", ""));
74 JarUtils.createJarFile(jarfile, man, classes,
75 Paths.get("Test1.class"), Paths.get("Test2.class"));
76
77 // java -jar test.jar
78 return ProcessTools.executeTestJava("--illegal-access=deny",
79 "-jar", jarfile.toString())
80 .outputTo(System.out)
81 .errorTo(System.out);
82 }
83
84 /**
85 * Run test with the given JAR attributes, expecting the test to pass
86 */
87 private void runExpectingPass(String attrs) throws Exception {
88 int exitValue = runTest(attrs).getExitValue();
89 assertTrue(exitValue == 0);
90 }
155 /**
156 * Run tests a bad module or package name
157 */
158 public void testWithBadModuleOrPackage() throws Exception {
159 // Add-Exports with bad module name
160 String attrs = "Main-Class=Test1,Add-Exports=java.DoesNotExist/jdk.internal.misc";
161 runExpectingFail(attrs, "IllegalAccessError");
162
163 // Add-Exports with bad package name
164 attrs = "Main-Class=Test1,Add-Exports=java.base/jdk.internal.DoesNotExit";
165 runExpectingFail(attrs, "IllegalAccessError");
166
167 // Add-Opens with bad module name
168 attrs = "Main-Class=Test1,Add-Opens=java.DoesNotExist/jdk.internal.misc";
169 runExpectingFail(attrs, "IllegalAccessError");
170
171 // Add-Opens with bad package name
172 attrs = "Main-Class=Test1,Add-Opens=java.base/jdk.internal.DoesNotExit";
173 runExpectingFail(attrs, "IllegalAccessError");
174 }
175
176 }
|
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 }
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 }
|