2
0

test-qdev-global-props.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*
  2. * Test code for qdev global-properties handling
  3. *
  4. * Copyright (c) 2012 Red Hat Inc.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "qemu/osdep.h"
  25. #include "hw/qdev-properties.h"
  26. #include "qom/object.h"
  27. #include "qapi/visitor.h"
  28. #define TYPE_STATIC_PROPS "static_prop_type"
  29. #define STATIC_TYPE(obj) \
  30. OBJECT_CHECK(MyType, (obj), TYPE_STATIC_PROPS)
  31. #define TYPE_SUBCLASS "static_prop_subtype"
  32. #define PROP_DEFAULT 100
  33. typedef struct MyType {
  34. DeviceState parent_obj;
  35. uint32_t prop1;
  36. uint32_t prop2;
  37. } MyType;
  38. static Property static_props[] = {
  39. DEFINE_PROP_UINT32("prop1", MyType, prop1, PROP_DEFAULT),
  40. DEFINE_PROP_UINT32("prop2", MyType, prop2, PROP_DEFAULT),
  41. DEFINE_PROP_END_OF_LIST()
  42. };
  43. static void static_prop_class_init(ObjectClass *oc, void *data)
  44. {
  45. DeviceClass *dc = DEVICE_CLASS(oc);
  46. dc->realize = NULL;
  47. dc->props = static_props;
  48. }
  49. static const TypeInfo static_prop_type = {
  50. .name = TYPE_STATIC_PROPS,
  51. .parent = TYPE_DEVICE,
  52. .instance_size = sizeof(MyType),
  53. .class_init = static_prop_class_init,
  54. };
  55. static const TypeInfo subclass_type = {
  56. .name = TYPE_SUBCLASS,
  57. .parent = TYPE_STATIC_PROPS,
  58. };
  59. /* Test simple static property setting to default value */
  60. static void test_static_prop_subprocess(void)
  61. {
  62. MyType *mt;
  63. mt = STATIC_TYPE(object_new(TYPE_STATIC_PROPS));
  64. qdev_init_nofail(DEVICE(mt));
  65. g_assert_cmpuint(mt->prop1, ==, PROP_DEFAULT);
  66. }
  67. static void test_static_prop(void)
  68. {
  69. g_test_trap_subprocess("/qdev/properties/static/default/subprocess", 0, 0);
  70. g_test_trap_assert_passed();
  71. g_test_trap_assert_stderr("");
  72. g_test_trap_assert_stdout("");
  73. }
  74. static void register_global_properties(GlobalProperty *props)
  75. {
  76. int i;
  77. for (i = 0; props[i].driver != NULL; i++) {
  78. qdev_prop_register_global(props + i);
  79. }
  80. }
  81. /* Test setting of static property using global properties */
  82. static void test_static_globalprop_subprocess(void)
  83. {
  84. MyType *mt;
  85. static GlobalProperty props[] = {
  86. { TYPE_STATIC_PROPS, "prop1", "200" },
  87. {}
  88. };
  89. register_global_properties(props);
  90. mt = STATIC_TYPE(object_new(TYPE_STATIC_PROPS));
  91. qdev_init_nofail(DEVICE(mt));
  92. g_assert_cmpuint(mt->prop1, ==, 200);
  93. g_assert_cmpuint(mt->prop2, ==, PROP_DEFAULT);
  94. }
  95. static void test_static_globalprop(void)
  96. {
  97. g_test_trap_subprocess("/qdev/properties/static/global/subprocess", 0, 0);
  98. g_test_trap_assert_passed();
  99. g_test_trap_assert_stderr("");
  100. g_test_trap_assert_stdout("");
  101. }
  102. #define TYPE_DYNAMIC_PROPS "dynamic-prop-type"
  103. #define DYNAMIC_TYPE(obj) \
  104. OBJECT_CHECK(MyType, (obj), TYPE_DYNAMIC_PROPS)
  105. #define TYPE_UNUSED_HOTPLUG "hotplug-type"
  106. #define TYPE_UNUSED_NOHOTPLUG "nohotplug-type"
  107. static void prop1_accessor(Object *obj, Visitor *v, const char *name,
  108. void *opaque, Error **errp)
  109. {
  110. MyType *mt = DYNAMIC_TYPE(obj);
  111. visit_type_uint32(v, name, &mt->prop1, errp);
  112. }
  113. static void prop2_accessor(Object *obj, Visitor *v, const char *name,
  114. void *opaque, Error **errp)
  115. {
  116. MyType *mt = DYNAMIC_TYPE(obj);
  117. visit_type_uint32(v, name, &mt->prop2, errp);
  118. }
  119. static void dynamic_instance_init(Object *obj)
  120. {
  121. object_property_add(obj, "prop1", "uint32", prop1_accessor, prop1_accessor,
  122. NULL, NULL, NULL);
  123. object_property_add(obj, "prop2", "uint32", prop2_accessor, prop2_accessor,
  124. NULL, NULL, NULL);
  125. }
  126. static void dynamic_class_init(ObjectClass *oc, void *data)
  127. {
  128. DeviceClass *dc = DEVICE_CLASS(oc);
  129. dc->realize = NULL;
  130. }
  131. static const TypeInfo dynamic_prop_type = {
  132. .name = TYPE_DYNAMIC_PROPS,
  133. .parent = TYPE_DEVICE,
  134. .instance_size = sizeof(MyType),
  135. .instance_init = dynamic_instance_init,
  136. .class_init = dynamic_class_init,
  137. };
  138. static void hotplug_class_init(ObjectClass *oc, void *data)
  139. {
  140. DeviceClass *dc = DEVICE_CLASS(oc);
  141. dc->realize = NULL;
  142. dc->hotpluggable = true;
  143. }
  144. static const TypeInfo hotplug_type = {
  145. .name = TYPE_UNUSED_HOTPLUG,
  146. .parent = TYPE_DEVICE,
  147. .instance_size = sizeof(MyType),
  148. .instance_init = dynamic_instance_init,
  149. .class_init = hotplug_class_init,
  150. };
  151. static void nohotplug_class_init(ObjectClass *oc, void *data)
  152. {
  153. DeviceClass *dc = DEVICE_CLASS(oc);
  154. dc->realize = NULL;
  155. dc->hotpluggable = false;
  156. }
  157. static const TypeInfo nohotplug_type = {
  158. .name = TYPE_UNUSED_NOHOTPLUG,
  159. .parent = TYPE_DEVICE,
  160. .instance_size = sizeof(MyType),
  161. .instance_init = dynamic_instance_init,
  162. .class_init = nohotplug_class_init,
  163. };
  164. #define TYPE_NONDEVICE "nondevice-type"
  165. static const TypeInfo nondevice_type = {
  166. .name = TYPE_NONDEVICE,
  167. .parent = TYPE_OBJECT,
  168. };
  169. /* Test setting of dynamic properties using global properties */
  170. static void test_dynamic_globalprop_subprocess(void)
  171. {
  172. MyType *mt;
  173. static GlobalProperty props[] = {
  174. { TYPE_DYNAMIC_PROPS, "prop1", "101", },
  175. { TYPE_DYNAMIC_PROPS, "prop2", "102", },
  176. { TYPE_DYNAMIC_PROPS"-bad", "prop3", "103", },
  177. { TYPE_UNUSED_HOTPLUG, "prop4", "104", },
  178. { TYPE_UNUSED_NOHOTPLUG, "prop5", "105", },
  179. { TYPE_NONDEVICE, "prop6", "106", },
  180. {}
  181. };
  182. int global_error;
  183. register_global_properties(props);
  184. mt = DYNAMIC_TYPE(object_new(TYPE_DYNAMIC_PROPS));
  185. qdev_init_nofail(DEVICE(mt));
  186. g_assert_cmpuint(mt->prop1, ==, 101);
  187. g_assert_cmpuint(mt->prop2, ==, 102);
  188. global_error = qdev_prop_check_globals();
  189. g_assert_cmpuint(global_error, ==, 1);
  190. g_assert(props[0].used);
  191. g_assert(props[1].used);
  192. g_assert(!props[2].used);
  193. g_assert(!props[3].used);
  194. g_assert(!props[4].used);
  195. g_assert(!props[5].used);
  196. }
  197. static void test_dynamic_globalprop(void)
  198. {
  199. g_test_trap_subprocess("/qdev/properties/dynamic/global/subprocess", 0, 0);
  200. g_test_trap_assert_passed();
  201. g_test_trap_assert_stderr_unmatched("*prop1*");
  202. g_test_trap_assert_stderr_unmatched("*prop2*");
  203. g_test_trap_assert_stderr("*warning: global dynamic-prop-type-bad.prop3 has invalid class name\n*");
  204. g_test_trap_assert_stderr_unmatched("*prop4*");
  205. g_test_trap_assert_stderr("*warning: global nohotplug-type.prop5=105 not used\n*");
  206. g_test_trap_assert_stderr("*warning: global nondevice-type.prop6 has invalid class name\n*");
  207. g_test_trap_assert_stdout("");
  208. }
  209. /* Test if global props affecting subclasses are applied in the right order */
  210. static void test_subclass_global_props(void)
  211. {
  212. MyType *mt;
  213. /* Global properties must be applied in the order they were registered */
  214. static GlobalProperty props[] = {
  215. { TYPE_STATIC_PROPS, "prop1", "101" },
  216. { TYPE_SUBCLASS, "prop1", "102" },
  217. { TYPE_SUBCLASS, "prop2", "103" },
  218. { TYPE_STATIC_PROPS, "prop2", "104" },
  219. {}
  220. };
  221. register_global_properties(props);
  222. mt = STATIC_TYPE(object_new(TYPE_SUBCLASS));
  223. qdev_init_nofail(DEVICE(mt));
  224. g_assert_cmpuint(mt->prop1, ==, 102);
  225. g_assert_cmpuint(mt->prop2, ==, 104);
  226. }
  227. int main(int argc, char **argv)
  228. {
  229. g_test_init(&argc, &argv, NULL);
  230. module_call_init(MODULE_INIT_QOM);
  231. type_register_static(&static_prop_type);
  232. type_register_static(&subclass_type);
  233. type_register_static(&dynamic_prop_type);
  234. type_register_static(&hotplug_type);
  235. type_register_static(&nohotplug_type);
  236. type_register_static(&nondevice_type);
  237. g_test_add_func("/qdev/properties/static/default/subprocess",
  238. test_static_prop_subprocess);
  239. g_test_add_func("/qdev/properties/static/default",
  240. test_static_prop);
  241. g_test_add_func("/qdev/properties/static/global/subprocess",
  242. test_static_globalprop_subprocess);
  243. g_test_add_func("/qdev/properties/static/global",
  244. test_static_globalprop);
  245. g_test_add_func("/qdev/properties/dynamic/global/subprocess",
  246. test_dynamic_globalprop_subprocess);
  247. g_test_add_func("/qdev/properties/dynamic/global",
  248. test_dynamic_globalprop);
  249. g_test_add_func("/qdev/properties/global/subclass",
  250. test_subclass_global_props);
  251. g_test_run();
  252. return 0;
  253. }