xref: /openbmc/qemu/tests/qtest/vnc-display-test.c (revision 6c25794fa36fe3fa483c6392c44ae68692aabb3a)
1f5755989SMarc-André Lureau /*
2f5755989SMarc-André Lureau  * VNC display tests
3f5755989SMarc-André Lureau  *
4f5755989SMarc-André Lureau  * Copyright (c) 2022 Red Hat, Inc.
5f5755989SMarc-André Lureau  *
6f5755989SMarc-André Lureau  * This work is licensed under the terms of the GNU GPL, version 2 or later.
7f5755989SMarc-André Lureau  * See the COPYING file in the top-level directory.
8f5755989SMarc-André Lureau  */
9f5755989SMarc-André Lureau 
10f5755989SMarc-André Lureau #include "qemu/osdep.h"
11f5755989SMarc-André Lureau #include "qemu/sockets.h"
12f5755989SMarc-André Lureau #include "libqtest.h"
13f5755989SMarc-André Lureau #include <gio/gio.h>
14f5755989SMarc-André Lureau #include <gvnc.h>
15f5755989SMarc-André Lureau 
16f5755989SMarc-André Lureau typedef struct Test {
17f5755989SMarc-André Lureau     QTestState *qts;
18f5755989SMarc-André Lureau     VncConnection *conn;
19f5755989SMarc-André Lureau     GMainLoop *loop;
20f5755989SMarc-André Lureau } Test;
21f5755989SMarc-André Lureau 
22*6c25794fSPhilippe Mathieu-Daudé #if !defined(WIN32)
23*6c25794fSPhilippe Mathieu-Daudé 
24f5755989SMarc-André Lureau static void on_vnc_error(VncConnection* self,
25f5755989SMarc-André Lureau                          const char* msg)
26f5755989SMarc-André Lureau {
27f5755989SMarc-André Lureau     g_error("vnc-error: %s", msg);
28f5755989SMarc-André Lureau }
29f5755989SMarc-André Lureau 
30f5755989SMarc-André Lureau static void on_vnc_auth_failure(VncConnection *self,
31f5755989SMarc-André Lureau                                 const char *msg)
32f5755989SMarc-André Lureau {
33f5755989SMarc-André Lureau     g_error("vnc-auth-failure: %s", msg);
34f5755989SMarc-André Lureau }
35f5755989SMarc-André Lureau 
36*6c25794fSPhilippe Mathieu-Daudé #endif
37*6c25794fSPhilippe Mathieu-Daudé 
38f5755989SMarc-André Lureau static bool
39f5755989SMarc-André Lureau test_setup(Test *test)
40f5755989SMarc-André Lureau {
41f5755989SMarc-André Lureau #ifdef WIN32
42f5755989SMarc-André Lureau     g_test_skip("Not supported on Windows yet");
43f5755989SMarc-André Lureau     return false;
44f5755989SMarc-André Lureau #else
45f5755989SMarc-André Lureau     int pair[2];
46f5755989SMarc-André Lureau 
47f5755989SMarc-André Lureau     test->qts = qtest_init("-vnc none -name vnc-test");
48f5755989SMarc-André Lureau 
49f5755989SMarc-André Lureau     g_assert_cmpint(qemu_socketpair(AF_UNIX, SOCK_STREAM, 0, pair), ==, 0);
50f5755989SMarc-André Lureau 
51f5755989SMarc-André Lureau     qtest_qmp_add_client(test->qts, "vnc", pair[1]);
52f5755989SMarc-André Lureau 
53f5755989SMarc-André Lureau     test->conn = vnc_connection_new();
54f5755989SMarc-André Lureau     g_signal_connect(test->conn, "vnc-error",
55f5755989SMarc-André Lureau                      G_CALLBACK(on_vnc_error), NULL);
56f5755989SMarc-André Lureau     g_signal_connect(test->conn, "vnc-auth-failure",
57f5755989SMarc-André Lureau                      G_CALLBACK(on_vnc_auth_failure), NULL);
58f5755989SMarc-André Lureau     vnc_connection_set_auth_type(test->conn, VNC_CONNECTION_AUTH_NONE);
59f5755989SMarc-André Lureau     vnc_connection_open_fd(test->conn, pair[0]);
60f5755989SMarc-André Lureau 
61f5755989SMarc-André Lureau     test->loop = g_main_loop_new(NULL, FALSE);
62f5755989SMarc-André Lureau     return true;
63f5755989SMarc-André Lureau #endif
64f5755989SMarc-André Lureau }
65f5755989SMarc-André Lureau 
66f5755989SMarc-André Lureau static void
67f5755989SMarc-André Lureau test_vnc_basic_on_vnc_initialized(VncConnection *self,
68f5755989SMarc-André Lureau                                  Test *test)
69f5755989SMarc-André Lureau {
70f5755989SMarc-André Lureau     const char *name = vnc_connection_get_name(test->conn);
71f5755989SMarc-André Lureau 
72f5755989SMarc-André Lureau     g_assert_cmpstr(name, ==, "QEMU (vnc-test)");
73f5755989SMarc-André Lureau     g_main_loop_quit(test->loop);
74f5755989SMarc-André Lureau }
75f5755989SMarc-André Lureau 
76f5755989SMarc-André Lureau static void
77f5755989SMarc-André Lureau test_vnc_basic(void)
78f5755989SMarc-André Lureau {
79f5755989SMarc-André Lureau     Test test;
80f5755989SMarc-André Lureau 
81f5755989SMarc-André Lureau     if (!test_setup(&test)) {
82f5755989SMarc-André Lureau         return;
83f5755989SMarc-André Lureau     }
84f5755989SMarc-André Lureau 
85f5755989SMarc-André Lureau     g_signal_connect(test.conn, "vnc-initialized",
86f5755989SMarc-André Lureau                      G_CALLBACK(test_vnc_basic_on_vnc_initialized), &test);
87f5755989SMarc-André Lureau 
88f5755989SMarc-André Lureau     g_main_loop_run(test.loop);
89f5755989SMarc-André Lureau 
90f5755989SMarc-André Lureau     qtest_quit(test.qts);
91f5755989SMarc-André Lureau     g_object_unref(test.conn);
92f5755989SMarc-André Lureau     g_main_loop_unref(test.loop);
93f5755989SMarc-André Lureau }
94f5755989SMarc-André Lureau 
95f5755989SMarc-André Lureau int
96f5755989SMarc-André Lureau main(int argc, char **argv)
97f5755989SMarc-André Lureau {
98f5755989SMarc-André Lureau     if (getenv("GTK_VNC_DEBUG")) {
99f5755989SMarc-André Lureau         vnc_util_set_debug(true);
100f5755989SMarc-André Lureau     }
101f5755989SMarc-André Lureau 
102f5755989SMarc-André Lureau     g_test_init(&argc, &argv, NULL);
103f5755989SMarc-André Lureau 
104f5755989SMarc-André Lureau     qtest_add_func("/vnc-display/basic", test_vnc_basic);
105f5755989SMarc-André Lureau 
106f5755989SMarc-André Lureau     return g_test_run();
107f5755989SMarc-André Lureau }
108