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