summaryrefslogtreecommitdiff
path: root/server/tests/test-channel.cpp
blob: ed083f1ad710cc7068cebca604d1ab9a1c0436d4 (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
/* -*- Mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
   Copyright (C) 2017 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 test allocate a channel and do some test sending some messages
 */
#include <config.h>
#include <unistd.h>
#include <spice.h>

#include "test-glib-compat.h"
#include "basic-event-loop.h"
#include "reds.h"
#include "red-client.h"
#include "cursor-channel.h"
#include "net-utils.h"
#include "win-alarm.h"

/*
 * Declare a RedTestChannel to be used for the test
 */
SPICE_DECLARE_TYPE(RedTestChannel, red_test_channel, TEST_CHANNEL);
#define RED_TYPE_TEST_CHANNEL red_test_channel_get_type()

struct RedTestChannel final: public RedChannel
{
};

struct RedTestChannelClass
{
    RedChannelClass parent_class;
};

G_DEFINE_TYPE(RedTestChannel, red_test_channel, RED_TYPE_CHANNEL)

SPICE_DECLARE_TYPE(RedTestChannelClient, red_test_channel_client, TEST_CHANNEL_CLIENT);
#define RED_TYPE_TEST_CHANNEL_CLIENT red_test_channel_client_get_type()

struct RedTestChannelClient final: public RedChannelClient
{
};

struct RedTestChannelClientClass
{
    RedChannelClientClass parent_class;
};

G_DEFINE_TYPE(RedTestChannelClient, red_test_channel_client, RED_TYPE_CHANNEL_CLIENT)

static void
red_test_channel_init(RedTestChannel *self)
{
}

static void
red_test_channel_client_init(RedTestChannelClient *self)
{
}

static void
test_channel_send_item(RedChannelClient *rcc, RedPipeItem *item)
{
}

static void
test_connect_client(RedChannel *channel, RedClient *client, RedStream *stream,
                    int migration, RedChannelCapabilities *caps)
{
    RedChannelClient *rcc;
    rcc = (RedChannelClient *)
          g_initable_new(RED_TYPE_TEST_CHANNEL_CLIENT,
                         NULL, NULL,
                         "channel", channel,
                         "client", client,
                         "stream", stream,
                         "caps", caps,
                         NULL);
    g_assert_nonnull(rcc);

    // requires an ACK after 10 messages
    rcc->ack_set_client_window(10);

    // initialize ACK feature
    rcc->ack_zero_messages_window();
    rcc->push_set_ack();

    // send enough messages till we should require an ACK
    // the ACK is waited after 2 * 10, append some other messages
    for (int i = 0; i < 25; ++i) {
        rcc->pipe_add_empty_msg(SPICE_MSG_MIGRATE_DATA);
    }
}

static void
red_test_channel_class_init(RedTestChannelClass *klass)
{
    RedChannelClass *channel_class = RED_CHANNEL_CLASS(klass);
    channel_class->parser = spice_get_client_channel_parser(SPICE_CHANNEL_PORT, NULL);
    channel_class->handle_message = RedChannelClient::handle_message;
    channel_class->send_item = test_channel_send_item;
    channel_class->connect = test_connect_client;
}

static uint8_t *
red_test_channel_client_alloc_msg_rcv_buf(RedChannelClient *rcc, uint16_t type, uint32_t size)
{
    return (uint8_t*) g_malloc(size);
}

static void
red_test_channel_client_release_msg_rcv_buf(RedChannelClient *rcc,
                                            uint16_t type, uint32_t size, uint8_t *msg)
{
    g_free(msg);
}

static void
red_test_channel_client_class_init(RedTestChannelClientClass *klass)
{
    RedChannelClientClass *client_class = RED_CHANNEL_CLIENT_CLASS(klass);
    client_class->alloc_recv_buf = red_test_channel_client_alloc_msg_rcv_buf;
    client_class->release_recv_buf = red_test_channel_client_release_msg_rcv_buf;
}


/*
 * Main test part
 */
typedef SpiceWatch *watch_add_t(const SpiceCoreInterfaceInternal *iface,
                                int fd, int event_mask, SpiceWatchFunc func, void *opaque);
static watch_add_t *old_watch_add = NULL;
static SpiceWatchFunc old_watch_func = NULL;

static int watch_called_countdown = 5;

// this function is injected in the RedChannelClient watch function
static void watch_func_inject(int fd, int event, void *opaque)
{
    // check we are not doing too many loops
    if (--watch_called_countdown <= 0) {
        spice_error("Watch called too many times");
    }
    old_watch_func(fd, event, opaque);
}

static SpiceWatch *
watch_add_inject(const SpiceCoreInterfaceInternal *iface,
                 int fd, int event_mask, SpiceWatchFunc func, void *opaque)
{
    g_assert_null(old_watch_func);
    old_watch_func = func;
    SpiceWatch* ret = old_watch_add(iface, fd, event_mask, watch_func_inject, opaque);
    return ret;
}

static int client_socket = -1;

static void send_ack_sync(int socket, uint32_t generation)
{
    struct {
        uint16_t dummy;
        uint16_t type;
        uint32_t len;
        uint32_t generation;
    } msg;
    SPICE_VERIFY(sizeof(msg) == 12);
    msg.type = GUINT16_TO_LE(SPICE_MSGC_ACK_SYNC);
    msg.len = GUINT32_TO_LE(sizeof(generation));
    msg.generation = GUINT32_TO_LE(generation);

    g_assert_cmpint(socket_write(socket, &msg.type, 10), ==, 10);
}

static SpiceTimer *waked_up_timer;

// timer waiting we get data again
static void timer_wakeup(void *opaque)
{
    SpiceCoreInterface *core = (SpiceCoreInterface*) opaque;

    // check we are receiving data again
    size_t got_data = 0;
    ssize_t len;
    alarm(1);
    char buffer[256];
    while ((len=socket_read(client_socket, buffer, sizeof(buffer))) > 0)
        got_data += len;
    alarm(0);

    g_assert_cmpint(got_data, >, 0);

    core->timer_remove(waked_up_timer);

    basic_event_loop_quit();
}

// timeout, now we can send the ack
// if we arrive here it means we didn't receive too many watch events
static void timeout_watch_count(void *opaque)
{
    SpiceCoreInterface *core = (SpiceCoreInterface*) opaque;

    // get all pending data
    alarm(1);
    char buffer[256];
    while (socket_read(client_socket, buffer, sizeof(buffer)) > 0)
        continue;
    alarm(0);

    // we don't need to count anymore
    watch_called_countdown = 20;

    // send ack reply, this should unblock data from RedChannelClient
    g_assert_cmpint(socket_write(client_socket, "\2\0\0\0\0\0", 6), ==, 6);

    // expect data soon
    waked_up_timer = core->timer_add(timer_wakeup, core);
    core->timer_start(waked_up_timer, 100);
    // TODO watch
}

static RedStream *create_dummy_stream(SpiceServer *server, int *p_socket)
{
    int sv[2];
    g_assert_cmpint(socketpair(AF_LOCAL, SOCK_STREAM, 0, sv), ==, 0);
    if (p_socket) {
        *p_socket = sv[1];
    }
    red_socket_set_non_blocking(sv[0], true);
    red_socket_set_non_blocking(sv[1], true);

    RedStream * stream = red_stream_new(server, sv[0]);
    g_assert_nonnull(stream);

    return stream;
}

static void channel_loop(void)
{
    SpiceCoreInterface *core;
    SpiceServer *server = spice_server_new();

    g_assert_nonnull(server);

    core = basic_event_loop_init();
    g_assert_nonnull(core);

    g_assert_cmpint(spice_server_init(server, core), ==, 0);

    // create a channel and connect to it
    RedChannel *channel =
        (RedChannel*) g_object_new(RED_TYPE_TEST_CHANNEL,
                     "spice-server", server,
                     "core-interface", reds_get_core_interface(server),
                     "channel-type", SPICE_CHANNEL_PORT, // any other than main is fine
                     "id", 0,
                     "handle-acks", TRUE, // we want to test this
                     NULL);

    // create dummy RedClient and MainChannelClient
    RedChannelCapabilities caps;
    memset(&caps, 0, sizeof(caps));
    uint32_t common_caps = 1 << SPICE_COMMON_CAP_MINI_HEADER;
    caps.num_common_caps = 1;
    caps.common_caps = (uint32_t*) spice_memdup(&common_caps, sizeof(common_caps));

    RedClient *client = red_client_new(server, FALSE);
    g_assert_nonnull(client);

    MainChannel *main_channel = main_channel_new(server);
    g_assert_nonnull(main_channel);

    MainChannelClient *mcc;
    mcc = main_channel_link(main_channel, client, create_dummy_stream(server, NULL),
                            0, FALSE, &caps);
    g_assert_nonnull(mcc);

    // inject a trace into the core interface to count the events
    SpiceCoreInterfaceInternal *server_core = reds_get_core_interface(server);
    old_watch_add = server_core->watch_add;
    server_core->watch_add = watch_add_inject;

    // create our testing RedChannelClient
    channel->connect(client, create_dummy_stream(server, &client_socket),
                     FALSE, &caps);
    red_channel_capabilities_reset(&caps);

    // remove code to inject code during RedChannelClient watch, we set it
    g_assert_nonnull(old_watch_func);
    server_core->watch_add = old_watch_add;

    send_ack_sync(client_socket, 1);

    // set a timeout when to send back the acknowledge,
    // during this time we check not receiving too many events
    SpiceTimer *watch_timer;
    watch_timer = core->timer_add(timeout_watch_count, core);
    core->timer_start(watch_timer, 100);

    // start all test
    basic_event_loop_mainloop();

    // cleanup
    red_client_destroy(client);
    g_object_unref(main_channel);
    g_object_unref(channel);

    core->timer_remove(watch_timer);

    spice_server_destroy(server);

    basic_event_loop_destroy();
}

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

    g_test_add_func("/server/channel", channel_loop);

    return g_test_run();
}