1
/* Copyright (c) 2009-2012 Dovecot authors, see the included COPYING file */
6
#include "mail-cache.h"
7
#include "dsync-proxy.h"
8
#include "test-dsync-common.h"
9
#include "test-common.h"
11
static void test_dsync_proxy_msg(void)
13
static const char *test_keywords[] = {
17
struct dsync_message msg_in, msg_out;
21
memset(&msg_in, 0, sizeof(msg_in));
22
memset(&msg_out, 0, sizeof(msg_out));
24
pool = pool_alloconly_create("msg pool", 1024);
26
msg_in.guid = "\t\001\r\nguid\t\001\n\r";
27
msg_in.uid = (uint32_t)-1;
28
msg_in.modseq = (uint64_t)-1;
29
msg_in.save_date = (1U << 31)-1;
31
test_begin("dsync proxy msg");
34
dsync_proxy_msg_export(str, &msg_in);
35
test_assert(dsync_proxy_msg_import(pool, str_c(str),
36
&msg_out, &error) == 0);
37
test_assert(dsync_messages_equal(&msg_in, &msg_out));
40
msg_in.flags = DSYNC_MAIL_FLAG_EXPUNGED;
42
dsync_proxy_msg_export(str, &msg_in);
43
test_assert(dsync_proxy_msg_import(pool, str_c(str),
44
&msg_out, &error) == 0);
45
test_assert(dsync_messages_equal(&msg_in, &msg_out));
47
/* expunged flag and another flag */
48
msg_in.flags = DSYNC_MAIL_FLAG_EXPUNGED | MAIL_DRAFT;
50
dsync_proxy_msg_export(str, &msg_in);
51
test_assert(dsync_proxy_msg_import(pool, str_c(str),
52
&msg_out, &error) == 0);
53
test_assert(dsync_messages_equal(&msg_in, &msg_out));
55
/* all flags, some keywords */
56
msg_in.flags = MAIL_FLAGS_MASK;
57
msg_in.keywords = test_keywords;
59
dsync_proxy_msg_export(str, &msg_in);
60
test_assert(dsync_proxy_msg_import(pool, str_c(str),
61
&msg_out, &error) == 0);
62
test_assert(dsync_messages_equal(&msg_in, &msg_out));
65
test_assert(dsync_proxy_msg_import(pool, "0", &msg_out, &error) < 0);
66
test_assert(dsync_proxy_msg_import(pool, "0\t0", &msg_out, &error) < 0);
67
test_assert(dsync_proxy_msg_import(pool, "0\t0\t0", &msg_out, &error) < 0);
68
test_assert(dsync_proxy_msg_import(pool, "0\t0\t0\t0", &msg_out, &error) < 0);
69
test_assert(dsync_proxy_msg_import(pool, "0\t0\t0\t\\\t0", &msg_out, &error) < 0);
70
test_assert(dsync_proxy_msg_import(pool, "0\t0\t0\t\\seen foo \\foo\t0", &msg_out, &error) < 0);
73
test_assert(dsync_proxy_msg_parse_flags(pool, "\\seen \\draft", &msg_out) == 0);
74
test_assert(msg_out.flags == (MAIL_SEEN | MAIL_DRAFT));
75
test_assert(dsync_proxy_msg_parse_flags(pool, "\\answered \\flagged", &msg_out) == 0);
76
test_assert(msg_out.flags == (MAIL_ANSWERED | MAIL_FLAGGED));
77
test_assert(dsync_proxy_msg_parse_flags(pool, "\\deleted \\recent", &msg_out) == 0);
78
test_assert(msg_out.flags == (MAIL_DELETED | MAIL_RECENT));
79
test_assert(dsync_proxy_msg_parse_flags(pool, "\\draft draft \\seen", &msg_out) == 0);
80
test_assert(msg_out.flags == (MAIL_DRAFT | MAIL_SEEN));
81
test_assert(strcasecmp(msg_out.keywords[0], "draft") == 0 && msg_out.keywords[1] == NULL);
87
static void test_dsync_proxy_mailbox(void)
89
static struct mailbox_cache_field cache1 =
90
{ "cache1", MAIL_CACHE_DECISION_NO, 1234 };
91
static struct mailbox_cache_field cache2 =
92
{ "cache2", MAIL_CACHE_DECISION_TEMP | MAIL_CACHE_DECISION_FORCED, 0 };
94
struct dsync_mailbox box_in, box_out;
98
memset(&box_in, 0, sizeof(box_in));
99
memset(&box_out, 0, sizeof(box_out));
101
pool = pool_alloconly_create("mailbox pool", 1024);
102
str = t_str_new(256);
104
test_begin("dsync proxy mailbox");
106
/* test \noselect mailbox */
107
box_in.name = "\t\001\r\nname\t\001\n\r";
108
box_in.name_sep = '/';
109
box_in.flags = DSYNC_MAILBOX_FLAG_NOSELECT;
110
dsync_proxy_mailbox_export(str, &box_in);
111
test_assert(dsync_proxy_mailbox_import(pool, str_c(str),
112
&box_out, &error) == 0);
113
test_assert(dsync_mailboxes_equal(&box_in, &box_out));
116
i_assert(sizeof(box_in.mailbox_guid.guid) == sizeof(test_mailbox_guid1));
117
memcpy(box_in.mailbox_guid.guid, test_mailbox_guid2, GUID_128_SIZE);
118
box_in.flags = 24242 & ~DSYNC_MAILBOX_FLAG_NOSELECT;
119
box_in.uid_validity = 0xf74d921b;
120
box_in.uid_next = 73529472;
121
box_in.highest_modseq = 0x123456789abcdef0ULL;
123
str_truncate(str, 0);
124
dsync_proxy_mailbox_export(str, &box_in);
125
test_assert(dsync_proxy_mailbox_import(pool, str_c(str),
126
&box_out, &error) == 0);
127
test_assert(dsync_mailboxes_equal(&box_in, &box_out));
130
box_in.uid_next = (uint32_t)-1;
131
box_in.highest_modseq = (uint64_t)-1;
133
str_truncate(str, 0);
134
dsync_proxy_mailbox_export(str, &box_in);
135
test_assert(dsync_proxy_mailbox_import(pool, str_c(str),
136
&box_out, &error) == 0);
137
test_assert(dsync_mailboxes_equal(&box_in, &box_out));
139
/* mailbox with cache fields */
140
t_array_init(&box_in.cache_fields, 10);
141
array_append(&box_in.cache_fields, &cache1, 1);
142
array_append(&box_in.cache_fields, &cache2, 1);
144
str_truncate(str, 0);
145
dsync_proxy_mailbox_export(str, &box_in);
146
test_assert(dsync_proxy_mailbox_import(pool, str_c(str),
147
&box_out, &error) == 0);
148
test_assert(dsync_mailboxes_equal(&box_in, &box_out));
154
static void test_dsync_proxy_guid(void)
156
mailbox_guid_t guid_in, guid_out;
159
test_begin("dsync proxy mailbox guid");
161
str = t_str_new(128);
162
memcpy(guid_in.guid, test_mailbox_guid1, sizeof(guid_in.guid));
163
dsync_proxy_mailbox_guid_export(str, &guid_in);
164
test_assert(dsync_proxy_mailbox_guid_import(str_c(str), &guid_out) == 0);
165
test_assert(memcmp(guid_in.guid, guid_out.guid, sizeof(guid_in.guid)) == 0);
167
test_assert(dsync_proxy_mailbox_guid_import("12345678901234567890123456789012", &guid_out) == 0);
168
test_assert(dsync_proxy_mailbox_guid_import("1234567890123456789012345678901", &guid_out) < 0);
169
test_assert(dsync_proxy_mailbox_guid_import("1234567890123456789012345678901g", &guid_out) < 0);
170
test_assert(dsync_proxy_mailbox_guid_import("", &guid_out) < 0);
177
static void (*test_functions[])(void) = {
178
test_dsync_proxy_msg,
179
test_dsync_proxy_mailbox,
180
test_dsync_proxy_guid,
183
return test_run(test_functions);