darkhttpd/devel/test_make_safe_uri.c

67 lines
1.5 KiB
C
Raw Normal View History

2006-12-13 11:14:27 +03:00
#define main _main_disabled_
#include "trunk/darkhttpd.c"
#undef main
static void
test(const char *input, const char *expected)
{
char *tmp = xstrdup(input);
char *out = make_safe_uri(tmp);
if (expected == NULL) {
2006-12-13 11:16:16 +03:00
if (out == NULL)
2006-12-13 11:14:27 +03:00
printf("PASS: \"%s\" is unsafe\n", input);
2006-12-13 11:16:16 +03:00
else
2006-12-13 11:14:27 +03:00
printf("FAIL: \"%s\" is unsafe, but got \"%s\"\n",
input, out);
}
2006-12-13 11:16:16 +03:00
else if (out == NULL)
2006-12-13 11:14:27 +03:00
printf("FAIL: \"%s\" should become \"%s\", got unsafe\n",
input, expected);
2006-12-13 11:16:16 +03:00
else if (strcmp(out, expected) == 0)
2006-12-13 11:14:27 +03:00
printf("PASS: \"%s\" => \"%s\"\n", input, out);
else
printf("FAIL: \"%s\" => \"%s\", expecting \"%s\"\n",
input, out, expected);
2006-12-13 11:16:16 +03:00
free(tmp);
2006-12-13 11:14:27 +03:00
}
2006-12-13 11:41:05 +03:00
static char const *tests[] = {
2006-12-13 11:14:27 +03:00
"/", "/",
"/.", "/",
"/./", "/",
"/../", NULL,
"/abc", "/abc",
"/abc/", "/abc/",
"/abc/.", "/abc",
"/abc/./", "/abc/",
"/abc/..", "/",
"/abc/../", "/",
"/abc/../def", "/def",
"/abc/../def/", "/def/",
"/abc/../def/..", "/",
"/abc/../def/../", "/",
"/abc/../def/../../", NULL,
"/abc/../def/.././", "/",
"/abc/../def/.././../", NULL,
"/a/b/c/../../d/", "/a/d/",
"/a/b/../../../c", NULL,
2006-12-13 11:17:56 +03:00
/* don't forget consolidate_slashes */
"//a///b////c/////", "/a/b/c/",
2006-12-13 11:14:27 +03:00
NULL
};
int
2006-12-13 11:41:05 +03:00
main(void)
2006-12-13 11:14:27 +03:00
{
2006-12-13 11:41:05 +03:00
const char **curr = tests;
2006-12-13 11:14:27 +03:00
while (curr[0] != NULL) {
test(curr[0], curr[1]);
curr += 2;
}
return 0;
}
/* vim:set tabstop=4 shiftwidth=4 expandtab tw=78: */