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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
|
/* Copyright (c) 2007-2018 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "str.h"
#include "message-address.h"
#include "test-common.h"
enum test_message_address {
TEST_MESSAGE_ADDRESS_FLAG_SKIP_LIST = BIT(0),
};
static bool cmp_addr(const struct message_address *a1,
const struct message_address *a2)
{
return null_strcmp(a1->name, a2->name) == 0 &&
null_strcmp(a1->route, a2->route) == 0 &&
null_strcmp(a1->mailbox, a2->mailbox) == 0 &&
null_strcmp(a1->domain, a2->domain) == 0 &&
a1->invalid_syntax == a2->invalid_syntax;
}
static void
test_parse_address_full(const char *input, bool fill_missing,
struct message_address_list *list_r)
{
const enum message_address_parse_flags flags =
fill_missing ? MESSAGE_ADDRESS_PARSE_FLAG_FILL_MISSING : 0;
/* duplicate the input (without trailing NUL) so valgrind notices
if there's any out-of-bounds access */
size_t input_len = strlen(input);
unsigned char *input_dup = i_memdup(input, input_len);
message_address_parse_full(pool_datastack_create(),
input_dup, input_len, UINT_MAX, flags,
list_r);
i_free(input_dup);
}
static const struct message_address *
test_parse_address(const char *input, bool fill_missing)
{
struct message_address_list list;
test_parse_address_full(input, fill_missing, &list);
return list.head;
}
static void test_message_address(void)
{
static const struct test {
const char *input;
const char *wanted_output;
const char *wanted_filled_output;
struct message_address addr;
struct message_address filled_addr;
enum test_message_address flags;
} tests[] = {
/* user@domain -> <user@domain> */
{ "user@domain", "<user@domain>", NULL,
{ NULL, NULL, NULL, NULL, "user", "domain", FALSE },
{ NULL, NULL, NULL, NULL, "user", "domain", FALSE }, 0 },
{ "\"user\"@domain", "<user@domain>", NULL,
{ NULL, NULL, NULL, NULL, "user", "domain", FALSE },
{ NULL, NULL, NULL, NULL, "user", "domain", FALSE }, 0 },
{ "\"user name\"@domain", "<\"user name\"@domain>", NULL,
{ NULL, NULL, NULL, NULL, "user name", "domain", FALSE },
{ NULL, NULL, NULL, NULL, "user name", "domain", FALSE }, 0 },
{ "\"user@na\\\\me\"@domain", "<\"user@na\\\\me\"@domain>", NULL,
{ NULL, NULL, NULL, NULL, "user@na\\me", "domain", FALSE },
{ NULL, NULL, NULL, NULL, "user@na\\me", "domain", FALSE }, 0 },
{ "\"user\\\"name\"@domain", "<\"user\\\"name\"@domain>", NULL,
{ NULL, NULL, NULL, NULL, "user\"name", "domain", FALSE },
{ NULL, NULL, NULL, NULL, "user\"name", "domain", FALSE }, 0 },
{ "\"\"@domain", "<\"\"@domain>", NULL,
{ NULL, NULL, NULL, NULL, "", "domain", FALSE },
{ NULL, NULL, NULL, NULL, "", "domain", FALSE }, 0 },
{ "user", "<user>", "<user@MISSING_DOMAIN>",
{ NULL, NULL, NULL, NULL, "user", "", TRUE },
{ NULL, NULL, NULL, NULL, "user", "MISSING_DOMAIN", TRUE }, 0 },
{ "@domain", "<\"\"@domain>", "<MISSING_MAILBOX@domain>",
{ NULL, NULL, NULL, NULL, "", "domain", TRUE },
{ NULL, NULL, NULL, NULL, "MISSING_MAILBOX", "domain", TRUE }, 0 },
/* Display Name -> Display Name */
{ "Display Name", "\"Display Name\"", "\"Display Name\" <MISSING_MAILBOX@MISSING_DOMAIN>",
{ NULL, NULL, "Display Name", NULL, "", "", TRUE },
{ NULL, NULL, "Display Name", NULL, "MISSING_MAILBOX", "MISSING_DOMAIN", TRUE }, 0 },
{ "\"Display Name\"", "\"Display Name\"", "\"Display Name\" <MISSING_MAILBOX@MISSING_DOMAIN>",
{ NULL, NULL, "Display Name", NULL, "", "", TRUE },
{ NULL, NULL, "Display Name", NULL, "MISSING_MAILBOX", "MISSING_DOMAIN", TRUE }, 0 },
{ "Display \"Name\"", "\"Display Name\"", "\"Display Name\" <MISSING_MAILBOX@MISSING_DOMAIN>",
{ NULL, NULL, "Display Name", NULL, "", "", TRUE },
{ NULL, NULL, "Display Name", NULL, "MISSING_MAILBOX", "MISSING_DOMAIN", TRUE }, 0 },
{ "\"Display\" \"Name\"", "\"Display Name\"", "\"Display Name\" <MISSING_MAILBOX@MISSING_DOMAIN>",
{ NULL, NULL, "Display Name", NULL, "", "", TRUE },
{ NULL, NULL, "Display Name", NULL, "MISSING_MAILBOX", "MISSING_DOMAIN", TRUE }, 0 },
{ "\"\"", "", "<MISSING_MAILBOX@MISSING_DOMAIN>",
{ NULL, NULL, "", NULL, "", "", TRUE },
{ NULL, NULL, "", NULL, "MISSING_MAILBOX", "MISSING_DOMAIN", TRUE }, 0 },
/* <user@domain> -> <user@domain> */
{ "<user@domain>", NULL, NULL,
{ NULL, NULL, NULL, NULL, "user", "domain", FALSE },
{ NULL, NULL, NULL, NULL, "user", "domain", FALSE }, 0 },
{ "<\"user\"@domain>", "<user@domain>", NULL,
{ NULL, NULL, NULL, NULL, "user", "domain", FALSE },
{ NULL, NULL, NULL, NULL, "user", "domain", FALSE }, 0 },
{ "<\"user name\"@domain>", NULL, NULL,
{ NULL, NULL, NULL, NULL, "user name", "domain", FALSE },
{ NULL, NULL, NULL, NULL, "user name", "domain", FALSE }, 0 },
{ "<\"user@na\\\\me\"@domain>", NULL, NULL,
{ NULL, NULL, NULL, NULL, "user@na\\me", "domain", FALSE },
{ NULL, NULL, NULL, NULL, "user@na\\me", "domain", FALSE }, 0 },
{ "<\"user\\\"name\"@domain>", NULL, NULL,
{ NULL, NULL, NULL, NULL, "user\"name", "domain", FALSE },
{ NULL, NULL, NULL, NULL, "user\"name", "domain", FALSE }, 0 },
{ "<\"\"@domain>", NULL, NULL,
{ NULL, NULL, NULL, NULL, "", "domain", FALSE },
{ NULL, NULL, NULL, NULL, "", "domain", FALSE }, 0 },
{ "<user>", NULL, "<user@MISSING_DOMAIN>",
{ NULL, NULL, NULL, NULL, "user", "", TRUE },
{ NULL, NULL, NULL, NULL, "user", "MISSING_DOMAIN", TRUE }, 0 },
{ "<@route>", "<@route:\"\">", "<INVALID_ROUTE:MISSING_MAILBOX@MISSING_DOMAIN>",
{ NULL, NULL, NULL, "@route", "", "", TRUE },
{ NULL, NULL, NULL, "INVALID_ROUTE", "MISSING_MAILBOX", "MISSING_DOMAIN", TRUE }, 0 },
/* user@domain (Display Name) -> "Display Name" <user@domain> */
{ "user@domain (DisplayName)", "DisplayName <user@domain>", NULL,
{ NULL, NULL, "DisplayName", NULL, "user", "domain", FALSE },
{ NULL, NULL, "DisplayName", NULL, "user", "domain", FALSE }, 0 },
{ "user@domain (Display Name)", "\"Display Name\" <user@domain>", NULL,
{ NULL, NULL, "Display Name", NULL, "user", "domain", FALSE },
{ NULL, NULL, "Display Name", NULL, "user", "domain", FALSE }, 0 },
{ "user@domain (Display\"Name)", "\"Display\\\"Name\" <user@domain>", NULL,
{ NULL, NULL, "Display\"Name", NULL, "user", "domain", FALSE },
{ NULL, NULL, "Display\"Name", NULL, "user", "domain", FALSE }, 0 },
{ "user (Display Name)", "\"Display Name\" <user>", "\"Display Name\" <user@MISSING_DOMAIN>",
{ NULL, NULL, "Display Name", NULL, "user", "", TRUE },
{ NULL, NULL, "Display Name", NULL, "user", "MISSING_DOMAIN", TRUE }, 0 },
{ "@domain (Display Name)", "\"Display Name\" <\"\"@domain>", "\"Display Name\" <MISSING_MAILBOX@domain>",
{ NULL, NULL, "Display Name", NULL, "", "domain", TRUE },
{ NULL, NULL, "Display Name", NULL, "MISSING_MAILBOX", "domain", TRUE }, 0 },
{ "user@domain ()", "<user@domain>", NULL,
{ NULL, NULL, NULL, NULL, "user", "domain", FALSE },
{ NULL, NULL, NULL, NULL, "user", "domain", FALSE }, 0 },
/* Display Name <user@domain> -> "Display Name" <user@domain> */
{ "DisplayName <user@domain>", NULL, NULL,
{ NULL, NULL, "DisplayName", NULL, "user", "domain", FALSE },
{ NULL, NULL, "DisplayName", NULL, "user", "domain", FALSE }, 0 },
{ "Display Name <user@domain>", "\"Display Name\" <user@domain>", NULL,
{ NULL, NULL, "Display Name", NULL, "user", "domain", FALSE },
{ NULL, NULL, "Display Name", NULL, "user", "domain", FALSE }, 0 },
{ "\"Display Name\" <user@domain>", NULL, NULL,
{ NULL, NULL, "Display Name", NULL, "user", "domain", FALSE },
{ NULL, NULL, "Display Name", NULL, "user", "domain", FALSE }, 0 },
{ "\"Display\\\"Name\" <user@domain>", NULL, NULL,
{ NULL, NULL, "Display\"Name", NULL, "user", "domain", FALSE },
{ NULL, NULL, "Display\"Name", NULL, "user", "domain", FALSE }, 0 },
{ "Display Name <user>", "\"Display Name\" <user>", "\"Display Name\" <user@MISSING_DOMAIN>",
{ NULL, NULL, "Display Name", NULL, "user", "", TRUE },
{ NULL, NULL, "Display Name", NULL, "user", "MISSING_DOMAIN", TRUE }, 0 },
{ "\"\" <user@domain>", "<user@domain>", NULL,
{ NULL, NULL, NULL, NULL, "user", "domain", FALSE },
{ NULL, NULL, NULL, NULL, "user", "domain", FALSE }, 0 },
/* <@route:user@domain> -> <@route:user@domain> */
{ "<@route:user@domain>", NULL, NULL,
{ NULL, NULL, NULL, "@route", "user", "domain", FALSE },
{ NULL, NULL, NULL, "@route", "user", "domain", FALSE }, 0 },
{ "<@route,@route2:user@domain>", NULL, NULL,
{ NULL, NULL, NULL, "@route,@route2", "user", "domain", FALSE },
{ NULL, NULL, NULL, "@route,@route2", "user", "domain", FALSE }, 0 },
{ "<@route@route2:user@domain>", "<@route,@route2:user@domain>", NULL,
{ NULL, NULL, NULL, "@route,@route2", "user", "domain", FALSE },
{ NULL, NULL, NULL, "@route,@route2", "user", "domain", FALSE }, 0 },
{ "<@route@route2:user>", "<@route,@route2:user>", "<@route,@route2:user@MISSING_DOMAIN>",
{ NULL, NULL, NULL, "@route,@route2", "user", "", TRUE },
{ NULL, NULL, NULL, "@route,@route2", "user", "MISSING_DOMAIN", TRUE }, 0 },
{ "<@route@route2:\"\"@domain>", "<@route,@route2:\"\"@domain>", NULL,
{ NULL, NULL, NULL, "@route,@route2", "", "domain", FALSE },
{ NULL, NULL, NULL, "@route,@route2", "", "domain", FALSE }, 0 },
/* Display Name <@route:user@domain> ->
"Display Name" <@route:user@domain> */
{ "Display Name <@route:user@domain>", "\"Display Name\" <@route:user@domain>", NULL,
{ NULL, NULL, "Display Name", "@route", "user", "domain", FALSE },
{ NULL, NULL, "Display Name", "@route", "user", "domain", FALSE }, 0 },
{ "Display Name <@route,@route2:user@domain>", "\"Display Name\" <@route,@route2:user@domain>", NULL,
{ NULL, NULL, "Display Name", "@route,@route2", "user", "domain", FALSE },
{ NULL, NULL, "Display Name", "@route,@route2", "user", "domain", FALSE }, 0 },
{ "Display Name <@route@route2:user@domain>", "\"Display Name\" <@route,@route2:user@domain>", NULL,
{ NULL, NULL, "Display Name", "@route,@route2", "user", "domain", FALSE },
{ NULL, NULL, "Display Name", "@route,@route2", "user", "domain", FALSE }, 0 },
{ "Display Name <@route@route2:user>", "\"Display Name\" <@route,@route2:user>", "\"Display Name\" <@route,@route2:user@MISSING_DOMAIN>",
{ NULL, NULL, "Display Name", "@route,@route2", "user", "", TRUE },
{ NULL, NULL, "Display Name", "@route,@route2", "user", "MISSING_DOMAIN", TRUE }, 0 },
{ "Display Name <@route@route2:\"\"@domain>", "\"Display Name\" <@route,@route2:\"\"@domain>", NULL,
{ NULL, NULL, "Display Name", "@route,@route2", "", "domain", FALSE },
{ NULL, NULL, "Display Name", "@route,@route2", "", "domain", FALSE }, 0 },
/* other tests: */
{ "\"foo: <a@b>;,\" <user@domain>", NULL, NULL,
{ NULL, NULL, "foo: <a@b>;,", NULL, "user", "domain", FALSE },
{ NULL, NULL, "foo: <a@b>;,", NULL, "user", "domain", FALSE }, 0 },
{ "<>", "", "<MISSING_MAILBOX@MISSING_DOMAIN>",
{ NULL, NULL, NULL, NULL, "", "", TRUE },
{ NULL, NULL, NULL, NULL, "MISSING_MAILBOX", "MISSING_DOMAIN", TRUE }, 0 },
{ "<@>", "", "<INVALID_ROUTE:MISSING_MAILBOX@MISSING_DOMAIN>",
{ NULL, NULL, NULL, NULL, "", "", TRUE },
{ NULL, NULL, NULL, "INVALID_ROUTE", "MISSING_MAILBOX", "MISSING_DOMAIN", TRUE }, 0 },
/* Test against a out-of-bounds read bug - keep these two tests
together in this same order: */
{ "aaaa@", "<aaaa>", "<aaaa@MISSING_DOMAIN>",
{ NULL, NULL, NULL, NULL, "aaaa", "", TRUE },
{ NULL, NULL, NULL, NULL, "aaaa", "MISSING_DOMAIN", TRUE }, 0 },
{ "a(aa", "", "<MISSING_MAILBOX@MISSING_DOMAIN>",
{ NULL, NULL, NULL, NULL, "", "", TRUE },
{ NULL, NULL, NULL, NULL, "MISSING_MAILBOX", "MISSING_DOMAIN", TRUE },
TEST_MESSAGE_ADDRESS_FLAG_SKIP_LIST },
};
static struct message_address group_prefix = {
NULL, NULL, NULL, NULL, "group", NULL, FALSE
};
static struct message_address group_suffix = {
NULL, NULL, NULL, NULL, NULL, NULL, FALSE
};
const struct message_address *addr;
string_t *str, *group;
const char *wanted_string;
unsigned int i;
test_begin("message address parsing");
str = t_str_new(128);
group = t_str_new(256);
for (i = 0; i < N_ELEMENTS(tests)*2; i++) {
const struct test *test = &tests[i/2];
const struct message_address *test_wanted_addr;
bool fill_missing = i%2 != 0;
test_wanted_addr = !fill_missing ?
&test->addr : &test->filled_addr;
addr = test_parse_address(test->input, fill_missing);
test_assert_idx(addr != NULL && addr->next == NULL &&
cmp_addr(addr, test_wanted_addr), i);
/* test the address alone */
str_truncate(str, 0);
message_address_write(str, addr);
if (fill_missing && test->wanted_filled_output != NULL)
wanted_string = test->wanted_filled_output;
else if (test->wanted_output != NULL)
wanted_string = test->wanted_output;
else
wanted_string = test->input;
test_assert_idx(strcmp(str_c(str), wanted_string) == 0, i);
if ((test->flags & TEST_MESSAGE_ADDRESS_FLAG_SKIP_LIST) != 0)
continue;
/* test the address as a list of itself */
for (unsigned int list_length = 2; list_length <= 5; list_length++) {
str_truncate(group, 0);
str_append(group, test->input);
for (unsigned int j = 1; j < list_length; j++) {
if ((j % 2) == 0)
str_append(group, ",");
else
str_append(group, " , \n ");
str_append(group, test->input);
}
addr = test_parse_address(str_c(group), fill_missing);
for (unsigned int j = 0; j < list_length; j++) {
test_assert_idx(addr != NULL &&
cmp_addr(addr, test_wanted_addr), i);
if (addr != NULL)
addr = addr->next;
}
test_assert_idx(addr == NULL, i);
}
/* test the address as a group of itself */
for (unsigned int list_length = 1; list_length <= 5; list_length++) {
str_truncate(group, 0);
str_printfa(group, "group: %s", test->input);
for (unsigned int j = 1; j < list_length; j++) {
if ((j % 2) == 0)
str_append(group, ",");
else
str_append(group, " , \n ");
str_append(group, test->input);
}
str_append_c(group, ';');
addr = test_parse_address(str_c(group), fill_missing);
test_assert(addr != NULL && cmp_addr(addr, &group_prefix));
addr = addr->next;
for (unsigned int j = 0; j < list_length; j++) {
test_assert_idx(addr != NULL &&
cmp_addr(addr, test_wanted_addr), i);
if (addr != NULL)
addr = addr->next;
}
test_assert_idx(addr != NULL && addr->next == NULL &&
cmp_addr(addr, &group_suffix), i);
}
}
test_end();
test_begin("message address parsing with empty group");
str_truncate(group, 0);
str_append(group, "group:;");
addr = test_parse_address(str_c(group), FALSE);
str_truncate(str, 0);
message_address_write(str, addr);
test_assert(addr != NULL && cmp_addr(addr, &group_prefix));
addr = addr->next;
test_assert(addr != NULL && addr->next == NULL &&
cmp_addr(addr, &group_suffix));
test_assert(strcmp(str_c(str), "group:;") == 0);
test_end();
test_begin("message address parsing empty string");
test_assert(message_address_parse(unsafe_data_stack_pool, &uchar_nul, 0, 10,
MESSAGE_ADDRESS_PARSE_FLAG_FILL_MISSING) == NULL);
str_truncate(str, 0);
message_address_write(str, NULL);
test_assert(str_len(str) == 0);
test_end();
}
static void test_message_address_list(void)
{
test_begin("message address list");
const char *test_input =
"user1@example1.com, user2@example2.com, user3@example3.com";
const struct message_address wanted_addrs[] = {
{ NULL, NULL, NULL, NULL, "user1", "example1.com", FALSE },
{ NULL, NULL, NULL, NULL, "user2", "example2.com", FALSE },
{ NULL, NULL, NULL, NULL, "user3", "example3.com", FALSE },
};
struct message_address_list list;
struct message_address *addr, *scanned_last_addr;
test_parse_address_full(test_input, FALSE, &list);
addr = list.head;
for (unsigned int i = 0; i < N_ELEMENTS(wanted_addrs); i++) {
test_assert_idx(cmp_addr(addr, &wanted_addrs[i]), i);
scanned_last_addr = addr;
addr = addr->next;
}
test_assert(list.tail == scanned_last_addr);
test_assert(addr == NULL);
test_end();
}
static void test_message_address_nuls(void)
{
const unsigned char input[] =
"\"user\0nuls\\\0-esc\"@[domain\0nuls\\\0-esc] (comment\0nuls\\\0-esc)";
const struct message_address output = {
NULL, NULL, "comment\xEF\xBF\xBDnuls\\\xEF\xBF\xBD-esc", NULL,
"user\xEF\xBF\xBDnuls\\\xEF\xBF\xBD-esc",
"[domain\xEF\xBF\xBDnuls\\\xEF\xBF\xBD-esc]", FALSE
};
const struct message_address *addr;
test_begin("message address parsing with NULs");
addr = message_address_parse(pool_datastack_create(),
input, sizeof(input)-1, UINT_MAX, 0);
test_assert(addr != NULL && cmp_addr(addr, &output));
test_end();
}
static void test_message_address_nuls_display_name(void)
{
const unsigned char input[] =
"\"displayname\0nuls\\\0-esc\" <\"user\0nuls\\\0-esc\"@[domain\0nuls\\\0-esc]>";
const struct message_address output = {
NULL, NULL, "displayname\xEF\xBF\xBDnuls\\\xEF\xBF\xBD-esc", NULL,
"user\xEF\xBF\xBDnuls\\\xEF\xBF\xBD-esc",
"[domain\xEF\xBF\xBDnuls\\\xEF\xBF\xBD-esc]", FALSE
};
const struct message_address *addr;
test_begin("message address parsing with NULs in display-name");
addr = message_address_parse(pool_datastack_create(),
input, sizeof(input)-1, UINT_MAX, 0);
test_assert(addr != NULL && cmp_addr(addr, &output));
test_end();
}
static void test_message_address_non_strict_dots(void)
{
const char *const inputs[] = {
".@example.com",
"..@example.com",
"..foo@example.com",
"..foo..@example.com",
"..foo..bar..@example.com",
};
const struct message_address *addr;
struct message_address output = {
NULL, NULL, NULL, NULL, "local-part",
"example.com", FALSE
};
test_begin("message address parsing with non-strict dots");
for (unsigned int i = 0; i < N_ELEMENTS(inputs); i++) {
const unsigned char *addr_input =
(const unsigned char *)inputs[i];
/* invalid with strict-dots flag */
addr = message_address_parse(pool_datastack_create(),
addr_input, strlen(inputs[i]), UINT_MAX,
MESSAGE_ADDRESS_PARSE_FLAG_STRICT_DOTS);
test_assert_idx(addr != NULL && addr->invalid_syntax, i);
/* valid without the strict-dots flag */
addr = message_address_parse(pool_datastack_create(),
addr_input, strlen(inputs[i]), UINT_MAX, 0);
output.mailbox = t_strcut(inputs[i], '@');
test_assert_idx(addr != NULL && cmp_addr(addr, &output), i);
}
test_end();
}
static int
test_parse_path(const char *input, const struct message_address **addr_r)
{
struct message_address *addr;
char *input_dup;
int ret;
/* duplicate the input (without trailing NUL) so valgrind notices
if there's any out-of-bounds access */
size_t input_len = strlen(input);
if (input_len > 0)
input = input_dup = i_memdup(input, input_len);
ret = message_address_parse_path(pool_datastack_create(),
(const unsigned char *)input, input_len,
&addr);
if (input_len > 0)
i_free(input_dup);
*addr_r = addr;
return ret;
}
static void test_message_address_path(void)
{
static const struct test {
const char *input;
const char *wanted_output;
struct message_address addr;
} tests[] = {
{ "<>", NULL,
{ NULL, NULL, NULL, NULL, NULL, NULL, FALSE } },
{ " < > ", "<>",
{ NULL, NULL, NULL, NULL, NULL, NULL, FALSE } },
{ "<user@domain>", NULL,
{ NULL, NULL, NULL, NULL, "user", "domain", FALSE } },
{ " <user@domain> ", "<user@domain>",
{ NULL, NULL, NULL, NULL, "user", "domain", FALSE } },
{ "user@domain", "<user@domain>",
{ NULL, NULL, NULL, NULL, "user", "domain", FALSE } },
{ " user@domain ", "<user@domain>",
{ NULL, NULL, NULL, NULL, "user", "domain", FALSE } },
{ "<\"user\"@domain>", "<user@domain>",
{ NULL, NULL, NULL, NULL, "user", "domain", FALSE } },
{ "<\"user name\"@domain>", NULL,
{ NULL, NULL, NULL, NULL, "user name", "domain", FALSE } },
{ "<\"user@na\\\\me\"@domain>", NULL,
{ NULL, NULL, NULL, NULL, "user@na\\me", "domain", FALSE } },
{ "<\"user\\\"name\"@domain>", NULL,
{ NULL, NULL, NULL, NULL, "user\"name", "domain", FALSE } },
{ "<\"\"@domain>", NULL,
{ NULL, NULL, NULL, NULL, "", "domain", FALSE } },
{ "<@source", "<>",
{ NULL, NULL, NULL, NULL, NULL, NULL, TRUE } },
};
const struct message_address *addr;
string_t *str;
const char *wanted_string;
unsigned int i;
test_begin("message address path parsing");
str = t_str_new(128);
for (i = 0; i < N_ELEMENTS(tests); i++) {
const struct test *test = &tests[i];
const struct message_address *test_wanted_addr;
int ret;
test_wanted_addr = &test->addr;
ret = test_parse_path(test->input, &addr);
if (addr->invalid_syntax)
test_assert_idx(ret == -1, i);
else
test_assert_idx(ret == 0, i);
test_assert_idx(addr != NULL && addr->next == NULL &&
cmp_addr(addr, test_wanted_addr), i);
/* test the address alone */
str_truncate(str, 0);
message_address_write(str, addr);
if (test->wanted_output != NULL)
wanted_string = test->wanted_output;
else
wanted_string = test->input;
test_assert_idx(strcmp(str_c(str), wanted_string) == 0, i);
}
test_end();
}
static void test_message_address_path_invalid(void)
{
static const char *tests[] = {
"",
"<",
" < ",
">",
" > ",
"<user@domain",
" <user@domain ",
"user@domain>",
" user@domain> ",
"<user>",
"<@route@route2:user>",
"<@domain>",
"@domain",
" @domain ",
"<user@>",
"user@",
" user@ ",
"<user@domain>bladiebla",
"user@domain@"
};
const struct message_address *addr;
unsigned int i;
test_begin("message address path invalid");
for (i = 0; i < N_ELEMENTS(tests); i++) {
const char *test = tests[i];
int ret;
ret = test_parse_path(test, &addr);
test_assert_idx(ret < 0, i);
}
test_end();
}
int main(void)
{
static void (*const test_functions[])(void) = {
test_message_address,
test_message_address_list,
test_message_address_nuls,
test_message_address_nuls_display_name,
test_message_address_non_strict_dots,
test_message_address_path,
test_message_address_path_invalid,
NULL
};
return test_run(test_functions);
}
|