From b7d2b0db4894bb8b7c91dfd6bfd3a87fc0613750 Mon Sep 17 00:00:00 2001 From: Inseob Kim Date: Mon, 13 Jun 2022 17:19:43 +0900 Subject: [PATCH] Add invalid config test This test ensures that an invalid VM config is correctly reported via death reason. Bug: 220071963 Test: atest MicrodroidTests Change-Id: I3e507828a72b40f51b4f188350ed6a19cf5e5a50 --- tests/testapk/assets/vm_config_no_task.json | 6 +++ .../microdroid/test/MicrodroidTests.java | 44 +++++++++++++++++-- 2 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 tests/testapk/assets/vm_config_no_task.json diff --git a/tests/testapk/assets/vm_config_no_task.json b/tests/testapk/assets/vm_config_no_task.json new file mode 100644 index 00000000..3162bd03 --- /dev/null +++ b/tests/testapk/assets/vm_config_no_task.json @@ -0,0 +1,6 @@ +{ + "os": { + "name": "microdroid" + }, + "export_tombstones": true +} diff --git a/tests/testapk/src/java/com/android/microdroid/test/MicrodroidTests.java b/tests/testapk/src/java/com/android/microdroid/test/MicrodroidTests.java index 5c48a41c..59f9d179 100644 --- a/tests/testapk/src/java/com/android/microdroid/test/MicrodroidTests.java +++ b/tests/testapk/src/java/com/android/microdroid/test/MicrodroidTests.java @@ -27,6 +27,7 @@ import android.os.Build; import android.os.ParcelFileDescriptor; import android.os.SystemProperties; import android.sysprop.HypervisorProperties; +import android.system.virtualizationservice.DeathReason; import android.system.virtualmachine.VirtualMachine; import android.system.virtualmachine.VirtualMachineCallback; import android.system.virtualmachine.VirtualMachineConfig; @@ -479,10 +480,21 @@ public class MicrodroidTests { file.writeByte(b ^ 1); } - private boolean tryBootVm(String vmName) + private static class BootResult { + public final boolean payloadStarted; + public final int deathReason; + + BootResult(boolean payloadStarted, int deathReason) { + this.payloadStarted = payloadStarted; + this.deathReason = deathReason; + } + } + + private BootResult tryBootVm(String vmName) throws VirtualMachineException, InterruptedException { mInner.mVm = mInner.mVmm.get(vmName); // re-load the vm before running tests final CompletableFuture payloadStarted = new CompletableFuture<>(); + final CompletableFuture deathReason = new CompletableFuture<>(); VmEventListener listener = new VmEventListener() { @Override @@ -490,9 +502,15 @@ public class MicrodroidTests { payloadStarted.complete(true); forceStop(vm); } + @Override + public void onDied(VirtualMachine vm, int reason) { + deathReason.complete(reason); + super.onDied(vm, reason); + } }; listener.runToFinish(mInner.mVm); - return payloadStarted.getNow(false); + return new BootResult( + payloadStarted.getNow(false), deathReason.getNow(DeathReason.INFRASTRUCTURE_ERROR)); } private RandomAccessFile prepareInstanceImage(String vmName) @@ -506,7 +524,7 @@ public class MicrodroidTests { oldVm.delete(); mInner.mVmm.getOrCreate(vmName, config); - assertThat(tryBootVm(vmName)).isTrue(); + assertThat(tryBootVm(vmName).payloadStarted).isTrue(); File vmRoot = new File(mInner.mContext.getFilesDir(), "vm"); File vmDir = new File(vmRoot, vmName); @@ -530,7 +548,7 @@ public class MicrodroidTests { assertThat(offset.isPresent()).isTrue(); flipBit(instanceFile, offset.getAsLong()); - assertThat(tryBootVm("test_vm_integrity")).isFalse(); + assertThat(tryBootVm("test_vm_integrity").payloadStarted).isFalse(); } @Test @@ -571,4 +589,22 @@ public class MicrodroidTests { assertThatPartitionIsMissing(PVM_FW_PARTITION_UUID); } } + + @Test + public void bootFailsWhenConfigIsInvalid() + throws VirtualMachineException, InterruptedException, IOException { + VirtualMachine existingVm = mInner.mVmm.get("test_vm_invalid_config"); + if (existingVm != null) { + existingVm.delete(); + } + + VirtualMachineConfig.Builder builder = + mInner.newVmConfigBuilder("assets/vm_config_no_task.json"); + VirtualMachineConfig normalConfig = builder.debugLevel(DebugLevel.NONE).build(); + mInner.mVmm.create("test_vm_invalid_config", normalConfig); + + BootResult bootResult = tryBootVm("test_vm_invalid_config"); + assertThat(bootResult.payloadStarted).isFalse(); + assertThat(bootResult.deathReason).isEqualTo(DeathReason.MICRODROID_INVALID_PAYLOAD_CONFIG); + } }