Merge "Test: bootloader checks vbmeta signing key"
diff --git a/compos/composd/aidl/android/system/composd/IIsolatedCompilationService.aidl b/compos/composd/aidl/android/system/composd/IIsolatedCompilationService.aidl
index 8156265..dde75e1 100644
--- a/compos/composd/aidl/android/system/composd/IIsolatedCompilationService.aidl
+++ b/compos/composd/aidl/android/system/composd/IIsolatedCompilationService.aidl
@@ -19,6 +19,13 @@
 import android.system.composd.ICompilationTaskCallback;
 
 interface IIsolatedCompilationService {
+    enum ApexSource {
+        /** Only use the activated APEXes */
+        NoStaged,
+        /** Prefer any staged APEXes, otherwise use the activated ones */
+        PreferStaged,
+    }
+
     /**
      * Compile BCP extensions and system server, using any staged APEXes that are present in
      * preference to active APEXes, writing the results to the pending artifacts directory to be
@@ -41,5 +48,5 @@
      * callback, unless the returned ICompilationTask is cancelled. The caller should maintain
      * a reference to the ICompilationTask until compilation completes or is cancelled.
      */
-    ICompilationTask startTestCompile(ICompilationTaskCallback callback);
+    ICompilationTask startTestCompile(ApexSource apexSource, ICompilationTaskCallback callback);
 }
diff --git a/compos/composd/src/instance_manager.rs b/compos/composd/src/instance_manager.rs
index 848fc8c..f1289e8 100644
--- a/compos/composd/src/instance_manager.rs
+++ b/compos/composd/src/instance_manager.rs
@@ -48,9 +48,12 @@
         self.start_instance(CURRENT_INSTANCE_DIR, vm_parameters)
     }
 
-    pub fn start_test_instance(&self) -> Result<Arc<CompOsInstance>> {
+    pub fn start_test_instance(&self, prefer_staged: bool) -> Result<Arc<CompOsInstance>> {
         let mut vm_parameters = new_vm_parameters()?;
         vm_parameters.debug_mode = true;
+        if prefer_staged {
+            vm_parameters.config_path = Some(PREFER_STAGED_VM_CONFIG_PATH.to_owned());
+        }
         self.start_instance(TEST_INSTANCE_DIR, vm_parameters)
     }
 
diff --git a/compos/composd/src/service.rs b/compos/composd/src/service.rs
index f4798d7..a9b8202 100644
--- a/compos/composd/src/service.rs
+++ b/compos/composd/src/service.rs
@@ -22,7 +22,9 @@
 use android_system_composd::aidl::android::system::composd::{
     ICompilationTask::{BnCompilationTask, ICompilationTask},
     ICompilationTaskCallback::ICompilationTaskCallback,
-    IIsolatedCompilationService::{BnIsolatedCompilationService, IIsolatedCompilationService},
+    IIsolatedCompilationService::{
+        ApexSource::ApexSource, BnIsolatedCompilationService, IIsolatedCompilationService,
+    },
 };
 use android_system_composd::binder::{
     self, BinderFeatures, ExceptionCode, Interface, Status, Strong, ThreadState,
@@ -58,10 +60,16 @@
 
     fn startTestCompile(
         &self,
+        apex_source: ApexSource,
         callback: &Strong<dyn ICompilationTaskCallback>,
     ) -> binder::Result<Strong<dyn ICompilationTask>> {
         check_permissions()?;
-        to_binder_result(self.do_start_test_compile(callback))
+        let prefer_staged = match apex_source {
+            ApexSource::NoStaged => false,
+            ApexSource::PreferStaged => true,
+            _ => unreachable!("Invalid ApexSource {:?}", apex_source),
+        };
+        to_binder_result(self.do_start_test_compile(prefer_staged, callback))
     }
 }
 
@@ -85,9 +93,11 @@
 
     fn do_start_test_compile(
         &self,
+        prefer_staged: bool,
         callback: &Strong<dyn ICompilationTaskCallback>,
     ) -> Result<Strong<dyn ICompilationTask>> {
-        let comp_os = self.instance_manager.start_test_instance().context("Starting CompOS")?;
+        let comp_os =
+            self.instance_manager.start_test_instance(prefer_staged).context("Starting CompOS")?;
 
         let target_dir_name = TEST_ARTIFACTS_SUBDIR.to_owned();
         let task = OdrefreshTask::start(
diff --git a/compos/composd_cmd/composd_cmd.rs b/compos/composd_cmd/composd_cmd.rs
index 546c4af..9f535d5 100644
--- a/compos/composd_cmd/composd_cmd.rs
+++ b/compos/composd_cmd/composd_cmd.rs
@@ -20,6 +20,7 @@
     aidl::android::system::composd::{
         ICompilationTask::ICompilationTask,
         ICompilationTaskCallback::{BnCompilationTaskCallback, ICompilationTaskCallback},
+        IIsolatedCompilationService::ApexSource::ApexSource,
         IIsolatedCompilationService::IIsolatedCompilationService,
     },
     binder::{
@@ -33,22 +34,25 @@
 use std::time::Duration;
 
 fn main() -> Result<()> {
-    let app = clap::App::new("composd_cmd").arg(
-        clap::Arg::with_name("command")
-            .index(1)
-            .takes_value(true)
-            .required(true)
-            .possible_values(&["staged-apex-compile", "test-compile"]),
-    );
+    #[rustfmt::skip]
+    let app = clap::App::new("composd_cmd")
+        .subcommand(
+            clap::SubCommand::with_name("staged-apex-compile"))
+        .subcommand(
+            clap::SubCommand::with_name("test-compile")
+                .arg(clap::Arg::with_name("prefer-staged").long("prefer-staged")),
+        );
     let args = app.get_matches();
-    let command = args.value_of("command").unwrap();
 
     ProcessState::start_thread_pool();
 
-    match command {
-        "staged-apex-compile" => run_staged_apex_compile()?,
-        "test-compile" => run_test_compile()?,
-        _ => panic!("Unexpected command {}", command),
+    match args.subcommand() {
+        ("staged-apex-compile", _) => run_staged_apex_compile()?,
+        ("test-compile", Some(sub_matches)) => {
+            let prefer_staged = sub_matches.is_present("prefer-staged");
+            run_test_compile(prefer_staged)?;
+        }
+        _ => panic!("Unrecognized subcommand"),
     }
 
     println!("All Ok!");
@@ -108,8 +112,9 @@
     run_async_compilation(|service, callback| service.startStagedApexCompile(callback))
 }
 
-fn run_test_compile() -> Result<()> {
-    run_async_compilation(|service, callback| service.startTestCompile(callback))
+fn run_test_compile(prefer_staged: bool) -> Result<()> {
+    let apex_source = if prefer_staged { ApexSource::PreferStaged } else { ApexSource::NoStaged };
+    run_async_compilation(|service, callback| service.startTestCompile(apex_source, callback))
 }
 
 fn run_async_compilation<F>(start_compile_fn: F) -> Result<()>
diff --git a/compos/service/java/com/android/server/compos/IsolatedCompilationJobService.java b/compos/service/java/com/android/server/compos/IsolatedCompilationJobService.java
index f801a8d..75f5334 100644
--- a/compos/service/java/com/android/server/compos/IsolatedCompilationJobService.java
+++ b/compos/service/java/com/android/server/compos/IsolatedCompilationJobService.java
@@ -176,7 +176,8 @@
             try {
                 ICompilationTask composTask;
                 if (jobId == DAILY_JOB_ID) {
-                    composTask = composd.startTestCompile(this);
+                    composTask = composd.startTestCompile(
+                            IIsolatedCompilationService.ApexSource.NoStaged, this);
                 } else {
                     composTask = composd.startStagedApexCompile(this);
                 }