summaryrefslogtreecommitdiffstats
path: root/src/lib-smtp/test-smtp-reply.c
blob: 69ded10ccd14ce2261c64d741e1806d0c83ba2b9 (plain)
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
/* Copyright (c) 2013-2018 Dovecot authors, see the included COPYING file */

#include "test-lib.h"
#include "array.h"
#include "buffer.h"
#include "str.h"
#include "str-sanitize.h"
#include "istream.h"
#include "ostream.h"
#include "test-common.h"
#include "smtp-reply-parser.h"

#include <time.h>

struct smtp_reply_parse_valid_test {
	const char *input, *output;
	unsigned int status;
	bool ehlo;
	size_t max_size;
	struct {
		unsigned int x, y, z;
	} enhanced_code;
	const char *const *text_lines;
};

/* Valid reply tests */

static const struct smtp_reply_parse_valid_test
valid_reply_parse_tests[] = {
	{
		.input = "220\r\n",
		.output = "220 \r\n",
		.status = 220,
		.text_lines = (const char *[]){ "", NULL }
	},{
		.input = "220 \r\n",
		.status = 220,
		.text_lines = (const char *[]){ "", NULL }
	},{
		.input = "220 OK\r\n",
		.status = 220,
		.text_lines = (const char *[]){ "OK", NULL }
	},{
		.input = "550 Requested action not taken: mailbox unavailable\r\n",
		.status = 550,
		.text_lines = (const char *[])
			{ "Requested action not taken: mailbox unavailable", NULL }
	},{
		.input =
			"250-smtp.example.com Hello client.example.org [10.0.0.1]\r\n"
			"250-SIZE 52428800\r\n"
			"250-PIPELINING\r\n"
			"250-STARTTLS\r\n"
			"250 HELP\r\n",
		.ehlo = TRUE,
		.status = 250,
		.text_lines = (const char *[]) {
			"smtp.example.com Hello client.example.org [10.0.0.1]",
			"SIZE 52428800",
			"PIPELINING",
			"STARTTLS",
			"HELP",
			NULL
		}
	},{
		.input =
			"250-smtp.example.com We got some nice '\x03' and '\x04'\r\n"
			"250 HELP\r\n",
		.output =
			"250-smtp.example.com We got some nice ' ' and ' '\r\n"
			"250 HELP\r\n",
		.ehlo = TRUE,
		.status = 250,
		.text_lines = (const char *[]) {
			"smtp.example.com We got some nice ' ' and ' '",
			"HELP",
			NULL
		}
	},{
		.input =
			"250 smtp.example.com We got some nice '\x08'\r\n",
		.output =
			"250 smtp.example.com We got some nice ' '\r\n",
		.ehlo = TRUE,
		.status = 250,
		.text_lines = (const char *[]) {
			"smtp.example.com We got some nice ' '",
			NULL
		}
	},{
		.input = "250 2.1.0 Originator <frop@example.com> ok\r\n",
		.status = 250,
		.enhanced_code = { 2, 1, 0 },
		.text_lines = (const char *[]){
			"Originator <frop@example.com> ok", NULL
		}
	},{
		.input =
			"551-5.7.1 Forwarding to remote hosts disabled\r\n"
			"551 5.7.1 Select another host to act as your forwarder\r\n",
		.status = 551,
		.enhanced_code = { 5, 7, 1 },
		.text_lines = (const char *[])	{
			"Forwarding to remote hosts disabled",
			"Select another host to act as your forwarder",
			NULL
		}
	}
};

unsigned int valid_reply_parse_test_count =
	N_ELEMENTS(valid_reply_parse_tests);

static void test_smtp_reply_parse_valid(void)
{
	unsigned int i;

	for (i = 0; i < valid_reply_parse_test_count; i++) T_BEGIN {
		struct istream *input;
		const struct smtp_reply_parse_valid_test *test;
		struct smtp_reply_parser *parser;
		struct smtp_reply *reply;
		const char *error;
		int ret;

		test = &valid_reply_parse_tests[i];
		input = i_stream_create_from_data(test->input,
						  strlen(test->input));
		parser = smtp_reply_parser_init(input, test->max_size);
		i_stream_unref(&input);

		test_begin(t_strdup_printf("smtp reply valid [%d]", i));

		if (test->ehlo) {
			while ((ret=smtp_reply_parse_ehlo
				(parser, &reply, &error)) > 0) {
			}
		} else {
			while ((ret=smtp_reply_parse_next
				(parser, test->enhanced_code.x > 0, &reply, &error)) > 0) {
			}
		}

		test_out_reason("parse success", ret == 0, error);

		if (ret == 0) {
			const char *output;
			string_t *encoded;

			/* verify last response only */
			test_out(t_strdup_printf("reply->status = %d", test->status),
					reply->status == test->status);
			if (test->enhanced_code.x > 0) {
				test_out(t_strdup_printf("reply->enhanced_code = %d.%d.%d",
					test->enhanced_code.x, test->enhanced_code.y, test->enhanced_code.z),
					(reply->enhanced_code.x == test->enhanced_code.x &&
						reply->enhanced_code.y == test->enhanced_code.y &&
						reply->enhanced_code.z == test->enhanced_code.z));
			}
			if (test->text_lines != NULL) {
				const char *const *line = test->text_lines;
				const char *const *reply_line = reply->text_lines;
				unsigned int index = 0;

				while (*line != NULL) {
					if (*reply_line == NULL) {
						test_out(
							t_strdup_printf("reply->text_lines[%d] = NULL", index),
							FALSE);
						break;
					}
					test_out(t_strdup_printf(
						"reply->text_lines[%d] = \"%s\"", index, *reply_line),
						strcmp(*line, *reply_line) == 0);
					line++;
					reply_line++;
					index++;
				}
			} else {
				test_out("reply->text_lines = NULL", reply->text_lines == NULL);
			}

			encoded = t_str_new(512);
			smtp_reply_write(encoded, reply);

			output = (test->output == NULL ? test->input : test->output);
			test_out("write() = input",
				strcmp(str_c(encoded), output) == 0);
		}
		test_end();
		smtp_reply_parser_deinit(&parser);
	} T_END;
}

struct smtp_reply_parse_invalid_test {
	const char *reply;
	bool ehlo;
	size_t max_size;
};

static const struct smtp_reply_parse_invalid_test
	invalid_reply_parse_tests[] = {
	{
		.reply = "22X OK\r\n"
	},{
		.reply = "220OK\r\n"
	},{
		.reply =
			"200-This is\r\n"
			"250 inconsistent.\r\n"
	},{
		.reply = "400 This \r is wrong\r\n"
	},{
		.reply = "500 This is \x03 worse\r\n"
	},{
		.reply = "699 Obscure\r\n"
	},{
		.reply = "100 Invalid\r\n"
	},{
		.reply = "400 Interrupted\r"
	},{
		.reply = "251 example.com We got '\x04'\r\n",
		.ehlo = TRUE
	},{
		.reply =
			"250-example.com Hello\r\n"
			"250 We got some '\x08' for you\r\n",
		.ehlo = TRUE
	},{
		.reply =
			"556-This is a very long reply\r\n"
			"556 that exceeds the very low limit.\r\n",
		.max_size = 50
	}
};

unsigned int invalid_reply_parse_test_count =
	N_ELEMENTS(invalid_reply_parse_tests);

static void test_smtp_reply_parse_invalid(void)
{
	unsigned int i;

	for (i = 0; i < invalid_reply_parse_test_count; i++) T_BEGIN {
		const struct smtp_reply_parse_invalid_test *test;
		struct istream *input;
		struct smtp_reply_parser *parser;
		struct smtp_reply *reply;
		const char *reply_text, *error;
		int ret;

		test = &invalid_reply_parse_tests[i];
		reply_text = test->reply;
		input = i_stream_create_from_data(reply_text, strlen(reply_text));
		parser = smtp_reply_parser_init(input, test->max_size);
		i_stream_unref(&input);

		test_begin(t_strdup_printf("smtp reply invalid [%d]", i));

		if (test->ehlo)
			while ((ret=smtp_reply_parse_ehlo(parser, &reply, &error)) > 0);
		else
			while ((ret=smtp_reply_parse_next(parser, FALSE, &reply, &error)) > 0);
		test_out_reason(t_strdup_printf("parse(\"%s\")",
			str_sanitize(reply_text, 80)), ret < 0, error);
		test_end();
		smtp_reply_parser_deinit(&parser);
	} T_END;
}

int main(void)
{
	static void (*test_functions[])(void) = {
		test_smtp_reply_parse_valid,
		test_smtp_reply_parse_invalid,
		NULL
	};
	return test_run(test_functions);
}