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