Skip to content

Commit d3ef97c

Browse files
Winni ChangGerrit Code Review
authored andcommitted
Merge "Fix ShellCommandPermissionTest" into android12-tests-dev
2 parents 24d774b + 53b7aea commit d3ef97c

1 file changed

Lines changed: 14 additions & 28 deletions

File tree

tests/tests/permission/src/android/permission/cts/ShellCommandPermissionTest.java

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,60 +16,46 @@
1616

1717
package android.permission.cts;
1818

19-
import static org.junit.Assert.assertTrue;
19+
import static com.google.common.truth.Truth.assertThat;
20+
2021
import static org.junit.Assert.fail;
2122

2223
import androidx.test.runner.AndroidJUnit4;
2324

2425
import org.junit.Test;
2526
import org.junit.runner.RunWith;
2627

27-
import java.io.BufferedReader;
28-
import java.io.InputStreamReader;
29-
3028
/**
3129
* Test shell command capability enforcement
3230
*/
3331
@RunWith(AndroidJUnit4.class)
3432
public class ShellCommandPermissionTest {
3533

36-
private void executeShellCommandAndRequireError(String command, String required)
34+
static final int EXPECTED_ERROR_CODE = 255;
35+
36+
/**
37+
* Runs the given command, waits for it to exit, and verifies the return
38+
* code indicates failure.
39+
*/
40+
private void executeShellCommandAndWaitForError(String command)
3741
throws Exception {
3842
try {
3943
java.lang.Process proc = Runtime.getRuntime().exec(command);
40-
41-
// read output of command
42-
BufferedReader reader =
43-
new BufferedReader(new InputStreamReader(proc.getErrorStream()));
44-
StringBuilder stderr = new StringBuilder();
45-
String line = reader.readLine();
46-
while (line != null) {
47-
stderr.append(line);
48-
line = reader.readLine();
49-
}
50-
reader.close();
51-
proc.waitFor();
52-
53-
String stderrString = stderr.toString();
54-
assertTrue("Expected command stderr '" + stderrString
55-
+ " to contain '" + required + "'",
56-
stderrString.contains(required));
44+
assertThat(proc.waitFor()).isEqualTo(EXPECTED_ERROR_CODE);
5745
} catch (InterruptedException e) {
5846
fail("Unsuccessful shell command");
5947
}
6048
}
6149

6250
@Test
6351
public void testTraceIpc() throws Exception {
64-
executeShellCommandAndRequireError(
65-
"cmd activity trace-ipc stop --dump-file /data/system/last-fstrim",
66-
"Error:");
52+
executeShellCommandAndWaitForError(
53+
"cmd activity trace-ipc stop --dump-file /data/system/last-fstrim");
6754
}
6855

6956
@Test
7057
public void testDumpheap() throws Exception {
71-
executeShellCommandAndRequireError(
72-
"cmd activity dumpheap system_server /data/system/last-fstrim",
73-
"Error:");
58+
executeShellCommandAndWaitForError(
59+
"cmd activity dumpheap system_server /data/system/last-fstrim");
7460
}
7561
}

0 commit comments

Comments
 (0)