Skip to content

Commit dc8bb7c

Browse files
authored
Merge pull request wildfly#17258 from jamezp/WFLY-18602
[WFLY-18602] Add security manager testing support for script tests
2 parents 9df1a9b + 4619bc6 commit dc8bb7c

File tree

5 files changed

+92
-34
lines changed

5 files changed

+92
-34
lines changed

ee-feature-pack/galleon-shared/src/main/resources/content/bin/appclient.bat

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@ if "x%JBOSS_MODULEPATH%" == "x" (
116116
set "JBOSS_MODULEPATH=%JBOSS_HOME%\modules"
117117
)
118118

119+
setlocal EnableDelayedExpansion
120+
call "!DIRNAME!common.bat" :setSecurityManagerDefault
121+
set "JAVA_OPTS=!JAVA_OPTS! !SECURITY_MANAGER_CONFIG_OPTION!"
122+
setlocal DisableDelayedExpansion
123+
119124
rem Set the module options
120125
set "MODULE_OPTS="
121126
if "%SECMGR%" == "true" (

ee-feature-pack/galleon-shared/src/main/resources/content/bin/appclient.sh

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,18 @@ if $cygwin; then
142142
JBOSS_MODULEPATH=`cygpath --path --windows "$JBOSS_MODULEPATH"`
143143
fi
144144

145+
# Set default Security Manager configuration value
146+
setSecurityManagerDefault
147+
JAVA_OPTS="$JAVA_OPTS $SECURITY_MANAGER_CONFIG_OPTION"
148+
145149
# Process the JAVA_OPTS failing if the java.security.manager is set.
146150
SECURITY_MANAGER_SET=`echo $JAVA_OPTS | $GREP "java\.security\.manager"`
147151
if [ "x$SECURITY_MANAGER_SET" != "x" ]; then
148-
echo "ERROR: The use of -Djava.security.manager has been removed. Please use the -secmgr command line argument or SECMGR=true environment variable."
149-
exit 1
152+
SECURITY_MANAGER_SET_TO_ALLOW=`echo $JAVA_OPTS | $GREP "java\.security\.manager=allow"`
153+
if [ "x$SECURITY_MANAGER_SET_TO_ALLOW" = "x" ]; then
154+
echo "ERROR: The use of -Djava.security.manager has been removed. Please use the -secmgr command line argument or SECMGR=true environment variable."
155+
exit 1
156+
fi
150157
fi
151158

152159
# Set up the module arguments

testsuite/scripts/src/test/java/org/wildfly/test/scripts/AppClientScriptTestCase.java

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,29 @@ public AppClientScriptTestCase() {
2222

2323
@Override
2424
void testScript(final ScriptProcess script) throws InterruptedException, TimeoutException, IOException {
25+
// First check the standard script
2526
script.start(MAVEN_JAVA_OPTS, "-v");
26-
Assert.assertNotNull("The process is null and may have failed to start.", script);
27+
testScript(script, 0);
2728

28-
validateProcess(script);
29+
// Test with the security manager enabled
30+
script.start(MAVEN_JAVA_OPTS, "-v", "-secmgr");
31+
testScript(script, jvmVersion() >= 17 ? 4 : 0);
32+
}
33+
34+
private void testScript(final ScriptProcess script, final int additionalLines) throws InterruptedException, IOException {
35+
try (script) {
36+
validateProcess(script);
2937

30-
final List<String> lines = script.getStdout();
31-
int count = 2;
32-
for (String stdout : lines) {
33-
if (stdout.startsWith("Picked up")) {
34-
count += 1;
38+
final List<String> lines = script.getStdout();
39+
int count = 2 + additionalLines;
40+
for (String stdout : lines) {
41+
if (stdout.startsWith("Picked up")) {
42+
count += 1;
43+
}
3544
}
45+
final int expectedLines = (script.getShell() == Shell.BATCH ? 3 + additionalLines : count);
46+
Assert.assertEquals(script.getErrorMessage(String.format("Expected %d lines.", expectedLines)), expectedLines,
47+
lines.size());
3648
}
37-
final int expectedLines = (script.getShell() == Shell.BATCH ? 3 : count );
38-
Assert.assertEquals(script.getErrorMessage(String.format("Expected %d lines.", expectedLines)), expectedLines,
39-
lines.size());
4049
}
4150
}

testsuite/scripts/src/test/java/org/wildfly/test/scripts/ScriptProcess.java

Lines changed: 50 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
package org.wildfly.test.scripts;
77

88
import java.io.IOException;
9-
import java.io.InputStream;
10-
import java.io.OutputStream;
119
import java.io.UncheckedIOException;
1210
import java.nio.charset.StandardCharsets;
1311
import java.nio.file.Files;
@@ -20,14 +18,17 @@
2018
import java.util.Iterator;
2119
import java.util.List;
2220
import java.util.Map;
21+
import java.util.Optional;
2322
import java.util.concurrent.Callable;
23+
import java.util.concurrent.CompletableFuture;
2424
import java.util.concurrent.ExecutionException;
2525
import java.util.concurrent.ExecutorService;
2626
import java.util.concurrent.Executors;
2727
import java.util.concurrent.Future;
2828
import java.util.concurrent.TimeUnit;
2929
import java.util.concurrent.TimeoutException;
3030
import java.util.function.Function;
31+
import java.util.stream.Stream;
3132

3233
import org.jboss.as.controller.client.ModelControllerClient;
3334
import org.jboss.as.test.shared.TestSuiteEnvironment;
@@ -38,7 +39,7 @@
3839
*
3940
* @author <a href="mailto:jperkins@redhat.com">James R. Perkins</a>
4041
*/
41-
public class ScriptProcess extends Process implements AutoCloseable {
42+
public class ScriptProcess implements AutoCloseable, ProcessHandle {
4243
private static final Logger LOGGER = Logger.getLogger(ScriptProcess.class);
4344

4445
private static final Path PROC_DIR;
@@ -59,6 +60,7 @@ public class ScriptProcess extends Process implements AutoCloseable {
5960
private final long timeout;
6061
private final Collection<String> prefixCmds;
6162
private Process delegate;
63+
private ProcessHandle handleDelegate;
6264
private Path stdoutLog;
6365
private String lastExecutedCmd;
6466

@@ -107,6 +109,7 @@ void start(final Function<ModelControllerClient, Boolean> check, final Map<Strin
107109
waitFor(process, check);
108110
}
109111
this.delegate = process;
112+
this.handleDelegate = process.toHandle();
110113
}
111114

112115
@SuppressWarnings("unused")
@@ -170,64 +173,89 @@ public void close() {
170173
}
171174

172175
@Override
173-
public OutputStream getOutputStream() {
176+
public long pid() {
174177
checkStatus();
175-
return delegate.getOutputStream();
178+
return handleDelegate.pid();
176179
}
177180

178181
@Override
179-
public InputStream getInputStream() {
182+
public Optional<ProcessHandle> parent() {
180183
checkStatus();
181-
return delegate.getInputStream();
184+
return handleDelegate.parent();
182185
}
183186

184187
@Override
185-
public InputStream getErrorStream() {
188+
public Stream<ProcessHandle> children() {
186189
checkStatus();
187-
return delegate.getErrorStream();
190+
return handleDelegate.children();
188191
}
189192

190193
@Override
191-
public int waitFor() throws InterruptedException {
194+
public Stream<ProcessHandle> descendants() {
192195
checkStatus();
193-
return delegate.waitFor();
196+
return handleDelegate.children();
194197
}
195198

196199
@Override
197-
public boolean waitFor(final long timeout, final TimeUnit unit) throws InterruptedException {
200+
public Info info() {
198201
checkStatus();
199-
return delegate.waitFor(timeout, unit);
202+
return handleDelegate.info();
200203
}
201204

202205
@Override
203-
public int exitValue() {
206+
public CompletableFuture<ProcessHandle> onExit() {
204207
checkStatus();
205-
return delegate.exitValue();
208+
return handleDelegate.onExit();
206209
}
207210

208211
@Override
209-
public void destroy() {
212+
public boolean supportsNormalTermination() {
210213
checkStatus();
211-
delegate.destroy();
214+
return handleDelegate.supportsNormalTermination();
215+
}
216+
217+
@Override
218+
public boolean destroy() {
219+
if (handleDelegate != null) {
220+
return handleDelegate.destroy();
221+
}
222+
return false;
223+
}
224+
225+
@Override
226+
public boolean destroyForcibly() {
227+
if (handleDelegate != null) {
228+
return handleDelegate.destroyForcibly();
229+
}
230+
return false;
212231
}
213232

214233
@Override
215-
public Process destroyForcibly() {
234+
public int compareTo(final ProcessHandle other) {
216235
checkStatus();
217-
return delegate.destroyForcibly();
236+
return handleDelegate.compareTo(other);
218237
}
219238

220239
@Override
221240
public boolean isAlive() {
222-
checkStatus();
223-
return delegate.isAlive();
241+
return delegate != null && delegate.isAlive();
224242
}
225243

226244
@Override
227245
public String toString() {
228246
return getCommandString(Collections.emptyList());
229247
}
230248

249+
boolean waitFor(final long timeout, final TimeUnit unit) throws InterruptedException {
250+
checkStatus();
251+
return delegate.waitFor(timeout, unit);
252+
}
253+
254+
int exitValue() {
255+
checkStatus();
256+
return delegate.exitValue();
257+
}
258+
231259
private String getCommandString(final Collection<String> arguments) {
232260
final List<String> cmd = getCommand(arguments);
233261
final StringBuilder result = new StringBuilder();
@@ -242,7 +270,7 @@ private String getCommandString(final Collection<String> arguments) {
242270
}
243271

244272
private void checkStatus() {
245-
if (delegate == null) {
273+
if (delegate == null || handleDelegate == null) {
246274
throw new IllegalStateException("The script has not yet been started.");
247275
}
248276
}

testsuite/scripts/src/test/java/org/wildfly/test/scripts/ScriptTestCase.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,15 @@ void validateProcess(final ScriptProcess script) throws InterruptedException {
132132
}
133133
}
134134

135+
/**
136+
* Returns the currently running JVM version.
137+
*
138+
* @return the JVM version
139+
*/
140+
int jvmVersion() {
141+
return Integer.parseInt(System.getProperty("java.vm.specification.version"));
142+
}
143+
135144
private void executeTests(final Shell shell) throws InterruptedException, IOException, TimeoutException {
136145
for (Path path : ServerConfigurator.PATHS) {
137146
try (ScriptProcess script = new ScriptProcess(path, scriptBaseName, shell, ServerHelper.TIMEOUT)) {

0 commit comments

Comments
 (0)