From f0553fe16a8944655b3d9997c00d8c363c4896cd Mon Sep 17 00:00:00 2001 From: Elie Kheirallah Date: Tue, 16 Aug 2022 01:52:58 +0000 Subject: [PATCH] devices: Adding Suspendable to ConstantDevice and DummyDevice Added suspendable trait to ConstantDevice. Generate tests for ConstantDevice. Added suspendable trait to DummyDevice. Generate tests for DummyDevice. Bug=b:232437513 Test=cargo test Change-Id: I38b7b84350c503a9b307a10789b5f328135ac036 Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/3831266 Reviewed-by: Daniel Verkamp Commit-Queue: Elie Kheirallah --- devices/src/bus.rs | 57 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/devices/src/bus.rs b/devices/src/bus.rs index 545e7a9382..d08800946d 100644 --- a/devices/src/bus.rs +++ b/devices/src/bus.rs @@ -551,8 +551,14 @@ impl Bus { mod tests { use super::*; use crate::pci::CrosvmDeviceId; + use crate::suspendable::Suspendable; + use crate::suspendable_tests; + use anyhow::Context; + use anyhow::Result as AnyhowResult; + #[derive(Copy, Clone, Serialize, Deserialize, Eq, PartialEq, Debug)] struct DummyDevice; + impl BusDevice for DummyDevice { fn device_id(&self) -> DeviceId { CrosvmDeviceId::Cmos.into() @@ -562,6 +568,27 @@ mod tests { } } + impl Suspendable for DummyDevice { + fn snapshot(&self) -> AnyhowResult { + serde_json::to_string_pretty(&self).context("error serializing") + } + + fn restore(&mut self, data: &str) -> AnyhowResult<()> { + let deser = serde_json::from_str(data).context("error deserializing"); + *self = deser.unwrap(); + Ok(()) + } + + fn sleep(&mut self) -> AnyhowResult<()> { + Ok(()) + } + + fn wake(&mut self) -> AnyhowResult<()> { + Ok(()) + } + } + + #[derive(Copy, Clone, Serialize, Deserialize, Eq, PartialEq, Debug)] struct ConstantDevice { uses_full_addr: bool, } @@ -598,6 +625,26 @@ mod tests { } } + impl Suspendable for ConstantDevice { + fn snapshot(&self) -> AnyhowResult { + serde_json::to_string_pretty(&self).context("error serializing") + } + + fn restore(&mut self, data: &str) -> AnyhowResult<()> { + let deser = serde_json::from_str(data).context("error deserializing"); + *self = deser.unwrap(); + Ok(()) + } + + fn sleep(&mut self) -> AnyhowResult<()> { + Ok(()) + } + + fn wake(&mut self) -> AnyhowResult<()> { + Ok(()) + } + } + #[test] fn bus_insert() { let bus = Bus::new(); @@ -683,6 +730,16 @@ mod tests { assert!(bus.write(0x15, &values)); } + suspendable_tests! { + dummy_device: DummyDevice, + constant_device_true: ConstantDevice { + uses_full_addr: true, + }, + constant_device_false: ConstantDevice { + uses_full_addr: false, + }, + } + #[test] fn bus_range_contains() { let a = BusRange {