xref: /qemu/tests/qtest/vnc-display-test.c (revision 370ed600)
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 #if !defined(CONFIG_DARWIN)
23 
24 static void on_vnc_error(VncConnection* self,
25                          const char* msg)
26 {
27     g_error("vnc-error: %s", msg);
28 }
29 
30 static void on_vnc_auth_failure(VncConnection *self,
31                                 const char *msg)
32 {
33     g_error("vnc-auth-failure: %s", msg);
34 }
35 
36 #endif
37 
38 static bool
39 test_setup(Test *test)
40 {
41 #if defined(CONFIG_DARWIN)
42     g_test_skip("Broken on Darwin");
43     return false;
44 #else
45     int pair[2];
46 
47     test->qts = qtest_init("-M none -vnc none -name vnc-test");
48 
49     g_assert_cmpint(qemu_socketpair(AF_UNIX, SOCK_STREAM, 0, pair), ==, 0);
50 
51     qtest_qmp_add_client(test->qts, "vnc", pair[1]);
52 
53     test->conn = vnc_connection_new();
54     g_signal_connect(test->conn, "vnc-error",
55                      G_CALLBACK(on_vnc_error), NULL);
56     g_signal_connect(test->conn, "vnc-auth-failure",
57                      G_CALLBACK(on_vnc_auth_failure), NULL);
58     vnc_connection_set_auth_type(test->conn, VNC_CONNECTION_AUTH_NONE);
59 
60 #ifdef WIN32
61     vnc_connection_open_fd(test->conn, _get_osfhandle(pair[0]));
62 #else
63     vnc_connection_open_fd(test->conn, pair[0]);
64 #endif
65 
66     test->loop = g_main_loop_new(NULL, FALSE);
67     return true;
68 #endif
69 }
70 
71 static void
72 test_vnc_basic_on_vnc_initialized(VncConnection *self,
73                                  Test *test)
74 {
75     const char *name = vnc_connection_get_name(test->conn);
76 
77     g_assert_cmpstr(name, ==, "QEMU (vnc-test)");
78     g_main_loop_quit(test->loop);
79 }
80 
81 static void
82 test_vnc_basic(void)
83 {
84     Test test;
85 
86     if (!test_setup(&test)) {
87         return;
88     }
89 
90     g_signal_connect(test.conn, "vnc-initialized",
91                      G_CALLBACK(test_vnc_basic_on_vnc_initialized), &test);
92 
93     g_main_loop_run(test.loop);
94 
95     qtest_quit(test.qts);
96     g_object_unref(test.conn);
97     g_main_loop_unref(test.loop);
98 }
99 
100 int
101 main(int argc, char **argv)
102 {
103     if (getenv("GTK_VNC_DEBUG")) {
104         vnc_util_set_debug(true);
105     }
106 
107     g_test_init(&argc, &argv, NULL);
108 
109     qtest_add_func("/vnc-display/basic", test_vnc_basic);
110 
111     return g_test_run();
112 }
113