Skip to content

Commit

Permalink
Unit test for path sanitation porting
Browse files Browse the repository at this point in the history
  • Loading branch information
henrybear327 committed Nov 23, 2023
1 parent ad83679 commit 40e2355
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "elf.h"
#include "state.h"
#include "utils.h"

/* enable program trace mode */
static bool opt_trace = false;
Expand Down Expand Up @@ -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;
Expand Down
62 changes: 62 additions & 0 deletions src/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* "LICENSE" for information on usage and redistribution of this file.
*/

#include <assert.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -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
2 changes: 2 additions & 0 deletions src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

0 comments on commit 40e2355

Please sign in to comment.