Skip to content
This repository has been archived by the owner on Jun 30, 2021. It is now read-only.

Commit

Permalink
evhtp_unescape_string tests
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanFrench committed Aug 9, 2018
1 parent 5f74f32 commit ae731f1
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 29 deletions.
68 changes: 44 additions & 24 deletions examples/test_htparse.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ struct testobj {


static int
_msg_begin(htparser * p) {
_msg_begin(htparser * p)
{
char * buf = (char *)htparser_get_userdata(p);

strcat(buf, "START\n");
Expand All @@ -31,7 +32,8 @@ _msg_begin(htparser * p) {
}

static int
_method(htparser * p, const char * b, size_t s) {
_method(htparser * p, const char * b, size_t s)
{
char * buf = (char *)htparser_get_userdata(p);

if (htparser_get_method(p) == htp_method_UNKNOWN) {
Expand All @@ -45,7 +47,8 @@ _method(htparser * p, const char * b, size_t s) {
}

static int
_scheme(htparser * p, const char * b, size_t s) {
_scheme(htparser * p, const char * b, size_t s)
{
char * buf = (char *)htparser_get_userdata(p);

ADD_DATA_BUF(buf, "SCHEME", b, s);
Expand All @@ -54,7 +57,8 @@ _scheme(htparser * p, const char * b, size_t s) {
}

static int
_host(htparser * p, const char * b, size_t s) {
_host(htparser * p, const char * b, size_t s)
{
char * buf = (char *)htparser_get_userdata(p);

ADD_DATA_BUF(buf, "HOST", b, s);
Expand All @@ -63,7 +67,8 @@ _host(htparser * p, const char * b, size_t s) {
}

static int
_port(htparser * p, const char * b, size_t s) {
_port(htparser * p, const char * b, size_t s)
{
char * buf = (char *)htparser_get_userdata(p);

ADD_DATA_BUF(buf, "PORT", b, s);
Expand All @@ -72,7 +77,8 @@ _port(htparser * p, const char * b, size_t s) {
}

static int
_path(htparser * p, const char * b, size_t s) {
_path(htparser * p, const char * b, size_t s)
{
char * buf = (char *)htparser_get_userdata(p);

ADD_DATA_BUF(buf, "PATH", b, s);
Expand All @@ -81,7 +87,8 @@ _path(htparser * p, const char * b, size_t s) {
}

static int
_args(htparser * p, const char * b, size_t s) {
_args(htparser * p, const char * b, size_t s)
{
char * buf = (char *)htparser_get_userdata(p);

ADD_DATA_BUF(buf, "ARGS", b, s);
Expand All @@ -90,7 +97,8 @@ _args(htparser * p, const char * b, size_t s) {
}

static int
_uri(htparser * p, const char * b, size_t s) {
_uri(htparser * p, const char * b, size_t s)
{
char * buf = (char *)htparser_get_userdata(p);

ADD_DATA_BUF(buf, "URI", b, s);
Expand All @@ -99,7 +107,8 @@ _uri(htparser * p, const char * b, size_t s) {
}

static int
_hdrs_begin(htparser * p) {
_hdrs_begin(htparser * p)
{
char * buf = (char *)htparser_get_userdata(p);

strcat(buf, "HDRS_BEGIN\n");
Expand All @@ -108,7 +117,8 @@ _hdrs_begin(htparser * p) {
}

static int
_hdr_key(htparser * p, const char * b, size_t s) {
_hdr_key(htparser * p, const char * b, size_t s)
{
char * buf = (char *)htparser_get_userdata(p);

ADD_DATA_BUF(buf, "HDR_KEY", b, s);
Expand All @@ -117,7 +127,8 @@ _hdr_key(htparser * p, const char * b, size_t s) {
}

static int
_hdr_val(htparser * p, const char * b, size_t s) {
_hdr_val(htparser * p, const char * b, size_t s)
{
char * buf = (char *)htparser_get_userdata(p);

ADD_DATA_BUF(buf, "HDR_VAL", b, s);
Expand All @@ -126,7 +137,8 @@ _hdr_val(htparser * p, const char * b, size_t s) {
}

static int
_hostname(htparser * p, const char * b, size_t s) {
_hostname(htparser * p, const char * b, size_t s)
{
char * buf = (char *)htparser_get_userdata(p);

ADD_DATA_BUF(buf, "HOSTNAME", b, s);
Expand All @@ -135,7 +147,8 @@ _hostname(htparser * p, const char * b, size_t s) {
}

static int
_hdrs_complete(htparser * p) {
_hdrs_complete(htparser * p)
{
char * buf = (char *)htparser_get_userdata(p);

strcat(buf, "HDRS_COMPLETE\n");
Expand All @@ -144,7 +157,8 @@ _hdrs_complete(htparser * p) {
}

static int
_new_chunk(htparser * p) {
_new_chunk(htparser * p)
{
char * buf = (char *)htparser_get_userdata(p);
char tbuf[1024] = { 0 };

Expand All @@ -155,7 +169,8 @@ _new_chunk(htparser * p) {
}

static int
_chunk_complete(htparser * p) {
_chunk_complete(htparser * p)
{
char * buf = (char *)htparser_get_userdata(p);

strcat(buf, "END_CHUNK\n");
Expand All @@ -164,7 +179,8 @@ _chunk_complete(htparser * p) {
}

static int
_chunks_complete(htparser * p) {
_chunks_complete(htparser * p)
{
char * buf = (char *)htparser_get_userdata(p);

strcat(buf, "END_CHUNKS\n");
Expand All @@ -173,7 +189,8 @@ _chunks_complete(htparser * p) {
}

static int
_body(htparser * p, const char * b, size_t s) {
_body(htparser * p, const char * b, size_t s)
{
char * buf = (char *)htparser_get_userdata(p);

ADD_DATA_BUF(buf, "BODY", b, s);
Expand All @@ -182,15 +199,16 @@ _body(htparser * p, const char * b, size_t s) {
}

static int
_msg_complete(htparser * p) {
_msg_complete(htparser * p)
{
char * buf = (char *)htparser_get_userdata(p);

strcat(buf, "MSG_COMPLETE\n");

return 0;
}

htparse_hooks hooks = {
htparse_hooks hooks = {
.on_msg_begin = _msg_begin,
.method = _method,
.scheme = _scheme,
Expand Down Expand Up @@ -246,7 +264,7 @@ struct testobj t5 = {
.name = "POST request with multiple chunks",
.type = htp_type_request,
.data = "POST /test/ HTTP/1.1\r\n"
"Connection: Keep-Alive\r\n"
"Connection: Keep-Alive\r\n"
"Transfer-Encoding: chunked\r\n\r\n"
"23\r\n"
"This is the data in the first chunk"
Expand Down Expand Up @@ -345,7 +363,8 @@ struct testobj t15 = {
};

struct testobj t16 = {
.name = "GET request which should run all callbacks minus scheme stuff, this includes multiple requests",
.name =
"GET request which should run all callbacks minus scheme stuff, this includes multiple requests",
.type = htp_type_request,
.data = "GET /test1?a=b&c=d&e=f HTTP/1.1\r\n"
"Content-Length: 6\r\n\r\n"
Expand Down Expand Up @@ -443,7 +462,8 @@ struct testobj t22 = {


static int
_run_test(htparser * p, struct testobj * obj) {
_run_test(htparser * p, struct testobj * obj)
{
size_t data_sz;
size_t parsed_sz;
char result_buf[5000] = { 0 };
Expand All @@ -467,7 +487,8 @@ _run_test(htparser * p, struct testobj * obj) {
}

int
main(int argc, char **argv) {
main(int argc, char ** argv)
{
htparser * parser;

parser = htparser_new();
Expand Down Expand Up @@ -498,4 +519,3 @@ main(int argc, char **argv) {

return 0;
}

22 changes: 17 additions & 5 deletions examples/test_query.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ struct test {
};

static int
test_cmp(evhtp_query_t * query, evhtp_kv_t * kvobj, const char * valstr, struct expected * exp) {
test_cmp(evhtp_query_t * query, evhtp_kv_t * kvobj, const char * valstr, struct expected * exp)
{
if (!query || !kvobj) {
return -1;
}
Expand Down Expand Up @@ -43,7 +44,8 @@ test_cmp(evhtp_query_t * query, evhtp_kv_t * kvobj, const char * valstr, struct

/* evhtp_kvs_iterator */
int
kvs_print(evhtp_kv_t * kvobj, void * arg) {
kvs_print(evhtp_kv_t * kvobj, void * arg)
{
int * key_idx = arg;

if (*key_idx) {
Expand All @@ -61,7 +63,8 @@ kvs_print(evhtp_kv_t * kvobj, void * arg) {
}

static int
query_test(const char * raw_query, int exp_error, struct expected exp[], int flags) {
query_test(const char * raw_query, int exp_error, struct expected exp[], int flags)
{
evhtp_query_t * query;
struct expected * check;
int key_idx = 0;
Expand Down Expand Up @@ -210,15 +213,17 @@ struct test lenient_tests[] = {
};

static void
test(const char * raw_query, int exp_error, struct expected exp[], int flags) {
test(const char * raw_query, int exp_error, struct expected exp[], int flags)
{
printf(" %-30s ", raw_query);
printf("\r %s\n", query_test(raw_query, exp_error, exp, flags) ? "ERROR" : "OK");
}

#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))

int
main(int argc, char ** argv) {
main(int argc, char ** argv)
{
int i;

#define PARSE_QUERY_TEST(tests, flags) do { \
Expand All @@ -235,5 +240,12 @@ main(int argc, char ** argv) {
PARSE_QUERY_TEST(treat_semicolon_as_sep_tests, EVHTP_PARSE_QUERY_FLAG_TREAT_SEMICOLON_AS_SEP);
PARSE_QUERY_TEST(lenient_tests, EVHTP_PARSE_QUERY_FLAG_LENIENT);

unsigned char unescaped_string[1024] = { 0 };
unsigned char * escaped_string = "foo?bar=foo%23bar&baz=ONE%20TWO%20&%20THRee%20-foUr";

unsigned char *ptr = unescaped_string;
evhtp_unescape_string(&ptr, escaped_string, strlen(escaped_string));
printf("%s\n", ptr);

return 0;
}

0 comments on commit ae731f1

Please sign in to comment.