Add tombstone upon crash test

Bug: 236588647
Test: atest MicrodroidHostTestCases both on Pixel and CF
Change-Id: I5e0a5db243e9688580c8ca44614f4a0c944dd997
diff --git a/tests/hostside/java/android/virt/test/MicrodroidTestCase.java b/tests/hostside/java/android/virt/test/MicrodroidTestCase.java
index afccef6..d25868e 100644
--- a/tests/hostside/java/android/virt/test/MicrodroidTestCase.java
+++ b/tests/hostside/java/android/virt/test/MicrodroidTestCase.java
@@ -363,6 +363,46 @@
         shutdownMicrodroid(getDevice(), cid);
     }
 
+    private boolean isTombstoneGeneratedWithConfig(String configPath) throws Exception {
+        // Note this test relies on logcat values being printed by tombstone_transmit on
+        // and the reeceiver on host (virtualization_service)
+        final String cid =
+                startMicrodroid(
+                        getDevice(),
+                        getBuild(),
+                        APK_NAME,
+                        PACKAGE_NAME,
+                        configPath,
+                        /* debug */ true,
+                        minMemorySize(),
+                        Optional.of(NUM_VCPUS),
+                        Optional.of(CPU_AFFINITY));
+        // check until microdroid is shut down
+        CommandRunner android = new CommandRunner(getDevice());
+        android.runWithTimeout(
+                15000,
+                "logcat",
+                "-m",
+                "1",
+                "-e",
+                "'crosvm has exited normally'");
+        // Check that tombstone is received (from host logcat)
+        String result = runOnHost("adb", "-s", getDevice().getSerialNumber(),
+                "logcat", "-d", "-e",
+                "Received [0-9]+ bytes from guest & wrote to tombstone file");
+        return !result.trim().isEmpty();
+    }
+
+    @Test
+    public void testTombstonesAreGeneratedUponCrash() throws Exception {
+        assertTrue(isTombstoneGeneratedWithConfig("assets/vm_config_crash.json"));
+    }
+
+    @Test
+    public void testTombstonesAreNotGeneratedIfNotExported() throws Exception {
+        assertFalse(isTombstoneGeneratedWithConfig("assets/vm_config_crash_no_tombstone.json"));
+    }
+
     @Test
     public void testTombstonesAreBeingForwarded() throws Exception {
         // This test requires rooting. Skip on user builds where rooting is impossible.
diff --git a/tests/testapk/assets/vm_config_crash.json b/tests/testapk/assets/vm_config_crash.json
new file mode 100644
index 0000000..282f25c
--- /dev/null
+++ b/tests/testapk/assets/vm_config_crash.json
@@ -0,0 +1,13 @@
+{
+    "os": {
+      "name": "microdroid"
+    },
+    "task": {
+      "type": "microdroid_launcher",
+      "command": "MicrodroidTestNativeLib.so",
+      "args": [
+        "crash"
+      ]
+    },
+    "export_tombstones": true
+  }
diff --git a/tests/testapk/assets/vm_config_crash_no_tombstone.json b/tests/testapk/assets/vm_config_crash_no_tombstone.json
new file mode 100644
index 0000000..be0983d
--- /dev/null
+++ b/tests/testapk/assets/vm_config_crash_no_tombstone.json
@@ -0,0 +1,13 @@
+{
+    "os": {
+      "name": "microdroid"
+    },
+    "task": {
+      "type": "microdroid_launcher",
+      "command": "MicrodroidTestNativeLib.so",
+      "args": [
+        "crash"
+      ]
+    },
+    "export_tombstones": false
+  }
diff --git a/tests/testapk/src/native/testbinary.cpp b/tests/testapk/src/native/testbinary.cpp
index 422afca..b4fee86 100644
--- a/tests/testapk/src/native/testbinary.cpp
+++ b/tests/testapk/src/native/testbinary.cpp
@@ -181,6 +181,11 @@
     setvbuf(stdout, nullptr, _IONBF, 0);
     setvbuf(stderr, nullptr, _IONBF, 0);
 
+    if (strcmp(argv[1], "crash") == 0) {
+        printf("test crash!!!!\n");
+        abort();
+    }
+
     printf("Hello Microdroid ");
     for (int i = 0; i < argc; i++) {
         printf("%s", argv[i]);