From e79c32cbb7724687f55dca6a557688a59429090c Mon Sep 17 00:00:00 2001 From: Chun-Hung Tseng Date: Wed, 22 Nov 2023 23:59:39 +0100 Subject: [PATCH] Unit test for path sanitation porting Reference: https://cs.opensource.google/go/go/+/refs/tags/go1.21.4:src/path/path_test.go --- src/main.c | 5 +++++ src/utils.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/utils.h | 2 ++ 3 files changed, 69 insertions(+) diff --git a/src/main.c b/src/main.c index 6c4d86d39..cc6f5e60c 100644 --- a/src/main.c +++ b/src/main.c @@ -10,6 +10,7 @@ #include "elf.h" #include "state.h" +#include "utils.h" /* enable program trace mode */ static bool opt_trace = false; @@ -188,6 +189,10 @@ static void dump_test_signature(elf_t *elf) int main(int argc, char **args) { +#ifdef UNITTEST + sanitize_path_test(); +#endif + if (argc == 1 || !parse_args(argc, args)) { print_usage(args[0]); return 1; diff --git a/src/utils.c b/src/utils.c index 962430d35..72ba938ee 100644 --- a/src/utils.c +++ b/src/utils.c @@ -3,6 +3,7 @@ * "LICENSE" for information on usage and redistribution of this file. */ +#include #include #include #include @@ -166,3 +167,64 @@ char *sanitize_path(const char *orig_path) } return ret; } + +#ifdef UNITTEST +void compare(char *input, char *expected_output) +{ + char *input_sanitized = sanitize_path(input); + // printf("\n\nInput =\t\t\t%s\nOutput =\t\t%s\nExpected output =\t%s\n", + // input, input_sanitized, expected_output); + assert(strcmp(input_sanitized, expected_output) == 0); + free(input_sanitized); +} + +void sanitize_path_test() +{ + // Already clean + compare("", "."); + compare("abc", "abc"); + compare("abc/def", "abc/def"); + compare(".", "."); + compare("..", ".."); + compare("../..", "../.."); + compare("../../abc", "../../abc"); + compare("/abc", "/abc"); + compare("/", "/"); + + // Remove trailing slash + compare("abc/", "abc"); + compare("abc/def/", "abc/def"); + compare("a/b/c/", "a/b/c"); + compare("./", "."); + compare("../", ".."); + compare("../../", "../.."); + compare("/abc/", "/abc"); + + // Remove doubled slash + compare("abc//def//ghi", "abc/def/ghi"); + compare("//abc", "/abc"); + compare("///abc", "/abc"); + compare("//abc//", "/abc"); + compare("abc//", "abc"); + + // Remove . elements + compare("abc/./def", "abc/def"); + compare("/./abc/def", "/abc/def"); + compare("abc/.", "abc"); + + // Remove .. elements + compare("abc/def/ghi/../jkl", "abc/def/jkl"); + compare("abc/def/../ghi/../jkl", "abc/jkl"); + compare("abc/def/..", "abc"); + compare("abc/def/../..", "."); + compare("/abc/def/../..", "/"); + compare("abc/def/../../..", ".."); + compare("/abc/def/../../..", "/"); + compare("abc/def/../../../ghi/jkl/../../../mno", "../../mno"); + + // Combinations + compare("abc/./../def", "def"); + compare("abc//./../def", "def"); + compare("abc/../../././../def", "../../def"); +} +#endif diff --git a/src/utils.h b/src/utils.h index fa11e66c1..c40f1a4b4 100644 --- a/src/utils.h +++ b/src/utils.h @@ -47,3 +47,5 @@ void rv_clock_gettime(struct timespec *tp); * https://9p.io/sys/doc/lexnames.html */ char *sanitize_path(const char *orig_path); + +void sanitize_path_test();