2
0

tests.rs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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::ffi::CStr;
  5. use qemu_api::{
  6. bindings::*,
  7. c_str,
  8. cell::{self, BqlCell},
  9. declare_properties, define_property,
  10. prelude::*,
  11. qdev::{DeviceImpl, DeviceState, Property},
  12. qom::ObjectImpl,
  13. vmstate::VMStateDescription,
  14. zeroable::Zeroable,
  15. };
  16. // Test that macros can compile.
  17. pub static VMSTATE: VMStateDescription = VMStateDescription {
  18. name: c_str!("name").as_ptr(),
  19. unmigratable: true,
  20. ..Zeroable::ZERO
  21. };
  22. #[derive(qemu_api_macros::offsets)]
  23. #[repr(C)]
  24. #[derive(qemu_api_macros::Object)]
  25. pub struct DummyState {
  26. parent: DeviceState,
  27. migrate_clock: bool,
  28. }
  29. declare_properties! {
  30. DUMMY_PROPERTIES,
  31. define_property!(
  32. c_str!("migrate-clk"),
  33. DummyState,
  34. migrate_clock,
  35. unsafe { &qdev_prop_bool },
  36. bool
  37. ),
  38. }
  39. unsafe impl ObjectType for DummyState {
  40. type Class = <DeviceState as ObjectType>::Class;
  41. const TYPE_NAME: &'static CStr = c_str!("dummy");
  42. }
  43. impl ObjectImpl for DummyState {
  44. type ParentType = DeviceState;
  45. const ABSTRACT: bool = false;
  46. }
  47. impl DeviceImpl for DummyState {
  48. fn properties() -> &'static [Property] {
  49. &DUMMY_PROPERTIES
  50. }
  51. fn vmsd() -> Option<&'static VMStateDescription> {
  52. Some(&VMSTATE)
  53. }
  54. }
  55. fn init_qom() {
  56. static ONCE: BqlCell<bool> = BqlCell::new(false);
  57. cell::bql_start_test();
  58. if !ONCE.get() {
  59. unsafe {
  60. module_call_init(module_init_type::MODULE_INIT_QOM);
  61. }
  62. ONCE.set(true);
  63. }
  64. }
  65. #[test]
  66. /// Create and immediately drop an instance.
  67. fn test_object_new() {
  68. init_qom();
  69. unsafe {
  70. object_unref(object_new(DummyState::TYPE_NAME.as_ptr()).cast());
  71. }
  72. }