test-http-request-parser.c revision 10962368c30afde135743fd9796122e88a708e87
/* Copyright (c) 2013 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-url.h"
#include "http-request-parser.h"
#include <time.h>
struct http_request_parse_test {
const char *request;
const char *method;
const char *target_raw;
struct http_request_target target;
unsigned char version_major;
unsigned char version_minor;
const char *payload;
};
/* Valid header tests */
static const struct http_request_parse_test
{ .request =
"GET / HTTP/1.1\r\n"
"Host: example.com\r\n"
"\r\n",
.method = "GET",
.target_raw = "/",
.target = {
},
},{ .request =
"OPTIONS * HTTP/1.0\r\n"
"Host: example.com\r\n"
"\r\n",
.method = "OPTIONS",
.target_raw = "*",
.target = {
},
},{ .request =
"CONNECT example.com:443 HTTP/1.2\r\n"
"Host: example.com:443\r\n"
"\r\n",
.method = "CONNECT",
.target_raw = "example.com:443",
.target = {
},
},{ .request =
"GET https://www.example.com:443 HTTP/1.1\r\n"
"Host: www.example.com:80\r\n"
"\r\n",
.method = "GET",
.target = {
},
},{ .request =
"POST http://api.example.com:8080/commit?user=dirk HTTP/1.1\r\n"
"Host: api.example.com:8080\r\n"
"Content-Length: 10\r\n"
"\r\n"
"Content!\r\n",
.method = "POST",
.target = {
},
.payload = "Content!\r\n"
}
};
unsigned int valid_request_parse_test_count =
static const char *
{
switch (target_format) {
return "origin";
return "absolute";
return "authority";
return "asterisk";
}
}
static void test_http_request_parse_valid(void)
{
unsigned int i;
for (i = 0; i < valid_request_parse_test_count; i++) T_BEGIN {
const struct http_request_parse_test *test;
struct http_request_parser *parser;
struct http_request request;
unsigned int pos, request_text_len;
int ret = 0;
test = &valid_request_parse_tests[i];
}
while (ret > 0) {
test_out("payload receive",
} else {
}
}
if (ret == 0) {
/* verify last request only */
} else {
}
} else {
}
} else {
}
}
test_end();
} T_END;
}
static const char *invalid_request_parse_tests[] = {
"GET: / HTTP/1.1\r\n"
"Host: example.com\r\n"
"\r\n",
"GET % HTTP/1.1\r\n"
"Host: example.com\r\n"
"\r\n",
"GET /frop\" HTTP/1.1\r\n"
"Host: example.com\r\n"
"\r\n",
"GET / HTCPCP/1.0\r\n"
"Host: example.com\r\n"
"\r\n",
"GET / HTTP/1.0.1\r\n"
"Host: example.com\r\n"
"\r\n",
"GET / HTTP/1.1\r\n"
"Host: \"example.com\r\n"
"\r\n",
};
static unsigned char invalid_request_with_nuls[] =
"GET / HTTP/1.1\r\n"
"Host: example.com\r\n"
"Null: text\0server\r\n"
"\r\n";
unsigned int invalid_request_parse_test_count =
static void test_http_request_parse_invalid(void)
{
struct http_request_parser *parser;
struct http_request request;
const char *request_text, *error;
int ret;
unsigned int i;
for (i = 0; i < invalid_request_parse_test_count; i++) T_BEGIN {
const char *test;
request_text = test;
while ((ret=http_request_parse_next
test_end();
} T_END;
/* parse failure guarantees http_request_header.size equals
strlen(http_request_header.value) */
test_begin("http request with NULs");
sizeof(invalid_request_with_nuls)-1);
while ((ret=http_request_parse_next
test_assert(ret < 0);
test_end();
}
int main(void)
{
static void (*test_functions[])(void) = {
};
return test_run(test_functions);
}