xref: /openbmc/qemu/rust/qemu-api/tests/tests.rs (revision 7bd8e3ef63330e870cf4644d21c285cce35c703d)
1 // Copyright 2024, Linaro Limited
2 // Author(s): Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
3 // SPDX-License-Identifier: GPL-2.0-or-later
4 
5 use std::ffi::CStr;
6 
7 use qemu_api::{
8     bindings::*, c_str, declare_properties, define_property, definitions::ObjectImpl,
9     device_class::DeviceImpl, impl_device_class, prelude::*, zeroable::Zeroable,
10 };
11 
12 #[test]
13 fn test_device_decl_macros() {
14     // Test that macros can compile.
15     pub static VMSTATE: VMStateDescription = VMStateDescription {
16         name: c_str!("name").as_ptr(),
17         unmigratable: true,
18         ..Zeroable::ZERO
19     };
20 
21     #[derive(qemu_api_macros::offsets)]
22     #[repr(C)]
23     #[derive(qemu_api_macros::Object)]
24     pub struct DummyState {
25         pub _parent: DeviceState,
26         pub migrate_clock: bool,
27     }
28 
29     #[repr(C)]
30     pub struct DummyClass {
31         pub _parent: DeviceClass,
32     }
33 
34     declare_properties! {
35         DUMMY_PROPERTIES,
36             define_property!(
37                 c_str!("migrate-clk"),
38                 DummyState,
39                 migrate_clock,
40                 unsafe { &qdev_prop_bool },
41                 bool
42             ),
43     }
44 
45     unsafe impl ObjectType for DummyState {
46         type Class = DummyClass;
47         const TYPE_NAME: &'static CStr = c_str!("dummy");
48     }
49 
50     impl ObjectImpl for DummyState {
51         const PARENT_TYPE_NAME: Option<&'static CStr> =
52             Some(<DeviceState as ObjectType>::TYPE_NAME);
53         const ABSTRACT: bool = false;
54     }
55 
56     impl DeviceImpl for DummyState {
57         fn properties() -> &'static [Property] {
58             &DUMMY_PROPERTIES
59         }
60         fn vmsd() -> Option<&'static VMStateDescription> {
61             Some(&VMSTATE)
62         }
63     }
64 
65     impl_device_class!(DummyState);
66 
67     unsafe {
68         module_call_init(module_init_type::MODULE_INIT_QOM);
69         object_unref(object_new(DummyState::TYPE_NAME.as_ptr()).cast());
70     }
71 }
72