Pass "shared libs" apexes

Signing key tests rely on "raw configs" and custom-built payload image.
When passing "adbd" and "statsd" apexes to VM, "shared libs" apexes
should be passed together but the custom-built payload image was missing
"shared libs" apexes.

This fixes the test by passing "shared libs" apexes to VM.

Note that virtualizationservice handles this already.

Bug: 243460563
Test: atest MicrodroidHostTestCases
Change-Id: I4208c9d117f5cb996d511d8fd47da73c134ed0f7
diff --git a/tests/hostside/Android.bp b/tests/hostside/Android.bp
index d77f3de..7679c57 100644
--- a/tests/hostside/Android.bp
+++ b/tests/hostside/Android.bp
@@ -23,7 +23,6 @@
         ":microdroid_general_sepolicy.conf",
         ":test.com.android.virt.pem",
         ":test2.com.android.virt.pem",
-        ":test-payload-metadata",
     ],
     data_native_bins: [
         "sepolicy-analyze",
@@ -32,6 +31,7 @@
         "img2simg",
         "lpmake",
         "lpunpack",
+        "mk_payload",
         "sign_virt_apex",
         "simg2img",
     ],
diff --git a/tests/hostside/java/com/android/microdroid/test/MicrodroidTestCase.java b/tests/hostside/java/com/android/microdroid/test/MicrodroidTestCase.java
index 5ac36a0..c0f293e 100644
--- a/tests/hostside/java/com/android/microdroid/test/MicrodroidTestCase.java
+++ b/tests/hostside/java/com/android/microdroid/test/MicrodroidTestCase.java
@@ -32,6 +32,8 @@
 import static org.junit.Assert.fail;
 import static org.junit.Assume.assumeTrue;
 
+import static java.util.stream.Collectors.toList;
+
 import android.cts.statsdatom.lib.ConfigUtils;
 import android.cts.statsdatom.lib.ReportUtils;
 
@@ -139,6 +141,53 @@
         return new JSONObject(Map.of("label", label, "path", path));
     }
 
+    private void createPayloadMetadata(List<ActiveApexInfo> apexes, File payloadMetadata)
+            throws Exception {
+        // mk_payload's config
+        File configFile = new File(payloadMetadata.getParentFile(), "payload_config.json");
+        JSONObject config = new JSONObject();
+        config.put(
+                "apk",
+                new JSONObject(Map.of("name", "microdroid-apk", "path", "", "idsig_path", "")));
+        config.put("payload_config_path", "/mnt/apk/assets/vm_config.json");
+        config.put(
+                "apexes",
+                new JSONArray(
+                        apexes.stream()
+                                .map(apex -> new JSONObject(Map.of("name", apex.name, "path", "")))
+                                .collect(toList())));
+        FileUtil.writeToFile(config.toString(), configFile);
+
+        File mkPayload = findTestFile("mk_payload");
+        RunUtil runUtil = new RunUtil();
+        // Set the parent dir on the PATH (e.g. <workdir>/bin)
+        String separator = System.getProperty("path.separator");
+        String path = mkPayload.getParentFile().getPath() + separator + System.getenv("PATH");
+        runUtil.setEnvVariable("PATH", path);
+
+        List<String> command = new ArrayList<String>();
+        command.add("mk_payload");
+        command.add("--metadata-only");
+        command.add(configFile.toString());
+        command.add(payloadMetadata.toString());
+
+        CommandResult result =
+                runUtil.runTimedCmd(
+                        // mk_payload should run fast enough
+                        5 * 1000, "/bin/bash", "-c", String.join(" ", command));
+        String out = result.getStdout();
+        String err = result.getStderr();
+        assertWithMessage(
+                        "creating payload metadata failed:\n\tout: "
+                                + out
+                                + "\n\terr: "
+                                + err
+                                + "\n")
+                .about(command_results())
+                .that(result)
+                .isSuccess();
+    }
+
     private void resignVirtApex(File virtApexDir, File signingKey, Map<String, File> keyOverrides) {
         File signVirtApex = findTestFile("sign_virt_apex");
 
@@ -193,10 +242,12 @@
     static class ActiveApexInfo {
         public String name;
         public String path;
+        public boolean provideSharedApexLibs;
 
-        ActiveApexInfo(String name, String path) {
+        ActiveApexInfo(String name, String path, boolean provideSharedApexLibs) {
             this.name = name;
             this.path = path;
+            this.provideSharedApexLibs = provideSharedApexLibs;
         }
     }
 
@@ -215,6 +266,10 @@
             }
             return null;
         }
+
+        List<ActiveApexInfo> getSharedLibApexes() {
+            return mList.stream().filter(info -> info.provideSharedApexLibs).collect(toList());
+        }
     }
 
     private ActiveApexInfoList getActiveApexInfoList() throws Exception {
@@ -229,10 +284,10 @@
                             String uri, String localName, String qName, Attributes attributes) {
                         if (localName.equals("apex-info")
                                 && attributes.getValue("isActive").equals("true")) {
-                            list.add(
-                                    new ActiveApexInfo(
-                                            attributes.getValue("moduleName"),
-                                            attributes.getValue("modulePath")));
+                            String name = attributes.getValue("moduleName");
+                            String path = attributes.getValue("modulePath");
+                            String sharedApex = attributes.getValue("provideSharedApexLibs");
+                            list.add(new ActiveApexInfo(name, path, "true".equals(sharedApex)));
                         }
                     }
                 };
@@ -277,14 +332,11 @@
                 instanceImgPath,
                 Integer.toString(10 * 1024 * 1024));
 
-        // payload-metadata is prepared on host with the two APEXes and APK
+        // payload-metadata is created on device
         final String payloadMetadataPath = TEST_ROOT + "payload-metadata.img";
-        getDevice().pushFile(findTestFile("test-payload-metadata.img"), payloadMetadataPath);
 
-        // get paths to the two APEXes required for the VM.
+        // Load /apex/apex-info-list.xml to get paths to APEXes required for the VM.
         ActiveApexInfoList list = getActiveApexInfoList();
-        final String statsdApexPath = list.get("com.android.os.statsd").path;
-        final String adbdApexPath = list.get("com.android.adbd").path;
 
         // Since Java APP can't start a VM with a custom image, here, we start a VM using `vm run`
         // command with a VM Raw config which is equiv. to what virtualizationservice creates with
@@ -325,19 +377,26 @@
 
         // Add payload image disk with partitions:
         // - payload-metadata
-        // - apexes: com.android.os.statsd, com.android.adbd
+        // - apexes: com.android.os.statsd, com.android.adbd, [sharedlib apex](optional)
         // - apk and idsig
-        disks.put(
-                new JSONObject()
-                        .put("writable", false)
-                        .put(
-                                "partitions",
-                                new JSONArray()
-                                        .put(newPartition("payload-metadata", payloadMetadataPath))
-                                        .put(newPartition("com.android.os.statsd", statsdApexPath))
-                                        .put(newPartition("com.android.adbd", adbdApexPath))
-                                        .put(newPartition("microdroid-apk", apkPath))
-                                        .put(newPartition("microdroid-apk-idsig", idSigPath))));
+        List<ActiveApexInfo> apexesForVm = new ArrayList<>();
+        apexesForVm.add(list.get("com.android.os.statsd"));
+        apexesForVm.add(list.get("com.android.adbd"));
+        apexesForVm.addAll(list.getSharedLibApexes());
+
+        final JSONArray partitions = new JSONArray();
+        partitions.put(newPartition("payload-metadata", payloadMetadataPath));
+        for (ActiveApexInfo apex : apexesForVm) {
+            partitions.put(newPartition(apex.name, apex.path));
+        }
+        partitions
+                .put(newPartition("microdroid-apk", apkPath))
+                .put(newPartition("microdroid-apk-idsig", idSigPath));
+        disks.put(new JSONObject().put("writable", false).put("partitions", partitions));
+
+        final File localPayloadMetadata = new File(virtApexDir, "payload-metadata.img");
+        createPayloadMetadata(apexesForVm, localPayloadMetadata);
+        getDevice().pushFile(localPayloadMetadata, payloadMetadataPath);
 
         config.put("protected", isProtected);