Path: blob/master/test/jdk/java/lang/RuntimeTests/exec/WinCommand.java
41153 views
/*1* Copyright (c) 2004, 2019, 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/* @test24* @bug 500652025* @summary Check many different ways to run Windows programs26* @author Martin Buchholz27* @key randomness28*/2930import java.io.*;31import java.util.*;32import static java.lang.System.*;3334class StreamDrainer extends Thread {35private final InputStream is;36private final ByteArrayOutputStream os = new ByteArrayOutputStream();37public StreamDrainer(InputStream is) { this.is = is; }38public void run() {39try {40int i;41while ((i = is.read()) >= 0)42os.write(i);43} catch (Exception e) {}44}45public String toString() { return os.toString(); }46}4748class CommandRunner {49private static Random generator = new Random();50public final int exitValue;51public final String out;52public final String err;53CommandRunner(String... args) throws Exception {54Process p = (generator.nextInt(2) == 0)55? new ProcessBuilder(args).start()56: Runtime.getRuntime().exec(args);57StreamDrainer d1 = new StreamDrainer(p.getInputStream());58StreamDrainer d2 = new StreamDrainer(p.getErrorStream());59d1.start();60d2.start();61p.waitFor();62d1.join();63d2.join();64this.exitValue = p.exitValue();65this.out = d1.toString();66this.err = d2.toString();67}68}6970public class WinCommand {71private static int failed = 0;7273private static void fail(String msg) {74err.printf("FAIL: %s%n", msg);75failed++;76}7778private static String outputOf(String... args) {79try {80CommandRunner cr = new CommandRunner(args);81if (cr.exitValue != 0)82fail("exitValue != 0");83if (! cr.err.equals(""))84fail("stderr: " + cr.err);85return cr.out.replaceFirst("[\r\n]+$", "");86} catch (Exception e) {87fail(e.toString());88return "";89}90}9192private static void checkCD(String... filespecs) {93String firstCD = null;94for (String filespec : filespecs) {95String CD = outputOf(filespec, "/C", "CD");96out.printf("%s CD ==> %s%n", filespec, CD);97if (firstCD == null) {98firstCD = CD;99checkDir(CD);100}101if (! CD.equals(firstCD)) {102fail("Inconsistent result from CD subcommand");103checkDir(CD);104}105}106}107108private static void checkDir(String dirname) {109if (! new File(dirname).isDirectory())110fail(String.format("Not a directory: %s%n", dirname));111}112113private static void writeFile(String filename, String contents) {114try {115FileOutputStream fos = new FileOutputStream(filename);116fos.write(contents.getBytes());117fos.close();118} catch (Exception e) {119fail("Unexpected exception" + e.toString());120}121}122123public static void main(String[] args) throws Exception {124File systemRoot =125getenv("SystemRoot") != null ? new File(getenv("SystemRoot")) :126getenv("WINDIR") != null ? new File(getenv ("WINDIR")) :127null;128if (systemRoot == null || ! systemRoot.isDirectory())129return; // Not Windows as we know it130131String systemDirW = new File(systemRoot, "System32").getPath();132String systemDirM = systemDirW.replace('\\', '/');133out.printf("systemDirW=%s%n", systemDirW);134out.printf("systemDirM=%s%n", systemDirM);135136// Win9x systems don't have a cmd.exe137if (new File(systemDirW, "cmd.exe").exists()) {138try {139out.println("Running cmd.exe tests...");140writeFile("cdcmd.cmd", "@echo off\r\nCD\r\n");141writeFile("cdbat.bat", "@echo off\r\nCD\r\n");142checkCD("cmd",143"cmd.exe",144systemDirW + "\\cmd.exe",145// Only the ".exe" extension can be omitted146systemDirW + "\\cmd",147systemDirM + "/cmd.exe",148systemDirM + "/cmd",149"/" + systemDirM + "/cmd",150"cdcmd.cmd", "./cdcmd.cmd", ".\\cdcmd.cmd",151"cdbat.bat", "./cdbat.bat", ".\\cdbat.bat");152} finally {153new File("cdcmd.cmd").delete();154new File("cdbat.bat").delete();155}156}157158// 16-bit apps like command.com must have a console;159// fix this someday...160161// // Win64 systems don't have a command.com162// if (new File(systemDirW, "command.com").exists()163// // no output if running without a console;164// // fix this in Mustang165// && ! outputOf("command.com", "/C", "CD").equals("")) {166// out.println("Running command.com tests...");167// checkCD("command.com",168// systemDirM + "/command.com",169// systemDirW + "\\command.com");170// }171172// Win9x systems have a %SYSTEMDRIVE%\command.com173// if (new File("C:\\COMMAND.COM").exists()174// && ! outputOf("COMMAND.COM", "/C", "CD").equals("")) {175// out.println("Running COMMAND.COM tests...");176// checkCD("C:/command.com",177// "C:\\command.com");178// }179180if (failed > 0)181throw new Exception(failed + " tests failed");182}183}184185186