summaryrefslogtreecommitdiff
path: root/tests/file-transfer.c
blob: 1e25cacaf47bd85525550cb7ecc5a38d4562b84f (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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#include <gio/gio.h>

#include "spice-file-transfer-task-priv.h"

typedef struct _Fixture {
    GFile         **files;
    guint           num_files;
    guint           num_files_done;
    GCancellable   *cancellable;
    GMainLoop      *loop;
    GList          *tasks;
} Fixture;

typedef struct _AgentAsync {
    SpiceFileTransferTask          *xfer_task;
    VDAgentFileXferStatusMessage    msg;
} AgentAsync;

#define SINGLE_FILE     1
#define MULTIPLE_FILES  10

#define T10ns (G_TIME_SPAN_MILLISECOND / 100)

const gchar content[] = "0123456789_spice-file-transfer-task";

static void
f_setup(Fixture *f, gconstpointer user_data)
{
    gint i;
    GError *err = NULL;

    f->loop = g_main_loop_new(NULL, FALSE);
    f->num_files = GPOINTER_TO_UINT(user_data);
    f->num_files_done = 0;
    f->files = g_new0(GFile *, f->num_files + 1);
    f->cancellable = g_cancellable_new();
    for (i = 0; i < f->num_files; i++) {
        gboolean success;
        GFileIOStream *iostream;

        f->files[i] = g_file_new_tmp("spice-file-transfer-XXXXXX", &iostream, &err);
        g_assert_no_error(err);
        g_assert_nonnull(iostream);
        g_clear_object(&iostream);

        success = g_file_replace_contents (f->files[i], content, strlen(content), NULL, FALSE,
                                           G_FILE_CREATE_NONE, NULL, f->cancellable, &err);
        g_assert_no_error(err);
        g_assert_true(success);
    }
}

static void
f_teardown(Fixture *f, gconstpointer user_data)
{
    gint i;
    GError *err = NULL;

    g_main_loop_unref(f->loop);
    g_clear_object(&f->cancellable);
    g_clear_pointer(&f->tasks, g_list_free);

    for (i = 0; i < f->num_files; i++) {
        g_file_delete(f->files[i], NULL, &err);
        g_assert_no_error(err);
        g_object_unref(f->files[i]);
    }
    g_clear_pointer(&f->files, g_free);
}

static gboolean
agent_send_msg(gpointer user_data)
{
    AgentAsync *aa = user_data;
    spice_file_transfer_task_handle_status(aa->xfer_task, &aa->msg);
    g_free(aa);
    return G_SOURCE_REMOVE;
}

static void
agent_send_msg_async(SpiceFileTransferTask *xfer_task, guint32 result)
{
    AgentAsync *aa = g_new0(AgentAsync, 1);
    aa->xfer_task = xfer_task;
    aa->msg.result = result;
    g_object_get(xfer_task, "id", &aa->msg.id, NULL);
    g_timeout_add_full(G_PRIORITY_LOW, T10ns, agent_send_msg, aa, NULL);
}

#define agent_send_success_async(t)   agent_send_msg_async(t, VD_AGENT_FILE_XFER_STATUS_SUCCESS)
#define agent_send_more_data_async(t) agent_send_msg_async(t, VD_AGENT_FILE_XFER_STATUS_CAN_SEND_DATA)
#define agent_send_cancel_async(t)    agent_send_msg_async(t, VD_AGENT_FILE_XFER_STATUS_CANCELLED)
#define agent_send_error_async(t)     agent_send_msg_async(t, VD_AGENT_FILE_XFER_STATUS_ERROR)

/*******************************************************************************
 * TEST SIMPLE TRANSFER
 ******************************************************************************/
static void
transfer_done(GObject *source_object, GAsyncResult *res, gpointer user_data)
{
    Fixture *f = user_data;

    f->num_files_done++;
    if (f->num_files == f->num_files_done)
        g_main_loop_quit(f->loop);
}

static void
transfer_flush_callback(SpiceFileTransferTask *xfer_task,
                        void *buffer,
                        gssize count,
                        gpointer user_data)
{
    spice_file_transfer_task_flush_done(xfer_task, NULL);
    agent_send_success_async(xfer_task);
}

static void
transfer_task_on_file_info(SpiceFileTransferTask *xfer_task, GFileInfo *info, gpointer data)
{
    guint32 id;
    g_object_get (xfer_task, "id", &id, NULL);
    agent_send_more_data_async(xfer_task);
}

static void
test_simple_transfer(Fixture *f, gconstpointer user_data)
{
    GList *it;

    f->tasks = spice_file_transfer_task_create_tasks(NULL,
                                                     f->files,
                                                     G_FILE_COPY_NONE,
                                                     f->cancellable,
                                                     transfer_flush_callback,
                                                     NULL,
                                                     transfer_done,
                                                     f);
    for (it = f->tasks; it != NULL; it = it->next) {
        SpiceFileTransferTask *xfer_task = SPICE_FILE_TRANSFER_TASK(it->data);
        g_signal_connect(xfer_task, "file-info", G_CALLBACK(transfer_task_on_file_info), f);
        spice_file_transfer_task_start_task(xfer_task);
    }
    g_main_loop_run (f->loop);
}

/*******************************************************************************
 * TEST CANCEL ON START TASK
 ******************************************************************************/

static void
test_cancel_on_start_task(Fixture *f, gconstpointer user_data)
{
    GList *it;

    f->tasks = spice_file_transfer_task_create_tasks(NULL,
                                                     f->files,
                                                     G_FILE_COPY_NONE,
                                                     f->cancellable,
                                                     transfer_flush_callback,
                                                     NULL,
                                                     transfer_done,
                                                     f);
    for (it = f->tasks; it != NULL; it = it->next) {
        SpiceFileTransferTask *xfer_task = SPICE_FILE_TRANSFER_TASK(it->data);
        g_signal_connect(xfer_task, "file-info", G_CALLBACK(transfer_task_on_file_info), f);
        g_cancellable_cancel(f->cancellable);
        spice_file_transfer_task_start_task(xfer_task);
    }
    g_main_loop_run (f->loop);
}

/*******************************************************************************
 * TEST CANCEL ON FILE-INFO
 ******************************************************************************/

static void
transfer_flush_callback_cancel_before_flush(SpiceFileTransferTask *xfer_task,
                                            void *buffer,
                                            gssize count,
                                            gpointer user_data)
{
    gint i;
    Fixture *f = user_data;
    for (i = 0; i < f->num_files; i++) {
        g_test_expect_message("GSpice", G_LOG_LEVEL_CRITICAL, "*assertion 'SPICE_IS_CHANNEL(channel)*");
    }
    g_cancellable_cancel(f->cancellable);
    spice_file_transfer_task_flush_done(xfer_task, NULL);
    agent_send_success_async(xfer_task);
}

static void
test_cancel_on_file_info(Fixture *f, gconstpointer user_data)
{
    GList *it;

    f->tasks = spice_file_transfer_task_create_tasks(NULL,
                                                     f->files,
                                                     G_FILE_COPY_NONE,
                                                     f->cancellable,
                                                     transfer_flush_callback_cancel_before_flush,
                                                     f,
                                                     transfer_done,
                                                     f);
    for (it = f->tasks; it != NULL; it = it->next) {
        SpiceFileTransferTask *xfer_task = SPICE_FILE_TRANSFER_TASK(it->data);
        g_signal_connect(xfer_task, "file-info", G_CALLBACK(transfer_task_on_file_info), f);
        spice_file_transfer_task_start_task(xfer_task);
    }
    g_main_loop_run (f->loop);
}

/*******************************************************************************
 * TEST CANCEL ON READ FILE
 ******************************************************************************/

static void
transfer_flush_callback_cancel_after_flush(SpiceFileTransferTask *xfer_task,
                                           void *buffer,
                                           gssize count,
                                           gpointer user_data)
{
    gint i;
    Fixture *f = user_data;
    for (i = 0; i < f->num_files; i++) {
        g_test_expect_message("GSpice", G_LOG_LEVEL_CRITICAL, "*assertion 'SPICE_IS_CHANNEL(channel)*");
    }
    spice_file_transfer_task_flush_done(xfer_task, NULL);
    g_cancellable_cancel(f->cancellable);
    agent_send_success_async(xfer_task);
}

static void
test_cancel_on_read_file(Fixture *f, gconstpointer user_data)
{
    GList *it;

    f->tasks = spice_file_transfer_task_create_tasks(NULL,
                                                     f->files,
                                                     G_FILE_COPY_NONE,
                                                     f->cancellable,
                                                     transfer_flush_callback_cancel_after_flush,
                                                     f,
                                                     transfer_done,
                                                     f);
    for (it = f->tasks; it != NULL; it = it->next) {
        SpiceFileTransferTask *xfer_task = SPICE_FILE_TRANSFER_TASK(it->data);
        g_signal_connect(xfer_task, "file-info", G_CALLBACK(transfer_task_on_file_info), f);
        spice_file_transfer_task_start_task(xfer_task);
    }
    g_main_loop_run (f->loop);
}

/*******************************************************************************
 * TEST AGENT CANCEL ON READ
 ******************************************************************************/

static void
transfer_flush_callback_agent_cancel(SpiceFileTransferTask *xfer_task,
                                     void *buffer,
                                     gssize count,
                                     gpointer user_data)
{
    gint i;
    Fixture *f = user_data;
    spice_file_transfer_task_flush_done(xfer_task, NULL);
    agent_send_cancel_async(xfer_task);
}

static void
test_agent_cancel_on_read(Fixture *f, gconstpointer user_data)
{
    GList *it;

    f->tasks = spice_file_transfer_task_create_tasks(NULL,
                                                     f->files,
                                                     G_FILE_COPY_NONE,
                                                     f->cancellable,
                                                     transfer_flush_callback_agent_cancel,
                                                     f,
                                                     transfer_done,
                                                     f);
    for (it = f->tasks; it != NULL; it = it->next) {
        SpiceFileTransferTask *xfer_task = SPICE_FILE_TRANSFER_TASK(it->data);
        g_signal_connect(xfer_task, "file-info", G_CALLBACK(transfer_task_on_file_info), f);
        spice_file_transfer_task_start_task(xfer_task);
    }
    g_main_loop_run (f->loop);
}

/*******************************************************************************
 * TEST AGENT ERROR ON READ
 ******************************************************************************/

static void
transfer_flush_callback_agent_error(SpiceFileTransferTask *xfer_task,
                                    void *buffer,
                                    gssize count,
                                    gpointer user_data)
{
    gint i;
    Fixture *f = user_data;
    spice_file_transfer_task_flush_done(xfer_task, NULL);
    agent_send_error_async(xfer_task);
}

static void
test_agent_error_on_read(Fixture *f, gconstpointer user_data)
{
    GList *it;

    f->tasks = spice_file_transfer_task_create_tasks(NULL,
                                                     f->files,
                                                     G_FILE_COPY_NONE,
                                                     f->cancellable,
                                                     transfer_flush_callback_agent_error,
                                                     f,
                                                     transfer_done,
                                                     f);
    for (it = f->tasks; it != NULL; it = it->next) {
        SpiceFileTransferTask *xfer_task = SPICE_FILE_TRANSFER_TASK(it->data);
        g_signal_connect(xfer_task, "file-info", G_CALLBACK(transfer_task_on_file_info), f);
        spice_file_transfer_task_start_task(xfer_task);
    }
    g_main_loop_run (f->loop);
}

/* Tests summary:
 *
 * This tests are specific to SpiceFileTransferTask and how it handles the
 * Cancelation from the client and Cancelation/Error that it can receive from
 * the agent.
 *
 * Thanks to the helper spice_file_transfer_task_handle_status, we can simulate
 * the agent responde fairly easy by calling this function with the expected
 * message from agent in the test case.
 *
 * Small overview of how File Transfer works.
 *
 * 1.) User calls spice_main_file_copy_async with a list of files to send to the
 *     guest
 * 2.) Channel-Main creates a SpiceFileTransferTask per File, with a
 *     flush_callback() function which is used to send data to the Agent.
 * 3.) Channel-Main attach handlers to SpiceFileTransferTask signals such as
 *     - "file-info": needed to retrieve file information and to send this
 *       information to the Agent. The file-transfer protocol starts here with
 *       VD_AGENT_FILE_XFER_START message;
 *     - "finalized": needed to finalize the operation and resources used;
 * 4-) Channel-Main start each task by calling spice_file_transfer_task_start_task()
 *     which starts the async IO read from the file.
 * 5-) SpiceFileTransferTask calls flush_callback() everytime data is ready
 * 6-) Channel-Main sends data to the agent and waits for
 *     VD_AGENT_FILE_XFER_STATUS_CAN_SEND_DATA (to send more data) or
 *     VD_AGENT_FILE_XFER_STATUS_SUCCESS in case transfer of specific file is over.
 * 7-) SpiceFileTransferTask only finalizes its operation when receives success,
 *     error or cancel from Agent or upon cancel from client.
 */
int main(int argc, char* argv[])
{
    g_test_init(&argc, &argv, NULL);

    g_test_add("/spice-file-transfer-task/single/simple-transfer",
               Fixture, GUINT_TO_POINTER(SINGLE_FILE),
               f_setup, test_simple_transfer, f_teardown);

    g_test_add("/spice-file-transfer-task/single/cancel/on-start-task",
               Fixture, GUINT_TO_POINTER(SINGLE_FILE),
               f_setup, test_cancel_on_start_task, f_teardown);

    g_test_add("/spice-file-transfer-task/single/cancel/on-file-info",
               Fixture, GUINT_TO_POINTER(SINGLE_FILE),
               f_setup, test_cancel_on_file_info, f_teardown);

    g_test_add("/spice-file-transfer-task/single/cancel/on-read-file",
               Fixture, GUINT_TO_POINTER(SINGLE_FILE),
               f_setup, test_cancel_on_read_file, f_teardown);

    g_test_add("/spice-file-transfer-task/single/agent/cancel",
               Fixture, GUINT_TO_POINTER(SINGLE_FILE),
               f_setup, test_agent_cancel_on_read, f_teardown);

    g_test_add("/spice-file-transfer-task/single/agent/error",
               Fixture, GUINT_TO_POINTER(SINGLE_FILE),
               f_setup, test_agent_error_on_read, f_teardown);

    g_test_add("/spice-file-transfer-task/multiple/simple-transfer",
               Fixture, GUINT_TO_POINTER(MULTIPLE_FILES),
               f_setup, test_simple_transfer, f_teardown);

    g_test_add("/spice-file-transfer-task/multiple/cancel/on-start-task",
               Fixture, GUINT_TO_POINTER(MULTIPLE_FILES),
               f_setup, test_cancel_on_start_task, f_teardown);

    g_test_add("/spice-file-transfer-task/multiple/cancel/on-file-info",
               Fixture, GUINT_TO_POINTER(MULTIPLE_FILES),
               f_setup, test_cancel_on_file_info, f_teardown);

    g_test_add("/spice-file-transfer-task/multiple/cancel/on-read-file",
               Fixture, GUINT_TO_POINTER(MULTIPLE_FILES),
               f_setup, test_cancel_on_read_file, f_teardown);

    g_test_add("/spice-file-transfer-task/multiple/agent/cancel",
               Fixture, GUINT_TO_POINTER(MULTIPLE_FILES),
               f_setup, test_agent_cancel_on_read, f_teardown);

    g_test_add("/spice-file-transfer-task/multiple/agent/error",
               Fixture, GUINT_TO_POINTER(MULTIPLE_FILES),
               f_setup, test_agent_error_on_read, f_teardown);

    return g_test_run();
}