1 /* 2 * Validate -readconfig 3 * 4 * Copyright (c) 2022 Red Hat, Inc. 5 * 6 * This work is licensed under the terms of the GNU GPL, version 2 or later. 7 * See the COPYING file in the top-level directory. 8 */ 9 10 #include "qemu/osdep.h" 11 #include "libqtest.h" 12 #include "qapi/error.h" 13 #include "qapi/qapi-visit-machine.h" 14 #include "qapi/qapi-visit-qom.h" 15 #include "qapi/qapi-visit-ui.h" 16 #include "qapi/qmp/qdict.h" 17 #include "qapi/qmp/qlist.h" 18 #include "qapi/qobject-input-visitor.h" 19 #include "qapi/qmp/qstring.h" 20 #include "qemu/units.h" 21 22 static QTestState *qtest_init_with_config(const char *cfgdata) 23 { 24 GError *error = NULL; 25 g_autofree char *args = NULL; 26 int cfgfd = -1; 27 g_autofree char *cfgpath = NULL; 28 QTestState *qts; 29 ssize_t ret; 30 31 cfgfd = g_file_open_tmp("readconfig-test-XXXXXX", &cfgpath, &error); 32 g_assert_no_error(error); 33 g_assert_cmpint(cfgfd, >=, 0); 34 35 ret = qemu_write_full(cfgfd, cfgdata, strlen(cfgdata)); 36 close(cfgfd); 37 if (ret < 0) { 38 unlink(cfgpath); 39 } 40 g_assert_cmpint(ret, ==, strlen(cfgdata)); 41 42 args = g_strdup_printf("-nodefaults -machine none -readconfig %s", cfgpath); 43 44 qts = qtest_init(args); 45 46 unlink(cfgpath); 47 48 return qts; 49 } 50 51 static void test_x86_memdev_resp(QObject *res) 52 { 53 Visitor *v; 54 g_autoptr(MemdevList) memdevs = NULL; 55 Memdev *memdev; 56 57 g_assert(res); 58 v = qobject_input_visitor_new(res); 59 visit_type_MemdevList(v, NULL, &memdevs, &error_abort); 60 61 g_assert(memdevs); 62 g_assert(memdevs->value); 63 g_assert(!memdevs->next); 64 65 memdev = memdevs->value; 66 g_assert_cmpstr(memdev->id, ==, "ram"); 67 g_assert_cmpint(memdev->size, ==, 200 * MiB); 68 69 visit_free(v); 70 } 71 72 static void test_x86_memdev(void) 73 { 74 QDict *resp; 75 QTestState *qts; 76 const char *cfgdata = 77 "[memory]\n" 78 "size = \"200\""; 79 80 qts = qtest_init_with_config(cfgdata); 81 /* Test valid command */ 82 resp = qtest_qmp(qts, "{ 'execute': 'query-memdev' }"); 83 test_x86_memdev_resp(qdict_get(resp, "return")); 84 qobject_unref(resp); 85 86 qtest_quit(qts); 87 } 88 89 /* FIXME: The test is currently broken on FreeBSD */ 90 #if defined(CONFIG_SPICE) && !defined(__FreeBSD__) 91 static void test_spice_resp(QObject *res) 92 { 93 Visitor *v; 94 g_autoptr(SpiceInfo) spice = NULL; 95 96 g_assert(res); 97 v = qobject_input_visitor_new(res); 98 visit_type_SpiceInfo(v, "spice", &spice, &error_abort); 99 100 g_assert(spice); 101 g_assert(spice->enabled); 102 103 visit_free(v); 104 } 105 106 static void test_spice(void) 107 { 108 QDict *resp; 109 QTestState *qts; 110 const char *cfgdata = 111 "[spice]\n" 112 #ifndef WIN32 113 "unix = \"on\"\n" 114 #endif 115 "disable-ticketing = \"on\"\n"; 116 117 qts = qtest_init_with_config(cfgdata); 118 /* Test valid command */ 119 resp = qtest_qmp(qts, "{ 'execute': 'query-spice' }"); 120 test_spice_resp(qdict_get(resp, "return")); 121 qobject_unref(resp); 122 123 qtest_quit(qts); 124 } 125 #endif 126 127 static void test_object_available(QObject *res, const char *name, 128 const char *type) 129 { 130 Visitor *v; 131 g_autoptr(ObjectPropertyInfoList) objs = NULL; 132 ObjectPropertyInfoList *tmp; 133 ObjectPropertyInfo *obj; 134 bool object_available = false; 135 g_autofree char *childtype = g_strdup_printf("child<%s>", type); 136 137 g_assert(res); 138 v = qobject_input_visitor_new(res); 139 visit_type_ObjectPropertyInfoList(v, NULL, &objs, &error_abort); 140 141 g_assert(objs); 142 tmp = objs; 143 while (tmp) { 144 g_assert(tmp->value); 145 146 obj = tmp->value; 147 if (g_str_equal(obj->name, name) && g_str_equal(obj->type, childtype)) { 148 object_available = true; 149 break; 150 } 151 152 tmp = tmp->next; 153 } 154 155 g_assert(object_available); 156 157 visit_free(v); 158 } 159 160 static void test_object_rng(void) 161 { 162 QDict *resp; 163 QTestState *qts; 164 const char *cfgdata = 165 "[object]\n" 166 "qom-type = \"rng-builtin\"\n" 167 "id = \"rng0\"\n"; 168 169 qts = qtest_init_with_config(cfgdata); 170 /* Test valid command */ 171 resp = qtest_qmp(qts, 172 "{ 'execute': 'qom-list'," 173 " 'arguments': {'path': '/objects' }}"); 174 test_object_available(qdict_get(resp, "return"), "rng0", "rng-builtin"); 175 qobject_unref(resp); 176 177 qtest_quit(qts); 178 } 179 180 static void test_docs_config_ich9(void) 181 { 182 QTestState *qts; 183 QDict *resp; 184 QObject *qobj; 185 186 qts = qtest_initf("-nodefaults -readconfig docs/config/ich9-ehci-uhci.cfg"); 187 188 resp = qtest_qmp(qts, "{ 'execute': 'qom-list'," 189 " 'arguments': {'path': '/machine/peripheral' }}"); 190 qobj = qdict_get(resp, "return"); 191 test_object_available(qobj, "ehci", "ich9-usb-ehci1"); 192 test_object_available(qobj, "uhci-1", "ich9-usb-uhci1"); 193 test_object_available(qobj, "uhci-2", "ich9-usb-uhci2"); 194 test_object_available(qobj, "uhci-3", "ich9-usb-uhci3"); 195 qobject_unref(resp); 196 197 qtest_quit(qts); 198 } 199 200 int main(int argc, char *argv[]) 201 { 202 const char *arch; 203 g_test_init(&argc, &argv, NULL); 204 205 arch = qtest_get_arch(); 206 207 if (g_str_equal(arch, "i386") || 208 g_str_equal(arch, "x86_64")) { 209 qtest_add_func("readconfig/x86/memdev", test_x86_memdev); 210 qtest_add_func("readconfig/x86/ich9-ehci-uhci", test_docs_config_ich9); 211 } 212 #if defined(CONFIG_SPICE) && !defined(__FreeBSD__) 213 qtest_add_func("readconfig/spice", test_spice); 214 #endif 215 216 qtest_add_func("readconfig/object-rng", test_object_rng); 217 218 return g_test_run(); 219 } 220