1 /*
  2  * Copyright (c) 2023, 2025, 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  * @summary Basic test for Enable-Native-Access attribute in the
 27  *          manifest of a main application JAR
 28  * @library /test/lib
 29  * @requires jdk.foreign.linker != "UNSUPPORTED"
 30  * @requires !vm.musl
 31  *
 32  * @enablePreview
 33  * @build TestEnableNativeAccessJarManifest
 34  *        panama_module/*
 35  *        org.openjdk.foreigntest.unnamed.PanamaMainUnnamedModule
 36  * @run testng/native TestEnableNativeAccessJarManifest
 37  */
 38 
 39 import java.nio.file.Files;
 40 import java.nio.file.Path;
 41 import java.nio.file.Paths;
 42 import java.util.List;
 43 import java.util.ArrayList;
 44 import java.util.jar.Attributes;
 45 import java.util.jar.Manifest;
 46 
 47 import jdk.test.lib.process.OutputAnalyzer;
 48 import jdk.test.lib.process.ProcessTools;
 49 import jdk.test.lib.util.JarUtils;
 50 
 51 import org.testng.annotations.Test;
 52 import org.testng.annotations.DataProvider;
 53 
 54 public class TestEnableNativeAccessJarManifest extends TestEnableNativeAccessBase {
 55 
 56     private static final String REINVOKER = "TestEnableNativeAccessJarManifest$Reinvoker";
 57 
 58     static record Attribute(String name, String value) {}
 59 
 60     @Test(dataProvider = "cases")
 61     public void testEnableNativeAccessInJarManifest(String action, String cls, Result expectedResult,
 62                                                     List<Attribute> attributes, List<String> vmArgs, List<String> programArgs) throws Exception {
 63         Manifest man = new Manifest();
 64         Attributes attrs = man.getMainAttributes();
 65         attrs.put(Attributes.Name.MANIFEST_VERSION, "1.0");
 66         attrs.put(Attributes.Name.MAIN_CLASS, cls);
 67 
 68         for (Attribute attrib : attributes) {
 69             attrs.put(new Attributes.Name(attrib.name()), attrib.value());
 70         }
 71 
 72         // create the JAR file with Test1 and Test2
 73         Path jarfile = Paths.get(action + ".jar");
 74         Files.deleteIfExists(jarfile);
 75 
 76         Path classes = Paths.get(System.getProperty("test.classes", ""));
 77         JarUtils.createJarFile(jarfile, man, classes, Paths.get(cls.replace('.', '/') + ".class"));
 78 
 79         // java -jar test.jar
 80         List<String> command = new ArrayList<>(List.of(
 81             "-Djava.library.path=" + System.getProperty("java.library.path")
 82         ));
 83         command.addAll(vmArgs);
 84         command.add("-jar");
 85         command.add(jarfile.toString());
 86         command.addAll(programArgs);
 87         OutputAnalyzer outputAnalyzer = ProcessTools.executeTestJava(command.toArray(String[]::new))
 88                 .outputTo(System.out)
 89                 .errorTo(System.out);
 90         checkResult(expectedResult, outputAnalyzer);
 91     }
 92 
 93     @DataProvider
 94     public Object[][] cases() {
 95         return new Object[][] {
 96             // simple cases where a jar contains a single main class with no dependencies
 97             { "panama_no_unnamed_module_native_access", UNNAMED, successWithWarning("ALL-UNNAMED"),
 98                     List.of(), List.of(), List.of() },
 99             { "panama_unnamed_module_native_access", UNNAMED, successNoWarning(),
100                     List.of(new Attribute("Enable-Native-Access", "ALL-UNNAMED")), List.of(), List.of() },
101             { "panama_unnamed_module_native_access_invalid", UNNAMED,
102                     failWithError("Error: illegal value \"asdf\" for Enable-Native-Access manifest attribute. Only ALL-UNNAMED is allowed"),
103                     List.of(new Attribute("Enable-Native-Access", "asdf")), List.of(), List.of() },
104 
105             // more complex cases where a jar invokes a module on the module path that does native access
106             { "panama_enable_native_access_false", REINVOKER, successWithWarning("panama_module"),
107                     List.of(new Attribute("Enable-Native-Access", "ALL-UNNAMED")),
108                     List.of("-p", MODULE_PATH, "--add-modules=panama_module"),
109                     List.of(PANAMA_MAIN_CLS) },
110             { "panama_enable_native_access_reflection_false", REINVOKER, successWithWarning("panama_module"),
111                     List.of(new Attribute("Enable-Native-Access", "ALL-UNNAMED")),
112                     List.of("-p", MODULE_PATH, "--add-modules=panama_module"),
113                     List.of(PANAMA_REFLECTION_CLS) },
114             { "panama_enable_native_access_invoke_false", REINVOKER, successWithWarning("panama_module"),
115                     List.of(new Attribute("Enable-Native-Access", "ALL-UNNAMED")),
116                     List.of("-p", MODULE_PATH, "--add-modules=panama_module"),
117                     List.of(PANAMA_INVOKE_CLS) },
118 
119             { "panama_enable_native_access_true", REINVOKER, successNoWarning(),
120                     List.of(new Attribute("Enable-Native-Access", "ALL-UNNAMED")),
121                     List.of("-p", MODULE_PATH, "--add-modules=panama_module", "--enable-native-access=panama_module"),
122                     List.of(PANAMA_MAIN_CLS) },
123             { "panama_enable_native_access_reflection_true", REINVOKER, successNoWarning(),
124                     List.of(new Attribute("Enable-Native-Access", "ALL-UNNAMED")),
125                     List.of("-p", MODULE_PATH, "--add-modules=panama_module", "--enable-native-access=panama_module"),
126                     List.of(PANAMA_REFLECTION_CLS) },
127             { "panama_enable_native_access_invoke_true", REINVOKER, successNoWarning(),
128                     List.of(new Attribute("Enable-Native-Access", "ALL-UNNAMED")),
129                     List.of("-p", MODULE_PATH, "--add-modules=panama_module", "--enable-native-access=panama_module"),
130                     List.of(PANAMA_INVOKE_CLS) }
131         };
132     }
133 
134     public class Reinvoker {
135         public static void main(String[] args) throws Throwable {
136             Class<?> realMainClass = Class.forName(args[0]);
137             realMainClass.getMethod("main", String[].class).invoke(null, (Object) new String[0]);
138         }
139     }
140 }