tests.rs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // Copyright 2024, Linaro Limited
  2. // Author(s): Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
  3. // SPDX-License-Identifier: GPL-2.0-or-later
  4. use std::{
  5. ffi::{c_void, CStr},
  6. ptr::{addr_of, addr_of_mut},
  7. };
  8. use qemu_api::{
  9. bindings::{module_call_init, module_init_type, object_new, object_unref, qdev_prop_bool},
  10. c_str,
  11. cell::{self, BqlCell},
  12. declare_properties, define_property,
  13. prelude::*,
  14. qdev::{DeviceImpl, DeviceState, Property, ResettablePhasesImpl},
  15. qom::{ObjectImpl, ParentField},
  16. sysbus::SysBusDevice,
  17. vmstate::VMStateDescription,
  18. zeroable::Zeroable,
  19. };
  20. // Test that macros can compile.
  21. pub static VMSTATE: VMStateDescription = VMStateDescription {
  22. name: c_str!("name").as_ptr(),
  23. unmigratable: true,
  24. ..Zeroable::ZERO
  25. };
  26. #[derive(qemu_api_macros::offsets)]
  27. #[repr(C)]
  28. #[derive(qemu_api_macros::Object)]
  29. pub struct DummyState {
  30. parent: ParentField<DeviceState>,
  31. migrate_clock: bool,
  32. }
  33. qom_isa!(DummyState: Object, DeviceState);
  34. pub struct DummyClass {
  35. parent_class: <DeviceState as ObjectType>::Class,
  36. }
  37. impl DummyClass {
  38. pub fn class_init<T: DeviceImpl>(self: &mut DummyClass) {
  39. self.parent_class.class_init::<T>();
  40. }
  41. }
  42. declare_properties! {
  43. DUMMY_PROPERTIES,
  44. define_property!(
  45. c_str!("migrate-clk"),
  46. DummyState,
  47. migrate_clock,
  48. unsafe { &qdev_prop_bool },
  49. bool
  50. ),
  51. }
  52. unsafe impl ObjectType for DummyState {
  53. type Class = DummyClass;
  54. const TYPE_NAME: &'static CStr = c_str!("dummy");
  55. }
  56. impl ObjectImpl for DummyState {
  57. type ParentType = DeviceState;
  58. const ABSTRACT: bool = false;
  59. const CLASS_INIT: fn(&mut DummyClass) = DummyClass::class_init::<Self>;
  60. }
  61. impl ResettablePhasesImpl for DummyState {}
  62. impl DeviceImpl for DummyState {
  63. fn properties() -> &'static [Property] {
  64. &DUMMY_PROPERTIES
  65. }
  66. fn vmsd() -> Option<&'static VMStateDescription> {
  67. Some(&VMSTATE)
  68. }
  69. }
  70. #[derive(qemu_api_macros::offsets)]
  71. #[repr(C)]
  72. #[derive(qemu_api_macros::Object)]
  73. pub struct DummyChildState {
  74. parent: ParentField<DummyState>,
  75. }
  76. qom_isa!(DummyChildState: Object, DeviceState, DummyState);
  77. pub struct DummyChildClass {
  78. parent_class: <DummyState as ObjectType>::Class,
  79. }
  80. unsafe impl ObjectType for DummyChildState {
  81. type Class = DummyChildClass;
  82. const TYPE_NAME: &'static CStr = c_str!("dummy_child");
  83. }
  84. impl ObjectImpl for DummyChildState {
  85. type ParentType = DummyState;
  86. const ABSTRACT: bool = false;
  87. const CLASS_INIT: fn(&mut DummyChildClass) = DummyChildClass::class_init::<Self>;
  88. }
  89. impl ResettablePhasesImpl for DummyChildState {}
  90. impl DeviceImpl for DummyChildState {}
  91. impl DummyChildClass {
  92. pub fn class_init<T: DeviceImpl>(self: &mut DummyChildClass) {
  93. self.parent_class.class_init::<T>();
  94. }
  95. }
  96. fn init_qom() {
  97. static ONCE: BqlCell<bool> = BqlCell::new(false);
  98. cell::bql_start_test();
  99. if !ONCE.get() {
  100. unsafe {
  101. module_call_init(module_init_type::MODULE_INIT_QOM);
  102. }
  103. ONCE.set(true);
  104. }
  105. }
  106. #[test]
  107. /// Create and immediately drop an instance.
  108. fn test_object_new() {
  109. init_qom();
  110. drop(DummyState::new());
  111. drop(DummyChildState::new());
  112. }
  113. #[test]
  114. #[allow(clippy::redundant_clone)]
  115. /// Create, clone and then drop an instance.
  116. fn test_clone() {
  117. init_qom();
  118. let p = DummyState::new();
  119. assert_eq!(p.clone().typename(), "dummy");
  120. drop(p);
  121. }
  122. #[test]
  123. /// Try invoking a method on an object.
  124. fn test_typename() {
  125. init_qom();
  126. let p = DummyState::new();
  127. assert_eq!(p.typename(), "dummy");
  128. }
  129. // a note on all "cast" tests: usually, especially for downcasts the desired
  130. // class would be placed on the right, for example:
  131. //
  132. // let sbd_ref = p.dynamic_cast::<SysBusDevice>();
  133. //
  134. // Here I am doing the opposite to check that the resulting type is correct.
  135. #[test]
  136. #[allow(clippy::shadow_unrelated)]
  137. /// Test casts on shared references.
  138. fn test_cast() {
  139. init_qom();
  140. let p = DummyState::new();
  141. let p_ptr: *mut DummyState = p.as_mut_ptr();
  142. let p_ref: &mut DummyState = unsafe { &mut *p_ptr };
  143. let obj_ref: &Object = p_ref.upcast();
  144. assert_eq!(addr_of!(*obj_ref), p_ptr.cast());
  145. let sbd_ref: Option<&SysBusDevice> = obj_ref.dynamic_cast();
  146. assert!(sbd_ref.is_none());
  147. let dev_ref: Option<&DeviceState> = obj_ref.downcast();
  148. assert_eq!(addr_of!(*dev_ref.unwrap()), p_ptr.cast());
  149. // SAFETY: the cast is wrong, but the value is only used for comparison
  150. unsafe {
  151. let sbd_ref: &SysBusDevice = obj_ref.unsafe_cast();
  152. assert_eq!(addr_of!(*sbd_ref), p_ptr.cast());
  153. }
  154. }
  155. #[test]
  156. #[allow(clippy::shadow_unrelated)]
  157. /// Test casts on mutable references.
  158. fn test_cast_mut() {
  159. init_qom();
  160. let p: *mut DummyState = unsafe { object_new(DummyState::TYPE_NAME.as_ptr()).cast() };
  161. let p_ref: &mut DummyState = unsafe { &mut *p };
  162. let obj_ref: &mut Object = p_ref.upcast_mut();
  163. assert_eq!(addr_of_mut!(*obj_ref), p.cast());
  164. let sbd_ref: Result<&mut SysBusDevice, &mut Object> = obj_ref.dynamic_cast_mut();
  165. let obj_ref = sbd_ref.unwrap_err();
  166. let dev_ref: Result<&mut DeviceState, &mut Object> = obj_ref.downcast_mut();
  167. let dev_ref = dev_ref.unwrap();
  168. assert_eq!(addr_of_mut!(*dev_ref), p.cast());
  169. // SAFETY: the cast is wrong, but the value is only used for comparison
  170. unsafe {
  171. let sbd_ref: &mut SysBusDevice = obj_ref.unsafe_cast_mut();
  172. assert_eq!(addr_of_mut!(*sbd_ref), p.cast());
  173. object_unref(p_ref.as_object_mut_ptr().cast::<c_void>());
  174. }
  175. }