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         Manifest man = new Manifest();
  68         Attributes attrs = man.getMainAttributes();
  69         attrs.put(Attributes.Name.MANIFEST_VERSION, "1.0");
  70 
  71         for (String nameAndValue : attributes.split(",")) {
  72             String[] s = nameAndValue.split("=");
  73             if (s.length != 2)
  74                 throw new RuntimeException("Malformed: " + nameAndValue);
  75             String name = s[0];
  76             String value = s[1];
  77             attrs.put(new Attributes.Name(name), value);
  78         }
  79 
  80         // create the JAR file with Test1 and Test2
  81         Path jarfile = Paths.get(String.format("%s-%s.jar", testName, ++testCaseNum));
  82         Files.deleteIfExists(jarfile);
  83 
  84         Path classes = Paths.get(System.getProperty("test.classes", ""));
  85         JarUtils.createJarFile(jarfile, man, classes,
  86                 Paths.get("Test1.class"), Paths.get("Test2.class"));
  87 
  88         // java -jar test.jar
  89         return ProcessTools.executeTestJava("--illegal-access=deny",
  90                                             "-jar", jarfile.toString())
  91                 .outputTo(System.out)
  92                 .errorTo(System.out);
  93     }
  94 
  95     /**
  96      * Run test with the given JAR attributes, expecting the test to pass
  97      */
  98     private void runExpectingPass(String attrs) throws Exception {
  99         int exitValue = runTest(attrs).getExitValue();
 100         assertTrue(exitValue == 0);
 101     }
 102 
 103     /**
 104      * Run test with the given JAR attributes, expecting the test to fail
 105      * with at least the given output
 106      */
 107     private void runExpectingFail(String attrs, String errorString) throws Exception {
 108         int exitValue = runTest(attrs).shouldContain(errorString).getExitValue();
 109         assertTrue(exitValue != 0);
 110     }
 111 
 112 
 113     /**
 114      * Run tests to make sure that they fail in the expected way.
 115      */
 116     public void testSanity() throws Exception {
 117         runExpectingFail("Main-Class=Test1", "IllegalAccessError");
 118         runExpectingFail("Main-Class=Test2", "InaccessibleObjectException");
 119     }
 120 
 121     /**
 122      * Run tests with the Add-Exports attribute in the main manifest.
 123      */
 124     public void testWithAddExports() throws Exception {
 125         runExpectingPass("Main-Class=Test1,Add-Exports=java.base/jdk.internal.misc");
 126         runExpectingFail("Main-Class=Test2,Add-Exports=java.base/jdk.internal.misc",
 127                          "InaccessibleObjectException");
 128 
 129         // run with leading and trailing spaces
 130         runExpectingPass("Main-Class=Test1,Add-Exports=  java.base/jdk.internal.misc");
 131         runExpectingPass("Main-Class=Test1,Add-Exports=java.base/jdk.internal.misc  ");
 132 
 133         // run with multiple values
 134         runExpectingPass("Main-Class=Test1,Add-Exports=java.base/jdk.internal.misc"
 135                 + " java.base/jdk.internal.loader");
 136         runExpectingPass("Main-Class=Test1,Add-Exports=java.base/jdk.internal.loader"
 137                 + " java.base/jdk.internal.misc");
 138 
 139         // run with duplicate values
 140         runExpectingPass("Main-Class=Test1,Add-Exports=java.base/jdk.internal.misc"
 141                 + " java.base/jdk.internal.misc");
 142     }
 143 
 144     /**
 145      * Run tests with the Add-Opens attribute in the main manifest.
 146      */
 147     public void testWithAddOpens() throws Exception {
 148         runExpectingPass("Main-Class=Test1,Add-Opens=java.base/jdk.internal.misc");
 149         runExpectingPass("Main-Class=Test2,Add-Opens=java.base/jdk.internal.misc");
 150 
 151         // run with leading and trailing spaces
 152         runExpectingPass("Main-Class=Test1,Add-Opens=  java.base/jdk.internal.misc");
 153         runExpectingPass("Main-Class=Test1,Add-Opens=java.base/jdk.internal.misc  ");
 154 
 155         // run with multiple values
 156         runExpectingPass("Main-Class=Test1,Add-Opens=java.base/jdk.internal.misc"
 157                 + " java.base/jdk.internal.loader");
 158         runExpectingPass("Main-Class=Test1,Add-Opens=java.base/jdk.internal.loader"
 159                 + " java.base/jdk.internal.misc");
 160 
 161         // run with duplicate values
 162         runExpectingPass("Main-Class=Test1,Add-Opens=java.base/jdk.internal.misc"
 163                 + " java.base/jdk.internal.misc");
 164     }
 165 
 166     /**
 167      * Run tests a bad module or package name
 168      */
 169     public void testWithBadModuleOrPackage() throws Exception {
 170         // Add-Exports with bad module name
 171         String attrs = "Main-Class=Test1,Add-Exports=java.DoesNotExist/jdk.internal.misc";
 172         runExpectingFail(attrs, "IllegalAccessError");
 173 
 174         // Add-Exports with bad package name
 175         attrs = "Main-Class=Test1,Add-Exports=java.base/jdk.internal.DoesNotExit";
 176         runExpectingFail(attrs, "IllegalAccessError");
 177 
 178         // Add-Opens with bad module name
 179         attrs = "Main-Class=Test1,Add-Opens=java.DoesNotExist/jdk.internal.misc";
 180         runExpectingFail(attrs, "IllegalAccessError");
 181 
 182         // Add-Opens with bad package name
 183         attrs = "Main-Class=Test1,Add-Opens=java.base/jdk.internal.DoesNotExit";
 184         runExpectingFail(attrs, "IllegalAccessError");
 185     }
 186 }