test-http-header-parser.c revision d1f1ad02d1ed5d83c62fba356259b7c9073608c6
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen/* Copyright (c) 2013 Dovecot authors, see the included COPYING file */
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen#include "test-lib.h"
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen#include "istream.h"
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen#include "test-common.h"
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen#include "http-header-parser.h"
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen#include <time.h>
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainenstruct http_header_parse_result {
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen const char *name;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen const char *value;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen};
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainenstruct http_header_parse_test {
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen const char *header;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen const struct http_header_parse_result *fields;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen};
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen/* Valid header tests */
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainenstatic struct http_header_parse_result valid_header_parse_result1[] = {
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen { "Date", "Sat, 06 Oct 2012 16:01:44 GMT" },
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen { "Server", "Apache/2.2.16 (Debian)" },
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen { "Last-Modified", "Mon, 30 Jul 2012 11:09:28 GMT" },
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen { "Etag", "\"3d24677-3261-4c60a1863aa00\"" },
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen { "Accept-Ranges", "bytes" },
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen { "Vary", "Accept-Encoding" },
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen { "Content-Encoding", "gzip" },
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen { "Content-Length", "4092" },
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen { "Keep-Alive", "timeout=15, max=100" },
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen { "Connection", "Keep-Alive" },
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen { "Content-Type", "text/html" },
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen { NULL, NULL }
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen};
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainenstatic struct http_header_parse_result valid_header_parse_result2[] = {
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen { "Host", "p5-lrqzb4yavu4l7nagydw-428649-i2-v6exp3-ds.metric.example.com" },
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen { "User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0)" },
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen { "Accept", "image/png,image/*;q=0.8,*/*;q=0.5" },
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen { "Accept-Language", "en-us,en;q=0.5" },
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen { "Accept-Encoding", "gzip, deflate" },
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen { "DNT", "1" },
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen { "Connection", "keep-alive" },
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen { "Referer", "http://www.example.nl/" },
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen { NULL, NULL }
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen};
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainenstatic struct http_header_parse_result valid_header_parse_result3[] = {
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen { "Date", "Sat, 06 Oct 2012 17:12:37 GMT" },
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen { "Server", "Apache/2.2.16 (Debian) PHP/5.3.3-7+squeeze14 with"
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen " Suhosin-Patch proxy_html/3.0.1 mod_python/3.3.1 Python/2.6.6"
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen " mod_ssl/2.2.16 OpenSSL/0.9.8o mod_perl/2.0.4 Perl/v5.10.1" },
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen { "WWW-Authenticate", "Basic realm=\"Munin\"" },
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen { "Vary", "Accept-Encoding" },
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen { "Content-Encoding", "gzip" },
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen { "Content-Length", "445" },
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen { "Keep-Alive", "timeout=15, max=98" },
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen { "Connection", "Keep-Alive" },
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen { "Content-Type", "text/html; charset=iso-8859-1" },
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen { NULL, NULL }
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen};
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainenstatic struct http_header_parse_result valid_header_parse_result4[] = {
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen { NULL, NULL }
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen};
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainenstatic const struct http_header_parse_test valid_header_parse_tests[] = {
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen { .header =
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen "Date: Sat, 06 Oct 2012 16:01:44 GMT\r\n"
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen "Server: Apache/2.2.16 (Debian)\r\n"
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen "Last-Modified: Mon, 30 Jul 2012 11:09:28 GMT\r\n"
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen "Etag: \"3d24677-3261-4c60a1863aa00\"\r\n"
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen "Accept-Ranges: bytes\r\n"
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen "Vary: Accept-Encoding\r\n"
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen "Content-Encoding: gzip\r\n"
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen "Content-Length: 4092\r\n"
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen "Keep-Alive: timeout=15, max=100\r\n"
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen "Connection: Keep-Alive\r\n"
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen "Content-Type: text/html\r\n"
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen "\r\n",
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen .fields = valid_header_parse_result1
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen },{
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen .header =
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen "Host: p5-lrqzb4yavu4l7nagydw-428649-i2-v6exp3-ds.metric.example.com\n"
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen "User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0)\n"
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen "Accept:\t\timage/png,image/*;q=0.8,*/*;q=0.5\n"
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen "Accept-Language:\ten-us,en;q=0.5\n"
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen "Accept-Encoding: \t\tgzip, deflate\n"
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen "DNT: 1\n"
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen "Connection: \t\tkeep-alive\n"
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen "Referer: http://www.example.nl/\n"
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen "\n",
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen .fields = valid_header_parse_result2
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen },{
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen .header =
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen "Date: Sat, 06 Oct 2012 17:12:37 GMT\r\n"
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen "Server: Apache/2.2.16 (Debian) PHP/5.3.3-7+squeeze14 with\r\n"
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen " Suhosin-Patch proxy_html/3.0.1 mod_python/3.3.1 Python/2.6.6\r\n"
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen " mod_ssl/2.2.16 OpenSSL/0.9.8o mod_perl/2.0.4 Perl/v5.10.1\r\n"
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen "WWW-Authenticate: Basic realm=\"Munin\"\r\n"
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen "Vary: Accept-Encoding\r\n"
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen "Content-Encoding: gzip\r\n"
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen "Content-Length: 445\r\n"
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen "Keep-Alive: timeout=15, max=98\r\n"
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen "Connection: Keep-Alive\r\n"
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen "Content-Type: text/html; charset=iso-8859-1\r\n"
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen "\r\n",
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen .fields = valid_header_parse_result3
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen },{
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen .header =
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen "\r\n",
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen .fields = valid_header_parse_result4
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen }
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen};
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainenunsigned int valid_header_parse_test_count = N_ELEMENTS(valid_header_parse_tests);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainenstatic void test_http_header_parse_valid(void)
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen{
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen unsigned int i;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen for (i = 0; i < valid_header_parse_test_count; i++) T_BEGIN {
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen struct istream *input;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen struct http_header_parser *parser;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen const char *header, *field_name, *error;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen const unsigned char *field_data;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen size_t field_size;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen int ret;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen unsigned int j, pos, header_len;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen header = valid_header_parse_tests[i].header;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen header_len = strlen(header);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen input = test_istream_create_data(header, header_len);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen parser = http_header_parser_init(input);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen test_begin(t_strdup_printf("http header valid [%d]", i));
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen j = 0; pos = 0; test_istream_set_size(input, 0);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen while ((ret=http_header_parse_next_field
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen (parser, &field_name, &field_data, &field_size, &error)) >= 0) {
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen const struct http_header_parse_result *result;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen const char *field_value;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen if (ret == 0) {
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen if (pos == header_len)
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen break;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen test_istream_set_size(input, ++pos);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen continue;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen }
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen if (field_name == NULL) break;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen result = &valid_header_parse_tests[i].fields[j];
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen field_value = t_strndup(field_data, field_size);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen if (result->name == NULL) {
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen test_out_reason("valid", FALSE,
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen t_strdup_printf("%s: %s", field_name, field_value));
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen break;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen }
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen test_out_reason("valid",
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen strcmp(result->name, field_name) == 0 &&
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen strcmp(result->value, field_value) == 0,
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen t_strdup_printf("%s: %s", field_name, field_value));
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen j++;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen }
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen test_out("parse success", ret > 0);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen test_end();
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen http_header_parser_deinit(&parser);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen } T_END;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen}
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainenstatic const char *invalid_header_parse_tests[] = {
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen "Date: Sat, 06 Oct 2012 16:01:44 GMT\r\n"
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen "Server : Apache/2.2.16 (Debian)\r\n"
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen "Last-Modified: Mon, 30 Jul 2012 11:09:28 GMT\r\n"
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen "\r\n",
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen "Date: Sat, 06 Oct 2012 17:18:22 GMT\r\n"
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen "Server: Apache/2.2.3 (CentOS)\r\n"
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen "X Powered By: PHP/5.3.6\r\n"
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen "\r\n",
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen "Host: www.example.com\n\r"
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen "Accept: image/png,image/*;q=0.8,*/*;q=0.5\n\r"
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen "Accept-Language: en-us,en;q=0.5\n\r"
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen "Accept-Encoding: gzip, deflate\n\r"
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen "\n\r",
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen "Host: p5-lrqzb4yavu4l7nagydw-428649-i2-v6exp3-ds.metric.example.com\n"
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen "User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0)\n"
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen "Accept:\t\timage/png,image/*;q=0.8,*/\177;q=0.5\n"
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen "\n",
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen "Date: Sat, 06 Oct 2012 17:12:37 GMT\r\n"
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen "Server: Apache/2.2.16 (Debian) PHP/5.3.3-7+squeeze14 with\r\n"
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen "Suhosin-Patch proxy_html/3.0.1 mod_python/3.3.1 Python/2.6.6\r\n"
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen "mod_ssl/2.2.16 OpenSSL/0.9.8o mod_perl/2.0.4 Perl/v5.10.1\r\n"
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen "\r\n",
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen};
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainenunsigned int invalid_header_parse_test_count = N_ELEMENTS(invalid_header_parse_tests);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainenstatic void test_http_header_parse_invalid(void)
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen{
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen unsigned int i;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen for (i = 0; i < invalid_header_parse_test_count; i++) T_BEGIN {
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen struct istream *input;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen struct http_header_parser *parser;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen const char *header, *field_name, *error;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen const unsigned char *field_data;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen size_t field_size;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen int ret;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen header = invalid_header_parse_tests[i];
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen input = i_stream_create_from_data(header, strlen(header));
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen parser = http_header_parser_init(input);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen test_begin(t_strdup_printf("http header invalid [%d]", i));
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen
while ((ret=http_header_parse_next_field
(parser, &field_name, &field_data, &field_size, &error)) > 0) {
if (field_name == NULL) break;
}
test_out("parse failure", ret < 0);
test_end();
http_header_parser_deinit(&parser);
} T_END;
}
int main(void)
{
static void (*test_functions[])(void) = {
test_http_header_parse_valid,
test_http_header_parse_invalid,
NULL
};
return test_run(test_functions);
}