aboutsummaryrefslogtreecommitdiffstats
path: root/camel/tests/lib/folders.c
blob: ba02001bccfc1216c981f705a1e8dfb3144965aa (plain) (blame)
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
#include <string.h>

#include "camel-test.h"
#include "folders.h"
#include "messages.h"

#include "camel/camel-exception.h"

/* check the total/unread is what we think it should be */
void
test_folder_counts(CamelFolder *folder, int total, int unread)
{
    GPtrArray *s;
    int i, myunread;
    CamelMessageInfo *info;

    push("test folder counts %d total %d unread", total, unread);

    /* first, use the standard functions */
    check(camel_folder_get_message_count(folder) == total);
    check(camel_folder_get_unread_message_count(folder) == total);

    /* next, use the summary */
    s = camel_folder_get_summary(folder);
    check(s != NULL);
    check(s->len == total);
    myunread = s->len;
    for (i=0;i<s->len;i++) {
        info = s->pdata[i];
        if (info->flags & CAMEL_MESSAGE_SEEN)
            myunread--;
    }
    check(unread == myunread);
    camel_folder_free_summary(folder, s);

    /* last, use the uid list */
    s = camel_folder_get_uids(folder);
    check(s != NULL);
    check(s->len == total);
    myunread = s->len;
    for (i=0;i<s->len;i++) {
        info = camel_folder_get_message_info(folder, s->pdata[i]);
        if (info->flags & CAMEL_MESSAGE_SEEN)
            myunread--;
        camel_folder_free_message_info(folder, info);
    }
    check(unread == myunread);
    camel_folder_free_uids(folder, s);

    pull();
}

static int
safe_strcmp(const char *a, const char *b)
{
    if (a == NULL && b == NULL)
        return 0;
    if (a == NULL)
        return 1;
    if (b == NULL)
        return -1;
    return strcmp(a, b);
}

void
test_message_info(CamelMimeMessage *msg, const CamelMessageInfo *info)
{
    check_msg(safe_strcmp(camel_message_info_subject(info), camel_mime_message_get_subject(msg)) == 0,
          "info->subject = '%s', get_subject() = '%s'", camel_message_info_subject(info), camel_mime_message_get_subject(msg));

    /* FIXME: testing from/cc/to, etc is more tricky */

    check(info->date_sent == camel_mime_message_get_date(msg, NULL));

    /* date received isn't set for messages that haven't been sent anywhere ... */
    /*check(info->date_received == camel_mime_message_get_date_received(msg, NULL));*/

    /* so is messageid/references, etc */
}

/* check a message is present */
void
test_folder_message(CamelFolder *folder, const char *uid)
{
    CamelMimeMessage *msg;
    CamelMessageInfo *info;
    GPtrArray *s;
    int i;
    CamelException *ex = camel_exception_new();
    int found;

    push("uid %s is in folder", uid);

    /* first try getting info */
    info = camel_folder_get_message_info(folder, uid);
    check(info != NULL);
    check(strcmp(camel_message_info_uid(info), uid) == 0);
    camel_folder_free_message_info(folder, info);

    /* then, getting message */
    msg = camel_folder_get_message(folder, uid, ex);
    check_msg(!camel_exception_is_set(ex), "%s", camel_exception_get_description(ex));
    check(msg != NULL);

    /* cross check with info */
    test_message_info(msg, info);

    camel_object_unref((CamelObject *)msg);

    /* see if it is in the summary (only once) */
    s = camel_folder_get_summary(folder);
    check(s != NULL);
    found = 0;
    for (i=0;i<s->len;i++) {
        info = s->pdata[i];
        if (strcmp(camel_message_info_uid(info), uid) == 0)
            found++;
    }
    check(found == 1);
    camel_folder_free_summary(folder, s);

    /* check it is in the uid list */
    s = camel_folder_get_uids(folder);
    check(s != NULL);
    found = 0;
    for (i=0;i<s->len;i++) {
        if (strcmp(s->pdata[i], uid) == 0)
            found++;
    }
    check(found == 1);
    camel_folder_free_uids(folder, s);

    camel_exception_free(ex);

    pull();
}

/* check message not present */
void
test_folder_not_message(CamelFolder *folder, const char *uid)
{
    CamelMimeMessage *msg;
    CamelMessageInfo *info;
    GPtrArray *s;
    int i;
    CamelException *ex = camel_exception_new();
    int found;

    push("uid %s is not in folder", uid);

    /* first try getting info */
    info = camel_folder_get_message_info(folder, uid);
    check(info == NULL);

    /* then, getting message */
    msg = camel_folder_get_message(folder, uid, ex);
    check(camel_exception_is_set(ex));
    check(msg == NULL);
    camel_exception_clear(ex);

    /* see if it is not in the summary (only once) */
    s = camel_folder_get_summary(folder);
    check(s != NULL);
    found = 0;
    for (i=0;i<s->len;i++) {
        info = s->pdata[i];
        if (strcmp(camel_message_info_uid(info), uid) == 0)
            found++;
    }
    check(found == 0);
    camel_folder_free_summary(folder, s);

    /* check it is not in the uid list */
    s = camel_folder_get_uids(folder);
    check(s != NULL);
    found = 0;
    for (i=0;i<s->len;i++) {
        if (strcmp(s->pdata[i], uid) == 0)
            found++;
    }
    check(found == 0);
    camel_folder_free_uids(folder, s);

    camel_exception_free(ex);

    pull();
}

/* test basic store operations on folders */
/* TODO: Add subscription stuff */
void
test_folder_basic(CamelSession *session, const char *storename, int local)
{
    CamelStore *store;
    CamelException *ex = camel_exception_new();
    CamelFolder *folder, *root;
    char *what = g_strdup_printf("testing store: %s", storename);

    camel_test_start(what);
    test_free(what);

    push("getting store");
    store = camel_session_get_store(session, storename, ex);
    check_msg(!camel_exception_is_set(ex), "getting store: %s", camel_exception_get_description(ex));
    check(store != NULL);
    pull();

    /* local providers == no root folder */
    push("getting root folder");
    root = camel_store_get_root_folder(store, ex);
    if (local) {
        check(camel_exception_is_set(ex));
        check(root == NULL);
        camel_exception_clear(ex);
    } else {
        check_msg(!camel_exception_is_set(ex), "%s", camel_exception_get_description(ex));
        check(root != NULL);
        check_unref(root, 1);
    }
    pull();

    /* same for default folder */
    push("getting default folder");
    root = camel_store_get_root_folder(store, ex);
    if (local) {
        check(camel_exception_is_set(ex));
        check(root == NULL);
        camel_exception_clear(ex);
    } else {
        check_msg(!camel_exception_is_set(ex), "%s", camel_exception_get_description(ex));
        check(root != NULL);
        check_unref(root, 1);
    }
    pull();

    push("getting a non-existant folder, no create");
    folder = camel_store_get_folder(store, "unknown", 0, ex);
    check(camel_exception_is_set(ex));
    check(folder == NULL);
    camel_exception_clear(ex);
    pull();

    push("getting a non-existant folder, with create");
    folder = camel_store_get_folder(store, "testbox", CAMEL_STORE_FOLDER_CREATE, ex);
    check_msg(!camel_exception_is_set(ex), "%s", camel_exception_get_description(ex));
    check(folder != NULL);
    check_unref(folder, 1);
    pull();

    push("getting an existing folder");
    folder = camel_store_get_folder(store, "testbox", 0, ex);
    check_msg(!camel_exception_is_set(ex), "%s", camel_exception_get_description(ex));
    check(folder != NULL);
    check_unref(folder, 1);
    pull();

    push("renaming a non-existant folder");
    camel_store_rename_folder(store, "unknown1", "unknown2", ex);
    check(camel_exception_is_set(ex));
    camel_exception_clear(ex);
    pull();

    push("renaming an existing folder");
    camel_store_rename_folder(store, "testbox", "testbox2", ex);
    check_msg(!camel_exception_is_set(ex), "%s", camel_exception_get_description(ex));
    pull();

    push("opening the old name of a renamed folder");
    folder = camel_store_get_folder(store, "testbox", 0, ex);
    check(camel_exception_is_set(ex));
    check(folder == NULL);
    camel_exception_clear(ex);
    pull();

    push("opening the new name of a renamed folder");
    folder = camel_store_get_folder(store, "testbox2", 0, ex);
    check_msg(!camel_exception_is_set(ex), "%s", camel_exception_get_description(ex));
    check(folder != NULL);
    check_unref(folder, 1);
    pull();

    push("deleting a non-existant folder");
    camel_store_delete_folder(store, "unknown", ex);
    check(camel_exception_is_set(ex));
    camel_exception_clear(ex);
    pull();

    push("deleting an existing folder");
    camel_store_delete_folder(store, "testbox2", ex);
    check_msg(!camel_exception_is_set(ex), "%s", camel_exception_get_description(ex));
    pull();

    push("opening a folder that has been deleted");
    folder = camel_store_get_folder(store, "testbox2", 0, ex);
    check(camel_exception_is_set(ex));
    check(folder == NULL);
    camel_exception_clear(ex);
    pull();

    check_unref(store, 1);

    camel_test_end();

    camel_exception_free(ex);
}


/* todo: cross-check everything with folder_info checks as well */
/* this should probably take a folder instead of a session ... */
void
test_folder_message_ops(CamelSession *session, const char *name, int local)
{
    CamelStore *store;
    CamelException *ex = camel_exception_new();
    CamelFolder *folder;
    CamelMimeMessage *msg;
    int j;
    int indexed, max;
    GPtrArray *uids;
    CamelMessageInfo *info;

    max=local?2:1;

    for (indexed = 0;indexed<max;indexed++) {
        char *what = g_strdup_printf("folder ops: %s %s", name, local?(indexed?"indexed":"non-indexed"):"");
        int flags;

        camel_test_start(what);
        test_free(what);

        push("getting store");
        store = camel_session_get_store(session, name, ex);
        check_msg(!camel_exception_is_set(ex), "getting store: %s", camel_exception_get_description(ex));
        check(store != NULL);
        pull();

        push("creating %sindexed folder", indexed?"":"non-");
        if (indexed)
            flags = CAMEL_STORE_FOLDER_CREATE|CAMEL_STORE_FOLDER_BODY_INDEX;
        else
            flags = CAMEL_STORE_FOLDER_CREATE;
        folder = camel_store_get_folder(store, "testbox", flags, ex);
        check_msg(!camel_exception_is_set(ex), "%s", camel_exception_get_description(ex));
        check(folder != NULL);

        /* verify empty/can't get nonexistant stuff */
        test_folder_counts(folder, 0, 0);
        test_folder_not_message(folder, "0");
        test_folder_not_message(folder, "");

        for (j=0;j<10;j++) {
            char *content, *subject;

            push("creating test message");
            msg = test_message_create_simple();
            content = g_strdup_printf("Test message %d contents\n\n", j);
            test_message_set_content_simple((CamelMimePart *)msg, 0, "text/plain",
                            content, strlen(content));
            test_free(content);
            subject = g_strdup_printf("Test message %d", j);
            camel_mime_message_set_subject(msg, subject);
            pull();

            push("appending simple message %d", j);
            camel_folder_append_message(folder, msg, NULL, ex);
            check_msg(!camel_exception_is_set(ex), "%s", camel_exception_get_description(ex));

            /* sigh, this shouldn't be required, but the imap code is too dumb to do it itself */
            if (!local) {
                push("forcing a refresh of folder updates");
                camel_folder_refresh_info(folder, ex);
                check_msg(!camel_exception_is_set(ex), "%s", camel_exception_get_description(ex));
                pull();
            }

            if (!local)
                camel_test_nonfatal("unread counts dont seem right for imap");

            test_folder_counts(folder, j+1, j+1);

            if (!local)
                camel_test_fatal();

            push("checking it is in the right uid slot & exists");
            uids = camel_folder_get_uids(folder);
            check(uids != NULL);
            check(uids->len == j+1);
            test_folder_message(folder, uids->pdata[j]);
            pull();

            push("checking it is the right message (subject): %s", subject);
            info = camel_folder_get_message_info(folder, uids->pdata[j]);
            check_msg(strcmp(camel_message_info_subject(info), subject)==0,
                  "info->subject %s", camel_message_info_subject(info));
            camel_folder_free_uids(folder, uids);
            camel_folder_free_message_info(folder, info);
            pull();

            test_free(subject);

            check_unref(msg, 1);
            pull();
        }

        check_unref(folder, 1);
        pull();

        push("deleting test folder, with messages in it");
        camel_store_delete_folder(store, "testbox", ex);
        check(camel_exception_is_set(ex));
        camel_exception_clear(ex);
        pull();

        push("re-opening folder");
        folder = camel_store_get_folder(store, "testbox", flags, ex);
        check_msg(!camel_exception_is_set(ex), "%s", camel_exception_get_description(ex));
        check(folder != NULL);

            /* verify counts */
        test_folder_counts(folder, 10, 10);

        /* re-check uid's, after a reload */
        uids = camel_folder_get_uids(folder);
        check(uids != NULL);
        check(uids->len == 10);
        for (j=0;j<10;j++) {
            char *subject = g_strdup_printf("Test message %d", j);

            push("verify reload of %s", subject);
            test_folder_message(folder, uids->pdata[j]);

            info = camel_folder_get_message_info(folder, uids->pdata[j]);
            check_msg(strcmp(camel_message_info_subject(info), subject)==0,
                  "info->subject %s", camel_message_info_subject(info));
            test_free(subject);
            camel_folder_free_message_info(folder, info);
            pull();
        }

        push("deleting first message & expunging");
        camel_folder_delete_message(folder, uids->pdata[0]);
        test_folder_counts(folder, 10, 10);
        camel_folder_expunge(folder, ex);
        check_msg(!camel_exception_is_set(ex), "%s", camel_exception_get_description(ex));
        test_folder_not_message(folder, uids->pdata[0]);
        test_folder_counts(folder, 9, 9);

        camel_folder_free_uids(folder, uids);

        uids = camel_folder_get_uids(folder);
        check(uids != NULL);
        check(uids->len == 9);
        for (j=0;j<9;j++) {
            char *subject = g_strdup_printf("Test message %d", j+1);

            push("verify after expunge of %s", subject);
            test_folder_message(folder, uids->pdata[j]);

            info = camel_folder_get_message_info(folder, uids->pdata[j]);
            check_msg(strcmp(camel_message_info_subject(info), subject)==0,
                  "info->subject %s", camel_message_info_subject(info));
            test_free(subject);
            camel_folder_free_message_info(folder, info);
            pull();
        }
        pull();

        push("deleting last message & expunging");
        camel_folder_delete_message(folder, uids->pdata[8]);
        /* sync? */
        test_folder_counts(folder, 9, 9);
        camel_folder_expunge(folder, ex);
        check_msg(!camel_exception_is_set(ex), "%s", camel_exception_get_description(ex));
        test_folder_not_message(folder, uids->pdata[8]);
        test_folder_counts(folder, 8, 8);

        camel_folder_free_uids(folder, uids);

        uids = camel_folder_get_uids(folder);
        check(uids != NULL);
        check(uids->len == 8);
        for (j=0;j<8;j++) {
            char *subject = g_strdup_printf("Test message %d", j+1);

            push("verify after expunge of %s", subject);
            test_folder_message(folder, uids->pdata[j]);

            info = camel_folder_get_message_info(folder, uids->pdata[j]);
            check_msg(strcmp(camel_message_info_subject(info), subject)==0,
                  "info->subject %s", camel_message_info_subject(info));
            test_free(subject);
            camel_folder_free_message_info(folder, info);
            pull();
        }
        pull();

        push("deleting all messages & expunging");
        for (j=0;j<8;j++) {
            camel_folder_delete_message(folder, uids->pdata[j]);
        }
        /* sync? */
        test_folder_counts(folder, 8, 8);
        camel_folder_expunge(folder, ex);
        check_msg(!camel_exception_is_set(ex), "%s", camel_exception_get_description(ex));
        for (j=0;j<8;j++) {
            test_folder_not_message(folder, uids->pdata[j]);
        }
        test_folder_counts(folder, 0, 0);

        camel_folder_free_uids(folder, uids);
        pull();

        check_unref(folder, 1);
        pull(); /* re-opening folder */

        push("deleting test folder, with no messages in it");
        camel_store_delete_folder(store, "testbox", ex);
        check_msg(!camel_exception_is_set(ex), "%s", camel_exception_get_description(ex));
        pull();

        check_unref(store, 1);
        camel_test_end();
    }

    camel_exception_free(ex);
}