2 #include "qemu-objects.h"
3 #include "test-qmp-commands.h"
4 #include "qapi/qmp-core.h"
7 void qmp_user_def_cmd(Error
**errp
)
11 void qmp_user_def_cmd1(UserDefOne
* ud1
, Error
**errp
)
15 UserDefTwo
* qmp_user_def_cmd2(UserDefOne
* ud1a
, UserDefOne
* ud1b
, Error
**errp
)
18 UserDefOne
*ud1c
= g_malloc0(sizeof(UserDefOne
));
19 UserDefOne
*ud1d
= g_malloc0(sizeof(UserDefOne
));
21 ud1c
->string
= strdup(ud1a
->string
);
22 ud1c
->integer
= ud1a
->integer
;
23 ud1d
->string
= strdup(ud1b
->string
);
24 ud1d
->integer
= ud1b
->integer
;
26 ret
= g_malloc0(sizeof(UserDefTwo
));
27 ret
->string
= strdup("blah1");
28 ret
->dict
.string
= strdup("blah2");
29 ret
->dict
.dict
.userdef
= ud1c
;
30 ret
->dict
.dict
.string
= strdup("blah3");
31 ret
->dict
.has_dict2
= true;
32 ret
->dict
.dict2
.userdef
= ud1d
;
33 ret
->dict
.dict2
.string
= strdup("blah4");
38 /* test commands with no input and no return value */
39 static void test_dispatch_cmd(void)
41 QDict
*req
= qdict_new();
44 qdict_put_obj(req
, "execute", QOBJECT(qstring_from_str("user_def_cmd")));
46 resp
= qmp_dispatch(QOBJECT(req
));
48 assert(!qdict_haskey(qobject_to_qdict(resp
), "error"));
54 /* test commands that return an error due to invalid parameters */
55 static void test_dispatch_cmd_error(void)
57 QDict
*req
= qdict_new();
60 qdict_put_obj(req
, "execute", QOBJECT(qstring_from_str("user_def_cmd2")));
62 resp
= qmp_dispatch(QOBJECT(req
));
64 assert(qdict_haskey(qobject_to_qdict(resp
), "error"));
70 /* test commands that involve both input parameters and return values */
71 static void test_dispatch_cmd_io(void)
73 QDict
*req
= qdict_new();
74 QDict
*args
= qdict_new();
75 QDict
*ud1a
= qdict_new();
76 QDict
*ud1b
= qdict_new();
79 qdict_put_obj(ud1a
, "integer", QOBJECT(qint_from_int(42)));
80 qdict_put_obj(ud1a
, "string", QOBJECT(qstring_from_str("hello")));
81 qdict_put_obj(ud1b
, "integer", QOBJECT(qint_from_int(422)));
82 qdict_put_obj(ud1b
, "string", QOBJECT(qstring_from_str("hello2")));
83 qdict_put_obj(args
, "ud1a", QOBJECT(ud1a
));
84 qdict_put_obj(args
, "ud1b", QOBJECT(ud1b
));
85 qdict_put_obj(req
, "arguments", QOBJECT(args
));
87 qdict_put_obj(req
, "execute", QOBJECT(qstring_from_str("user_def_cmd2")));
89 /* TODO: put in full payload and check for errors */
90 resp
= qmp_dispatch(QOBJECT(req
));
92 assert(!qdict_haskey(qobject_to_qdict(resp
), "error"));
98 /* test generated dealloc functions for generated types */
99 static void test_dealloc_types(void)
101 UserDefOne
*ud1test
, *ud1a
, *ud1b
;
102 UserDefOneList
*ud1list
;
104 ud1test
= g_malloc0(sizeof(UserDefOne
));
105 ud1test
->integer
= 42;
106 ud1test
->string
= g_strdup("hi there 42");
108 qapi_free_UserDefOne(ud1test
);
110 ud1a
= g_malloc0(sizeof(UserDefOne
));
112 ud1a
->string
= g_strdup("hi there 43");
114 ud1b
= g_malloc0(sizeof(UserDefOne
));
116 ud1b
->string
= g_strdup("hi there 44");
118 ud1list
= g_malloc0(sizeof(UserDefOneList
));
119 ud1list
->value
= ud1a
;
120 ud1list
->next
= g_malloc0(sizeof(UserDefOneList
));
121 ud1list
->next
->value
= ud1b
;
123 qapi_free_UserDefOneList(ud1list
);
126 int main(int argc
, char **argv
)
128 g_test_init(&argc
, &argv
, NULL
);
130 g_test_add_func("/0.15/dispatch_cmd", test_dispatch_cmd
);
131 g_test_add_func("/0.15/dispatch_cmd_error", test_dispatch_cmd_error
);
132 g_test_add_func("/0.15/dispatch_cmd_io", test_dispatch_cmd_io
);
133 g_test_add_func("/0.15/dealloc_types", test_dealloc_types
);
135 module_call_init(MODULE_INIT_QAPI
);