mirror of
https://github.com/emikulic/darkhttpd.git
synced 2023-08-10 21:13:08 +03:00
Add tests for make_safe_uri.
This commit is contained in:
parent
e6c8b820fd
commit
a0afb2cc8b
4
run-tests.sh
Executable file
4
run-tests.sh
Executable file
@ -0,0 +1,4 @@
|
||||
#!/bin/sh
|
||||
|
||||
(cc test_make_safe_uri.c -o test_make_safe_uri &&
|
||||
./test_make_safe_uri)
|
71
test_make_safe_uri.c
Normal file
71
test_make_safe_uri.c
Normal file
@ -0,0 +1,71 @@
|
||||
#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) {
|
||||
if (out == NULL) {
|
||||
printf("PASS: \"%s\" is unsafe\n", input);
|
||||
return;
|
||||
}
|
||||
else {
|
||||
printf("FAIL: \"%s\" is unsafe, but got \"%s\"\n",
|
||||
input, out);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (out == NULL) {
|
||||
printf("FAIL: \"%s\" should become \"%s\", got unsafe\n",
|
||||
input, expected);
|
||||
return;
|
||||
}
|
||||
|
||||
if (strcmp(out, expected) == 0)
|
||||
printf("PASS: \"%s\" => \"%s\"\n", input, out);
|
||||
else
|
||||
printf("FAIL: \"%s\" => \"%s\", expecting \"%s\"\n",
|
||||
input, out, expected);
|
||||
}
|
||||
|
||||
static char * const tests[] = {
|
||||
"/", "/",
|
||||
"/.", "/",
|
||||
"/./", "/",
|
||||
"/../", 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,
|
||||
NULL
|
||||
};
|
||||
|
||||
int
|
||||
main()
|
||||
{
|
||||
char * const *curr = tests;
|
||||
|
||||
while (curr[0] != NULL) {
|
||||
test(curr[0], curr[1]);
|
||||
curr += 2;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
/* vim:set tabstop=4 shiftwidth=4 expandtab tw=78: */
|
Loading…
Reference in New Issue
Block a user