Statistics
| Branch: | Revision:

root / test-qmp-commands.c @ 02021812

History | View | Annotate | Download (4 kB)

1 69ed8366 Michael Roth
#include <glib.h>
2 69ed8366 Michael Roth
#include "qemu-objects.h"
3 69ed8366 Michael Roth
#include "test-qmp-commands.h"
4 69ed8366 Michael Roth
#include "qapi/qmp-core.h"
5 69ed8366 Michael Roth
#include "module.h"
6 69ed8366 Michael Roth
7 69ed8366 Michael Roth
void qmp_user_def_cmd(Error **errp)
8 69ed8366 Michael Roth
{
9 69ed8366 Michael Roth
}
10 69ed8366 Michael Roth
11 69ed8366 Michael Roth
void qmp_user_def_cmd1(UserDefOne * ud1, Error **errp)
12 69ed8366 Michael Roth
{
13 69ed8366 Michael Roth
}
14 69ed8366 Michael Roth
15 69ed8366 Michael Roth
UserDefTwo * qmp_user_def_cmd2(UserDefOne * ud1a, UserDefOne * ud1b, Error **errp)
16 69ed8366 Michael Roth
{
17 69ed8366 Michael Roth
    UserDefTwo *ret;
18 7267c094 Anthony Liguori
    UserDefOne *ud1c = g_malloc0(sizeof(UserDefOne));
19 7267c094 Anthony Liguori
    UserDefOne *ud1d = g_malloc0(sizeof(UserDefOne));
20 69ed8366 Michael Roth
21 69ed8366 Michael Roth
    ud1c->string = strdup(ud1a->string);
22 69ed8366 Michael Roth
    ud1c->integer = ud1a->integer;
23 69ed8366 Michael Roth
    ud1d->string = strdup(ud1b->string);
24 69ed8366 Michael Roth
    ud1d->integer = ud1b->integer;
25 69ed8366 Michael Roth
26 7267c094 Anthony Liguori
    ret = g_malloc0(sizeof(UserDefTwo));
27 69ed8366 Michael Roth
    ret->string = strdup("blah1");
28 69ed8366 Michael Roth
    ret->dict.string = strdup("blah2");
29 69ed8366 Michael Roth
    ret->dict.dict.userdef = ud1c;
30 69ed8366 Michael Roth
    ret->dict.dict.string = strdup("blah3");
31 69ed8366 Michael Roth
    ret->dict.has_dict2 = true;
32 69ed8366 Michael Roth
    ret->dict.dict2.userdef = ud1d;
33 69ed8366 Michael Roth
    ret->dict.dict2.string = strdup("blah4");
34 69ed8366 Michael Roth
35 69ed8366 Michael Roth
    return ret;
36 69ed8366 Michael Roth
}
37 69ed8366 Michael Roth
38 69ed8366 Michael Roth
/* test commands with no input and no return value */
39 69ed8366 Michael Roth
static void test_dispatch_cmd(void)
40 69ed8366 Michael Roth
{
41 69ed8366 Michael Roth
    QDict *req = qdict_new();
42 69ed8366 Michael Roth
    QObject *resp;
43 69ed8366 Michael Roth
44 69ed8366 Michael Roth
    qdict_put_obj(req, "execute", QOBJECT(qstring_from_str("user_def_cmd")));
45 69ed8366 Michael Roth
46 69ed8366 Michael Roth
    resp = qmp_dispatch(QOBJECT(req));
47 69ed8366 Michael Roth
    assert(resp != NULL);
48 69ed8366 Michael Roth
    assert(!qdict_haskey(qobject_to_qdict(resp), "error"));
49 69ed8366 Michael Roth
    g_print("\nresp: %s\n", qstring_get_str(qobject_to_json(resp)));
50 69ed8366 Michael Roth
51 69ed8366 Michael Roth
    qobject_decref(resp);
52 69ed8366 Michael Roth
    QDECREF(req);
53 69ed8366 Michael Roth
}
54 69ed8366 Michael Roth
55 69ed8366 Michael Roth
/* test commands that return an error due to invalid parameters */
56 69ed8366 Michael Roth
static void test_dispatch_cmd_error(void)
57 69ed8366 Michael Roth
{
58 69ed8366 Michael Roth
    QDict *req = qdict_new();
59 69ed8366 Michael Roth
    QObject *resp;
60 69ed8366 Michael Roth
61 69ed8366 Michael Roth
    qdict_put_obj(req, "execute", QOBJECT(qstring_from_str("user_def_cmd2")));
62 69ed8366 Michael Roth
63 69ed8366 Michael Roth
    resp = qmp_dispatch(QOBJECT(req));
64 69ed8366 Michael Roth
    assert(resp != NULL);
65 69ed8366 Michael Roth
    assert(qdict_haskey(qobject_to_qdict(resp), "error"));
66 69ed8366 Michael Roth
    g_print("\nresp: %s\n", qstring_get_str(qobject_to_json_pretty(resp)));
67 69ed8366 Michael Roth
68 69ed8366 Michael Roth
    qobject_decref(resp);
69 69ed8366 Michael Roth
    QDECREF(req);
70 69ed8366 Michael Roth
}
71 69ed8366 Michael Roth
72 69ed8366 Michael Roth
/* test commands that involve both input parameters and return values */
73 69ed8366 Michael Roth
static void test_dispatch_cmd_io(void)
74 69ed8366 Michael Roth
{
75 69ed8366 Michael Roth
    QDict *req = qdict_new();
76 69ed8366 Michael Roth
    QDict *args = qdict_new();
77 69ed8366 Michael Roth
    QDict *ud1a = qdict_new();
78 69ed8366 Michael Roth
    QDict *ud1b = qdict_new();
79 69ed8366 Michael Roth
    QObject *resp;
80 69ed8366 Michael Roth
81 69ed8366 Michael Roth
    qdict_put_obj(ud1a, "integer", QOBJECT(qint_from_int(42)));
82 69ed8366 Michael Roth
    qdict_put_obj(ud1a, "string", QOBJECT(qstring_from_str("hello")));
83 69ed8366 Michael Roth
    qdict_put_obj(ud1b, "integer", QOBJECT(qint_from_int(422)));
84 69ed8366 Michael Roth
    qdict_put_obj(ud1b, "string", QOBJECT(qstring_from_str("hello2")));
85 69ed8366 Michael Roth
    qdict_put_obj(args, "ud1a", QOBJECT(ud1a));
86 69ed8366 Michael Roth
    qdict_put_obj(args, "ud1b", QOBJECT(ud1b));
87 69ed8366 Michael Roth
    qdict_put_obj(req, "arguments", QOBJECT(args));
88 69ed8366 Michael Roth
89 69ed8366 Michael Roth
    qdict_put_obj(req, "execute", QOBJECT(qstring_from_str("user_def_cmd2")));
90 69ed8366 Michael Roth
91 69ed8366 Michael Roth
    /* TODO: put in full payload and check for errors */
92 69ed8366 Michael Roth
    resp = qmp_dispatch(QOBJECT(req));
93 69ed8366 Michael Roth
    assert(resp != NULL);
94 69ed8366 Michael Roth
    assert(!qdict_haskey(qobject_to_qdict(resp), "error"));
95 69ed8366 Michael Roth
    g_print("\nresp: %s\n", qstring_get_str(qobject_to_json_pretty(resp)));
96 69ed8366 Michael Roth
97 69ed8366 Michael Roth
    qobject_decref(resp);
98 69ed8366 Michael Roth
    QDECREF(req);
99 69ed8366 Michael Roth
}
100 69ed8366 Michael Roth
101 5cd5f0d0 Michael Roth
/* test generated dealloc functions for generated types */
102 5cd5f0d0 Michael Roth
static void test_dealloc_types(void)
103 5cd5f0d0 Michael Roth
{
104 5cd5f0d0 Michael Roth
    UserDefOne *ud1test, *ud1a, *ud1b;
105 5cd5f0d0 Michael Roth
    UserDefOneList *ud1list;
106 5cd5f0d0 Michael Roth
107 5cd5f0d0 Michael Roth
    ud1test = g_malloc0(sizeof(UserDefOne));
108 5cd5f0d0 Michael Roth
    ud1test->integer = 42;
109 5cd5f0d0 Michael Roth
    ud1test->string = g_strdup("hi there 42");
110 5cd5f0d0 Michael Roth
111 5cd5f0d0 Michael Roth
    qapi_free_UserDefOne(ud1test);
112 5cd5f0d0 Michael Roth
113 5cd5f0d0 Michael Roth
    ud1a = g_malloc0(sizeof(UserDefOne));
114 5cd5f0d0 Michael Roth
    ud1a->integer = 43;
115 5cd5f0d0 Michael Roth
    ud1a->string = g_strdup("hi there 43");
116 5cd5f0d0 Michael Roth
117 5cd5f0d0 Michael Roth
    ud1b = g_malloc0(sizeof(UserDefOne));
118 5cd5f0d0 Michael Roth
    ud1b->integer = 44;
119 5cd5f0d0 Michael Roth
    ud1b->string = g_strdup("hi there 44");
120 5cd5f0d0 Michael Roth
121 5cd5f0d0 Michael Roth
    ud1list = g_malloc0(sizeof(UserDefOneList));
122 5cd5f0d0 Michael Roth
    ud1list->value = ud1a;
123 5cd5f0d0 Michael Roth
    ud1list->next = g_malloc0(sizeof(UserDefOneList));
124 5cd5f0d0 Michael Roth
    ud1list->next->value = ud1b;
125 5cd5f0d0 Michael Roth
126 5cd5f0d0 Michael Roth
    qapi_free_UserDefOneList(ud1list);
127 5cd5f0d0 Michael Roth
}
128 5cd5f0d0 Michael Roth
129 69ed8366 Michael Roth
int main(int argc, char **argv)
130 69ed8366 Michael Roth
{
131 69ed8366 Michael Roth
    g_test_init(&argc, &argv, NULL);
132 69ed8366 Michael Roth
133 69ed8366 Michael Roth
    g_test_add_func("/0.15/dispatch_cmd", test_dispatch_cmd);
134 69ed8366 Michael Roth
    g_test_add_func("/0.15/dispatch_cmd_error", test_dispatch_cmd_error);
135 69ed8366 Michael Roth
    g_test_add_func("/0.15/dispatch_cmd_io", test_dispatch_cmd_io);
136 5cd5f0d0 Michael Roth
    g_test_add_func("/0.15/dealloc_types", test_dealloc_types);
137 69ed8366 Michael Roth
138 69ed8366 Michael Roth
    module_call_init(MODULE_INIT_QAPI);
139 69ed8366 Michael Roth
    g_test_run();
140 69ed8366 Michael Roth
141 69ed8366 Michael Roth
    return 0;
142 69ed8366 Michael Roth
}