darkhttpd/devel/test_make_safe_uri.c

70 lines
1.6 KiB
C
Raw Normal View History

2006-12-13 11:14:27 +03:00
#define main _main_disabled_
2011-01-18 16:48:59 +03:00
#include "../darkhttpd.c"
2006-12-13 11:14:27 +03:00
#undef main
static void
test(const char *input, const char *expected)
{
char *tmp = xstrdup(input);
2011-04-16 12:52:53 +04:00
char *out = make_safe_url(tmp);
2006-12-13 11:14:27 +03:00
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[] = {
"", NULL,
2006-12-13 11:14:27 +03:00
"/", "/",
"/.", "/",
"/./", "/",
2021-03-21 07:31:04 +03:00
"/.d", "/.d",
"//.d", "/.d",
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: */