tests: make threadpool cancellation test looser
[qemu.git] / tests / test-qmp-commands.c
blobdc3c507f2b1e395ab1d1f1cb8d4230857a9422d4
1 #include <glib.h>
2 #include "qemu-objects.h"
3 #include "test-qmp-commands.h"
4 #include "qapi/qmp-core.h"
5 #include "module.h"
6 #include "qapi/qmp-input-visitor.h"
7 #include "tests/test-qapi-types.h"
8 #include "tests/test-qapi-visit.h"
10 void qmp_user_def_cmd(Error **errp)
14 void qmp_user_def_cmd1(UserDefOne * ud1, Error **errp)
18 UserDefTwo * qmp_user_def_cmd2(UserDefOne * ud1a, UserDefOne * ud1b, Error **errp)
20 UserDefTwo *ret;
21 UserDefOne *ud1c = g_malloc0(sizeof(UserDefOne));
22 UserDefOne *ud1d = g_malloc0(sizeof(UserDefOne));
24 ud1c->string = strdup(ud1a->string);
25 ud1c->integer = ud1a->integer;
26 ud1d->string = strdup(ud1b->string);
27 ud1d->integer = ud1b->integer;
29 ret = g_malloc0(sizeof(UserDefTwo));
30 ret->string = strdup("blah1");
31 ret->dict.string = strdup("blah2");
32 ret->dict.dict.userdef = ud1c;
33 ret->dict.dict.string = strdup("blah3");
34 ret->dict.has_dict2 = true;
35 ret->dict.dict2.userdef = ud1d;
36 ret->dict.dict2.string = strdup("blah4");
38 return ret;
41 /* test commands with no input and no return value */
42 static void test_dispatch_cmd(void)
44 QDict *req = qdict_new();
45 QObject *resp;
47 qdict_put_obj(req, "execute", QOBJECT(qstring_from_str("user_def_cmd")));
49 resp = qmp_dispatch(QOBJECT(req));
50 assert(resp != NULL);
51 assert(!qdict_haskey(qobject_to_qdict(resp), "error"));
53 qobject_decref(resp);
54 QDECREF(req);
57 /* test commands that return an error due to invalid parameters */
58 static void test_dispatch_cmd_error(void)
60 QDict *req = qdict_new();
61 QObject *resp;
63 qdict_put_obj(req, "execute", QOBJECT(qstring_from_str("user_def_cmd2")));
65 resp = qmp_dispatch(QOBJECT(req));
66 assert(resp != NULL);
67 assert(qdict_haskey(qobject_to_qdict(resp), "error"));
69 qobject_decref(resp);
70 QDECREF(req);
73 /* test commands that involve both input parameters and return values */
74 static void test_dispatch_cmd_io(void)
76 QDict *req = qdict_new();
77 QDict *args = qdict_new();
78 QDict *ud1a = qdict_new();
79 QDict *ud1b = qdict_new();
80 QObject *resp;
82 qdict_put_obj(ud1a, "integer", QOBJECT(qint_from_int(42)));
83 qdict_put_obj(ud1a, "string", QOBJECT(qstring_from_str("hello")));
84 qdict_put_obj(ud1b, "integer", QOBJECT(qint_from_int(422)));
85 qdict_put_obj(ud1b, "string", QOBJECT(qstring_from_str("hello2")));
86 qdict_put_obj(args, "ud1a", QOBJECT(ud1a));
87 qdict_put_obj(args, "ud1b", QOBJECT(ud1b));
88 qdict_put_obj(req, "arguments", QOBJECT(args));
90 qdict_put_obj(req, "execute", QOBJECT(qstring_from_str("user_def_cmd2")));
92 /* TODO: put in full payload and check for errors */
93 resp = qmp_dispatch(QOBJECT(req));
94 assert(resp != NULL);
95 assert(!qdict_haskey(qobject_to_qdict(resp), "error"));
97 qobject_decref(resp);
98 QDECREF(req);
101 /* test generated dealloc functions for generated types */
102 static void test_dealloc_types(void)
104 UserDefOne *ud1test, *ud1a, *ud1b;
105 UserDefOneList *ud1list;
107 ud1test = g_malloc0(sizeof(UserDefOne));
108 ud1test->integer = 42;
109 ud1test->string = g_strdup("hi there 42");
111 qapi_free_UserDefOne(ud1test);
113 ud1a = g_malloc0(sizeof(UserDefOne));
114 ud1a->integer = 43;
115 ud1a->string = g_strdup("hi there 43");
117 ud1b = g_malloc0(sizeof(UserDefOne));
118 ud1b->integer = 44;
119 ud1b->string = g_strdup("hi there 44");
121 ud1list = g_malloc0(sizeof(UserDefOneList));
122 ud1list->value = ud1a;
123 ud1list->next = g_malloc0(sizeof(UserDefOneList));
124 ud1list->next->value = ud1b;
126 qapi_free_UserDefOneList(ud1list);
129 /* test generated deallocation on an object whose construction was prematurely
130 * terminated due to an error */
131 static void test_dealloc_partial(void)
133 static const char text[] = "don't leak me";
135 UserDefTwo *ud2 = NULL;
136 Error *err = NULL;
138 /* create partial object */
140 QDict *ud2_dict;
141 QmpInputVisitor *qiv;
143 ud2_dict = qdict_new();
144 qdict_put_obj(ud2_dict, "string", QOBJECT(qstring_from_str(text)));
146 qiv = qmp_input_visitor_new(QOBJECT(ud2_dict));
147 visit_type_UserDefTwo(qmp_input_get_visitor(qiv), &ud2, NULL, &err);
148 qmp_input_visitor_cleanup(qiv);
149 QDECREF(ud2_dict);
152 /* verify partial success */
153 assert(ud2 != NULL);
154 assert(ud2->string != NULL);
155 assert(strcmp(ud2->string, text) == 0);
156 assert(ud2->dict.dict.userdef == NULL);
158 /* confirm & release construction error */
159 assert(err != NULL);
160 error_free(err);
162 /* tear down partial object */
163 qapi_free_UserDefTwo(ud2);
167 int main(int argc, char **argv)
169 g_test_init(&argc, &argv, NULL);
171 g_test_add_func("/0.15/dispatch_cmd", test_dispatch_cmd);
172 g_test_add_func("/0.15/dispatch_cmd_error", test_dispatch_cmd_error);
173 g_test_add_func("/0.15/dispatch_cmd_io", test_dispatch_cmd_io);
174 g_test_add_func("/0.15/dealloc_types", test_dealloc_types);
175 g_test_add_func("/0.15/dealloc_partial", test_dealloc_partial);
177 module_call_init(MODULE_INIT_QAPI);
178 g_test_run();
180 return 0;