|
16 | 16 |
|
17 | 17 | package android.permission.cts; |
18 | 18 |
|
19 | | -import static org.junit.Assert.assertTrue; |
| 19 | +import static com.google.common.truth.Truth.assertThat; |
| 20 | + |
20 | 21 | import static org.junit.Assert.fail; |
21 | 22 |
|
22 | 23 | import androidx.test.runner.AndroidJUnit4; |
23 | 24 |
|
24 | 25 | import org.junit.Test; |
25 | 26 | import org.junit.runner.RunWith; |
26 | 27 |
|
27 | | -import java.io.BufferedReader; |
28 | | -import java.io.InputStreamReader; |
29 | | - |
30 | 28 | /** |
31 | 29 | * Test shell command capability enforcement |
32 | 30 | */ |
33 | 31 | @RunWith(AndroidJUnit4.class) |
34 | 32 | public class ShellCommandPermissionTest { |
35 | 33 |
|
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) |
37 | 41 | throws Exception { |
38 | 42 | try { |
39 | 43 | 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); |
57 | 45 | } catch (InterruptedException e) { |
58 | 46 | fail("Unsuccessful shell command"); |
59 | 47 | } |
60 | 48 | } |
61 | 49 |
|
62 | 50 | @Test |
63 | 51 | 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"); |
67 | 54 | } |
68 | 55 |
|
69 | 56 | @Test |
70 | 57 | 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"); |
74 | 60 | } |
75 | 61 | } |
0 commit comments