Wenchao Xia | f6dadb0 | 2014-06-18 08:43:29 +0200 | [diff] [blame] | 1 | /* |
| 2 | * qapi event unit-tests. |
| 3 | * |
| 4 | * Copyright (c) 2014 Wenchao Xia |
| 5 | * |
| 6 | * Authors: |
| 7 | * Wenchao Xia <wenchaoqemu@gmail.com> |
| 8 | * |
| 9 | * This work is licensed under the terms of the GNU LGPL, version 2.1 or later. |
| 10 | * See the COPYING.LIB file in the top-level directory. |
| 11 | * |
| 12 | */ |
| 13 | |
Peter Maydell | 681c28a | 2016-02-08 18:08:51 +0000 | [diff] [blame] | 14 | #include "qemu/osdep.h" |
Wenchao Xia | f6dadb0 | 2014-06-18 08:43:29 +0200 | [diff] [blame] | 15 | |
| 16 | #include "qemu-common.h" |
| 17 | #include "test-qapi-types.h" |
| 18 | #include "test-qapi-visit.h" |
| 19 | #include "test-qapi-event.h" |
| 20 | #include "qapi/qmp/types.h" |
Wenchao Xia | f6dadb0 | 2014-06-18 08:43:29 +0200 | [diff] [blame] | 21 | #include "qapi/qmp/qobject.h" |
| 22 | #include "qapi/qmp-event.h" |
| 23 | |
| 24 | typedef struct TestEventData { |
| 25 | QDict *expect; |
| 26 | } TestEventData; |
| 27 | |
| 28 | typedef struct QDictCmpData { |
| 29 | QDict *expect; |
| 30 | bool result; |
| 31 | } QDictCmpData; |
| 32 | |
| 33 | TestEventData *test_event_data; |
| 34 | static CompatGMutex test_event_lock; |
| 35 | |
| 36 | /* Only compares bool, int, string */ |
| 37 | static |
| 38 | void qdict_cmp_do_simple(const char *key, QObject *obj1, void *opaque) |
| 39 | |
| 40 | { |
| 41 | QObject *obj2; |
| 42 | QDictCmpData d_new, *d = opaque; |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 43 | int64_t val1, val2; |
Wenchao Xia | f6dadb0 | 2014-06-18 08:43:29 +0200 | [diff] [blame] | 44 | |
| 45 | if (!d->result) { |
| 46 | return; |
| 47 | } |
| 48 | |
| 49 | obj2 = qdict_get(d->expect, key); |
| 50 | if (!obj2) { |
| 51 | d->result = false; |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | if (qobject_type(obj1) != qobject_type(obj2)) { |
| 56 | d->result = false; |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | switch (qobject_type(obj1)) { |
| 61 | case QTYPE_QBOOL: |
Eric Blake | fc48ffc | 2015-05-15 16:24:59 -0600 | [diff] [blame] | 62 | d->result = (qbool_get_bool(qobject_to_qbool(obj1)) == |
| 63 | qbool_get_bool(qobject_to_qbool(obj2))); |
Wenchao Xia | f6dadb0 | 2014-06-18 08:43:29 +0200 | [diff] [blame] | 64 | return; |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 65 | case QTYPE_QNUM: |
| 66 | g_assert(qnum_get_try_int(qobject_to_qnum(obj1), &val1)); |
| 67 | g_assert(qnum_get_try_int(qobject_to_qnum(obj2), &val2)); |
| 68 | d->result = val1 == val2; |
Wenchao Xia | f6dadb0 | 2014-06-18 08:43:29 +0200 | [diff] [blame] | 69 | return; |
| 70 | case QTYPE_QSTRING: |
| 71 | d->result = g_strcmp0(qstring_get_str(qobject_to_qstring(obj1)), |
| 72 | qstring_get_str(qobject_to_qstring(obj2))) == 0; |
| 73 | return; |
| 74 | case QTYPE_QDICT: |
| 75 | d_new.expect = qobject_to_qdict(obj2); |
| 76 | d_new.result = true; |
| 77 | qdict_iter(qobject_to_qdict(obj1), qdict_cmp_do_simple, &d_new); |
| 78 | d->result = d_new.result; |
| 79 | return; |
| 80 | default: |
| 81 | abort(); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | static bool qdict_cmp_simple(QDict *a, QDict *b) |
| 86 | { |
| 87 | QDictCmpData d; |
| 88 | |
| 89 | d.expect = b; |
| 90 | d.result = true; |
| 91 | qdict_iter(a, qdict_cmp_do_simple, &d); |
| 92 | return d.result; |
| 93 | } |
| 94 | |
| 95 | /* This function is hooked as final emit function, which can verify the |
| 96 | correctness. */ |
Markus Armbruster | 016a335 | 2015-07-01 12:59:40 +0200 | [diff] [blame] | 97 | static void event_test_emit(test_QAPIEvent event, QDict *d, Error **errp) |
Wenchao Xia | f6dadb0 | 2014-06-18 08:43:29 +0200 | [diff] [blame] | 98 | { |
Wenchao Xia | f6dadb0 | 2014-06-18 08:43:29 +0200 | [diff] [blame] | 99 | QDict *t; |
| 100 | int64_t s, ms; |
| 101 | |
| 102 | /* Verify that we have timestamp, then remove it to compare other fields */ |
Markus Armbruster | 4b32e11 | 2017-02-17 21:38:17 +0100 | [diff] [blame] | 103 | t = qdict_get_qdict(d, "timestamp"); |
Wenchao Xia | f6dadb0 | 2014-06-18 08:43:29 +0200 | [diff] [blame] | 104 | g_assert(t); |
Markus Armbruster | 4b32e11 | 2017-02-17 21:38:17 +0100 | [diff] [blame] | 105 | s = qdict_get_try_int(t, "seconds", -2); |
| 106 | ms = qdict_get_try_int(t, "microseconds", -2); |
Wenchao Xia | f6dadb0 | 2014-06-18 08:43:29 +0200 | [diff] [blame] | 107 | if (s == -1) { |
| 108 | g_assert(ms == -1); |
| 109 | } else { |
Markus Armbruster | 4b32e11 | 2017-02-17 21:38:17 +0100 | [diff] [blame] | 110 | g_assert(s >= 0); |
Wenchao Xia | f6dadb0 | 2014-06-18 08:43:29 +0200 | [diff] [blame] | 111 | g_assert(ms >= 0 && ms <= 999999); |
| 112 | } |
| 113 | g_assert(qdict_size(t) == 2); |
| 114 | |
| 115 | qdict_del(d, "timestamp"); |
| 116 | |
| 117 | g_assert(qdict_cmp_simple(d, test_event_data->expect)); |
| 118 | |
| 119 | } |
| 120 | |
| 121 | static void event_prepare(TestEventData *data, |
| 122 | const void *unused) |
| 123 | { |
| 124 | /* Global variable test_event_data was used to pass the expectation, so |
| 125 | test cases can't be executed at same time. */ |
| 126 | g_mutex_lock(&test_event_lock); |
| 127 | |
| 128 | data->expect = qdict_new(); |
| 129 | test_event_data = data; |
| 130 | } |
| 131 | |
| 132 | static void event_teardown(TestEventData *data, |
| 133 | const void *unused) |
| 134 | { |
| 135 | QDECREF(data->expect); |
| 136 | test_event_data = NULL; |
| 137 | |
| 138 | g_mutex_unlock(&test_event_lock); |
| 139 | } |
| 140 | |
| 141 | static void event_test_add(const char *testpath, |
| 142 | void (*test_func)(TestEventData *data, |
| 143 | const void *user_data)) |
| 144 | { |
| 145 | g_test_add(testpath, TestEventData, NULL, event_prepare, test_func, |
| 146 | event_teardown); |
| 147 | } |
| 148 | |
| 149 | |
| 150 | /* Test cases */ |
| 151 | |
| 152 | static void test_event_a(TestEventData *data, |
| 153 | const void *unused) |
| 154 | { |
| 155 | QDict *d; |
| 156 | d = data->expect; |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 157 | qdict_put_str(d, "event", "EVENT_A"); |
Wenchao Xia | f6dadb0 | 2014-06-18 08:43:29 +0200 | [diff] [blame] | 158 | qapi_event_send_event_a(&error_abort); |
| 159 | } |
| 160 | |
| 161 | static void test_event_b(TestEventData *data, |
| 162 | const void *unused) |
| 163 | { |
| 164 | QDict *d; |
| 165 | d = data->expect; |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 166 | qdict_put_str(d, "event", "EVENT_B"); |
Wenchao Xia | f6dadb0 | 2014-06-18 08:43:29 +0200 | [diff] [blame] | 167 | qapi_event_send_event_b(&error_abort); |
| 168 | } |
| 169 | |
| 170 | static void test_event_c(TestEventData *data, |
| 171 | const void *unused) |
| 172 | { |
| 173 | QDict *d, *d_data, *d_b; |
| 174 | |
| 175 | UserDefOne b; |
Eric Blake | ddf2190 | 2015-10-26 16:34:49 -0600 | [diff] [blame] | 176 | b.integer = 2; |
Wenchao Xia | f6dadb0 | 2014-06-18 08:43:29 +0200 | [diff] [blame] | 177 | b.string = g_strdup("test1"); |
| 178 | b.has_enum1 = false; |
| 179 | |
| 180 | d_b = qdict_new(); |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 181 | qdict_put_int(d_b, "integer", 2); |
| 182 | qdict_put_str(d_b, "string", "test1"); |
Wenchao Xia | f6dadb0 | 2014-06-18 08:43:29 +0200 | [diff] [blame] | 183 | |
| 184 | d_data = qdict_new(); |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 185 | qdict_put_int(d_data, "a", 1); |
Wenchao Xia | f6dadb0 | 2014-06-18 08:43:29 +0200 | [diff] [blame] | 186 | qdict_put(d_data, "b", d_b); |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 187 | qdict_put_str(d_data, "c", "test2"); |
Wenchao Xia | f6dadb0 | 2014-06-18 08:43:29 +0200 | [diff] [blame] | 188 | |
| 189 | d = data->expect; |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 190 | qdict_put_str(d, "event", "EVENT_C"); |
Wenchao Xia | f6dadb0 | 2014-06-18 08:43:29 +0200 | [diff] [blame] | 191 | qdict_put(d, "data", d_data); |
| 192 | |
| 193 | qapi_event_send_event_c(true, 1, true, &b, "test2", &error_abort); |
| 194 | |
| 195 | g_free(b.string); |
| 196 | } |
| 197 | |
| 198 | /* Complex type */ |
| 199 | static void test_event_d(TestEventData *data, |
| 200 | const void *unused) |
| 201 | { |
| 202 | UserDefOne struct1; |
| 203 | EventStructOne a; |
Wenchao Xia | f6dadb0 | 2014-06-18 08:43:29 +0200 | [diff] [blame] | 204 | QDict *d, *d_data, *d_a, *d_struct1; |
| 205 | |
Eric Blake | ddf2190 | 2015-10-26 16:34:49 -0600 | [diff] [blame] | 206 | struct1.integer = 2; |
Wenchao Xia | f6dadb0 | 2014-06-18 08:43:29 +0200 | [diff] [blame] | 207 | struct1.string = g_strdup("test1"); |
| 208 | struct1.has_enum1 = true; |
| 209 | struct1.enum1 = ENUM_ONE_VALUE1; |
| 210 | |
| 211 | a.struct1 = &struct1; |
| 212 | a.string = g_strdup("test2"); |
| 213 | a.has_enum2 = true; |
| 214 | a.enum2 = ENUM_ONE_VALUE2; |
| 215 | |
| 216 | d_struct1 = qdict_new(); |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 217 | qdict_put_int(d_struct1, "integer", 2); |
| 218 | qdict_put_str(d_struct1, "string", "test1"); |
| 219 | qdict_put_str(d_struct1, "enum1", "value1"); |
Wenchao Xia | f6dadb0 | 2014-06-18 08:43:29 +0200 | [diff] [blame] | 220 | |
| 221 | d_a = qdict_new(); |
| 222 | qdict_put(d_a, "struct1", d_struct1); |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 223 | qdict_put_str(d_a, "string", "test2"); |
| 224 | qdict_put_str(d_a, "enum2", "value2"); |
Wenchao Xia | f6dadb0 | 2014-06-18 08:43:29 +0200 | [diff] [blame] | 225 | |
| 226 | d_data = qdict_new(); |
| 227 | qdict_put(d_data, "a", d_a); |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 228 | qdict_put_str(d_data, "b", "test3"); |
| 229 | qdict_put_str(d_data, "enum3", "value3"); |
Wenchao Xia | f6dadb0 | 2014-06-18 08:43:29 +0200 | [diff] [blame] | 230 | |
| 231 | d = data->expect; |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 232 | qdict_put_str(d, "event", "EVENT_D"); |
Wenchao Xia | f6dadb0 | 2014-06-18 08:43:29 +0200 | [diff] [blame] | 233 | qdict_put(d, "data", d_data); |
| 234 | |
| 235 | qapi_event_send_event_d(&a, "test3", false, NULL, true, ENUM_ONE_VALUE3, |
| 236 | &error_abort); |
| 237 | |
| 238 | g_free(struct1.string); |
| 239 | g_free(a.string); |
| 240 | } |
| 241 | |
| 242 | int main(int argc, char **argv) |
| 243 | { |
Paolo Bonzini | af35e5e | 2014-06-30 15:05:49 +0200 | [diff] [blame] | 244 | #if !GLIB_CHECK_VERSION(2, 31, 0) |
| 245 | if (!g_thread_supported()) { |
| 246 | g_thread_init(NULL); |
| 247 | } |
| 248 | #endif |
| 249 | |
Wenchao Xia | f6dadb0 | 2014-06-18 08:43:29 +0200 | [diff] [blame] | 250 | qmp_event_set_func_emit(event_test_emit); |
| 251 | |
| 252 | g_test_init(&argc, &argv, NULL); |
| 253 | |
| 254 | event_test_add("/event/event_a", test_event_a); |
| 255 | event_test_add("/event/event_b", test_event_b); |
| 256 | event_test_add("/event/event_c", test_event_c); |
| 257 | event_test_add("/event/event_d", test_event_d); |
| 258 | g_test_run(); |
| 259 | |
| 260 | return 0; |
| 261 | } |