tests.rs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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::{DeviceClass, DeviceImpl, DeviceState, Property, ResettablePhasesImpl},
  15. qom::{ClassInitImpl, 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. declare_properties! {
  38. DUMMY_PROPERTIES,
  39. define_property!(
  40. c_str!("migrate-clk"),
  41. DummyState,
  42. migrate_clock,
  43. unsafe { &qdev_prop_bool },
  44. bool
  45. ),
  46. }
  47. unsafe impl ObjectType for DummyState {
  48. type Class = DummyClass;
  49. const TYPE_NAME: &'static CStr = c_str!("dummy");
  50. }
  51. impl ObjectImpl for DummyState {
  52. type ParentType = DeviceState;
  53. const ABSTRACT: bool = false;
  54. }
  55. impl ResettablePhasesImpl for DummyState {}
  56. impl DeviceImpl for DummyState {
  57. fn properties() -> &'static [Property] {
  58. &DUMMY_PROPERTIES
  59. }
  60. fn vmsd() -> Option<&'static VMStateDescription> {
  61. Some(&VMSTATE)
  62. }
  63. }
  64. // `impl<T> ClassInitImpl<DummyClass> for T` doesn't work since it violates
  65. // orphan rule.
  66. impl ClassInitImpl<DummyClass> for DummyState {
  67. fn class_init(klass: &mut DummyClass) {
  68. <Self as ClassInitImpl<DeviceClass>>::class_init(&mut klass.parent_class);
  69. }
  70. }
  71. #[derive(qemu_api_macros::offsets)]
  72. #[repr(C)]
  73. #[derive(qemu_api_macros::Object)]
  74. pub struct DummyChildState {
  75. parent: ParentField<DummyState>,
  76. }
  77. qom_isa!(DummyChildState: Object, DeviceState, DummyState);
  78. pub struct DummyChildClass {
  79. parent_class: <DummyState as ObjectType>::Class,
  80. }
  81. unsafe impl ObjectType for DummyChildState {
  82. type Class = DummyChildClass;
  83. const TYPE_NAME: &'static CStr = c_str!("dummy_child");
  84. }
  85. impl ObjectImpl for DummyChildState {
  86. type ParentType = DummyState;
  87. const ABSTRACT: bool = false;
  88. }
  89. impl ResettablePhasesImpl for DummyChildState {}
  90. impl DeviceImpl for DummyChildState {}
  91. impl ClassInitImpl<DummyClass> for DummyChildState {
  92. fn class_init(klass: &mut DummyClass) {
  93. <Self as ClassInitImpl<DeviceClass>>::class_init(&mut klass.parent_class);
  94. }
  95. }
  96. impl ClassInitImpl<DummyChildClass> for DummyChildState {
  97. fn class_init(klass: &mut DummyChildClass) {
  98. <Self as ClassInitImpl<DummyClass>>::class_init(&mut klass.parent_class);
  99. }
  100. }
  101. fn init_qom() {
  102. static ONCE: BqlCell<bool> = BqlCell::new(false);
  103. cell::bql_start_test();
  104. if !ONCE.get() {
  105. unsafe {
  106. module_call_init(module_init_type::MODULE_INIT_QOM);
  107. }
  108. ONCE.set(true);
  109. }
  110. }
  111. #[test]
  112. /// Create and immediately drop an instance.
  113. fn test_object_new() {
  114. init_qom();
  115. drop(DummyState::new());
  116. drop(DummyChildState::new());
  117. }
  118. #[test]
  119. #[allow(clippy::redundant_clone)]
  120. /// Create, clone and then drop an instance.
  121. fn test_clone() {
  122. init_qom();
  123. let p = DummyState::new();
  124. assert_eq!(p.clone().typename(), "dummy");
  125. drop(p);
  126. }
  127. #[test]
  128. /// Try invoking a method on an object.
  129. fn test_typename() {
  130. init_qom();
  131. let p = DummyState::new();
  132. assert_eq!(p.typename(), "dummy");
  133. }
  134. // a note on all "cast" tests: usually, especially for downcasts the desired
  135. // class would be placed on the right, for example:
  136. //
  137. // let sbd_ref = p.dynamic_cast::<SysBusDevice>();
  138. //
  139. // Here I am doing the opposite to check that the resulting type is correct.
  140. #[test]
  141. #[allow(clippy::shadow_unrelated)]
  142. /// Test casts on shared references.
  143. fn test_cast() {
  144. init_qom();
  145. let p = DummyState::new();
  146. let p_ptr: *mut DummyState = p.as_mut_ptr();
  147. let p_ref: &mut DummyState = unsafe { &mut *p_ptr };
  148. let obj_ref: &Object = p_ref.upcast();
  149. assert_eq!(addr_of!(*obj_ref), p_ptr.cast());
  150. let sbd_ref: Option<&SysBusDevice> = obj_ref.dynamic_cast();
  151. assert!(sbd_ref.is_none());
  152. let dev_ref: Option<&DeviceState> = obj_ref.downcast();
  153. assert_eq!(addr_of!(*dev_ref.unwrap()), p_ptr.cast());
  154. // SAFETY: the cast is wrong, but the value is only used for comparison
  155. unsafe {
  156. let sbd_ref: &SysBusDevice = obj_ref.unsafe_cast();
  157. assert_eq!(addr_of!(*sbd_ref), p_ptr.cast());
  158. }
  159. }
  160. #[test]
  161. #[allow(clippy::shadow_unrelated)]
  162. /// Test casts on mutable references.
  163. fn test_cast_mut() {
  164. init_qom();
  165. let p: *mut DummyState = unsafe { object_new(DummyState::TYPE_NAME.as_ptr()).cast() };
  166. let p_ref: &mut DummyState = unsafe { &mut *p };
  167. let obj_ref: &mut Object = p_ref.upcast_mut();
  168. assert_eq!(addr_of_mut!(*obj_ref), p.cast());
  169. let sbd_ref: Result<&mut SysBusDevice, &mut Object> = obj_ref.dynamic_cast_mut();
  170. let obj_ref = sbd_ref.unwrap_err();
  171. let dev_ref: Result<&mut DeviceState, &mut Object> = obj_ref.downcast_mut();
  172. let dev_ref = dev_ref.unwrap();
  173. assert_eq!(addr_of_mut!(*dev_ref), p.cast());
  174. // SAFETY: the cast is wrong, but the value is only used for comparison
  175. unsafe {
  176. let sbd_ref: &mut SysBusDevice = obj_ref.unsafe_cast_mut();
  177. assert_eq!(addr_of_mut!(*sbd_ref), p.cast());
  178. object_unref(p_ref.as_object_mut_ptr().cast::<c_void>());
  179. }
  180. }