Andrew Walbran | 94bbf2f | 2022-05-12 18:35:42 +0000 | [diff] [blame] | 1 | // 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 VM bootloader. |
| 16 | |
| 17 | use android_system_virtualizationservice::{ |
| 18 | aidl::android::system::virtualizationservice::{ |
| 19 | VirtualMachineConfig::VirtualMachineConfig, |
| 20 | VirtualMachineRawConfig::VirtualMachineRawConfig, |
| 21 | }, |
| 22 | binder::{ParcelFileDescriptor, ProcessState}, |
| 23 | }; |
| 24 | use anyhow::{Context, Error}; |
| 25 | use log::info; |
| 26 | use std::{ |
| 27 | fs::File, |
| 28 | io, |
| 29 | os::unix::io::{AsRawFd, FromRawFd}, |
| 30 | }; |
| 31 | use vmclient::{DeathReason, VmInstance}; |
| 32 | |
| 33 | const VMBASE_EXAMPLE_PATH: &str = |
| 34 | "/data/local/tmp/vmbase_example.integration_test/arm64/vmbase_example.bin"; |
| 35 | |
| 36 | /// Runs the vmbase_example VM as an unprotected VM via VirtualizationService. |
| 37 | #[test] |
| 38 | fn test_run_example_vm() -> Result<(), Error> { |
| 39 | env_logger::init(); |
| 40 | |
| 41 | // We need to start the thread pool for Binder to work properly, especially link_to_death. |
| 42 | ProcessState::start_thread_pool(); |
| 43 | |
| 44 | let service = vmclient::connect().context("Failed to find VirtualizationService")?; |
| 45 | |
| 46 | // Start example VM. |
| 47 | let bootloader = ParcelFileDescriptor::new( |
| 48 | File::open(VMBASE_EXAMPLE_PATH) |
| 49 | .with_context(|| format!("Failed to open VM image {}", VMBASE_EXAMPLE_PATH))?, |
| 50 | ); |
Seungjae Yoo | 62085c0 | 2022-08-12 04:44:52 +0000 | [diff] [blame^] | 51 | |
Andrew Walbran | 94bbf2f | 2022-05-12 18:35:42 +0000 | [diff] [blame] | 52 | let config = VirtualMachineConfig::RawConfig(VirtualMachineRawConfig { |
Seungjae Yoo | 62085c0 | 2022-08-12 04:44:52 +0000 | [diff] [blame^] | 53 | name: String::from("VmBaseTest"), |
Andrew Walbran | 94bbf2f | 2022-05-12 18:35:42 +0000 | [diff] [blame] | 54 | kernel: None, |
| 55 | initrd: None, |
| 56 | params: None, |
| 57 | bootloader: Some(bootloader), |
| 58 | disks: vec![], |
| 59 | protectedVm: false, |
| 60 | memoryMib: 300, |
| 61 | numCpus: 1, |
| 62 | cpuAffinity: None, |
| 63 | platformVersion: "~1.0".to_string(), |
| 64 | taskProfiles: vec![], |
| 65 | }); |
| 66 | let console = duplicate_stdout()?; |
| 67 | let log = duplicate_stdout()?; |
Alan Stokes | 0e82b50 | 2022-08-08 14:44:48 +0100 | [diff] [blame] | 68 | let vm = VmInstance::create(service.as_ref(), &config, Some(console), Some(log), None) |
Andrew Walbran | 94bbf2f | 2022-05-12 18:35:42 +0000 | [diff] [blame] | 69 | .context("Failed to create VM")?; |
| 70 | vm.start().context("Failed to start VM")?; |
| 71 | info!("Started example VM."); |
| 72 | |
| 73 | // Wait for VM to finish, and check that it shut down cleanly. |
| 74 | let death_reason = vm.wait_for_death(); |
| 75 | assert_eq!(death_reason, DeathReason::Shutdown); |
| 76 | |
| 77 | Ok(()) |
| 78 | } |
| 79 | |
| 80 | /// Safely duplicate the standard output file descriptor. |
| 81 | fn duplicate_stdout() -> io::Result<File> { |
| 82 | let stdout_fd = io::stdout().as_raw_fd(); |
| 83 | // Safe because this just duplicates a file descriptor which we know to be valid, and we check |
| 84 | // for an error. |
| 85 | let dup_fd = unsafe { libc::dup(stdout_fd) }; |
| 86 | if dup_fd < 0 { |
| 87 | Err(io::Error::last_os_error()) |
| 88 | } else { |
| 89 | // Safe because we have just duplicated the file descriptor so we own it, and `from_raw_fd` |
| 90 | // takes ownership of it. |
| 91 | Ok(unsafe { File::from_raw_fd(dup_fd) }) |
| 92 | } |
| 93 | } |