Path: blob/master/test/jdk/tools/launcher/ArgsEnvVar.java
41144 views
/*1* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 8170832 818044726* @summary Arguments passed in environment variable27* @modules jdk.compiler28* jdk.zipfs29* @build TestHelper30* @run main ArgsEnvVar31*/32import java.io.File;33import java.io.IOException;34import java.util.ArrayList;35import java.util.HashMap;36import java.util.List;37import java.util.Map;38import java.util.regex.Pattern;39import java.nio.file.Paths;40import java.nio.file.Path;4142public class ArgsEnvVar extends TestHelper {43private static File testJar = null;44private static Map<String, String> env = new HashMap<>();4546private static String JDK_JAVA_OPTIONS = "JDK_JAVA_OPTIONS";4748static void init() throws IOException {49if (testJar != null) {50return;51}52testJar = new File("test.jar");53StringBuilder tsrc = new StringBuilder();54tsrc.append("public static void main(String... args) {\n");55tsrc.append(" for (String x : args) {\n");56tsrc.append(" System.out.println(x);\n");57tsrc.append(" }\n");58tsrc.append("}\n");59createJar(testJar, new File("Foo"), tsrc.toString());6061env.put(JLDEBUG_KEY, "true");62}6364private File createArgFile(String fname, List<String> lines) throws IOException {65File argFile = new File(fname);66argFile.delete();67createAFile(argFile, lines);68return argFile;69}7071private void verifyOptions(List<String> args, TestResult tr) {72if (args.isEmpty()) {73return;74}7576int i = 1;77for (String x : args) {78tr.matches(".*argv\\[" + i + "\\] = " + Pattern.quote(x) + ".*");79i++;80}81if (! tr.testStatus) {82System.out.println(tr);83throw new RuntimeException("test fails");84}85}8687private void verifyUserArgs(List<String> args, TestResult tr, int index) {88if (javaCmd != TestHelper.javaCmd) {89tr.contains("\tFirst application arg index: 1");90} else {91tr.contains("\tFirst application arg index: " + index);9293for (String arg: args) {94tr.matches("^" + Pattern.quote(arg) + "$");95}96}9798if (! tr.testStatus) {99System.out.println(tr);100throw new RuntimeException("test fails");101}102}103104@Test105// Verify prepend and @argfile expansion106public void basic() throws IOException {107File argFile1 = createArgFile("argFile1", List.of("-Xmx32m"));108File argFile2 = createArgFile("argFile2", List.of("-Darg.file2=TWO"));109File argFile3 = createArgFile("argFile3", List.of("-Darg.file3=THREE"));110111env.put(JDK_JAVA_OPTIONS, "@argFile1\n-Xint\r-cp @@escaped\t@argFile2");112113TestResult tr = doExec(env, javaCmd, "@argFile3", "-cp", "test.jar", "Foo", "uarg1", "@uarg2");114115List<String> appArgs = new ArrayList<>();116appArgs.add("uarg1");117appArgs.add("@uarg2");118119List<String> options = new ArrayList<>();120options.add("-Xmx32m");121options.add("-Xint");122options.add("-cp");123options.add("@escaped");124options.add("-Darg.file2=TWO");125options.add("-Darg.file3=THREE");126options.add("-cp");127options.add("test.jar");128options.add("Foo");129options.addAll(appArgs);130131verifyOptions(options, tr);132verifyUserArgs(appArgs, tr, 10);133argFile1.delete();134argFile2.delete();135argFile3.delete();136}137138private TestResult testInEnv(List<String> options) {139env.put(JDK_JAVA_OPTIONS, String.join(" ", options));140return doExec(env, javaCmd, "-jar", "test.jar");141}142143private TestResult testInEnvAsArgFile(List<String> options) throws IOException {144File argFile = createArgFile("argFile", options);145env.put(JDK_JAVA_OPTIONS, "@argFile");146TestResult tr = doExec(env, javaCmd, "-jar", "test.jar");147argFile.delete();148return tr;149}150151@Test152public void noTerminalOpt() throws IOException {153List<List<String>> terminal_opts = List.of(154List.of("-jar", "test.jar"),155List.of("-m", "test/Foo"),156List.of("--module", "test/Foo"),157List.of("--module=test/Foo"),158List.of("--dry-run"),159List.of("-h"),160List.of("-?"),161List.of("-help"),162List.of("--help"),163List.of("-X"),164List.of("--help-extra"),165List.of("-version"),166List.of("--version"),167List.of("-fullversion"),168List.of("--full-version"));169170for (List<String> options: terminal_opts) {171// terminal opt in environment variable172TestResult tr = testInEnv(options);173tr.checkNegative();174if (!tr.testStatus) {175System.out.println(tr);176throw new RuntimeException("test fails");177}178179// terminal opt in environment variable through @file180tr = testInEnvAsArgFile(options);181tr.checkNegative();182if (!tr.testStatus) {183System.out.println(tr);184throw new RuntimeException("test fails");185}186}187}188189@Test190public void quote() throws IOException {191File argFile1 = createArgFile("arg File 1", List.of("-Xint"));192File argFile2 = createArgFile("arg File 2", List.of("-Dprop='value with spaces'"));193File argFile3 = createArgFile("arg File 3", List.of("-Xmx32m"));194env.put(JDK_JAVA_OPTIONS, "'@arg File 1' @\"arg File 2\" @'arg File'\" 3\"");195196TestResult tr = doExec(env, javaCmd, "-jar", "test.jar");197List<String> options = new ArrayList<>();198options.add("-Xint");199options.add("-Dprop=value with spaces");200options.add("-Xmx32m");201options.add("-jar");202options.add("test.jar");203verifyOptions(options, tr);204argFile1.delete();205argFile2.delete();206argFile3.delete();207}208209@Test210public void openQuoteShouldFail() {211env.put(JDK_JAVA_OPTIONS, "-Dprop='value missing close quote");212TestResult tr = doExec(env, javaCmd, "-version");213tr.checkNegative();214if (!tr.testStatus) {215System.out.println(tr);216throw new RuntimeException("test fails");217}218}219220@Test221public void noWildcard() {222env.put(JDK_JAVA_OPTIONS, "-cp *");223TestResult tr = doExec(env, javaCmd, "-jar", "test.jar");224verifyOptions(List.of("-cp", "*", "-jar", "test.jar"), tr);225226env.put(JDK_JAVA_OPTIONS, "-p ?");227tr = doExec(env, javaCmd, "-jar", "test.jar", "one", "two");228verifyOptions(List.of("-p", "?", "-jar", "test.jar", "one", "two"), tr);229}230231@Test232public void testTrailingSpaces() {233env.put(JDK_JAVA_OPTIONS, "--add-exports java.base/jdk.internal.misc=ALL-UNNAMED ");234TestResult tr = doExec(env, javaCmd, "-jar", "test.jar");235verifyOptions(List.of("--add-exports", "java.base/jdk.internal.misc=ALL-UNNAMED", "-jar", "test.jar"), tr);236237env.put(JDK_JAVA_OPTIONS, "--class-path ' '");238tr = doExec(env, javaCmd, "-jar", "test.jar");239verifyOptions(List.of("--class-path", " ", "-jar", "test.jar"), tr);240241env.put(JDK_JAVA_OPTIONS, " --add-exports java.base/jdk.internal.misc=ALL-UNNAMED ");242tr = doExec(env, javaCmd, "-jar", "test.jar");243verifyOptions(List.of("--add-exports", "java.base/jdk.internal.misc=ALL-UNNAMED", "-jar", "test.jar"), tr);244}245246247@Test248// That that we can correctly handle the module longform argument option249// when supplied in an argument file250public void modulesInArgsFile() throws IOException {251File cwd = new File(".");252File testModuleDir = new File(cwd, "modules_test");253254createEchoArgumentsModule(testModuleDir);255256Path SRC_DIR = Paths.get(testModuleDir.getAbsolutePath(), "src");257Path MODS_DIR = Paths.get(testModuleDir.getAbsolutePath(), "mods");258259// test module / main class260String MODULE_OPTION = "--module=test/launcher.Main";261String TEST_MODULE = "test";262263// javac -d mods/test src/test/**264TestResult tr = doExec(265javacCmd,266"-d", MODS_DIR.toString(),267"--module-source-path", SRC_DIR.toString(),268"--module", TEST_MODULE);269270if (!tr.isOK()) {271System.out.println("test did not compile");272throw new RuntimeException("Error: modules test did not compile");273}274275// verify the terminating ability of --module= through environment variables276File argFile = createArgFile("cmdargs", List.of("--module-path", MODS_DIR.toString(), MODULE_OPTION, "--hello"));277env.put(JDK_JAVA_OPTIONS, "@cmdargs");278tr = doExec(env, javaCmd);279tr.checkNegative();280tr.contains("Error: Option " + MODULE_OPTION + " in @cmdargs is not allowed in environment variable JDK_JAVA_OPTIONS");281if (!tr.testStatus) {282System.out.println(tr);283throw new RuntimeException("test fails");284}285286// check that specifying --module and --module-path with file works287tr = doExec(javaCmd, "-Dfile.encoding=UTF-8", "@cmdargs");288tr.contains("[--hello]");289if (!tr.testStatus) {290System.out.println(tr);291throw new RuntimeException("test fails");292}293294// check with reversed --module-path and --module in the arguments file, this will fail, --module= is terminating295File argFile1 = createArgFile("cmdargs1", List.of(MODULE_OPTION, "--module-path", MODS_DIR.toString(), "--hello"));296tr = doExec(javaCmd, "-Dfile.encoding=UTF-8", "@cmdargs1");297tr.checkNegative();298if (!tr.testStatus) {299System.out.println(tr);300throw new RuntimeException("test fails");301}302303// clean-up304argFile.delete();305argFile1.delete();306recursiveDelete(testModuleDir);307}308309public static void main(String... args) throws Exception {310init();311ArgsEnvVar a = new ArgsEnvVar();312a.run(args);313if (testExitValue > 0) {314System.out.println("Total of " + testExitValue + " failed");315System.exit(1);316} else {317System.out.println("All tests pass");318}319}320}321322323