bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2013-2018 Dovecot authors, see the included COPYING file */
1bd20e2a575473f3d05499f05f1d72da59b34fd6Stephan Bosch const struct http_header_parse_result *fields;
1bd20e2a575473f3d05499f05f1d72da59b34fd6Stephan Bosch/* Valid header tests */
ba592dc74a004ad47dfe58edcfc1ca7297551e39Phil Carmodystatic const struct http_header_parse_result valid_header_parse_result1[] = {
1bd20e2a575473f3d05499f05f1d72da59b34fd6Stephan Bosch { "Last-Modified", "Mon, 30 Jul 2012 11:09:28 GMT" },
ba592dc74a004ad47dfe58edcfc1ca7297551e39Phil Carmodystatic const struct http_header_parse_result valid_header_parse_result2[] = {
1bd20e2a575473f3d05499f05f1d72da59b34fd6Stephan Bosch { "Host", "p5-lrqzb4yavu4l7nagydw-428649-i2-v6exp3-ds.metric.example.com" },
1bd20e2a575473f3d05499f05f1d72da59b34fd6Stephan Bosch { "User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0)" },
1bd20e2a575473f3d05499f05f1d72da59b34fd6Stephan Bosch { "Accept", "image/png,image/*;q=0.8,*/*;q=0.5" },
ba592dc74a004ad47dfe58edcfc1ca7297551e39Phil Carmodystatic const struct http_header_parse_result valid_header_parse_result3[] = {
1bd20e2a575473f3d05499f05f1d72da59b34fd6Stephan Bosch { "Server", "Apache/2.2.16 (Debian) PHP/5.3.3-7+squeeze14 with"
1bd20e2a575473f3d05499f05f1d72da59b34fd6Stephan Bosch " Suhosin-Patch proxy_html/3.0.1 mod_python/3.3.1 Python/2.6.6"
1bd20e2a575473f3d05499f05f1d72da59b34fd6Stephan Bosch " mod_ssl/2.2.16 OpenSSL/0.9.8o mod_perl/2.0.4 Perl/v5.10.1" },
1bd20e2a575473f3d05499f05f1d72da59b34fd6Stephan Bosch { "WWW-Authenticate", "Basic realm=\"Munin\"" },
1bd20e2a575473f3d05499f05f1d72da59b34fd6Stephan Bosch { "Content-Type", "text/html; charset=iso-8859-1" },
ba592dc74a004ad47dfe58edcfc1ca7297551e39Phil Carmodystatic const struct http_header_parse_result valid_header_parse_result4[] = {
9a449fb205b61613586b905ce3ee537486d5bd7cTimo Sirainen { "Expires", "Sun, 04 Aug 2013 09:34:08 GMT" },
9a449fb205b61613586b905ce3ee537486d5bd7cTimo Sirainen { "Last-Modified", "Sun, 04 Aug 2013 09:33:07 GMT" },
9a449fb205b61613586b905ce3ee537486d5bd7cTimo Sirainen { "Content-Type", "text/html; charset=utf-8" },
ba592dc74a004ad47dfe58edcfc1ca7297551e39Phil Carmodystatic const struct http_header_parse_result valid_header_parse_result5[] = {
ba592dc74a004ad47dfe58edcfc1ca7297551e39Phil Carmodystatic const struct http_header_parse_result valid_header_parse_result6[] = {
e0cf44fb802a5e7aa4cbeee5e80ef8f3f6aecdbeStephan Bosch { "X-Frop", "This text\x80 contains obs-text\x81 characters" },
ba592dc74a004ad47dfe58edcfc1ca7297551e39Phil Carmodystatic const struct http_header_parse_result valid_header_parse_result7[] = {
e0cf44fb802a5e7aa4cbeee5e80ef8f3f6aecdbeStephan Bosch { "X-Frop", "This text contains invalid characters" },
1bd20e2a575473f3d05499f05f1d72da59b34fd6Stephan Boschstatic const struct http_header_parse_test valid_header_parse_tests[] = {
1bd20e2a575473f3d05499f05f1d72da59b34fd6Stephan Bosch "Date: Sat, 06 Oct 2012 16:01:44 GMT\r\n"
1bd20e2a575473f3d05499f05f1d72da59b34fd6Stephan Bosch "Server: Apache/2.2.16 (Debian)\r\n"
1bd20e2a575473f3d05499f05f1d72da59b34fd6Stephan Bosch "Last-Modified: Mon, 30 Jul 2012 11:09:28 GMT\r\n"
1bd20e2a575473f3d05499f05f1d72da59b34fd6Stephan Bosch "Etag: \"3d24677-3261-4c60a1863aa00\"\r\n"
1bd20e2a575473f3d05499f05f1d72da59b34fd6Stephan Bosch "Accept-Ranges: bytes\r\n"
1bd20e2a575473f3d05499f05f1d72da59b34fd6Stephan Bosch "Vary: Accept-Encoding\r\n"
1bd20e2a575473f3d05499f05f1d72da59b34fd6Stephan Bosch "Content-Encoding: gzip\r\n"
1bd20e2a575473f3d05499f05f1d72da59b34fd6Stephan Bosch "Content-Length: 4092\r\n"
1bd20e2a575473f3d05499f05f1d72da59b34fd6Stephan Bosch "Keep-Alive: timeout=15, max=100\r\n"
1bd20e2a575473f3d05499f05f1d72da59b34fd6Stephan Bosch "Connection: Keep-Alive\r\n"
1bd20e2a575473f3d05499f05f1d72da59b34fd6Stephan Bosch "Content-Type: text/html\r\n"
1bd20e2a575473f3d05499f05f1d72da59b34fd6Stephan Bosch "Host: p5-lrqzb4yavu4l7nagydw-428649-i2-v6exp3-ds.metric.example.com\n"
1bd20e2a575473f3d05499f05f1d72da59b34fd6Stephan Bosch "User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0)\n"
1bd20e2a575473f3d05499f05f1d72da59b34fd6Stephan Bosch "Accept:\t\timage/png,image/*;q=0.8,*/*;q=0.5\n"
1bd20e2a575473f3d05499f05f1d72da59b34fd6Stephan Bosch "Accept-Language:\ten-us,en;q=0.5\n"
1bd20e2a575473f3d05499f05f1d72da59b34fd6Stephan Bosch "Accept-Encoding: \t\tgzip, deflate\n"
1bd20e2a575473f3d05499f05f1d72da59b34fd6Stephan Bosch "Connection: \t\tkeep-alive\n"
1bd20e2a575473f3d05499f05f1d72da59b34fd6Stephan Bosch "Date: Sat, 06 Oct 2012 17:12:37 GMT\r\n"
1bd20e2a575473f3d05499f05f1d72da59b34fd6Stephan Bosch "Server: Apache/2.2.16 (Debian) PHP/5.3.3-7+squeeze14 with\r\n"
1bd20e2a575473f3d05499f05f1d72da59b34fd6Stephan Bosch " Suhosin-Patch proxy_html/3.0.1 mod_python/3.3.1 Python/2.6.6\r\n"
1bd20e2a575473f3d05499f05f1d72da59b34fd6Stephan Bosch " mod_ssl/2.2.16 OpenSSL/0.9.8o mod_perl/2.0.4 Perl/v5.10.1\r\n"
1bd20e2a575473f3d05499f05f1d72da59b34fd6Stephan Bosch "WWW-Authenticate: Basic realm=\"Munin\"\r\n"
1bd20e2a575473f3d05499f05f1d72da59b34fd6Stephan Bosch "Vary: Accept-Encoding\r\n"
1bd20e2a575473f3d05499f05f1d72da59b34fd6Stephan Bosch "Content-Encoding: gzip\r\n"
1bd20e2a575473f3d05499f05f1d72da59b34fd6Stephan Bosch "Content-Length: 445\r\n"
1bd20e2a575473f3d05499f05f1d72da59b34fd6Stephan Bosch "Keep-Alive: timeout=15, max=98\r\n"
1bd20e2a575473f3d05499f05f1d72da59b34fd6Stephan Bosch "Connection: Keep-Alive\r\n"
1bd20e2a575473f3d05499f05f1d72da59b34fd6Stephan Bosch "Content-Type: text/html; charset=iso-8859-1\r\n"
9a449fb205b61613586b905ce3ee537486d5bd7cTimo Sirainen "Age: 58 \r\n"
9a449fb205b61613586b905ce3ee537486d5bd7cTimo Sirainen "Date: Sun, 04 Aug 2013 09:33:09 GMT\r\n"
9a449fb205b61613586b905ce3ee537486d5bd7cTimo Sirainen "Expires: Sun, 04 Aug 2013 09:34:08 GMT\r\n"
9a449fb205b61613586b905ce3ee537486d5bd7cTimo Sirainen "Cache-Control: max-age=60 \r\n"
9a449fb205b61613586b905ce3ee537486d5bd7cTimo Sirainen "Content-Length: 17336 \r\n"
9a449fb205b61613586b905ce3ee537486d5bd7cTimo Sirainen "Connection: Keep-Alive\r\n"
9a449fb205b61613586b905ce3ee537486d5bd7cTimo Sirainen "Via: NS-CACHE-9.3\r\n"
9a449fb205b61613586b905ce3ee537486d5bd7cTimo Sirainen "Server: Apache\r\n"
9a449fb205b61613586b905ce3ee537486d5bd7cTimo Sirainen "Vary: Host\r\n"
9a449fb205b61613586b905ce3ee537486d5bd7cTimo Sirainen "Last-Modified: Sun, 04 Aug 2013 09:33:07 GMT\r\n"
9a449fb205b61613586b905ce3ee537486d5bd7cTimo Sirainen "Content-Type: text/html; charset=utf-8\r\n"
9a449fb205b61613586b905ce3ee537486d5bd7cTimo Sirainen "Content-Encoding: gzip\r\n"
e0cf44fb802a5e7aa4cbeee5e80ef8f3f6aecdbeStephan Bosch "X-Frop: This text\x80 contains obs-text\x81 characters\r\n"
e0cf44fb802a5e7aa4cbeee5e80ef8f3f6aecdbeStephan Bosch "X-Frop: This text\x01 contains invalid\x7f characters\r\n"
ba592dc74a004ad47dfe58edcfc1ca7297551e39Phil Carmodystatic const unsigned int valid_header_parse_test_count = N_ELEMENTS(valid_header_parse_tests);
1bd20e2a575473f3d05499f05f1d72da59b34fd6Stephan Bosch unsigned int i;
1bd20e2a575473f3d05499f05f1d72da59b34fd6Stephan Bosch for (i = 0; i < valid_header_parse_test_count; i++) T_BEGIN {
feba5e502b2131c9a1c766b7ef9ff041dbf71d1dStephan Bosch const char *header, *field_name, *error = NULL;
d1f1ad02d1ed5d83c62fba356259b7c9073608c6Timo Sirainen input = test_istream_create_data(header, header_len);
7ebcb054e0d3cc4be54038cbf763ec4189d9725bStephan Bosch parser = http_header_parser_init(input, limits,
1bd20e2a575473f3d05499f05f1d72da59b34fd6Stephan Bosch test_begin(t_strdup_printf("http header valid [%d]", i));
d1f1ad02d1ed5d83c62fba356259b7c9073608c6Timo Sirainen j = 0; pos = 0; test_istream_set_size(input, 0);
d1f1ad02d1ed5d83c62fba356259b7c9073608c6Timo Sirainen (parser, &field_name, &field_data, &field_size, &error)) >= 0) {
1bd20e2a575473f3d05499f05f1d72da59b34fd6Stephan Bosch const struct http_header_parse_result *result;
1bd20e2a575473f3d05499f05f1d72da59b34fd6Stephan Bosch result = &valid_header_parse_tests[i].fields[j];
1bd20e2a575473f3d05499f05f1d72da59b34fd6Stephan Bosch field_value = t_strndup(field_data, field_size);
e0cf44fb802a5e7aa4cbeee5e80ef8f3f6aecdbeStephan Bosch test_out_reason("valid", FALSE, t_strdup_printf
e0cf44fb802a5e7aa4cbeee5e80ef8f3f6aecdbeStephan Bosch ("%s: %s", field_name, str_sanitize(field_value, 100)));
feba5e502b2131c9a1c766b7ef9ff041dbf71d1dStephan Bosch test_out_reason("parse success", ret > 0, error);
feba5e502b2131c9a1c766b7ef9ff041dbf71d1dStephan Boschstatic const struct http_header_parse_test invalid_header_parse_tests[] = {
feba5e502b2131c9a1c766b7ef9ff041dbf71d1dStephan Bosch "Date: Sat, 06 Oct 2012 16:01:44 GMT\r\n"
feba5e502b2131c9a1c766b7ef9ff041dbf71d1dStephan Bosch "Server : Apache/2.2.16 (Debian)\r\n"
feba5e502b2131c9a1c766b7ef9ff041dbf71d1dStephan Bosch "Last-Modified: Mon, 30 Jul 2012 11:09:28 GMT\r\n"
feba5e502b2131c9a1c766b7ef9ff041dbf71d1dStephan Bosch "Date: Sat, 06 Oct 2012 17:18:22 GMT\r\n"
feba5e502b2131c9a1c766b7ef9ff041dbf71d1dStephan Bosch "Server: Apache/2.2.3 (CentOS)\r\n"
feba5e502b2131c9a1c766b7ef9ff041dbf71d1dStephan Bosch "X Powered By: PHP/5.3.6\r\n"
feba5e502b2131c9a1c766b7ef9ff041dbf71d1dStephan Bosch "Accept: image/png,image/*;q=0.8,*/*;q=0.5\n\r"
feba5e502b2131c9a1c766b7ef9ff041dbf71d1dStephan Bosch "Accept-Language: en-us,en;q=0.5\n\r"
feba5e502b2131c9a1c766b7ef9ff041dbf71d1dStephan Bosch "Accept-Encoding: gzip, deflate\n\r"
feba5e502b2131c9a1c766b7ef9ff041dbf71d1dStephan Bosch "Host: p5-lrqzb4yavu4l7nagydw-428649-i2-v6exp3-ds.metric.example.com\n"
feba5e502b2131c9a1c766b7ef9ff041dbf71d1dStephan Bosch "User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0)\n"
e0cf44fb802a5e7aa4cbeee5e80ef8f3f6aecdbeStephan Bosch "Accept:\t\timage/png,image/*;q=0.8,*/\1;q=0.5\n"
e0cf44fb802a5e7aa4cbeee5e80ef8f3f6aecdbeStephan Bosch "Date: Sat, 06 Oct 2012 17:18:22 GMT\r\n"
e0cf44fb802a5e7aa4cbeee5e80ef8f3f6aecdbeStephan Bosch "Server: Apache/2.2.3\177 (CentOS)\r\n"
feba5e502b2131c9a1c766b7ef9ff041dbf71d1dStephan Bosch "Date: Sat, 06 Oct 2012 17:12:37 GMT\r\n"
feba5e502b2131c9a1c766b7ef9ff041dbf71d1dStephan Bosch "Server: Apache/2.2.16 (Debian) PHP/5.3.3-7+squeeze14 with\r\n"
feba5e502b2131c9a1c766b7ef9ff041dbf71d1dStephan Bosch "Suhosin-Patch proxy_html/3.0.1 mod_python/3.3.1 Python/2.6.6\r\n"
feba5e502b2131c9a1c766b7ef9ff041dbf71d1dStephan Bosch "mod_ssl/2.2.16 OpenSSL/0.9.8o mod_perl/2.0.4 Perl/v5.10.1\r\n"
e0cf44fb802a5e7aa4cbeee5e80ef8f3f6aecdbeStephan Bosch "Date: Sat, 06 Oct 2012 17:12:37 GMT\r\n"
feba5e502b2131c9a1c766b7ef9ff041dbf71d1dStephan Bosch "Age: 58 \r\n"
feba5e502b2131c9a1c766b7ef9ff041dbf71d1dStephan Bosch "Date: Sun, 04 Aug 2013 09:33:09 GMT\r\n"
feba5e502b2131c9a1c766b7ef9ff041dbf71d1dStephan Bosch "Expires: Sun, 04 Aug 2013 09:34:08 GMT\r\n"
feba5e502b2131c9a1c766b7ef9ff041dbf71d1dStephan Bosch "Cache-Control: max-age=60 \r\n"
feba5e502b2131c9a1c766b7ef9ff041dbf71d1dStephan Bosch "Content-Length: 17336 \r\n"
feba5e502b2131c9a1c766b7ef9ff041dbf71d1dStephan Bosch "Connection: Keep-Alive\r\n"
feba5e502b2131c9a1c766b7ef9ff041dbf71d1dStephan Bosch "Via: NS-CACHE-9.3\r\n"
feba5e502b2131c9a1c766b7ef9ff041dbf71d1dStephan Bosch "Server: Apache\r\n"
feba5e502b2131c9a1c766b7ef9ff041dbf71d1dStephan Bosch "Vary: Host\r\n"
feba5e502b2131c9a1c766b7ef9ff041dbf71d1dStephan Bosch "Last-Modified: Sun, 04 Aug 2013 09:33:07 GMT\r\n"
feba5e502b2131c9a1c766b7ef9ff041dbf71d1dStephan Bosch "Content-Type: text/html; charset=utf-8\r\n"
feba5e502b2131c9a1c766b7ef9ff041dbf71d1dStephan Bosch "Content-Encoding: gzip\r\n"
feba5e502b2131c9a1c766b7ef9ff041dbf71d1dStephan Bosch "Age: 58 \r\n"
feba5e502b2131c9a1c766b7ef9ff041dbf71d1dStephan Bosch "Date: Sun, 04 Aug 2013 09:33:09 GMT\r\n"
feba5e502b2131c9a1c766b7ef9ff041dbf71d1dStephan Bosch "Expires: Sun, 04 Aug 2013 09:34:08 GMT\r\n"
feba5e502b2131c9a1c766b7ef9ff041dbf71d1dStephan Bosch "Cache-Control: max-age=60 \r\n"
feba5e502b2131c9a1c766b7ef9ff041dbf71d1dStephan Bosch "Content-Length: 17336 \r\n"
feba5e502b2131c9a1c766b7ef9ff041dbf71d1dStephan Bosch "Connection: Keep-Alive\r\n"
feba5e502b2131c9a1c766b7ef9ff041dbf71d1dStephan Bosch "Via: NS-CACHE-9.3\r\n"
feba5e502b2131c9a1c766b7ef9ff041dbf71d1dStephan Bosch "Server: Apache\r\n"
feba5e502b2131c9a1c766b7ef9ff041dbf71d1dStephan Bosch "Vary: Host\r\n"
feba5e502b2131c9a1c766b7ef9ff041dbf71d1dStephan Bosch "Last-Modified: Sun, 04 Aug 2013 09:33:07 GMT\r\n"
feba5e502b2131c9a1c766b7ef9ff041dbf71d1dStephan Bosch "Content-Type: text/html; charset=utf-8\r\n"
feba5e502b2131c9a1c766b7ef9ff041dbf71d1dStephan Bosch "Content-Encoding: gzip\r\n"
feba5e502b2131c9a1c766b7ef9ff041dbf71d1dStephan Bosch "Age: 58 \r\n"
feba5e502b2131c9a1c766b7ef9ff041dbf71d1dStephan Bosch "Date: Sun, 04 Aug 2013 09:33:09 GMT\r\n"
feba5e502b2131c9a1c766b7ef9ff041dbf71d1dStephan Bosch "Expires: Sun, 04 Aug 2013 09:34:08 GMT\r\n"
feba5e502b2131c9a1c766b7ef9ff041dbf71d1dStephan Bosch "Cache-Control: max-age=60 \r\n"
feba5e502b2131c9a1c766b7ef9ff041dbf71d1dStephan Bosch "Content-Length: 17336 \r\n"
feba5e502b2131c9a1c766b7ef9ff041dbf71d1dStephan Bosch "Connection: Keep-Alive\r\n"
feba5e502b2131c9a1c766b7ef9ff041dbf71d1dStephan Bosch "Via: NS-CACHE-9.3\r\n"
feba5e502b2131c9a1c766b7ef9ff041dbf71d1dStephan Bosch "Server: Apache\r\n"
feba5e502b2131c9a1c766b7ef9ff041dbf71d1dStephan Bosch "Vary: Host\r\n"
feba5e502b2131c9a1c766b7ef9ff041dbf71d1dStephan Bosch "Last-Modified: Sun, 04 Aug 2013 09:33:07 GMT\r\n"
feba5e502b2131c9a1c766b7ef9ff041dbf71d1dStephan Bosch "Content-Type: text/html; charset=utf-8\r\n"
feba5e502b2131c9a1c766b7ef9ff041dbf71d1dStephan Bosch "Content-Encoding: gzip\r\n"
ba592dc74a004ad47dfe58edcfc1ca7297551e39Phil Carmodystatic const unsigned int invalid_header_parse_test_count = N_ELEMENTS(invalid_header_parse_tests);
1bd20e2a575473f3d05499f05f1d72da59b34fd6Stephan Boschstatic void test_http_header_parse_invalid(void)
1bd20e2a575473f3d05499f05f1d72da59b34fd6Stephan Bosch unsigned int i;
1bd20e2a575473f3d05499f05f1d72da59b34fd6Stephan Bosch for (i = 0; i < invalid_header_parse_test_count; i++) T_BEGIN {
feba5e502b2131c9a1c766b7ef9ff041dbf71d1dStephan Bosch const char *header, *field_name, *error = NULL;
feba5e502b2131c9a1c766b7ef9ff041dbf71d1dStephan Bosch header = invalid_header_parse_tests[i].header;
feba5e502b2131c9a1c766b7ef9ff041dbf71d1dStephan Bosch limits = &invalid_header_parse_tests[i].limits;
1bd20e2a575473f3d05499f05f1d72da59b34fd6Stephan Bosch input = i_stream_create_from_data(header, strlen(header));
7ebcb054e0d3cc4be54038cbf763ec4189d9725bStephan Bosch parser = http_header_parser_init(input, limits,
1bd20e2a575473f3d05499f05f1d72da59b34fd6Stephan Bosch test_begin(t_strdup_printf("http header invalid [%d]", i));
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch (parser, &field_name, &field_data, &field_size, &error)) > 0) {
feba5e502b2131c9a1c766b7ef9ff041dbf71d1dStephan Bosch test_out_reason("parse failure", ret < 0, error);
baf3e87e186453fda13bd21f7cbcb2efc8492e8bTimo Sirainen static void (*const test_functions[])(void) = {