[test] Refactor rialto_test to check all the service_vm_manager

Test: atest rialto_test
Bug: 299411175
Change-Id: Id1fdc5c3877ccfa32a1cda454a808da73b48da53
diff --git a/rialto/Android.bp b/rialto/Android.bp
index 860e656..d8e4536 100644
--- a/rialto/Android.bp
+++ b/rialto/Android.bp
@@ -110,7 +110,6 @@
         "libvmclient",
     ],
     data: [
-        ":rialto_bin",
         ":rialto_unsigned",
     ],
     test_suites: ["general-tests"],
diff --git a/rialto/tests/test.rs b/rialto/tests/test.rs
index da1a063..20d00b5 100644
--- a/rialto/tests/test.rs
+++ b/rialto/tests/test.rs
@@ -16,7 +16,7 @@
 
 use android_system_virtualizationservice::{
     aidl::android::system::virtualizationservice::{
-        DiskImage::DiskImage, Partition::Partition, VirtualMachineConfig::VirtualMachineConfig,
+        VirtualMachineConfig::VirtualMachineConfig,
         VirtualMachineRawConfig::VirtualMachineRawConfig,
     },
     binder::{ParcelFileDescriptor, ProcessState},
@@ -30,17 +30,9 @@
 use std::path::PathBuf;
 use vmclient::VmInstance;
 
-const SIGNED_RIALTO_PATH: &str = "/data/local/tmp/rialto_test/arm64/rialto.bin";
 const UNSIGNED_RIALTO_PATH: &str = "/data/local/tmp/rialto_test/arm64/rialto_unsigned.bin";
 const INSTANCE_IMG_PATH: &str = "/data/local/tmp/rialto_test/arm64/instance.img";
 
-fn rialto_path(vm_type: VmType) -> &'static str {
-    match vm_type {
-        VmType::ProtectedVm => SIGNED_RIALTO_PATH,
-        VmType::NonProtectedVm => UNSIGNED_RIALTO_PATH,
-    }
-}
-
 #[test]
 fn process_requests_in_protected_vm() -> Result<()> {
     let mut vm = start_service_vm(VmType::ProtectedVm)?;
@@ -85,45 +77,29 @@
 }
 
 fn vm_instance(vm_type: VmType) -> Result<VmInstance> {
-    let virtmgr =
-        vmclient::VirtualizationService::new().context("Failed to spawn VirtualizationService")?;
-    let service = virtmgr.connect().context("Failed to connect to VirtualizationService")?;
-
-    let rialto = File::open(rialto_path(vm_type)).context("Failed to open Rialto kernel binary")?;
-    let console = service_vm_manager::android_log_fd()?;
-    let log = service_vm_manager::android_log_fd()?;
-
-    let disks = match vm_type {
+    match vm_type {
         VmType::ProtectedVm => {
-            let instance_img = service_vm_manager::instance_img(
-                service.as_ref(),
-                PathBuf::from(INSTANCE_IMG_PATH),
-            )?;
-            let writable_partitions = vec![Partition {
-                label: "vm-instance".to_owned(),
-                image: Some(instance_img),
-                writable: true,
-            }];
-            vec![DiskImage { image: None, partitions: writable_partitions, writable: true }]
+            service_vm_manager::protected_vm_instance(PathBuf::from(INSTANCE_IMG_PATH))
         }
-        VmType::NonProtectedVm => vec![],
-    };
+        VmType::NonProtectedVm => nonprotected_vm_instance(),
+    }
+}
+
+fn nonprotected_vm_instance() -> Result<VmInstance> {
+    let rialto = File::open(UNSIGNED_RIALTO_PATH).context("Failed to open Rialto kernel binary")?;
     let config = VirtualMachineConfig::RawConfig(VirtualMachineRawConfig {
-        name: String::from("RialtoTest"),
+        name: String::from("Non protected rialto"),
         bootloader: Some(ParcelFileDescriptor::new(rialto)),
-        disks,
-        protectedVm: vm_type.is_protected(),
+        protectedVm: false,
         memoryMib: 300,
         platformVersion: "~1.0".to_string(),
         ..Default::default()
     });
-    VmInstance::create(
-        service.as_ref(),
-        &config,
-        Some(console),
-        /* consoleIn */ None,
-        Some(log),
-        None,
-    )
-    .context("Failed to create VM")
+    let console = Some(service_vm_manager::android_log_fd()?);
+    let log = Some(service_vm_manager::android_log_fd()?);
+    let virtmgr = vmclient::VirtualizationService::new().context("Failed to spawn VirtMgr")?;
+    let service = virtmgr.connect().context("Failed to connect to VirtMgr")?;
+    info!("Connected to VirtMgr for service VM");
+    VmInstance::create(service.as_ref(), &config, console, /* consoleIn */ None, log, None)
+        .context("Failed to create VM")
 }
diff --git a/service_vm_manager/src/lib.rs b/service_vm_manager/src/lib.rs
index 29eafdd..c27570c 100644
--- a/service_vm_manager/src/lib.rs
+++ b/service_vm_manager/src/lib.rs
@@ -57,7 +57,8 @@
     /// The same instance image is used for different VMs.
     /// TODO(b/278858244): Allow only one service VM running at each time.
     pub fn start() -> Result<Self> {
-        let vm = vm_instance()?;
+        let instance_img_path = Path::new(VIRT_DATA_DIR).join(INSTANCE_IMG_NAME);
+        let vm = protected_vm_instance(instance_img_path)?;
         Self::start_vm(vm, VmType::ProtectedVm)
     }
 
@@ -122,12 +123,12 @@
     }
 }
 
-fn vm_instance() -> Result<VmInstance> {
+/// Returns a `VmInstance` of a protected VM with the instance image from the given path.
+pub fn protected_vm_instance(instance_img_path: PathBuf) -> Result<VmInstance> {
     let virtmgr = vmclient::VirtualizationService::new().context("Failed to spawn VirtMgr")?;
     let service = virtmgr.connect().context("Failed to connect to VirtMgr")?;
     info!("Connected to VirtMgr for service VM");
 
-    let instance_img_path = Path::new(VIRT_DATA_DIR).join(INSTANCE_IMG_NAME);
     let instance_img = instance_img(service.as_ref(), instance_img_path)?;
     let writable_partitions = vec![Partition {
         label: "vm-instance".to_owned(),
@@ -155,8 +156,7 @@
 }
 
 /// Returns the file descriptor of the instance image at the given path.
-/// This function is only exposed for testing.
-pub fn instance_img(
+fn instance_img(
     service: &dyn IVirtualizationService,
     instance_img_path: PathBuf,
 ) -> Result<ParcelFileDescriptor> {