Merge "Prefer rlib for executables in compos"
diff --git a/authfs/TEST_MAPPING b/authfs/TEST_MAPPING
index d0c0b09..cabd5df 100644
--- a/authfs/TEST_MAPPING
+++ b/authfs/TEST_MAPPING
@@ -3,5 +3,10 @@
     {
       "name": "authfs_device_test_src_lib"
     }
+  ],
+  "postsubmit": [
+    {
+      "name": "MicrodroidHostTestCases"
+    }
   ]
 }
diff --git a/microdroid/Android.bp b/microdroid/Android.bp
index 55075ca..1fc1b7a 100644
--- a/microdroid/Android.bp
+++ b/microdroid/Android.bp
@@ -49,6 +49,7 @@
         "microdroid_build_prop",
         "ueventd.rc",
         "libbinder",
+        "libbinder_ndk",
         "libstdc++",
         "logcat",
         "logd",
@@ -86,6 +87,11 @@
                 "plat_keystore2_key_contexts",
             ],
         },
+        lib64: {
+            deps: [
+                "zipfuse",
+            ],
+        },
     },
     linker_config_src: "linker.config.json",
     base_dir: "system",
diff --git a/microdroid/README.md b/microdroid/README.md
index 4edd65b..d57efa3 100644
--- a/microdroid/README.md
+++ b/microdroid/README.md
@@ -69,7 +69,7 @@
 $ adb shell 'cd /data/local/tmp/microdroid; /apex/com.android.virt/bin/mk_cdisk /apex/com.android.virt/etc/microdroid_cdisk_userdata.json userdata_composite.img'
 $ adb shell '/apex/com.android.virt/bin/crosvm create_qcow2 --backing_file=/data/local/tmp/microdroid/userdata_composite.img /data/local/tmp/microdroid/userdata_composite.qcow2'
 $ adb shell 'cd /data/local/tmp/microdroid; /apex/com.android.virt/bin/mk_payload /apex/com.android.virt/etc/microdroid_payload.json payload.img'
-$ adb shell 'chmod go+r /data/local/tmp/microdroid/*-header.img /data/local/tmp/microdroid/*-footer.img'
+$ adb shell 'chmod go+r /data/local/tmp/microdroid/*-header.img /data/local/tmp/microdroid/*-footer.img /data/local/tmp/microdroid/payload.img.*'
 $ adb push microdroid.json /data/local/tmp/microdroid/microdroid.json
 ```
 
diff --git a/tests/hostside/java/android/virt/test/MicrodroidTestCase.java b/tests/hostside/java/android/virt/test/MicrodroidTestCase.java
index 6dedb49..5958480 100644
--- a/tests/hostside/java/android/virt/test/MicrodroidTestCase.java
+++ b/tests/hostside/java/android/virt/test/MicrodroidTestCase.java
@@ -19,10 +19,12 @@
 import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.CoreMatchers.not;
 import static org.junit.Assert.assertThat;
+import static org.junit.Assume.assumeThat;
 
 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner;
 import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test;
 import com.android.tradefed.util.CommandResult;
+import com.android.tradefed.util.CommandStatus;
 import com.android.tradefed.util.FileUtil;
 import com.android.tradefed.util.RunUtil;
 
@@ -38,18 +40,21 @@
 import java.util.List;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
 
 @RunWith(DeviceJUnit4ClassRunner.class)
 public class MicrodroidTestCase extends BaseHostJUnit4Test {
     private static final String TEST_ROOT = "/data/local/tmp/virt/";
     private static final String VIRT_APEX = "/apex/com.android.virt/";
-    private static final int TEST_VM_CID = 5;
+    private static final int TEST_VM_CID = 10;
     private static final int TEST_VM_ADB_PORT = 8000;
     private static final String MICRODROID_SERIAL = "localhost:" + TEST_VM_ADB_PORT;
-    private static final long MICRODROID_BOOT_TIMEOUT_MILLIS = 15000;
+    // This is really slow on GCE (2m 40s) but fast on localhost or actual Android phones (< 10s)
+    // Set the maximum timeout value big enough.
+    private static final long MICRODROID_BOOT_TIMEOUT_MINUTES = 5;
 
     private String executeCommand(String cmd) {
-        final long defaultCommandTimeoutMillis = 1000; // 1 sec
+        final long defaultCommandTimeoutMillis = 3000; // 3 sec. Can be slow on GCE
         return executeCommand(defaultCommandTimeoutMillis, cmd);
     }
 
@@ -58,6 +63,11 @@
         return result.getStdout().trim(); // remove the trailing whitespace including newline
     }
 
+    private String executeCommandOnMicrodroid(String cmd) {
+        cmd = "adb -s " + MICRODROID_SERIAL + " " + cmd;
+        return executeCommand(cmd);
+    }
+
     @Test
     public void testMicrodroidBoots() throws Exception {
         // Prepare input files
@@ -122,7 +132,7 @@
                         "cd %s; %sbin/crosvm run --cid=%d --disable-sandbox --bios=bootloader"
                                 + " --serial=type=syslog --disk=os_composite.img"
                                 + " --disk=env_composite.img --disk=payload.img"
-                                + " --rwdisk=userdata_composite.qcow2",
+                                + " --rwdisk=userdata_composite.qcow2 &",
                         TEST_ROOT, VIRT_APEX, TEST_VM_CID);
         executor.execute(
                 () -> {
@@ -132,9 +142,7 @@
                         throw new RuntimeException(e);
                     }
                 });
-        // .. and wait for microdroid to boot
-        // TODO(jiyong): don't wait too long. We can wait less by monitoring log from microdroid
-        Thread.sleep(MICRODROID_BOOT_TIMEOUT_MILLIS);
+        waitForMicrodroidBoot(MICRODROID_BOOT_TIMEOUT_MINUTES);
 
         // Connect to microdroid and read a system property from there
         executeCommand(
@@ -146,7 +154,7 @@
                         + TEST_VM_CID
                         + ":5555");
         executeCommand("adb connect " + MICRODROID_SERIAL);
-        String prop = executeCommand("adb -s " + MICRODROID_SERIAL + " shell getprop ro.hardware");
+        String prop = executeCommandOnMicrodroid("shell getprop ro.hardware");
         assertThat(prop, is("microdroid"));
 
         // Test writing to /data partition
@@ -156,28 +164,57 @@
         writer.write("MicrodroidTest");
         writer.close();
 
-        executeCommand(
-                "adb -s "
-                        + MICRODROID_SERIAL
-                        + " push "
-                        + tmpFile.getPath()
-                        + " /data/local/tmp/test.txt");
-        String catResult =
-                executeCommand(
-                        "adb -s " + MICRODROID_SERIAL + " shell cat /data/local/tmp/test.txt");
-        assertThat(catResult, is("MicrodroidTest"));
+        executeCommandOnMicrodroid("push " + tmpFile.getPath() + " /data/local/tmp/test.txt");
+        assertThat(
+                executeCommandOnMicrodroid("shell cat /data/local/tmp/test.txt"),
+                is("MicrodroidTest"));
+
+        assertThat(
+                executeCommandOnMicrodroid("shell ls /system/bin/zipfuse"),
+                is("/system/bin/zipfuse"));
 
         // Shutdown microdroid
         executeCommand("adb -s localhost:" + TEST_VM_ADB_PORT + " shell reboot");
     }
 
+    private void waitForMicrodroidBoot(long timeoutMinutes) throws Exception {
+        // Wait for a specific log from logd
+        // TODO(jiyong): use a more reasonable marker
+        final String pattern = "logd:\\ logd\\ reinit";
+        getDevice()
+                .executeShellV2Command(
+                        "logcat --regex=\"" + pattern + "\" -m 1",
+                        timeoutMinutes,
+                        TimeUnit.MINUTES);
+    }
+
+    private void skipIfFail(String command) throws Exception {
+        assumeThat(
+                getDevice().executeShellV2Command(command).getStatus(), is(CommandStatus.SUCCESS));
+    }
+
+    @Before
+    public void testIfDeviceIsCapable() throws Exception {
+        // Checks the preconditions to run microdroid. If the condition is not satisfied
+        // don't run the test (instead of failing)
+        skipIfFail("ls /dev/kvm");
+        skipIfFail("ls /dev/vhost-vsock");
+        skipIfFail("ls /apex/com.android.virt/bin/crosvm");
+    }
+
     @Before
     public void setUp() throws Exception {
+        // kill stale crosvm processes
+        getDevice().executeShellV2Command("killall crosvm");
+
         // delete the test root
         getDevice().executeShellCommand("rm -rf " + TEST_ROOT);
 
         // disconnect from microdroid
         executeCommand("adb disconnect " + MICRODROID_SERIAL);
+
+        // clear the log
+        getDevice().executeShellV2Command("logcat -c");
     }
 
     @After
diff --git a/zipfuse/TEST_MAPPING b/zipfuse/TEST_MAPPING
new file mode 100644
index 0000000..5b313c1
--- /dev/null
+++ b/zipfuse/TEST_MAPPING
@@ -0,0 +1,7 @@
+{
+  "postsubmit" : [
+    {
+      "name" : "ZipFuseTest"
+    }
+  ]
+}