aboutsummaryrefslogtreecommitdiffstats
path: root/camel/tests/lib/folders.c
blob: 738f8d2913b3a5dc5616176151f33fb9a4a77e9a (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

#include "camel-test.h"
#include "folders.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;
    const 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--;
    }
    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(info->subject, camel_mime_message_get_subject(msg)) == 0,
          "info->subject = '%s', get_subject() = '%s'", info->subject, 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;
    const 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(info->uid, uid) == 0);

    /* 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(info->uid, 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;
    const 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(info->uid, 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();
}