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