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 
24 /**
25  * @test
26  * @bug 8170832 8180447
27  * @summary Arguments passed in environment variable
28  * @modules jdk.compiler
29  *          jdk.zipfs
30  * @build TestHelper
31  * @run main ArgsEnvVar
32  */
33 import java.io.File;
34 import java.io.IOException;
35 import java.util.ArrayList;
36 import java.util.HashMap;
37 import java.util.List;
38 import java.util.Map;
39 import java.util.regex.Pattern;
40 import java.nio.file.Paths;
41 import java.nio.file.Path;
42 
43 public class ArgsEnvVar extends TestHelper {
44     private static File testJar = null;
45     private static Map<String, String> env = new HashMap<>();
46 
47     private static String JDK_JAVA_OPTIONS = "JDK_JAVA_OPTIONS";
48 
init()49     static void init() throws IOException {
50         if  (testJar != null) {
51             return;
52         }
53         testJar = new File("test.jar");
54         StringBuilder tsrc = new StringBuilder();
55         tsrc.append("public static void main(String... args) {\n");
56         tsrc.append("   for (String x : args) {\n");
57         tsrc.append("        System.out.println(x);\n");
58         tsrc.append("   }\n");
59         tsrc.append("}\n");
60         createJar(testJar, new File("Foo"), tsrc.toString());
61 
62         env.put(JLDEBUG_KEY, "true");
63     }
64 
createArgFile(String fname, List<String> lines)65     private File createArgFile(String fname, List<String> lines) throws IOException {
66         File argFile = new File(fname);
67         argFile.delete();
68         createAFile(argFile, lines);
69         return argFile;
70     }
71 
verifyOptions(List<String> args, TestResult tr)72     private void verifyOptions(List<String> args, TestResult tr) {
73         if (args.isEmpty()) {
74             return;
75         }
76 
77         int i = 1;
78         for (String x : args) {
79             tr.matches(".*argv\\[" + i + "\\] = " + Pattern.quote(x) + ".*");
80             i++;
81         }
82         if (! tr.testStatus) {
83             System.out.println(tr);
84             throw new RuntimeException("test fails");
85         }
86     }
87 
verifyUserArgs(List<String> args, TestResult tr, int index)88     private void verifyUserArgs(List<String> args, TestResult tr, int index) {
89         if (javaCmd != TestHelper.javaCmd) {
90             tr.contains("\tFirst application arg index: 1");
91         } else {
92             tr.contains("\tFirst application arg index: " + index);
93 
94             for (String arg: args) {
95                 tr.matches("^" + Pattern.quote(arg) + "$");
96             }
97         }
98 
99         if (! tr.testStatus) {
100             System.out.println(tr);
101             throw new RuntimeException("test fails");
102         }
103     }
104 
105     @Test
106     // Verify prepend and @argfile expansion
basic()107     public void basic() throws IOException {
108         File argFile1 = createArgFile("argFile1", List.of("-Xmx32m"));
109         File argFile2 = createArgFile("argFile2", List.of("-Darg.file2=TWO"));
110         File argFile3 = createArgFile("argFile3", List.of("-Darg.file3=THREE"));
111 
112         env.put(JDK_JAVA_OPTIONS, "@argFile1\n-Xint\r-cp @@escaped\t@argFile2");
113 
114         TestResult tr = doExec(env, javaCmd, "@argFile3", "-cp", "test.jar", "Foo", "uarg1", "@uarg2");
115 
116         List<String> appArgs = new ArrayList<>();
117         appArgs.add("uarg1");
118         appArgs.add("@uarg2");
119 
120         List<String> options = new ArrayList<>();
121         options.add("-Xmx32m");
122         options.add("-Xint");
123         options.add("-cp");
124         options.add("@escaped");
125         options.add("-Darg.file2=TWO");
126         options.add("-Darg.file3=THREE");
127         options.add("-cp");
128         options.add("test.jar");
129         options.add("Foo");
130         options.addAll(appArgs);
131 
132         verifyOptions(options, tr);
133         verifyUserArgs(appArgs, tr, 10);
134         argFile1.delete();
135         argFile2.delete();
136         argFile3.delete();
137     }
138 
testInEnv(List<String> options)139     private TestResult testInEnv(List<String> options) {
140         env.put(JDK_JAVA_OPTIONS, String.join(" ", options));
141         return doExec(env, javaCmd, "-jar", "test.jar");
142     }
143 
testInEnvAsArgFile(List<String> options)144     private TestResult testInEnvAsArgFile(List<String> options) throws IOException {
145         File argFile = createArgFile("argFile", options);
146         env.put(JDK_JAVA_OPTIONS, "@argFile");
147         TestResult tr = doExec(env, javaCmd, "-jar", "test.jar");
148         argFile.delete();
149         return tr;
150     }
151 
152     @Test
noTerminalOpt()153     public void noTerminalOpt() throws IOException {
154         List<List<String>> terminal_opts = List.of(
155                 List.of("-jar", "test.jar"),
156                 List.of("-m", "test/Foo"),
157                 List.of("--module", "test/Foo"),
158                 List.of("--module=test/Foo"),
159                 List.of("--dry-run"),
160                 List.of("-h"),
161                 List.of("-?"),
162                 List.of("-help"),
163                 List.of("--help"),
164                 List.of("-X"),
165                 List.of("--help-extra"),
166                 List.of("-version"),
167                 List.of("--version"),
168                 List.of("-fullversion"),
169                 List.of("--full-version"));
170 
171         for (List<String> options: terminal_opts) {
172             // terminal opt in environment variable
173             TestResult tr = testInEnv(options);
174             tr.checkNegative();
175             if (!tr.testStatus) {
176                 System.out.println(tr);
177                 throw new RuntimeException("test fails");
178             }
179 
180             // terminal opt in environment variable through @file
181             tr = testInEnvAsArgFile(options);
182             tr.checkNegative();
183             if (!tr.testStatus) {
184                 System.out.println(tr);
185                 throw new RuntimeException("test fails");
186             }
187         }
188     }
189 
190     @Test
quote()191     public void quote() throws IOException {
192         File argFile1 = createArgFile("arg File 1", List.of("-Xint"));
193         File argFile2 = createArgFile("arg File 2", List.of("-Dprop='value with spaces'"));
194         File argFile3 = createArgFile("arg File 3", List.of("-Xmx32m"));
195         env.put(JDK_JAVA_OPTIONS, "'@arg File 1' @\"arg File 2\" @'arg File'\" 3\"");
196 
197         TestResult tr = doExec(env, javaCmd, "-jar", "test.jar");
198         List<String> options = new ArrayList<>();
199         options.add("-Xint");
200         options.add("-Dprop=value with spaces");
201         options.add("-Xmx32m");
202         options.add("-jar");
203         options.add("test.jar");
204         verifyOptions(options, tr);
205         argFile1.delete();
206         argFile2.delete();
207         argFile3.delete();
208     }
209 
210     @Test
openQuoteShouldFail()211     public void openQuoteShouldFail() {
212         env.put(JDK_JAVA_OPTIONS, "-Dprop='value missing close quote");
213         TestResult tr = doExec(env, javaCmd, "-version");
214         tr.checkNegative();
215         if (!tr.testStatus) {
216             System.out.println(tr);
217             throw new RuntimeException("test fails");
218         }
219     }
220 
221     @Test
noWildcard()222     public void noWildcard() {
223         env.put(JDK_JAVA_OPTIONS, "-cp *");
224         TestResult tr = doExec(env, javaCmd, "-jar", "test.jar");
225         verifyOptions(List.of("-cp", "*", "-jar", "test.jar"), tr);
226 
227         env.put(JDK_JAVA_OPTIONS, "-p ?");
228         tr = doExec(env, javaCmd, "-jar", "test.jar", "one", "two");
229         verifyOptions(List.of("-p", "?", "-jar", "test.jar", "one", "two"), tr);
230     }
231 
232     @Test
testTrailingSpaces()233     public void testTrailingSpaces() {
234         env.put(JDK_JAVA_OPTIONS, "--add-exports java.base/jdk.internal.misc=ALL-UNNAMED ");
235         TestResult tr = doExec(env, javaCmd, "-jar", "test.jar");
236         verifyOptions(List.of("--add-exports", "java.base/jdk.internal.misc=ALL-UNNAMED", "-jar", "test.jar"), tr);
237 
238         env.put(JDK_JAVA_OPTIONS, "--class-path ' '");
239         tr = doExec(env, javaCmd, "-jar", "test.jar");
240         verifyOptions(List.of("--class-path", " ", "-jar", "test.jar"), tr);
241 
242         env.put(JDK_JAVA_OPTIONS, "  --add-exports java.base/jdk.internal.misc=ALL-UNNAMED ");
243         tr = doExec(env, javaCmd, "-jar", "test.jar");
244         verifyOptions(List.of("--add-exports", "java.base/jdk.internal.misc=ALL-UNNAMED", "-jar", "test.jar"), tr);
245     }
246 
247 
248     @Test
249     // That that we can correctly handle the module longform argument option
250     // when supplied in an argument file
modulesInArgsFile()251     public void modulesInArgsFile() throws IOException {
252         File cwd = new File(".");
253         File testModuleDir = new File(cwd, "modules_test");
254 
255         createEchoArgumentsModule(testModuleDir);
256 
257         Path SRC_DIR = Paths.get(testModuleDir.getAbsolutePath(), "src");
258         Path MODS_DIR = Paths.get(testModuleDir.getAbsolutePath(), "mods");
259 
260         // test module / main class
261         String MODULE_OPTION = "--module=test/launcher.Main";
262         String TEST_MODULE = "test";
263 
264         // javac -d mods/test src/test/**
265         TestResult tr = doExec(
266             javacCmd,
267             "-d", MODS_DIR.toString(),
268             "--module-source-path", SRC_DIR.toString(),
269             "--module", TEST_MODULE);
270 
271         if (!tr.isOK()) {
272             System.out.println("test did not compile");
273             throw new RuntimeException("Error: modules test did not compile");
274         }
275 
276         // verify the terminating ability of --module= through environment variables
277         File argFile = createArgFile("cmdargs", List.of("--module-path", MODS_DIR.toString(), MODULE_OPTION, "--hello"));
278         env.put(JDK_JAVA_OPTIONS, "@cmdargs");
279         tr = doExec(env, javaCmd);
280         tr.checkNegative();
281         tr.contains("Error: Option " + MODULE_OPTION + " in @cmdargs is not allowed in environment variable JDK_JAVA_OPTIONS");
282         if (!tr.testStatus) {
283             System.out.println(tr);
284             throw new RuntimeException("test fails");
285         }
286 
287         // check that specifying --module and --module-path with file works
288         tr = doExec(javaCmd, "-Dfile.encoding=UTF-8", "@cmdargs");
289         tr.contains("[--hello]");
290         if (!tr.testStatus) {
291             System.out.println(tr);
292             throw new RuntimeException("test fails");
293         }
294 
295         // check with reversed --module-path and --module in the arguments file, this will fail, --module= is terminating
296         File argFile1 = createArgFile("cmdargs1", List.of(MODULE_OPTION, "--module-path", MODS_DIR.toString(), "--hello"));
297         tr = doExec(javaCmd, "-Dfile.encoding=UTF-8", "@cmdargs1");
298         tr.checkNegative();
299         if (!tr.testStatus) {
300             System.out.println(tr);
301             throw new RuntimeException("test fails");
302         }
303 
304         // clean-up
305         argFile.delete();
306         argFile1.delete();
307         recursiveDelete(testModuleDir);
308     }
309 
main(String... args)310     public static void main(String... args) throws Exception {
311         init();
312         ArgsEnvVar a = new ArgsEnvVar();
313         a.run(args);
314         if (testExitValue > 0) {
315             System.out.println("Total of " + testExitValue + " failed");
316             System.exit(1);
317         } else {
318             System.out.println("All tests pass");
319         }
320     }
321 }
322