Path: blob/master/test/jdk/java/lang/RuntimeTests/exec/ExecCommand.java
41153 views
/*1* Copyright (c) 2013, 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*/222324/**25* @test26* @bug 8012453 801604627* @run main/othervm -Djava.security.manager=allow ExecCommand28* @summary workaround for legacy applications with Runtime.getRuntime().exec(String command)29*/3031import java.io.BufferedWriter;32import java.io.File;33import java.io.FileNotFoundException;34import java.io.FileWriter;35import java.io.IOException;36import java.nio.file.FileSystems;37import java.nio.file.Files;38import java.security.AccessControlException;3940public class ExecCommand {41static class SecurityMan extends SecurityManager {42public static String unquote(String str)43{44int length = (str == null)45? 046: str.length();4748if (length > 149&& str.charAt(0) == '\"'50&& str.charAt(length - 1) == '\"')51{52return str.substring(1, length - 1);53}54return str;55}5657@Override public void checkExec(String cmd) {58String ncmd = (new File(unquote(cmd))).getPath();59if ( ncmd.equals(".\\Program")60|| ncmd.equals("\".\\Program")61|| ncmd.equals(".\\Program Files\\do.cmd")62|| ncmd.equals(".\\Program.cmd")63|| ncmd.equals("cmd"))64{65return;66}67super.checkExec(cmd);68}6970@Override public void checkDelete(String file) {}71@Override public void checkRead(String file) {}72}7374// Parameters for the Runtime.exec calls75private static final String TEST_RTE_ARG[] = {76"cmd /C dir > dirOut.txt",77"cmd /C dir > \".\\Program Files\\dirOut.txt\"",78".\\Program Files\\do.cmd",79"\".\\Program Files\\doNot.cmd\" arg",80"\".\\Program Files\\do.cmd\" arg",81// compatibility82"\".\\Program.cmd\" arg",83".\\Program.cmd arg",84};8586private static final String doCmdCopy[] = {87".\\Program.cmd",88".\\Program Files\\doNot.cmd",89".\\Program Files\\do.cmd",90};9192// Golden image for results93private static final String TEST_RTE_GI[][] = {94//Def Legacy mode, Enforced mode, Set Legacy mode, Set Legacy mode & SM95// [cmd /C dir > dirOut.txt]96new String[]{"Success",97"IOException", // [cmd /C dir ">" dirOut.txt] no redirection98"Success",99"IOException"}, //SM - no legacy mode, bad command100101// [cmd /C dir > ".\Program Files\dirOut.txt"]102new String[]{"Success",103"IOException", // [cmd /C dir ">" ".\Program Files\dirOut.txt"] no redirection104"Success",105"IOException"}, //SM - no legacy mode, bad command106107// [.\Program File\do.cmd]108new String[]{"Success",109"IOException", // [.\Program] not found110"Success",111"IOException"}, //SM - no legacy mode [.\Program] - OK112113// [".\Program File\doNot.cmd" arg]114new String[]{"Success",115"Success",116"Success",117"AccessControlException"}, //SM - [".\Program] - OK,118// [.\\Program Files\\doNot.cmd] - Fail119120// [".\Program File\do.cmd" arg]121// AccessControlException122new String[]{"Success",123"Success",124"Success",125"Success"}, //SM - [".\Program] - OK,126// [.\\Program Files\\do.cmd] - OK127128// compatibility129new String[]{"Success", "Success", "Success", "Success"}, //[".\Program.cmd"]130new String[]{"Success", "Success", "Success", "Success"} //[.\Program.cmd]131};132133private static void deleteOut(String path) {134try {135Files.delete(FileSystems.getDefault().getPath(path));136} catch (IOException ex) {137//that is OK138}139}140private static void checkOut(String path) throws FileNotFoundException {141if (Files.notExists(FileSystems.getDefault().getPath(path)))142throw new FileNotFoundException(path);143}144145public static void main(String[] _args) throws Exception {146if (!System.getProperty("os.name").startsWith("Windows")) {147return;148}149150// tear up151try {152new File(".\\Program Files").mkdirs();153for (int i = 0; i < doCmdCopy.length; ++i) {154try (BufferedWriter outCmd = new BufferedWriter(155new FileWriter(doCmdCopy[i]))) {156outCmd.write("@echo %1");157}158}159} catch (IOException e) {160throw new Error(e.getMessage());161}162163// action164for (int k = 0; k < 4; ++k) {165switch (k) {166case 0:167// the "jdk.lang.Process.allowAmbiguousCommands" is undefined168// "true" by default with the legacy verification procedure169break;170case 1:171System.setProperty("jdk.lang.Process.allowAmbiguousCommands", "false");172break;173case 2:174System.setProperty("jdk.lang.Process.allowAmbiguousCommands", "");175break;176case 3:177System.setSecurityManager( new SecurityMan() );178break;179}180for (int i = 0; i < TEST_RTE_ARG.length; ++i) {181String outRes;182try {183// tear up184switch (i) {185case 0:186// [cmd /C dir > dirOut.txt]187deleteOut(".\\dirOut.txt");188break;189case 1:190// [cmd /C dir > ".\Program Files\dirOut.txt"]191deleteOut(".\\Program Files\\dirOut.txt");192break;193}194195Process exec = Runtime.getRuntime().exec(TEST_RTE_ARG[i]);196exec.waitFor();197198//exteded check199switch (i) {200case 0:201// [cmd /C dir > dirOut.txt]202checkOut(".\\dirOut.txt");203break;204case 1:205// [cmd /C dir > ".\Program Files\dirOut.txt"]206checkOut(".\\Program Files\\dirOut.txt");207break;208}209outRes = "Success";210} catch (IOException ioe) {211outRes = "IOException: " + ioe.getMessage();212} catch (IllegalArgumentException iae) {213outRes = "IllegalArgumentException: " + iae.getMessage();214} catch (AccessControlException se) {215outRes = "AccessControlException: " + se.getMessage();216}217218if (!outRes.startsWith(TEST_RTE_GI[i][k])) {219throw new Error("Unexpected output! Step" + k + ":" + i220+ "\nArgument: " + TEST_RTE_ARG[i]221+ "\nExpected: " + TEST_RTE_GI[i][k]222+ "\n Output: " + outRes);223} else {224System.out.println("RTE OK:" + TEST_RTE_ARG[i]);225}226}227}228}229}230231232