Arrrghs.java revision 2362
0N/A/*
196N/A * Copyright (c) 2007, 2008, 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 *
0N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0N/A * or visit www.oracle.com if you need additional information or have any
0N/A * questions.
0N/A */
0N/A
0N/A/**
0N/A * @test
304N/A * @bug 5030233 6214916 6356475 6571029 6684582 6742159 4459600 6758881
0N/A * @summary Argument parsing validation.
0N/A * @compile -XDignore.symbol.file Arrrghs.java TestHelper.java
0N/A * @run main Arrrghs
0N/A */
0N/A
0N/Aimport java.io.BufferedReader;
0N/Aimport java.io.File;
0N/Aimport java.io.FileNotFoundException;
0N/Aimport java.io.IOException;
0N/Aimport java.io.InputStream;
0N/Aimport java.io.InputStreamReader;
0N/Aimport java.util.Map;
0N/A
0N/Apublic class Arrrghs {
0N/A private Arrrghs(){}
0N/A /**
0N/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
0N/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) {
0N/A if (TestHelper.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);
304N/A }
304N/A }
0N/A return in;
0N/A }
0N/A
0N/A /*
304N/A * This method detects the cookie in the output stream of the process.
304N/A */
304N/A private static boolean detectCookie(InputStream istream,
304N/A String expectedArguments) throws IOException {
304N/A BufferedReader rd = new BufferedReader(new InputStreamReader(istream));
304N/A boolean retval = false;
304N/A
304N/A String in = rd.readLine();
304N/A while (in != null) {
304N/A if (TestHelper.debug) System.out.println(in);
304N/A if (in.startsWith(Cookie)) {
304N/A String detectedArgument = removeExtraQuotes(in.substring(Cookie.length()));
304N/A if (expectedArguments.equals(detectedArgument)) {
304N/A retval = true;
304N/A } else {
304N/A System.out.println("Error: Expected Arguments\t:'" +
304N/A expectedArguments + "'");
304N/A System.out.println(" Detected Arguments\t:'" +
304N/A detectedArgument + "'");
304N/A }
304N/A // Return the value asap if not in debug mode.
304N/A if (!TestHelper.debug) {
304N/A rd.close();
304N/A istream.close();
304N/A return retval;
304N/A }
304N/A }
304N/A in = rd.readLine();
304N/A }
304N/A return retval;
304N/A }
304N/A
304N/A private static boolean doTest0(ProcessBuilder pb, String expectedArguments) {
0N/A boolean retval = false;
0N/A try {
0N/A pb.redirectErrorStream(true);
0N/A Process p = pb.start();
0N/A retval = detectCookie(p.getInputStream(), expectedArguments);
0N/A p.waitFor();
304N/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 /**
0N/A * This method return true if the expected and detected arguments are the same.
304N/A * Quoting could cause dissimilar testArguments and expected arguments.
0N/A */
0N/A static int doTest(String testArguments, String expectedPattern) {
304N/A ProcessBuilder pb = new ProcessBuilder(TestHelper.javaCmd,
304N/A VersionStr, testArguments);
0N/A
0N/A Map<String, String> env = pb.environment();
0N/A env.put("_JAVA_LAUNCHER_DEBUG", "true");
0N/A return doTest0(pb, testArguments) ? 0 : 1;
0N/A }
0N/A
0N/A /**
0N/A * A convenience method for identical test pattern and expected arguments
0N/A */
0N/A static int doTest(String testPattern) {
0N/A return doTest(testPattern, testPattern);
0N/A }
0N/A
0N/A static void quoteParsingTests() {
0N/A /*
0N/A * Tests for 6214916
0N/A * These tests require that a JVM (any JVM) be installed in the system registry.
0N/A * If none is installed, skip this test.
0N/A */
0N/A TestHelper.TestResult tr =
0N/A TestHelper.doExec(TestHelper.javaCmd, VersionStr, "-version");
0N/A if (!tr.isOK()) {
0N/A System.err.println("Warning:Argument Passing Tests were skipped, " +
0N/A "no java found in system registry.");
0N/A return;
0N/A }
0N/A
0N/A // Basic test
304N/A TestHelper.testExitValue += doTest("-a -b -c -d");
0N/A
0N/A // Basic test with many spaces
0N/A TestHelper.testExitValue += doTest("-a -b -c -d");
0N/A
0N/A // Quoted whitespace does matter ?
0N/A TestHelper.testExitValue += doTest("-a \"\"-b -c\"\" -d");
0N/A
0N/A
0N/A // Escaped quotes outside of quotes as literals
0N/A TestHelper.testExitValue += doTest("-a \\\"-b -c\\\" -d");
0N/A
0N/A // Check for escaped quotes inside of quotes as literal
0N/A TestHelper.testExitValue += doTest("-a \"-b \\\"stuff\\\"\" -c -d");
0N/A
0N/A // A quote preceeded by an odd number of slashes is a literal quote
0N/A TestHelper.testExitValue += doTest("-a -b\\\\\\\" -c -d");
0N/A
304N/A // A quote preceeded by an even number of slashes is a literal quote
304N/A // see 6214916.
304N/A TestHelper.testExitValue += doTest("-a -b\\\\\\\\\" -c -d");
304N/A
304N/A // Make sure that whitespace doesn't interfere with the removal of the
304N/A // appropriate tokens. (space-tab-space preceeds -jre-restict-search).
304N/A TestHelper.testExitValue += doTest("-a -b \t -jre-restrict-search -c -d","-a -b -c -d");
304N/A
304N/A // Make sure that the mJRE tokens being stripped, aren't stripped if
304N/A // they happen to appear as arguments to the main class.
304N/A TestHelper.testExitValue += doTest("foo -version:1.1+");
304N/A
304N/A System.out.println("Completed arguments quoting tests with " +
304N/A TestHelper.testExitValue + " errors");
304N/A }
0N/A
0N/A /*
0N/A * These tests are usually run on non-existent targets to check error results
0N/A */
0N/A static void runBasicErrorMessageTests() {
0N/A // Tests for 5030233
0N/A TestHelper.TestResult tr = TestHelper.doExec(TestHelper.javaCmd, "-cp");
304N/A tr.checkNegative();
0N/A tr.isNotZeroOutput();
0N/A System.out.println(tr);
0N/A
0N/A tr = TestHelper.doExec(TestHelper.javaCmd, "-classpath");
304N/A tr.checkNegative();
0N/A tr.isNotZeroOutput();
0N/A System.out.println(tr);
0N/A
0N/A tr = TestHelper.doExec(TestHelper.javaCmd, "-jar");
0N/A tr.checkNegative();
304N/A tr.isNotZeroOutput();
0N/A System.out.println(tr);
0N/A
0N/A tr = TestHelper.doExec(TestHelper.javacCmd, "-cp");
304N/A tr.checkNegative();
0N/A tr.isNotZeroOutput();
0N/A System.out.println(tr);
0N/A
0N/A // Test for 6356475 "REGRESSION:"java -X" from cmdline fails"
0N/A tr = TestHelper.doExec(TestHelper.javaCmd, "-X");
304N/A tr.checkPositive();
0N/A tr.isNotZeroOutput();
0N/A System.out.println(tr);
0N/A
0N/A tr = TestHelper.doExec(TestHelper.javaCmd, "-help");
304N/A tr.checkPositive();
0N/A tr.isNotZeroOutput();
0N/A System.out.println(tr);
0N/A }
304N/A
0N/A /*
0N/A * A set of tests which tests various dispositions of the main method.
0N/A */
0N/A static void runMainMethodTests() throws FileNotFoundException {
0N/A TestHelper.TestResult tr = null;
304N/A
0N/A // a missing class
0N/A TestHelper.createJar("MIA", new File("some.jar"), new File("Foo"),
0N/A (String[])null);
0N/A tr = TestHelper.doExec(TestHelper.javaCmd, "-jar", "some.jar");
0N/A tr.contains("Error: Could not find main class MIA");
304N/A tr.contains("java.lang.NoClassDefFoundError: MIA");
304N/A System.out.println(tr);
0N/A // use classpath to check
0N/A tr = TestHelper.doExec(TestHelper.javaCmd, "-cp", "some.jar", "MIA");
0N/A tr.contains("Error: Could not find main class MIA");
0N/A tr.contains("java.lang.NoClassDefFoundError: MIA");
0N/A System.out.println(tr);
0N/A
304N/A // incorrect method access
304N/A TestHelper.createJar(new File("some.jar"), new File("Foo"),
304N/A "private static void main(String[] args){}");
304N/A tr = TestHelper.doExec(TestHelper.javaCmd, "-jar", "some.jar");
304N/A tr.contains("Error: Main method not found in class Foo");
304N/A System.out.println(tr);
304N/A // use classpath to check
304N/A tr = TestHelper.doExec(TestHelper.javaCmd, "-cp", "some.jar", "Foo");
0N/A tr.contains("Error: Main method not found in class Foo");
304N/A System.out.println(tr);
304N/A
304N/A // incorrect return type
304N/A TestHelper.createJar(new File("some.jar"), new File("Foo"),
304N/A "public static int main(String[] args){return 1;}");
304N/A tr = TestHelper.doExec(TestHelper.javaCmd, "-jar", "some.jar");
304N/A tr.contains("Error: Main method must return a value of type void in class Foo");
0N/A System.out.println(tr);
0N/A // use classpath to check
0N/A tr = TestHelper.doExec(TestHelper.javaCmd, "-cp", "some.jar", "Foo");
0N/A tr.contains("Error: Main method must return a value of type void in class Foo");
304N/A System.out.println(tr);
304N/A
0N/A // incorrect parameter type
0N/A TestHelper.createJar(new File("some.jar"), new File("Foo"),
0N/A "public static void main(Object[] args){}");
304N/A tr = TestHelper.doExec(TestHelper.javaCmd, "-jar", "some.jar");
304N/A tr.contains("Error: Main method not found in class Foo");
0N/A System.out.println(tr);
0N/A // use classpath to check
0N/A tr = TestHelper.doExec(TestHelper.javaCmd, "-cp", "some.jar", "Foo");
0N/A tr.contains("Error: Main method not found in class Foo");
304N/A System.out.println(tr);
304N/A
0N/A // incorrect method type - non-static
0N/A TestHelper.createJar(new File("some.jar"), new File("Foo"),
304N/A "public void main(String[] args){}");
304N/A tr = TestHelper.doExec(TestHelper.javaCmd, "-jar", "some.jar");
0N/A tr.contains("Error: Main method is not static in class Foo");
304N/A System.out.println(tr);
304N/A // use classpath to check
0N/A tr = TestHelper.doExec(TestHelper.javaCmd, "-cp", "some.jar", "Foo");
0N/A tr.contains("Error: Main method is not static in class Foo");
304N/A System.out.println(tr);
304N/A
0N/A // amongst a potpourri of kindred main methods, is the right one chosen ?
304N/A TestHelper.createJar(new File("some.jar"), new File("Foo"),
0N/A "void main(Object[] args){}",
304N/A "int main(Float[] args){return 1;}",
0N/A "private void main() {}",
0N/A "private static void main(int x) {}",
304N/A "public int main(int argc, String[] argv) {return 1;}",
304N/A "public static void main(String[] args) {System.out.println(\"THE_CHOSEN_ONE\");}");
304N/A tr = TestHelper.doExec(TestHelper.javaCmd, "-jar", "some.jar");
0N/A tr.contains("THE_CHOSEN_ONE");
0N/A System.out.println(tr);
0N/A // use classpath to check
304N/A tr = TestHelper.doExec(TestHelper.javaCmd, "-cp", "some.jar", "Foo");
304N/A tr.contains("THE_CHOSEN_ONE");
304N/A System.out.println(tr);
0N/A
0N/A // test for extraneous whitespace in the Main-Class attribute
0N/A TestHelper.createJar(" Foo ", new File("some.jar"), new File("Foo"),
0N/A "public static void main(String... args){}");
0N/A tr = TestHelper.doExec(TestHelper.javaCmd, "-jar", "some.jar");
304N/A tr.checkPositive();
304N/A System.out.println(tr);
304N/A }
0N/A
0N/A /**
304N/A * @param args the command line arguments
0N/A * @throws java.io.FileNotFoundException
304N/A */
304N/A public static void main(String[] args) throws FileNotFoundException {
304N/A if (TestHelper.debug) System.out.println("Starting Arrrghs tests");
0N/A quoteParsingTests();
0N/A runBasicErrorMessageTests();
304N/A runMainMethodTests();
304N/A if (TestHelper.testExitValue > 0) {
304N/A System.out.println("Total of " + TestHelper.testExitValue + " failed");
0N/A System.exit(1);
0N/A } else {
0N/A System.out.println("All tests pass");
0N/A }
0N/A }
0N/A }
0N/A