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
|
/* Copyright (c) 2013-2018 Dovecot authors, see the included COPYING file */
#include "test-lib.h"
#include "buffer.h"
#include "str.h"
#include "str-sanitize.h"
#include "istream.h"
#include "ostream.h"
#include "test-common.h"
#include "http-response-parser.h"
#include <time.h>
struct valid_parse_test_response {
unsigned char version_major;
unsigned char version_minor;
unsigned int status;
uoff_t content_length;
const char *payload;
};
struct valid_parse_test {
const char *input;
enum http_response_parse_flags flags;
const struct valid_parse_test_response *responses;
unsigned int responses_count;
};
/* Valid response tests */
static const struct valid_parse_test_response valid_responses1[] = {
{
.status = 200,
.payload = "This is a piece of stupid text.\r\n"
}
};
static const struct valid_parse_test_response valid_responses2[] = {
{
.status = 200,
.payload = "This is a piece of stupid text.\r\n"
},{
.status = 200,
.payload = "This is a piece of even more stupid text.\r\n"
}
};
static const struct valid_parse_test_response valid_responses3[] = {
{
.status = 401,
.payload = "Frop!"
}
};
static const struct valid_parse_test_response valid_responses4[] = {
{
.status = 200,
.payload = "Invalid date header"
}
};
static const struct valid_parse_test_response valid_responses5[] = {
{
.status = 200,
.payload = "Duplicate headers"
}
};
static const struct valid_parse_test
valid_response_parse_tests[] = {
{ .input =
"HTTP/1.1 200 OK\r\n"
"Date: Sun, 07 Oct 2012 13:02:27 GMT\r\n"
"Server: Apache/2.2.16 (Debian)\r\n"
"Last-Modified: Tue, 18 Sep 2012 19:31:41 GMT\r\n"
"Etag: \"2a8400c-10751f-4c9fef0858140\"\r\n"
"Accept-Ranges: bytes\r\n"
"Content-Length: 33\r\n"
"Keep-Alive: timeout=15, max=100\r\n"
"Connection: Keep-Alive\r\n"
"Content-Type: text/plain\r\n"
"\r\n"
"This is a piece of stupid text.\r\n",
.responses = valid_responses1,
.responses_count = N_ELEMENTS(valid_responses1)
},{
.input =
"HTTP/1.1 200 OK\r\n"
"Date: Sun, 07 Oct 2012 13:02:27 GMT\r\n"
"Server: Apache/2.2.16 (Debian)\r\n"
"Last-Modified: Tue, 18 Sep 2012 19:31:41 GMT\r\n"
"Etag: \"2a8400c-10751f-4c9fef0858140\"\r\n"
"Accept-Ranges: bytes\r\n"
"Content-Length: 33\r\n"
"Keep-Alive: timeout=15, max=100\r\n"
"Connection: Keep-Alive\r\n"
"Content-Type: text/plain\r\n"
"\r\n"
"This is a piece of stupid text.\r\n"
"HTTP/1.1 200 OK\r\n"
"Date: Sun, 07 Oct 2012 13:02:27 GMT\r\n"
"Server: Apache/2.2.16 (Debian)\r\n"
"Last-Modified: Tue, 18 Sep 2012 19:31:41 GMT\r\n"
"Etag: \"2a8400c-10751f-4c9fef0858140\"\r\n"
"Accept-Ranges: bytes\r\n"
"Content-Length: 43\r\n"
"Keep-Alive: timeout=15, max=100\r\n"
"Connection: Keep-Alive\r\n"
"Content-Type: text/plain\r\n"
"\r\n"
"This is a piece of even more stupid text.\r\n",
.responses = valid_responses2,
.responses_count = N_ELEMENTS(valid_responses2)
},{
.input =
"HTTP/1.1 401 Authorization Required\r\n"
"Date: Sun, 07 Oct 2012 19:52:03 GMT\r\n"
"Server: Apache/2.2.16 (Debian) PHP/5.3.3-7+squeeze14\r\n"
"WWW-Authenticate: Basic realm=\"Munin\"\r\n"
"Vary: Accept-Encoding\r\n"
"Content-Encoding: gzip\r\n"
"Content-Length: 5\r\n"
"Keep-Alive: timeout=15, max=99\r\n"
"Connection: Keep-Alive\r\n"
"Content-Type: text/html; charset=iso-8859-1\r\n"
"\r\n"
"Frop!",
.responses = valid_responses3,
.responses_count = N_ELEMENTS(valid_responses3)
},{
.input =
"HTTP/1.1 200 OK\r\n"
"Date: Sun, 07 Ocu 2012 19:52:03 GMT\r\n"
"Content-Length: 19\r\n"
"Keep-Alive: timeout=15, max=99\r\n"
"Connection: Keep-Alive\r\n"
"Date: Sun, 13 Oct 2013 13:13:13 GMT\r\n"
"\r\n"
"Invalid date header",
.responses = valid_responses4,
.responses_count = N_ELEMENTS(valid_responses4)
},{
.input =
"HTTP/1.1 200 OK\r\n"
"Date: Sun, 07 Oct 2012 19:52:03 GMT\r\n"
"Server: Apache/2.2.16 (Debian) PHP/5.3.3-7+squeeze14\r\n"
"Content-Length: 17\r\n"
"Keep-Alive: timeout=15, max=99\r\n"
"Connection: Keep-Alive\r\n"
"Content-Type: text/html; charset=iso-8859-1\r\n"
"Date: Sun, 13 Oct 2013 13:13:13 GMT\r\n"
"\r\n"
"Duplicate headers",
.responses = valid_responses5,
.responses_count = N_ELEMENTS(valid_responses5)
}
};
static const unsigned int valid_response_parse_test_count =
N_ELEMENTS(valid_response_parse_tests);
static void test_http_response_parse_valid(void)
{
unsigned int i;
buffer_t *payload_buffer = buffer_create_dynamic(default_pool, 1024);
for (i = 0; i < valid_response_parse_test_count; i++) T_BEGIN {
struct istream *input;
struct ostream *output;
const struct valid_parse_test *test;
const struct valid_parse_test_response *tresponse;
struct http_response_parser *parser;
struct http_response presponse;
const char *input_text, *payload, *error;
unsigned int j, pos, input_text_len;
int ret = 0;
i_zero(&presponse);
test = &valid_response_parse_tests[i];
input_text = test->input;
input_text_len = strlen(input_text);
input = test_istream_create_data(input_text, input_text_len);
parser = http_response_parser_init(input, NULL,
valid_response_parse_tests[i].flags);
test_begin(t_strdup_printf("http response valid [%d]", i));
payload = NULL;
for (pos = 0; pos < input_text_len && ret == 0; pos++) {
test_istream_set_size(input, pos);
ret = http_response_parse_next(parser,
HTTP_RESPONSE_PAYLOAD_TYPE_ALLOWED, &presponse, &error);
}
test_istream_set_size(input, input_text_len);
i_stream_unref(&input);
j = 0;
test_out("parse success", ret > 0);
while (ret > 0) {
if (presponse.payload != NULL) {
buffer_set_used_size(payload_buffer, 0);
output = o_stream_create_buffer(payload_buffer);
test_out("payload receive",
o_stream_send_istream(output, presponse.payload)
== OSTREAM_SEND_ISTREAM_RESULT_FINISHED);
o_stream_destroy(&output);
payload = str_c(payload_buffer);
} else {
payload = NULL;
}
test_assert(j < test->responses_count);
if (j >= test->responses_count)
break;
tresponse = &test->responses[j];
/* verify last response only */
test_out(t_strdup_printf("response->status = %d",
tresponse->status),
presponse.status == tresponse->status);
if (payload == NULL || tresponse->payload == NULL) {
test_out(t_strdup_printf("response->payload = %s",
str_sanitize(payload, 80)),
payload == tresponse->payload);
} else {
test_out(t_strdup_printf("response->payload = %s",
str_sanitize(payload, 80)),
strcmp(payload, tresponse->payload) == 0);
}
ret = http_response_parse_next(parser,
HTTP_RESPONSE_PAYLOAD_TYPE_ALLOWED, &presponse, &error);
if (++j == test->responses_count)
test_out("parse end", ret == 0);
else
test_out("parse success", ret > 0);
}
test_assert(ret == 0);
test_end();
http_response_parser_deinit(&parser);
} T_END;
buffer_free(&payload_buffer);
}
/*
* Invalid response tests
*/
struct invalid_parse_test {
const char *input;
enum http_response_parse_flags flags;
};
static struct invalid_parse_test invalid_response_parse_tests[] = {
{
.input =
"XMPP/1.0 302 Found\r\n"
"Location: http://www.example.nl/\r\n"
"Cache-Control: private\r\n"
},{
.input =
"HTTP/1.1 302 Found\r\n"
"Location: http://www.example.nl/\r\n"
"Cache-Control: private\r\n"
},{
.input =
"HTTP/1.1 ABC Found\r\n"
"Location: http://www.example.nl/\r\n"
"Cache-Control: private\r\n"
},{
.input =
"HTTP/1.1 302 \177\r\n"
"Location: http://www.example.nl/\r\n"
"Cache-Control: private\r\n"
},{
.input =
"HTTP/1.1 302 Found\n\r"
"Location: http://www.example.nl/\n\r"
"Cache-Control: private\n\r"
},{
.input =
"HTTP/1.1 200 OK\r\n"
"Date: Sun, 07 Ocu 2012 19:52:03 GMT\r\n"
"Content-Length: 19\r\n"
"Keep-Alive: timeout=15, max=99\r\n"
"Connection: Keep-Alive\r\n"
"\r\n"
"Invalid date header",
.flags = HTTP_RESPONSE_PARSE_FLAG_STRICT
},{
.input =
"HTTP/1.1 200 OK\r\n"
"Date: Sun, 07 Oct 2012 19:52:03 GMT\r\n"
"Server: Apache/2.2.16 (Debian) PHP/5.3.3-7+squeeze14\r\n"
"Content-Length: 17\r\n"
"Keep-Alive: timeout=15, max=99\r\n"
"Connection: Keep-Alive\r\n"
"Content-Type: text/html; charset=iso-8859-1\r\n"
"Date: Sun, 13 Oct 2013 13:13:13 GMT\r\n"
"\r\n"
"Duplicate headers",
.flags = HTTP_RESPONSE_PARSE_FLAG_STRICT
}
};
static const unsigned int invalid_response_parse_test_count =
N_ELEMENTS(invalid_response_parse_tests);
static void test_http_response_parse_invalid(void)
{
struct http_response_parser *parser;
struct http_response response;
const char *response_text, *error;
struct istream *input;
int ret;
unsigned int i;
for (i = 0; i < invalid_response_parse_test_count; i++) T_BEGIN {
const char *test;
test = invalid_response_parse_tests[i].input;
response_text = test;
input = i_stream_create_from_data(response_text, strlen(response_text));
parser = http_response_parser_init(input, NULL,
invalid_response_parse_tests[i].flags);
i_stream_unref(&input);
test_begin(t_strdup_printf("http response invalid [%d]", i));
while ((ret=http_response_parse_next(parser, HTTP_RESPONSE_PAYLOAD_TYPE_ALLOWED, &response, &error)) > 0);
test_out_reason("parse failure", ret < 0, error);
test_end();
http_response_parser_deinit(&parser);
} T_END;
}
/*
* Bad response tests
*/
static const unsigned char bad_response_with_nuls[] =
"HTTP/1.1 200 OK\r\n"
"Server: text\0server\r\n"
"\r\n";
static void test_http_response_parse_bad(void)
{
struct http_response_parser *parser;
struct http_response response;
const char *header, *error;
struct istream *input;
int ret;
/* parse failure guarantees http_response_header.size equals
strlen(http_response_header.value) */
test_begin("http response with NULs (strict)");
input = i_stream_create_from_data(bad_response_with_nuls,
sizeof(bad_response_with_nuls)-1);
parser = http_response_parser_init(input, NULL,
HTTP_RESPONSE_PARSE_FLAG_STRICT);
i_stream_unref(&input);
while ((ret=http_response_parse_next(parser,
HTTP_RESPONSE_PAYLOAD_TYPE_ALLOWED, &response, &error)) > 0);
test_assert(ret < 0);
http_response_parser_deinit(&parser);
test_end();
/* even when lenient, bad characters like NUL must not be returned */
test_begin("http response with NULs (lenient)");
input = i_stream_create_from_data(bad_response_with_nuls,
sizeof(bad_response_with_nuls)-1);
parser = http_response_parser_init(input, NULL, 0);
i_stream_unref(&input);
ret = http_response_parse_next(parser,
HTTP_RESPONSE_PAYLOAD_TYPE_ALLOWED, &response, &error);
test_out("parse success", ret > 0);
header = http_response_header_get(&response, "server");
test_out("header present", header != NULL);
if (header != NULL) {
test_out(t_strdup_printf("header Server: %s", header),
strcmp(header, "textserver") == 0);
}
ret = http_response_parse_next(parser,
HTTP_RESPONSE_PAYLOAD_TYPE_ALLOWED, &response, &error);
test_out("parse end", ret == 0);
http_response_parser_deinit(&parser);
test_end();
}
int main(void)
{
static void (*const test_functions[])(void) = {
test_http_response_parse_valid,
test_http_response_parse_invalid,
test_http_response_parse_bad,
NULL
};
return test_run(test_functions);
}
|