1 /*
  2  * Copyright (c) 2022, 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  * @requires !vm.musl
 27  *
 28  * @library /test/lib
 29  * @build TestEnableNativeAccessDynamic
 30  *        panama_module/*
 31           NativeAccessDynamicMain
 32  * @run testng/othervm/timeout=180 TestEnableNativeAccessDynamic
 33  * @summary Test for dynamically setting --enable-native-access flag for a module
 34  */
 35 
 36 import java.util.ArrayList;
 37 import java.util.List;
 38 
 39 import jdk.test.lib.process.ProcessTools;
 40 import jdk.test.lib.process.OutputAnalyzer;
 41 
 42 import org.testng.annotations.DataProvider;
 43 import org.testng.annotations.Test;
 44 
 45 @Test
 46 public class TestEnableNativeAccessDynamic extends TestEnableNativeAccessBase {
 47 
 48     @DataProvider(name = "succeedCases")
 49     public Object[][] succeedCases() {
 50         return new Object[][] {
 51                 { "panama_enable_native_access", PANAMA_MAIN, successNoWarning() },
 52                 { "panama_enable_native_access_reflection", PANAMA_REFLECTION, successNoWarning() },
 53                 { "panama_enable_native_access_invoke", PANAMA_INVOKE, successNoWarning() },
 54         };
 55     }
 56 
 57     @DataProvider(name = "failureCases")
 58     public Object[][] failureCases() {
 59         String errMsg = "Illegal native access from: module panama_module";
 60         return new Object[][] {
 61                 { "panama_enable_native_access_fail", PANAMA_MAIN, failWithError(errMsg) },
 62                 { "panama_enable_native_access_fail_reflection", PANAMA_REFLECTION, failWithError(errMsg) },
 63                 { "panama_enable_native_access_fail_invoke", PANAMA_INVOKE, failWithError(errMsg) },
 64         };
 65     }
 66 
 67     /**
 68      * Runs the test to execute the given test action. The VM is run with the
 69      * given VM options and the output checked to see that it matches the
 70      * expected result.
 71      */
 72     OutputAnalyzer run(String action, String moduleAndCls, boolean enableNativeAccess,
 73             Result expectedResult, boolean panamaModuleInBootLayer) throws Exception
 74     {
 75         List<String> list = new ArrayList<>();
 76         if (panamaModuleInBootLayer) {
 77             list.addAll(List.of("-p", MODULE_PATH));
 78             list.add("--add-modules=panama_module");
 79             list.add("--enable-native-access=panama_module");
 80         } else {
 81             list.add("--enable-native-access=ALL-UNNAMED");
 82         }
 83         list.addAll(List.of("NativeAccessDynamicMain", MODULE_PATH,
 84                 moduleAndCls, Boolean.toString(enableNativeAccess), action));
 85         String[] opts = list.toArray(String[]::new);
 86         OutputAnalyzer outputAnalyzer = ProcessTools
 87                 .executeTestJava(opts)
 88                 .outputTo(System.out)
 89                 .errorTo(System.out);
 90         checkResult(expectedResult, outputAnalyzer);
 91         return outputAnalyzer;
 92     }
 93 
 94     @Test(dataProvider = "succeedCases")
 95     public void testSucceed(String action, String moduleAndCls,
 96             Result expectedResult) throws Exception {
 97         run(action, moduleAndCls, true, expectedResult, false);
 98     }
 99 
100     @Test(dataProvider = "failureCases")
101     public void testFailures(String action, String moduleAndCls,
102             Result expectedResult) throws Exception {
103         run(action, moduleAndCls, false, expectedResult, false);
104     }
105 
106     // make sure that having a same named module in boot layer with native access
107     // does not influence same named dynamic module.
108     @Test(dataProvider = "failureCases")
109     public void testFailuresWithPanamaModuleInBootLayer(String action, String moduleAndCls,
110             Result expectedResult) throws Exception {
111         run(action, moduleAndCls, false, expectedResult, true);
112     }
113 }