blob: e975bbf609f0410f6c633f7c0da062d5f29bf476 [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 Wanga6357692023-09-07 14:59:37 +000019 VirtualMachineConfig::VirtualMachineConfig,
David Brazdil66fc1202022-07-04 21:48:45 +010020 VirtualMachineRawConfig::VirtualMachineRawConfig,
21 },
22 binder::{ParcelFileDescriptor, ProcessState},
23};
Alice Wang9646fb32023-09-08 10:01:31 +000024use anyhow::{bail, Context, Result};
David Brazdil66fc1202022-07-04 21:48:45 +010025use log::info;
Alice Wang9646fb32023-09-08 10:01:31 +000026use service_vm_comm::{
27 EcdsaP256KeyPair, GenerateCertificateRequestParams, Request, Response, VmType,
28};
Alice Wang17dc76e2023-09-06 09:43:52 +000029use service_vm_manager::ServiceVm;
David Brazdil66fc1202022-07-04 21:48:45 +010030use std::fs::File;
David Brazdil66fc1202022-07-04 21:48:45 +010031use std::panic;
Alice Wang17dc76e2023-09-06 09:43:52 +000032use std::path::PathBuf;
Alice Wang17dc76e2023-09-06 09:43:52 +000033use vmclient::VmInstance;
Alice Wang4e082c32023-07-11 07:41:50 +000034
Alice Wang9a8b39f2023-04-12 15:31:48 +000035const UNSIGNED_RIALTO_PATH: &str = "/data/local/tmp/rialto_test/arm64/rialto_unsigned.bin";
36const INSTANCE_IMG_PATH: &str = "/data/local/tmp/rialto_test/arm64/instance.img";
David Brazdil66fc1202022-07-04 21:48:45 +010037
Alice Wang9a8b39f2023-04-12 15:31:48 +000038#[test]
Alice Wange910b902023-09-07 10:35:12 +000039fn process_requests_in_protected_vm() -> Result<()> {
Alice Wang9646fb32023-09-08 10:01:31 +000040 check_processing_requests(VmType::ProtectedVm)
Alice Wang9a8b39f2023-04-12 15:31:48 +000041}
42
Alice Wange910b902023-09-07 10:35:12 +000043#[test]
44fn process_requests_in_non_protected_vm() -> Result<()> {
Alice Wang9646fb32023-09-08 10:01:31 +000045 check_processing_requests(VmType::NonProtectedVm)
46}
47
48fn check_processing_requests(vm_type: VmType) -> Result<()> {
49 let mut vm = start_service_vm(vm_type)?;
Alice Wange910b902023-09-07 10:35:12 +000050
51 check_processing_reverse_request(&mut vm)?;
Alice Wang9646fb32023-09-08 10:01:31 +000052 check_processing_generating_key_pair_request(&mut vm)?;
53 check_processing_generating_certificate_request(&mut vm)?;
Alice Wange910b902023-09-07 10:35:12 +000054 Ok(())
55}
56
57fn check_processing_reverse_request(vm: &mut ServiceVm) -> Result<()> {
58 // TODO(b/292080257): Test with message longer than the receiver's buffer capacity
59 // 1024 bytes once the guest virtio-vsock driver fixes the credit update in recv().
60 let message = "abc".repeat(166);
61 let request = Request::Reverse(message.as_bytes().to_vec());
62
Alice Wangfbdc85b2023-09-07 12:56:46 +000063 let response = vm.process_request(request)?;
64 info!("Received response: {response:?}.");
Alice Wange910b902023-09-07 10:35:12 +000065
66 let expected_response: Vec<u8> = message.as_bytes().iter().rev().cloned().collect();
67 assert_eq!(Response::Reverse(expected_response), response);
68 Ok(())
69}
70
Alice Wang9646fb32023-09-08 10:01:31 +000071fn check_processing_generating_key_pair_request(vm: &mut ServiceVm) -> Result<()> {
72 let request = Request::GenerateEcdsaP256KeyPair;
73
74 let response = vm.process_request(request)?;
75 info!("Received response: {response:?}.");
76
77 match response {
Alice Wanga78d3f02023-09-13 12:39:16 +000078 Response::GenerateEcdsaP256KeyPair(EcdsaP256KeyPair { maced_public_key, .. }) => {
79 assert_array_has_nonzero(&maced_public_key[..]);
80 Ok(())
81 }
Alice Wang9646fb32023-09-08 10:01:31 +000082 _ => bail!("Incorrect response type"),
83 }
84}
85
Alice Wanga78d3f02023-09-13 12:39:16 +000086fn assert_array_has_nonzero(v: &[u8]) {
87 assert!(v.iter().any(|&x| x != 0))
88}
89
Alice Wang9646fb32023-09-08 10:01:31 +000090fn check_processing_generating_certificate_request(vm: &mut ServiceVm) -> Result<()> {
91 let params = GenerateCertificateRequestParams { keys_to_sign: vec![], challenge: vec![] };
92 let request = Request::GenerateCertificateRequest(params);
93
94 let response = vm.process_request(request)?;
95 info!("Received response: {response:?}.");
96
97 match response {
98 Response::GenerateCertificateRequest(_) => Ok(()),
99 _ => bail!("Incorrect response type"),
100 }
101}
102
Alice Wange910b902023-09-07 10:35:12 +0000103fn start_service_vm(vm_type: VmType) -> Result<ServiceVm> {
David Brazdil66fc1202022-07-04 21:48:45 +0100104 android_logger::init_once(
105 android_logger::Config::default().with_tag("rialto").with_min_level(log::Level::Debug),
106 );
David Brazdil66fc1202022-07-04 21:48:45 +0100107 // Redirect panic messages to logcat.
108 panic::set_hook(Box::new(|panic_info| {
109 log::error!("{}", panic_info);
110 }));
David Brazdil66fc1202022-07-04 21:48:45 +0100111 // We need to start the thread pool for Binder to work properly, especially link_to_death.
112 ProcessState::start_thread_pool();
Alice Wange910b902023-09-07 10:35:12 +0000113 ServiceVm::start_vm(vm_instance(vm_type)?, vm_type)
Alice Wang17dc76e2023-09-06 09:43:52 +0000114}
115
Alice Wange910b902023-09-07 10:35:12 +0000116fn vm_instance(vm_type: VmType) -> Result<VmInstance> {
Alice Wanga6357692023-09-07 14:59:37 +0000117 match vm_type {
Alice Wang1d9a5872023-09-06 14:32:36 +0000118 VmType::ProtectedVm => {
Alice Wanga6357692023-09-07 14:59:37 +0000119 service_vm_manager::protected_vm_instance(PathBuf::from(INSTANCE_IMG_PATH))
Alice Wang1d9a5872023-09-06 14:32:36 +0000120 }
Alice Wanga6357692023-09-07 14:59:37 +0000121 VmType::NonProtectedVm => nonprotected_vm_instance(),
122 }
123}
124
125fn nonprotected_vm_instance() -> Result<VmInstance> {
126 let rialto = File::open(UNSIGNED_RIALTO_PATH).context("Failed to open Rialto kernel binary")?;
David Brazdil66fc1202022-07-04 21:48:45 +0100127 let config = VirtualMachineConfig::RawConfig(VirtualMachineRawConfig {
Alice Wanga6357692023-09-07 14:59:37 +0000128 name: String::from("Non protected rialto"),
David Brazdil66fc1202022-07-04 21:48:45 +0100129 bootloader: Some(ParcelFileDescriptor::new(rialto)),
Alice Wanga6357692023-09-07 14:59:37 +0000130 protectedVm: false,
David Brazdil66fc1202022-07-04 21:48:45 +0100131 memoryMib: 300,
David Brazdil66fc1202022-07-04 21:48:45 +0100132 platformVersion: "~1.0".to_string(),
Inseob Kim6ef80972023-07-20 17:23:36 +0900133 ..Default::default()
David Brazdil66fc1202022-07-04 21:48:45 +0100134 });
Alice Wanga6357692023-09-07 14:59:37 +0000135 let console = Some(service_vm_manager::android_log_fd()?);
136 let log = Some(service_vm_manager::android_log_fd()?);
137 let virtmgr = vmclient::VirtualizationService::new().context("Failed to spawn VirtMgr")?;
138 let service = virtmgr.connect().context("Failed to connect to VirtMgr")?;
139 info!("Connected to VirtMgr for service VM");
140 VmInstance::create(service.as_ref(), &config, console, /* consoleIn */ None, log, None)
141 .context("Failed to create VM")
David Brazdil66fc1202022-07-04 21:48:45 +0100142}