tests.rs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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::CStr,
  6. os::raw::c_void,
  7. ptr::{addr_of, addr_of_mut},
  8. };
  9. use qemu_api::{
  10. bindings::*,
  11. c_str,
  12. cell::{self, BqlCell},
  13. declare_properties, define_property,
  14. prelude::*,
  15. qdev::{DeviceImpl, DeviceState, Property},
  16. qom::{ObjectImpl, ParentField},
  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. 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. unsafe impl ObjectType for DummyState {
  45. type Class = <DeviceState as ObjectType>::Class;
  46. const TYPE_NAME: &'static CStr = c_str!("dummy");
  47. }
  48. impl ObjectImpl for DummyState {
  49. type ParentType = DeviceState;
  50. const ABSTRACT: bool = false;
  51. }
  52. impl DeviceImpl for DummyState {
  53. fn properties() -> &'static [Property] {
  54. &DUMMY_PROPERTIES
  55. }
  56. fn vmsd() -> Option<&'static VMStateDescription> {
  57. Some(&VMSTATE)
  58. }
  59. }
  60. fn init_qom() {
  61. static ONCE: BqlCell<bool> = BqlCell::new(false);
  62. cell::bql_start_test();
  63. if !ONCE.get() {
  64. unsafe {
  65. module_call_init(module_init_type::MODULE_INIT_QOM);
  66. }
  67. ONCE.set(true);
  68. }
  69. }
  70. #[test]
  71. /// Create and immediately drop an instance.
  72. fn test_object_new() {
  73. init_qom();
  74. unsafe {
  75. object_unref(object_new(DummyState::TYPE_NAME.as_ptr()).cast());
  76. }
  77. }
  78. #[test]
  79. /// Try invoking a method on an object.
  80. fn test_typename() {
  81. init_qom();
  82. let p: *mut DummyState = unsafe { object_new(DummyState::TYPE_NAME.as_ptr()).cast() };
  83. let p_ref: &DummyState = unsafe { &*p };
  84. assert_eq!(p_ref.typename(), "dummy");
  85. unsafe {
  86. object_unref(p_ref.as_object_mut_ptr().cast::<c_void>());
  87. }
  88. }
  89. // a note on all "cast" tests: usually, especially for downcasts the desired
  90. // class would be placed on the right, for example:
  91. //
  92. // let sbd_ref = p.dynamic_cast::<SysBusDevice>();
  93. //
  94. // Here I am doing the opposite to check that the resulting type is correct.
  95. #[test]
  96. #[allow(clippy::shadow_unrelated)]
  97. /// Test casts on shared references.
  98. fn test_cast() {
  99. init_qom();
  100. let p: *mut DummyState = unsafe { object_new(DummyState::TYPE_NAME.as_ptr()).cast() };
  101. let p_ref: &DummyState = unsafe { &*p };
  102. let obj_ref: &Object = p_ref.upcast();
  103. assert_eq!(addr_of!(*obj_ref), p.cast());
  104. let sbd_ref: Option<&SysBusDevice> = obj_ref.dynamic_cast();
  105. assert!(sbd_ref.is_none());
  106. let dev_ref: Option<&DeviceState> = obj_ref.downcast();
  107. assert_eq!(addr_of!(*dev_ref.unwrap()), p.cast());
  108. // SAFETY: the cast is wrong, but the value is only used for comparison
  109. unsafe {
  110. let sbd_ref: &SysBusDevice = obj_ref.unsafe_cast();
  111. assert_eq!(addr_of!(*sbd_ref), p.cast());
  112. object_unref(p_ref.as_object_mut_ptr().cast::<c_void>());
  113. }
  114. }
  115. #[test]
  116. #[allow(clippy::shadow_unrelated)]
  117. /// Test casts on mutable references.
  118. fn test_cast_mut() {
  119. init_qom();
  120. let p: *mut DummyState = unsafe { object_new(DummyState::TYPE_NAME.as_ptr()).cast() };
  121. let p_ref: &mut DummyState = unsafe { &mut *p };
  122. let obj_ref: &mut Object = p_ref.upcast_mut();
  123. assert_eq!(addr_of_mut!(*obj_ref), p.cast());
  124. let sbd_ref: Result<&mut SysBusDevice, &mut Object> = obj_ref.dynamic_cast_mut();
  125. let obj_ref = sbd_ref.unwrap_err();
  126. let dev_ref: Result<&mut DeviceState, &mut Object> = obj_ref.downcast_mut();
  127. let dev_ref = dev_ref.unwrap();
  128. assert_eq!(addr_of_mut!(*dev_ref), p.cast());
  129. // SAFETY: the cast is wrong, but the value is only used for comparison
  130. unsafe {
  131. let sbd_ref: &mut SysBusDevice = obj_ref.unsafe_cast_mut();
  132. assert_eq!(addr_of_mut!(*sbd_ref), p.cast());
  133. object_unref(p_ref.as_object_mut_ptr().cast::<c_void>());
  134. }
  135. }