Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 1 | // Copyright 2021, 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 | //! Payload disk image |
| 16 | |
Andrew Walbran | cc0db52 | 2021-07-12 17:03:42 +0000 | [diff] [blame] | 17 | use android_system_virtualizationservice::aidl::android::system::virtualizationservice::{ |
Alan Stokes | 0d1ef78 | 2022-09-27 13:46:35 +0100 | [diff] [blame] | 18 | DiskImage::DiskImage, |
| 19 | Partition::Partition, |
| 20 | VirtualMachineAppConfig::DebugLevel::DebugLevel, |
| 21 | VirtualMachineAppConfig::{Payload::Payload, VirtualMachineAppConfig}, |
Andrew Walbran | cc0db52 | 2021-07-12 17:03:42 +0000 | [diff] [blame] | 22 | VirtualMachineRawConfig::VirtualMachineRawConfig, |
| 23 | }; |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 24 | use anyhow::{anyhow, bail, Context, Result}; |
Alan Stokes | 0e82b50 | 2022-08-08 14:44:48 +0100 | [diff] [blame] | 25 | use binder::{wait_for_interface, ParcelFileDescriptor}; |
Alan Stokes | bf20c6a | 2022-01-04 12:30:50 +0000 | [diff] [blame] | 26 | use log::{info, warn}; |
Alan Stokes | 0d1ef78 | 2022-09-27 13:46:35 +0100 | [diff] [blame] | 27 | use microdroid_metadata::{ApexPayload, ApkPayload, Metadata, PayloadConfig, PayloadMetadata}; |
Jooyung Han | 5dc4217 | 2021-10-05 16:43:47 +0900 | [diff] [blame] | 28 | use microdroid_payload_config::{ApexConfig, VmPayloadConfig}; |
Jooyung Han | 9900f3d | 2021-07-06 10:27:54 +0900 | [diff] [blame] | 29 | use once_cell::sync::OnceCell; |
Jooyung Han | 743e0d6 | 2022-11-07 20:57:48 +0900 | [diff] [blame^] | 30 | use packagemanager_aidl::aidl::android::content::pm::{ |
| 31 | IPackageManagerNative::IPackageManagerNative, StagedApexInfo::StagedApexInfo, |
| 32 | }; |
Alan Stokes | bf20c6a | 2022-01-04 12:30:50 +0000 | [diff] [blame] | 33 | use regex::Regex; |
Jooyung Han | 44b02ab | 2021-07-16 03:19:13 +0900 | [diff] [blame] | 34 | use serde::Deserialize; |
| 35 | use serde_xml_rs::from_reader; |
Alan Stokes | bf20c6a | 2022-01-04 12:30:50 +0000 | [diff] [blame] | 36 | use std::collections::HashSet; |
Andrew Walbran | 40be9d5 | 2022-01-19 14:32:53 +0000 | [diff] [blame] | 37 | use std::fs::{metadata, File, OpenOptions}; |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 38 | use std::path::{Path, PathBuf}; |
Alan Stokes | bf20c6a | 2022-01-04 12:30:50 +0000 | [diff] [blame] | 39 | use std::process::Command; |
Andrew Walbran | 40be9d5 | 2022-01-19 14:32:53 +0000 | [diff] [blame] | 40 | use std::time::SystemTime; |
Andrew Walbran | cc0db52 | 2021-07-12 17:03:42 +0000 | [diff] [blame] | 41 | use vmconfig::open_parcel_file; |
| 42 | |
| 43 | /// The list of APEXes which microdroid requires. |
| 44 | // TODO(b/192200378) move this to microdroid.json? |
Inseob Kim | b4868e6 | 2021-11-09 17:28:23 +0900 | [diff] [blame] | 45 | const MICRODROID_REQUIRED_APEXES: [&str; 1] = ["com.android.os.statsd"]; |
| 46 | const MICRODROID_REQUIRED_APEXES_DEBUG: [&str; 1] = ["com.android.adbd"]; |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 47 | |
Jooyung Han | 44b02ab | 2021-07-16 03:19:13 +0900 | [diff] [blame] | 48 | const APEX_INFO_LIST_PATH: &str = "/apex/apex-info-list.xml"; |
| 49 | |
Jooyung Han | 5dc4217 | 2021-10-05 16:43:47 +0900 | [diff] [blame] | 50 | const PACKAGE_MANAGER_NATIVE_SERVICE: &str = "package_native"; |
| 51 | |
Jooyung Han | 73bac24 | 2021-07-02 10:25:49 +0900 | [diff] [blame] | 52 | /// Represents the list of APEXes |
Jooyung Han | 743e0d6 | 2022-11-07 20:57:48 +0900 | [diff] [blame^] | 53 | #[derive(Clone, Debug, Deserialize, Eq, PartialEq)] |
Jooyung Han | 9900f3d | 2021-07-06 10:27:54 +0900 | [diff] [blame] | 54 | struct ApexInfoList { |
Jooyung Han | 44b02ab | 2021-07-16 03:19:13 +0900 | [diff] [blame] | 55 | #[serde(rename = "apex-info")] |
Jooyung Han | 73bac24 | 2021-07-02 10:25:49 +0900 | [diff] [blame] | 56 | list: Vec<ApexInfo>, |
| 57 | } |
| 58 | |
Jooyung Han | 5ce867a | 2022-01-28 03:18:38 +0900 | [diff] [blame] | 59 | #[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq)] |
Jooyung Han | 73bac24 | 2021-07-02 10:25:49 +0900 | [diff] [blame] | 60 | struct ApexInfo { |
Jooyung Han | 44b02ab | 2021-07-16 03:19:13 +0900 | [diff] [blame] | 61 | #[serde(rename = "moduleName")] |
Jooyung Han | 73bac24 | 2021-07-02 10:25:49 +0900 | [diff] [blame] | 62 | name: String, |
Jooyung Han | 44b02ab | 2021-07-16 03:19:13 +0900 | [diff] [blame] | 63 | #[serde(rename = "modulePath")] |
Jooyung Han | 73bac24 | 2021-07-02 10:25:49 +0900 | [diff] [blame] | 64 | path: PathBuf, |
Jooyung Han | 9d7cd7d | 2021-10-12 17:44:14 +0900 | [diff] [blame] | 65 | |
| 66 | #[serde(default)] |
Alan Stokes | 46ac386 | 2021-12-21 15:31:47 +0000 | [diff] [blame] | 67 | has_classpath_jar: bool, |
Andrew Walbran | 40be9d5 | 2022-01-19 14:32:53 +0000 | [diff] [blame] | 68 | |
| 69 | // The field claims to be milliseconds but is actually seconds. |
| 70 | #[serde(rename = "lastUpdateMillis")] |
| 71 | last_update_seconds: u64, |
Jiyong Park | d650235 | 2022-01-27 01:07:30 +0900 | [diff] [blame] | 72 | |
| 73 | #[serde(rename = "isFactory")] |
| 74 | is_factory: bool, |
Jooyung Han | ec78804 | 2022-01-27 22:28:37 +0900 | [diff] [blame] | 75 | |
| 76 | #[serde(rename = "isActive")] |
| 77 | is_active: bool, |
Jooyung Han | 5ce867a | 2022-01-28 03:18:38 +0900 | [diff] [blame] | 78 | |
| 79 | #[serde(rename = "provideSharedApexLibs")] |
| 80 | provide_shared_apex_libs: bool, |
Jooyung Han | 73bac24 | 2021-07-02 10:25:49 +0900 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | impl ApexInfoList { |
| 84 | /// Loads ApexInfoList |
Jooyung Han | 9900f3d | 2021-07-06 10:27:54 +0900 | [diff] [blame] | 85 | fn load() -> Result<&'static ApexInfoList> { |
| 86 | static INSTANCE: OnceCell<ApexInfoList> = OnceCell::new(); |
| 87 | INSTANCE.get_or_try_init(|| { |
Jooyung Han | 44b02ab | 2021-07-16 03:19:13 +0900 | [diff] [blame] | 88 | let apex_info_list = File::open(APEX_INFO_LIST_PATH) |
| 89 | .context(format!("Failed to open {}", APEX_INFO_LIST_PATH))?; |
Jooyung Han | 9d7cd7d | 2021-10-12 17:44:14 +0900 | [diff] [blame] | 90 | let mut apex_info_list: ApexInfoList = from_reader(apex_info_list) |
Jooyung Han | 44b02ab | 2021-07-16 03:19:13 +0900 | [diff] [blame] | 91 | .context(format!("Failed to parse {}", APEX_INFO_LIST_PATH))?; |
Jooyung Han | 9d7cd7d | 2021-10-12 17:44:14 +0900 | [diff] [blame] | 92 | |
Alan Stokes | bf20c6a | 2022-01-04 12:30:50 +0000 | [diff] [blame] | 93 | // For active APEXes, we run derive_classpath and parse its output to see if it |
| 94 | // contributes to the classpath(s). (This allows us to handle any new classpath env |
| 95 | // vars seamlessly.) |
| 96 | let classpath_vars = run_derive_classpath()?; |
| 97 | let classpath_apexes = find_apex_names_in_classpath(&classpath_vars)?; |
| 98 | |
Jooyung Han | 9d7cd7d | 2021-10-12 17:44:14 +0900 | [diff] [blame] | 99 | for apex_info in apex_info_list.list.iter_mut() { |
Alan Stokes | bf20c6a | 2022-01-04 12:30:50 +0000 | [diff] [blame] | 100 | apex_info.has_classpath_jar = classpath_apexes.contains(&apex_info.name); |
Jooyung Han | 9d7cd7d | 2021-10-12 17:44:14 +0900 | [diff] [blame] | 101 | } |
Alan Stokes | bf20c6a | 2022-01-04 12:30:50 +0000 | [diff] [blame] | 102 | |
Jooyung Han | 44b02ab | 2021-07-16 03:19:13 +0900 | [diff] [blame] | 103 | Ok(apex_info_list) |
Jooyung Han | 9900f3d | 2021-07-06 10:27:54 +0900 | [diff] [blame] | 104 | }) |
Jooyung Han | 73bac24 | 2021-07-02 10:25:49 +0900 | [diff] [blame] | 105 | } |
Jooyung Han | 743e0d6 | 2022-11-07 20:57:48 +0900 | [diff] [blame^] | 106 | |
| 107 | // Override apex info with the staged one |
| 108 | fn override_staged_apex(&mut self, staged_apex_info: &StagedApexInfo) -> Result<()> { |
| 109 | let mut need_to_add: Option<ApexInfo> = None; |
| 110 | for apex_info in self.list.iter_mut() { |
| 111 | if staged_apex_info.moduleName == apex_info.name { |
| 112 | if apex_info.is_active && apex_info.is_factory { |
| 113 | // Copy the entry to the end as factory/non-active after the loop |
| 114 | // to keep the factory version. Typically this step is unncessary, |
| 115 | // but some apexes (like sharedlibs) need to be kept even if it's inactive. |
| 116 | need_to_add.replace(ApexInfo { is_active: false, ..apex_info.clone() }); |
| 117 | // And make this one as non-factory. Note that this one is still active |
| 118 | // and overridden right below. |
| 119 | apex_info.is_factory = false; |
| 120 | } |
| 121 | // Active one is overridden with the staged one. |
| 122 | if apex_info.is_active { |
| 123 | apex_info.path = PathBuf::from(&staged_apex_info.diskImagePath); |
| 124 | apex_info.has_classpath_jar = staged_apex_info.hasClassPathJars; |
| 125 | apex_info.last_update_seconds = last_updated(&apex_info.path)?; |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | if let Some(info) = need_to_add { |
| 130 | self.list.push(info); |
| 131 | } |
| 132 | Ok(()) |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | fn last_updated<P: AsRef<Path>>(path: P) -> Result<u64> { |
| 137 | let metadata = metadata(path)?; |
| 138 | Ok(metadata.modified()?.duration_since(SystemTime::UNIX_EPOCH)?.as_secs()) |
Jooyung Han | ec78804 | 2022-01-27 22:28:37 +0900 | [diff] [blame] | 139 | } |
Jooyung Han | 73bac24 | 2021-07-02 10:25:49 +0900 | [diff] [blame] | 140 | |
Jooyung Han | ec78804 | 2022-01-27 22:28:37 +0900 | [diff] [blame] | 141 | impl ApexInfo { |
| 142 | fn matches(&self, apex_config: &ApexConfig) -> bool { |
| 143 | // Match with pseudo name "{CLASSPATH}" which represents APEXes contributing |
| 144 | // to any derive_classpath environment variable |
| 145 | if apex_config.name == "{CLASSPATH}" && self.has_classpath_jar { |
| 146 | return true; |
| 147 | } |
| 148 | if apex_config.name == self.name { |
| 149 | return true; |
| 150 | } |
| 151 | false |
Jooyung Han | 73bac24 | 2021-07-02 10:25:49 +0900 | [diff] [blame] | 152 | } |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 153 | } |
| 154 | |
Jooyung Han | 5dc4217 | 2021-10-05 16:43:47 +0900 | [diff] [blame] | 155 | struct PackageManager { |
Jooyung Han | 5dc4217 | 2021-10-05 16:43:47 +0900 | [diff] [blame] | 156 | apex_info_list: &'static ApexInfoList, |
| 157 | } |
| 158 | |
| 159 | impl PackageManager { |
| 160 | fn new() -> Result<Self> { |
Jooyung Han | 5dc4217 | 2021-10-05 16:43:47 +0900 | [diff] [blame] | 161 | let apex_info_list = ApexInfoList::load()?; |
Jooyung Han | 53cf799 | 2021-10-18 19:38:41 +0900 | [diff] [blame] | 162 | Ok(Self { apex_info_list }) |
Jooyung Han | 5dc4217 | 2021-10-05 16:43:47 +0900 | [diff] [blame] | 163 | } |
| 164 | |
Jooyung Han | 9d7cd7d | 2021-10-12 17:44:14 +0900 | [diff] [blame] | 165 | fn get_apex_list(&self, prefer_staged: bool) -> Result<ApexInfoList> { |
Jooyung Han | 53cf799 | 2021-10-18 19:38:41 +0900 | [diff] [blame] | 166 | // get the list of active apexes |
Jooyung Han | 9d7cd7d | 2021-10-12 17:44:14 +0900 | [diff] [blame] | 167 | let mut list = self.apex_info_list.clone(); |
Jooyung Han | 53cf799 | 2021-10-18 19:38:41 +0900 | [diff] [blame] | 168 | // When prefer_staged, we override ApexInfo by consulting "package_native" |
Jooyung Han | 5dc4217 | 2021-10-05 16:43:47 +0900 | [diff] [blame] | 169 | if prefer_staged { |
Jooyung Han | 53cf799 | 2021-10-18 19:38:41 +0900 | [diff] [blame] | 170 | let pm = |
| 171 | wait_for_interface::<dyn IPackageManagerNative>(PACKAGE_MANAGER_NATIVE_SERVICE) |
| 172 | .context("Failed to get service when prefer_staged is set.")?; |
Alan Stokes | 70ccf16 | 2022-07-08 11:05:03 +0100 | [diff] [blame] | 173 | let staged = |
| 174 | pm.getStagedApexModuleNames().context("getStagedApexModuleNames failed")?; |
Jooyung Han | 743e0d6 | 2022-11-07 20:57:48 +0900 | [diff] [blame^] | 175 | for name in staged { |
| 176 | if let Some(staged_apex_info) = |
| 177 | pm.getStagedApexInfo(&name).context("getStagedApexInfo failed")? |
| 178 | { |
| 179 | list.override_staged_apex(&staged_apex_info)?; |
Jooyung Han | 9d7cd7d | 2021-10-12 17:44:14 +0900 | [diff] [blame] | 180 | } |
Jooyung Han | 5dc4217 | 2021-10-05 16:43:47 +0900 | [diff] [blame] | 181 | } |
| 182 | } |
Jooyung Han | 9d7cd7d | 2021-10-12 17:44:14 +0900 | [diff] [blame] | 183 | Ok(list) |
Jooyung Han | 5dc4217 | 2021-10-05 16:43:47 +0900 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | |
Andrew Walbran | cc0db52 | 2021-07-12 17:03:42 +0000 | [diff] [blame] | 187 | fn make_metadata_file( |
Alan Stokes | 0d1ef78 | 2022-09-27 13:46:35 +0100 | [diff] [blame] | 188 | app_config: &VirtualMachineAppConfig, |
Jooyung Han | ec78804 | 2022-01-27 22:28:37 +0900 | [diff] [blame] | 189 | apex_infos: &[&ApexInfo], |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 190 | temporary_directory: &Path, |
Andrew Walbran | cc0db52 | 2021-07-12 17:03:42 +0000 | [diff] [blame] | 191 | ) -> Result<ParcelFileDescriptor> { |
Alan Stokes | 0d1ef78 | 2022-09-27 13:46:35 +0100 | [diff] [blame] | 192 | let payload_metadata = match &app_config.payload { |
| 193 | Payload::PayloadConfig(payload_config) => PayloadMetadata::config(PayloadConfig { |
| 194 | payload_binary_path: payload_config.payloadPath.clone(), |
Alan Stokes | 0d1ef78 | 2022-09-27 13:46:35 +0100 | [diff] [blame] | 195 | ..Default::default() |
| 196 | }), |
| 197 | Payload::ConfigPath(config_path) => { |
| 198 | PayloadMetadata::config_path(format!("/mnt/apk/{}", config_path)) |
| 199 | } |
| 200 | }; |
| 201 | |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 202 | let metadata = Metadata { |
Andrew Walbran | cc0db52 | 2021-07-12 17:03:42 +0000 | [diff] [blame] | 203 | version: 1, |
Jooyung Han | ec78804 | 2022-01-27 22:28:37 +0900 | [diff] [blame] | 204 | apexes: apex_infos |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 205 | .iter() |
Jooyung Han | 19c1d6c | 2021-08-06 14:08:16 +0900 | [diff] [blame] | 206 | .enumerate() |
Jooyung Han | ec78804 | 2022-01-27 22:28:37 +0900 | [diff] [blame] | 207 | .map(|(i, apex_info)| { |
Andrew Walbran | 40be9d5 | 2022-01-19 14:32:53 +0000 | [diff] [blame] | 208 | Ok(ApexPayload { |
Jooyung Han | ec78804 | 2022-01-27 22:28:37 +0900 | [diff] [blame] | 209 | name: apex_info.name.clone(), |
Andrew Walbran | 40be9d5 | 2022-01-19 14:32:53 +0000 | [diff] [blame] | 210 | partition_name: format!("microdroid-apex-{}", i), |
Jiyong Park | d650235 | 2022-01-27 01:07:30 +0900 | [diff] [blame] | 211 | last_update_seconds: apex_info.last_update_seconds, |
| 212 | is_factory: apex_info.is_factory, |
Andrew Walbran | 40be9d5 | 2022-01-19 14:32:53 +0000 | [diff] [blame] | 213 | ..Default::default() |
| 214 | }) |
Jooyung Han | 19c1d6c | 2021-08-06 14:08:16 +0900 | [diff] [blame] | 215 | }) |
Andrew Walbran | 40be9d5 | 2022-01-19 14:32:53 +0000 | [diff] [blame] | 216 | .collect::<Result<_>>()?, |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 217 | apk: Some(ApkPayload { |
Jooyung Han | 35edb8f | 2021-07-01 16:17:16 +0900 | [diff] [blame] | 218 | name: "apk".to_owned(), |
| 219 | payload_partition_name: "microdroid-apk".to_owned(), |
| 220 | idsig_partition_name: "microdroid-apk-idsig".to_owned(), |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 221 | ..Default::default() |
| 222 | }) |
| 223 | .into(), |
Alan Stokes | 0d1ef78 | 2022-09-27 13:46:35 +0100 | [diff] [blame] | 224 | payload: Some(payload_metadata), |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 225 | ..Default::default() |
| 226 | }; |
Andrew Walbran | cc0db52 | 2021-07-12 17:03:42 +0000 | [diff] [blame] | 227 | |
| 228 | // Write metadata to file. |
Alan Stokes | 0d1ef78 | 2022-09-27 13:46:35 +0100 | [diff] [blame] | 229 | let metadata_path = temporary_directory.join("metadata"); |
Andrew Walbran | cc0db52 | 2021-07-12 17:03:42 +0000 | [diff] [blame] | 230 | let mut metadata_file = OpenOptions::new() |
| 231 | .create_new(true) |
| 232 | .read(true) |
| 233 | .write(true) |
| 234 | .open(&metadata_path) |
| 235 | .with_context(|| format!("Failed to open metadata file {:?}", metadata_path))?; |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 236 | microdroid_metadata::write_metadata(&metadata, &mut metadata_file)?; |
| 237 | |
Andrew Walbran | cc0db52 | 2021-07-12 17:03:42 +0000 | [diff] [blame] | 238 | // Re-open the metadata file as read-only. |
| 239 | open_parcel_file(&metadata_path, false) |
| 240 | } |
| 241 | |
| 242 | /// Creates a DiskImage with partitions: |
Alan Stokes | 53cc5ca | 2022-08-30 14:28:19 +0100 | [diff] [blame] | 243 | /// payload-metadata: metadata |
Andrew Walbran | cc0db52 | 2021-07-12 17:03:42 +0000 | [diff] [blame] | 244 | /// microdroid-apex-0: apex 0 |
| 245 | /// microdroid-apex-1: apex 1 |
| 246 | /// .. |
| 247 | /// microdroid-apk: apk |
| 248 | /// microdroid-apk-idsig: idsig |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 249 | /// extra-apk-0: additional apk 0 |
| 250 | /// extra-idsig-0: additional idsig 0 |
| 251 | /// extra-apk-1: additional apk 1 |
| 252 | /// extra-idsig-1: additional idsig 1 |
| 253 | /// .. |
Andrew Walbran | cc0db52 | 2021-07-12 17:03:42 +0000 | [diff] [blame] | 254 | fn make_payload_disk( |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 255 | app_config: &VirtualMachineAppConfig, |
Andrew Walbran | cc0db52 | 2021-07-12 17:03:42 +0000 | [diff] [blame] | 256 | apk_file: File, |
| 257 | idsig_file: File, |
Jooyung Han | 9d7cd7d | 2021-10-12 17:44:14 +0900 | [diff] [blame] | 258 | vm_payload_config: &VmPayloadConfig, |
Andrew Walbran | cc0db52 | 2021-07-12 17:03:42 +0000 | [diff] [blame] | 259 | temporary_directory: &Path, |
| 260 | ) -> Result<DiskImage> { |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 261 | if vm_payload_config.extra_apks.len() != app_config.extraIdsigs.len() { |
| 262 | bail!( |
| 263 | "payload config has {} apks, but app config has {} idsigs", |
| 264 | vm_payload_config.extra_apks.len(), |
| 265 | app_config.extraIdsigs.len() |
| 266 | ); |
| 267 | } |
| 268 | |
Jooyung Han | 9d7cd7d | 2021-10-12 17:44:14 +0900 | [diff] [blame] | 269 | let pm = PackageManager::new()?; |
| 270 | let apex_list = pm.get_apex_list(vm_payload_config.prefer_staged)?; |
| 271 | |
Jooyung Han | ec78804 | 2022-01-27 22:28:37 +0900 | [diff] [blame] | 272 | // collect APEXes from config |
| 273 | let apex_infos = |
| 274 | collect_apex_infos(&apex_list, &vm_payload_config.apexes, app_config.debugLevel); |
| 275 | info!("Microdroid payload APEXes: {:?}", apex_infos.iter().map(|ai| &ai.name)); |
Jooyung Han | 9d7cd7d | 2021-10-12 17:44:14 +0900 | [diff] [blame] | 276 | |
Alan Stokes | 0d1ef78 | 2022-09-27 13:46:35 +0100 | [diff] [blame] | 277 | let metadata_file = make_metadata_file(app_config, &apex_infos, temporary_directory)?; |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 278 | // put metadata at the first partition |
| 279 | let mut partitions = vec![Partition { |
Jooyung Han | 14e5a8e | 2021-07-06 20:48:38 +0900 | [diff] [blame] | 280 | label: "payload-metadata".to_owned(), |
Jooyung Han | 631d588 | 2021-07-29 06:34:05 +0900 | [diff] [blame] | 281 | image: Some(metadata_file), |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 282 | writable: false, |
| 283 | }]; |
| 284 | |
Jooyung Han | ec78804 | 2022-01-27 22:28:37 +0900 | [diff] [blame] | 285 | for (i, apex_info) in apex_infos.iter().enumerate() { |
| 286 | let apex_file = open_parcel_file(&apex_info.path, false)?; |
Jooyung Han | 9588463 | 2021-07-06 22:27:54 +0900 | [diff] [blame] | 287 | partitions.push(Partition { |
| 288 | label: format!("microdroid-apex-{}", i), |
Jooyung Han | 631d588 | 2021-07-29 06:34:05 +0900 | [diff] [blame] | 289 | image: Some(apex_file), |
Jooyung Han | 9588463 | 2021-07-06 22:27:54 +0900 | [diff] [blame] | 290 | writable: false, |
| 291 | }); |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 292 | } |
Jooyung Han | 9588463 | 2021-07-06 22:27:54 +0900 | [diff] [blame] | 293 | partitions.push(Partition { |
| 294 | label: "microdroid-apk".to_owned(), |
Jooyung Han | 631d588 | 2021-07-29 06:34:05 +0900 | [diff] [blame] | 295 | image: Some(ParcelFileDescriptor::new(apk_file)), |
Jooyung Han | 9588463 | 2021-07-06 22:27:54 +0900 | [diff] [blame] | 296 | writable: false, |
| 297 | }); |
| 298 | partitions.push(Partition { |
| 299 | label: "microdroid-apk-idsig".to_owned(), |
Jooyung Han | 631d588 | 2021-07-29 06:34:05 +0900 | [diff] [blame] | 300 | image: Some(ParcelFileDescriptor::new(idsig_file)), |
Jooyung Han | 9588463 | 2021-07-06 22:27:54 +0900 | [diff] [blame] | 301 | writable: false, |
| 302 | }); |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 303 | |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 304 | // we've already checked that extra_apks and extraIdsigs are in the same size. |
| 305 | let extra_apks = &vm_payload_config.extra_apks; |
| 306 | let extra_idsigs = &app_config.extraIdsigs; |
| 307 | for (i, (extra_apk, extra_idsig)) in extra_apks.iter().zip(extra_idsigs.iter()).enumerate() { |
| 308 | partitions.push(Partition { |
| 309 | label: format!("extra-apk-{}", i), |
Victor Hsieh | 14497f0 | 2022-09-21 10:10:16 -0700 | [diff] [blame] | 310 | image: Some(ParcelFileDescriptor::new( |
| 311 | File::open(PathBuf::from(&extra_apk.path)).with_context(|| { |
| 312 | format!("Failed to open the extra apk #{} {}", i, extra_apk.path) |
| 313 | })?, |
| 314 | )), |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 315 | writable: false, |
| 316 | }); |
| 317 | |
| 318 | partitions.push(Partition { |
| 319 | label: format!("extra-idsig-{}", i), |
Victor Hsieh | 14497f0 | 2022-09-21 10:10:16 -0700 | [diff] [blame] | 320 | image: Some(ParcelFileDescriptor::new( |
| 321 | extra_idsig |
| 322 | .as_ref() |
| 323 | .try_clone() |
| 324 | .with_context(|| format!("Failed to clone the extra idsig #{}", i))?, |
| 325 | )), |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 326 | writable: false, |
| 327 | }); |
| 328 | } |
| 329 | |
Jooyung Han | 21e9b92 | 2021-06-26 04:14:16 +0900 | [diff] [blame] | 330 | Ok(DiskImage { image: None, partitions, writable: false }) |
| 331 | } |
Andrew Walbran | cc0db52 | 2021-07-12 17:03:42 +0000 | [diff] [blame] | 332 | |
Alan Stokes | bf20c6a | 2022-01-04 12:30:50 +0000 | [diff] [blame] | 333 | fn run_derive_classpath() -> Result<String> { |
| 334 | let result = Command::new("/apex/com.android.sdkext/bin/derive_classpath") |
| 335 | .arg("/proc/self/fd/1") |
| 336 | .output() |
| 337 | .context("Failed to run derive_classpath")?; |
| 338 | |
| 339 | if !result.status.success() { |
| 340 | bail!("derive_classpath returned {}", result.status); |
| 341 | } |
| 342 | |
| 343 | String::from_utf8(result.stdout).context("Converting derive_classpath output") |
| 344 | } |
| 345 | |
| 346 | fn find_apex_names_in_classpath(classpath_vars: &str) -> Result<HashSet<String>> { |
| 347 | // Each line should be in the format "export <var name> <paths>", where <paths> is a |
| 348 | // colon-separated list of paths to JARs. We don't care about the var names, and we're only |
| 349 | // interested in paths that look like "/apex/<apex name>/<anything>" so we know which APEXes |
| 350 | // contribute to at least one var. |
| 351 | let mut apexes = HashSet::new(); |
| 352 | |
| 353 | let pattern = Regex::new(r"^export [^ ]+ ([^ ]+)$").context("Failed to construct Regex")?; |
| 354 | for line in classpath_vars.lines() { |
| 355 | if let Some(captures) = pattern.captures(line) { |
| 356 | if let Some(paths) = captures.get(1) { |
| 357 | apexes.extend(paths.as_str().split(':').filter_map(|path| { |
| 358 | let path = path.strip_prefix("/apex/")?; |
| 359 | Some(path[..path.find('/')?].to_owned()) |
| 360 | })); |
| 361 | continue; |
| 362 | } |
| 363 | } |
| 364 | warn!("Malformed line from derive_classpath: {}", line); |
| 365 | } |
| 366 | |
| 367 | Ok(apexes) |
Jooyung Han | 5e0f206 | 2021-10-12 14:00:46 +0900 | [diff] [blame] | 368 | } |
| 369 | |
Jooyung Han | ec78804 | 2022-01-27 22:28:37 +0900 | [diff] [blame] | 370 | // Collect ApexInfos from VM config |
| 371 | fn collect_apex_infos<'a>( |
| 372 | apex_list: &'a ApexInfoList, |
| 373 | apex_configs: &[ApexConfig], |
Inseob Kim | b4868e6 | 2021-11-09 17:28:23 +0900 | [diff] [blame] | 374 | debug_level: DebugLevel, |
Jooyung Han | ec78804 | 2022-01-27 22:28:37 +0900 | [diff] [blame] | 375 | ) -> Vec<&'a ApexInfo> { |
| 376 | let mut additional_apexes: Vec<&str> = MICRODROID_REQUIRED_APEXES.to_vec(); |
Inseob Kim | b4868e6 | 2021-11-09 17:28:23 +0900 | [diff] [blame] | 377 | if debug_level != DebugLevel::NONE { |
Jooyung Han | ec78804 | 2022-01-27 22:28:37 +0900 | [diff] [blame] | 378 | additional_apexes.extend(MICRODROID_REQUIRED_APEXES_DEBUG.to_vec()); |
Inseob Kim | b4868e6 | 2021-11-09 17:28:23 +0900 | [diff] [blame] | 379 | } |
Jooyung Han | ec78804 | 2022-01-27 22:28:37 +0900 | [diff] [blame] | 380 | |
| 381 | apex_list |
| 382 | .list |
| 383 | .iter() |
| 384 | .filter(|ai| { |
| 385 | apex_configs.iter().any(|cfg| ai.matches(cfg) && ai.is_active) |
| 386 | || additional_apexes.iter().any(|name| name == &ai.name && ai.is_active) |
Jooyung Han | 5ce867a | 2022-01-28 03:18:38 +0900 | [diff] [blame] | 387 | || ai.provide_shared_apex_libs |
Jooyung Han | ec78804 | 2022-01-27 22:28:37 +0900 | [diff] [blame] | 388 | }) |
| 389 | .collect() |
Jooyung Han | 5e0f206 | 2021-10-12 14:00:46 +0900 | [diff] [blame] | 390 | } |
| 391 | |
Shikha Panwar | 22e7045 | 2022-10-10 18:32:55 +0000 | [diff] [blame] | 392 | pub fn add_microdroid_system_images( |
Andrew Walbran | cc0db52 | 2021-07-12 17:03:42 +0000 | [diff] [blame] | 393 | config: &VirtualMachineAppConfig, |
Jiyong Park | 8d08181 | 2021-07-23 17:45:04 +0900 | [diff] [blame] | 394 | instance_file: File, |
Shikha Panwar | 22e7045 | 2022-10-10 18:32:55 +0000 | [diff] [blame] | 395 | storage_image: Option<File>, |
Andrew Walbran | cc0db52 | 2021-07-12 17:03:42 +0000 | [diff] [blame] | 396 | vm_config: &mut VirtualMachineRawConfig, |
| 397 | ) -> Result<()> { |
Shikha Panwar | ed8ace4 | 2022-09-28 12:52:16 +0000 | [diff] [blame] | 398 | let debug_suffix = match config.debugLevel { |
| 399 | DebugLevel::NONE => "normal", |
| 400 | DebugLevel::APP_ONLY => "app_debuggable", |
| 401 | DebugLevel::FULL => "full_debuggable", |
| 402 | _ => return Err(anyhow!("unsupported debug level: {:?}", config.debugLevel)), |
| 403 | }; |
| 404 | let initrd = format!("/apex/com.android.virt/etc/microdroid_initrd_{}.img", debug_suffix); |
| 405 | vm_config.initrd = Some(open_parcel_file(Path::new(&initrd), false)?); |
| 406 | |
Shikha Panwar | 22e7045 | 2022-10-10 18:32:55 +0000 | [diff] [blame] | 407 | let mut writable_partitions = vec![Partition { |
Shikha Panwar | ed8ace4 | 2022-09-28 12:52:16 +0000 | [diff] [blame] | 408 | label: "vm-instance".to_owned(), |
| 409 | image: Some(ParcelFileDescriptor::new(instance_file)), |
| 410 | writable: true, |
Shikha Panwar | 22e7045 | 2022-10-10 18:32:55 +0000 | [diff] [blame] | 411 | }]; |
| 412 | |
| 413 | if let Some(file) = storage_image { |
| 414 | writable_partitions.push(Partition { |
| 415 | label: "encrypted-storage".to_owned(), |
| 416 | image: Some(ParcelFileDescriptor::new(file)), |
| 417 | writable: true, |
| 418 | }); |
| 419 | } |
| 420 | |
| 421 | vm_config.disks.push(DiskImage { |
| 422 | image: None, |
| 423 | partitions: writable_partitions, |
| 424 | writable: true, |
| 425 | }); |
| 426 | |
| 427 | Ok(()) |
| 428 | } |
| 429 | |
| 430 | pub fn add_microdroid_payload_images( |
| 431 | config: &VirtualMachineAppConfig, |
| 432 | temporary_directory: &Path, |
| 433 | apk_file: File, |
| 434 | idsig_file: File, |
| 435 | vm_payload_config: &VmPayloadConfig, |
| 436 | vm_config: &mut VirtualMachineRawConfig, |
| 437 | ) -> Result<()> { |
Andrew Walbran | cc0db52 | 2021-07-12 17:03:42 +0000 | [diff] [blame] | 438 | vm_config.disks.push(make_payload_disk( |
Inseob Kim | a5a262f | 2021-11-17 19:41:03 +0900 | [diff] [blame] | 439 | config, |
Andrew Walbran | cc0db52 | 2021-07-12 17:03:42 +0000 | [diff] [blame] | 440 | apk_file, |
| 441 | idsig_file, |
Jooyung Han | 9d7cd7d | 2021-10-12 17:44:14 +0900 | [diff] [blame] | 442 | vm_payload_config, |
Andrew Walbran | cc0db52 | 2021-07-12 17:03:42 +0000 | [diff] [blame] | 443 | temporary_directory, |
| 444 | )?); |
| 445 | |
Andrew Walbran | cc0db52 | 2021-07-12 17:03:42 +0000 | [diff] [blame] | 446 | Ok(()) |
| 447 | } |
Jooyung Han | 5e0f206 | 2021-10-12 14:00:46 +0900 | [diff] [blame] | 448 | |
| 449 | #[cfg(test)] |
| 450 | mod tests { |
| 451 | use super::*; |
Jooyung Han | 743e0d6 | 2022-11-07 20:57:48 +0900 | [diff] [blame^] | 452 | use tempfile::NamedTempFile; |
Andrew Walbran | c1a5f5a | 2022-01-19 13:38:13 +0000 | [diff] [blame] | 453 | |
Jooyung Han | 5e0f206 | 2021-10-12 14:00:46 +0900 | [diff] [blame] | 454 | #[test] |
Alan Stokes | bf20c6a | 2022-01-04 12:30:50 +0000 | [diff] [blame] | 455 | fn test_find_apex_names_in_classpath() { |
| 456 | let vars = r#" |
| 457 | export FOO /apex/unterminated |
| 458 | export BAR /apex/valid.apex/something |
| 459 | wrong |
| 460 | export EMPTY |
| 461 | export OTHER /foo/bar:/baz:/apex/second.valid.apex/:gibberish:"#; |
| 462 | let expected = vec!["valid.apex", "second.valid.apex"]; |
| 463 | let expected: HashSet<_> = expected.into_iter().map(ToString::to_string).collect(); |
| 464 | |
| 465 | assert_eq!(find_apex_names_in_classpath(vars).unwrap(), expected); |
Jooyung Han | 5e0f206 | 2021-10-12 14:00:46 +0900 | [diff] [blame] | 466 | } |
Andrew Walbran | c1a5f5a | 2022-01-19 13:38:13 +0000 | [diff] [blame] | 467 | |
| 468 | #[test] |
Jooyung Han | ec78804 | 2022-01-27 22:28:37 +0900 | [diff] [blame] | 469 | fn test_collect_apexes() { |
| 470 | let apex_info_list = ApexInfoList { |
Andrew Walbran | c1a5f5a | 2022-01-19 13:38:13 +0000 | [diff] [blame] | 471 | list: vec![ |
| 472 | ApexInfo { |
Jooyung Han | ec78804 | 2022-01-27 22:28:37 +0900 | [diff] [blame] | 473 | // 0 |
| 474 | name: "com.android.adbd".to_string(), |
| 475 | path: PathBuf::from("adbd"), |
Andrew Walbran | c1a5f5a | 2022-01-19 13:38:13 +0000 | [diff] [blame] | 476 | has_classpath_jar: false, |
Andrew Walbran | 40be9d5 | 2022-01-19 14:32:53 +0000 | [diff] [blame] | 477 | last_update_seconds: 12345678, |
Jiyong Park | d650235 | 2022-01-27 01:07:30 +0900 | [diff] [blame] | 478 | is_factory: true, |
Jooyung Han | ec78804 | 2022-01-27 22:28:37 +0900 | [diff] [blame] | 479 | is_active: true, |
Jooyung Han | 5ce867a | 2022-01-28 03:18:38 +0900 | [diff] [blame] | 480 | ..Default::default() |
Andrew Walbran | c1a5f5a | 2022-01-19 13:38:13 +0000 | [diff] [blame] | 481 | }, |
| 482 | ApexInfo { |
Jooyung Han | ec78804 | 2022-01-27 22:28:37 +0900 | [diff] [blame] | 483 | // 1 |
| 484 | name: "com.android.os.statsd".to_string(), |
| 485 | path: PathBuf::from("statsd"), |
| 486 | has_classpath_jar: false, |
| 487 | last_update_seconds: 12345678, |
| 488 | is_factory: true, |
| 489 | is_active: false, |
Jooyung Han | 5ce867a | 2022-01-28 03:18:38 +0900 | [diff] [blame] | 490 | ..Default::default() |
Jooyung Han | ec78804 | 2022-01-27 22:28:37 +0900 | [diff] [blame] | 491 | }, |
| 492 | ApexInfo { |
| 493 | // 2 |
| 494 | name: "com.android.os.statsd".to_string(), |
| 495 | path: PathBuf::from("statsd/updated"), |
| 496 | has_classpath_jar: false, |
| 497 | last_update_seconds: 12345678 + 1, |
| 498 | is_factory: false, |
| 499 | is_active: true, |
Jooyung Han | 5ce867a | 2022-01-28 03:18:38 +0900 | [diff] [blame] | 500 | ..Default::default() |
Jooyung Han | ec78804 | 2022-01-27 22:28:37 +0900 | [diff] [blame] | 501 | }, |
| 502 | ApexInfo { |
| 503 | // 3 |
| 504 | name: "no_classpath".to_string(), |
| 505 | path: PathBuf::from("no_classpath"), |
| 506 | has_classpath_jar: false, |
| 507 | last_update_seconds: 12345678, |
| 508 | is_factory: true, |
| 509 | is_active: true, |
Jooyung Han | 5ce867a | 2022-01-28 03:18:38 +0900 | [diff] [blame] | 510 | ..Default::default() |
Jooyung Han | ec78804 | 2022-01-27 22:28:37 +0900 | [diff] [blame] | 511 | }, |
| 512 | ApexInfo { |
| 513 | // 4 |
Andrew Walbran | c1a5f5a | 2022-01-19 13:38:13 +0000 | [diff] [blame] | 514 | name: "has_classpath".to_string(), |
Jooyung Han | ec78804 | 2022-01-27 22:28:37 +0900 | [diff] [blame] | 515 | path: PathBuf::from("has_classpath"), |
Andrew Walbran | c1a5f5a | 2022-01-19 13:38:13 +0000 | [diff] [blame] | 516 | has_classpath_jar: true, |
Andrew Walbran | 40be9d5 | 2022-01-19 14:32:53 +0000 | [diff] [blame] | 517 | last_update_seconds: 87654321, |
Jooyung Han | ec78804 | 2022-01-27 22:28:37 +0900 | [diff] [blame] | 518 | is_factory: true, |
| 519 | is_active: false, |
Jooyung Han | 5ce867a | 2022-01-28 03:18:38 +0900 | [diff] [blame] | 520 | ..Default::default() |
Jooyung Han | ec78804 | 2022-01-27 22:28:37 +0900 | [diff] [blame] | 521 | }, |
| 522 | ApexInfo { |
| 523 | // 5 |
| 524 | name: "has_classpath".to_string(), |
| 525 | path: PathBuf::from("has_classpath/updated"), |
| 526 | has_classpath_jar: true, |
| 527 | last_update_seconds: 87654321 + 1, |
Jiyong Park | d650235 | 2022-01-27 01:07:30 +0900 | [diff] [blame] | 528 | is_factory: false, |
Jooyung Han | ec78804 | 2022-01-27 22:28:37 +0900 | [diff] [blame] | 529 | is_active: true, |
Jooyung Han | 5ce867a | 2022-01-28 03:18:38 +0900 | [diff] [blame] | 530 | ..Default::default() |
Jooyung Han | ec78804 | 2022-01-27 22:28:37 +0900 | [diff] [blame] | 531 | }, |
| 532 | ApexInfo { |
| 533 | // 6 |
| 534 | name: "apex-foo".to_string(), |
| 535 | path: PathBuf::from("apex-foo"), |
| 536 | has_classpath_jar: false, |
| 537 | last_update_seconds: 87654321, |
| 538 | is_factory: true, |
| 539 | is_active: false, |
Jooyung Han | 5ce867a | 2022-01-28 03:18:38 +0900 | [diff] [blame] | 540 | ..Default::default() |
Jooyung Han | ec78804 | 2022-01-27 22:28:37 +0900 | [diff] [blame] | 541 | }, |
| 542 | ApexInfo { |
| 543 | // 7 |
| 544 | name: "apex-foo".to_string(), |
| 545 | path: PathBuf::from("apex-foo/updated"), |
| 546 | has_classpath_jar: false, |
| 547 | last_update_seconds: 87654321 + 1, |
| 548 | is_factory: false, |
| 549 | is_active: true, |
Jooyung Han | 5ce867a | 2022-01-28 03:18:38 +0900 | [diff] [blame] | 550 | ..Default::default() |
| 551 | }, |
| 552 | ApexInfo { |
| 553 | // 8 |
| 554 | name: "sharedlibs".to_string(), |
| 555 | path: PathBuf::from("apex-foo"), |
| 556 | last_update_seconds: 87654321, |
| 557 | is_factory: true, |
| 558 | provide_shared_apex_libs: true, |
| 559 | ..Default::default() |
| 560 | }, |
| 561 | ApexInfo { |
| 562 | // 9 |
| 563 | name: "sharedlibs".to_string(), |
| 564 | path: PathBuf::from("apex-foo/updated"), |
| 565 | last_update_seconds: 87654321 + 1, |
| 566 | is_active: true, |
| 567 | provide_shared_apex_libs: true, |
| 568 | ..Default::default() |
Andrew Walbran | c1a5f5a | 2022-01-19 13:38:13 +0000 | [diff] [blame] | 569 | }, |
| 570 | ], |
| 571 | }; |
Jooyung Han | ec78804 | 2022-01-27 22:28:37 +0900 | [diff] [blame] | 572 | let apex_configs = vec![ |
| 573 | ApexConfig { name: "apex-foo".to_string() }, |
Andrew Walbran | c1a5f5a | 2022-01-19 13:38:13 +0000 | [diff] [blame] | 574 | ApexConfig { name: "{CLASSPATH}".to_string() }, |
| 575 | ]; |
| 576 | assert_eq!( |
Jooyung Han | ec78804 | 2022-01-27 22:28:37 +0900 | [diff] [blame] | 577 | collect_apex_infos(&apex_info_list, &apex_configs, DebugLevel::FULL), |
Andrew Walbran | c1a5f5a | 2022-01-19 13:38:13 +0000 | [diff] [blame] | 578 | vec![ |
Jooyung Han | 5ce867a | 2022-01-28 03:18:38 +0900 | [diff] [blame] | 579 | // Pass active/required APEXes |
Jooyung Han | ec78804 | 2022-01-27 22:28:37 +0900 | [diff] [blame] | 580 | &apex_info_list.list[0], |
| 581 | &apex_info_list.list[2], |
Jooyung Han | 5ce867a | 2022-01-28 03:18:38 +0900 | [diff] [blame] | 582 | // Pass active APEXes specified in the config |
Jooyung Han | ec78804 | 2022-01-27 22:28:37 +0900 | [diff] [blame] | 583 | &apex_info_list.list[5], |
| 584 | &apex_info_list.list[7], |
Jooyung Han | 5ce867a | 2022-01-28 03:18:38 +0900 | [diff] [blame] | 585 | // Pass both preinstalled(inactive) and updated(active) for "sharedlibs" APEXes |
| 586 | &apex_info_list.list[8], |
| 587 | &apex_info_list.list[9], |
Andrew Walbran | c1a5f5a | 2022-01-19 13:38:13 +0000 | [diff] [blame] | 588 | ] |
| 589 | ); |
| 590 | } |
Jooyung Han | 743e0d6 | 2022-11-07 20:57:48 +0900 | [diff] [blame^] | 591 | |
| 592 | #[test] |
| 593 | fn test_prefer_staged_apex_with_factory_active_apex() { |
| 594 | let single_apex = ApexInfo { |
| 595 | name: "foo".to_string(), |
| 596 | path: PathBuf::from("foo.apex"), |
| 597 | is_factory: true, |
| 598 | is_active: true, |
| 599 | ..Default::default() |
| 600 | }; |
| 601 | let mut apex_info_list = ApexInfoList { list: vec![single_apex.clone()] }; |
| 602 | |
| 603 | let staged = NamedTempFile::new().unwrap(); |
| 604 | apex_info_list |
| 605 | .override_staged_apex(&StagedApexInfo { |
| 606 | moduleName: "foo".to_string(), |
| 607 | diskImagePath: staged.path().to_string_lossy().to_string(), |
| 608 | ..Default::default() |
| 609 | }) |
| 610 | .expect("should be ok"); |
| 611 | |
| 612 | assert_eq!( |
| 613 | apex_info_list, |
| 614 | ApexInfoList { |
| 615 | list: vec![ |
| 616 | ApexInfo { |
| 617 | is_factory: false, |
| 618 | path: staged.path().to_owned(), |
| 619 | last_update_seconds: last_updated(staged.path()).unwrap(), |
| 620 | ..single_apex.clone() |
| 621 | }, |
| 622 | ApexInfo { is_active: false, ..single_apex }, |
| 623 | ], |
| 624 | } |
| 625 | ); |
| 626 | } |
| 627 | |
| 628 | #[test] |
| 629 | fn test_prefer_staged_apex_with_factory_and_inactive_apex() { |
| 630 | let factory_apex = ApexInfo { |
| 631 | name: "foo".to_string(), |
| 632 | path: PathBuf::from("foo.apex"), |
| 633 | is_factory: true, |
| 634 | ..Default::default() |
| 635 | }; |
| 636 | let active_apex = ApexInfo { |
| 637 | name: "foo".to_string(), |
| 638 | path: PathBuf::from("foo.downloaded.apex"), |
| 639 | is_active: true, |
| 640 | ..Default::default() |
| 641 | }; |
| 642 | let mut apex_info_list = |
| 643 | ApexInfoList { list: vec![factory_apex.clone(), active_apex.clone()] }; |
| 644 | |
| 645 | let staged = NamedTempFile::new().unwrap(); |
| 646 | apex_info_list |
| 647 | .override_staged_apex(&StagedApexInfo { |
| 648 | moduleName: "foo".to_string(), |
| 649 | diskImagePath: staged.path().to_string_lossy().to_string(), |
| 650 | ..Default::default() |
| 651 | }) |
| 652 | .expect("should be ok"); |
| 653 | |
| 654 | assert_eq!( |
| 655 | apex_info_list, |
| 656 | ApexInfoList { |
| 657 | list: vec![ |
| 658 | // factory apex isn't touched |
| 659 | factory_apex, |
| 660 | // update active one |
| 661 | ApexInfo { |
| 662 | path: staged.path().to_owned(), |
| 663 | last_update_seconds: last_updated(staged.path()).unwrap(), |
| 664 | ..active_apex |
| 665 | }, |
| 666 | ], |
| 667 | } |
| 668 | ); |
| 669 | } |
Jooyung Han | 5e0f206 | 2021-10-12 14:00:46 +0900 | [diff] [blame] | 670 | } |