summaryrefslogtreecommitdiff
path: root/server/tests/test-listen.c
blob: 4d4bd1590ab7527c1aaec232c97740414fd2d5c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/* -*- Mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
   Copyright (C) 2018 Red Hat, Inc.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
/*
 * This tests the external API entry points to configure the address/port
 * spice-server is listening on
 */
#include <config.h>

#include "basic-event-loop.h"

#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <gio/gio.h>
#ifndef _WIN32
#include <gio/gunixsocketaddress.h>
#endif

#include "test-glib-compat.h"

/* Arbitrary base port, we want a port which is not in use by the system, and
 * by another of our tests (in case of parallel runs)
 */
#define BASE_PORT 5728

#define PKI_DIR SPICE_TOP_SRCDIR "/server/tests/pki/"

static bool error_is_set(GError **error)
{
    return ((error != NULL) && (*error != NULL));
}

typedef struct {
    SpiceCoreInterface *core;
    SpiceTimer *exit_mainloop_timer;
    SpiceTimer *timeout_timer;
} TestEventLoop;

static void timeout_cb(SPICE_GNUC_UNUSED void *opaque)
{
    g_assert_not_reached();
}

static void exit_mainloop_cb(SPICE_GNUC_UNUSED void *opaque)
{
    basic_event_loop_quit();
}

static void test_event_loop_quit(TestEventLoop *event_loop)
{
    event_loop->core->timer_start(event_loop->exit_mainloop_timer, 0);
}

static void test_event_loop_init(TestEventLoop *event_loop)
{
    event_loop->core = basic_event_loop_init();
    event_loop->timeout_timer = event_loop->core->timer_add(timeout_cb, NULL);
    event_loop->exit_mainloop_timer = event_loop->core->timer_add(exit_mainloop_cb, NULL);
}

static void test_event_loop_destroy(TestEventLoop *event_loop)
{
    if (event_loop->timeout_timer != NULL) {
        event_loop->core->timer_remove(event_loop->timeout_timer);
        event_loop->timeout_timer = NULL;
    }
    if (event_loop->exit_mainloop_timer != NULL) {
        event_loop->core->timer_remove(event_loop->exit_mainloop_timer);
        event_loop->exit_mainloop_timer = NULL;
    }
    basic_event_loop_destroy();
    event_loop->core = NULL;
}

static void test_event_loop_run(TestEventLoop *event_loop)
{
    event_loop->core->timer_start(event_loop->timeout_timer, 50000);
    basic_event_loop_mainloop();
}

static GIOStream *fake_client_connect(GSocketConnectable *connectable, GError **error)
{
    GSocketClient *client;
    GSocketConnection *connection;

    client = g_socket_client_new();
    connection = g_socket_client_connect(client, connectable, NULL, error);

    g_object_unref(client);

    return G_IO_STREAM(connection);
}

static GIOStream *fake_client_connect_tls(GSocketConnectable *connectable, GError **error)
{
    GSocketClient *client;
    GSocketConnection *connection;
    GIOStream *tls_connection;

    client = g_socket_client_new();
    connection = g_socket_client_connect(client, connectable, NULL, error);
    g_assert_no_error(*error);
    tls_connection = g_tls_client_connection_new(G_IO_STREAM(connection),
                                                 connectable,
                                                 error);
    g_assert_no_error(*error);
    /* Disable all certificate checks as our test setup is known to be invalid */
    g_tls_client_connection_set_validation_flags(G_TLS_CLIENT_CONNECTION(tls_connection), (GTlsCertificateFlags) 0);

    g_object_unref(connection);
    g_object_unref(client);

    return tls_connection;
}

static void check_magic(GIOStream *io_stream, GError **error)
{
    uint8_t buffer[4];
    gsize bytes_read;
    gsize bytes_written;
    GInputStream *input_stream;
    GOutputStream *output_stream;

    /* send dummy data to trigger a response from the server */
    output_stream = g_io_stream_get_output_stream(io_stream);
    memset(buffer, 0xa5, G_N_ELEMENTS(buffer));
    g_output_stream_write_all(output_stream, buffer, G_N_ELEMENTS(buffer), &bytes_written, NULL, error);
    if (error_is_set(error)) {
        return;
    }

    input_stream = g_io_stream_get_input_stream(io_stream);
    g_input_stream_read_all(input_stream, buffer, G_N_ELEMENTS(buffer), &bytes_read, NULL, error);
    if (error_is_set(error)) {
        return;
    }
    g_assert_cmpuint(bytes_read, ==, G_N_ELEMENTS(buffer));
    g_assert_cmpint(memcmp(buffer, "REDQ", 4), ==, 0);
}

typedef struct
{
    GSocketConnectable *connectable;
    bool use_tls;
    TestEventLoop *event_loop;
} ThreadData;

static gpointer check_magic_thread(gpointer data)
{
    GError *error = NULL;
    ThreadData *thread_data = (ThreadData*) data;
    GSocketConnectable *connectable = G_SOCKET_CONNECTABLE(thread_data->connectable);
    GIOStream *stream;

    if (thread_data->use_tls) {
        stream = fake_client_connect_tls(connectable, &error);
    } else {
        stream = fake_client_connect(connectable, &error);
    }
    g_assert_no_error(error);
    check_magic(stream, &error);
    g_assert_no_error(error);

    g_object_unref(stream);
    g_object_unref(connectable);

    test_event_loop_quit(thread_data->event_loop);
    g_free(thread_data);

    return NULL;
}

static gpointer check_no_connect_thread(gpointer data)
{
    GError *error = NULL;
    ThreadData *thread_data = (ThreadData*) data;
    GSocketConnectable *connectable = G_SOCKET_CONNECTABLE(thread_data->connectable);
    GIOStream *stream;

    stream = fake_client_connect(connectable, &error);
    g_assert_nonnull(error);
    g_assert_null(stream);
    g_clear_error(&error);

    g_object_unref(connectable);

    test_event_loop_quit(thread_data->event_loop);
    g_free(thread_data);

    return NULL;
}

static GThread *fake_client_new(GThreadFunc thread_func,
                                const char *hostname, int port,
                                bool use_tls,
                                TestEventLoop *event_loop)
{
    ThreadData *thread_data = g_new0(ThreadData, 1);

    if (port == -1) {
#ifndef _WIN32
        thread_data->connectable = G_SOCKET_CONNECTABLE(g_unix_socket_address_new(hostname));
#else
        g_assert_not_reached();
#endif
    } else {
        g_assert_cmpuint(port, >, 0);
        g_assert_cmpuint(port, <, 65536);
        thread_data->connectable = g_network_address_new(hostname, port);
    }
    thread_data->use_tls = use_tls;
    thread_data->event_loop = event_loop;

    /* check_magic_thread will assume ownership of 'connectable' */
    return g_thread_new("fake-client-thread", thread_func, thread_data);
}

static void test_connect_plain(void)
{
    GThread *thread;
    int result;

    TestEventLoop event_loop = { 0, };

    test_event_loop_init(&event_loop);

    /* server */
    SpiceServer *server = spice_server_new();
    spice_server_set_name(server, "SPICE listen test");
    spice_server_set_noauth(server);
    spice_server_set_port(server, BASE_PORT);
    result = spice_server_init(server, event_loop.core);
    g_assert_cmpint(result, ==, 0);

    /* fake client */
    thread = fake_client_new(check_magic_thread, "localhost", BASE_PORT, false, &event_loop);
    test_event_loop_run(&event_loop);
    g_assert_null(g_thread_join(thread));

    test_event_loop_destroy(&event_loop);
    spice_server_destroy(server);
}

static void test_connect_tls(void)
{
    GThread *thread;
    int result;

    TestEventLoop event_loop = { 0, };

    test_event_loop_init(&event_loop);

    /* server */
    SpiceServer *server = spice_server_new();
    spice_server_set_name(server, "SPICE listen test");
    spice_server_set_noauth(server);
    result = spice_server_set_tls(server, BASE_PORT,
                                  PKI_DIR "ca-cert.pem",
                                  PKI_DIR "server-cert.pem",
                                  PKI_DIR "server-key.pem",
                                  NULL, NULL, NULL);
    g_assert_cmpint(result, ==, 0);
    result = spice_server_init(server, event_loop.core);
    g_assert_cmpint(result, ==, 0);

    /* fake client */
    thread = fake_client_new(check_magic_thread, "localhost", BASE_PORT, true, &event_loop);
    test_event_loop_run(&event_loop);
    g_assert_null(g_thread_join(thread));

    test_event_loop_destroy(&event_loop);
    spice_server_destroy(server);
}

static void test_connect_plain_and_tls(void)
{
    GThread *thread;
    int result;

    TestEventLoop event_loop = { 0, };

    test_event_loop_init(&event_loop);

    /* server */
    SpiceServer *server = spice_server_new();
    spice_server_set_name(server, "SPICE listen test");
    spice_server_set_noauth(server);
    spice_server_set_port(server, BASE_PORT);
    result = spice_server_set_tls(server, BASE_PORT+1,
                                  PKI_DIR "ca-cert.pem",
                                  PKI_DIR "server-cert.pem",
                                  PKI_DIR "server-key.pem",
                                  NULL, NULL, NULL);
    g_assert_cmpint(result, ==, 0);
    result = spice_server_init(server, event_loop.core);
    g_assert_cmpint(result, ==, 0);

    /* fake client */
    thread = fake_client_new(check_magic_thread, "localhost", BASE_PORT, false, &event_loop);
    test_event_loop_run(&event_loop);
    g_assert_null(g_thread_join(thread));

    thread = fake_client_new(check_magic_thread, "localhost", BASE_PORT+1, true, &event_loop);
    test_event_loop_run(&event_loop);
    g_assert_null(g_thread_join(thread));

    test_event_loop_destroy(&event_loop);
    spice_server_destroy(server);
}

#ifndef _WIN32
static void test_connect_unix(void)
{
    GThread *thread;
    int result;

    TestEventLoop event_loop = { 0, };

    test_event_loop_init(&event_loop);

    /* server */
    SpiceServer *server = spice_server_new();
    spice_server_set_name(server, "SPICE listen test");
    spice_server_set_noauth(server);
    spice_server_set_addr(server, "test-listen.unix", SPICE_ADDR_FLAG_UNIX_ONLY);
    result = spice_server_init(server, event_loop.core);
    g_assert_cmpint(result, ==, 0);

    /* fake client */
    thread = fake_client_new(check_magic_thread, "test-listen.unix", -1, false, &event_loop);
    test_event_loop_run(&event_loop);
    g_assert_null(g_thread_join(thread));

    test_event_loop_destroy(&event_loop);
    spice_server_destroy(server);
}
#endif

static void test_connect_ko(void)
{
    GThread *thread;
    TestEventLoop event_loop = { 0, };

    test_event_loop_init(&event_loop);

    /* fake client */
    thread = fake_client_new(check_no_connect_thread, "localhost", BASE_PORT, false, &event_loop);
    test_event_loop_run(&event_loop);
    g_assert_null(g_thread_join(thread));

    test_event_loop_destroy(&event_loop);
}

int main(int argc, char **argv)
{
    g_test_init(&argc, &argv, NULL);

    g_test_add_func("/server/listen/connect_plain", test_connect_plain);
    g_test_add_func("/server/listen/connect_tls", test_connect_tls);
    g_test_add_func("/server/listen/connect_both", test_connect_plain_and_tls);
#ifndef _WIN32
    g_test_add_func("/server/listen/connect_unix", test_connect_unix);
#endif
    g_test_add_func("/server/listen/connect_ko", test_connect_ko);

    return g_test_run();
}