| 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, ptr::addr_of}; |
| 6 | |
| 7 | use bql::prelude::*; |
| 8 | use hwcore::prelude::*; |
| 9 | use migration::prelude::*; |
| 10 | use qom::prelude::*; |
| 11 | use util::bindings::{module_call_init, module_init_type}; |
| 12 | |
| 13 | // Test that macros can compile. |
| 14 | pub const VMSTATE: VMStateDescription<DummyState> = VMStateDescriptionBuilder::<DummyState>::new() |
| 15 | .name(c"name") |
| 16 | .unmigratable() |
| 17 | .build(); |
| 18 | |
| 19 | #[repr(C)] |
| 20 | #[derive(qom::Object, hwcore::Device)] |
| 21 | pub struct DummyState { |
| 22 | parent: ParentField<DeviceState>, |
| 23 | #[property(rename = "migrate-clk", default = true)] |
| 24 | migrate_clock: bool, |
| 25 | } |
| 26 | |
| 27 | qom_isa!(DummyState: Object, DeviceState); |
| 28 | |
| 29 | pub struct DummyClass { |
| 30 | parent_class: <DeviceState as ObjectType>::Class, |
| 31 | } |
| 32 | |
| 33 | impl DummyClass { |
| 34 | pub fn class_init<T: DeviceImpl>(self: &mut DummyClass) { |
| 35 | self.parent_class.class_init::<T>(); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | unsafe impl ObjectType for DummyState { |
| 40 | type Class = DummyClass; |
| 41 | const TYPE_NAME: &'static CStr = c"dummy"; |
| 42 | } |
| 43 | |
| 44 | impl ObjectImpl for DummyState { |
| 45 | type ParentType = DeviceState; |
| 46 | const ABSTRACT: bool = false; |
| 47 | const CLASS_INIT: fn(&mut DummyClass) = DummyClass::class_init::<Self>; |
| 48 | } |
| 49 | |
| 50 | impl ResettablePhasesImpl for DummyState {} |
| 51 | |
| 52 | impl DeviceImpl for DummyState { |
| 53 | const VMSTATE: Option<VMStateDescription<Self>> = Some(VMSTATE); |
| 54 | } |
| 55 | |
| 56 | #[repr(C)] |
| 57 | #[derive(qom::Object, hwcore::Device)] |
| 58 | pub struct DummyChildState { |
| 59 | parent: ParentField<DummyState>, |
| 60 | } |
| 61 | |
| 62 | qom_isa!(DummyChildState: Object, DeviceState, DummyState); |
| 63 | |
| 64 | pub struct DummyChildClass { |
| 65 | parent_class: <DummyState as ObjectType>::Class, |
| 66 | } |
| 67 | |
| 68 | unsafe impl ObjectType for DummyChildState { |
| 69 | type Class = DummyChildClass; |
| 70 | const TYPE_NAME: &'static CStr = c"dummy_child"; |
| 71 | } |
| 72 | |
| 73 | impl ObjectImpl for DummyChildState { |
| 74 | type ParentType = DummyState; |
| 75 | const ABSTRACT: bool = false; |
| 76 | const CLASS_INIT: fn(&mut DummyChildClass) = DummyChildClass::class_init::<Self>; |
| 77 | } |
| 78 | |
| 79 | impl ResettablePhasesImpl for DummyChildState {} |
| 80 | impl DeviceImpl for DummyChildState {} |
| 81 | |
| 82 | impl DummyChildClass { |
| 83 | pub fn class_init<T: DeviceImpl>(self: &mut DummyChildClass) { |
| 84 | self.parent_class.class_init::<T>(); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | fn init_qom() { |
| 89 | static ONCE: BqlCell<bool> = BqlCell::new(false); |
| 90 | |
| 91 | bql::start_test(); |
| 92 | if !ONCE.get() { |
| 93 | unsafe { |
| 94 | module_call_init(module_init_type::MODULE_INIT_QOM); |
| 95 | } |
| 96 | ONCE.set(true); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | #[test] |
| 101 | /// Create and immediately drop an instance. |
| 102 | fn test_object_new() { |
| 103 | init_qom(); |
| 104 | drop(DummyState::new()); |
| 105 | drop(DummyChildState::new()); |
| 106 | } |
| 107 | |
| 108 | #[test] |
| 109 | #[allow(clippy::redundant_clone)] |
| 110 | /// Create, clone and then drop an instance. |
| 111 | fn test_clone() { |
| 112 | init_qom(); |
| 113 | let p = DummyState::new(); |
| 114 | assert_eq!(p.clone().typename(), "dummy"); |
| 115 | drop(p); |
| 116 | } |
| 117 | |
| 118 | #[test] |
| 119 | /// Try invoking a method on an object. |
| 120 | fn test_typename() { |
| 121 | init_qom(); |
| 122 | let p = DummyState::new(); |
| 123 | assert_eq!(p.typename(), "dummy"); |
| 124 | } |
| 125 | |
| 126 | // a note on all "cast" tests: usually, especially for downcasts the desired |
| 127 | // class would be placed on the right, for example: |
| 128 | // |
| 129 | // let sbd_ref = p.dynamic_cast::<SysBusDevice>(); |
| 130 | // |
| 131 | // Here I am doing the opposite to check that the resulting type is correct. |
| 132 | |
| 133 | #[test] |
| 134 | #[allow(clippy::shadow_unrelated)] |
| 135 | /// Test casts on shared references. |
| 136 | fn test_cast() { |
| 137 | init_qom(); |
| 138 | let p = DummyState::new(); |
| 139 | let p_ptr: *mut DummyState = p.as_mut_ptr(); |
| 140 | let p_ref: &mut DummyState = unsafe { &mut *p_ptr }; |
| 141 | |
| 142 | let obj_ref: &Object = p_ref.upcast(); |
| 143 | assert_eq!(addr_of!(*obj_ref), p_ptr.cast()); |
| 144 | |
| 145 | let sbd_ref: Option<&DummyChildState> = obj_ref.dynamic_cast(); |
| 146 | assert!(sbd_ref.is_none()); |
| 147 | |
| 148 | let dev_ref: Option<&DeviceState> = obj_ref.downcast(); |
| 149 | assert_eq!(addr_of!(*dev_ref.unwrap()), p_ptr.cast()); |
| 150 | |
| 151 | // SAFETY: the cast is wrong, but the value is only used for comparison |
| 152 | unsafe { |
| 153 | let sbd_ref: &DummyChildState = obj_ref.unsafe_cast(); |
| 154 | assert_eq!(addr_of!(*sbd_ref), p_ptr.cast()); |
| 155 | } |
| 156 | } |