/
test-rfc822-parser.c
163 lines (151 loc) · 4.51 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
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
/* 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_comment(void)
{
static const struct {
const char *input, *output;
int ret;
} tests[] = {
{ "(", "", -1 },
{ "(()", "", -1 },
{ "()", "", 0 },
{ "(())", "()", 0 },
{ "(foo ( bar ) baz)", "foo ( bar ) baz", 0 },
{ "(foo\t\tbar)", "foo\t\tbar", 0 },
{ "(foo\\(bar)", "foo(bar", 0 },
{ "(foo\\\\bar)", "foo\\bar", 0 },
{ "(foo\\\\\\\\)", "foo\\\\", 0 },
{ "(foo\\)bar)", "foo)bar", 0 },
{ "(foo\"flop\"\"bar)", "foo\"flop\"\"bar", 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, parser2;
string_t *str = t_str_new(64);
unsigned int i = 0;
test_begin("rfc822 parse comment");
for (i = 0; i < N_ELEMENTS(tests); i++) {
rfc822_parser_init(&parser, (const void *)tests[i].input,
strlen(tests[i].input), str);
rfc822_parser_init(&parser2, (const void *)tests[i].input,
strlen(tests[i].input), NULL);
test_assert_idx(rfc822_skip_comment(&parser) == tests[i].ret, i);
test_assert_idx(rfc822_skip_comment(&parser2) == tests[i].ret, i);
test_assert_idx(tests[i].ret < 0 ||
strcmp(tests[i].output, str_c(str)) == 0, i);
rfc822_parser_deinit(&parser);
rfc822_parser_deinit(&parser2);
str_truncate(str, 0);
}
test_end();
}
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_domain_literal(void)
{
static const struct {
const char *input, *output;
int ret;
} tests[] = {
{ "@[", "", -1 },
{ "@[foo", "", -1 },
{ "@[foo[]", "", -1 },
{ "@[foo[]]", "", -1 },
{ "@[]", "[]", 0 },
{ "@[foo bar]", "[foo bar]", 0 },
{ "@[foo\n bar]", "[foo bar]", 0 },
{ "@[foo\n\t\t bar]", "[foo\t\t bar]", 0 },
{ "@[foo\\\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 domain literal");
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_domain(&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_comment,
test_rfc822_parse_quoted_string,
test_rfc822_parse_domain_literal,
test_rfc822_parse_content_param,
NULL
};
return test_run(test_functions);
}