blob: bd6bf10c18a1460e60c8b507ada8df9c1a8f3b69 [file] [log] [blame]
Jooyung Han21e9b922021-06-26 04:14:16 +09001// 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
Jaewan Kim61f86142023-03-28 15:12:52 +090017use crate::debug_config::DebugConfig;
Andrew Walbrancc0db522021-07-12 17:03:42 +000018use android_system_virtualizationservice::aidl::android::system::virtualizationservice::{
Alan Stokes0d1ef782022-09-27 13:46:35 +010019 DiskImage::DiskImage,
20 Partition::Partition,
21 VirtualMachineAppConfig::DebugLevel::DebugLevel,
22 VirtualMachineAppConfig::{Payload::Payload, VirtualMachineAppConfig},
Andrew Walbrancc0db522021-07-12 17:03:42 +000023 VirtualMachineRawConfig::VirtualMachineRawConfig,
24};
Inseob Kima5a262f2021-11-17 19:41:03 +090025use anyhow::{anyhow, bail, Context, Result};
Alan Stokes0e82b502022-08-08 14:44:48 +010026use binder::{wait_for_interface, ParcelFileDescriptor};
Alan Stokesbf20c6a2022-01-04 12:30:50 +000027use log::{info, warn};
Alan Stokes0d1ef782022-09-27 13:46:35 +010028use microdroid_metadata::{ApexPayload, ApkPayload, Metadata, PayloadConfig, PayloadMetadata};
Jooyung Han5dc42172021-10-05 16:43:47 +090029use microdroid_payload_config::{ApexConfig, VmPayloadConfig};
Jooyung Han9900f3d2021-07-06 10:27:54 +090030use once_cell::sync::OnceCell;
Jooyung Han743e0d62022-11-07 20:57:48 +090031use packagemanager_aidl::aidl::android::content::pm::{
32 IPackageManagerNative::IPackageManagerNative, StagedApexInfo::StagedApexInfo,
33};
Alan Stokesbf20c6a2022-01-04 12:30:50 +000034use regex::Regex;
Jooyung Han44b02ab2021-07-16 03:19:13 +090035use serde::Deserialize;
36use serde_xml_rs::from_reader;
Alan Stokesbf20c6a2022-01-04 12:30:50 +000037use std::collections::HashSet;
Inseob Kimecde8c02024-09-03 13:11:08 +090038use std::ffi::OsStr;
Andrew Walbran40be9d52022-01-19 14:32:53 +000039use std::fs::{metadata, File, OpenOptions};
Jooyung Han21e9b922021-06-26 04:14:16 +090040use std::path::{Path, PathBuf};
Alan Stokesbf20c6a2022-01-04 12:30:50 +000041use std::process::Command;
Andrew Walbran40be9d52022-01-19 14:32:53 +000042use std::time::SystemTime;
Andrew Walbrancc0db522021-07-12 17:03:42 +000043use vmconfig::open_parcel_file;
44
Jooyung Han44b02ab2021-07-16 03:19:13 +090045const APEX_INFO_LIST_PATH: &str = "/apex/apex-info-list.xml";
46
Jooyung Han5dc42172021-10-05 16:43:47 +090047const PACKAGE_MANAGER_NATIVE_SERVICE: &str = "package_native";
48
Jooyung Han73bac242021-07-02 10:25:49 +090049/// Represents the list of APEXes
Jooyung Han743e0d62022-11-07 20:57:48 +090050#[derive(Clone, Debug, Deserialize, Eq, PartialEq)]
Inseob Kimf79c5722024-12-16 17:29:57 +090051pub(crate) struct ApexInfoList {
52 /// The list of APEXes
Jooyung Han44b02ab2021-07-16 03:19:13 +090053 #[serde(rename = "apex-info")]
Inseob Kimf79c5722024-12-16 17:29:57 +090054 pub(crate) list: Vec<ApexInfo>,
Jooyung Han73bac242021-07-02 10:25:49 +090055}
56
Inseob Kimf79c5722024-12-16 17:29:57 +090057/// Represents info of an APEX
Jooyung Han5ce867a2022-01-28 03:18:38 +090058#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq)]
Inseob Kimf79c5722024-12-16 17:29:57 +090059pub(crate) struct ApexInfo {
60 /// Name of APEX
Jooyung Han44b02ab2021-07-16 03:19:13 +090061 #[serde(rename = "moduleName")]
Inseob Kimf79c5722024-12-16 17:29:57 +090062 pub(crate) name: String,
63
Jooyung Hancaa995c2022-11-08 16:35:50 +090064 #[serde(rename = "versionCode")]
65 version: u64,
Jooyung Han44b02ab2021-07-16 03:19:13 +090066 #[serde(rename = "modulePath")]
Jooyung Han73bac242021-07-02 10:25:49 +090067 path: PathBuf,
Jooyung Han9d7cd7d2021-10-12 17:44:14 +090068
69 #[serde(default)]
Alan Stokes46ac3862021-12-21 15:31:47 +000070 has_classpath_jar: bool,
Andrew Walbran40be9d52022-01-19 14:32:53 +000071
72 // The field claims to be milliseconds but is actually seconds.
73 #[serde(rename = "lastUpdateMillis")]
74 last_update_seconds: u64,
Jiyong Parkd6502352022-01-27 01:07:30 +090075
76 #[serde(rename = "isFactory")]
77 is_factory: bool,
Jooyung Hanec788042022-01-27 22:28:37 +090078
79 #[serde(rename = "isActive")]
80 is_active: bool,
Jooyung Han5ce867a2022-01-28 03:18:38 +090081
82 #[serde(rename = "provideSharedApexLibs")]
83 provide_shared_apex_libs: bool,
Nikita Ioffed4551e12023-07-14 16:01:03 +010084
85 #[serde(rename = "preinstalledModulePath")]
86 preinstalled_path: PathBuf,
Inseob Kimf79c5722024-12-16 17:29:57 +090087
88 /// Partition of APEX
89 #[serde(default)]
90 pub(crate) partition: String,
Jooyung Han73bac242021-07-02 10:25:49 +090091}
92
93impl ApexInfoList {
94 /// Loads ApexInfoList
Inseob Kimf79c5722024-12-16 17:29:57 +090095 pub(crate) fn load() -> Result<&'static ApexInfoList> {
Jooyung Han9900f3d2021-07-06 10:27:54 +090096 static INSTANCE: OnceCell<ApexInfoList> = OnceCell::new();
97 INSTANCE.get_or_try_init(|| {
Jooyung Han44b02ab2021-07-16 03:19:13 +090098 let apex_info_list = File::open(APEX_INFO_LIST_PATH)
99 .context(format!("Failed to open {}", APEX_INFO_LIST_PATH))?;
Jooyung Han9d7cd7d2021-10-12 17:44:14 +0900100 let mut apex_info_list: ApexInfoList = from_reader(apex_info_list)
Jooyung Han44b02ab2021-07-16 03:19:13 +0900101 .context(format!("Failed to parse {}", APEX_INFO_LIST_PATH))?;
Jooyung Han9d7cd7d2021-10-12 17:44:14 +0900102
Alan Stokesbf20c6a2022-01-04 12:30:50 +0000103 // For active APEXes, we run derive_classpath and parse its output to see if it
104 // contributes to the classpath(s). (This allows us to handle any new classpath env
105 // vars seamlessly.)
Inseob Kimecde8c02024-09-03 13:11:08 +0900106 if !cfg!(early) {
107 let classpath_vars = run_derive_classpath()?;
108 let classpath_apexes = find_apex_names_in_classpath(&classpath_vars)?;
Alan Stokesbf20c6a2022-01-04 12:30:50 +0000109
Inseob Kimecde8c02024-09-03 13:11:08 +0900110 for apex_info in apex_info_list.list.iter_mut() {
111 apex_info.has_classpath_jar = classpath_apexes.contains(&apex_info.name);
112 }
Jooyung Han9d7cd7d2021-10-12 17:44:14 +0900113 }
Alan Stokesbf20c6a2022-01-04 12:30:50 +0000114
Jooyung Han44b02ab2021-07-16 03:19:13 +0900115 Ok(apex_info_list)
Jooyung Han9900f3d2021-07-06 10:27:54 +0900116 })
Jooyung Han73bac242021-07-02 10:25:49 +0900117 }
Jooyung Han743e0d62022-11-07 20:57:48 +0900118
119 // Override apex info with the staged one
120 fn override_staged_apex(&mut self, staged_apex_info: &StagedApexInfo) -> Result<()> {
121 let mut need_to_add: Option<ApexInfo> = None;
122 for apex_info in self.list.iter_mut() {
123 if staged_apex_info.moduleName == apex_info.name {
124 if apex_info.is_active && apex_info.is_factory {
125 // Copy the entry to the end as factory/non-active after the loop
126 // to keep the factory version. Typically this step is unncessary,
127 // but some apexes (like sharedlibs) need to be kept even if it's inactive.
128 need_to_add.replace(ApexInfo { is_active: false, ..apex_info.clone() });
129 // And make this one as non-factory. Note that this one is still active
130 // and overridden right below.
131 apex_info.is_factory = false;
132 }
133 // Active one is overridden with the staged one.
134 if apex_info.is_active {
Jooyung Hancaa995c2022-11-08 16:35:50 +0900135 apex_info.version = staged_apex_info.versionCode as u64;
Jooyung Han743e0d62022-11-07 20:57:48 +0900136 apex_info.path = PathBuf::from(&staged_apex_info.diskImagePath);
137 apex_info.has_classpath_jar = staged_apex_info.hasClassPathJars;
138 apex_info.last_update_seconds = last_updated(&apex_info.path)?;
139 }
140 }
141 }
142 if let Some(info) = need_to_add {
143 self.list.push(info);
144 }
145 Ok(())
146 }
147}
148
149fn last_updated<P: AsRef<Path>>(path: P) -> Result<u64> {
150 let metadata = metadata(path)?;
151 Ok(metadata.modified()?.duration_since(SystemTime::UNIX_EPOCH)?.as_secs())
Jooyung Hanec788042022-01-27 22:28:37 +0900152}
Jooyung Han73bac242021-07-02 10:25:49 +0900153
Jooyung Hanec788042022-01-27 22:28:37 +0900154impl ApexInfo {
155 fn matches(&self, apex_config: &ApexConfig) -> bool {
156 // Match with pseudo name "{CLASSPATH}" which represents APEXes contributing
157 // to any derive_classpath environment variable
158 if apex_config.name == "{CLASSPATH}" && self.has_classpath_jar {
159 return true;
160 }
161 if apex_config.name == self.name {
162 return true;
163 }
164 false
Jooyung Han73bac242021-07-02 10:25:49 +0900165 }
Jooyung Han21e9b922021-06-26 04:14:16 +0900166}
167
Jooyung Han5dc42172021-10-05 16:43:47 +0900168struct PackageManager {
Jooyung Han5dc42172021-10-05 16:43:47 +0900169 apex_info_list: &'static ApexInfoList,
170}
171
172impl PackageManager {
173 fn new() -> Result<Self> {
Jooyung Han5dc42172021-10-05 16:43:47 +0900174 let apex_info_list = ApexInfoList::load()?;
Jooyung Han53cf7992021-10-18 19:38:41 +0900175 Ok(Self { apex_info_list })
Jooyung Han5dc42172021-10-05 16:43:47 +0900176 }
177
Jooyung Han9d7cd7d2021-10-12 17:44:14 +0900178 fn get_apex_list(&self, prefer_staged: bool) -> Result<ApexInfoList> {
Jooyung Han53cf7992021-10-18 19:38:41 +0900179 // get the list of active apexes
Jooyung Han9d7cd7d2021-10-12 17:44:14 +0900180 let mut list = self.apex_info_list.clone();
Jooyung Han53cf7992021-10-18 19:38:41 +0900181 // When prefer_staged, we override ApexInfo by consulting "package_native"
Jooyung Han5dc42172021-10-05 16:43:47 +0900182 if prefer_staged {
Inseob Kimecde8c02024-09-03 13:11:08 +0900183 if cfg!(early) {
184 return Err(anyhow!("Can't turn on prefer_staged on early boot VMs"));
185 }
Jooyung Han53cf7992021-10-18 19:38:41 +0900186 let pm =
187 wait_for_interface::<dyn IPackageManagerNative>(PACKAGE_MANAGER_NATIVE_SERVICE)
188 .context("Failed to get service when prefer_staged is set.")?;
Jooyung Hana94e92d2024-10-11 14:16:27 +0900189 let staged = pm.getStagedApexInfos().context("getStagedApexInfos failed")?;
190 for apex in staged {
191 list.override_staged_apex(&apex)?;
Jooyung Han5dc42172021-10-05 16:43:47 +0900192 }
193 }
Jooyung Han9d7cd7d2021-10-12 17:44:14 +0900194 Ok(list)
Jooyung Han5dc42172021-10-05 16:43:47 +0900195 }
196}
197
Andrew Walbrancc0db522021-07-12 17:03:42 +0000198fn make_metadata_file(
Alan Stokes0d1ef782022-09-27 13:46:35 +0100199 app_config: &VirtualMachineAppConfig,
Jooyung Hanec788042022-01-27 22:28:37 +0900200 apex_infos: &[&ApexInfo],
Jooyung Han21e9b922021-06-26 04:14:16 +0900201 temporary_directory: &Path,
Andrew Walbrancc0db522021-07-12 17:03:42 +0000202) -> Result<ParcelFileDescriptor> {
Alan Stokes0d1ef782022-09-27 13:46:35 +0100203 let payload_metadata = match &app_config.payload {
Ludovic Barman93ee3082023-06-20 12:18:43 +0000204 Payload::PayloadConfig(payload_config) => PayloadMetadata::Config(PayloadConfig {
Alan Stokes8f12f2b2023-01-09 09:19:20 +0000205 payload_binary_name: payload_config.payloadBinaryName.clone(),
Alan Stokesfda70842023-12-20 17:50:14 +0000206 extra_apk_count: payload_config.extraApks.len().try_into()?,
207 special_fields: Default::default(),
Alan Stokes0d1ef782022-09-27 13:46:35 +0100208 }),
209 Payload::ConfigPath(config_path) => {
Ludovic Barman93ee3082023-06-20 12:18:43 +0000210 PayloadMetadata::ConfigPath(format!("/mnt/apk/{}", config_path))
Alan Stokes0d1ef782022-09-27 13:46:35 +0100211 }
212 };
213
Jooyung Han21e9b922021-06-26 04:14:16 +0900214 let metadata = Metadata {
Andrew Walbrancc0db522021-07-12 17:03:42 +0000215 version: 1,
Jooyung Hanec788042022-01-27 22:28:37 +0900216 apexes: apex_infos
Jooyung Han21e9b922021-06-26 04:14:16 +0900217 .iter()
Jooyung Han19c1d6c2021-08-06 14:08:16 +0900218 .enumerate()
Jooyung Hanec788042022-01-27 22:28:37 +0900219 .map(|(i, apex_info)| {
Andrew Walbran40be9d52022-01-19 14:32:53 +0000220 Ok(ApexPayload {
Jooyung Hanec788042022-01-27 22:28:37 +0900221 name: apex_info.name.clone(),
Andrew Walbran40be9d52022-01-19 14:32:53 +0000222 partition_name: format!("microdroid-apex-{}", i),
Jiyong Parkd6502352022-01-27 01:07:30 +0900223 last_update_seconds: apex_info.last_update_seconds,
224 is_factory: apex_info.is_factory,
Andrew Walbran40be9d52022-01-19 14:32:53 +0000225 ..Default::default()
226 })
Jooyung Han19c1d6c2021-08-06 14:08:16 +0900227 })
Andrew Walbran40be9d52022-01-19 14:32:53 +0000228 .collect::<Result<_>>()?,
Jooyung Han21e9b922021-06-26 04:14:16 +0900229 apk: Some(ApkPayload {
Jooyung Han35edb8f2021-07-01 16:17:16 +0900230 name: "apk".to_owned(),
231 payload_partition_name: "microdroid-apk".to_owned(),
232 idsig_partition_name: "microdroid-apk-idsig".to_owned(),
Jooyung Han21e9b922021-06-26 04:14:16 +0900233 ..Default::default()
234 })
235 .into(),
Alan Stokes0d1ef782022-09-27 13:46:35 +0100236 payload: Some(payload_metadata),
Jooyung Han21e9b922021-06-26 04:14:16 +0900237 ..Default::default()
238 };
Andrew Walbrancc0db522021-07-12 17:03:42 +0000239
240 // Write metadata to file.
Alan Stokes0d1ef782022-09-27 13:46:35 +0100241 let metadata_path = temporary_directory.join("metadata");
Andrew Walbrancc0db522021-07-12 17:03:42 +0000242 let mut metadata_file = OpenOptions::new()
243 .create_new(true)
244 .read(true)
245 .write(true)
246 .open(&metadata_path)
247 .with_context(|| format!("Failed to open metadata file {:?}", metadata_path))?;
Jooyung Han21e9b922021-06-26 04:14:16 +0900248 microdroid_metadata::write_metadata(&metadata, &mut metadata_file)?;
249
Andrew Walbrancc0db522021-07-12 17:03:42 +0000250 // Re-open the metadata file as read-only.
251 open_parcel_file(&metadata_path, false)
252}
253
254/// Creates a DiskImage with partitions:
Alan Stokes53cc5ca2022-08-30 14:28:19 +0100255/// payload-metadata: metadata
Andrew Walbrancc0db522021-07-12 17:03:42 +0000256/// microdroid-apex-0: apex 0
257/// microdroid-apex-1: apex 1
258/// ..
259/// microdroid-apk: apk
260/// microdroid-apk-idsig: idsig
Inseob Kima5a262f2021-11-17 19:41:03 +0900261/// extra-apk-0: additional apk 0
262/// extra-idsig-0: additional idsig 0
263/// extra-apk-1: additional apk 1
264/// extra-idsig-1: additional idsig 1
265/// ..
Andrew Walbrancc0db522021-07-12 17:03:42 +0000266fn make_payload_disk(
Inseob Kima5a262f2021-11-17 19:41:03 +0900267 app_config: &VirtualMachineAppConfig,
Jaewan Kim61f86142023-03-28 15:12:52 +0900268 debug_config: &DebugConfig,
Andrew Walbrancc0db522021-07-12 17:03:42 +0000269 apk_file: File,
270 idsig_file: File,
Alan Stokesfda70842023-12-20 17:50:14 +0000271 extra_apk_files: Vec<File>,
Jooyung Han9d7cd7d2021-10-12 17:44:14 +0900272 vm_payload_config: &VmPayloadConfig,
Andrew Walbrancc0db522021-07-12 17:03:42 +0000273 temporary_directory: &Path,
274) -> Result<DiskImage> {
Alan Stokesfda70842023-12-20 17:50:14 +0000275 if extra_apk_files.len() != app_config.extraIdsigs.len() {
Inseob Kima5a262f2021-11-17 19:41:03 +0900276 bail!(
277 "payload config has {} apks, but app config has {} idsigs",
278 vm_payload_config.extra_apks.len(),
279 app_config.extraIdsigs.len()
280 );
281 }
282
Jooyung Han9d7cd7d2021-10-12 17:44:14 +0900283 let pm = PackageManager::new()?;
284 let apex_list = pm.get_apex_list(vm_payload_config.prefer_staged)?;
285
Jooyung Hanec788042022-01-27 22:28:37 +0900286 // collect APEXes from config
Nikita Ioffed4551e12023-07-14 16:01:03 +0100287 let mut apex_infos = collect_apex_infos(&apex_list, &vm_payload_config.apexes, debug_config)?;
Jooyung Hancaa995c2022-11-08 16:35:50 +0900288
289 // Pass sorted list of apexes. Sorting key shouldn't use `path` because it will change after
290 // reboot with prefer_staged. `last_update_seconds` is added to distinguish "samegrade"
291 // update.
292 apex_infos.sort_by_key(|info| (&info.name, &info.version, &info.last_update_seconds));
Jooyung Hanec788042022-01-27 22:28:37 +0900293 info!("Microdroid payload APEXes: {:?}", apex_infos.iter().map(|ai| &ai.name));
Jooyung Han9d7cd7d2021-10-12 17:44:14 +0900294
Alan Stokes0d1ef782022-09-27 13:46:35 +0100295 let metadata_file = make_metadata_file(app_config, &apex_infos, temporary_directory)?;
Jooyung Han21e9b922021-06-26 04:14:16 +0900296 // put metadata at the first partition
297 let mut partitions = vec![Partition {
Jooyung Han14e5a8e2021-07-06 20:48:38 +0900298 label: "payload-metadata".to_owned(),
Jooyung Han631d5882021-07-29 06:34:05 +0900299 image: Some(metadata_file),
Jooyung Han21e9b922021-06-26 04:14:16 +0900300 writable: false,
Jiyong Park3f9b5092024-07-10 13:38:29 +0900301 guid: None,
Jooyung Han21e9b922021-06-26 04:14:16 +0900302 }];
303
Jooyung Hanec788042022-01-27 22:28:37 +0900304 for (i, apex_info) in apex_infos.iter().enumerate() {
Inseob Kimecde8c02024-09-03 13:11:08 +0900305 let path = if cfg!(early) {
306 let path = &apex_info.preinstalled_path;
307 if path.extension().and_then(OsStr::to_str).unwrap_or("") != "apex" {
308 bail!("compressed APEX {} not supported", path.display());
309 }
310 path
311 } else {
312 &apex_info.path
313 };
314 let apex_file = open_parcel_file(path, false)?;
Jooyung Han95884632021-07-06 22:27:54 +0900315 partitions.push(Partition {
316 label: format!("microdroid-apex-{}", i),
Jooyung Han631d5882021-07-29 06:34:05 +0900317 image: Some(apex_file),
Jooyung Han95884632021-07-06 22:27:54 +0900318 writable: false,
Jiyong Park3f9b5092024-07-10 13:38:29 +0900319 guid: None,
Jooyung Han95884632021-07-06 22:27:54 +0900320 });
Jooyung Han21e9b922021-06-26 04:14:16 +0900321 }
Jooyung Han95884632021-07-06 22:27:54 +0900322 partitions.push(Partition {
323 label: "microdroid-apk".to_owned(),
Jooyung Han631d5882021-07-29 06:34:05 +0900324 image: Some(ParcelFileDescriptor::new(apk_file)),
Jooyung Han95884632021-07-06 22:27:54 +0900325 writable: false,
Jiyong Park3f9b5092024-07-10 13:38:29 +0900326 guid: None,
Jooyung Han95884632021-07-06 22:27:54 +0900327 });
328 partitions.push(Partition {
329 label: "microdroid-apk-idsig".to_owned(),
Jooyung Han631d5882021-07-29 06:34:05 +0900330 image: Some(ParcelFileDescriptor::new(idsig_file)),
Jooyung Han95884632021-07-06 22:27:54 +0900331 writable: false,
Jiyong Park3f9b5092024-07-10 13:38:29 +0900332 guid: None,
Jooyung Han95884632021-07-06 22:27:54 +0900333 });
Jooyung Han21e9b922021-06-26 04:14:16 +0900334
Inseob Kima5a262f2021-11-17 19:41:03 +0900335 // we've already checked that extra_apks and extraIdsigs are in the same size.
Inseob Kima5a262f2021-11-17 19:41:03 +0900336 let extra_idsigs = &app_config.extraIdsigs;
Alan Stokesfda70842023-12-20 17:50:14 +0000337 for (i, (extra_apk_file, extra_idsig)) in
338 extra_apk_files.into_iter().zip(extra_idsigs.iter()).enumerate()
339 {
Inseob Kima5a262f2021-11-17 19:41:03 +0900340 partitions.push(Partition {
Alan Stokesfda70842023-12-20 17:50:14 +0000341 label: format!("extra-apk-{i}"),
342 image: Some(ParcelFileDescriptor::new(extra_apk_file)),
Inseob Kima5a262f2021-11-17 19:41:03 +0900343 writable: false,
Jiyong Park3f9b5092024-07-10 13:38:29 +0900344 guid: None,
Inseob Kima5a262f2021-11-17 19:41:03 +0900345 });
346
347 partitions.push(Partition {
Alan Stokesfda70842023-12-20 17:50:14 +0000348 label: format!("extra-idsig-{i}"),
Victor Hsieh14497f02022-09-21 10:10:16 -0700349 image: Some(ParcelFileDescriptor::new(
350 extra_idsig
351 .as_ref()
352 .try_clone()
Alan Stokesfda70842023-12-20 17:50:14 +0000353 .with_context(|| format!("Failed to clone the extra idsig #{i}"))?,
Victor Hsieh14497f02022-09-21 10:10:16 -0700354 )),
Inseob Kima5a262f2021-11-17 19:41:03 +0900355 writable: false,
Jiyong Park3f9b5092024-07-10 13:38:29 +0900356 guid: None,
Inseob Kima5a262f2021-11-17 19:41:03 +0900357 });
358 }
359
Jooyung Han21e9b922021-06-26 04:14:16 +0900360 Ok(DiskImage { image: None, partitions, writable: false })
361}
Andrew Walbrancc0db522021-07-12 17:03:42 +0000362
Alan Stokesbf20c6a2022-01-04 12:30:50 +0000363fn run_derive_classpath() -> Result<String> {
364 let result = Command::new("/apex/com.android.sdkext/bin/derive_classpath")
365 .arg("/proc/self/fd/1")
366 .output()
367 .context("Failed to run derive_classpath")?;
368
369 if !result.status.success() {
370 bail!("derive_classpath returned {}", result.status);
371 }
372
373 String::from_utf8(result.stdout).context("Converting derive_classpath output")
374}
375
376fn find_apex_names_in_classpath(classpath_vars: &str) -> Result<HashSet<String>> {
377 // Each line should be in the format "export <var name> <paths>", where <paths> is a
378 // colon-separated list of paths to JARs. We don't care about the var names, and we're only
379 // interested in paths that look like "/apex/<apex name>/<anything>" so we know which APEXes
380 // contribute to at least one var.
381 let mut apexes = HashSet::new();
382
383 let pattern = Regex::new(r"^export [^ ]+ ([^ ]+)$").context("Failed to construct Regex")?;
384 for line in classpath_vars.lines() {
385 if let Some(captures) = pattern.captures(line) {
386 if let Some(paths) = captures.get(1) {
387 apexes.extend(paths.as_str().split(':').filter_map(|path| {
388 let path = path.strip_prefix("/apex/")?;
389 Some(path[..path.find('/')?].to_owned())
390 }));
391 continue;
392 }
393 }
394 warn!("Malformed line from derive_classpath: {}", line);
395 }
396
397 Ok(apexes)
Jooyung Han5e0f2062021-10-12 14:00:46 +0900398}
399
Nikita Ioffed4551e12023-07-14 16:01:03 +0100400fn check_apexes_are_from_allowed_partitions(requested_apexes: &Vec<&ApexInfo>) -> Result<()> {
401 const ALLOWED_PARTITIONS: [&str; 2] = ["/system", "/system_ext"];
402 for apex in requested_apexes {
403 if !ALLOWED_PARTITIONS.iter().any(|p| apex.preinstalled_path.starts_with(p)) {
404 bail!("Non-system APEX {} is not supported in Microdroid", apex.name);
405 }
406 }
407 Ok(())
408}
409
Jooyung Hanec788042022-01-27 22:28:37 +0900410// Collect ApexInfos from VM config
411fn collect_apex_infos<'a>(
412 apex_list: &'a ApexInfoList,
413 apex_configs: &[ApexConfig],
Jaewan Kim61f86142023-03-28 15:12:52 +0900414 debug_config: &DebugConfig,
Nikita Ioffed4551e12023-07-14 16:01:03 +0100415) -> Result<Vec<&'a ApexInfo>> {
Alan Stokesf7260f12023-08-30 17:25:21 +0100416 // APEXes which any Microdroid VM needs.
417 // TODO(b/192200378) move this to microdroid.json?
418 let required_apexes: &[_] =
419 if debug_config.should_include_debug_apexes() { &["com.android.adbd"] } else { &[] };
Jooyung Hanec788042022-01-27 22:28:37 +0900420
Nikita Ioffed4551e12023-07-14 16:01:03 +0100421 let apex_infos = apex_list
Jooyung Hanec788042022-01-27 22:28:37 +0900422 .list
423 .iter()
424 .filter(|ai| {
425 apex_configs.iter().any(|cfg| ai.matches(cfg) && ai.is_active)
Alan Stokesf7260f12023-08-30 17:25:21 +0100426 || required_apexes.iter().any(|name| name == &ai.name && ai.is_active)
Jooyung Han5ce867a2022-01-28 03:18:38 +0900427 || ai.provide_shared_apex_libs
Jooyung Hanec788042022-01-27 22:28:37 +0900428 })
Nikita Ioffed4551e12023-07-14 16:01:03 +0100429 .collect();
430
431 check_apexes_are_from_allowed_partitions(&apex_infos)?;
432 Ok(apex_infos)
Jooyung Han5e0f2062021-10-12 14:00:46 +0900433}
434
Nikita Ioffe5dfddf22023-06-29 16:11:26 +0100435pub fn add_microdroid_vendor_image(vendor_image: File, vm_config: &mut VirtualMachineRawConfig) {
436 vm_config.disks.push(DiskImage {
437 image: None,
438 writable: false,
439 partitions: vec![Partition {
440 label: "microdroid-vendor".to_owned(),
441 image: Some(ParcelFileDescriptor::new(vendor_image)),
442 writable: false,
Jiyong Park3f9b5092024-07-10 13:38:29 +0900443 guid: None,
Nikita Ioffe5dfddf22023-06-29 16:11:26 +0100444 }],
445 })
446}
447
Shikha Panwar22e70452022-10-10 18:32:55 +0000448pub fn add_microdroid_system_images(
Andrew Walbrancc0db522021-07-12 17:03:42 +0000449 config: &VirtualMachineAppConfig,
Jiyong Park8d081812021-07-23 17:45:04 +0900450 instance_file: File,
Shikha Panwar22e70452022-10-10 18:32:55 +0000451 storage_image: Option<File>,
Inseob Kim172f9eb2023-11-06 17:02:08 +0900452 os_name: &str,
Andrew Walbrancc0db522021-07-12 17:03:42 +0000453 vm_config: &mut VirtualMachineRawConfig,
454) -> Result<()> {
Shikha Panwared8ace42022-09-28 12:52:16 +0000455 let debug_suffix = match config.debugLevel {
456 DebugLevel::NONE => "normal",
Seungjae Yooe85831e2022-12-12 09:34:58 +0900457 DebugLevel::FULL => "debuggable",
Shikha Panwared8ace42022-09-28 12:52:16 +0000458 _ => return Err(anyhow!("unsupported debug level: {:?}", config.debugLevel)),
459 };
Inseob Kim172f9eb2023-11-06 17:02:08 +0900460 let initrd = format!("/apex/com.android.virt/etc/{os_name}_initrd_{debug_suffix}.img");
Shikha Panwared8ace42022-09-28 12:52:16 +0000461 vm_config.initrd = Some(open_parcel_file(Path::new(&initrd), false)?);
462
Shikha Panwar22e70452022-10-10 18:32:55 +0000463 let mut writable_partitions = vec![Partition {
Shikha Panwared8ace42022-09-28 12:52:16 +0000464 label: "vm-instance".to_owned(),
465 image: Some(ParcelFileDescriptor::new(instance_file)),
466 writable: true,
Jiyong Park3f9b5092024-07-10 13:38:29 +0900467 guid: None,
Shikha Panwar22e70452022-10-10 18:32:55 +0000468 }];
469
470 if let Some(file) = storage_image {
471 writable_partitions.push(Partition {
Shikha Panwar566c9672022-11-15 14:39:58 +0000472 label: "encryptedstore".to_owned(),
Shikha Panwar22e70452022-10-10 18:32:55 +0000473 image: Some(ParcelFileDescriptor::new(file)),
474 writable: true,
Jiyong Park3f9b5092024-07-10 13:38:29 +0900475 guid: None,
Shikha Panwar22e70452022-10-10 18:32:55 +0000476 });
477 }
478
479 vm_config.disks.push(DiskImage {
480 image: None,
481 partitions: writable_partitions,
482 writable: true,
483 });
484
485 Ok(())
486}
487
Alan Stokesfda70842023-12-20 17:50:14 +0000488#[allow(clippy::too_many_arguments)] // TODO: Fewer arguments
Shikha Panwar22e70452022-10-10 18:32:55 +0000489pub fn add_microdroid_payload_images(
490 config: &VirtualMachineAppConfig,
Jaewan Kim61f86142023-03-28 15:12:52 +0900491 debug_config: &DebugConfig,
Shikha Panwar22e70452022-10-10 18:32:55 +0000492 temporary_directory: &Path,
493 apk_file: File,
494 idsig_file: File,
Alan Stokesfda70842023-12-20 17:50:14 +0000495 extra_apk_files: Vec<File>,
Shikha Panwar22e70452022-10-10 18:32:55 +0000496 vm_payload_config: &VmPayloadConfig,
497 vm_config: &mut VirtualMachineRawConfig,
498) -> Result<()> {
Andrew Walbrancc0db522021-07-12 17:03:42 +0000499 vm_config.disks.push(make_payload_disk(
Inseob Kima5a262f2021-11-17 19:41:03 +0900500 config,
Jaewan Kim61f86142023-03-28 15:12:52 +0900501 debug_config,
Andrew Walbrancc0db522021-07-12 17:03:42 +0000502 apk_file,
503 idsig_file,
Alan Stokesfda70842023-12-20 17:50:14 +0000504 extra_apk_files,
Jooyung Han9d7cd7d2021-10-12 17:44:14 +0900505 vm_payload_config,
Andrew Walbrancc0db522021-07-12 17:03:42 +0000506 temporary_directory,
507 )?);
508
Andrew Walbrancc0db522021-07-12 17:03:42 +0000509 Ok(())
510}
Jooyung Han5e0f2062021-10-12 14:00:46 +0900511
512#[cfg(test)]
513mod tests {
514 use super::*;
Alan Stokesf7260f12023-08-30 17:25:21 +0100515 use std::collections::HashMap;
Jooyung Han743e0d62022-11-07 20:57:48 +0900516 use tempfile::NamedTempFile;
Andrew Walbranc1a5f5a2022-01-19 13:38:13 +0000517
Jooyung Han5e0f2062021-10-12 14:00:46 +0900518 #[test]
Alan Stokesbf20c6a2022-01-04 12:30:50 +0000519 fn test_find_apex_names_in_classpath() {
520 let vars = r#"
521export FOO /apex/unterminated
522export BAR /apex/valid.apex/something
523wrong
524export EMPTY
525export OTHER /foo/bar:/baz:/apex/second.valid.apex/:gibberish:"#;
526 let expected = vec!["valid.apex", "second.valid.apex"];
527 let expected: HashSet<_> = expected.into_iter().map(ToString::to_string).collect();
528
529 assert_eq!(find_apex_names_in_classpath(vars).unwrap(), expected);
Jooyung Han5e0f2062021-10-12 14:00:46 +0900530 }
Andrew Walbranc1a5f5a2022-01-19 13:38:13 +0000531
532 #[test]
Nikita Ioffed4551e12023-07-14 16:01:03 +0100533 fn test_collect_apexes() -> Result<()> {
Alan Stokesf7260f12023-08-30 17:25:21 +0100534 let apex_infos_for_test = [
535 (
536 "adbd",
Andrew Walbranc1a5f5a2022-01-19 13:38:13 +0000537 ApexInfo {
Jooyung Hanec788042022-01-27 22:28:37 +0900538 name: "com.android.adbd".to_string(),
539 path: PathBuf::from("adbd"),
Nikita Ioffed4551e12023-07-14 16:01:03 +0100540 preinstalled_path: PathBuf::from("/system/adbd"),
Andrew Walbranc1a5f5a2022-01-19 13:38:13 +0000541 has_classpath_jar: false,
Andrew Walbran40be9d52022-01-19 14:32:53 +0000542 last_update_seconds: 12345678,
Jiyong Parkd6502352022-01-27 01:07:30 +0900543 is_factory: true,
Jooyung Hanec788042022-01-27 22:28:37 +0900544 is_active: false,
Jooyung Han5ce867a2022-01-28 03:18:38 +0900545 ..Default::default()
Jooyung Hanec788042022-01-27 22:28:37 +0900546 },
Alan Stokesf7260f12023-08-30 17:25:21 +0100547 ),
548 (
549 "adbd_updated",
Jooyung Hanec788042022-01-27 22:28:37 +0900550 ApexInfo {
Alan Stokesf7260f12023-08-30 17:25:21 +0100551 name: "com.android.adbd".to_string(),
552 path: PathBuf::from("adbd"),
553 preinstalled_path: PathBuf::from("/system/adbd"),
Jooyung Hanec788042022-01-27 22:28:37 +0900554 has_classpath_jar: false,
555 last_update_seconds: 12345678 + 1,
556 is_factory: false,
557 is_active: true,
Jooyung Han5ce867a2022-01-28 03:18:38 +0900558 ..Default::default()
Jooyung Hanec788042022-01-27 22:28:37 +0900559 },
Alan Stokesf7260f12023-08-30 17:25:21 +0100560 ),
561 (
562 "no_classpath",
Jooyung Hanec788042022-01-27 22:28:37 +0900563 ApexInfo {
Jooyung Hanec788042022-01-27 22:28:37 +0900564 name: "no_classpath".to_string(),
565 path: PathBuf::from("no_classpath"),
566 has_classpath_jar: false,
567 last_update_seconds: 12345678,
568 is_factory: true,
569 is_active: true,
Jooyung Han5ce867a2022-01-28 03:18:38 +0900570 ..Default::default()
Jooyung Hanec788042022-01-27 22:28:37 +0900571 },
Alan Stokesf7260f12023-08-30 17:25:21 +0100572 ),
573 (
574 "has_classpath",
Jooyung Hanec788042022-01-27 22:28:37 +0900575 ApexInfo {
Andrew Walbranc1a5f5a2022-01-19 13:38:13 +0000576 name: "has_classpath".to_string(),
Jooyung Hanec788042022-01-27 22:28:37 +0900577 path: PathBuf::from("has_classpath"),
Andrew Walbranc1a5f5a2022-01-19 13:38:13 +0000578 has_classpath_jar: true,
Andrew Walbran40be9d52022-01-19 14:32:53 +0000579 last_update_seconds: 87654321,
Jooyung Hanec788042022-01-27 22:28:37 +0900580 is_factory: true,
581 is_active: false,
Jooyung Han5ce867a2022-01-28 03:18:38 +0900582 ..Default::default()
Jooyung Hanec788042022-01-27 22:28:37 +0900583 },
Alan Stokesf7260f12023-08-30 17:25:21 +0100584 ),
585 (
586 "has_classpath_updated",
Jooyung Hanec788042022-01-27 22:28:37 +0900587 ApexInfo {
Jooyung Hanec788042022-01-27 22:28:37 +0900588 name: "has_classpath".to_string(),
589 path: PathBuf::from("has_classpath/updated"),
Nikita Ioffed4551e12023-07-14 16:01:03 +0100590 preinstalled_path: PathBuf::from("/system/has_classpath"),
Jooyung Hanec788042022-01-27 22:28:37 +0900591 has_classpath_jar: true,
592 last_update_seconds: 87654321 + 1,
Jiyong Parkd6502352022-01-27 01:07:30 +0900593 is_factory: false,
Jooyung Hanec788042022-01-27 22:28:37 +0900594 is_active: true,
Jooyung Han5ce867a2022-01-28 03:18:38 +0900595 ..Default::default()
Jooyung Hanec788042022-01-27 22:28:37 +0900596 },
Alan Stokesf7260f12023-08-30 17:25:21 +0100597 ),
598 (
599 "apex-foo",
Jooyung Hanec788042022-01-27 22:28:37 +0900600 ApexInfo {
Jooyung Hanec788042022-01-27 22:28:37 +0900601 name: "apex-foo".to_string(),
602 path: PathBuf::from("apex-foo"),
Nikita Ioffed4551e12023-07-14 16:01:03 +0100603 preinstalled_path: PathBuf::from("/system/apex-foo"),
Jooyung Hanec788042022-01-27 22:28:37 +0900604 has_classpath_jar: false,
605 last_update_seconds: 87654321,
606 is_factory: true,
607 is_active: false,
Jooyung Han5ce867a2022-01-28 03:18:38 +0900608 ..Default::default()
Jooyung Hanec788042022-01-27 22:28:37 +0900609 },
Alan Stokesf7260f12023-08-30 17:25:21 +0100610 ),
611 (
612 "apex-foo-updated",
Jooyung Hanec788042022-01-27 22:28:37 +0900613 ApexInfo {
Jooyung Hanec788042022-01-27 22:28:37 +0900614 name: "apex-foo".to_string(),
615 path: PathBuf::from("apex-foo/updated"),
Nikita Ioffed4551e12023-07-14 16:01:03 +0100616 preinstalled_path: PathBuf::from("/system/apex-foo"),
Jooyung Hanec788042022-01-27 22:28:37 +0900617 has_classpath_jar: false,
618 last_update_seconds: 87654321 + 1,
619 is_factory: false,
620 is_active: true,
Jooyung Han5ce867a2022-01-28 03:18:38 +0900621 ..Default::default()
622 },
Alan Stokesf7260f12023-08-30 17:25:21 +0100623 ),
624 (
625 "sharedlibs",
Jooyung Han5ce867a2022-01-28 03:18:38 +0900626 ApexInfo {
Jooyung Han5ce867a2022-01-28 03:18:38 +0900627 name: "sharedlibs".to_string(),
628 path: PathBuf::from("apex-foo"),
Nikita Ioffed4551e12023-07-14 16:01:03 +0100629 preinstalled_path: PathBuf::from("/system/apex-foo"),
Jooyung Han5ce867a2022-01-28 03:18:38 +0900630 last_update_seconds: 87654321,
631 is_factory: true,
632 provide_shared_apex_libs: true,
633 ..Default::default()
634 },
Alan Stokesf7260f12023-08-30 17:25:21 +0100635 ),
636 (
637 "sharedlibs-updated",
Jooyung Han5ce867a2022-01-28 03:18:38 +0900638 ApexInfo {
Jooyung Han5ce867a2022-01-28 03:18:38 +0900639 name: "sharedlibs".to_string(),
640 path: PathBuf::from("apex-foo/updated"),
Nikita Ioffed4551e12023-07-14 16:01:03 +0100641 preinstalled_path: PathBuf::from("/system/apex-foo"),
Jooyung Han5ce867a2022-01-28 03:18:38 +0900642 last_update_seconds: 87654321 + 1,
643 is_active: true,
644 provide_shared_apex_libs: true,
645 ..Default::default()
Andrew Walbranc1a5f5a2022-01-19 13:38:13 +0000646 },
Alan Stokesf7260f12023-08-30 17:25:21 +0100647 ),
648 ];
649 let apex_info_list = ApexInfoList {
650 list: apex_infos_for_test.iter().map(|(_, info)| info).cloned().collect(),
Andrew Walbranc1a5f5a2022-01-19 13:38:13 +0000651 };
Alan Stokesf7260f12023-08-30 17:25:21 +0100652 let apex_info_map = HashMap::from(apex_infos_for_test);
Jooyung Hanec788042022-01-27 22:28:37 +0900653 let apex_configs = vec![
654 ApexConfig { name: "apex-foo".to_string() },
Andrew Walbranc1a5f5a2022-01-19 13:38:13 +0000655 ApexConfig { name: "{CLASSPATH}".to_string() },
656 ];
657 assert_eq!(
Nikita Ioffed4551e12023-07-14 16:01:03 +0100658 collect_apex_infos(
659 &apex_info_list,
660 &apex_configs,
Jaewan Kimf3143242024-03-15 06:56:31 +0000661 &DebugConfig::new_with_debug_level(DebugLevel::FULL)
Nikita Ioffed4551e12023-07-14 16:01:03 +0100662 )?,
Andrew Walbranc1a5f5a2022-01-19 13:38:13 +0000663 vec![
Jooyung Han5ce867a2022-01-28 03:18:38 +0900664 // Pass active/required APEXes
Alan Stokesf7260f12023-08-30 17:25:21 +0100665 &apex_info_map["adbd_updated"],
Jooyung Han5ce867a2022-01-28 03:18:38 +0900666 // Pass active APEXes specified in the config
Alan Stokesf7260f12023-08-30 17:25:21 +0100667 &apex_info_map["has_classpath_updated"],
668 &apex_info_map["apex-foo-updated"],
Jooyung Han5ce867a2022-01-28 03:18:38 +0900669 // Pass both preinstalled(inactive) and updated(active) for "sharedlibs" APEXes
Alan Stokesf7260f12023-08-30 17:25:21 +0100670 &apex_info_map["sharedlibs"],
671 &apex_info_map["sharedlibs-updated"],
Andrew Walbranc1a5f5a2022-01-19 13:38:13 +0000672 ]
673 );
Nikita Ioffed4551e12023-07-14 16:01:03 +0100674 Ok(())
675 }
676
677 #[test]
678 fn test_check_allowed_partitions_vendor_not_allowed() -> Result<()> {
679 let apex_info_list = ApexInfoList {
680 list: vec![ApexInfo {
681 name: "apex-vendor".to_string(),
682 path: PathBuf::from("apex-vendor"),
683 preinstalled_path: PathBuf::from("/vendor/apex-vendor"),
684 is_active: true,
685 ..Default::default()
686 }],
687 };
688 let apex_configs = vec![ApexConfig { name: "apex-vendor".to_string() }];
689
Jaewan Kimf3143242024-03-15 06:56:31 +0000690 let ret = collect_apex_infos(
691 &apex_info_list,
692 &apex_configs,
693 &DebugConfig::new_with_debug_level(DebugLevel::NONE),
694 );
Nikita Ioffed4551e12023-07-14 16:01:03 +0100695 assert!(ret
696 .is_err_and(|ret| ret.to_string()
697 == "Non-system APEX apex-vendor is not supported in Microdroid"));
698
699 Ok(())
700 }
701
702 #[test]
703 fn test_check_allowed_partitions_system_ext_allowed() -> Result<()> {
704 let apex_info_list = ApexInfoList {
705 list: vec![ApexInfo {
706 name: "apex-system_ext".to_string(),
707 path: PathBuf::from("apex-system_ext"),
708 preinstalled_path: PathBuf::from("/system_ext/apex-system_ext"),
709 is_active: true,
710 ..Default::default()
711 }],
712 };
713
714 let apex_configs = vec![ApexConfig { name: "apex-system_ext".to_string() }];
715
716 assert_eq!(
717 collect_apex_infos(
718 &apex_info_list,
719 &apex_configs,
Jaewan Kimf3143242024-03-15 06:56:31 +0000720 &DebugConfig::new_with_debug_level(DebugLevel::NONE)
Nikita Ioffed4551e12023-07-14 16:01:03 +0100721 )?,
722 vec![&apex_info_list.list[0]]
723 );
724
725 Ok(())
Andrew Walbranc1a5f5a2022-01-19 13:38:13 +0000726 }
Jooyung Han743e0d62022-11-07 20:57:48 +0900727
728 #[test]
729 fn test_prefer_staged_apex_with_factory_active_apex() {
730 let single_apex = ApexInfo {
731 name: "foo".to_string(),
Jooyung Hancaa995c2022-11-08 16:35:50 +0900732 version: 1,
Jooyung Han743e0d62022-11-07 20:57:48 +0900733 path: PathBuf::from("foo.apex"),
734 is_factory: true,
735 is_active: true,
736 ..Default::default()
737 };
738 let mut apex_info_list = ApexInfoList { list: vec![single_apex.clone()] };
739
740 let staged = NamedTempFile::new().unwrap();
741 apex_info_list
742 .override_staged_apex(&StagedApexInfo {
743 moduleName: "foo".to_string(),
Jooyung Hancaa995c2022-11-08 16:35:50 +0900744 versionCode: 2,
Jooyung Han743e0d62022-11-07 20:57:48 +0900745 diskImagePath: staged.path().to_string_lossy().to_string(),
746 ..Default::default()
747 })
748 .expect("should be ok");
749
750 assert_eq!(
751 apex_info_list,
752 ApexInfoList {
753 list: vec![
754 ApexInfo {
Jooyung Hancaa995c2022-11-08 16:35:50 +0900755 version: 2,
Jooyung Han743e0d62022-11-07 20:57:48 +0900756 is_factory: false,
757 path: staged.path().to_owned(),
758 last_update_seconds: last_updated(staged.path()).unwrap(),
759 ..single_apex.clone()
760 },
761 ApexInfo { is_active: false, ..single_apex },
762 ],
763 }
764 );
765 }
766
767 #[test]
768 fn test_prefer_staged_apex_with_factory_and_inactive_apex() {
769 let factory_apex = ApexInfo {
770 name: "foo".to_string(),
Jooyung Hancaa995c2022-11-08 16:35:50 +0900771 version: 1,
Jooyung Han743e0d62022-11-07 20:57:48 +0900772 path: PathBuf::from("foo.apex"),
773 is_factory: true,
774 ..Default::default()
775 };
776 let active_apex = ApexInfo {
777 name: "foo".to_string(),
Jooyung Hancaa995c2022-11-08 16:35:50 +0900778 version: 2,
Jooyung Han743e0d62022-11-07 20:57:48 +0900779 path: PathBuf::from("foo.downloaded.apex"),
780 is_active: true,
781 ..Default::default()
782 };
783 let mut apex_info_list =
784 ApexInfoList { list: vec![factory_apex.clone(), active_apex.clone()] };
785
786 let staged = NamedTempFile::new().unwrap();
787 apex_info_list
788 .override_staged_apex(&StagedApexInfo {
789 moduleName: "foo".to_string(),
Jooyung Hancaa995c2022-11-08 16:35:50 +0900790 versionCode: 3,
Jooyung Han743e0d62022-11-07 20:57:48 +0900791 diskImagePath: staged.path().to_string_lossy().to_string(),
792 ..Default::default()
793 })
794 .expect("should be ok");
795
796 assert_eq!(
797 apex_info_list,
798 ApexInfoList {
799 list: vec![
800 // factory apex isn't touched
801 factory_apex,
802 // update active one
803 ApexInfo {
Jooyung Hancaa995c2022-11-08 16:35:50 +0900804 version: 3,
Jooyung Han743e0d62022-11-07 20:57:48 +0900805 path: staged.path().to_owned(),
806 last_update_seconds: last_updated(staged.path()).unwrap(),
807 ..active_apex
808 },
809 ],
810 }
811 );
812 }
Jooyung Han5e0f2062021-10-12 14:00:46 +0900813}