Fix ShellCommandPermissionTest

The `ShellCommandPermission` tests previously checked `stderr` for a
line containing the string `Error:` to determine if the command failed
to run or not. Due to recent changes no output is generated when
checking whether the command can run, instead the process returns an
exit code of -1. This updates the permissions tests to check that the
process exits with an error code instead of relying on parsing the
output of stderr.

Bug: 307955579
Test: atest CtsPermissionTestCases:android.permission.cts.ShellCommandPermissionTest
(cherry picked from https://googleplex-android-review.googlesource.com/q/commit:1df4da190091d1ecfd58448a83cf5a4eb29a1c03)
Merged-In: I2a5b1d4ac19d8def11a7775f56c23d8befb433b7
Change-Id: I7107db9e7241967beca05956c18f15710b6b8c6e
diff --git a/tests/tests/permission/src/android/permission/cts/ShellCommandPermissionTest.java b/tests/tests/permission/src/android/permission/cts/ShellCommandPermissionTest.java
index e70f07b..54562d5 100644
--- a/tests/tests/permission/src/android/permission/cts/ShellCommandPermissionTest.java
+++ b/tests/tests/permission/src/android/permission/cts/ShellCommandPermissionTest.java
@@ -16,7 +16,8 @@
 
 package android.permission.cts;
 
-import static org.junit.Assert.assertTrue;
+import static com.google.common.truth.Truth.assertThat;
+
 import static org.junit.Assert.fail;
 
 import androidx.test.runner.AndroidJUnit4;
@@ -24,36 +25,23 @@
 import org.junit.Test;
 import org.junit.runner.RunWith;
 
-import java.io.BufferedReader;
-import java.io.InputStreamReader;
-
 /**
  * Test shell command capability enforcement
  */
 @RunWith(AndroidJUnit4.class)
 public class ShellCommandPermissionTest {
 
-    private void executeShellCommandAndRequireError(String command, String required)
+    static final int EXPECTED_ERROR_CODE = 255;
+
+    /**
+     * Runs the given command, waits for it to exit, and verifies the return
+     * code indicates failure.
+     */
+    private void executeShellCommandAndWaitForError(String command)
             throws Exception {
         try {
             java.lang.Process proc = Runtime.getRuntime().exec(command);
-
-            // read output of command
-            BufferedReader reader =
-                    new BufferedReader(new InputStreamReader(proc.getErrorStream()));
-            StringBuilder stderr = new StringBuilder();
-            String line = reader.readLine();
-            while (line != null) {
-                stderr.append(line);
-                line = reader.readLine();
-            }
-            reader.close();
-            proc.waitFor();
-
-            String stderrString = stderr.toString();
-            assertTrue("Expected command stderr '" + stderrString
-                            + " to contain '" + required + "'",
-                    stderrString.contains(required));
+            assertThat(proc.waitFor()).isEqualTo(EXPECTED_ERROR_CODE);
         } catch (InterruptedException e) {
             fail("Unsuccessful shell command");
         }
@@ -61,15 +49,13 @@
 
     @Test
     public void testTraceIpc() throws Exception {
-        executeShellCommandAndRequireError(
-                "cmd activity trace-ipc stop --dump-file /data/system/last-fstrim",
-                "Error:");
+        executeShellCommandAndWaitForError(
+                "cmd activity trace-ipc stop --dump-file /data/system/last-fstrim");
     }
 
     @Test
     public void testDumpheap() throws Exception {
-        executeShellCommandAndRequireError(
-                "cmd activity dumpheap system_server /data/system/last-fstrim",
-                "Error:");
+        executeShellCommandAndWaitForError(
+                "cmd activity dumpheap system_server /data/system/last-fstrim");
     }
 }