xref: /openbmc/qemu/rust/qemu-api/tests/tests.rs (revision cde3c425)
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 core::ffi::CStr;
6 
7 use qemu_api::{
8     bindings::*,
9     declare_properties, define_property,
10     definitions::{Class, ObjectImpl},
11     device_class_init, vm_state_description,
12 };
13 
14 #[test]
15 fn test_device_decl_macros() {
16     // Test that macros can compile.
17     vm_state_description! {
18         VMSTATE,
19         name: c"name",
20         unmigratable: true,
21     }
22 
23     #[repr(C)]
24     #[derive(qemu_api_macros::Object)]
25     pub struct DummyState {
26         pub _parent: DeviceState,
27         pub migrate_clock: bool,
28     }
29 
30     #[repr(C)]
31     pub struct DummyClass {
32         pub _parent: DeviceClass,
33     }
34 
35     declare_properties! {
36         DUMMY_PROPERTIES,
37             define_property!(
38                 c"migrate-clk",
39                 DummyState,
40                 migrate_clock,
41                 unsafe { &qdev_prop_bool },
42                 bool
43             ),
44     }
45 
46     device_class_init! {
47         dummy_class_init,
48         props => DUMMY_PROPERTIES,
49         realize_fn => None,
50         legacy_reset_fn => None,
51         vmsd => VMSTATE,
52     }
53 
54     impl ObjectImpl for DummyState {
55         type Class = DummyClass;
56         const TYPE_INFO: qemu_api::bindings::TypeInfo = qemu_api::type_info! { Self };
57         const TYPE_NAME: &'static CStr = c"dummy";
58         const PARENT_TYPE_NAME: Option<&'static CStr> = Some(TYPE_DEVICE);
59         const ABSTRACT: bool = false;
60         const INSTANCE_INIT: Option<unsafe extern "C" fn(obj: *mut Object)> = None;
61         const INSTANCE_POST_INIT: Option<unsafe extern "C" fn(obj: *mut Object)> = None;
62         const INSTANCE_FINALIZE: Option<unsafe extern "C" fn(obj: *mut Object)> = None;
63     }
64 
65     impl Class for DummyClass {
66         const CLASS_INIT: Option<
67             unsafe extern "C" fn(klass: *mut ObjectClass, data: *mut core::ffi::c_void),
68         > = Some(dummy_class_init);
69         const CLASS_BASE_INIT: Option<
70             unsafe extern "C" fn(klass: *mut ObjectClass, data: *mut core::ffi::c_void),
71         > = None;
72     }
73 
74     unsafe {
75         module_call_init(module_init_type::MODULE_INIT_QOM);
76         object_unref(object_new(DummyState::TYPE_NAME.as_ptr()) as *mut _);
77     }
78 }
79