xref: /openbmc/qemu/rust/qemu-api/tests/tests.rs (revision f3518400)
1cde3c425SPaolo Bonzini // Copyright 2024, Linaro Limited
2cde3c425SPaolo Bonzini // Author(s): Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
3cde3c425SPaolo Bonzini // SPDX-License-Identifier: GPL-2.0-or-later
4cde3c425SPaolo Bonzini 
59f7d4520SPaolo Bonzini use std::{ffi::CStr, os::raw::c_void};
6cde3c425SPaolo Bonzini 
7cde3c425SPaolo Bonzini use qemu_api::{
8cde3c425SPaolo Bonzini     bindings::*,
9718e255fSPaolo Bonzini     c_str, declare_properties, define_property,
10cde3c425SPaolo Bonzini     definitions::{Class, ObjectImpl},
110a65e412SManos Pitsidianakis     device_class_init,
120a65e412SManos Pitsidianakis     zeroable::Zeroable,
13cde3c425SPaolo Bonzini };
14cde3c425SPaolo Bonzini 
15cde3c425SPaolo Bonzini #[test]
16cde3c425SPaolo Bonzini fn test_device_decl_macros() {
17cde3c425SPaolo Bonzini     // Test that macros can compile.
180a65e412SManos Pitsidianakis     pub static VMSTATE: VMStateDescription = VMStateDescription {
19718e255fSPaolo Bonzini         name: c_str!("name").as_ptr(),
20cde3c425SPaolo Bonzini         unmigratable: true,
210a65e412SManos Pitsidianakis         ..Zeroable::ZERO
220a65e412SManos Pitsidianakis     };
23cde3c425SPaolo Bonzini 
24*f3518400SJunjie Mao     #[derive(qemu_api_macros::offsets)]
25cde3c425SPaolo Bonzini     #[repr(C)]
26cde3c425SPaolo Bonzini     #[derive(qemu_api_macros::Object)]
27cde3c425SPaolo Bonzini     pub struct DummyState {
28cde3c425SPaolo Bonzini         pub _parent: DeviceState,
29cde3c425SPaolo Bonzini         pub migrate_clock: bool,
30cde3c425SPaolo Bonzini     }
31cde3c425SPaolo Bonzini 
32cde3c425SPaolo Bonzini     #[repr(C)]
33cde3c425SPaolo Bonzini     pub struct DummyClass {
34cde3c425SPaolo Bonzini         pub _parent: DeviceClass,
35cde3c425SPaolo Bonzini     }
36cde3c425SPaolo Bonzini 
37cde3c425SPaolo Bonzini     declare_properties! {
38cde3c425SPaolo Bonzini         DUMMY_PROPERTIES,
39cde3c425SPaolo Bonzini             define_property!(
40718e255fSPaolo Bonzini                 c_str!("migrate-clk"),
41cde3c425SPaolo Bonzini                 DummyState,
42cde3c425SPaolo Bonzini                 migrate_clock,
43cde3c425SPaolo Bonzini                 unsafe { &qdev_prop_bool },
44cde3c425SPaolo Bonzini                 bool
45cde3c425SPaolo Bonzini             ),
46cde3c425SPaolo Bonzini     }
47cde3c425SPaolo Bonzini 
48cde3c425SPaolo Bonzini     device_class_init! {
49cde3c425SPaolo Bonzini         dummy_class_init,
50cde3c425SPaolo Bonzini         props => DUMMY_PROPERTIES,
51cde3c425SPaolo Bonzini         realize_fn => None,
52cde3c425SPaolo Bonzini         legacy_reset_fn => None,
53cde3c425SPaolo Bonzini         vmsd => VMSTATE,
54cde3c425SPaolo Bonzini     }
55cde3c425SPaolo Bonzini 
56cde3c425SPaolo Bonzini     impl ObjectImpl for DummyState {
57cde3c425SPaolo Bonzini         type Class = DummyClass;
58cde3c425SPaolo Bonzini         const TYPE_INFO: qemu_api::bindings::TypeInfo = qemu_api::type_info! { Self };
59718e255fSPaolo Bonzini         const TYPE_NAME: &'static CStr = c_str!("dummy");
60cde3c425SPaolo Bonzini         const PARENT_TYPE_NAME: Option<&'static CStr> = Some(TYPE_DEVICE);
61cde3c425SPaolo Bonzini         const ABSTRACT: bool = false;
62cde3c425SPaolo Bonzini         const INSTANCE_INIT: Option<unsafe extern "C" fn(obj: *mut Object)> = None;
63cde3c425SPaolo Bonzini         const INSTANCE_POST_INIT: Option<unsafe extern "C" fn(obj: *mut Object)> = None;
64cde3c425SPaolo Bonzini         const INSTANCE_FINALIZE: Option<unsafe extern "C" fn(obj: *mut Object)> = None;
65cde3c425SPaolo Bonzini     }
66cde3c425SPaolo Bonzini 
67cde3c425SPaolo Bonzini     impl Class for DummyClass {
689f7d4520SPaolo Bonzini         const CLASS_INIT: Option<unsafe extern "C" fn(klass: *mut ObjectClass, data: *mut c_void)> =
699f7d4520SPaolo Bonzini             Some(dummy_class_init);
70cde3c425SPaolo Bonzini         const CLASS_BASE_INIT: Option<
719f7d4520SPaolo Bonzini             unsafe extern "C" fn(klass: *mut ObjectClass, data: *mut c_void),
72cde3c425SPaolo Bonzini         > = None;
73cde3c425SPaolo Bonzini     }
74cde3c425SPaolo Bonzini 
75cde3c425SPaolo Bonzini     unsafe {
76cde3c425SPaolo Bonzini         module_call_init(module_init_type::MODULE_INIT_QOM);
77cde3c425SPaolo Bonzini         object_unref(object_new(DummyState::TYPE_NAME.as_ptr()) as *mut _);
78cde3c425SPaolo Bonzini     }
79cde3c425SPaolo Bonzini }
80