Path: blob/master/test/jdk/tools/launcher/ArgFileSyntax.java
41145 views
/*1* Copyright (c) 2015, 2020, 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 8027634 8210810 824062926* @summary Verify syntax of argument file27* @build TestHelper28* @run main ArgFileSyntax29*/30import java.io.File;31import java.io.IOException;32import java.util.ArrayList;33import java.util.Arrays;34import java.util.Collections;35import java.util.HashMap;36import java.util.List;37import java.util.Map;38import java.util.regex.Pattern;3940public class ArgFileSyntax extends TestHelper {41// Buffer size in args.c readArgFile() method42private static final int ARG_FILE_PARSER_BUF_SIZE = 4096;4344private File createArgFile(List<String> lines) throws IOException {45File argFile = new File("argfile");46argFile.delete();47createAFile(argFile, lines);48return argFile;49}5051private void verifyOutput(List<String> args, TestResult tr) {52if (args.isEmpty()) {53return;54}5556int i = 1;57for (String x : args) {58tr.matches(".*argv\\[" + i + "\\] = " + Pattern.quote(x) + ".*");59i++;60}61if (! tr.testStatus) {62System.out.println(tr);63throw new RuntimeException("test fails");64}65}6667// arg file content, expected options68static String[] testCases[][] = {69{ // empty file70{}, {}71},72{ // comments and # inside quote73{ "# a couple of -X flags",74"-Xmx32m",75"-XshowSettings #inline comment",76"-Dpound.in.quote=\"This property contains #.\"",77"# add -version",78"-version",79"# trail comment"80},81{ "-Xmx32m",82"-XshowSettings",83"-Dpound.in.quote=This property contains #.",84"-version"85}86},87{ // open quote with continuation directive88// multiple options in a line89{ "-cp \"c:\\\\java lib\\\\all;\\",90" c:\\\\lib\"",91"-Xmx32m -XshowSettings",92"-version"93},94{ "-cp",95"c:\\java lib\\all;c:\\lib",96"-Xmx32m",97"-XshowSettings",98"-version"99}100},101{ // no continuation on open quote102// multiple lines in a property103{ "-cp \"c:\\\\open quote\\\\all;",104" # c:\\\\lib\"",105"-Dmultiple.lines=\"line 1\\nline 2\\n\\rline 3\"",106"-Dopen.quote=\"Open quote to EOL",107"-Dcontinue.with.leadingWS=\"Continue with\\",108" \\ leading WS.",109"-Dcontinue.without.leadingWS=\"Continue without \\",110" leading WS.",111"-Descape.seq=\"escaped chars: \\\"\\a\\b\\c\\f\\t\\v\\9\\6\\23\\82\\28\\377\\477\\278\\287\\n\"",112"-version"113},114{ "-cp",115"c:\\open quote\\all;",116"-Dmultiple.lines=line 1",117// line 2 and line 3 shoule be in output, but not as arg[x]=118"-Dopen.quote=Open quote to EOL",119"-Dcontinue.with.leadingWS=Continue with leading WS.",120"-Dcontinue.without.leadingWS=Continue without leading WS.",121// cannot verify \n and \r as that break output lines122"-Descape.seq=escaped chars: \"abc\f\tv96238228377477278287",123"-version"124}125},126{ // No need to escape if not in quote127// also quote part of a token128{ "-cp c:\\\"partial quote\"\\all",129"-Xmx32m -XshowSettings",130"-version"131},132{ "-cp",133"c:\\partial quote\\all",134"-Xmx32m",135"-XshowSettings",136"-version"137}138},139{ // No recursive expansion140{ "-Xmx32m",141"-cp",142" # @cpfile should remains @cpfile",143"@cpfile",144"-version"145},146{ "-Xmx32m",147"-cp",148"@cpfile",149"-version"150}151},152{ // Mix quotation153{ "-Dsingle.in.double=\"Mix 'single' in double\"",154"-Ddouble.in.single='Mix \"double\" in single'",155"-Dsingle.in.single='Escape \\\'single\\\' in single'",156"-Ddouble.in.double=\"Escape \\\"double\\\" in double\""157},158{ "-Dsingle.in.double=Mix 'single' in double",159"-Ddouble.in.single=Mix \"double\" in single",160"-Dsingle.in.single=Escape 'single' in single",161"-Ddouble.in.double=Escape \"double\" in double"162},163},164{ // \t\f as whitespace and in escape165{ "-Xmx32m\t-Xint\f-version",166"-Dcontinue.with.leadingws=\"Line1\\",167" \t\fcontinue with \\f<ff> and \\t<tab>"168},169{ "-Xmx32m",170"-Xint",171"-version",172"-Dcontinue.with.leadingws=Line1continue with \f<ff> and \t<tab>"173}174}175};176177public List<List<List<String>>> loadCases() {178List<List<List<String>>> rv = new ArrayList<>();179for (String[][] testCaseArray: testCases) {180List<List<String>> testCase = new ArrayList<>(2);181testCase.add(Arrays.asList(testCaseArray[0]));182testCase.add(Arrays.asList(testCaseArray[1]));183rv.add(testCase);184}185186// long lines187String bag = "-Dgarbage=";188String ver = "-version";189// a token 8192 long190char[] data = new char[2*ARG_FILE_PARSER_BUF_SIZE - bag.length()];191Arrays.fill(data, 'O');192List<String> scratch = new ArrayList<>();193scratch.add("-Xmx32m");194scratch.add(bag + String.valueOf(data));195scratch.add(ver);196rv.add(Collections.nCopies(2, scratch));197198data = new char[2*ARG_FILE_PARSER_BUF_SIZE + 1024];199Arrays.fill(data, 'O');200scratch = new ArrayList<>();201scratch.add(bag + String.valueOf(data));202scratch.add(ver);203rv.add(Collections.nCopies(2, scratch));204205// 8210810: position escaping character at boundary206// reserve space for quote and backslash207data = new char[ARG_FILE_PARSER_BUF_SIZE - bag.length() - 2];208Arrays.fill(data, 'O');209scratch = new ArrayList<>();210String filling = String.valueOf(data);211scratch.add(bag + "'" + filling + "\\\\aaa\\\\'");212scratch.add(ver);213rv.add(List.of(scratch, List.of(bag + filling + "\\aaa\\", ver)));214return rv;215}216217// 8240629: end or start comment at boundary218@Test219public void test8240629() throws IOException {220char[] data = new char[ARG_FILE_PARSER_BUF_SIZE];221data[0] = '#';222Arrays.fill(data, 1, data.length, '0');223224int need = ARG_FILE_PARSER_BUF_SIZE - System.lineSeparator().length();225// Comment end before, at, after boundary226for (int count = need - 1; count <= need + 1 ; count++) {227String commentAtBoundary = String.valueOf(data, 0, count);228List<String> content = new ArrayList<>();229content.add(commentAtBoundary);230content.add("# start a new comment at boundary");231content.add("-Dfoo=bar");232verifyParsing(content, List.of("-Dfoo=bar"));233}234}235236// ensure the arguments in the file are read in correctly237private void verifyParsing(List<String> lines, List<String> args) throws IOException {238File argFile = createArgFile(lines);239String fname = "@" + argFile.getName();240Map<String, String> env = new HashMap<>();241env.put(JLDEBUG_KEY, "true");242243TestResult tr;244if (args.contains("-version")) {245tr = doExec(env, javaCmd, fname);246} else {247tr = doExec(env, javaCmd, fname, "-version");248}249tr.checkPositive();250verifyOutput(args, tr);251252String lastArg = args.contains("-version") ? "-Dlast.arg" : "-version";253tr = doExec(env, javaCmd, "-Xint", fname, lastArg);254List<String> scratch = new ArrayList<>();255scratch.add("-Xint");256scratch.addAll(args);257scratch.add(lastArg);258verifyOutput(scratch, tr);259260argFile.delete();261}262263@Test264public void testSyntax() throws IOException {265List<List<List<String>>> allcases = loadCases();266for (List<List<String>> test: allcases) {267verifyParsing(test.get(0), test.get(1));268}269}270271@Test272public void badCases() throws IOException {273List<String> lines = Arrays.asList(274"-Dno.escape=\"Forgot to escape backslash\\\" -version");275File argFile = createArgFile(lines);276String fname = "@" + argFile.getName();277Map<String, String> env = new HashMap<>();278env.put(JLDEBUG_KEY, "true");279280TestResult tr = doExec(env, javaCmd, fname);281tr.contains("argv[1] = -Dno.escape=Forgot to escape backslash\" -version");282tr.checkNegative();283if (!tr.testStatus) {284System.out.println(tr);285throw new RuntimeException("test fails");286}287argFile.delete();288}289290public static void main(String... args) throws Exception {291ArgFileSyntax a = new ArgFileSyntax();292a.run(args);293if (testExitValue > 0) {294System.out.println("Total of " + testExitValue + " failed");295System.exit(1);296} else {297System.out.println("All tests pass");298}299}300}301302303