Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/lang/RuntimeTests/exec/ExecCommand.java
41153 views
1
/*
2
* Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
25
/**
26
* @test
27
* @bug 8012453 8016046
28
* @run main/othervm -Djava.security.manager=allow ExecCommand
29
* @summary workaround for legacy applications with Runtime.getRuntime().exec(String command)
30
*/
31
32
import java.io.BufferedWriter;
33
import java.io.File;
34
import java.io.FileNotFoundException;
35
import java.io.FileWriter;
36
import java.io.IOException;
37
import java.nio.file.FileSystems;
38
import java.nio.file.Files;
39
import java.security.AccessControlException;
40
41
public class ExecCommand {
42
static class SecurityMan extends SecurityManager {
43
public static String unquote(String str)
44
{
45
int length = (str == null)
46
? 0
47
: str.length();
48
49
if (length > 1
50
&& str.charAt(0) == '\"'
51
&& str.charAt(length - 1) == '\"')
52
{
53
return str.substring(1, length - 1);
54
}
55
return str;
56
}
57
58
@Override public void checkExec(String cmd) {
59
String ncmd = (new File(unquote(cmd))).getPath();
60
if ( ncmd.equals(".\\Program")
61
|| ncmd.equals("\".\\Program")
62
|| ncmd.equals(".\\Program Files\\do.cmd")
63
|| ncmd.equals(".\\Program.cmd")
64
|| ncmd.equals("cmd"))
65
{
66
return;
67
}
68
super.checkExec(cmd);
69
}
70
71
@Override public void checkDelete(String file) {}
72
@Override public void checkRead(String file) {}
73
}
74
75
// Parameters for the Runtime.exec calls
76
private static final String TEST_RTE_ARG[] = {
77
"cmd /C dir > dirOut.txt",
78
"cmd /C dir > \".\\Program Files\\dirOut.txt\"",
79
".\\Program Files\\do.cmd",
80
"\".\\Program Files\\doNot.cmd\" arg",
81
"\".\\Program Files\\do.cmd\" arg",
82
// compatibility
83
"\".\\Program.cmd\" arg",
84
".\\Program.cmd arg",
85
};
86
87
private static final String doCmdCopy[] = {
88
".\\Program.cmd",
89
".\\Program Files\\doNot.cmd",
90
".\\Program Files\\do.cmd",
91
};
92
93
// Golden image for results
94
private static final String TEST_RTE_GI[][] = {
95
//Def Legacy mode, Enforced mode, Set Legacy mode, Set Legacy mode & SM
96
// [cmd /C dir > dirOut.txt]
97
new String[]{"Success",
98
"IOException", // [cmd /C dir ">" dirOut.txt] no redirection
99
"Success",
100
"IOException"}, //SM - no legacy mode, bad command
101
102
// [cmd /C dir > ".\Program Files\dirOut.txt"]
103
new String[]{"Success",
104
"IOException", // [cmd /C dir ">" ".\Program Files\dirOut.txt"] no redirection
105
"Success",
106
"IOException"}, //SM - no legacy mode, bad command
107
108
// [.\Program File\do.cmd]
109
new String[]{"Success",
110
"IOException", // [.\Program] not found
111
"Success",
112
"IOException"}, //SM - no legacy mode [.\Program] - OK
113
114
// [".\Program File\doNot.cmd" arg]
115
new String[]{"Success",
116
"Success",
117
"Success",
118
"AccessControlException"}, //SM - [".\Program] - OK,
119
// [.\\Program Files\\doNot.cmd] - Fail
120
121
// [".\Program File\do.cmd" arg]
122
// AccessControlException
123
new String[]{"Success",
124
"Success",
125
"Success",
126
"Success"}, //SM - [".\Program] - OK,
127
// [.\\Program Files\\do.cmd] - OK
128
129
// compatibility
130
new String[]{"Success", "Success", "Success", "Success"}, //[".\Program.cmd"]
131
new String[]{"Success", "Success", "Success", "Success"} //[.\Program.cmd]
132
};
133
134
private static void deleteOut(String path) {
135
try {
136
Files.delete(FileSystems.getDefault().getPath(path));
137
} catch (IOException ex) {
138
//that is OK
139
}
140
}
141
private static void checkOut(String path) throws FileNotFoundException {
142
if (Files.notExists(FileSystems.getDefault().getPath(path)))
143
throw new FileNotFoundException(path);
144
}
145
146
public static void main(String[] _args) throws Exception {
147
if (!System.getProperty("os.name").startsWith("Windows")) {
148
return;
149
}
150
151
// tear up
152
try {
153
new File(".\\Program Files").mkdirs();
154
for (int i = 0; i < doCmdCopy.length; ++i) {
155
try (BufferedWriter outCmd = new BufferedWriter(
156
new FileWriter(doCmdCopy[i]))) {
157
outCmd.write("@echo %1");
158
}
159
}
160
} catch (IOException e) {
161
throw new Error(e.getMessage());
162
}
163
164
// action
165
for (int k = 0; k < 4; ++k) {
166
switch (k) {
167
case 0:
168
// the "jdk.lang.Process.allowAmbiguousCommands" is undefined
169
// "true" by default with the legacy verification procedure
170
break;
171
case 1:
172
System.setProperty("jdk.lang.Process.allowAmbiguousCommands", "false");
173
break;
174
case 2:
175
System.setProperty("jdk.lang.Process.allowAmbiguousCommands", "");
176
break;
177
case 3:
178
System.setSecurityManager( new SecurityMan() );
179
break;
180
}
181
for (int i = 0; i < TEST_RTE_ARG.length; ++i) {
182
String outRes;
183
try {
184
// tear up
185
switch (i) {
186
case 0:
187
// [cmd /C dir > dirOut.txt]
188
deleteOut(".\\dirOut.txt");
189
break;
190
case 1:
191
// [cmd /C dir > ".\Program Files\dirOut.txt"]
192
deleteOut(".\\Program Files\\dirOut.txt");
193
break;
194
}
195
196
Process exec = Runtime.getRuntime().exec(TEST_RTE_ARG[i]);
197
exec.waitFor();
198
199
//exteded check
200
switch (i) {
201
case 0:
202
// [cmd /C dir > dirOut.txt]
203
checkOut(".\\dirOut.txt");
204
break;
205
case 1:
206
// [cmd /C dir > ".\Program Files\dirOut.txt"]
207
checkOut(".\\Program Files\\dirOut.txt");
208
break;
209
}
210
outRes = "Success";
211
} catch (IOException ioe) {
212
outRes = "IOException: " + ioe.getMessage();
213
} catch (IllegalArgumentException iae) {
214
outRes = "IllegalArgumentException: " + iae.getMessage();
215
} catch (AccessControlException se) {
216
outRes = "AccessControlException: " + se.getMessage();
217
}
218
219
if (!outRes.startsWith(TEST_RTE_GI[i][k])) {
220
throw new Error("Unexpected output! Step" + k + ":" + i
221
+ "\nArgument: " + TEST_RTE_ARG[i]
222
+ "\nExpected: " + TEST_RTE_GI[i][k]
223
+ "\n Output: " + outRes);
224
} else {
225
System.out.println("RTE OK:" + TEST_RTE_ARG[i]);
226
}
227
}
228
}
229
}
230
}
231
232