-
Notifications
You must be signed in to change notification settings - Fork 271
/
test-rfc822-parser.c
83 lines (77 loc) · 2.21 KB
/
test-rfc822-parser.c
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
/* Copyright (c) 2015-2018 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "str.h"
#include "rfc822-parser.h"
#include "test-common.h"
static void test_rfc822_parse_quoted_string(void)
{
static const struct {
const char *input, *output;
int ret;
} tests[] = {
{ "\"", "", -1 },
{ "\"\"", "", 0 },
{ "\"foo\"", "foo", 0 },
{ "\"\"foo", "", 1 },
{ "\"\"\"", "", 1 },
{ "\"\\\"\"", "\"", 0 },
{ "\"\\\\\"", "\\", 0 },
{ "\"\\\\foo\\\\foo\\\\\"", "\\foo\\foo\\", 0 },
{ "\"foo\n bar\"", "foo bar", 0 },
{ "\"foo\n\t\t bar\"", "foo\t\t bar", 0 },
{ "\"foo\\\n bar\"", "foo\\ bar", 0 },
{ "\"foo\\\r\n bar\"", "foo\\ bar", 0 },
};
struct rfc822_parser_context parser;
string_t *str = t_str_new(64);
unsigned int i = 0;
test_begin("rfc822 parse quoted string");
for (i = 0; i < N_ELEMENTS(tests); i++) {
rfc822_parser_init(&parser, (const void *)tests[i].input,
strlen(tests[i].input), NULL);
test_assert_idx(rfc822_parse_quoted_string(&parser, str) == tests[i].ret, i);
test_assert_idx(tests[i].ret < 0 ||
strcmp(tests[i].output, str_c(str)) == 0, i);
rfc822_parser_deinit(&parser);
str_truncate(str, 0);
}
test_end();
}
static void test_rfc822_parse_content_param(void)
{
const char *input =
"; key1=value1#$!%&'*+-.^_`{|}~"
"; key2=\" \\\"(),/:;<=>?@[\\\\]\"";
const struct {
const char *key, *value;
} output[] = {
{ "key1", "value1#$!%&'*+-.^_`{|}~" },
{ "key2", " \"(),/:;<=>?@[\\]" }
};
struct rfc822_parser_context parser;
const char *key;
string_t *value = t_str_new(64);
unsigned int i = 0;
int ret;
test_begin("rfc822 parse content param");
rfc822_parser_init(&parser, (const void *)input, strlen(input), NULL);
while ((ret = rfc822_parse_content_param(&parser, &key, value)) > 0 &&
i < N_ELEMENTS(output)) {
test_assert_idx(strcmp(output[i].key, key) == 0, i);
test_assert_idx(strcmp(output[i].value, str_c(value)) == 0, i);
i++;
}
rfc822_parser_deinit(&parser);
test_assert(ret == 0);
test_assert(i == N_ELEMENTS(output));
test_end();
}
int main(void)
{
static void (*const test_functions[])(void) = {
test_rfc822_parse_quoted_string,
test_rfc822_parse_content_param,
NULL
};
return test_run(test_functions);
}