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.

Ignore-AOSP-First: No clean cherry-pick from AOSP

Bug: 243460563
Test: atest MicrodroidHostTestCases
Merged-In: I4208c9d117f5cb996d511d8fd47da73c134ed0f7
Change-Id: I4208c9d117f5cb996d511d8fd47da73c134ed0f7
diff --git a/tests/Android.bp b/tests/Android.bp
index 2c36a62..71a73f7 100644
--- a/tests/Android.bp
+++ b/tests/Android.bp
@@ -90,11 +90,3 @@
     ],
     type: "cpio",
 }
-
-genrule {
-    name: "test-payload-metadata",
-    tools: ["mk_payload"],
-    cmd: "$(location mk_payload) --metadata-only $(in) $(out)",
-    srcs: ["test-payload-metadata-config.json"],
-    out: ["test-payload-metadata.img"],
-}
diff --git a/tests/hostside/Android.bp b/tests/hostside/Android.bp
index 61001d3..a7852d0 100644
--- a/tests/hostside/Android.bp
+++ b/tests/hostside/Android.bp
@@ -21,7 +21,6 @@
         ":microdroid_general_sepolicy.conf",
         ":test.com.android.virt.pem",
         ":test2.com.android.virt.pem",
-        ":test-payload-metadata",
     ],
     data_native_bins: [
         "sepolicy-analyze",
@@ -30,6 +29,7 @@
         "img2simg",
         "lpmake",
         "lpunpack",
+        "mk_payload",
         "sign_virt_apex",
         "simg2img",
     ],
diff --git a/tests/hostside/java/android/virt/test/MicrodroidTestCase.java b/tests/hostside/java/android/virt/test/MicrodroidTestCase.java
index 140f1c9..0a5c4ba 100644
--- a/tests/hostside/java/android/virt/test/MicrodroidTestCase.java
+++ b/tests/hostside/java/android/virt/test/MicrodroidTestCase.java
@@ -28,6 +28,8 @@
 import static org.junit.Assert.fail;
 import static org.junit.Assume.assumeTrue;
 
+import static java.util.stream.Collectors.toList;
+
 import com.android.tradefed.device.DeviceNotAvailableException;
 import com.android.tradefed.result.TestDescription;
 import com.android.tradefed.result.TestResult;
@@ -40,7 +42,6 @@
 import com.android.tradefed.util.xml.AbstractXmlParser;
 
 import org.json.JSONArray;
-import org.json.JSONException;
 import org.json.JSONObject;
 import org.junit.After;
 import org.junit.Before;
@@ -53,7 +54,6 @@
 
 import java.io.ByteArrayInputStream;
 import java.io.File;
-import java.io.IOException;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
@@ -137,6 +137,47 @@
         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();
+        assertEquals(
+                "creating payload metadata failed:\n\tout: " + out + "\n\terr: " + err + "\n",
+                CommandStatus.SUCCESS, result.getStatus());
+    }
+
     private void resignVirtApex(File virtApexDir, File signingKey, Map<String, File> keyOverrides) {
         File signVirtApex = findTestFile("sign_virt_apex");
 
@@ -190,9 +231,11 @@
     static class ActiveApexInfo {
         public String name;
         public String path;
-        ActiveApexInfo(String name, String path) {
+        public boolean provideSharedApexLibs;
+        ActiveApexInfo(String name, String path, boolean provideSharedApexLibs) {
             this.name = name;
             this.path = path;
+            this.provideSharedApexLibs = provideSharedApexLibs;
         }
     }
 
@@ -209,6 +252,9 @@
             }
             return null;
         }
+        List<ActiveApexInfo> getSharedLibApexes() {
+            return mList.stream().filter(info -> info.provideSharedApexLibs).collect(toList());
+        }
     }
 
     private ActiveApexInfoList getActiveApexInfoList() throws Exception {
@@ -223,8 +269,10 @@
                             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)));
                         }
                     }
                 };
@@ -261,14 +309,11 @@
         android.run(VIRT_APEX + "bin/vm", "create-partition", "--type instance",
                 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
@@ -308,14 +353,28 @@
 
         // 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("microdroid-apex-0", statsdApexPath))
-                .put(newPartition("microdroid-apex-1", adbdApexPath))
+        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));
+        int apexIndex = 0;
+        for (ActiveApexInfo apex : apexesForVm) {
+            partitions.put(
+                    newPartition(String.format("microdroid-apex-%d", apexIndex++), apex.path));
+        }
+        partitions
                 .put(newPartition("microdroid-apk", apkPath))
-                .put(newPartition("microdroid-apk-idsig", idSigPath))));
+                .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);
 
diff --git a/tests/test-payload-metadata-config.json b/tests/test-payload-metadata-config.json
deleted file mode 100644
index 3c56e5f..0000000
--- a/tests/test-payload-metadata-config.json
+++ /dev/null
@@ -1,19 +0,0 @@
-{
-  "_comment": "This file is to create a payload-metadata partition for payload.img which is for MicrodroidTestApp to run with assets/vm_config.json",
-  "apexes": [
-    {
-      "name": "com.android.os.statsd",
-      "path": ""
-    },
-    {
-      "name": "com.android.adbd",
-      "path": ""
-    }
-  ],
-  "apk": {
-    "name": "microdroid-apk",
-    "path": "",
-    "idsig_path": ""
-  },
-  "payload_config_path": "/mnt/apk/assets/vm_config.json"
-}
\ No newline at end of file