test-http-header-parser.c revision d1f1ad02d1ed5d83c62fba356259b7c9073608c6
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen/* Copyright (c) 2013 Dovecot authors, see the included COPYING file */
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen const struct http_header_parse_result *fields;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen/* Valid header tests */
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainenstatic struct http_header_parse_result valid_header_parse_result1[] = {
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen { "Last-Modified", "Mon, 30 Jul 2012 11:09:28 GMT" },
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 Sirainenstatic struct http_header_parse_result valid_header_parse_result3[] = {
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 { "Content-Type", "text/html; charset=iso-8859-1" },
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainenstatic struct http_header_parse_result valid_header_parse_result4[] = {
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainenstatic const struct http_header_parse_test valid_header_parse_tests[] = {
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"
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 "Connection: \t\tkeep-alive\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"
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"
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainenunsigned int valid_header_parse_test_count = N_ELEMENTS(valid_header_parse_tests);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen unsigned int i;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen for (i = 0; i < valid_header_parse_test_count; i++) T_BEGIN {
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen const unsigned char *field_data;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen input = test_istream_create_data(header, header_len);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen test_begin(t_strdup_printf("http header valid [%d]", i));
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen j = 0; pos = 0; test_istream_set_size(input, 0);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen (parser, &field_name, &field_data, &field_size, &error)) >= 0) {
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen const struct http_header_parse_result *result;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen result = &valid_header_parse_tests[i].fields[j];
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen field_value = t_strndup(field_data, field_size);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen t_strdup_printf("%s: %s", field_name, field_value));
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen t_strdup_printf("%s: %s", field_name, field_value));
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 "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 "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 "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 "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 Sirainenunsigned int invalid_header_parse_test_count = N_ELEMENTS(invalid_header_parse_tests);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainenstatic void test_http_header_parse_invalid(void)
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen unsigned int i;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen for (i = 0; i < invalid_header_parse_test_count; i++) T_BEGIN {
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen const unsigned char *field_data;
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen input = i_stream_create_from_data(header, strlen(header));
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen test_begin(t_strdup_printf("http header invalid [%d]", i));
test_end();
} T_END;
int main(void)
static void (*test_functions[])(void) = {