blob: da1a063112c024f1ffb011692c4df83e330ceb46 [file] [log] [blame]
David Brazdil66fc1202022-07-04 21:48:45 +01001// Copyright 2022, The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Integration test for Rialto.
16
17use android_system_virtualizationservice::{
18 aidl::android::system::virtualizationservice::{
Alice Wang977b64b2023-09-07 14:04:26 +000019 DiskImage::DiskImage, Partition::Partition, VirtualMachineConfig::VirtualMachineConfig,
David Brazdil66fc1202022-07-04 21:48:45 +010020 VirtualMachineRawConfig::VirtualMachineRawConfig,
21 },
22 binder::{ParcelFileDescriptor, ProcessState},
23};
Alice Wang17dc76e2023-09-06 09:43:52 +000024use anyhow::{Context, Result};
David Brazdil66fc1202022-07-04 21:48:45 +010025use log::info;
Alice Wang1d9a5872023-09-06 14:32:36 +000026use service_vm_comm::{Request, Response, VmType};
Alice Wang17dc76e2023-09-06 09:43:52 +000027use service_vm_manager::ServiceVm;
David Brazdil66fc1202022-07-04 21:48:45 +010028use std::fs::File;
David Brazdil66fc1202022-07-04 21:48:45 +010029use std::panic;
Alice Wang17dc76e2023-09-06 09:43:52 +000030use std::path::PathBuf;
Alice Wang17dc76e2023-09-06 09:43:52 +000031use vmclient::VmInstance;
Alice Wang4e082c32023-07-11 07:41:50 +000032
Alice Wang9a8b39f2023-04-12 15:31:48 +000033const SIGNED_RIALTO_PATH: &str = "/data/local/tmp/rialto_test/arm64/rialto.bin";
34const UNSIGNED_RIALTO_PATH: &str = "/data/local/tmp/rialto_test/arm64/rialto_unsigned.bin";
35const INSTANCE_IMG_PATH: &str = "/data/local/tmp/rialto_test/arm64/instance.img";
David Brazdil66fc1202022-07-04 21:48:45 +010036
Alice Wange910b902023-09-07 10:35:12 +000037fn rialto_path(vm_type: VmType) -> &'static str {
38 match vm_type {
39 VmType::ProtectedVm => SIGNED_RIALTO_PATH,
40 VmType::NonProtectedVm => UNSIGNED_RIALTO_PATH,
41 }
Alice Wang9a8b39f2023-04-12 15:31:48 +000042}
43
44#[test]
Alice Wange910b902023-09-07 10:35:12 +000045fn process_requests_in_protected_vm() -> Result<()> {
46 let mut vm = start_service_vm(VmType::ProtectedVm)?;
47
48 check_processing_reverse_request(&mut vm)?;
49 Ok(())
Alice Wang9a8b39f2023-04-12 15:31:48 +000050}
51
Alice Wange910b902023-09-07 10:35:12 +000052#[test]
53fn process_requests_in_non_protected_vm() -> Result<()> {
54 let mut vm = start_service_vm(VmType::NonProtectedVm)?;
55
56 check_processing_reverse_request(&mut vm)?;
57 Ok(())
58}
59
60fn check_processing_reverse_request(vm: &mut ServiceVm) -> Result<()> {
61 // TODO(b/292080257): Test with message longer than the receiver's buffer capacity
62 // 1024 bytes once the guest virtio-vsock driver fixes the credit update in recv().
63 let message = "abc".repeat(166);
64 let request = Request::Reverse(message.as_bytes().to_vec());
65
66 let response = vm.process_request(&request)?;
67 info!("Received response '{response:?}' for the request '{request:?}'.");
68
69 let expected_response: Vec<u8> = message.as_bytes().iter().rev().cloned().collect();
70 assert_eq!(Response::Reverse(expected_response), response);
71 Ok(())
72}
73
74fn start_service_vm(vm_type: VmType) -> Result<ServiceVm> {
David Brazdil66fc1202022-07-04 21:48:45 +010075 android_logger::init_once(
76 android_logger::Config::default().with_tag("rialto").with_min_level(log::Level::Debug),
77 );
David Brazdil66fc1202022-07-04 21:48:45 +010078 // Redirect panic messages to logcat.
79 panic::set_hook(Box::new(|panic_info| {
80 log::error!("{}", panic_info);
81 }));
David Brazdil66fc1202022-07-04 21:48:45 +010082 // We need to start the thread pool for Binder to work properly, especially link_to_death.
83 ProcessState::start_thread_pool();
Alice Wange910b902023-09-07 10:35:12 +000084 ServiceVm::start_vm(vm_instance(vm_type)?, vm_type)
Alice Wang17dc76e2023-09-06 09:43:52 +000085}
86
Alice Wange910b902023-09-07 10:35:12 +000087fn vm_instance(vm_type: VmType) -> Result<VmInstance> {
David Brazdil4b4c5102022-12-19 22:56:20 +000088 let virtmgr =
89 vmclient::VirtualizationService::new().context("Failed to spawn VirtualizationService")?;
90 let service = virtmgr.connect().context("Failed to connect to VirtualizationService")?;
91
Alice Wange910b902023-09-07 10:35:12 +000092 let rialto = File::open(rialto_path(vm_type)).context("Failed to open Rialto kernel binary")?;
Alice Wang977b64b2023-09-07 14:04:26 +000093 let console = service_vm_manager::android_log_fd()?;
94 let log = service_vm_manager::android_log_fd()?;
David Brazdil66fc1202022-07-04 21:48:45 +010095
Alice Wang1d9a5872023-09-06 14:32:36 +000096 let disks = match vm_type {
97 VmType::ProtectedVm => {
Alice Wang17dc76e2023-09-06 09:43:52 +000098 let instance_img = service_vm_manager::instance_img(
99 service.as_ref(),
100 PathBuf::from(INSTANCE_IMG_PATH),
101 )?;
Alice Wang1d9a5872023-09-06 14:32:36 +0000102 let writable_partitions = vec![Partition {
103 label: "vm-instance".to_owned(),
104 image: Some(instance_img),
105 writable: true,
106 }];
107 vec![DiskImage { image: None, partitions: writable_partitions, writable: true }]
108 }
109 VmType::NonProtectedVm => vec![],
Alice Wang9a8b39f2023-04-12 15:31:48 +0000110 };
David Brazdil66fc1202022-07-04 21:48:45 +0100111 let config = VirtualMachineConfig::RawConfig(VirtualMachineRawConfig {
Seungjae Yoo62085c02022-08-12 04:44:52 +0000112 name: String::from("RialtoTest"),
David Brazdil66fc1202022-07-04 21:48:45 +0100113 bootloader: Some(ParcelFileDescriptor::new(rialto)),
Alice Wang9a8b39f2023-04-12 15:31:48 +0000114 disks,
Alice Wang1d9a5872023-09-06 14:32:36 +0000115 protectedVm: vm_type.is_protected(),
David Brazdil66fc1202022-07-04 21:48:45 +0100116 memoryMib: 300,
David Brazdil66fc1202022-07-04 21:48:45 +0100117 platformVersion: "~1.0".to_string(),
Inseob Kim6ef80972023-07-20 17:23:36 +0900118 ..Default::default()
David Brazdil66fc1202022-07-04 21:48:45 +0100119 });
Alice Wang17dc76e2023-09-06 09:43:52 +0000120 VmInstance::create(
Jiyong Parke6fb1672023-06-26 16:45:55 +0900121 service.as_ref(),
122 &config,
123 Some(console),
124 /* consoleIn */ None,
125 Some(log),
126 None,
127 )
Alice Wang17dc76e2023-09-06 09:43:52 +0000128 .context("Failed to create VM")
David Brazdil66fc1202022-07-04 21:48:45 +0100129}