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 14 #include "qemu/osdep.h" 15 16 #include "qemu-common.h" 17 #include "qapi/error.h" 18 #include "qapi/qmp/qbool.h" 19 #include "qapi/qmp/qdict.h" 20 #include "qapi/qmp/qjson.h" 21 #include "qapi/qmp/qnum.h" 22 #include "qapi/qmp/qstring.h" 23 #include "qapi/qmp-event.h" 24 #include "test-qapi-events.h" 25 #include "test-qapi-emit-events.h" 26 27 typedef struct TestEventData { 28 QDict *expect; 29 } TestEventData; 30 31 typedef struct QDictCmpData { 32 QDict *expect; 33 bool result; 34 } QDictCmpData; 35 36 TestEventData *test_event_data; 37 static GMutex test_event_lock; 38 39 /* Only compares bool, int, string */ 40 static 41 void qdict_cmp_do_simple(const char *key, QObject *obj1, void *opaque) 42 43 { 44 QObject *obj2; 45 QDictCmpData d_new, *d = opaque; 46 int64_t val1, val2; 47 48 if (!d->result) { 49 return; 50 } 51 52 obj2 = qdict_get(d->expect, key); 53 if (!obj2) { 54 d->result = false; 55 return; 56 } 57 58 if (qobject_type(obj1) != qobject_type(obj2)) { 59 d->result = false; 60 return; 61 } 62 63 switch (qobject_type(obj1)) { 64 case QTYPE_QBOOL: 65 d->result = (qbool_get_bool(qobject_to(QBool, obj1)) == 66 qbool_get_bool(qobject_to(QBool, obj2))); 67 return; 68 case QTYPE_QNUM: 69 g_assert(qnum_get_try_int(qobject_to(QNum, obj1), &val1)); 70 g_assert(qnum_get_try_int(qobject_to(QNum, obj2), &val2)); 71 d->result = val1 == val2; 72 return; 73 case QTYPE_QSTRING: 74 d->result = g_strcmp0(qstring_get_str(qobject_to(QString, obj1)), 75 qstring_get_str(qobject_to(QString, obj2))) == 0; 76 return; 77 case QTYPE_QDICT: 78 d_new.expect = qobject_to(QDict, obj2); 79 d_new.result = true; 80 qdict_iter(qobject_to(QDict, obj1), qdict_cmp_do_simple, &d_new); 81 d->result = d_new.result; 82 return; 83 default: 84 abort(); 85 } 86 } 87 88 static bool qdict_cmp_simple(QDict *a, QDict *b) 89 { 90 QDictCmpData d; 91 92 d.expect = b; 93 d.result = true; 94 qdict_iter(a, qdict_cmp_do_simple, &d); 95 return d.result; 96 } 97 98 void test_qapi_event_emit(test_QAPIEvent event, QDict *d) 99 { 100 QDict *t; 101 int64_t s, ms; 102 103 /* Verify that we have timestamp, then remove it to compare other fields */ 104 t = qdict_get_qdict(d, "timestamp"); 105 g_assert(t); 106 s = qdict_get_try_int(t, "seconds", -2); 107 ms = qdict_get_try_int(t, "microseconds", -2); 108 if (s == -1) { 109 g_assert(ms == -1); 110 } else { 111 g_assert(s >= 0); 112 g_assert(ms >= 0 && ms <= 999999); 113 } 114 g_assert(qdict_size(t) == 2); 115 116 qdict_del(d, "timestamp"); 117 118 g_assert(qdict_cmp_simple(d, test_event_data->expect)); 119 120 } 121 122 static void event_prepare(TestEventData *data, 123 const void *unused) 124 { 125 /* Global variable test_event_data was used to pass the expectation, so 126 test cases can't be executed at same time. */ 127 g_mutex_lock(&test_event_lock); 128 test_event_data = data; 129 } 130 131 static void event_teardown(TestEventData *data, 132 const void *unused) 133 { 134 test_event_data = NULL; 135 g_mutex_unlock(&test_event_lock); 136 } 137 138 static void event_test_add(const char *testpath, 139 void (*test_func)(TestEventData *data, 140 const void *user_data)) 141 { 142 g_test_add(testpath, TestEventData, NULL, event_prepare, test_func, 143 event_teardown); 144 } 145 146 147 /* Test cases */ 148 149 static void test_event_a(TestEventData *data, 150 const void *unused) 151 { 152 data->expect = qdict_from_jsonf_nofail("{ 'event': 'EVENT_A' }"); 153 qapi_event_send_event_a(); 154 qobject_unref(data->expect); 155 } 156 157 static void test_event_b(TestEventData *data, 158 const void *unused) 159 { 160 data->expect = qdict_from_jsonf_nofail("{ 'event': 'EVENT_B' }"); 161 qapi_event_send_event_b(); 162 qobject_unref(data->expect); 163 } 164 165 static void test_event_c(TestEventData *data, 166 const void *unused) 167 { 168 UserDefOne b = { .integer = 2, .string = (char *)"test1" }; 169 170 data->expect = qdict_from_jsonf_nofail( 171 "{ 'event': 'EVENT_C', 'data': {" 172 " 'a': 1, 'b': { 'integer': 2, 'string': 'test1' }, 'c': 'test2' } }"); 173 qapi_event_send_event_c(true, 1, true, &b, "test2"); 174 qobject_unref(data->expect); 175 } 176 177 /* Complex type */ 178 static void test_event_d(TestEventData *data, 179 const void *unused) 180 { 181 UserDefOne struct1 = { 182 .integer = 2, .string = (char *)"test1", 183 .has_enum1 = true, .enum1 = ENUM_ONE_VALUE1, 184 }; 185 EventStructOne a = { 186 .struct1 = &struct1, 187 .string = (char *)"test2", 188 .has_enum2 = true, 189 .enum2 = ENUM_ONE_VALUE2, 190 }; 191 192 data->expect = qdict_from_jsonf_nofail( 193 "{ 'event': 'EVENT_D', 'data': {" 194 " 'a': {" 195 " 'struct1': { 'integer': 2, 'string': 'test1', 'enum1': 'value1' }," 196 " 'string': 'test2', 'enum2': 'value2' }," 197 " 'b': 'test3', 'enum3': 'value3' } }"); 198 qapi_event_send_event_d(&a, "test3", false, NULL, true, ENUM_ONE_VALUE3); 199 qobject_unref(data->expect); 200 } 201 202 int main(int argc, char **argv) 203 { 204 g_test_init(&argc, &argv, NULL); 205 206 event_test_add("/event/event_a", test_event_a); 207 event_test_add("/event/event_b", test_event_b); 208 event_test_add("/event/event_c", test_event_c); 209 event_test_add("/event/event_d", test_event_d); 210 g_test_run(); 211 212 return 0; 213 } 214