tdisp/
test_helpers.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4use crate::TdispHostDeviceInterface;
5use crate::TdispHostDeviceTargetEmulator;
6use parking_lot::Mutex;
7use std::sync::Arc;
8use tdisp_proto::TdispDeviceInterfaceInfo;
9use tdisp_proto::TdispGuestProtocolType;
10use tdisp_proto::TdispReportType;
11
12/// Guest protocol that will be negotiated by the mock device.
13pub const TDISP_MOCK_GUEST_PROTOCOL: TdispGuestProtocolType = TdispGuestProtocolType::AmdSevTioV1;
14
15/// Device features that will be negotiated by the mock device.
16pub const TDISP_MOCK_SUPPORTED_FEATURES: u64 = 0xDEAD;
17
18/// Device ID that will be negotiated by the mock device.
19pub const TDISP_MOCK_DEVICE_ID: u64 = 99;
20
21/// Implements the host side of the TDISP interface for the mock NullDevice.
22pub struct NullTdispHostInterface {}
23impl TdispHostDeviceInterface for NullTdispHostInterface {
24    fn tdisp_negotiate_protocol(
25        &mut self,
26        _requested_guest_protocol: TdispGuestProtocolType,
27    ) -> anyhow::Result<TdispDeviceInterfaceInfo> {
28        Ok(TdispDeviceInterfaceInfo {
29            guest_protocol_type: TDISP_MOCK_GUEST_PROTOCOL as i32,
30            supported_features: TDISP_MOCK_SUPPORTED_FEATURES,
31            tdisp_device_id: TDISP_MOCK_DEVICE_ID,
32        })
33    }
34
35    fn tdisp_bind_device(&mut self) -> anyhow::Result<()> {
36        Ok(())
37    }
38
39    fn tdisp_start_device(&mut self) -> anyhow::Result<()> {
40        Ok(())
41    }
42
43    fn tdisp_unbind_device(&mut self) -> anyhow::Result<()> {
44        Ok(())
45    }
46
47    fn tdisp_get_device_report(
48        &mut self,
49        _report_type: TdispReportType,
50    ) -> anyhow::Result<Vec<u8>> {
51        Ok(vec![])
52    }
53}
54
55/// Implements the host side of the TDISP interface for a mock device that does nothing.
56pub fn new_null_tdisp_interface(debug_device_id: &str) -> TdispHostDeviceTargetEmulator {
57    TdispHostDeviceTargetEmulator::new(
58        Arc::new(Mutex::new(NullTdispHostInterface {})),
59        debug_device_id,
60    )
61}