-
Notifications
You must be signed in to change notification settings - Fork 154
/
Copy pathduplicate.testc
309 lines (266 loc) · 8.49 KB
/
duplicate.testc
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
#include <unistd.h>
#include <stdlib.h>
#include "config.h"
#include "cunit/cyrunit.h"
#include "imap/duplicate.h"
#include "xmalloc.h"
#include "retry.h"
#include "imap/global.h"
#include "imap/imap_err.h"
#include "cyrusdb.h"
#include "libcyr_cfg.h"
#include "libconfig.h"
#define DBDIR "test-mb-dbdir"
struct result
{
struct result *next;
char *id;
char *to;
char *date;
time_t mark;
unsigned long uid;
};
#define GOTRESULT(expid, expto, expdate, expmark, expuid) \
{ \
struct result *actual = results; \
CU_ASSERT_PTR_NOT_NULL_FATAL(results); \
results = results->next; \
CU_ASSERT_STRING_EQUAL(expid, actual->id); \
CU_ASSERT_STRING_EQUAL(expto, actual->to); \
CU_ASSERT_STRING_EQUAL(expdate, actual->date); \
CU_ASSERT_EQUAL(expmark, actual->mark); \
CU_ASSERT_EQUAL(expuid, actual->uid); \
free(actual->id); \
free(actual->to); \
free(actual->date); \
free(actual); \
}
static void test_getset(void)
{
duplicate_key_t dkey = DUPLICATE_INITIALIZER;
time_t t;
time_t now;
static const char MSGID1[] = "<fake0999@fastmail.fm>";
static const char MSGID2[] = "<fake1001@fastmail.fm>";
static const char FOLDER[] = "user.smurf";
static const char DATE[] = "Wed, 27 Oct 2010 18:37:26 +1100";
static unsigned long UID = 42;
/* an invalid key returns 0 */
dkey.id = NULL;
dkey.to = NULL;
dkey.date = NULL;
t = duplicate_check(&dkey);
CU_ASSERT_EQUAL(t, 0);
/* a valid but missing key returns 0 */
dkey.id = MSGID1;
dkey.to = FOLDER;
dkey.date = DATE;
t = duplicate_check(&dkey);
CU_ASSERT_EQUAL(t, 0);
/* the other one too */
dkey.id = MSGID2;
dkey.to = FOLDER;
dkey.date = DATE;
t = duplicate_check(&dkey);
CU_ASSERT_EQUAL(t, 0);
/* mark appears to succeed */
dkey.id = MSGID1;
dkey.to = FOLDER;
dkey.date = DATE;
now = time(NULL);
duplicate_mark(&dkey, now, UID);
/* an invalid key still returns 0 */
dkey.id = NULL;
dkey.to = NULL;
dkey.date = NULL;
t = duplicate_check(&dkey);
CU_ASSERT_EQUAL(t, 0);
/* a valid and present key returns non-zero */
dkey.id = MSGID1;
dkey.to = FOLDER;
dkey.date = DATE;
t = duplicate_check(&dkey);
CU_ASSERT_NOT_EQUAL(t, 0);
CU_ASSERT_EQUAL(t, now);
/* a valid but missing key still returns 0 */
dkey.id = MSGID2;
dkey.to = FOLDER;
dkey.date = DATE;
t = duplicate_check(&dkey);
CU_ASSERT_EQUAL(t, 0);
}
static int finder(const duplicate_key_t *dkey, time_t mark,
unsigned long uid, void *rock)
{
struct result **head = (struct result **)rock;
struct result **tail;
struct result *res;
// if (verbose)
// fprintf(stderr, "found: id=\"%s\" to=\"%s\" date=\"%s\" "
// "mark=%lu uid=%lu\n",
// dkey->id, dkey->to, dkey->date,
// (unsigned long)mark, (unsigned long)uid);
/* remember arguments for later perusal */
res = xzmalloc(sizeof(*res));
res->id = xstrdup(dkey->id);
res->to = xstrdup(dkey->to);
res->date = xstrdup(dkey->date);
res->mark = mark;
res->uid = uid;
/* append to the list. yes, it's inefficient. */
for (tail = head ; *tail ; tail = &(*tail)->next)
;
*tail = res;
return 0;
}
static void test_find(void)
{
duplicate_key_t dkey = DUPLICATE_INITIALIZER;
struct result *results = NULL;
static const char MSGID1[] = "<fake0999@fastmail.fm>";
static const char MSGID2[] = "<fake1001@fastmail.fm>";
/* test data from hipsteripsum.me */
static const char FOLDER1[] = "user.vegan";
static const char FOLDER2[] = "user.irony";
static const char FOLDER3[] = "user.single.origin";
static const char FOLDER4[] = "user.loko";
static const char FOLDER5[] = "user.biodiesel";
static const char DATE1[] = "Wed, 27 Oct 2010 18:01:02 +1100";
static const char DATE2[] = "Wed, 27 Oct 2010 18:03:04 +1100";
static const char DATE3[] = "Wed, 27 Oct 2010 18:05:06 +1100";
static const char DATE4[] = "Wed, 27 Oct 2010 18:07:08 +1100";
static const char DATE5[] = "Wed, 27 Oct 2010 18:09:10 +1100";
static time_t MARK1 = 1319088235;
static time_t MARK2 = 1319088235;
static time_t MARK3 = 1319088236;
static time_t MARK4 = 1319088735;
static time_t MARK5 = 1319089235;
static unsigned long UID1 = 23;
static unsigned long UID2 = 37;
static unsigned long UID3 = 42;
static unsigned long UID4 = 1007;
static unsigned long UID5 = 314159;
int r;
/* find on an empty db returns nothing */
r = duplicate_find(MSGID1, finder, &results);
CU_ASSERT_EQUAL(r, 0);
CU_ASSERT_PTR_NULL(results);
r = duplicate_find(MSGID2, finder, &results);
CU_ASSERT_EQUAL(r, 0);
CU_ASSERT_PTR_NULL(results);
/* add one entry */
dkey.id = MSGID1;
dkey.to = FOLDER1;
dkey.date = DATE1;
duplicate_mark(&dkey, MARK1, UID1);
/* find returns the only entry */
r = duplicate_find(MSGID1, finder, &results);
CU_ASSERT_EQUAL(r, 0);
GOTRESULT(MSGID1, FOLDER1, DATE1, MARK1, UID1);
CU_ASSERT_PTR_NULL(results);
/* find of another msgid returns nothing */
r = duplicate_find(MSGID2, finder, &results);
CU_ASSERT_EQUAL(r, 0);
CU_ASSERT_PTR_NULL(results);
/* add some more entries */
dkey.id = MSGID1;
dkey.to = FOLDER2;
dkey.date = DATE2;
duplicate_mark(&dkey, MARK2, UID2);
dkey.id = MSGID1;
dkey.to = FOLDER3;
dkey.date = DATE3;
duplicate_mark(&dkey, MARK3, UID3);
/* find returns all the entries in lexical order */
r = duplicate_find(MSGID1, finder, &results);
CU_ASSERT_EQUAL(r, 0);
GOTRESULT(MSGID1, FOLDER2, DATE2, MARK2, UID2);
GOTRESULT(MSGID1, FOLDER3, DATE3, MARK3, UID3);
GOTRESULT(MSGID1, FOLDER1, DATE1, MARK1, UID1);
CU_ASSERT_PTR_NULL(results);
/* find of another msgid returns nothing */
r = duplicate_find(MSGID2, finder, &results);
CU_ASSERT_EQUAL(r, 0);
CU_ASSERT_PTR_NULL(results);
/* add some entries for another msgid */
dkey.id = MSGID2;
dkey.to = FOLDER4;
dkey.date = DATE4;
duplicate_mark(&dkey, MARK4, UID4);
dkey.id = MSGID2;
dkey.to = FOLDER5;
dkey.date = DATE5;
duplicate_mark(&dkey, MARK5, UID5);
/* find returns all the entries in lexical order */
r = duplicate_find(MSGID1, finder, &results);
CU_ASSERT_EQUAL(r, 0);
GOTRESULT(MSGID1, FOLDER2, DATE2, MARK2, UID2);
GOTRESULT(MSGID1, FOLDER3, DATE3, MARK3, UID3);
GOTRESULT(MSGID1, FOLDER1, DATE1, MARK1, UID1);
CU_ASSERT_PTR_NULL(results);
/* find of another msgid all it's entries in lexical order */
r = duplicate_find(MSGID2, finder, &results);
CU_ASSERT_EQUAL(r, 0);
GOTRESULT(MSGID2, FOLDER5, DATE5, MARK5, UID5);
GOTRESULT(MSGID2, FOLDER4, DATE4, MARK4, UID4);
CU_ASSERT_PTR_NULL(results);
/* find with msgid="" returns all the entries for all msgids */
r = duplicate_find("", finder, &results);
CU_ASSERT_EQUAL(r, 0);
GOTRESULT(MSGID1, FOLDER2, DATE2, MARK2, UID2);
GOTRESULT(MSGID1, FOLDER3, DATE3, MARK3, UID3);
GOTRESULT(MSGID1, FOLDER1, DATE1, MARK1, UID1);
GOTRESULT(MSGID2, FOLDER5, DATE5, MARK5, UID5);
GOTRESULT(MSGID2, FOLDER4, DATE4, MARK4, UID4);
CU_ASSERT_PTR_NULL(results);
/* likewise msgid=NULL */
r = duplicate_find(NULL, finder, &results);
CU_ASSERT_EQUAL(r, 0);
GOTRESULT(MSGID1, FOLDER2, DATE2, MARK2, UID2);
GOTRESULT(MSGID1, FOLDER3, DATE3, MARK3, UID3);
GOTRESULT(MSGID1, FOLDER1, DATE1, MARK1, UID1);
GOTRESULT(MSGID2, FOLDER5, DATE5, MARK5, UID5);
GOTRESULT(MSGID2, FOLDER4, DATE4, MARK4, UID4);
CU_ASSERT_PTR_NULL(results);
}
static int set_up(void)
{
int r;
const char * const *d;
static const char * const dirs[] = {
DBDIR,
DBDIR"/db",
NULL
};
r = system("rm -rf " DBDIR);
if (r)
return r;
for (d = dirs ; *d ; d++) {
r = mkdir(*d, 0777);
if (r < 0) {
int e = errno;
perror(*d);
return e;
}
}
libcyrus_config_setstring(CYRUSOPT_CONFIG_DIR, DBDIR);
config_read_string(
"configdirectory: "DBDIR"/conf\n"
);
cyrusdb_init();
config_duplicate_db = "skiplist";
duplicate_init(0);
return 0;
}
static int tear_down(void)
{
int r;
duplicate_done();
cyrusdb_done();
config_duplicate_db = NULL;
config_reset();
r = system("rm -rf " DBDIR);
if (r) r = -1;
return r;
}
/* vim: set ft=c: */