Peter Maydell | 681c28a | 2016-02-08 18:08:51 +0000 | [diff] [blame] | 1 | #include "qemu/osdep.h" |
Markus Armbruster | 91fa93e | 2021-03-18 16:55:11 +0100 | [diff] [blame] | 2 | #include "qapi/compat-policy.h" |
Markus Armbruster | 6b67395 | 2018-02-01 12:18:35 +0100 | [diff] [blame] | 3 | #include "qapi/qmp/qdict.h" |
Markus Armbruster | 3d16042 | 2020-03-17 12:54:32 +0100 | [diff] [blame] | 4 | #include "qapi/qmp/qjson.h" |
Markus Armbruster | 15280c3 | 2018-02-01 12:18:36 +0100 | [diff] [blame] | 5 | #include "qapi/qmp/qnum.h" |
Markus Armbruster | 6b67395 | 2018-02-01 12:18:35 +0100 | [diff] [blame] | 6 | #include "qapi/qmp/qstring.h" |
Markus Armbruster | e688df6 | 2018-02-01 12:18:31 +0100 | [diff] [blame] | 7 | #include "qapi/error.h" |
Daniel P. Berrange | b3db211 | 2016-09-30 15:45:27 +0100 | [diff] [blame] | 8 | #include "qapi/qobject-input-visitor.h" |
Laszlo Ersek | d98150f | 2012-07-17 16:17:05 +0200 | [diff] [blame] | 9 | #include "tests/test-qapi-types.h" |
| 10 | #include "tests/test-qapi-visit.h" |
Markus Armbruster | eb815e2 | 2018-02-11 10:36:05 +0100 | [diff] [blame] | 11 | #include "test-qapi-commands.h" |
Markus Armbruster | 00ca24f | 2019-11-20 19:25:48 +0100 | [diff] [blame] | 12 | #include "test-qapi-init-commands.h" |
Michael Roth | 69ed836 | 2011-07-19 14:50:45 -0500 | [diff] [blame] | 13 | |
Markus Armbruster | 1527bad | 2017-03-03 13:32:25 +0100 | [diff] [blame] | 14 | static QmpCommandList qmp_commands; |
| 15 | |
Markus Armbruster | 05ebf84 | 2021-03-23 10:40:21 +0100 | [diff] [blame] | 16 | UserDefThree *qmp_test_cmd_return_def_three(Error **errp) |
Marc-André Lureau | 967c885 | 2018-07-03 17:56:35 +0200 | [diff] [blame] | 17 | { |
| 18 | return NULL; |
| 19 | } |
| 20 | |
Michael Roth | 69ed836 | 2011-07-19 14:50:45 -0500 | [diff] [blame] | 21 | void qmp_user_def_cmd(Error **errp) |
| 22 | { |
| 23 | } |
| 24 | |
Igor Mammedov | 7b13f2c | 2018-05-11 19:15:59 +0200 | [diff] [blame] | 25 | void qmp_test_flags_command(Error **errp) |
Peter Xu | 1a1b11d | 2018-03-26 14:38:59 +0800 | [diff] [blame] | 26 | { |
| 27 | } |
| 28 | |
Marc-André Lureau | ae6bf76 | 2018-08-30 17:58:10 +0200 | [diff] [blame] | 29 | void qmp_cmd_success_response(Error **errp) |
| 30 | { |
| 31 | } |
| 32 | |
Kevin Wolf | 04f2236 | 2020-10-05 17:58:49 +0200 | [diff] [blame] | 33 | void qmp_coroutine_cmd(Error **errp) |
| 34 | { |
| 35 | } |
| 36 | |
Eric Blake | 972a110 | 2016-03-17 16:48:27 -0600 | [diff] [blame] | 37 | Empty2 *qmp_user_def_cmd0(Error **errp) |
| 38 | { |
| 39 | return g_new0(Empty2, 1); |
| 40 | } |
| 41 | |
Michael Roth | 69ed836 | 2011-07-19 14:50:45 -0500 | [diff] [blame] | 42 | void qmp_user_def_cmd1(UserDefOne * ud1, Error **errp) |
| 43 | { |
| 44 | } |
| 45 | |
Markus Armbruster | 91fa93e | 2021-03-18 16:55:11 +0100 | [diff] [blame] | 46 | FeatureStruct1 *qmp_test_features0(bool has_fs0, FeatureStruct0 *fs0, |
| 47 | bool has_fs1, FeatureStruct1 *fs1, |
| 48 | bool has_fs2, FeatureStruct2 *fs2, |
| 49 | bool has_fs3, FeatureStruct3 *fs3, |
| 50 | bool has_fs4, FeatureStruct4 *fs4, |
| 51 | bool has_cfs1, CondFeatureStruct1 *cfs1, |
| 52 | bool has_cfs2, CondFeatureStruct2 *cfs2, |
| 53 | bool has_cfs3, CondFeatureStruct3 *cfs3, |
Marc-André Lureau | 3ad64ed | 2021-08-04 12:31:02 +0400 | [diff] [blame] | 54 | bool has_cfs4, CondFeatureStruct4 *cfs4, |
Markus Armbruster | 91fa93e | 2021-03-18 16:55:11 +0100 | [diff] [blame] | 55 | Error **errp) |
Kevin Wolf | 8aa3a33 | 2019-06-06 17:37:58 +0200 | [diff] [blame] | 56 | { |
Markus Armbruster | 91fa93e | 2021-03-18 16:55:11 +0100 | [diff] [blame] | 57 | return g_new0(FeatureStruct1, 1); |
Kevin Wolf | 8aa3a33 | 2019-06-06 17:37:58 +0200 | [diff] [blame] | 58 | } |
| 59 | |
Peter Krempa | 2e2e0df2 | 2019-10-18 10:14:52 +0200 | [diff] [blame] | 60 | void qmp_test_command_features1(Error **errp) |
| 61 | { |
| 62 | } |
| 63 | |
| 64 | void qmp_test_command_features3(Error **errp) |
| 65 | { |
| 66 | } |
| 67 | |
| 68 | void qmp_test_command_cond_features1(Error **errp) |
| 69 | { |
| 70 | } |
| 71 | |
| 72 | void qmp_test_command_cond_features2(Error **errp) |
| 73 | { |
| 74 | } |
| 75 | |
| 76 | void qmp_test_command_cond_features3(Error **errp) |
| 77 | { |
| 78 | } |
| 79 | |
Markus Armbruster | ab22ad9 | 2014-03-01 08:40:28 +0100 | [diff] [blame] | 80 | UserDefTwo *qmp_user_def_cmd2(UserDefOne *ud1a, |
| 81 | bool has_udb1, UserDefOne *ud1b, |
| 82 | Error **errp) |
Michael Roth | 69ed836 | 2011-07-19 14:50:45 -0500 | [diff] [blame] | 83 | { |
| 84 | UserDefTwo *ret; |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 85 | UserDefOne *ud1c = g_malloc0(sizeof(UserDefOne)); |
| 86 | UserDefOne *ud1d = g_malloc0(sizeof(UserDefOne)); |
Michael Roth | 69ed836 | 2011-07-19 14:50:45 -0500 | [diff] [blame] | 87 | |
| 88 | ud1c->string = strdup(ud1a->string); |
Eric Blake | ddf2190 | 2015-10-26 16:34:49 -0600 | [diff] [blame] | 89 | ud1c->integer = ud1a->integer; |
Markus Armbruster | ab22ad9 | 2014-03-01 08:40:28 +0100 | [diff] [blame] | 90 | ud1d->string = strdup(has_udb1 ? ud1b->string : "blah0"); |
Eric Blake | ddf2190 | 2015-10-26 16:34:49 -0600 | [diff] [blame] | 91 | ud1d->integer = has_udb1 ? ud1b->integer : 0; |
Michael Roth | 69ed836 | 2011-07-19 14:50:45 -0500 | [diff] [blame] | 92 | |
Eric Blake | b6fcf32 | 2015-05-04 09:05:29 -0600 | [diff] [blame] | 93 | ret = g_new0(UserDefTwo, 1); |
| 94 | ret->string0 = strdup("blah1"); |
Eric Blake | 6446a59 | 2015-05-04 09:05:30 -0600 | [diff] [blame] | 95 | ret->dict1 = g_new0(UserDefTwoDict, 1); |
| 96 | ret->dict1->string1 = strdup("blah2"); |
| 97 | ret->dict1->dict2 = g_new0(UserDefTwoDictDict, 1); |
| 98 | ret->dict1->dict2->userdef = ud1c; |
| 99 | ret->dict1->dict2->string = strdup("blah3"); |
| 100 | ret->dict1->dict3 = g_new0(UserDefTwoDictDict, 1); |
| 101 | ret->dict1->has_dict3 = true; |
| 102 | ret->dict1->dict3->userdef = ud1d; |
| 103 | ret->dict1->dict3->string = strdup("blah4"); |
Michael Roth | 69ed836 | 2011-07-19 14:50:45 -0500 | [diff] [blame] | 104 | |
| 105 | return ret; |
| 106 | } |
| 107 | |
Eric Blake | cae95ea | 2015-10-12 22:22:25 -0600 | [diff] [blame] | 108 | int64_t qmp_guest_get_time(int64_t a, bool has_b, int64_t b, Error **errp) |
Markus Armbruster | c2216a8 | 2014-03-01 08:40:29 +0100 | [diff] [blame] | 109 | { |
| 110 | return a + (has_b ? b : 0); |
| 111 | } |
| 112 | |
Markus Armbruster | 28770e0 | 2015-09-16 13:06:24 +0200 | [diff] [blame] | 113 | QObject *qmp_guest_sync(QObject *arg, Error **errp) |
| 114 | { |
| 115 | return arg; |
| 116 | } |
| 117 | |
Eric Blake | c818408 | 2016-07-13 21:50:20 -0600 | [diff] [blame] | 118 | void qmp_boxed_struct(UserDefZero *arg, Error **errp) |
| 119 | { |
| 120 | } |
| 121 | |
Markus Armbruster | 1e65e16 | 2021-09-17 16:31:26 +0200 | [diff] [blame] | 122 | void qmp_boxed_union(UserDefFlatUnion *arg, Error **errp) |
Eric Blake | c818408 | 2016-07-13 21:50:20 -0600 | [diff] [blame] | 123 | { |
| 124 | } |
| 125 | |
Markus Armbruster | 675b214 | 2019-09-13 22:13:41 +0200 | [diff] [blame] | 126 | void qmp_boxed_empty(Empty1 *arg, Error **errp) |
| 127 | { |
| 128 | } |
| 129 | |
Markus Armbruster | bb5821d | 2021-09-17 16:31:30 +0200 | [diff] [blame] | 130 | void qmp___org_qemu_x_command(__org_qemu_x_EnumList *a, |
| 131 | __org_qemu_x_StructList *b, |
| 132 | __org_qemu_x_Union *c, |
| 133 | __org_qemu_x_Alt *d, |
| 134 | Error **errp) |
Eric Blake | e3c4c3d | 2015-05-14 06:51:01 -0600 | [diff] [blame] | 135 | { |
Eric Blake | c43567c | 2015-11-18 01:52:52 -0700 | [diff] [blame] | 136 | /* Also test that 'wchar-t' was munged to 'q_wchar_t' */ |
| 137 | if (b && b->value && !b->value->has_q_wchar_t) { |
| 138 | b->value->q_wchar_t = 1; |
| 139 | } |
Eric Blake | e3c4c3d | 2015-05-14 06:51:01 -0600 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | |
Markus Armbruster | 3d16042 | 2020-03-17 12:54:32 +0100 | [diff] [blame] | 143 | static QObject *do_qmp_dispatch(bool allow_oob, const char *template, ...) |
Markus Armbruster | 3306459 | 2020-03-17 12:54:30 +0100 | [diff] [blame] | 144 | { |
Markus Armbruster | 3d16042 | 2020-03-17 12:54:32 +0100 | [diff] [blame] | 145 | va_list ap; |
| 146 | QDict *req, *resp; |
Markus Armbruster | 3306459 | 2020-03-17 12:54:30 +0100 | [diff] [blame] | 147 | QObject *ret; |
| 148 | |
Markus Armbruster | 3d16042 | 2020-03-17 12:54:32 +0100 | [diff] [blame] | 149 | va_start(ap, template); |
| 150 | req = qdict_from_vjsonf_nofail(template, ap); |
| 151 | va_end(ap); |
| 152 | |
Kevin Wolf | 41725fa | 2020-10-05 17:58:47 +0200 | [diff] [blame] | 153 | resp = qmp_dispatch(&qmp_commands, QOBJECT(req), allow_oob, NULL); |
Markus Armbruster | ef9f5f0 | 2020-03-17 12:54:31 +0100 | [diff] [blame] | 154 | g_assert(resp); |
Markus Armbruster | 3306459 | 2020-03-17 12:54:30 +0100 | [diff] [blame] | 155 | ret = qdict_get(resp, "return"); |
| 156 | g_assert(ret); |
Markus Armbruster | ef9f5f0 | 2020-03-17 12:54:31 +0100 | [diff] [blame] | 157 | g_assert(qdict_size(resp) == 1); |
Markus Armbruster | 3306459 | 2020-03-17 12:54:30 +0100 | [diff] [blame] | 158 | |
| 159 | qobject_ref(ret); |
| 160 | qobject_unref(resp); |
Markus Armbruster | 3d16042 | 2020-03-17 12:54:32 +0100 | [diff] [blame] | 161 | qobject_unref(req); |
Markus Armbruster | 3306459 | 2020-03-17 12:54:30 +0100 | [diff] [blame] | 162 | return ret; |
| 163 | } |
| 164 | |
Markus Armbruster | 3d16042 | 2020-03-17 12:54:32 +0100 | [diff] [blame] | 165 | static void do_qmp_dispatch_error(bool allow_oob, ErrorClass cls, |
| 166 | const char *template, ...) |
Markus Armbruster | 3306459 | 2020-03-17 12:54:30 +0100 | [diff] [blame] | 167 | { |
Markus Armbruster | 3d16042 | 2020-03-17 12:54:32 +0100 | [diff] [blame] | 168 | va_list ap; |
| 169 | QDict *req, *resp; |
Markus Armbruster | ef9f5f0 | 2020-03-17 12:54:31 +0100 | [diff] [blame] | 170 | QDict *error; |
Markus Armbruster | 3306459 | 2020-03-17 12:54:30 +0100 | [diff] [blame] | 171 | |
Markus Armbruster | 3d16042 | 2020-03-17 12:54:32 +0100 | [diff] [blame] | 172 | va_start(ap, template); |
| 173 | req = qdict_from_vjsonf_nofail(template, ap); |
| 174 | va_end(ap); |
| 175 | |
Kevin Wolf | 41725fa | 2020-10-05 17:58:47 +0200 | [diff] [blame] | 176 | resp = qmp_dispatch(&qmp_commands, QOBJECT(req), allow_oob, NULL); |
Markus Armbruster | ef9f5f0 | 2020-03-17 12:54:31 +0100 | [diff] [blame] | 177 | g_assert(resp); |
| 178 | error = qdict_get_qdict(resp, "error"); |
| 179 | g_assert(error); |
| 180 | g_assert_cmpstr(qdict_get_try_str(error, "class"), |
| 181 | ==, QapiErrorClass_str(cls)); |
| 182 | g_assert(qdict_get_try_str(error, "desc")); |
| 183 | g_assert(qdict_size(error) == 2); |
| 184 | g_assert(qdict_size(resp) == 1); |
Markus Armbruster | 3306459 | 2020-03-17 12:54:30 +0100 | [diff] [blame] | 185 | |
| 186 | qobject_unref(resp); |
Markus Armbruster | 3d16042 | 2020-03-17 12:54:32 +0100 | [diff] [blame] | 187 | qobject_unref(req); |
Markus Armbruster | 3306459 | 2020-03-17 12:54:30 +0100 | [diff] [blame] | 188 | } |
| 189 | |
Michael Roth | 69ed836 | 2011-07-19 14:50:45 -0500 | [diff] [blame] | 190 | /* test commands with no input and no return value */ |
| 191 | static void test_dispatch_cmd(void) |
| 192 | { |
Markus Armbruster | ef9f5f0 | 2020-03-17 12:54:31 +0100 | [diff] [blame] | 193 | QDict *ret; |
Michael Roth | 69ed836 | 2011-07-19 14:50:45 -0500 | [diff] [blame] | 194 | |
Markus Armbruster | 3d16042 | 2020-03-17 12:54:32 +0100 | [diff] [blame] | 195 | ret = qobject_to(QDict, |
| 196 | do_qmp_dispatch(false, |
Markus Armbruster | 05ebf84 | 2021-03-23 10:40:21 +0100 | [diff] [blame] | 197 | "{ 'execute': 'user-def-cmd' }")); |
Markus Armbruster | ef9f5f0 | 2020-03-17 12:54:31 +0100 | [diff] [blame] | 198 | assert(ret && qdict_size(ret) == 0); |
Markus Armbruster | 3306459 | 2020-03-17 12:54:30 +0100 | [diff] [blame] | 199 | qobject_unref(ret); |
Michael Roth | 69ed836 | 2011-07-19 14:50:45 -0500 | [diff] [blame] | 200 | } |
| 201 | |
Peter Xu | c55f070 | 2018-10-09 14:27:17 +0800 | [diff] [blame] | 202 | static void test_dispatch_cmd_oob(void) |
| 203 | { |
Markus Armbruster | ef9f5f0 | 2020-03-17 12:54:31 +0100 | [diff] [blame] | 204 | QDict *ret; |
Peter Xu | c55f070 | 2018-10-09 14:27:17 +0800 | [diff] [blame] | 205 | |
Markus Armbruster | 3d16042 | 2020-03-17 12:54:32 +0100 | [diff] [blame] | 206 | ret = qobject_to(QDict, |
| 207 | do_qmp_dispatch(true, |
| 208 | "{ 'exec-oob': 'test-flags-command' }")); |
Markus Armbruster | ef9f5f0 | 2020-03-17 12:54:31 +0100 | [diff] [blame] | 209 | assert(ret && qdict_size(ret) == 0); |
Markus Armbruster | 3306459 | 2020-03-17 12:54:30 +0100 | [diff] [blame] | 210 | qobject_unref(ret); |
Peter Xu | c55f070 | 2018-10-09 14:27:17 +0800 | [diff] [blame] | 211 | } |
| 212 | |
Michael Roth | 69ed836 | 2011-07-19 14:50:45 -0500 | [diff] [blame] | 213 | /* test commands that return an error due to invalid parameters */ |
Peter Maydell | 5100917 | 2016-08-04 11:39:38 +0100 | [diff] [blame] | 214 | static void test_dispatch_cmd_failure(void) |
Michael Roth | 69ed836 | 2011-07-19 14:50:45 -0500 | [diff] [blame] | 215 | { |
Markus Armbruster | 3d16042 | 2020-03-17 12:54:32 +0100 | [diff] [blame] | 216 | /* missing arguments */ |
| 217 | do_qmp_dispatch_error(false, ERROR_CLASS_GENERIC_ERROR, |
Markus Armbruster | 05ebf84 | 2021-03-23 10:40:21 +0100 | [diff] [blame] | 218 | "{ 'execute': 'user-def-cmd2' }"); |
Michael Roth | 69ed836 | 2011-07-19 14:50:45 -0500 | [diff] [blame] | 219 | |
Markus Armbruster | 3d16042 | 2020-03-17 12:54:32 +0100 | [diff] [blame] | 220 | /* extra arguments */ |
| 221 | do_qmp_dispatch_error(false, ERROR_CLASS_GENERIC_ERROR, |
Markus Armbruster | 05ebf84 | 2021-03-23 10:40:21 +0100 | [diff] [blame] | 222 | "{ 'execute': 'user-def-cmd'," |
Markus Armbruster | 3d16042 | 2020-03-17 12:54:32 +0100 | [diff] [blame] | 223 | " 'arguments': { 'a': 66 } }"); |
Michael Roth | 69ed836 | 2011-07-19 14:50:45 -0500 | [diff] [blame] | 224 | } |
| 225 | |
Marc-André Lureau | ae6bf76 | 2018-08-30 17:58:10 +0200 | [diff] [blame] | 226 | static void test_dispatch_cmd_success_response(void) |
| 227 | { |
| 228 | QDict *req = qdict_new(); |
| 229 | QDict *resp; |
| 230 | |
| 231 | qdict_put_str(req, "execute", "cmd-success-response"); |
Kevin Wolf | 41725fa | 2020-10-05 17:58:47 +0200 | [diff] [blame] | 232 | resp = qmp_dispatch(&qmp_commands, QOBJECT(req), false, NULL); |
Marc-André Lureau | ae6bf76 | 2018-08-30 17:58:10 +0200 | [diff] [blame] | 233 | g_assert_null(resp); |
| 234 | qobject_unref(req); |
| 235 | } |
| 236 | |
Michael Roth | 69ed836 | 2011-07-19 14:50:45 -0500 | [diff] [blame] | 237 | /* test commands that involve both input parameters and return values */ |
| 238 | static void test_dispatch_cmd_io(void) |
| 239 | { |
Markus Armbruster | 357765f | 2014-03-01 08:40:27 +0100 | [diff] [blame] | 240 | QDict *ret, *ret_dict, *ret_dict_dict, *ret_dict_dict_userdef; |
| 241 | QDict *ret_dict_dict2, *ret_dict_dict2_userdef; |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 242 | QNum *ret3; |
| 243 | int64_t val; |
Michael Roth | 69ed836 | 2011-07-19 14:50:45 -0500 | [diff] [blame] | 244 | |
Markus Armbruster | 3d16042 | 2020-03-17 12:54:32 +0100 | [diff] [blame] | 245 | ret = qobject_to(QDict, do_qmp_dispatch(false, |
Markus Armbruster | 05ebf84 | 2021-03-23 10:40:21 +0100 | [diff] [blame] | 246 | "{ 'execute': 'user-def-cmd2', 'arguments': {" |
Markus Armbruster | 3d16042 | 2020-03-17 12:54:32 +0100 | [diff] [blame] | 247 | " 'ud1a': { 'integer': 42, 'string': 'hello' }," |
| 248 | " 'ud1b': { 'integer': 422, 'string': 'hello2' } } }")); |
Michael Roth | 69ed836 | 2011-07-19 14:50:45 -0500 | [diff] [blame] | 249 | |
Eric Blake | b6fcf32 | 2015-05-04 09:05:29 -0600 | [diff] [blame] | 250 | assert(!strcmp(qdict_get_str(ret, "string0"), "blah1")); |
| 251 | ret_dict = qdict_get_qdict(ret, "dict1"); |
| 252 | assert(!strcmp(qdict_get_str(ret_dict, "string1"), "blah2")); |
| 253 | ret_dict_dict = qdict_get_qdict(ret_dict, "dict2"); |
Markus Armbruster | 357765f | 2014-03-01 08:40:27 +0100 | [diff] [blame] | 254 | ret_dict_dict_userdef = qdict_get_qdict(ret_dict_dict, "userdef"); |
| 255 | assert(qdict_get_int(ret_dict_dict_userdef, "integer") == 42); |
| 256 | assert(!strcmp(qdict_get_str(ret_dict_dict_userdef, "string"), "hello")); |
| 257 | assert(!strcmp(qdict_get_str(ret_dict_dict, "string"), "blah3")); |
Eric Blake | b6fcf32 | 2015-05-04 09:05:29 -0600 | [diff] [blame] | 258 | ret_dict_dict2 = qdict_get_qdict(ret_dict, "dict3"); |
Markus Armbruster | 357765f | 2014-03-01 08:40:27 +0100 | [diff] [blame] | 259 | ret_dict_dict2_userdef = qdict_get_qdict(ret_dict_dict2, "userdef"); |
| 260 | assert(qdict_get_int(ret_dict_dict2_userdef, "integer") == 422); |
| 261 | assert(!strcmp(qdict_get_str(ret_dict_dict2_userdef, "string"), "hello2")); |
| 262 | assert(!strcmp(qdict_get_str(ret_dict_dict2, "string"), "blah4")); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 263 | qobject_unref(ret); |
Markus Armbruster | c2216a8 | 2014-03-01 08:40:29 +0100 | [diff] [blame] | 264 | |
Markus Armbruster | 3d16042 | 2020-03-17 12:54:32 +0100 | [diff] [blame] | 265 | ret3 = qobject_to(QNum, do_qmp_dispatch(false, |
| 266 | "{ 'execute': 'guest-get-time', 'arguments': { 'a': 66 } }")); |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 267 | g_assert(qnum_get_try_int(ret3, &val)); |
| 268 | g_assert_cmpint(val, ==, 66); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 269 | qobject_unref(ret3); |
Michael Roth | 69ed836 | 2011-07-19 14:50:45 -0500 | [diff] [blame] | 270 | } |
| 271 | |
Markus Armbruster | d203259 | 2021-03-18 16:55:17 +0100 | [diff] [blame] | 272 | static void test_dispatch_cmd_deprecated(void) |
| 273 | { |
| 274 | const char *cmd = "{ 'execute': 'test-command-features1' }"; |
| 275 | QDict *ret; |
| 276 | |
| 277 | memset(&compat_policy, 0, sizeof(compat_policy)); |
| 278 | |
| 279 | /* accept */ |
| 280 | ret = qobject_to(QDict, do_qmp_dispatch(false, cmd)); |
| 281 | assert(ret && qdict_size(ret) == 0); |
| 282 | qobject_unref(ret); |
| 283 | |
| 284 | compat_policy.has_deprecated_input = true; |
| 285 | compat_policy.deprecated_input = COMPAT_POLICY_INPUT_ACCEPT; |
| 286 | ret = qobject_to(QDict, do_qmp_dispatch(false, cmd)); |
| 287 | assert(ret && qdict_size(ret) == 0); |
| 288 | qobject_unref(ret); |
| 289 | |
| 290 | compat_policy.deprecated_input = COMPAT_POLICY_INPUT_REJECT; |
| 291 | do_qmp_dispatch_error(false, ERROR_CLASS_COMMAND_NOT_FOUND, cmd); |
| 292 | } |
| 293 | |
Markus Armbruster | db29164 | 2021-03-18 16:55:18 +0100 | [diff] [blame] | 294 | static void test_dispatch_cmd_arg_deprecated(void) |
| 295 | { |
| 296 | const char *cmd = "{ 'execute': 'test-features0'," |
| 297 | " 'arguments': { 'fs1': { 'foo': 42 } } }"; |
| 298 | QDict *ret; |
| 299 | |
| 300 | memset(&compat_policy, 0, sizeof(compat_policy)); |
| 301 | |
| 302 | /* accept */ |
| 303 | ret = qobject_to(QDict, do_qmp_dispatch(false, cmd)); |
| 304 | assert(ret && qdict_size(ret) == 1); |
| 305 | qobject_unref(ret); |
| 306 | |
| 307 | compat_policy.has_deprecated_input = true; |
| 308 | compat_policy.deprecated_input = COMPAT_POLICY_INPUT_ACCEPT; |
| 309 | ret = qobject_to(QDict, do_qmp_dispatch(false, cmd)); |
| 310 | assert(ret && qdict_size(ret) == 1); |
| 311 | qobject_unref(ret); |
| 312 | |
| 313 | compat_policy.deprecated_input = COMPAT_POLICY_INPUT_REJECT; |
| 314 | do_qmp_dispatch_error(false, ERROR_CLASS_GENERIC_ERROR, cmd); |
| 315 | } |
| 316 | |
Markus Armbruster | 91fa93e | 2021-03-18 16:55:11 +0100 | [diff] [blame] | 317 | static void test_dispatch_cmd_ret_deprecated(void) |
| 318 | { |
| 319 | const char *cmd = "{ 'execute': 'test-features0' }"; |
| 320 | QDict *ret; |
| 321 | |
| 322 | memset(&compat_policy, 0, sizeof(compat_policy)); |
| 323 | |
| 324 | /* default accept */ |
| 325 | ret = qobject_to(QDict, do_qmp_dispatch(false, cmd)); |
| 326 | assert(ret && qdict_size(ret) == 1); |
| 327 | qobject_unref(ret); |
| 328 | |
| 329 | compat_policy.has_deprecated_output = true; |
| 330 | compat_policy.deprecated_output = COMPAT_POLICY_OUTPUT_ACCEPT; |
| 331 | ret = qobject_to(QDict, do_qmp_dispatch(false, cmd)); |
| 332 | assert(ret && qdict_size(ret) == 1); |
| 333 | qobject_unref(ret); |
| 334 | |
| 335 | compat_policy.deprecated_output = COMPAT_POLICY_OUTPUT_HIDE; |
| 336 | ret = qobject_to(QDict, do_qmp_dispatch(false, cmd)); |
| 337 | assert(ret && qdict_size(ret) == 0); |
| 338 | qobject_unref(ret); |
| 339 | } |
| 340 | |
Michael Roth | 5cd5f0d | 2011-09-15 14:39:54 -0500 | [diff] [blame] | 341 | /* test generated dealloc functions for generated types */ |
| 342 | static void test_dealloc_types(void) |
| 343 | { |
| 344 | UserDefOne *ud1test, *ud1a, *ud1b; |
| 345 | UserDefOneList *ud1list; |
| 346 | |
| 347 | ud1test = g_malloc0(sizeof(UserDefOne)); |
Eric Blake | ddf2190 | 2015-10-26 16:34:49 -0600 | [diff] [blame] | 348 | ud1test->integer = 42; |
Michael Roth | 5cd5f0d | 2011-09-15 14:39:54 -0500 | [diff] [blame] | 349 | ud1test->string = g_strdup("hi there 42"); |
| 350 | |
| 351 | qapi_free_UserDefOne(ud1test); |
| 352 | |
| 353 | ud1a = g_malloc0(sizeof(UserDefOne)); |
Eric Blake | ddf2190 | 2015-10-26 16:34:49 -0600 | [diff] [blame] | 354 | ud1a->integer = 43; |
Michael Roth | 5cd5f0d | 2011-09-15 14:39:54 -0500 | [diff] [blame] | 355 | ud1a->string = g_strdup("hi there 43"); |
| 356 | |
| 357 | ud1b = g_malloc0(sizeof(UserDefOne)); |
Eric Blake | ddf2190 | 2015-10-26 16:34:49 -0600 | [diff] [blame] | 358 | ud1b->integer = 44; |
Michael Roth | 5cd5f0d | 2011-09-15 14:39:54 -0500 | [diff] [blame] | 359 | ud1b->string = g_strdup("hi there 44"); |
| 360 | |
| 361 | ud1list = g_malloc0(sizeof(UserDefOneList)); |
| 362 | ud1list->value = ud1a; |
| 363 | ud1list->next = g_malloc0(sizeof(UserDefOneList)); |
| 364 | ud1list->next->value = ud1b; |
| 365 | |
| 366 | qapi_free_UserDefOneList(ud1list); |
| 367 | } |
| 368 | |
Laszlo Ersek | d98150f | 2012-07-17 16:17:05 +0200 | [diff] [blame] | 369 | /* test generated deallocation on an object whose construction was prematurely |
| 370 | * terminated due to an error */ |
| 371 | static void test_dealloc_partial(void) |
| 372 | { |
| 373 | static const char text[] = "don't leak me"; |
| 374 | |
| 375 | UserDefTwo *ud2 = NULL; |
| 376 | Error *err = NULL; |
| 377 | |
| 378 | /* create partial object */ |
| 379 | { |
| 380 | QDict *ud2_dict; |
Eric Blake | b70ce10 | 2016-06-09 10:48:38 -0600 | [diff] [blame] | 381 | Visitor *v; |
Laszlo Ersek | d98150f | 2012-07-17 16:17:05 +0200 | [diff] [blame] | 382 | |
| 383 | ud2_dict = qdict_new(); |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 384 | qdict_put_str(ud2_dict, "string0", text); |
Laszlo Ersek | d98150f | 2012-07-17 16:17:05 +0200 | [diff] [blame] | 385 | |
Markus Armbruster | 048abb7 | 2017-03-03 13:32:39 +0100 | [diff] [blame] | 386 | v = qobject_input_visitor_new(QOBJECT(ud2_dict)); |
Eric Blake | b70ce10 | 2016-06-09 10:48:38 -0600 | [diff] [blame] | 387 | visit_type_UserDefTwo(v, NULL, &ud2, &err); |
| 388 | visit_free(v); |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 389 | qobject_unref(ud2_dict); |
Laszlo Ersek | d98150f | 2012-07-17 16:17:05 +0200 | [diff] [blame] | 390 | } |
| 391 | |
Eric Blake | 68ab47e | 2016-04-28 15:45:32 -0600 | [diff] [blame] | 392 | /* verify that visit_type_XXX() cleans up properly on error */ |
Eric Blake | a12a5a1 | 2015-11-05 23:35:31 -0700 | [diff] [blame] | 393 | error_free_or_abort(&err); |
Eric Blake | 68ab47e | 2016-04-28 15:45:32 -0600 | [diff] [blame] | 394 | assert(!ud2); |
| 395 | |
| 396 | /* Manually create a partial object, leaving ud2->dict1 at NULL */ |
| 397 | ud2 = g_new0(UserDefTwo, 1); |
| 398 | ud2->string0 = g_strdup(text); |
Laszlo Ersek | d98150f | 2012-07-17 16:17:05 +0200 | [diff] [blame] | 399 | |
| 400 | /* tear down partial object */ |
| 401 | qapi_free_UserDefTwo(ud2); |
| 402 | } |
| 403 | |
| 404 | |
Michael Roth | 69ed836 | 2011-07-19 14:50:45 -0500 | [diff] [blame] | 405 | int main(int argc, char **argv) |
| 406 | { |
| 407 | g_test_init(&argc, &argv, NULL); |
| 408 | |
Marc-André Lureau | 214e4a5 | 2018-03-26 17:08:49 +0200 | [diff] [blame] | 409 | g_test_add_func("/qmp/dispatch_cmd", test_dispatch_cmd); |
Peter Xu | c55f070 | 2018-10-09 14:27:17 +0800 | [diff] [blame] | 410 | g_test_add_func("/qmp/dispatch_cmd_oob", test_dispatch_cmd_oob); |
Marc-André Lureau | 214e4a5 | 2018-03-26 17:08:49 +0200 | [diff] [blame] | 411 | g_test_add_func("/qmp/dispatch_cmd_failure", test_dispatch_cmd_failure); |
| 412 | g_test_add_func("/qmp/dispatch_cmd_io", test_dispatch_cmd_io); |
Marc-André Lureau | ae6bf76 | 2018-08-30 17:58:10 +0200 | [diff] [blame] | 413 | g_test_add_func("/qmp/dispatch_cmd_success_response", |
| 414 | test_dispatch_cmd_success_response); |
Markus Armbruster | d203259 | 2021-03-18 16:55:17 +0100 | [diff] [blame] | 415 | g_test_add_func("/qmp/dispatch_cmd_deprecated", |
| 416 | test_dispatch_cmd_deprecated); |
Markus Armbruster | db29164 | 2021-03-18 16:55:18 +0100 | [diff] [blame] | 417 | g_test_add_func("/qmp/dispatch_cmd_arg_deprecated", |
| 418 | test_dispatch_cmd_arg_deprecated); |
Markus Armbruster | 91fa93e | 2021-03-18 16:55:11 +0100 | [diff] [blame] | 419 | g_test_add_func("/qmp/dispatch_cmd_ret_deprecated", |
| 420 | test_dispatch_cmd_ret_deprecated); |
Marc-André Lureau | 214e4a5 | 2018-03-26 17:08:49 +0200 | [diff] [blame] | 421 | g_test_add_func("/qmp/dealloc_types", test_dealloc_types); |
| 422 | g_test_add_func("/qmp/dealloc_partial", test_dealloc_partial); |
Michael Roth | 69ed836 | 2011-07-19 14:50:45 -0500 | [diff] [blame] | 423 | |
Markus Armbruster | 1527bad | 2017-03-03 13:32:25 +0100 | [diff] [blame] | 424 | test_qmp_init_marshal(&qmp_commands); |
Michael Roth | 69ed836 | 2011-07-19 14:50:45 -0500 | [diff] [blame] | 425 | g_test_run(); |
| 426 | |
| 427 | return 0; |
| 428 | } |