| 1 | #include "qemu/osdep.h" |
| 2 | #include "qemu/sockets.h" |
| 3 | #include "qemu/dbus.h" |
| 4 | #include "qemu/sockets.h" |
| 5 | #include "glib.h" |
| 6 | #include "glibconfig.h" |
| 7 | #include <gio/gio.h> |
| 8 | #include <gio/gunixfdlist.h> |
| 9 | #include "libqtest.h" |
| 10 | #include "qobject/qdict.h" |
| 11 | #include "qobject/qstring.h" |
| 12 | #include "ui/dbus-display1.h" |
| 13 | |
| 14 | static GDBusConnection* |
| 15 | test_dbus_p2p_from_fd(int fd) |
| 16 | { |
| 17 | g_autoptr(GError) err = NULL; |
| 18 | g_autoptr(GSocket) socket = NULL; |
| 19 | g_autoptr(GSocketConnection) socketc = NULL; |
| 20 | GDBusConnection *conn; |
| 21 | |
| 22 | #ifdef WIN32 |
| 23 | socket = g_socket_new_from_fd(_get_osfhandle(fd), &err); |
| 24 | #else |
| 25 | socket = g_socket_new_from_fd(fd, &err); |
| 26 | #endif |
| 27 | g_assert_no_error(err); |
| 28 | |
| 29 | socketc = g_socket_connection_factory_create_connection(socket); |
| 30 | g_assert(socketc != NULL); |
| 31 | |
| 32 | conn = g_dbus_connection_new_sync( |
| 33 | G_IO_STREAM(socketc), NULL, |
| 34 | G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT | |
| 35 | G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING, |
| 36 | NULL, NULL, &err); |
| 37 | g_assert_no_error(err); |
| 38 | |
| 39 | return conn; |
| 40 | } |
| 41 | |
| 42 | static void |
| 43 | test_setup_args(QTestState **qts, GDBusConnection **conn, const char *args) |
| 44 | { |
| 45 | int pair[2]; |
| 46 | |
| 47 | *qts = qtest_init(args); |
| 48 | |
| 49 | g_assert_cmpint(qemu_socketpair(AF_UNIX, SOCK_STREAM, 0, pair), ==, 0); |
| 50 | |
| 51 | qtest_qmp_add_client(*qts, "@dbus-display", pair[1]); |
| 52 | |
| 53 | *conn = test_dbus_p2p_from_fd(pair[0]); |
| 54 | g_dbus_connection_start_message_processing(*conn); |
| 55 | } |
| 56 | |
| 57 | static void |
| 58 | test_setup(QTestState **qts, GDBusConnection **conn) |
| 59 | { |
| 60 | test_setup_args(qts, conn, "-display dbus,p2p=yes -name dbus-test"); |
| 61 | } |
| 62 | |
| 63 | static void |
| 64 | test_dbus_display_vm(void) |
| 65 | { |
| 66 | g_autoptr(GError) err = NULL; |
| 67 | g_autoptr(GDBusConnection) conn = NULL; |
| 68 | g_autoptr(QemuDBusDisplay1VMProxy) vm = NULL; |
| 69 | QTestState *qts = NULL; |
| 70 | |
| 71 | test_setup(&qts, &conn); |
| 72 | |
| 73 | vm = QEMU_DBUS_DISPLAY1_VM_PROXY( |
| 74 | qemu_dbus_display1_vm_proxy_new_sync( |
| 75 | conn, |
| 76 | G_DBUS_PROXY_FLAGS_NONE, |
| 77 | NULL, |
| 78 | DBUS_DISPLAY1_ROOT "/VM", |
| 79 | NULL, |
| 80 | &err)); |
| 81 | g_assert_no_error(err); |
| 82 | |
| 83 | g_assert_cmpstr( |
| 84 | qemu_dbus_display1_vm_get_name(QEMU_DBUS_DISPLAY1_VM(vm)), |
| 85 | ==, |
| 86 | "dbus-test"); |
| 87 | g_clear_object(&conn); |
| 88 | qtest_quit(qts); |
| 89 | } |
| 90 | |
| 91 | typedef struct TestDBusConsoleRegister { |
| 92 | GMainLoop *loop; |
| 93 | GThread *thread; |
| 94 | GDBusConnection *listener_conn; |
| 95 | GDBusObjectManagerServer *server; |
| 96 | bool with_map; |
| 97 | } TestDBusConsoleRegister; |
| 98 | |
| 99 | static gboolean listener_handle_scanout( |
| 100 | QemuDBusDisplay1Listener *object, |
| 101 | GDBusMethodInvocation *invocation, |
| 102 | guint arg_width, |
| 103 | guint arg_height, |
| 104 | guint arg_stride, |
| 105 | guint arg_pixman_format, |
| 106 | GVariant *arg_data, |
| 107 | TestDBusConsoleRegister *test) |
| 108 | { |
| 109 | qemu_dbus_display1_listener_complete_scanout(object, invocation); |
| 110 | |
| 111 | if (!test->with_map) { |
| 112 | g_main_loop_quit(test->loop); |
| 113 | } |
| 114 | |
| 115 | return DBUS_METHOD_INVOCATION_HANDLED; |
| 116 | } |
| 117 | |
| 118 | #ifndef WIN32 |
| 119 | static gboolean listener_handle_scanout_map( |
| 120 | QemuDBusDisplay1ListenerUnixMap *object, |
| 121 | GDBusMethodInvocation *invocation, |
| 122 | GUnixFDList *fd_list, |
| 123 | GVariant *arg_handle, |
| 124 | guint arg_offset, |
| 125 | guint arg_width, |
| 126 | guint arg_height, |
| 127 | guint arg_stride, |
| 128 | guint arg_pixman_format, |
| 129 | TestDBusConsoleRegister *test) |
| 130 | { |
| 131 | int fd = -1; |
| 132 | gint32 handle = g_variant_get_handle(arg_handle); |
| 133 | g_autoptr(GError) error = NULL; |
| 134 | void *addr = NULL; |
| 135 | size_t len = arg_height * arg_stride; |
| 136 | |
| 137 | g_assert_cmpuint(g_unix_fd_list_get_length(fd_list), ==, 1); |
| 138 | fd = g_unix_fd_list_get(fd_list, handle, &error); |
| 139 | g_assert_no_error(error); |
| 140 | |
| 141 | addr = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, arg_offset); |
| 142 | g_assert_no_errno(addr == MAP_FAILED ? -1 : 0); |
| 143 | g_assert_no_errno(munmap(addr, len)); |
| 144 | |
| 145 | qemu_dbus_display1_listener_unix_map_complete_scanout_map(object, invocation, |
| 146 | NULL); |
| 147 | |
| 148 | g_main_loop_quit(test->loop); |
| 149 | |
| 150 | close(fd); |
| 151 | return DBUS_METHOD_INVOCATION_HANDLED; |
| 152 | } |
| 153 | #endif |
| 154 | |
| 155 | static void |
| 156 | test_dbus_console_setup_listener(TestDBusConsoleRegister *test, bool with_map) |
| 157 | { |
| 158 | g_autoptr(GDBusObjectSkeleton) listener = NULL; |
| 159 | g_autoptr(QemuDBusDisplay1ListenerSkeleton) iface = NULL; |
| 160 | |
| 161 | test->server = g_dbus_object_manager_server_new(DBUS_DISPLAY1_ROOT); |
| 162 | listener = g_dbus_object_skeleton_new(DBUS_DISPLAY1_ROOT "/Listener"); |
| 163 | iface = QEMU_DBUS_DISPLAY1_LISTENER_SKELETON( |
| 164 | qemu_dbus_display1_listener_skeleton_new()); |
| 165 | g_object_connect(iface, |
| 166 | "signal::handle-scanout", listener_handle_scanout, test, |
| 167 | NULL); |
| 168 | g_dbus_object_skeleton_add_interface(listener, |
| 169 | G_DBUS_INTERFACE_SKELETON(iface)); |
| 170 | if (with_map) { |
| 171 | #ifdef WIN32 |
| 172 | g_test_skip("map test lacking on win32"); |
| 173 | return; |
| 174 | #else |
| 175 | g_autoptr(QemuDBusDisplay1ListenerUnixMapSkeleton) iface_map = |
| 176 | QEMU_DBUS_DISPLAY1_LISTENER_UNIX_MAP_SKELETON( |
| 177 | qemu_dbus_display1_listener_unix_map_skeleton_new()); |
| 178 | |
| 179 | g_object_connect(iface_map, |
| 180 | "signal::handle-scanout-map", listener_handle_scanout_map, test, |
| 181 | NULL); |
| 182 | g_dbus_object_skeleton_add_interface(listener, |
| 183 | G_DBUS_INTERFACE_SKELETON(iface_map)); |
| 184 | g_object_set(iface, "interfaces", |
| 185 | (const gchar *[]) { "org.qemu.Display1.Listener.Unix.Map", NULL }, |
| 186 | NULL); |
| 187 | #endif |
| 188 | } |
| 189 | g_dbus_object_manager_server_export(test->server, listener); |
| 190 | g_dbus_object_manager_server_set_connection(test->server, |
| 191 | test->listener_conn); |
| 192 | |
| 193 | g_dbus_connection_start_message_processing(test->listener_conn); |
| 194 | } |
| 195 | |
| 196 | static void |
| 197 | test_dbus_console_registered(GObject *source_object, |
| 198 | GAsyncResult *res, |
| 199 | gpointer user_data) |
| 200 | { |
| 201 | TestDBusConsoleRegister *test = user_data; |
| 202 | g_autoptr(GError) err = NULL; |
| 203 | |
| 204 | qemu_dbus_display1_console_call_register_listener_finish( |
| 205 | QEMU_DBUS_DISPLAY1_CONSOLE(source_object), |
| 206 | #ifndef WIN32 |
| 207 | NULL, |
| 208 | #endif |
| 209 | res, &err); |
| 210 | |
| 211 | if (g_error_matches(err, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD)) { |
| 212 | g_test_skip("The VM doesn't have a console!"); |
| 213 | g_main_loop_quit(test->loop); |
| 214 | return; |
| 215 | } |
| 216 | |
| 217 | g_assert_no_error(err); |
| 218 | |
| 219 | test->listener_conn = g_thread_join(test->thread); |
| 220 | test_dbus_console_setup_listener(test, test->with_map); |
| 221 | } |
| 222 | |
| 223 | static gpointer |
| 224 | test_dbus_p2p_server_setup_thread(gpointer data) |
| 225 | { |
| 226 | return test_dbus_p2p_from_fd(GPOINTER_TO_INT(data)); |
| 227 | } |
| 228 | |
| 229 | static void |
| 230 | test_dbus_display_console(const void* data) |
| 231 | { |
| 232 | g_autoptr(GError) err = NULL; |
| 233 | g_autoptr(GDBusConnection) conn = NULL; |
| 234 | g_autoptr(QemuDBusDisplay1ConsoleProxy) console = NULL; |
| 235 | g_autoptr(GMainLoop) loop = NULL; |
| 236 | QTestState *qts = NULL; |
| 237 | int pair[2]; |
| 238 | TestDBusConsoleRegister test = { 0, .with_map = GPOINTER_TO_INT(data) }; |
| 239 | #ifdef WIN32 |
| 240 | WSAPROTOCOL_INFOW info; |
| 241 | g_autoptr(GVariant) listener = NULL; |
| 242 | #else |
| 243 | g_autoptr(GUnixFDList) fd_list = NULL; |
| 244 | int idx; |
| 245 | #endif |
| 246 | |
| 247 | test_setup(&qts, &conn); |
| 248 | |
| 249 | g_assert_cmpint(qemu_socketpair(AF_UNIX, SOCK_STREAM, 0, pair), ==, 0); |
| 250 | #ifndef WIN32 |
| 251 | fd_list = g_unix_fd_list_new(); |
| 252 | idx = g_unix_fd_list_append(fd_list, pair[1], NULL); |
| 253 | #endif |
| 254 | |
| 255 | console = QEMU_DBUS_DISPLAY1_CONSOLE_PROXY( |
| 256 | qemu_dbus_display1_console_proxy_new_sync( |
| 257 | conn, |
| 258 | G_DBUS_PROXY_FLAGS_NONE, |
| 259 | NULL, |
| 260 | "/org/qemu/Display1/Console_0", |
| 261 | NULL, |
| 262 | &err)); |
| 263 | g_assert_no_error(err); |
| 264 | |
| 265 | test.loop = loop = g_main_loop_new(NULL, FALSE); |
| 266 | test.thread = g_thread_new(NULL, test_dbus_p2p_server_setup_thread, |
| 267 | GINT_TO_POINTER(pair[0])); |
| 268 | |
| 269 | #ifdef WIN32 |
| 270 | if (WSADuplicateSocketW(_get_osfhandle(pair[1]), |
| 271 | GetProcessId((HANDLE) qtest_pid(qts)), |
| 272 | &info) == SOCKET_ERROR) |
| 273 | { |
| 274 | g_autofree char *emsg = g_win32_error_message(WSAGetLastError()); |
| 275 | g_error("WSADuplicateSocket failed: %s", emsg); |
| 276 | } |
| 277 | close(pair[1]); |
| 278 | listener = g_variant_new_fixed_array(G_VARIANT_TYPE_BYTE, |
| 279 | &info, |
| 280 | sizeof(info), |
| 281 | 1); |
| 282 | #endif |
| 283 | |
| 284 | qemu_dbus_display1_console_call_register_listener( |
| 285 | QEMU_DBUS_DISPLAY1_CONSOLE(console), |
| 286 | #ifdef WIN32 |
| 287 | listener, |
| 288 | #else |
| 289 | g_variant_new_handle(idx), |
| 290 | #endif |
| 291 | G_DBUS_CALL_FLAGS_NONE, |
| 292 | -1, |
| 293 | #ifndef WIN32 |
| 294 | fd_list, |
| 295 | #endif |
| 296 | NULL, |
| 297 | test_dbus_console_registered, |
| 298 | &test); |
| 299 | |
| 300 | g_main_loop_run(loop); |
| 301 | |
| 302 | g_clear_object(&test.server); |
| 303 | g_clear_object(&test.listener_conn); |
| 304 | g_clear_object(&conn); |
| 305 | qtest_quit(qts); |
| 306 | } |
| 307 | |
| 308 | static void |
| 309 | test_dbus_display_keyboard(void) |
| 310 | { |
| 311 | g_autoptr(GError) err = NULL; |
| 312 | g_autoptr(GDBusConnection) conn = NULL; |
| 313 | g_autoptr(QemuDBusDisplay1KeyboardProxy) keyboard = NULL; |
| 314 | QTestState *qts = NULL; |
| 315 | |
| 316 | test_setup(&qts, &conn); |
| 317 | |
| 318 | keyboard = QEMU_DBUS_DISPLAY1_KEYBOARD_PROXY( |
| 319 | qemu_dbus_display1_keyboard_proxy_new_sync( |
| 320 | conn, |
| 321 | G_DBUS_PROXY_FLAGS_NONE, |
| 322 | NULL, |
| 323 | "/org/qemu/Display1/Console_0", |
| 324 | NULL, |
| 325 | &err)); |
| 326 | g_assert_no_error(err); |
| 327 | |
| 328 | g_assert_cmpint(qtest_inb(qts, 0x64) & 0x1, ==, 0); |
| 329 | g_assert_cmpint(qtest_inb(qts, 0x60), ==, 0); |
| 330 | |
| 331 | qemu_dbus_display1_keyboard_call_press_sync( |
| 332 | QEMU_DBUS_DISPLAY1_KEYBOARD(keyboard), |
| 333 | 0x1C, /* qnum enter */ |
| 334 | G_DBUS_CALL_FLAGS_NONE, |
| 335 | -1, |
| 336 | NULL, |
| 337 | &err); |
| 338 | if (g_error_matches(err, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD)) { |
| 339 | g_test_skip("The VM doesn't have a console!"); |
| 340 | g_clear_object(&conn); |
| 341 | qtest_quit(qts); |
| 342 | return; |
| 343 | } |
| 344 | |
| 345 | g_assert_no_error(err); |
| 346 | |
| 347 | /* may be should wait for interrupt? */ |
| 348 | g_assert_cmpint(qtest_inb(qts, 0x64) & 0x1, ==, 1); |
| 349 | g_assert_cmpint(qtest_inb(qts, 0x60), ==, 0x5A); /* scan code 2 enter */ |
| 350 | |
| 351 | qemu_dbus_display1_keyboard_call_release_sync( |
| 352 | QEMU_DBUS_DISPLAY1_KEYBOARD(keyboard), |
| 353 | 0x1C, /* qnum enter */ |
| 354 | G_DBUS_CALL_FLAGS_NONE, |
| 355 | -1, |
| 356 | NULL, |
| 357 | &err); |
| 358 | g_assert_no_error(err); |
| 359 | |
| 360 | g_assert_cmpint(qtest_inb(qts, 0x64) & 0x1, ==, 1); |
| 361 | g_assert_cmpint(qtest_inb(qts, 0x60), ==, 0xF0); /* scan code 2 release */ |
| 362 | g_assert_cmpint(qtest_inb(qts, 0x60), ==, 0x5A); /* scan code 2 enter */ |
| 363 | |
| 364 | g_assert_cmpint(qemu_dbus_display1_keyboard_get_modifiers( |
| 365 | QEMU_DBUS_DISPLAY1_KEYBOARD(keyboard)), ==, 0); |
| 366 | |
| 367 | g_clear_object(&conn); |
| 368 | qtest_quit(qts); |
| 369 | } |
| 370 | |
| 371 | static gsize |
| 372 | get_console_ids_count(GDBusConnection *conn) |
| 373 | { |
| 374 | g_autoptr(GError) err = NULL; |
| 375 | g_autoptr(QemuDBusDisplay1VMProxy) vm = NULL; |
| 376 | GVariant *console_ids; |
| 377 | gsize n_ids = 0; |
| 378 | |
| 379 | vm = QEMU_DBUS_DISPLAY1_VM_PROXY( |
| 380 | qemu_dbus_display1_vm_proxy_new_sync( |
| 381 | conn, |
| 382 | G_DBUS_PROXY_FLAGS_NONE, |
| 383 | NULL, |
| 384 | DBUS_DISPLAY1_ROOT "/VM", |
| 385 | NULL, |
| 386 | &err)); |
| 387 | g_assert_no_error(err); |
| 388 | |
| 389 | console_ids = qemu_dbus_display1_vm_get_console_ids( |
| 390 | QEMU_DBUS_DISPLAY1_VM(vm)); |
| 391 | if (console_ids) { |
| 392 | n_ids = g_variant_n_children(console_ids); |
| 393 | } |
| 394 | return n_ids; |
| 395 | } |
| 396 | |
| 397 | static void |
| 398 | wait_device_event(QTestState *qts, const char *event_name, const char *id) |
| 399 | { |
| 400 | QDict *resp, *data; |
| 401 | QString *qstr; |
| 402 | |
| 403 | for (;;) { |
| 404 | resp = qtest_qmp_eventwait_ref(qts, event_name); |
| 405 | data = qdict_get_qdict(resp, "data"); |
| 406 | if (!data || !qdict_get(data, "device")) { |
| 407 | qobject_unref(resp); |
| 408 | continue; |
| 409 | } |
| 410 | qstr = qobject_to(QString, qdict_get(data, "device")); |
| 411 | if (!strcmp(qstring_get_str(qstr), id)) { |
| 412 | qobject_unref(resp); |
| 413 | break; |
| 414 | } |
| 415 | qobject_unref(resp); |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | static void |
| 420 | test_dbus_display_hotplug(void) |
| 421 | { |
| 422 | g_autoptr(GDBusConnection) conn = NULL; |
| 423 | QTestState *qts = NULL; |
| 424 | gsize n; |
| 425 | |
| 426 | test_setup_args(&qts, &conn, |
| 427 | "-machine q35" |
| 428 | " -device pcie-root-port,id=rp0" |
| 429 | " -display dbus,p2p=yes" |
| 430 | " -name dbus-test"); |
| 431 | |
| 432 | n = get_console_ids_count(conn); |
| 433 | g_assert_cmpuint(n, ==, 1); |
| 434 | |
| 435 | qtest_qmp_device_add(qts, "bochs-display", "bochs0", |
| 436 | "{'bus': 'rp0'}"); |
| 437 | |
| 438 | n = get_console_ids_count(conn); |
| 439 | g_assert_cmpuint(n, ==, 2); |
| 440 | |
| 441 | /* |
| 442 | * On q35, PCI device removal is ACPI-based and requires guest |
| 443 | * acknowledgement. Since qtest has no guest OS, issue the delete |
| 444 | * request and force removal via system reset. |
| 445 | */ |
| 446 | qtest_qmp_device_del_send(qts, "bochs0"); |
| 447 | qtest_system_reset_nowait(qts); |
| 448 | wait_device_event(qts, "DEVICE_DELETED", "bochs0"); |
| 449 | |
| 450 | n = get_console_ids_count(conn); |
| 451 | g_assert_cmpuint(n, ==, 1); |
| 452 | |
| 453 | g_clear_object(&conn); |
| 454 | qtest_quit(qts); |
| 455 | } |
| 456 | |
| 457 | int |
| 458 | main(int argc, char **argv) |
| 459 | { |
| 460 | g_test_init(&argc, &argv, NULL); |
| 461 | |
| 462 | qtest_add_func("/dbus-display/vm", test_dbus_display_vm); |
| 463 | qtest_add_data_func("/dbus-display/console", GINT_TO_POINTER(false), test_dbus_display_console); |
| 464 | qtest_add_data_func("/dbus-display/console/map", GINT_TO_POINTER(true), test_dbus_display_console); |
| 465 | qtest_add_func("/dbus-display/keyboard", test_dbus_display_keyboard); |
| 466 | if (qtest_has_machine("q35") && qtest_has_device("bochs-display")) { |
| 467 | qtest_add_func("/dbus-display/hotplug", test_dbus_display_hotplug); |
| 468 | } |
| 469 | |
| 470 | return g_test_run(); |
| 471 | } |