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 <dverkamp@chromium.org>
Commit-Queue: Elie Kheirallah <khei@google.com>
This commit is contained in:
Elie Kheirallah 2022-08-16 01:52:58 +00:00 committed by crosvm LUCI
parent 53cd18e062
commit f0553fe16a

View file

@ -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<String> {
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<String> {
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 {