0N/A/*
5996N/A * Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
647N/A/**
647N/A * @test
2581N/A * @bug 5030233 6214916 6356475 6571029 6684582 6742159 4459600 6758881 6753938
6109N/A * 6894719 6968053 7151434 7146424 8007333
667N/A * @summary Argument parsing validation.
4854N/A * @compile -XDignore.symbol.file Arrrghs.java
5828N/A * @run main/othervm Arrrghs
647N/A */
647N/A
176N/Aimport java.io.BufferedReader;
176N/Aimport java.io.File;
647N/Aimport java.io.FileNotFoundException;
176N/Aimport java.io.IOException;
176N/Aimport java.io.InputStream;
176N/Aimport java.io.InputStreamReader;
5270N/Aimport java.util.ArrayList;
5270N/Aimport java.util.Arrays;
5270N/Aimport java.util.HashMap;
5270N/Aimport java.util.List;
176N/Aimport java.util.Map;
5270N/Aimport java.util.regex.Matcher;
5270N/Aimport java.util.regex.Pattern;
0N/A
4854N/Apublic class Arrrghs extends TestHelper {
647N/A private Arrrghs(){}
0N/A /**
647N/A * This class provides various tests for arguments processing.
0N/A * A group of tests to ensure that arguments are passed correctly to
0N/A * a child java process upon a re-exec, this typically happens when
0N/A * a version other than the one being executed is requested by the user.
0N/A *
0N/A * History: these set of tests were part of Arrrghs.sh. The MKS shell
647N/A * implementations were notoriously buggy. Implementing these tests purely
0N/A * in Java is not only portable but also robust.
0N/A *
0N/A */
0N/A
0N/A // The version string to force a re-exec
0N/A final static String VersionStr = "-version:1.1+";
0N/A
0N/A // The Cookie or the pattern we match in the debug output.
0N/A final static String Cookie = "ReExec Args: ";
0N/A
0N/A /*
0N/A * SIGH, On Windows all strings are quoted, we need to unwrap it
0N/A */
0N/A private static String removeExtraQuotes(String in) {
4854N/A if (isWindows) {
0N/A // Trim the string and remove the enclosed quotes if any.
0N/A in = in.trim();
0N/A if (in.startsWith("\"") && in.endsWith("\"")) {
0N/A return in.substring(1, in.length()-1);
0N/A }
0N/A }
0N/A return in;
0N/A }
0N/A
0N/A /*
0N/A * This method detects the cookie in the output stream of the process.
0N/A */
5270N/A private boolean detectCookie(InputStream istream,
647N/A String expectedArguments) throws IOException {
0N/A BufferedReader rd = new BufferedReader(new InputStreamReader(istream));
0N/A boolean retval = false;
0N/A
0N/A String in = rd.readLine();
0N/A while (in != null) {
4854N/A if (debug) System.out.println(in);
0N/A if (in.startsWith(Cookie)) {
0N/A String detectedArgument = removeExtraQuotes(in.substring(Cookie.length()));
0N/A if (expectedArguments.equals(detectedArgument)) {
0N/A retval = true;
0N/A } else {
647N/A System.out.println("Error: Expected Arguments\t:'" +
647N/A expectedArguments + "'");
647N/A System.out.println(" Detected Arguments\t:'" +
647N/A detectedArgument + "'");
0N/A }
0N/A // Return the value asap if not in debug mode.
4854N/A if (!debug) {
0N/A rd.close();
0N/A istream.close();
0N/A return retval;
0N/A }
0N/A }
0N/A in = rd.readLine();
0N/A }
0N/A return retval;
0N/A }
0N/A
5270N/A private boolean doReExecTest0(ProcessBuilder pb, String expectedArguments) {
0N/A boolean retval = false;
0N/A try {
176N/A pb.redirectErrorStream(true);
0N/A Process p = pb.start();
0N/A retval = detectCookie(p.getInputStream(), expectedArguments);
0N/A p.waitFor();
0N/A p.destroy();
0N/A } catch (Exception ex) {
0N/A ex.printStackTrace();
0N/A throw new RuntimeException(ex.getMessage());
0N/A }
0N/A return retval;
0N/A }
0N/A
0N/A /**
5270N/A * This method returns true if the expected and detected arguments are the same.
0N/A * Quoting could cause dissimilar testArguments and expected arguments.
0N/A */
5270N/A int doReExecTest(String testArguments, String expectedPattern) {
4854N/A ProcessBuilder pb = new ProcessBuilder(javaCmd,
647N/A VersionStr, testArguments);
0N/A
0N/A Map<String, String> env = pb.environment();
5270N/A env.put(JLDEBUG_KEY, "true");
5270N/A return doReExecTest0(pb, testArguments) ? 0 : 1;
0N/A }
0N/A
0N/A /**
0N/A * A convenience method for identical test pattern and expected arguments
0N/A */
5270N/A int doReExecTest(String testPattern) {
5270N/A return doReExecTest(testPattern, testPattern);
647N/A }
647N/A
5270N/A @Test
5270N/A void testQuoteParsingThroughReExec() {
647N/A /*
647N/A * Tests for 6214916
647N/A * These tests require that a JVM (any JVM) be installed in the system registry.
647N/A * If none is installed, skip this test.
647N/A */
4854N/A TestResult tr = doExec(javaCmd, VersionStr, "-version");
647N/A if (!tr.isOK()) {
647N/A System.err.println("Warning:Argument Passing Tests were skipped, " +
647N/A "no java found in system registry.");
647N/A return;
647N/A }
647N/A
4854N/A
647N/A // Basic test
5270N/A testExitValue += doReExecTest("-a -b -c -d");
647N/A
647N/A // Basic test with many spaces
5270N/A testExitValue += doReExecTest("-a -b -c -d");
647N/A
647N/A // Quoted whitespace does matter ?
5270N/A testExitValue += doReExecTest("-a \"\"-b -c\"\" -d");
647N/A
647N/A
647N/A // Escaped quotes outside of quotes as literals
5270N/A testExitValue += doReExecTest("-a \\\"-b -c\\\" -d");
647N/A
647N/A // Check for escaped quotes inside of quotes as literal
5270N/A testExitValue += doReExecTest("-a \"-b \\\"stuff\\\"\" -c -d");
647N/A
647N/A // A quote preceeded by an odd number of slashes is a literal quote
5270N/A testExitValue += doReExecTest("-a -b\\\\\\\" -c -d");
647N/A
647N/A // A quote preceeded by an even number of slashes is a literal quote
647N/A // see 6214916.
5270N/A testExitValue += doReExecTest("-a -b\\\\\\\\\" -c -d");
647N/A
647N/A // Make sure that whitespace doesn't interfere with the removal of the
647N/A // appropriate tokens. (space-tab-space preceeds -jre-restict-search).
5270N/A testExitValue += doReExecTest("-a -b \t -jre-restrict-search -c -d", "-a -b -c -d");
647N/A
647N/A // Make sure that the mJRE tokens being stripped, aren't stripped if
647N/A // they happen to appear as arguments to the main class.
5270N/A testExitValue += doReExecTest("foo -version:1.1+");
5270N/A
5270N/A System.out.println("Completed arguments quoting tests with "
5270N/A + testExitValue + " errors");
5270N/A }
5270N/A // the pattern we hope to see in the output
5270N/A static final Pattern ArgPattern = Pattern.compile("\\s*argv\\[[0-9]*\\].*=.*");
5270N/A
5270N/A void checkArgumentParsing(String inArgs, String... expArgs) throws IOException {
5270N/A List<String> scratchpad = new ArrayList<>();
5270N/A scratchpad.add("set " + JLDEBUG_KEY + "=true");
5270N/A // GAK, -version needs to be added so that windows can flush its stderr
5270N/A // exiting the process prematurely can terminate the stderr.
5270N/A scratchpad.add(javaCmd + " -version " + inArgs);
5270N/A File batFile = new File("atest.bat");
5828N/A createAFile(batFile, scratchpad);
5270N/A
5270N/A TestResult tr = doExec(batFile.getName());
5270N/A
5270N/A ArrayList<String> expList = new ArrayList<>();
5270N/A expList.add(javaCmd);
5270N/A expList.add("-version");
5270N/A expList.addAll(Arrays.asList(expArgs));
5270N/A
5270N/A List<String> gotList = new ArrayList<>();
5270N/A for (String x : tr.testOutput) {
5270N/A Matcher m = ArgPattern.matcher(x);
5270N/A if (m.matches()) {
5270N/A String a[] = x.split("=");
5270N/A gotList.add(a[a.length - 1].trim());
5270N/A }
5270N/A }
5270N/A if (!gotList.equals(expList)) {
5270N/A System.out.println(tr);
5270N/A System.out.println("Expected args:");
5270N/A System.out.println(expList);
5270N/A System.out.println("Obtained args:");
5270N/A System.out.println(gotList);
5270N/A throw new RuntimeException("Error: args do not match");
5270N/A }
5270N/A System.out.println("\'" + inArgs + "\'" + " - Test passed");
5270N/A }
5270N/A
5270N/A /*
5270N/A * This tests general quoting and are specific to Windows, *nixes
5270N/A * need not worry about this, these have been tested with Windows
5270N/A * implementation and those that are known to work are used against
5270N/A * the java implementation. Note that the ProcessBuilder gets in the
5270N/A * way when testing some of these arguments, therefore we need to
5270N/A * create and execute a .bat file containing the arguments.
5270N/A */
5270N/A @Test
5270N/A void testArgumentParsing() throws IOException {
5270N/A if (!isWindows)
5270N/A return;
5270N/A // no quotes
5270N/A checkArgumentParsing("a b c d", "a", "b", "c", "d");
5270N/A
5270N/A // single quotes
5270N/A checkArgumentParsing("\"a b c d\"", "a b c d");
5270N/A
5270N/A //double quotes
5270N/A checkArgumentParsing("\"\"a b c d\"\"", "a", "b", "c", "d");
5270N/A
5270N/A // triple quotes
5270N/A checkArgumentParsing("\"\"\"a b c d\"\"\"", "\"a b c d\"");
5270N/A
5270N/A // a literal within single quotes
5270N/A checkArgumentParsing("\"a\"b c d\"e\"", "ab", "c", "de");
5270N/A
5270N/A // a literal within double quotes
5270N/A checkArgumentParsing("\"\"a\"b c d\"e\"\"", "ab c de");
5270N/A
5270N/A // a literal quote
5270N/A checkArgumentParsing("a\\\"b", "a\"b");
5270N/A
5270N/A // double back-slash
5270N/A checkArgumentParsing("\"a b c d\\\\\"", "a b c d\\");
5270N/A
5270N/A // triple back-slash
5270N/A checkArgumentParsing("a\\\\\\\"b", "a\\\"b");
5270N/A
5270N/A // dangling quote
5270N/A checkArgumentParsing("\"a b c\"\"", "a b c\"");
5270N/A
5270N/A // expansions of white space separators
5270N/A checkArgumentParsing("a b", "a", "b");
5270N/A checkArgumentParsing("a\tb", "a", "b");
5270N/A checkArgumentParsing("a \t b", "a", "b");
5270N/A
5270N/A checkArgumentParsing("\"C:\\TEST A\\\\\"", "C:\\TEST A\\");
5270N/A checkArgumentParsing("\"\"C:\\TEST A\\\\\"\"", "C:\\TEST", "A\\");
5270N/A
5270N/A // MS Windows tests
5270N/A // triple back-slash
5270N/A checkArgumentParsing("a\\\\\\d", "a\\\\\\d");
5270N/A
5270N/A // triple back-slash in quotes
5270N/A checkArgumentParsing("\"a\\\\\\d\"", "a\\\\\\d");
5270N/A
5270N/A // slashes separating characters
5270N/A checkArgumentParsing("X\\Y\\Z", "X\\Y\\Z");
5270N/A checkArgumentParsing("\\X\\Y\\Z", "\\X\\Y\\Z");
5270N/A
5270N/A // literals within dangling quotes, etc.
5270N/A checkArgumentParsing("\"a b c\" d e", "a b c", "d", "e");
5270N/A checkArgumentParsing("\"ab\\\"c\" \"\\\\\" d", "ab\"c", "\\", "d");
5270N/A checkArgumentParsing("a\\\\\\c d\"e f\"g h", "a\\\\\\c", "de fg", "h");
5270N/A checkArgumentParsing("a\\\\\\\"b c d", "a\\\"b", "c", "d");
5270N/A checkArgumentParsing("a\\\\\\\\\"g c\" d e", "a\\\\g c", "d", "e");
5270N/A
5270N/A // treatment of back-slashes
5270N/A checkArgumentParsing("*\\", "*\\");
5270N/A checkArgumentParsing("*/", "*/");
5270N/A checkArgumentParsing(".\\*", ".\\*");
5270N/A checkArgumentParsing("./*", "./*");
5270N/A checkArgumentParsing("..\\..\\*", "..\\..\\*");
5270N/A checkArgumentParsing("../../*", "../../*");
5270N/A checkArgumentParsing("..\\..\\", "..\\..\\");
5270N/A checkArgumentParsing("../../", "../../");
5996N/A checkArgumentParsing("a b\\ c", "a", "b\\", "c");
6109N/A // 2 back-slashes
6109N/A checkArgumentParsing("\\\\?", "\\\\?");
6109N/A // 3 back-slashes
6109N/A checkArgumentParsing("\\\\\\?", "\\\\\\?");
6109N/A // 4 back-slashes
6109N/A checkArgumentParsing("\\\\\\\\?", "\\\\\\\\?");
6109N/A // 5 back-slashes
6109N/A checkArgumentParsing("\\\\\\\\\\?", "\\\\\\\\\\?");
6109N/A // 6 back-slashes
6109N/A checkArgumentParsing("\\\\\\\\\\\\?", "\\\\\\\\\\\\?");
6109N/A
6109N/A // more treatment of mixed slashes
6109N/A checkArgumentParsing("f1/ f3\\ f4/", "f1/", "f3\\", "f4/");
6109N/A checkArgumentParsing("f1/ f2\' ' f3/ f4/", "f1/", "f2\'", "'", "f3/", "f4/");
5270N/A }
5270N/A
5270N/A private void initEmptyDir(File emptyDir) throws IOException {
5270N/A if (emptyDir.exists()) {
5270N/A recursiveDelete(emptyDir);
5270N/A }
5270N/A emptyDir.mkdir();
5270N/A }
5270N/A
5270N/A private void initDirWithJavaFiles(File libDir) throws IOException {
5270N/A
5270N/A if (libDir.exists()) {
5270N/A recursiveDelete(libDir);
5270N/A }
5270N/A libDir.mkdirs();
5270N/A ArrayList<String> scratchpad = new ArrayList<>();
5270N/A scratchpad.add("package lib;");
5270N/A scratchpad.add("public class Fbo {");
5270N/A scratchpad.add("public static void main(String... args){Foo.f();}");
5270N/A scratchpad.add("public static void f(){}");
5270N/A scratchpad.add("}");
5270N/A createFile(new File(libDir, "Fbo.java"), scratchpad);
5270N/A
5270N/A scratchpad.clear();
5270N/A scratchpad.add("package lib;");
5270N/A scratchpad.add("public class Foo {");
5270N/A scratchpad.add("public static void main(String... args){");
5270N/A scratchpad.add("for (String x : args) {");
5270N/A scratchpad.add("System.out.println(x);");
5270N/A scratchpad.add("}");
5270N/A scratchpad.add("Fbo.f();");
5270N/A scratchpad.add("}");
5270N/A scratchpad.add("public static void f(){}");
5270N/A scratchpad.add("}");
5270N/A createFile(new File(libDir, "Foo.java"), scratchpad);
5270N/A }
5270N/A
5270N/A void checkArgumentWildcard(String inArgs, String... expArgs) throws IOException {
5270N/A String[] in = {inArgs};
5270N/A checkArgumentWildcard(in, expArgs);
647N/A
5270N/A // now add arbitrary arguments before and after
5270N/A String[] outInArgs = { "-Q", inArgs, "-R"};
5270N/A
5270N/A String[] outExpArgs = new String[expArgs.length + 2];
5270N/A outExpArgs[0] = "-Q";
5270N/A System.arraycopy(expArgs, 0, outExpArgs, 1, expArgs.length);
5270N/A outExpArgs[expArgs.length + 1] = "-R";
5270N/A checkArgumentWildcard(outInArgs, outExpArgs);
5270N/A }
5270N/A
5270N/A void checkArgumentWildcard(String[] inArgs, String[] expArgs) throws IOException {
5270N/A ArrayList<String> argList = new ArrayList<>();
5270N/A argList.add(javaCmd);
5270N/A argList.add("-cp");
5270N/A argList.add("lib" + File.separator + "*");
5270N/A argList.add("lib.Foo");
5270N/A argList.addAll(Arrays.asList(inArgs));
5270N/A String[] cmds = new String[argList.size()];
5270N/A argList.toArray(cmds);
5270N/A TestResult tr = doExec(cmds);
5270N/A if (!tr.isOK()) {
5270N/A System.out.println(tr);
5270N/A throw new RuntimeException("Error: classpath single entry wildcard entry");
5270N/A }
5270N/A
5270N/A ArrayList<String> expList = new ArrayList<>();
5270N/A expList.addAll(Arrays.asList(expArgs));
5270N/A
5270N/A List<String> gotList = new ArrayList<>();
5270N/A for (String x : tr.testOutput) {
5270N/A gotList.add(x.trim());
5270N/A }
5270N/A if (!gotList.equals(expList)) {
5270N/A System.out.println(tr);
5270N/A System.out.println("Expected args:");
5270N/A System.out.println(expList);
5270N/A System.out.println("Obtained args:");
5270N/A System.out.println(gotList);
5270N/A throw new RuntimeException("Error: args do not match");
5270N/A }
5270N/A System.out.print("\'");
5270N/A for (String x : inArgs) {
5270N/A System.out.print(x + " ");
5270N/A }
5270N/A System.out.println("\'" + " - Test passed");
647N/A }
647N/A
647N/A /*
5270N/A * These tests are not expected to work on *nixes, and are ignored.
5270N/A */
5270N/A @Test
5270N/A void testWildCardArgumentProcessing() throws IOException {
5270N/A if (!isWindows)
5270N/A return;
5270N/A File cwd = new File(".");
5270N/A File libDir = new File(cwd, "lib");
5270N/A initDirWithJavaFiles(libDir);
5270N/A initEmptyDir(new File(cwd, "empty"));
5270N/A
5270N/A // test if javac (the command) can compile *.java
5270N/A TestResult tr = doExec(javacCmd, libDir.getName() + File.separator + "*.java");
5270N/A if (!tr.isOK()) {
5270N/A System.out.println(tr);
5270N/A throw new RuntimeException("Error: compiling java wildcards");
5270N/A }
5270N/A
5270N/A // use the jar cmd to create jars using the ? wildcard
5270N/A File jarFoo = new File(libDir, "Foo.jar");
5270N/A tr = doExec(jarCmd, "cvf", jarFoo.getAbsolutePath(), "lib" + File.separator + "F?o.class");
5270N/A if (!tr.isOK()) {
5270N/A System.out.println(tr);
5270N/A throw new RuntimeException("Error: creating jar with wildcards");
5270N/A }
5270N/A
5270N/A // now the litmus test!, this should work
5270N/A checkArgumentWildcard("a", "a");
5270N/A
5270N/A // test for basic expansion
5270N/A checkArgumentWildcard("lib\\F*java", "lib\\Fbo.java", "lib\\Foo.java");
5270N/A
5270N/A // basic expansion in quotes
5270N/A checkArgumentWildcard("\"lib\\F*java\"", "lib\\F*java");
5270N/A
5270N/A checkArgumentWildcard("lib\\**", "lib\\Fbo.class", "lib\\Fbo.java",
5270N/A "lib\\Foo.class", "lib\\Foo.jar", "lib\\Foo.java");
5270N/A
5270N/A checkArgumentWildcard("lib\\*?", "lib\\Fbo.class", "lib\\Fbo.java",
5270N/A "lib\\Foo.class", "lib\\Foo.jar", "lib\\Foo.java");
5270N/A
5270N/A checkArgumentWildcard("lib\\?*", "lib\\Fbo.class", "lib\\Fbo.java",
5270N/A "lib\\Foo.class", "lib\\Foo.jar", "lib\\Foo.java");
5270N/A
5270N/A checkArgumentWildcard("lib\\?", "lib\\?");
5270N/A
5270N/A // test for basic expansion
5270N/A checkArgumentWildcard("lib\\*java", "lib\\Fbo.java", "lib\\Foo.java");
5270N/A
5270N/A // basic expansion in quotes
5270N/A checkArgumentWildcard("\"lib\\*.java\"", "lib\\*.java");
5270N/A
5270N/A // suffix expansion
5270N/A checkArgumentWildcard("lib\\*.class", "lib\\Fbo.class", "lib\\Foo.class");
5270N/A
5270N/A // suffix expansion in quotes
5270N/A checkArgumentWildcard("\"lib\\*.class\"", "lib\\*.class");
5270N/A
5270N/A // check for ? expansion now
5270N/A checkArgumentWildcard("lib\\F?o.java", "lib\\Fbo.java", "lib\\Foo.java");
5270N/A
5270N/A // check ? in quotes
5270N/A checkArgumentWildcard("\"lib\\F?o.java\"", "lib\\F?o.java");
5270N/A
5270N/A // check ? as suffixes
5270N/A checkArgumentWildcard("lib\\F?o.????", "lib\\Fbo.java", "lib\\Foo.java");
5270N/A
5270N/A // check ? in a leading role
5270N/A checkArgumentWildcard("lib\\???.java", "lib\\Fbo.java", "lib\\Foo.java");
5270N/A checkArgumentWildcard("\"lib\\???.java\"", "lib\\???.java");
5270N/A
5270N/A // check ? prefixed with -
5270N/A checkArgumentWildcard("-?", "-?");
5270N/A
5270N/A // check * prefixed with -
5270N/A checkArgumentWildcard("-*", "-*");
5270N/A
5270N/A // check on empty directory
5270N/A checkArgumentWildcard("empty\\*", "empty\\*");
5270N/A checkArgumentWildcard("empty\\**", "empty\\**");
5270N/A checkArgumentWildcard("empty\\?", "empty\\?");
5270N/A checkArgumentWildcard("empty\\??", "empty\\??");
5270N/A checkArgumentWildcard("empty\\*?", "empty\\*?");
5270N/A checkArgumentWildcard("empty\\?*", "empty\\?*");
5270N/A
5270N/A }
5270N/A
5270N/A void doArgumentCheck(String inArgs, String... expArgs) {
5270N/A Map<String, String> env = new HashMap<>();
5270N/A env.put(JLDEBUG_KEY, "true");
5270N/A TestResult tr = doExec(env, javaCmd, inArgs);
5270N/A System.out.println(tr);
5270N/A int sindex = tr.testOutput.indexOf("Command line args:");
5270N/A if (sindex < 0) {
5270N/A System.out.println(tr);
5270N/A throw new RuntimeException("Error: no output");
5270N/A }
5270N/A sindex++; // skip over the tag
5270N/A List<String> gotList = new ArrayList<>();
5270N/A for (String x : tr.testOutput.subList(sindex, sindex + expArgs.length)) {
5270N/A String a[] = x.split("=");
5270N/A gotList.add(a[a.length - 1].trim());
5270N/A }
5270N/A List<String> expList = Arrays.asList(expArgs);
5270N/A if (!gotList.equals(expList)) {
5270N/A System.out.println(tr);
5270N/A System.out.println("Expected args:");
5270N/A System.out.println(expList);
5270N/A System.out.println("Obtained args:");
5270N/A System.out.println(gotList);
5270N/A throw new RuntimeException("Error: args do not match");
5270N/A }
5270N/A }
5270N/A
5270N/A
5270N/A /*
647N/A * These tests are usually run on non-existent targets to check error results
647N/A */
5270N/A @Test
5270N/A void testBasicErrorMessages() {
647N/A // Tests for 5030233
4854N/A TestResult tr = doExec(javaCmd, "-cp");
647N/A tr.checkNegative();
647N/A tr.isNotZeroOutput();
5270N/A if (!tr.testStatus)
5270N/A System.out.println(tr);
647N/A
4854N/A tr = doExec(javaCmd, "-classpath");
647N/A tr.checkNegative();
647N/A tr.isNotZeroOutput();
5270N/A if (!tr.testStatus)
5270N/A System.out.println(tr);
647N/A
4854N/A tr = doExec(javaCmd, "-jar");
647N/A tr.checkNegative();
647N/A tr.isNotZeroOutput();
5270N/A if (!tr.testStatus)
5270N/A System.out.println(tr);
647N/A
4854N/A tr = doExec(javacCmd, "-cp");
647N/A tr.checkNegative();
647N/A tr.isNotZeroOutput();
5270N/A if (!tr.testStatus)
5270N/A System.out.println(tr);
647N/A
647N/A // Test for 6356475 "REGRESSION:"java -X" from cmdline fails"
4854N/A tr = doExec(javaCmd, "-X");
647N/A tr.checkPositive();
647N/A tr.isNotZeroOutput();
5270N/A if (!tr.testStatus)
5270N/A System.out.println(tr);
647N/A
4854N/A tr = doExec(javaCmd, "-help");
647N/A tr.checkPositive();
647N/A tr.isNotZeroOutput();
5270N/A if (!tr.testStatus)
5270N/A System.out.println(tr);
2581N/A
2581N/A // 6753938, test for non-negative exit value for an incorrectly formed
2581N/A // command line, '% java'
4854N/A tr = doExec(javaCmd);
2581N/A tr.checkNegative();
2581N/A tr.isNotZeroOutput();
5270N/A if (!tr.testStatus)
5270N/A System.out.println(tr);
2581N/A
2581N/A // 6753938, test for non-negative exit value for an incorrectly formed
2581N/A // command line, '% java -Xcomp'
4854N/A tr = doExec(javaCmd, "-Xcomp");
2581N/A tr.checkNegative();
2581N/A tr.isNotZeroOutput();
5270N/A if (!tr.testStatus)
5270N/A System.out.println(tr);
5271N/A
5271N/A // 7151434, test for non-negative exit value for an incorrectly formed
5271N/A // command line, '% java -jar -W', note the bogus -W
5271N/A tr = doExec(javaCmd, "-jar", "-W");
5271N/A tr.checkNegative();
5271N/A tr.contains("Unrecognized option: -W");
5271N/A if (!tr.testStatus)
5271N/A System.out.println(tr);
647N/A }
647N/A
647N/A /*
4854N/A * Tests various dispositions of the main method, these tests are limited
4854N/A * to English locales as they check for error messages that are localized.
647N/A */
5270N/A @Test
5270N/A void testMainMethod() throws FileNotFoundException {
4854N/A if (!isEnglishLocale()) {
4854N/A return;
4854N/A }
4854N/A
4854N/A TestResult tr = null;
647N/A
647N/A // a missing class
4854N/A createJar("MIA", new File("some.jar"), new File("Foo"),
653N/A (String[])null);
4854N/A tr = doExec(javaCmd, "-jar", "some.jar");
3487N/A tr.contains("Error: Could not find or load main class MIA");
5270N/A if (!tr.testStatus)
5270N/A System.out.println(tr);
647N/A // use classpath to check
4854N/A tr = doExec(javaCmd, "-cp", "some.jar", "MIA");
3487N/A tr.contains("Error: Could not find or load main class MIA");
5270N/A if (!tr.testStatus)
5270N/A System.out.println(tr);
647N/A
647N/A // incorrect method access
4854N/A createJar(new File("some.jar"), new File("Foo"),
647N/A "private static void main(String[] args){}");
4854N/A tr = doExec(javaCmd, "-jar", "some.jar");
647N/A tr.contains("Error: Main method not found in class Foo");
5270N/A if (!tr.testStatus)
5270N/A System.out.println(tr);
647N/A // use classpath to check
4854N/A tr = doExec(javaCmd, "-cp", "some.jar", "Foo");
647N/A tr.contains("Error: Main method not found in class Foo");
5270N/A if (!tr.testStatus)
5270N/A System.out.println(tr);
647N/A
647N/A // incorrect return type
4854N/A createJar(new File("some.jar"), new File("Foo"),
647N/A "public static int main(String[] args){return 1;}");
4854N/A tr = doExec(javaCmd, "-jar", "some.jar");
647N/A tr.contains("Error: Main method must return a value of type void in class Foo");
5270N/A if (!tr.testStatus)
5270N/A System.out.println(tr);
647N/A // use classpath to check
4854N/A tr = doExec(javaCmd, "-cp", "some.jar", "Foo");
647N/A tr.contains("Error: Main method must return a value of type void in class Foo");
5270N/A if (!tr.testStatus)
5270N/A System.out.println(tr);
647N/A
647N/A // incorrect parameter type
4854N/A createJar(new File("some.jar"), new File("Foo"),
647N/A "public static void main(Object[] args){}");
4854N/A tr = doExec(javaCmd, "-jar", "some.jar");
647N/A tr.contains("Error: Main method not found in class Foo");
5270N/A if (!tr.testStatus)
5270N/A System.out.println(tr);
647N/A // use classpath to check
4854N/A tr = doExec(javaCmd, "-cp", "some.jar", "Foo");
647N/A tr.contains("Error: Main method not found in class Foo");
5270N/A if (!tr.testStatus)
5270N/A System.out.println(tr);
647N/A
647N/A // incorrect method type - non-static
4854N/A createJar(new File("some.jar"), new File("Foo"),
653N/A "public void main(String[] args){}");
4854N/A tr = doExec(javaCmd, "-jar", "some.jar");
653N/A tr.contains("Error: Main method is not static in class Foo");
5270N/A if (!tr.testStatus)
5270N/A System.out.println(tr);
647N/A // use classpath to check
4854N/A tr = doExec(javaCmd, "-cp", "some.jar", "Foo");
653N/A tr.contains("Error: Main method is not static in class Foo");
5270N/A if (!tr.testStatus)
5270N/A System.out.println(tr);
647N/A
647N/A // amongst a potpourri of kindred main methods, is the right one chosen ?
4854N/A createJar(new File("some.jar"), new File("Foo"),
3487N/A "void main(Object[] args){}",
3487N/A "int main(Float[] args){return 1;}",
3487N/A "private void main() {}",
3487N/A "private static void main(int x) {}",
3487N/A "public int main(int argc, String[] argv) {return 1;}",
3487N/A "public static void main(String[] args) {System.out.println(\"THE_CHOSEN_ONE\");}");
4854N/A tr = doExec(javaCmd, "-jar", "some.jar");
647N/A tr.contains("THE_CHOSEN_ONE");
5270N/A if (!tr.testStatus)
5270N/A System.out.println(tr);
647N/A // use classpath to check
4854N/A tr = doExec(javaCmd, "-cp", "some.jar", "Foo");
647N/A tr.contains("THE_CHOSEN_ONE");
5270N/A if (!tr.testStatus)
5270N/A System.out.println(tr);
653N/A
653N/A // test for extraneous whitespace in the Main-Class attribute
4854N/A createJar(" Foo ", new File("some.jar"), new File("Foo"),
653N/A "public static void main(String... args){}");
4854N/A tr = doExec(javaCmd, "-jar", "some.jar");
653N/A tr.checkPositive();
5270N/A if (!tr.testStatus)
5270N/A System.out.println(tr);
0N/A }
4854N/A /*
4854N/A * tests 6968053, ie. we turn on the -Xdiag (for now) flag and check if
4854N/A * the suppressed stack traces are exposed, ignore these tests for localized
4854N/A * locales, limiting to English only.
4854N/A */
5270N/A @Test
5270N/A void testDiagOptions() throws FileNotFoundException {
4854N/A if (!isEnglishLocale()) { // only english version
4854N/A return;
4854N/A }
4854N/A TestResult tr = null;
3487N/A // a missing class
4854N/A createJar("MIA", new File("some.jar"), new File("Foo"),
3487N/A (String[])null);
4854N/A tr = doExec(javaCmd, "-Xdiag", "-jar", "some.jar");
3487N/A tr.contains("Error: Could not find or load main class MIA");
3487N/A tr.contains("java.lang.ClassNotFoundException: MIA");
5270N/A if (!tr.testStatus)
5270N/A System.out.println(tr);
3487N/A
3487N/A // use classpath to check
4854N/A tr = doExec(javaCmd, "-Xdiag", "-cp", "some.jar", "MIA");
3487N/A tr.contains("Error: Could not find or load main class MIA");
3487N/A tr.contains("java.lang.ClassNotFoundException: MIA");
5270N/A if (!tr.testStatus)
5270N/A System.out.println(tr);
3487N/A
3487N/A // a missing class on the classpath
4854N/A tr = doExec(javaCmd, "-Xdiag", "NonExistentClass");
3487N/A tr.contains("Error: Could not find or load main class NonExistentClass");
3487N/A tr.contains("java.lang.ClassNotFoundException: NonExistentClass");
5270N/A if (!tr.testStatus)
5270N/A System.out.println(tr);
3487N/A }
0N/A
5270N/A @Test
5270N/A static void testJreRestrictSearchFlag() {
2922N/A // test both arguments to ensure they exist
4854N/A TestResult tr = null;
4854N/A tr = doExec(javaCmd,
2922N/A "-no-jre-restrict-search", "-version");
2922N/A tr.checkPositive();
5270N/A if (!tr.testStatus)
5270N/A System.out.println(tr);
2922N/A
4854N/A tr = doExec(javaCmd,
2922N/A "-jre-restrict-search", "-version");
2922N/A tr.checkPositive();
5270N/A if (!tr.testStatus)
5270N/A System.out.println(tr);
2922N/A }
4343N/A
0N/A /**
0N/A * @param args the command line arguments
647N/A * @throws java.io.FileNotFoundException
0N/A */
5270N/A public static void main(String[] args) throws Exception {
4854N/A if (debug) {
2922N/A System.out.println("Starting Arrrghs tests");
2922N/A }
5270N/A Arrrghs a = new Arrrghs();
5270N/A a.run(args);
4854N/A if (testExitValue > 0) {
4854N/A System.out.println("Total of " + testExitValue + " failed");
2922N/A System.exit(1);
2922N/A } else {
2922N/A System.out.println("All tests pass");
0N/A }
0N/A }
2922N/A}