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
|
/* Copyright (c) 2020 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "str.h"
#include "str-sanitize.h"
#include "test-common.h"
#include "smtp-syntax.h"
/*
* Valid string parse tests
*/
struct valid_string_parse_test {
const char *input, *parsed, *output;
};
static const struct valid_string_parse_test
valid_string_parse_tests[] = {
{
.input = "",
.parsed = "",
},
{
.input = "atom",
.parsed = "atom",
},
{
.input = "abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"0123456789!#$%&'*+-/=?^_`{|}~",
.parsed = "abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"0123456789!#$%&'*+-/=?^_`{|}~",
},
{
.input = "\"quoted-string\"",
.parsed = "quoted-string",
.output = "quoted-string",
},
{
.input = "\"quoted \\\"string\\\"\"",
.parsed = "quoted \"string\"",
},
{
.input = "\"quoted \\\\string\\\\\"",
.parsed = "quoted \\string\\",
},
};
static const unsigned int valid_string_parse_test_count =
N_ELEMENTS(valid_string_parse_tests);
static void test_smtp_string_parse_valid(void)
{
unsigned int i;
for (i = 0; i < valid_string_parse_test_count; i++) T_BEGIN {
const struct valid_string_parse_test *test =
&valid_string_parse_tests[i];
const char *parsed, *error = NULL;
int ret;
ret = smtp_string_parse(test->input, &parsed, &error);
test_begin(t_strdup_printf("smtp string valid [%d]", i));
test_out_reason(t_strdup_printf("parse(\"%s\")", test->input),
ret >= 0, error);
test_assert(ret != 0 || *test->input == '\0');
if (!test_has_failed()) {
string_t *encoded;
const char *output;
test_out(t_strdup_printf("parsed = \"%s\"", parsed),
null_strcmp(parsed, test->parsed) == 0);
encoded = t_str_new(255);
smtp_string_write(encoded, parsed);
output = (test->output == NULL ?
test->input : test->output);
test_out(t_strdup_printf("write() = \"%s\"",
str_c(encoded)),
strcmp(str_c(encoded), output) == 0);
}
test_end();
} T_END;
}
/*
* Invalid string parse tests
*/
struct invalid_string_parse_test {
const char *input;
};
static const struct invalid_string_parse_test
invalid_string_parse_tests[] = {
{
.input = " ",
},
{
.input = "\\",
},
{
.input = "\"",
},
{
.input = "\"aa",
},
{
.input = "aa\"",
},
};
static const unsigned int invalid_string_parse_test_count =
N_ELEMENTS(invalid_string_parse_tests);
static void test_smtp_string_parse_invalid(void)
{
unsigned int i;
for (i = 0; i < invalid_string_parse_test_count; i++) T_BEGIN {
const struct invalid_string_parse_test *test =
&invalid_string_parse_tests[i];
const char *parsed, *error;
int ret;
ret = smtp_string_parse(test->input, &parsed, &error);
test_begin(t_strdup_printf("smtp string invalid [%d]", i));
test_out_reason(t_strdup_printf("parse(\"%s\")", test->input),
ret < 0, error);
test_end();
} T_END;
}
/*
* Tests
*/
int main(void)
{
static void (*test_functions[])(void) = {
test_smtp_string_parse_valid,
test_smtp_string_parse_invalid,
NULL
};
return test_run(test_functions);
}
|