aboutsummaryrefslogtreecommitdiffstats
path: root/camel/tests/message/test2.c
blob: 00bd6aabf20a7c832830c73bb81463c18c87b375 (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
#include "camel-test.h"
#include "messages.h"
#include "addresses.h"

/* for stat */
#include <sys/stat.h>
#include <unistd.h>

#include <unicode.h>

#include <camel/camel-internet-address.h>
#include <camel/camel-address.h>

#include "address-data.h"

static char *convert(const char *in, const char *from, const char *to)
{
    unicode_iconv_t ic = unicode_iconv_open(to, from);
    char *out, *outp;
    const char *inp;
    int inlen, outlen;

    if (ic == (unicode_iconv_t)-1)
        return g_strdup(in);

    inlen = strlen(in);
    outlen = inlen*5 + 16;

    outp = out = g_malloc(outlen);
    inp = in;

    if (unicode_iconv(ic, &inp, &inlen, &outp, &outlen) == -1) {
        test_free(out);
        unicode_iconv_close(ic);
        return g_strdup(in);
    }

    if (unicode_iconv(ic, NULL, 0, &outp, &outlen) == -1) {
        test_free(out);
        unicode_iconv_close(ic);
        return g_strdup(in);
    }

    unicode_iconv_close(ic);

    *outp = 0;

#if 0
    /* lets see if we can convert back again? */
    {
        char *nout, *noutp;
        unicode_iconv_t ic = unicode_iconv_open(from, to);

        inp = out;
        inlen = strlen(out);
        outlen = inlen*5 + 16;
        noutp = nout = g_malloc(outlen);
        if (unicode_iconv(ic, &inp, &inlen, &noutp, &outlen) == -1
            || unicode_iconv(ic, NULL, 0, &noutp, &outlen) == -1) {
            g_warning("Cannot convert '%s' \n from %s to %s: %s\n", in, to, from, strerror(errno));
        }
        unicode_iconv_close(ic);
    }

    /* and lets see what camel thinks out optimal charset is */
    {
        printf("Camel thinks the best encoding of '%s' is %s, although we converted from %s\n",
               in, camel_charset_best(out, strlen(out)), from);
    }
#endif

    return out;
}

#define to_utf8(in, type) convert(in, type, "utf-8")
#define from_utf8(in, type) convert(in, "utf-8", type)

#define ARRAY_LEN(x) (sizeof(x)/sizeof(x[0]))

int main(int argc, char **argv)
{
    int i;
    CamelInternetAddress *addr, *addr2;
    char *name;
    char *charset;
    const char *real, *where;
    char *enc, *enc2, *format, *format2;

    camel_test_init(argc, argv);

    camel_test_start("CamelInternetAddress, basics");

    addr = camel_internet_address_new();

    push("Test blank address");
    check(camel_address_length(CAMEL_ADDRESS(addr)) == 0);
    check(camel_internet_address_get(addr, 0, &real, &where) == FALSE);
    pull();

    push("Test blank clone");
    addr2 = CAMEL_INTERNET_ADDRESS(camel_address_new_clone(CAMEL_ADDRESS(addr)));
    test_address_compare(addr, addr2);
    check_unref(addr2, 1);
    pull();

    push("Test add 1");
    camel_internet_address_add(addr, "Zed", "nowhere@here.com.au");
    check(camel_address_length(CAMEL_ADDRESS(addr)) == 1);
    check(camel_internet_address_get(addr, 0, &real, &where) == TRUE);
    check_msg(string_equal("Zed", real), "real = '%s'", real);
    check(strcmp(where, "nowhere@here.com.au") == 0);
    pull();

    push("Test clone 1");
    addr2 = CAMEL_INTERNET_ADDRESS(camel_address_new_clone(CAMEL_ADDRESS(addr)));
    test_address_compare(addr, addr2);
    check_unref(addr2, 1);
    pull();

    push("Test add many");
    for (i=1;i<10;i++) {
        char name[16], a[32];
        sprintf(name, "Zed %d", i);
        sprintf(a, "nowhere@here-%d.com.au", i);
        camel_internet_address_add(addr, name, a);
        check(camel_address_length(CAMEL_ADDRESS(addr)) == i+1);
        check(camel_internet_address_get(addr, i, &real, &where) == TRUE);
        check_msg(string_equal(name, real), "name = '%s' real = '%s'", name, real);
        check(strcmp(where, a) == 0);
    }
    pull();

    /* put a few of these in to make it look like its doing something impressive ... :) */
    camel_test_end();
    camel_test_start("CamelInternetAddress, search");

    push("Test search");
    camel_test_nonfatal("Address comparisons should ignore whitespace??");
    check(camel_internet_address_find_name(addr, "Zed 1", &where) == 1);
    check(camel_internet_address_find_name(addr, "Zed 9", &where) == 9);
    check(camel_internet_address_find_name(addr, "Zed", &where) == 0);
    check(camel_internet_address_find_name(addr, " Zed", &where) == 0);
    check(camel_internet_address_find_name(addr, "Zed ", &where) == 0);
    check(camel_internet_address_find_name(addr, "  Zed ", &where) == 0);
    check(camel_internet_address_find_name(addr, "Zed 20", &where) == -1);
    check(camel_internet_address_find_name(addr, "", &where) == -1);
    /* interface dont handle nulls :) */
    /*check(camel_internet_address_find_name(addr, NULL, &where) == -1);*/

    check(camel_internet_address_find_address(addr, "nowhere@here-1.com.au", &where) == 1);
    check(camel_internet_address_find_address(addr, "nowhere@here-1 . com.au", &where) == 1);
    check(camel_internet_address_find_address(addr, "nowhere@here-2 .com.au ", &where) == 2);
    check(camel_internet_address_find_address(addr, " nowhere @here-3.com.au", &where) == 3);
    check(camel_internet_address_find_address(addr, "nowhere@here-20.com.au ", &where) == -1);
    check(camel_internet_address_find_address(addr, "", &where) == -1);
    /*check(camel_internet_address_find_address(addr, NULL, &where) == -1);*/
    camel_test_fatal();
    pull();

    camel_test_end();
    camel_test_start("CamelInternetAddress, copy/cat/clone");

    push("Test clone many");
    addr2 = CAMEL_INTERNET_ADDRESS(camel_address_new_clone(CAMEL_ADDRESS(addr)));
    test_address_compare(addr, addr2);
    pull();

    push("Test remove items");
    camel_address_remove(CAMEL_ADDRESS(addr2), 0);
    check(camel_address_length(CAMEL_ADDRESS(addr2)) == 9);
    camel_address_remove(CAMEL_ADDRESS(addr2), 0);
    check(camel_address_length(CAMEL_ADDRESS(addr2)) == 8);
    camel_address_remove(CAMEL_ADDRESS(addr2), 5);
    check(camel_address_length(CAMEL_ADDRESS(addr2)) == 7);
    camel_address_remove(CAMEL_ADDRESS(addr2), 10);
    check(camel_address_length(CAMEL_ADDRESS(addr2)) == 7);
    camel_address_remove(CAMEL_ADDRESS(addr2), -1);
    check(camel_address_length(CAMEL_ADDRESS(addr2)) == 0);
    check_unref(addr2, 1);
    pull();

    push("Testing copy/cat");
    push("clone + cat");
    addr2 = CAMEL_INTERNET_ADDRESS(camel_address_new_clone(CAMEL_ADDRESS(addr)));
    camel_address_cat(CAMEL_ADDRESS(addr2), CAMEL_ADDRESS(addr));
    check(camel_address_length(CAMEL_ADDRESS(addr)) == 10);
    check(camel_address_length(CAMEL_ADDRESS(addr2)) == 20);
    check_unref(addr2, 1);
    pull();

    push("cat + cat + copy");
    addr2 = camel_internet_address_new();
    camel_address_cat(CAMEL_ADDRESS(addr2), CAMEL_ADDRESS(addr));
    test_address_compare(addr, addr2);
    camel_address_cat(CAMEL_ADDRESS(addr2), CAMEL_ADDRESS(addr));
    check(camel_address_length(CAMEL_ADDRESS(addr)) == 10);
    check(camel_address_length(CAMEL_ADDRESS(addr2)) == 20);
    camel_address_copy(CAMEL_ADDRESS(addr2), CAMEL_ADDRESS(addr));
    test_address_compare(addr, addr2);
    check_unref(addr2, 1);
    pull();

    push("copy");
    addr2 = camel_internet_address_new();
    camel_address_copy(CAMEL_ADDRESS(addr2), CAMEL_ADDRESS(addr));
    test_address_compare(addr, addr2);
    check_unref(addr2, 1);
    pull();

    pull();

    check_unref(addr, 1);

    camel_test_end();

    camel_test_start("CamelInternetAddress, I18N");

    for (i=0;i<ARRAY_LEN(test_lines);i++) {
        push("Testing text line %d (%s) '%s'", i, test_lines[i].type, test_lines[i].line);

        addr = camel_internet_address_new();

        /* first, convert to api format (utf-8) */
        charset = test_lines[i].type;
        name = to_utf8(test_lines[i].line, charset);

        push("Address setup");
        camel_internet_address_add(addr, name, "nobody@nowhere.com");
        check(camel_internet_address_get(addr, 0, &real, &where) == TRUE);
        check_msg(string_equal(name, real), "name = '%s' real = '%s'", name, real);
        check(strcmp(where, "nobody@nowhere.com") == 0);

        check(camel_internet_address_get(addr, 1, &real, &where) == FALSE);
        check(camel_address_length(CAMEL_ADDRESS(addr)) == 1);
        pull();

        push("Address encode/decode");
        enc = camel_address_encode(CAMEL_ADDRESS(addr));

        addr2 = camel_internet_address_new();
        check(camel_address_decode(CAMEL_ADDRESS(addr2), enc) == 1);
        check(camel_address_length(CAMEL_ADDRESS(addr2)) == 1);

        enc2 = camel_address_encode(CAMEL_ADDRESS(addr2));
        check_msg(string_equal(enc, enc2), "enc = '%s' enc2 = '%s'", enc, enc2);
        test_free(enc2);

        push("Compare addresses");
        test_address_compare(addr, addr2);
        pull();
        test_free(enc);
        pull();

        /* FIXME: format/unformat arne't guaranteed to be reversible, at least at the moment */
        camel_test_nonfatal("format/unformat not (yet) reversible for all cases");

        push("Address format/unformat");
        format = camel_address_format(CAMEL_ADDRESS(addr));

        addr2 = camel_internet_address_new();
        check(camel_address_unformat(CAMEL_ADDRESS(addr2), format) == 1);
        check(camel_address_length(CAMEL_ADDRESS(addr2)) == 1);

        format2 = camel_address_format(CAMEL_ADDRESS(addr2));
        check_msg(string_equal(format, format2), "format = '%s\n\tformat2 = '%s'", format, format2);
        test_free(format2);

        /* currently format/unformat doesn't handle ,'s and other special chars at all */
        if (camel_address_length(CAMEL_ADDRESS(addr2)) == 1) {
            push("Compare addresses");
            test_address_compare(addr, addr2);
            pull();
        }

        test_free(format);
        pull();

        camel_test_fatal();

        check_unref(addr2, 1);

        check_unref(addr, 1);
        pull();

    }

    camel_test_end();

    camel_test_start("CamelInternetAddress, I18N decode");

    for (i=0;i<ARRAY_LEN(test_address);i++) {
        push("Testing address line %d '%s'", i, test_address[i].addr);

        addr = camel_internet_address_new();
        push("checking decoded");
        check(camel_address_decode(CAMEL_ADDRESS(addr), test_address[i].addr) == test_address[i].count);
        format = camel_address_format(CAMEL_ADDRESS(addr));
        check(strcmp(format, test_address[i].utf8) == 0);
        test_free(format);
        pull();
        
        push("Comparing re-encoded output");
        addr2 = CAMEL_INTERNET_ADDRESS(camel_internet_address_new());
        enc = camel_address_encode(CAMEL_ADDRESS(addr));
        check_msg(camel_address_decode(CAMEL_ADDRESS(addr2), enc) == test_address[i].count, "enc = '%s'", enc);
        test_free(enc);
        test_address_compare(addr, addr2);
        check_unref(addr2, 1);
        pull();

        check_unref(addr, 1);

        pull();
    }

    camel_test_end();

    /* FIXME: Add test of decoding of broken addresses */

    return 0;
}