xref: /openbmc/qemu/tests/qtest/vnc-display-test.c (revision fe843ea21889236f41a96590513b042e59032a39)
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(WIN32)
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 #ifdef WIN32
42     g_test_skip("Not supported on Windows yet");
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     vnc_connection_open_fd(test->conn, pair[0]);
60 
61     test->loop = g_main_loop_new(NULL, FALSE);
62     return true;
63 #endif
64 }
65 
66 static void
67 test_vnc_basic_on_vnc_initialized(VncConnection *self,
68                                  Test *test)
69 {
70     const char *name = vnc_connection_get_name(test->conn);
71 
72     g_assert_cmpstr(name, ==, "QEMU (vnc-test)");
73     g_main_loop_quit(test->loop);
74 }
75 
76 static void
77 test_vnc_basic(void)
78 {
79     Test test;
80 
81     if (!test_setup(&test)) {
82         return;
83     }
84 
85     g_signal_connect(test.conn, "vnc-initialized",
86                      G_CALLBACK(test_vnc_basic_on_vnc_initialized), &test);
87 
88     g_main_loop_run(test.loop);
89 
90     qtest_quit(test.qts);
91     g_object_unref(test.conn);
92     g_main_loop_unref(test.loop);
93 }
94 
95 int
96 main(int argc, char **argv)
97 {
98     if (getenv("GTK_VNC_DEBUG")) {
99         vnc_util_set_debug(true);
100     }
101 
102     g_test_init(&argc, &argv, NULL);
103 
104     qtest_add_func("/vnc-display/basic", test_vnc_basic);
105 
106     return g_test_run();
107 }
108