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
24package compiler.aot.verification.vmflags;
25
26import jdk.test.lib.process.OutputAnalyzer;
27import jdk.test.lib.process.ProcessTools;
28import jdk.test.lib.Utils;
29import compiler.aot.HelloWorldPrinter;
30import compiler.aot.AotCompiler;
31import java.io.File;
32import java.util.ArrayList;
33import java.util.List;
34
35/**
36 * A class with common launch and check logic for testing vm flags change
37 */
38public class BasicFlagsChange {
39    private static final boolean CAN_LOAD = true;
40    /**
41     * A main method which parse arguments, expecting vm option name to
42     *     be present, launch java process with combinations of provided flag
43     *     enabled/disable in aot library and vm flag expecting different flag
44     *     values in library and vm to be negative cases
45     * @param args should have true/false treated as "loadAlways" for
46     *     tracked/non-tracked options and vm option name
47     */
48    public static void main(String args[]) {
49        if (args.length != 2) {
50            throw new Error("TESTBUG: Unexpected number of arguments: "
51                    + args.length);
52        }
53        if (!"false".equals(args[0]) && !"true".equals(args[0])) {
54            throw new Error("TESTBUG: unexpected value of 1st parameter: "
55                    + args[0]);
56        }
57        boolean loadAlways = Boolean.parseBoolean(args[0]);
58        String optName = args[1];
59        String optEnabled = "-XX:+" + optName;
60        String optDisabled = "-XX:-" + optName;
61        String enabledLibName = "libEnabled.so";
62        String disabledLibName = "libDisabled.so";
63        // compile libraries
64        compileLibrary(optEnabled, enabledLibName);
65        compileLibrary(optDisabled, disabledLibName);
66        // run 4 combinations
67        runAndCheck(optEnabled, enabledLibName, CAN_LOAD || loadAlways);
68        runAndCheck(optDisabled, enabledLibName, !CAN_LOAD || loadAlways);
69        runAndCheck(optEnabled, disabledLibName, !CAN_LOAD || loadAlways);
70        runAndCheck(optDisabled, disabledLibName, CAN_LOAD || loadAlways);
71    }
72
73    private static void compileLibrary(String option, String libName) {
74        String className = BasicFlagsChange.class.getName();
75        List<String> extraOpts = new ArrayList<>();
76        extraOpts.add(option);
77        extraOpts.add("-classpath");
78        extraOpts.add(Utils.TEST_CLASS_PATH + File.pathSeparator + Utils.TEST_SRC);
79        AotCompiler.launchCompiler(libName, className, extraOpts, null);
80    }
81
82    private static void runAndCheck(String option, String libName,
83            boolean positiveCase) {
84        ProcessBuilder pb;
85        try {
86            /* using +PrintAOT to check if library has been loaded or skipped,
87               so, a message like "skipped $pathTolibrary aot library" or
88               "loaded    $pathToLibrary  aot library" is present for cases of
89               incompatible or compatible flags respectively */
90            pb = ProcessTools.createJavaProcessBuilder(true, "-XX:+UseAOT",
91                    "-XX:+PrintAOT", "-XX:AOTLibrary=./" + libName, option,
92                    HelloWorldPrinter.class.getName());
93        } catch (Exception ex) {
94            throw new Error("Problems creating ProcessBuilder using " + option
95                    + " Caused by: " + ex, ex);
96        }
97        OutputAnalyzer oa;
98        try {
99            oa = ProcessTools.executeProcess(pb);
100        } catch (Exception ex) {
101            throw new Error("Problems execution child process using case "
102                    + option + " Caused by: " + ex, ex);
103        }
104        oa.shouldHaveExitValue(0);
105        oa.shouldContain(HelloWorldPrinter.MESSAGE);
106        if (positiveCase) {
107            oa.shouldContain("loaded    ./" + libName + "  aot library");
108        } else {
109            oa.shouldContain("skipped ./" + libName + "  aot library");
110        }
111    }
112}
113