// Test argument containing both a space and a trailing backslash // Depending on the mode the final backslash may act as an escape that may turn an added quote to a literal quote privatestaticfinal String SPACE_AND_BACKSLASH = "SPACE AND BACKSLASH\\"; privatestaticfinalchar DOUBLE_QUOTE = '"'; privatestaticfinalchar BACKSLASH = '\\';
for (CMD cmd : CASES) { // If System property jdk.lang.process.allowAmbiguousCommands matches the case, test it // If undefined, test them all if (AMBIGUOUS_PROP_BOOLEAN == null ||
AMBIGUOUS_PROP_BOOLEAN.booleanValue() == cmd.allowAmbiguous) { try {
testCommand(cmd);
success++;
} catch (Exception ex) {
ex.printStackTrace();
errors++;
}
} else { // skip unmatched cases
skipped++;
}
} if (skipped > 0) {
System.out.printf("%d cases skipped, they did not match the tests with jdk.lang.Process.allowAmbiguousCommands: %s%n",
skipped, AMBIGUOUS_PROP_BOOLEAN);
}
System.out.printf("\nSuccess: %d, errors: %d%n", success, errors); if (errors > 0) { thrownew RuntimeException("Errors: " + errors);
}
}
// allowAmbiguousCommands = false, without application supplied double-quotes. // The space in the argument requires it to be quoted, the final backslash // must not be allowed to turn the quote that is added into a literal // instead of closing the quote. new CMD(false,
ECHO_JAVA_ARGS,
Arrays.asList(SPACE_AND_BACKSLASH, "ARG_1"),
Arrays.asList(SPACE_AND_BACKSLASH, "ARG_1")), new CMD(false,
ECHO_CMD_ARGS,
Arrays.asList(SPACE_AND_BACKSLASH, "ARG_2"),
Arrays.asList(DOUBLE_QUOTE + SPACE_AND_BACKSLASH + DOUBLE_QUOTE, "ARG_2")), new CMD(false,
ECHO_VBS_ARGS,
Arrays.asList(SPACE_AND_BACKSLASH, "ARG_3"),
Arrays.asList(SPACE_AND_BACKSLASH + BACKSLASH, "ARG_3")),
// allowAmbiguousCommands = false, WITH application supplied double-quotes around the argument // The argument has surrounding quotes so does not need further quoting. // However, for exe commands, the final backslash must not be allowed to turn the quote // into a literal instead of closing the quote. new CMD(false,
ECHO_JAVA_ARGS,
Arrays.asList(DOUBLE_QUOTE + SPACE_AND_BACKSLASH + DOUBLE_QUOTE, "ARG_11"),
Arrays.asList(SPACE_AND_BACKSLASH, "ARG_11")), new CMD(false,
ECHO_CMD_ARGS,
Arrays.asList(DOUBLE_QUOTE + SPACE_AND_BACKSLASH + DOUBLE_QUOTE, "ARG_12"),
Arrays.asList(DOUBLE_QUOTE + SPACE_AND_BACKSLASH + DOUBLE_QUOTE, "ARG_12")), new CMD(false,
ECHO_VBS_ARGS,
Arrays.asList(DOUBLE_QUOTE + SPACE_AND_BACKSLASH + DOUBLE_QUOTE, "ARG_13"),
Arrays.asList(SPACE_AND_BACKSLASH + BACKSLASH, "ARG_13")),
// Legacy mode tests; allowAmbiguousCommands = true; no application supplied quotes // The space in the argument requires it to be quoted, the final backslash // must not be allowed to turn the quote that is added into a literal // instead of closing the quote. new CMD(true,
ECHO_JAVA_ARGS,
Arrays.asList(SPACE_AND_BACKSLASH, "ARG_21"),
Arrays.asList(SPACE_AND_BACKSLASH, "ARG_21")), new CMD(true,
ECHO_CMD_ARGS,
Arrays.asList(SPACE_AND_BACKSLASH, "ARG_22"),
Arrays.asList(DOUBLE_QUOTE + SPACE_AND_BACKSLASH + BACKSLASH + DOUBLE_QUOTE, "ARG_22")), new CMD(true,
ECHO_VBS_ARGS,
Arrays.asList(SPACE_AND_BACKSLASH, "ARG_23"),
Arrays.asList(SPACE_AND_BACKSLASH + BACKSLASH, "ARG_23")),
// allowAmbiguousCommands = true, WITH application supplied double-quotes around the argument // The argument has surrounding quotes so does not need further quoting. // The backslash before the final quote is ignored and is interpreted differently for each command. new CMD(true,
ECHO_JAVA_ARGS,
Arrays.asList(DOUBLE_QUOTE + SPACE_AND_BACKSLASH + DOUBLE_QUOTE, "ARG_31"),
Arrays.asList("SPACE AND BACKSLASH\" ARG_31")), new CMD(true,
ECHO_CMD_ARGS,
Arrays.asList(DOUBLE_QUOTE + SPACE_AND_BACKSLASH + DOUBLE_QUOTE, "ARG_32"),
Arrays.asList(DOUBLE_QUOTE + SPACE_AND_BACKSLASH + DOUBLE_QUOTE, "ARG_32")), new CMD(true,
ECHO_VBS_ARGS,
Arrays.asList(DOUBLE_QUOTE + SPACE_AND_BACKSLASH + DOUBLE_QUOTE, "ARG_33"),
Arrays.asList(SPACE_AND_BACKSLASH, "ARG_33"))
);
/** *CommonfunctiontoInvokeaprocesswiththecommandsandchecktheresult. * *@paramcmdaCMDtestcasewitharguments,allowAmbiguousCommandsmode,andexpectedoutput
*/ privatestaticvoid testCommand(CMD cmd) throws Exception {
System.setProperty(AMBIGUOUS_PROP_NAME, Boolean.toString(cmd.allowAmbiguous));
List<String> actual = null;
List<String> arguments = new ArrayList<>(cmd.command);
arguments.addAll(cmd.arguments); try { // Launch the process and wait for termination
ProcessBuilder pb = new ProcessBuilder(arguments);
Process process = pb.start(); try (InputStream is = process.getInputStream()) {
String str = readAllBytesAsString(is);
str = str.replace("\r", "");
actual = Arrays.asList(str.split("\n"));
} catch (IOException ioe) { thrownew RuntimeException(ioe.getMessage(), ioe);
} int exitCode = process.waitFor(); if (exitCode != 0) {
actual = new ArrayList(actual);
actual.add("Exit code: " + exitCode);
}
} catch (IOException ioe) {
actual = Arrays.asList(ioe.getMessage().replace(arguments.get(0), "CMD"));
} catch (Exception ex) {
actual = Arrays.asList(ex.getMessage()); // Use exception message as output
} if (!Objects.equals(actual, cmd.expected)) {
System.out.println("Invoking(" + cmd.allowAmbiguous + "): " + arguments); if (actual.size() != cmd.expected.size()) {
System.out.println("Args Length: actual: " + actual.size() + " expected: " + cmd.expected.size());
}
System.out.println("Actual: " + actual);
System.out.println("Expected: " + cmd.expected);
System.out.println(); thrownew RuntimeException("Unexpected output");
}
}
/** *PrivatemethodtoreadAllBytesasaString. *(InputStream.readAllBytesisnotsupportedbytheJDKuntil9) *@paramisanInputStream *@returnaStringwiththecontents *@throwsIOExceptionifanerroroccurs
*/ privatestatic String readAllBytesAsString(InputStream is) throws IOException { finalint BUF_SIZE = 8192; byte[] bytes = newbyte[BUF_SIZE]; int off = 0; int len; while ((len = is.read(bytes, off, bytes.length - off)) > 0) {
off += len; if (off >= bytes.length) { // no space in buffer, reallocate larger
bytes = Arrays.copyOf(bytes, bytes.length + BUF_SIZE);
}
} returnnew String(bytes, 0, off, Charset.defaultCharset());
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.