test-http-header-parser.c revision 1bd20e2a575473f3d05499f05f1d72da59b34fd6
/* Copyright (c) 2012 Dovecot authors, see the included COPYING file */
#include "test-lib.h"
#include "istream.h"
#include "test-common.h"
#include "http-header-parser.h"
#include <time.h>
struct http_header_parse_result {
const char *name;
const char *value;
};
struct http_header_parse_test {
const char *header;
const struct http_header_parse_result *fields;
};
/* Valid header tests */
static struct http_header_parse_result valid_header_parse_result1[] = {
{ "Date", "Sat, 06 Oct 2012 16:01:44 GMT" },
{ "Server", "Apache/2.2.16 (Debian)" },
{ "Last-Modified", "Mon, 30 Jul 2012 11:09:28 GMT" },
{ "Etag", "\"3d24677-3261-4c60a1863aa00\"" },
{ "Accept-Ranges", "bytes" },
{ "Vary", "Accept-Encoding" },
{ "Content-Encoding", "gzip" },
{ "Content-Length", "4092" },
{ "Keep-Alive", "timeout=15, max=100" },
{ "Connection", "Keep-Alive" },
};
static struct http_header_parse_result valid_header_parse_result2[] = {
{ "Host", "p5-lrqzb4yavu4l7nagydw-428649-i2-v6exp3-ds.metric.example.com" },
{ "User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0)" },
{ "Accept-Language", "en-us,en;q=0.5" },
{ "Accept-Encoding", "gzip, deflate" },
{ "DNT", "1" },
{ "Connection", "keep-alive" },
{ "Referer", "http://www.example.nl/" },
};
static struct http_header_parse_result valid_header_parse_result3[] = {
{ "Date", "Sat, 06 Oct 2012 17:12:37 GMT" },
{ "Server", "Apache/2.2.16 (Debian) PHP/5.3.3-7+squeeze14 with"
" Suhosin-Patch proxy_html/3.0.1 mod_python/3.3.1 Python/2.6.6"
{ "WWW-Authenticate", "Basic realm=\"Munin\"" },
{ "Vary", "Accept-Encoding" },
{ "Content-Encoding", "gzip" },
{ "Content-Length", "445" },
{ "Keep-Alive", "timeout=15, max=98" },
{ "Connection", "Keep-Alive" },
};
static const struct http_header_parse_test valid_header_parse_tests[] = {
{ .header =
"Date: Sat, 06 Oct 2012 16:01:44 GMT\r\n"
"Server: Apache/2.2.16 (Debian)\r\n"
"Last-Modified: Mon, 30 Jul 2012 11:09:28 GMT\r\n"
"Etag: \"3d24677-3261-4c60a1863aa00\"\r\n"
"Accept-Ranges: bytes\r\n"
"Vary: Accept-Encoding\r\n"
"Content-Encoding: gzip\r\n"
"Content-Length: 4092\r\n"
"Keep-Alive: timeout=15, max=100\r\n"
"Connection: Keep-Alive\r\n"
"\r\n",
},{
.header =
"User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0)\n"
"Accept-Language:\ten-us,en;q=0.5\n"
"Accept-Encoding: \t\tgzip, deflate\n"
"DNT: 1\n"
"Connection: \t\tkeep-alive\n"
"Referer: http://www.example.nl/\n"
"\n",
},{
.header =
"Date: Sat, 06 Oct 2012 17:12:37 GMT\r\n"
"Server: Apache/2.2.16 (Debian) PHP/5.3.3-7+squeeze14 with\r\n"
" Suhosin-Patch proxy_html/3.0.1 mod_python/3.3.1 Python/2.6.6\r\n"
"WWW-Authenticate: Basic realm=\"Munin\"\r\n"
"Vary: Accept-Encoding\r\n"
"Content-Encoding: gzip\r\n"
"Content-Length: 445\r\n"
"Keep-Alive: timeout=15, max=98\r\n"
"Connection: Keep-Alive\r\n"
"\r\n",
}
};
static void test_http_header_parse_valid(void)
{
unsigned int i;
for (i = 0; i < valid_header_parse_test_count; i++) T_BEGIN {
struct http_header_parser *parser;
const char *header, *field_name;
const unsigned char *field_data;
int ret;
int j;
j = 0;
while ((ret=http_header_parse_next_field
const struct http_header_parse_result *result;
const char *field_value;
if (field_name == NULL) break;
break;
}
test_out_reason("valid",
j++;
}
test_end();
} T_END;
}
static const char *invalid_header_parse_tests[] = {
"Date: Sat, 06 Oct 2012 16:01:44 GMT\r\n"
"Server : Apache/2.2.16 (Debian)\r\n"
"Last-Modified: Mon, 30 Jul 2012 11:09:28 GMT\r\n"
"\r\n",
"Date: Sat, 06 Oct 2012 17:18:22 GMT\r\n"
"Server: Apache/2.2.3 (CentOS)\r\n"
"X Powered By: PHP/5.3.6\r\n"
"\r\n",
"Host: www.example.com\n\r"
"Accept-Language: en-us,en;q=0.5\n\r"
"Accept-Encoding: gzip, deflate\n\r"
"\n\r",
"User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0)\n"
"\n",
"Date: Sat, 06 Oct 2012 17:12:37 GMT\r\n"
"Server: Apache/2.2.16 (Debian) PHP/5.3.3-7+squeeze14 with\r\n"
"Suhosin-Patch proxy_html/3.0.1 mod_python/3.3.1 Python/2.6.6\r\n"
"\r\n",
};
static void test_http_header_parse_invalid(void)
{
unsigned int i;
for (i = 0; i < invalid_header_parse_test_count; i++) T_BEGIN {
struct http_header_parser *parser;
const char *header, *field_name;
const unsigned char *field_data;
int ret;
while ((ret=http_header_parse_next_field
if (field_name == NULL) break;
}
test_end();
} T_END;
}
int main(void)
{
static void (*test_functions[])(void) = {
};
return test_run(test_functions);
}