1 /* 2 * VNC display tests 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 "qemu/sockets.h" 12 #include "libqtest.h" 13 #include <gio/gio.h> 14 #include <gvnc.h> 15 16 typedef struct Test { 17 QTestState *qts; 18 VncConnection *conn; 19 GMainLoop *loop; 20 } Test; 21 22 static void on_vnc_error(VncConnection* self, 23 const char* msg) 24 { 25 g_error("vnc-error: %s", msg); 26 } 27 28 static void on_vnc_auth_failure(VncConnection *self, 29 const char *msg) 30 { 31 g_error("vnc-auth-failure: %s", msg); 32 } 33 34 static bool 35 test_setup(Test *test) 36 { 37 #ifdef WIN32 38 g_test_skip("Not supported on Windows yet"); 39 return false; 40 #else 41 int pair[2]; 42 43 test->qts = qtest_init("-vnc none -name vnc-test"); 44 45 g_assert_cmpint(qemu_socketpair(AF_UNIX, SOCK_STREAM, 0, pair), ==, 0); 46 47 qtest_qmp_add_client(test->qts, "vnc", pair[1]); 48 49 test->conn = vnc_connection_new(); 50 g_signal_connect(test->conn, "vnc-error", 51 G_CALLBACK(on_vnc_error), NULL); 52 g_signal_connect(test->conn, "vnc-auth-failure", 53 G_CALLBACK(on_vnc_auth_failure), NULL); 54 vnc_connection_set_auth_type(test->conn, VNC_CONNECTION_AUTH_NONE); 55 vnc_connection_open_fd(test->conn, pair[0]); 56 57 test->loop = g_main_loop_new(NULL, FALSE); 58 return true; 59 #endif 60 } 61 62 static void 63 test_vnc_basic_on_vnc_initialized(VncConnection *self, 64 Test *test) 65 { 66 const char *name = vnc_connection_get_name(test->conn); 67 68 g_assert_cmpstr(name, ==, "QEMU (vnc-test)"); 69 g_main_loop_quit(test->loop); 70 } 71 72 static void 73 test_vnc_basic(void) 74 { 75 Test test; 76 77 if (!test_setup(&test)) { 78 return; 79 } 80 81 g_signal_connect(test.conn, "vnc-initialized", 82 G_CALLBACK(test_vnc_basic_on_vnc_initialized), &test); 83 84 g_main_loop_run(test.loop); 85 86 qtest_quit(test.qts); 87 g_object_unref(test.conn); 88 g_main_loop_unref(test.loop); 89 } 90 91 int 92 main(int argc, char **argv) 93 { 94 if (getenv("GTK_VNC_DEBUG")) { 95 vnc_util_set_debug(true); 96 } 97 98 g_test_init(&argc, &argv, NULL); 99 100 qtest_add_func("/vnc-display/basic", test_vnc_basic); 101 102 return g_test_run(); 103 } 104