Sign MicrdroidTestApp using APK sig scheme V4

... and copy the idsig file to the device.

Note that the copied idsig file is not used currently, because
mk_payload can't handle the file yet.

Bug: N/A
Test: atest MicrodroidHostTestCases
Change-Id: I7db582ac7933decfe00395ebe83e146ed61618a6
diff --git a/tests/hostside/Android.bp b/tests/hostside/Android.bp
index 0414030..8edd65d 100644
--- a/tests/hostside/Android.bp
+++ b/tests/hostside/Android.bp
@@ -6,6 +6,9 @@
     name: "MicrodroidHostTestCases",
     srcs: ["java/**/*.java"],
     test_suites: ["device-tests"],
-    libs: ["tradefed"],
-    data: [":MicrodroidTestApp"],
+    libs: [
+        "tradefed",
+        "compatibility-tradefed",
+    ],
+    data: [":MicrodroidTestApp.signed"],
 }
diff --git a/tests/hostside/java/android/virt/test/MicrodroidTestCase.java b/tests/hostside/java/android/virt/test/MicrodroidTestCase.java
index 29d633f..baa383b 100644
--- a/tests/hostside/java/android/virt/test/MicrodroidTestCase.java
+++ b/tests/hostside/java/android/virt/test/MicrodroidTestCase.java
@@ -23,6 +23,7 @@
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assume.assumeThat;
 
+import com.android.compatibility.common.tradefed.build.CompatibilityBuildHelper;
 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner;
 import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test;
 import com.android.tradefed.util.CommandResult;
@@ -51,6 +52,7 @@
     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 String TEST_APK = "MicrodroidTestApp.apk";
     // 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;
@@ -95,6 +97,15 @@
                         TEST_ROOT, VIRT_APEX, VIRT_APEX);
         getDevice().executeShellCommand(makeEnvCompositeCmd);
 
+        String idsigPath = TEST_ROOT + TEST_APK + ".idsig";
+        File idsigFile = (new CompatibilityBuildHelper(getBuild())).getTestFile(TEST_APK + ".idsig");
+        getDevice().pushFile(idsigFile, idsigPath);
+
+        // TODO(b/190343842): pass this file to mk_payload
+        String idsigExists = getDevice().executeShellV2Command("[ -f " + idsigPath + " ] && echo ok")
+                .getStdout().trim();
+        assertThat(idsigExists, is("ok"));
+
         String payloadJsonOrig = VIRT_APEX + "etc/microdroid_payload.json";
         String mkPayload = VIRT_APEX + "bin/mk_payload";
 
diff --git a/tests/testapk/Android.bp b/tests/testapk/Android.bp
index f2dad62..35f2f08 100644
--- a/tests/testapk/Android.bp
+++ b/tests/testapk/Android.bp
@@ -16,3 +16,20 @@
     srcs: ["src/native/*.cpp"],
     sdk_version: "current",
 }
+
+genrule {
+    name: "MicrodroidTestApp.signed",
+    out: [
+        "MicrodroidTestApp.apk",
+        "MicrodroidTestApp.apk.idsig",
+    ],
+    srcs: [":MicrodroidTestApp"],
+    tools:["apksigner"],
+    tool_files: ["test.keystore"],
+    cmd: "$(location apksigner) sign " +
+         "--ks $(location test.keystore) " +
+         "--ks-pass=pass:testkey --key-pass=pass:testkey " +
+         "--in $(in) " +
+         "--out $(genDir)/MicrodroidTestApp.apk",
+         // $(genDir)/MicrodroidTestApp.apk.idsig is generated implicitly
+}
diff --git a/tests/testapk/test.keystore b/tests/testapk/test.keystore
new file mode 100644
index 0000000..2f024d8
--- /dev/null
+++ b/tests/testapk/test.keystore
Binary files differ