1
/* Copyright (c) 2009-2012 Dovecot authors, see the included COPYING file */
9
#include "test-common.h"
10
#include "dsync-proxy-server.h"
11
#include "test-dsync-worker.h"
12
#include "test-dsync-common.h"
14
#define ALL_MAIL_FLAGS "\\Answered \\Flagged \\Deleted \\Seen \\Draft \\Recent"
16
struct master_service *master_service;
18
static struct dsync_proxy_server *server;
19
static struct test_dsync_worker *test_worker;
20
static struct dsync_proxy_server_command *cur_cmd;
21
static const char *cur_cmd_args[20];
23
static void out_clear(void)
25
o_stream_seek(server->output, 0);
29
static int run_more(void)
33
i_assert(cur_cmd != NULL);
35
ret = cur_cmd->func(server, cur_cmd_args);
43
static int ATTR_SENTINEL
44
run_cmd(const char *cmd_name, ...)
50
i_assert(cur_cmd == NULL);
52
va_start(va, cmd_name);
53
while ((str = va_arg(va, const char *)) != NULL) {
54
i_assert(i < N_ELEMENTS(cur_cmd_args)+1);
55
cur_cmd_args[i++] = str;
57
cur_cmd_args[i] = NULL;
60
cur_cmd = dsync_proxy_server_command_find(cmd_name);
61
i_assert(cur_cmd != NULL);
65
static void test_dsync_proxy_box_list(void)
67
struct dsync_mailbox box;
69
test_begin("proxy server box list");
71
test_assert(run_cmd("BOX-LIST", NULL) == 0);
73
/* \noselect mailbox */
74
memset(&box, 0, sizeof(box));
75
box.name = "\t\001\r\nname\t\001\n\r";
77
box.last_change = 992;
78
box.flags = DSYNC_MAILBOX_FLAG_NOSELECT;
79
test_worker->box_iter.next_box = &box;
80
test_assert(run_more() == 0);
81
test_assert(strcmp(str_c(out), t_strconcat(str_tabescape(box.name),
82
"\t/\t992\t1\n", NULL)) == 0);
85
/* selectable mailbox */
86
memset(&box, 0, sizeof(box));
89
memcpy(box.mailbox_guid.guid, test_mailbox_guid1, GUID_128_SIZE);
90
box.uid_validity = 4275878552;
91
box.uid_next = 4023233417;
92
box.message_count = 4525;
93
box.highest_modseq = 18080787909545915012ULL;
94
box.first_recent_uid = 353;
95
test_worker->box_iter.next_box = &box;
97
test_assert(run_more() == 0);
99
test_assert(strcmp(str_c(out), "foo/bar\t/\t0\t0\t"
100
TEST_MAILBOX_GUID1"\t"
104
"18080787909545915012\t"
109
test_worker->box_iter.last = TRUE;
110
test_assert(run_more() == 1);
111
test_assert(strcmp(str_c(out), "+\n") == 0);
117
static void test_dsync_proxy_subs_list(void)
120
struct dsync_worker_subscription subs;
121
struct dsync_worker_unsubscription unsubs;
123
test_begin("proxy server subs list");
125
test_assert(run_cmd("SUBS-LIST", NULL) == 0);
128
name = "\t\001\r\nname\t\001\n\r";
130
subs.storage_name = "\tstorage_name\n";
131
subs.last_change = 1234567890;
132
subs.ns_prefix = "\t\001\r\nprefix\t\001\n\r";
133
test_worker->subs_iter.next_subscription = &subs;
134
test_assert(run_more() == 0);
135
test_assert(strcmp(str_c(out), t_strconcat(
136
str_tabescape(name), "\t",
137
str_tabescape(subs.storage_name), "\t",
138
str_tabescape(subs.ns_prefix),
139
"\t1234567890\n", NULL)) == 0);
142
test_worker->subs_iter.last_subs = TRUE;
143
test_assert(run_more() == 0);
144
test_assert(strcmp(str_c(out), "+\n") == 0);
148
memcpy(unsubs.name_sha1.guid, test_mailbox_guid1,
149
sizeof(unsubs.name_sha1.guid));
150
unsubs.ns_prefix = "\t\001\r\nprefix2\t\001\n\r";
151
unsubs.last_change = 987654321;
152
test_worker->subs_iter.next_unsubscription = &unsubs;
153
test_assert(run_more() == 0);
154
test_assert(strcmp(str_c(out), t_strconcat(TEST_MAILBOX_GUID1, "\t",
155
str_tabescape(unsubs.ns_prefix), "\t987654321\n", NULL)) == 0);
158
test_worker->subs_iter.last_unsubs = TRUE;
159
test_assert(run_more() == 1);
160
test_assert(strcmp(str_c(out), "+\n") == 0);
166
static void test_dsync_proxy_msg_list(void)
168
static const char *test_keywords[] = {
171
struct dsync_message msg;
172
struct test_dsync_worker_msg test_msg;
174
test_begin("proxy server msg list");
176
test_assert(run_cmd("MSG-LIST", TEST_MAILBOX_GUID1, TEST_MAILBOX_GUID2, NULL) == 0);
178
memset(&msg, 0, sizeof(msg));
179
msg.guid = "\t\001\r\nguid\t\001\n\r";
181
msg.modseq = 98765432101234;
182
msg.save_date = 1234567890;
186
test_msg.mailbox_idx = 98;
187
array_append(&test_worker->msg_iter.msgs, &test_msg, 1);
188
test_assert(run_more() == 0);
189
test_assert(strcmp(str_c(out), t_strconcat(
190
"98\t", str_tabescape(msg.guid),
191
"\t123\t98765432101234\t\t1234567890\n", NULL)) == 0);
194
/* all flags, some keywords */
198
msg.flags = MAIL_FLAGS_MASK;
199
msg.keywords = test_keywords;
201
test_msg.mailbox_idx = 76;
202
array_append(&test_worker->msg_iter.msgs, &test_msg, 1);
203
test_assert(run_more() == 0);
204
test_assert(strcmp(str_c(out), "76\tguid\t123\t1\t"
205
ALL_MAIL_FLAGS" kw1 kw2\t2\n") == 0);
209
test_worker->msg_iter.last = TRUE;
210
test_assert(run_more() == 1);
211
test_assert(strcmp(str_c(out), "+\n") == 0);
217
static void test_dsync_proxy_box_create(void)
219
struct test_dsync_box_event event;
221
test_begin("proxy server box create");
223
test_assert(run_cmd("BOX-CREATE", "noselect", "/",
224
"553", "1", NULL) == 1);
225
test_assert(test_dsync_worker_next_box_event(test_worker, &event));
226
test_assert(event.type == LAST_BOX_TYPE_CREATE);
227
test_assert(strcmp(event.box.name, "noselect") == 0);
228
test_assert(event.box.name_sep == '/');
229
test_assert(event.box.last_change == 553);
230
test_assert(event.box.flags == DSYNC_MAILBOX_FLAG_NOSELECT);
231
test_assert(event.box.uid_validity == 0);
233
test_assert(run_cmd("BOX-CREATE", "selectable", "?",
234
"61", "2", TEST_MAILBOX_GUID2, "1234567890", "9876",
235
"4610", "28427847284728", "853", NULL) == 1);
236
test_assert(test_dsync_worker_next_box_event(test_worker, &event));
237
test_assert(event.type == LAST_BOX_TYPE_CREATE);
238
test_assert(strcmp(event.box.name, "selectable") == 0);
239
test_assert(event.box.name_sep == '?');
240
test_assert(memcmp(event.box.mailbox_guid.guid, test_mailbox_guid2, GUID_128_SIZE) == 0);
241
test_assert(event.box.flags == 2);
242
test_assert(event.box.uid_validity == 1234567890);
243
test_assert(event.box.uid_next == 9876);
244
test_assert(event.box.message_count == 4610);
245
test_assert(event.box.highest_modseq == 28427847284728);
246
test_assert(event.box.first_recent_uid == 853);
247
test_assert(event.box.last_change == 61);
252
static void test_dsync_proxy_box_delete(void)
254
struct test_dsync_box_event event;
256
test_begin("proxy server box delete");
258
test_assert(run_cmd("BOX-DELETE", TEST_MAILBOX_GUID1, "4351", NULL) == 1);
259
test_assert(test_dsync_worker_next_box_event(test_worker, &event));
260
test_assert(event.type == LAST_BOX_TYPE_DELETE);
261
test_assert(memcmp(event.box.mailbox_guid.guid, test_mailbox_guid1, GUID_128_SIZE) == 0);
262
test_assert(event.box.last_change == 4351);
264
test_assert(run_cmd("BOX-DELETE", TEST_MAILBOX_GUID2, "653", NULL) == 1);
265
test_assert(test_dsync_worker_next_box_event(test_worker, &event));
266
test_assert(event.type == LAST_BOX_TYPE_DELETE);
267
test_assert(memcmp(event.box.mailbox_guid.guid, test_mailbox_guid2, GUID_128_SIZE) == 0);
268
test_assert(event.box.last_change == 653);
273
static void test_dsync_proxy_box_rename(void)
275
struct test_dsync_box_event event;
277
test_begin("proxy server box rename");
279
test_assert(run_cmd("BOX-RENAME", TEST_MAILBOX_GUID1, "name\t1", "/", NULL) == 1);
280
test_assert(test_dsync_worker_next_box_event(test_worker, &event));
281
test_assert(event.type == LAST_BOX_TYPE_RENAME);
282
test_assert(memcmp(event.box.mailbox_guid.guid, test_mailbox_guid1, GUID_128_SIZE) == 0);
283
test_assert(strcmp(event.box.name, "name\t1") == 0);
284
test_assert(event.box.name_sep == '/');
286
test_assert(run_cmd("BOX-RENAME", TEST_MAILBOX_GUID2, "", "?", NULL) == 1);
287
test_assert(test_dsync_worker_next_box_event(test_worker, &event));
288
test_assert(event.type == LAST_BOX_TYPE_RENAME);
289
test_assert(memcmp(event.box.mailbox_guid.guid, test_mailbox_guid2, GUID_128_SIZE) == 0);
290
test_assert(strcmp(event.box.name, "") == 0);
291
test_assert(event.box.name_sep == '?');
296
static void test_dsync_proxy_box_update(void)
298
struct test_dsync_box_event event;
300
test_begin("proxy server box update");
302
test_assert(run_cmd("BOX-UPDATE", "updated", "/",
303
"53", "2", TEST_MAILBOX_GUID1, "34343", "22",
304
"58293", "2238427847284728", "2482", NULL) == 1);
305
test_assert(test_dsync_worker_next_box_event(test_worker, &event));
306
test_assert(event.type == LAST_BOX_TYPE_UPDATE);
307
test_assert(strcmp(event.box.name, "updated") == 0);
308
test_assert(event.box.name_sep == '/');
309
test_assert(memcmp(event.box.mailbox_guid.guid, test_mailbox_guid1, GUID_128_SIZE) == 0);
310
test_assert(event.box.flags == DSYNC_MAILBOX_FLAG_DELETED_MAILBOX);
311
test_assert(event.box.uid_validity == 34343);
312
test_assert(event.box.uid_next == 22);
313
test_assert(event.box.message_count == 58293);
314
test_assert(event.box.highest_modseq == 2238427847284728);
315
test_assert(event.box.first_recent_uid == 2482);
316
test_assert(event.box.last_change == 53);
321
static void test_dsync_proxy_box_select(void)
323
test_begin("proxy server box select");
325
test_assert(run_cmd("BOX-SELECT", TEST_MAILBOX_GUID1, NULL) == 1);
326
test_assert(memcmp(test_worker->selected_mailbox.guid, test_mailbox_guid1, GUID_128_SIZE) == 0);
328
test_assert(run_cmd("BOX-SELECT", TEST_MAILBOX_GUID2, NULL) == 1);
329
test_assert(memcmp(test_worker->selected_mailbox.guid, test_mailbox_guid2, GUID_128_SIZE) == 0);
334
static void test_dsync_proxy_msg_update(void)
336
struct test_dsync_msg_event event;
338
test_begin("proxy server msg update");
340
test_assert(run_cmd("MSG-UPDATE", "123", "4782782842924",
341
"kw1 "ALL_MAIL_FLAGS" kw2", NULL) == 1);
342
test_assert(test_dsync_worker_next_msg_event(test_worker, &event));
343
test_assert(event.type == LAST_MSG_TYPE_UPDATE);
344
test_assert(event.msg.uid == 123);
345
test_assert(event.msg.modseq == 4782782842924);
346
test_assert(event.msg.flags == MAIL_FLAGS_MASK);
347
test_assert(strcmp(event.msg.keywords[0], "kw1") == 0);
348
test_assert(strcmp(event.msg.keywords[1], "kw2") == 0);
349
test_assert(event.msg.keywords[2] == NULL);
354
static void test_dsync_proxy_msg_uid_change(void)
356
struct test_dsync_msg_event event;
358
test_begin("proxy server msg uid change");
360
test_assert(run_cmd("MSG-UID-CHANGE", "454", "995", NULL) == 1);
361
test_assert(test_dsync_worker_next_msg_event(test_worker, &event));
362
test_assert(event.type == LAST_MSG_TYPE_UPDATE_UID);
363
test_assert(event.msg.uid == 454);
364
test_assert(event.msg.modseq == 995);
369
static void test_dsync_proxy_msg_expunge(void)
371
struct test_dsync_msg_event event;
373
test_begin("proxy server msg expunge");
375
test_assert(run_cmd("MSG-EXPUNGE", "8585", NULL) == 1);
376
test_assert(test_dsync_worker_next_msg_event(test_worker, &event));
377
test_assert(event.type == LAST_MSG_TYPE_EXPUNGE);
378
test_assert(event.msg.uid == 8585);
383
static void test_dsync_proxy_msg_copy(void)
385
struct test_dsync_msg_event msg_event;
387
test_begin("proxy server msg copy");
389
test_assert(run_cmd("MSG-COPY", TEST_MAILBOX_GUID1, "5454",
390
"copyguid", "5678", "74782482882924", "\\Seen foo \\Draft",
391
"8294284", NULL) == 1);
392
test_assert(test_dsync_worker_next_msg_event(test_worker, &msg_event));
393
test_assert(msg_event.type == LAST_MSG_TYPE_COPY);
394
test_assert(memcmp(msg_event.copy_src_mailbox.guid, test_mailbox_guid1, GUID_128_SIZE) == 0);
395
test_assert(msg_event.copy_src_uid == 5454);
396
test_assert(strcmp(msg_event.msg.guid, "copyguid") == 0);
397
test_assert(msg_event.msg.uid == 5678);
398
test_assert(msg_event.msg.modseq == 74782482882924);
399
test_assert(msg_event.msg.flags == (MAIL_SEEN | MAIL_DRAFT));
400
test_assert(strcmp(msg_event.msg.keywords[0], "foo") == 0);
401
test_assert(msg_event.msg.keywords[1] == NULL);
402
test_assert(msg_event.msg.save_date == 8294284);
407
static void test_dsync_proxy_msg_save(void)
409
static const char *input = "..dotty\n..behavior\nfrom you\n.\nstop";
410
struct test_dsync_msg_event event;
411
const unsigned char *data;
414
test_begin("proxy server msg save");
416
server->input = i_stream_create_from_data(input, strlen(input));
418
test_assert(run_cmd("MSG-SAVE", "28492428", "pop3uidl",
419
"saveguid", "874", "33982482882924", "\\Flagged bar \\Answered",
420
"8294284", NULL) == 1);
421
test_assert(test_dsync_worker_next_msg_event(test_worker, &event));
422
test_assert(event.type == LAST_MSG_TYPE_SAVE);
423
test_assert(event.save_data.received_date == 28492428);
424
test_assert(strcmp(event.save_data.pop3_uidl, "pop3uidl") == 0);
425
test_assert(strcmp(event.save_body, ".dotty\n.behavior\nfrom you") == 0);
427
test_assert(strcmp(event.msg.guid, "saveguid") == 0);
428
test_assert(event.msg.uid == 874);
429
test_assert(event.msg.modseq == 33982482882924);
430
test_assert(event.msg.flags == (MAIL_FLAGGED | MAIL_ANSWERED));
431
test_assert(strcmp(event.msg.keywords[0], "bar") == 0);
432
test_assert(event.msg.keywords[1] == NULL);
433
test_assert(event.msg.save_date == 8294284);
435
data = i_stream_get_data(server->input, &size);
436
test_assert(size == 4 && memcmp(data, "stop", 4) == 0);
437
i_stream_destroy(&server->input);
442
static struct dsync_proxy_server *
443
dsync_proxy_server_init_test(buffer_t *outbuf)
445
struct dsync_proxy_server *server;
447
server = i_new(struct dsync_proxy_server, 1);
448
server->worker = dsync_worker_init_test();
452
server->cmd_pool = pool_alloconly_create("worker server cmd", 1024);
453
server->output = o_stream_create_buffer(outbuf);
459
static void (*test_functions[])(void) = {
460
test_dsync_proxy_box_list,
461
test_dsync_proxy_subs_list,
462
test_dsync_proxy_msg_list,
463
test_dsync_proxy_box_create,
464
test_dsync_proxy_box_delete,
465
test_dsync_proxy_box_rename,
466
test_dsync_proxy_box_update,
467
test_dsync_proxy_box_select,
468
test_dsync_proxy_msg_update,
469
test_dsync_proxy_msg_uid_change,
470
test_dsync_proxy_msg_expunge,
471
test_dsync_proxy_msg_copy,
472
test_dsync_proxy_msg_save,
478
out = buffer_create_dynamic(default_pool, 1024);
479
server = dsync_proxy_server_init_test(out);
480
test_worker = (struct test_dsync_worker *)server->worker;
482
test_run_funcs(test_functions);
483
return test_deinit();