From 27124aab29d54ba9f228dee18a48e903e222812a Mon Sep 17 00:00:00 2001 From: dzwdz Date: Fri, 5 Aug 2022 14:01:42 +0200 Subject: move path_simplify to shared code, move its tests to userland --- src/kernel/syscalls.c | 2 +- src/kernel/tests/vfs.c | 63 ------------------------------------ src/kernel/vfs/path.c | 58 --------------------------------- src/kernel/vfs/path.h | 9 ------ src/shared/include/camellia/path.h | 7 ++++ src/shared/path.c | 61 +++++++++++++++++++++++++++++++++++ src/user/app/tests/kernel/path.c | 66 ++++++++++++++++++++++++++++++++++++++ src/user/app/tests/tests.c | 1 + src/user/app/tests/tests.h | 1 + 9 files changed, 137 insertions(+), 131 deletions(-) delete mode 100644 src/kernel/vfs/path.c delete mode 100644 src/kernel/vfs/path.h create mode 100644 src/shared/path.c create mode 100644 src/user/app/tests/kernel/path.c (limited to 'src') diff --git a/src/kernel/syscalls.c b/src/kernel/syscalls.c index 0ab6106..3096c42 100644 --- a/src/kernel/syscalls.c +++ b/src/kernel/syscalls.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -8,7 +9,6 @@ #include #include #include -#include #include #include diff --git a/src/kernel/tests/vfs.c b/src/kernel/tests/vfs.c index 7b353e8..9f7c6da 100644 --- a/src/kernel/tests/vfs.c +++ b/src/kernel/tests/vfs.c @@ -2,70 +2,8 @@ #include #include #include -#include #include -TEST(path_simplify) { -#define TEST_WRAPPER(argument, result) do { \ - int len = path_simplify(argument, buf, sizeof(argument) - 1); \ - if (result == NULL) { \ - TEST_COND(len < 0); \ - } else { \ - if (len == sizeof(result) - 1) { \ - TEST_COND(0 == memcmp(result, buf, len)); \ - } else { \ - TEST_COND(false); \ - } \ - } \ - } while (0) - - char buf[256]; - - // some easy cases first - TEST_WRAPPER("/", "/"); - TEST_WRAPPER("/.", "/"); - TEST_WRAPPER("//", "/"); - TEST_WRAPPER("/asdf", "/asdf"); - TEST_WRAPPER("/asdf/", "/asdf/"); - TEST_WRAPPER("/asdf//", "/asdf/"); - TEST_WRAPPER("/asdf/./", "/asdf/"); - TEST_WRAPPER("/a/./b", "/a/b"); - TEST_WRAPPER("/a/./b/", "/a/b/"); - - // some slightly less easy cases - TEST_WRAPPER("/asdf/..", "/"); - TEST_WRAPPER("/asdf/../", "/"); - TEST_WRAPPER("/asdf/.", "/asdf/"); - TEST_WRAPPER("/asdf//.", "/asdf/"); - - TEST_WRAPPER("/foo/bar/..", "/foo/"); - TEST_WRAPPER("/foo/bar/../baz", "/foo/baz"); - TEST_WRAPPER("/foo/bar/../baz/", "/foo/baz/"); - TEST_WRAPPER("/foo/bar/xyz/..", "/foo/bar/"); - TEST_WRAPPER("/foo/bar/xyz/../", "/foo/bar/"); - - // going under the root or close to it - TEST_WRAPPER("/..", NULL); - TEST_WRAPPER("/../asdf", NULL); - TEST_WRAPPER("/../asdf/", NULL); - TEST_WRAPPER("/./a/../..", NULL); - TEST_WRAPPER("/a/a/../..", "/"); - TEST_WRAPPER("/a/../a/..", "/"); - TEST_WRAPPER("/a/../../a", NULL); - TEST_WRAPPER("/../a/../a", NULL); - TEST_WRAPPER("/../../a/a", NULL); - TEST_WRAPPER("/////../..", NULL); - TEST_WRAPPER("//a//../..", NULL); - - // relative paths aren't allowed - TEST_WRAPPER("relative", NULL); - TEST_WRAPPER("some/stuff", NULL); - TEST_WRAPPER("./stuff", NULL); - TEST_WRAPPER("../stuff", NULL); - TEST_WRAPPER("", NULL); -#undef TEST_WRAPPER -} - TEST(vfs_mount_resolve) { struct vfs_mount *mount = NULL; @@ -107,6 +45,5 @@ TEST(vfs_mount_resolve) { } void tests_vfs(void) { - TEST_RUN(path_simplify); TEST_RUN(vfs_mount_resolve); } diff --git a/src/kernel/vfs/path.c b/src/kernel/vfs/path.c deleted file mode 100644 index e8ce49c..0000000 --- a/src/kernel/vfs/path.c +++ /dev/null @@ -1,58 +0,0 @@ -#include -#include -#include - -int path_simplify(const char *in, char *out, size_t len) { - if (len == 0) return -1; // empty paths are invalid - if (in[0] != '/') return -1; // so are relative paths - - int seg_len; // the length of the current path segment - int out_pos = 0; - bool directory = 0; - - for (size_t i = 0; i < len; i += seg_len + 1) { - assert(in[i] == '/'); - - seg_len = 0; - directory = false; - for (size_t j = i + 1; j < len; j++) { - if (in[j] == '/') { - directory = true; - break; - } - seg_len++; - } - - /* example iteration, illustrated with terrible ASCII art - * - * |i=5 |next i = i + seg_len + 1 = 10 - * v v - * /some/path/asdf - * |--| - * seg_len = 4 - * (segment starts at i+1) */ - - if (seg_len == 0 || (seg_len == 1 && in[i + 1] == '.')) { - /* // or /./ */ - directory = true; - } else if (seg_len == 2 && in[i + 1] == '.' && in[i + 2] == '.') { - /* /../ */ - directory = true; - - /* try to backtrack to the last slash */ - while (--out_pos >= 0 && out[out_pos] != '/'); - if (out_pos < 0) return -1; - } else { - /* a normal segment, e.g. /asdf/ */ - out[out_pos] = '/'; - memcpy(&out[out_pos + 1], &in[i + 1], seg_len); - out_pos += seg_len + 1; - } - - } - - /* paths to directories should have a trailing slash */ - if (directory) out[out_pos++] = '/'; - - return out_pos; -} diff --git a/src/kernel/vfs/path.h b/src/kernel/vfs/path.h deleted file mode 100644 index 7484619..0000000 --- a/src/kernel/vfs/path.h +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once -#include -#include - -/** Reduce a path to its simplest form. - * - * @return length of the string in *out, always less than len. Negative if the path was invalid. - */ -int path_simplify(const char *in, char *out, size_t len); diff --git a/src/shared/include/camellia/path.h b/src/shared/include/camellia/path.h index ba6bccf..8efa0d4 100644 --- a/src/shared/include/camellia/path.h +++ b/src/shared/include/camellia/path.h @@ -1,3 +1,10 @@ #pragma once +#include #define PATH_MAX 512 + +/** Reduce a path to its simplest form. + * + * @return length of the string in *out, always less than len. Negative if the path was invalid. + */ +int path_simplify(const char *in, char *out, size_t len); diff --git a/src/shared/path.c b/src/shared/path.c new file mode 100644 index 0000000..9d2eda3 --- /dev/null +++ b/src/shared/path.c @@ -0,0 +1,61 @@ +#include +#include +#include + +// TODO shared assert +#define assert(...) + +int path_simplify(const char *in, char *out, size_t len) { + if (len == 0) return -1; // empty paths are invalid + if (in[0] != '/') return -1; // so are relative paths + + int seg_len; // the length of the current path segment + int out_pos = 0; + bool directory = 0; + + for (size_t i = 0; i < len; i += seg_len + 1) { + assert(in[i] == '/'); + + seg_len = 0; + directory = false; + for (size_t j = i + 1; j < len; j++) { + if (in[j] == '/') { + directory = true; + break; + } + seg_len++; + } + + /* example iteration, illustrated with terrible ASCII art + * + * |i=5 |next i = i + seg_len + 1 = 10 + * v v + * /some/path/asdf + * |--| + * seg_len = 4 + * (segment starts at i+1) */ + + if (seg_len == 0 || (seg_len == 1 && in[i + 1] == '.')) { + /* // or /./ */ + directory = true; + } else if (seg_len == 2 && in[i + 1] == '.' && in[i + 2] == '.') { + /* /../ */ + directory = true; + + /* try to backtrack to the last slash */ + while (--out_pos >= 0 && out[out_pos] != '/'); + if (out_pos < 0) return -1; + } else { + /* a normal segment, e.g. /asdf/ */ + out[out_pos] = '/'; + memcpy(&out[out_pos + 1], &in[i + 1], seg_len); + out_pos += seg_len + 1; + } + + } + + /* paths to directories should have a trailing slash */ + if (directory) out[out_pos++] = '/'; + + return out_pos; +} 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 +#include + +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); -- cgit v1.2.3