summaryrefslogtreecommitdiff
path: root/src/user
diff options
context:
space:
mode:
Diffstat (limited to 'src/user')
-rw-r--r--src/user/app/tests/kernel/path.c66
-rw-r--r--src/user/app/tests/tests.c1
-rw-r--r--src/user/app/tests/tests.h1
3 files changed, 68 insertions, 0 deletions
diff --git a/src/user/app/tests/kernel/path.c b/src/user/app/tests/kernel/path.c
new file mode 100644
index 0000000..bc87881
--- /dev/null
+++ b/src/user/app/tests/kernel/path.c
@@ -0,0 +1,66 @@
+#include "../tests.h"
+#include <camellia/path.h>
+#include <string.h>
+
+static void test_path_simplify(void) {
+ const char *testcases[][2] = {
+ {"/", "/"},
+ {"/.", "/"},
+ {"//", "/"},
+ {"/asdf", "/asdf"},
+ {"/asdf/", "/asdf/"},
+ {"/asdf//", "/asdf/"},
+ {"/asdf/./", "/asdf/"},
+ {"/a/./b", "/a/b"},
+ {"/a/./b/", "/a/b/"},
+
+ // some slightly less easy cases
+ {"/asdf/..", "/"},
+ {"/asdf/../", "/"},
+ {"/asdf/.", "/asdf/"},
+ {"/asdf//.", "/asdf/"},
+
+ {"/foo/bar/..", "/foo/"},
+ {"/foo/bar/../baz", "/foo/baz"},
+ {"/foo/bar/../baz/", "/foo/baz/"},
+ {"/foo/bar/xyz/..", "/foo/bar/"},
+ {"/foo/bar/xyz/../", "/foo/bar/"},
+
+ // going under the root or close to it
+ {"/..", NULL},
+ {"/../asdf", NULL},
+ {"/../asdf/", NULL},
+ {"/./a/../..", NULL},
+ {"/a/a/../..", "/"},
+ {"/a/../a/..", "/"},
+ {"/a/../../a", NULL},
+ {"/../a/../a", NULL},
+ {"/../../a/a", NULL},
+ {"/////../..", NULL},
+ {"//a//../..", NULL},
+
+ // relative paths aren't allowed
+ {"relative", NULL},
+ {"some/stuff", NULL},
+ {"./stuff", NULL},
+ {"../stuff", NULL},
+ {"", NULL},
+ };
+
+ char buf[256];
+ for (size_t i = 0; i < sizeof(testcases) / sizeof(testcases[0]); i++) {
+ const char *input = testcases[i][0];
+ const char *expected = testcases[i][1];
+ int len = path_simplify(input, buf, strlen(input));
+ if (expected == NULL) {
+ test(len < 0);
+ } else {
+ // TODO an argument for printing info on test failure
+ test(len == (int)strlen(expected) && !memcmp(expected, buf, len));
+ }
+ }
+}
+
+void r_k_path(void) {
+ run_test(test_path_simplify);
+}
diff --git a/src/user/app/tests/tests.c b/src/user/app/tests/tests.c
index 9cdf81c..2157e60 100644
--- a/src/user/app/tests/tests.c
+++ b/src/user/app/tests/tests.c
@@ -16,6 +16,7 @@ int main(void) {
r_k_fs();
r_k_misc();
r_k_miscsyscall();
+ r_k_path();
r_libc_esemaphore();
r_libc_string();
r_s_printf();
diff --git a/src/user/app/tests/tests.h b/src/user/app/tests/tests.h
index fadbc60..79169c9 100644
--- a/src/user/app/tests/tests.h
+++ b/src/user/app/tests/tests.h
@@ -10,6 +10,7 @@ void run_test(void (*fn)());
void r_k_fs(void);
void r_k_misc(void);
void r_k_miscsyscall(void);
+void r_k_path(void);
void r_libc_esemaphore(void);
void r_libc_string(void);
void r_s_printf(void);