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 TestEventData *test_event_data; 32 static GMutex test_event_lock; 33 34 void test_qapi_event_emit(test_QAPIEvent event, QDict *d) 35 { 36 QDict *t; 37 int64_t s, ms; 38 39 /* Verify that we have timestamp, then remove it to compare other fields */ 40 t = qdict_get_qdict(d, "timestamp"); 41 g_assert(t); 42 s = qdict_get_try_int(t, "seconds", -2); 43 ms = qdict_get_try_int(t, "microseconds", -2); 44 if (s == -1) { 45 g_assert(ms == -1); 46 } else { 47 g_assert(s >= 0); 48 g_assert(ms >= 0 && ms <= 999999); 49 } 50 g_assert(qdict_size(t) == 2); 51 52 qdict_del(d, "timestamp"); 53 54 g_assert(qobject_is_equal(QOBJECT(d), QOBJECT(test_event_data->expect))); 55 56 } 57 58 static void event_prepare(TestEventData *data, 59 const void *unused) 60 { 61 /* Global variable test_event_data was used to pass the expectation, so 62 test cases can't be executed at same time. */ 63 g_mutex_lock(&test_event_lock); 64 test_event_data = data; 65 } 66 67 static void event_teardown(TestEventData *data, 68 const void *unused) 69 { 70 test_event_data = NULL; 71 g_mutex_unlock(&test_event_lock); 72 } 73 74 static void event_test_add(const char *testpath, 75 void (*test_func)(TestEventData *data, 76 const void *user_data)) 77 { 78 g_test_add(testpath, TestEventData, NULL, event_prepare, test_func, 79 event_teardown); 80 } 81 82 83 /* Test cases */ 84 85 static void test_event_a(TestEventData *data, 86 const void *unused) 87 { 88 data->expect = qdict_from_jsonf_nofail("{ 'event': 'EVENT_A' }"); 89 qapi_event_send_event_a(); 90 qobject_unref(data->expect); 91 } 92 93 static void test_event_b(TestEventData *data, 94 const void *unused) 95 { 96 data->expect = qdict_from_jsonf_nofail("{ 'event': 'EVENT_B' }"); 97 qapi_event_send_event_b(); 98 qobject_unref(data->expect); 99 } 100 101 static void test_event_c(TestEventData *data, 102 const void *unused) 103 { 104 UserDefOne b = { .integer = 2, .string = (char *)"test1" }; 105 106 data->expect = qdict_from_jsonf_nofail( 107 "{ 'event': 'EVENT_C', 'data': {" 108 " 'a': 1, 'b': { 'integer': 2, 'string': 'test1' }, 'c': 'test2' } }"); 109 qapi_event_send_event_c(true, 1, true, &b, "test2"); 110 qobject_unref(data->expect); 111 } 112 113 /* Complex type */ 114 static void test_event_d(TestEventData *data, 115 const void *unused) 116 { 117 UserDefOne struct1 = { 118 .integer = 2, .string = (char *)"test1", 119 .has_enum1 = true, .enum1 = ENUM_ONE_VALUE1, 120 }; 121 EventStructOne a = { 122 .struct1 = &struct1, 123 .string = (char *)"test2", 124 .has_enum2 = true, 125 .enum2 = ENUM_ONE_VALUE2, 126 }; 127 128 data->expect = qdict_from_jsonf_nofail( 129 "{ 'event': 'EVENT_D', 'data': {" 130 " 'a': {" 131 " 'struct1': { 'integer': 2, 'string': 'test1', 'enum1': 'value1' }," 132 " 'string': 'test2', 'enum2': 'value2' }," 133 " 'b': 'test3', 'enum3': 'value3' } }"); 134 qapi_event_send_event_d(&a, "test3", false, NULL, true, ENUM_ONE_VALUE3); 135 qobject_unref(data->expect); 136 } 137 138 int main(int argc, char **argv) 139 { 140 g_test_init(&argc, &argv, NULL); 141 142 event_test_add("/event/event_a", test_event_a); 143 event_test_add("/event/event_b", test_event_b); 144 event_test_add("/event/event_c", test_event_c); 145 event_test_add("/event/event_d", test_event_d); 146 g_test_run(); 147 148 return 0; 149 } 150