aboutsummaryrefslogtreecommitdiff
path: root/rust/qemu-api/tests/tests.rs
blob: 92dbfb8a0c86fcf16b144990ffc1c2c684a20cd4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Copyright 2024, Linaro Limited
// Author(s): Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
// SPDX-License-Identifier: GPL-2.0-or-later

use std::{
    ffi::{c_void, CStr},
    ptr::{addr_of, addr_of_mut},
};

use qemu_api::{
    bindings::*,
    c_str,
    cell::{self, BqlCell},
    declare_properties, define_property,
    prelude::*,
    qdev::{DeviceClass, DeviceImpl, DeviceState, Property, ResettablePhasesImpl},
    qom::{ClassInitImpl, ObjectImpl, ParentField},
    vmstate::VMStateDescription,
    zeroable::Zeroable,
};

// Test that macros can compile.
pub static VMSTATE: VMStateDescription = VMStateDescription {
    name: c_str!("name").as_ptr(),
    unmigratable: true,
    ..Zeroable::ZERO
};

#[derive(qemu_api_macros::offsets)]
#[repr(C)]
#[derive(qemu_api_macros::Object)]
pub struct DummyState {
    parent: ParentField<DeviceState>,
    migrate_clock: bool,
}

qom_isa!(DummyState: Object, DeviceState);

pub struct DummyClass {
    parent_class: <DeviceState as ObjectType>::Class,
}

declare_properties! {
    DUMMY_PROPERTIES,
        define_property!(
            c_str!("migrate-clk"),
            DummyState,
            migrate_clock,
            unsafe { &qdev_prop_bool },
            bool
        ),
}

unsafe impl ObjectType for DummyState {
    type Class = DummyClass;
    const TYPE_NAME: &'static CStr = c_str!("dummy");
}

impl ObjectImpl for DummyState {
    type ParentType = DeviceState;
    const ABSTRACT: bool = false;
}

impl ResettablePhasesImpl for DummyState {}

impl DeviceImpl for DummyState {
    fn properties() -> &'static [Property] {
        &DUMMY_PROPERTIES
    }
    fn vmsd() -> Option<&'static VMStateDescription> {
        Some(&VMSTATE)
    }
}

// `impl<T> ClassInitImpl<DummyClass> for T` doesn't work since it violates
// orphan rule.
impl ClassInitImpl<DummyClass> for DummyState {
    fn class_init(klass: &mut DummyClass) {
        <Self as ClassInitImpl<DeviceClass>>::class_init(&mut klass.parent_class);
    }
}

#[derive(qemu_api_macros::offsets)]
#[repr(C)]
#[derive(qemu_api_macros::Object)]
pub struct DummyChildState {
    parent: ParentField<DummyState>,
}

qom_isa!(DummyChildState: Object, DeviceState, DummyState);

pub struct DummyChildClass {
    parent_class: <DummyState as ObjectType>::Class,
}

unsafe impl ObjectType for DummyChildState {
    type Class = DummyChildClass;
    const TYPE_NAME: &'static CStr = c_str!("dummy_child");
}

impl ObjectImpl for DummyChildState {
    type ParentType = DummyState;
    const ABSTRACT: bool = false;
}

impl ResettablePhasesImpl for DummyChildState {}
impl DeviceImpl for DummyChildState {}

impl ClassInitImpl<DummyClass> for DummyChildState {
    fn class_init(klass: &mut DummyClass) {
        <Self as ClassInitImpl<DeviceClass>>::class_init(&mut klass.parent_class);
    }
}

impl ClassInitImpl<DummyChildClass> for DummyChildState {
    fn class_init(klass: &mut DummyChildClass) {
        <Self as ClassInitImpl<DummyClass>>::class_init(&mut klass.parent_class);
    }
}

fn init_qom() {
    static ONCE: BqlCell<bool> = BqlCell::new(false);

    cell::bql_start_test();
    if !ONCE.get() {
        unsafe {
            module_call_init(module_init_type::MODULE_INIT_QOM);
        }
        ONCE.set(true);
    }
}

#[test]
/// Create and immediately drop an instance.
fn test_object_new() {
    init_qom();
    drop(DummyState::new());
    drop(DummyChildState::new());
}

#[test]
#[allow(clippy::redundant_clone)]
/// Create, clone and then drop an instance.
fn test_clone() {
    init_qom();
    let p = DummyState::new();
    assert_eq!(p.clone().typename(), "dummy");
    drop(p);
}

#[test]
/// Try invoking a method on an object.
fn test_typename() {
    init_qom();
    let p = DummyState::new();
    assert_eq!(p.typename(), "dummy");
}

// a note on all "cast" tests: usually, especially for downcasts the desired
// class would be placed on the right, for example:
//
//    let sbd_ref = p.dynamic_cast::<SysBusDevice>();
//
// Here I am doing the opposite to check that the resulting type is correct.

#[test]
#[allow(clippy::shadow_unrelated)]
/// Test casts on shared references.
fn test_cast() {
    init_qom();
    let p = DummyState::new();
    let p_ptr: *mut DummyState = p.as_mut_ptr();
    let p_ref: &mut DummyState = unsafe { &mut *p_ptr };

    let obj_ref: &Object = p_ref.upcast();
    assert_eq!(addr_of!(*obj_ref), p_ptr.cast());

    let sbd_ref: Option<&SysBusDevice> = obj_ref.dynamic_cast();
    assert!(sbd_ref.is_none());

    let dev_ref: Option<&DeviceState> = obj_ref.downcast();
    assert_eq!(addr_of!(*dev_ref.unwrap()), p_ptr.cast());

    // SAFETY: the cast is wrong, but the value is only used for comparison
    unsafe {
        let sbd_ref: &SysBusDevice = obj_ref.unsafe_cast();
        assert_eq!(addr_of!(*sbd_ref), p_ptr.cast());
    }
}

#[test]
#[allow(clippy::shadow_unrelated)]
/// Test casts on mutable references.
fn test_cast_mut() {
    init_qom();
    let p: *mut DummyState = unsafe { object_new(DummyState::TYPE_NAME.as_ptr()).cast() };

    let p_ref: &mut DummyState = unsafe { &mut *p };
    let obj_ref: &mut Object = p_ref.upcast_mut();
    assert_eq!(addr_of_mut!(*obj_ref), p.cast());

    let sbd_ref: Result<&mut SysBusDevice, &mut Object> = obj_ref.dynamic_cast_mut();
    let obj_ref = sbd_ref.unwrap_err();

    let dev_ref: Result<&mut DeviceState, &mut Object> = obj_ref.downcast_mut();
    let dev_ref = dev_ref.unwrap();
    assert_eq!(addr_of_mut!(*dev_ref), p.cast());

    // SAFETY: the cast is wrong, but the value is only used for comparison
    unsafe {
        let sbd_ref: &mut SysBusDevice = obj_ref.unsafe_cast_mut();
        assert_eq!(addr_of_mut!(*sbd_ref), p.cast());

        object_unref(p_ref.as_object_mut_ptr().cast::<c_void>());
    }
}