xref: /qemu/rust/qemu-api/tests/tests.rs (revision c2f41c1b152bfe9aa72bbdf413c11c5ae9209f30)
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::*,
9     c_str,
10     cell::{self, BqlCell},
11     declare_properties, define_property,
12     prelude::*,
13     qdev::{DeviceImpl, DeviceState, Property},
14     qom::ObjectImpl,
15     vmstate::VMStateDescription,
16     zeroable::Zeroable,
17 };
18 
19 // Test that macros can compile.
20 pub static VMSTATE: VMStateDescription = VMStateDescription {
21     name: c_str!("name").as_ptr(),
22     unmigratable: true,
23     ..Zeroable::ZERO
24 };
25 
26 #[derive(qemu_api_macros::offsets)]
27 #[repr(C)]
28 #[derive(qemu_api_macros::Object)]
29 pub struct DummyState {
30     parent: DeviceState,
31     migrate_clock: bool,
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 = <DeviceState as ObjectType>::Class;
47     const TYPE_NAME: &'static CStr = c_str!("dummy");
48 }
49 
50 impl ObjectImpl for DummyState {
51     type ParentType = DeviceState;
52     const ABSTRACT: bool = false;
53 }
54 
55 impl DeviceImpl for DummyState {
56     fn properties() -> &'static [Property] {
57         &DUMMY_PROPERTIES
58     }
59     fn vmsd() -> Option<&'static VMStateDescription> {
60         Some(&VMSTATE)
61     }
62 }
63 
64 fn init_qom() {
65     static ONCE: BqlCell<bool> = BqlCell::new(false);
66 
67     cell::bql_start_test();
68     if !ONCE.get() {
69         unsafe {
70             module_call_init(module_init_type::MODULE_INIT_QOM);
71         }
72         ONCE.set(true);
73     }
74 }
75 
76 #[test]
77 /// Create and immediately drop an instance.
78 fn test_object_new() {
79     init_qom();
80     unsafe {
81         object_unref(object_new(DummyState::TYPE_NAME.as_ptr()).cast());
82     }
83 }
84