Fix race in AuthFsHostTest.

This became solidly reproducible for me, which confused me for a bit
since it seemed unrelated to my change. The fix is slightly inelegant
but works nicely.

Hopefully this will make it a little more reliable.

I also introduced a constant for CID 2, again because it puzzled me.

Bug: 200924405
Test: atest AuthFsHostTest
Change-Id: I3183eb6ba26b00b40a94c16557b80ecc996694ac
diff --git a/authfs/tests/java/src/com/android/fs/AuthFsHostTest.java b/authfs/tests/java/src/com/android/fs/AuthFsHostTest.java
index 2c13ecb..3d97ee7 100644
--- a/authfs/tests/java/src/com/android/fs/AuthFsHostTest.java
+++ b/authfs/tests/java/src/com/android/fs/AuthFsHostTest.java
@@ -44,6 +44,7 @@
 
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
+import java.util.concurrent.atomic.AtomicBoolean;
 
 @RootPermissionTest
 @RunWith(DeviceJUnit4ClassRunner.class)
@@ -67,6 +68,8 @@
     /** FUSE's magic from statfs(2) */
     private static final String FUSE_SUPER_MAGIC_HEX = "65735546";
 
+    private static final int VMADDR_CID_HOST = 2;
+
     private static CommandRunner sAndroid;
     private static String sCid;
     private static boolean sAssumptionFailed;
@@ -155,7 +158,8 @@
                 "--ro-fds 3:4:5 --ro-fds 6");
 
         runAuthFsOnMicrodroid(
-                "--remote-ro-file-unverified 10:6 --remote-ro-file 11:3:cert.der --cid 2");
+                "--remote-ro-file-unverified 10:6 --remote-ro-file 11:3:cert.der --cid "
+                        + VMADDR_CID_HOST);
 
         // Action
         String actualHashUnverified4m = computeFileHashOnMicrodroid(MOUNT_DIR + "/10");
@@ -179,7 +183,8 @@
                         + " 6<input.4k1 7<input.4k1.merkle_dump 8<input.4k1.fsv_sig",
                 "--ro-fds 3:4:5 --ro-fds 6:7:8");
         runAuthFsOnMicrodroid(
-                "--remote-ro-file 10:3:cert.der --remote-ro-file 11:6:cert.der --cid 2");
+                "--remote-ro-file 10:3:cert.der --remote-ro-file 11:6:cert.der --cid "
+                        + VMADDR_CID_HOST);
 
         // Action
         String actualHash4k = computeFileHashOnMicrodroid(MOUNT_DIR + "/10");
@@ -199,7 +204,7 @@
         // Setup
         runFdServerOnAndroid(
                 "3<input.4m 4<input.4m.merkle_dump.bad 5<input.4m.fsv_sig", "--ro-fds 3:4:5");
-        runAuthFsOnMicrodroid("--remote-ro-file 10:3:cert.der --cid 2");
+        runAuthFsOnMicrodroid("--remote-ro-file 10:3:cert.der --cid " + VMADDR_CID_HOST);
 
         // Verify
         assertFalse(copyFileOnMicrodroid(MOUNT_DIR + "/10", "/dev/null"));
@@ -210,7 +215,7 @@
             throws DeviceNotAvailableException, InterruptedException {
         // Setup
         runFdServerOnAndroid("3<>output", "--rw-fds 3");
-        runAuthFsOnMicrodroid("--remote-new-rw-file 20:3 --cid 2");
+        runAuthFsOnMicrodroid("--remote-new-rw-file 20:3 --cid " + VMADDR_CID_HOST);
 
         // Action
         String srcPath = "/system/bin/linker64";
@@ -228,7 +233,7 @@
             throws DeviceNotAvailableException, InterruptedException {
         // Setup
         runFdServerOnAndroid("3<>output", "--rw-fds 3");
-        runAuthFsOnMicrodroid("--remote-new-rw-file 20:3 --cid 2");
+        runAuthFsOnMicrodroid("--remote-new-rw-file 20:3 --cid " + VMADDR_CID_HOST);
 
         String srcPath = "/system/bin/linker64";
         String destPath = MOUNT_DIR + "/20";
@@ -259,7 +264,7 @@
     public void testFileResize() throws DeviceNotAvailableException, InterruptedException {
         // Setup
         runFdServerOnAndroid("3<>output", "--rw-fds 3");
-        runAuthFsOnMicrodroid("--remote-new-rw-file 20:3 --cid 2");
+        runAuthFsOnMicrodroid("--remote-new-rw-file 20:3 --cid " + VMADDR_CID_HOST);
         String outputPath = MOUNT_DIR + "/20";
         String backendPath = TEST_DIR + "/output";
 
@@ -338,11 +343,16 @@
     private void runAuthFsOnMicrodroid(String flags) {
         String cmd = AUTHFS_BIN + " " + MOUNT_DIR + " " + flags;
 
+        AtomicBoolean starting = new AtomicBoolean(true);
         mThreadPool.submit(
                 () -> {
-                    CLog.i("Starting authfs");
-                    CommandResult result = runOnMicrodroidForResult(cmd);
-                    CLog.w("authfs has stopped: " + result);
+                    // authfs may fail to start if fd_server is not yet listening on the vsock
+                    // ("Error: Invalid raw AIBinder"). Just restart if that happens.
+                    while (starting.get()) {
+                        CLog.i("Starting authfs");
+                        CommandResult result = runOnMicrodroidForResult(cmd);
+                        CLog.w("authfs has stopped: " + result);
+                    }
                 });
         try {
             PollingCheck.waitFor(
@@ -352,6 +362,8 @@
             // methods. waitFor throws Exception because the callback, Callable#call(), has a
             // signature to throw an Exception.
             throw new RuntimeException(e);
+        } finally {
+            starting.set(false);
         }
     }