From 8fc5d60777cc48d74bdfd5359dad3b7dfd9c3964 Mon Sep 17 00:00:00 2001 From: borysp Date: Sat, 3 Aug 2019 03:31:33 +0200 Subject: [PATCH] [Pal/regression] Add path normalization tests --- Pal/regression/Makefile | 2 +- Pal/regression/normalize_path.c | 101 ++++++++++++++++++++++++++++++++ Pal/regression/test_pal.py | 7 ++- 3 files changed, 108 insertions(+), 2 deletions(-) create mode 100644 Pal/regression/normalize_path.c diff --git a/Pal/regression/Makefile b/Pal/regression/Makefile index 1699e3cb..0b6afe9c 100644 --- a/Pal/regression/Makefile +++ b/Pal/regression/Makefile @@ -2,7 +2,7 @@ include ../src/Makefile.Host CC = gcc CFLAGS = -Wall -O2 -std=c11 -fno-builtin -nostdlib -mavx -no-pie \ - -I../include/pal -I../lib + -I../include/pal -I../lib -I../src preloads = $(patsubst %.c,%,$(wildcard *.so.c)) executables = $(filter-out $(preloads),$(patsubst %.c,%,$(wildcard *.c))) ..Bootstrap diff --git a/Pal/regression/normalize_path.c b/Pal/regression/normalize_path.c new file mode 100644 index 00000000..3456063c --- /dev/null +++ b/Pal/regression/normalize_path.c @@ -0,0 +1,101 @@ +#include "api.h" +#include "pal_debug.h" +#include "pal_defs.h" +#include "pal_error.h" + +static int strcmp(const char* a, const char* b) { + for (; *a && *b && *a == *b; a++, b++); + return *a - *b; +} + +static const char* get_norm_path_cases[][2] = { + { "/", "/" }, + { "/a/b/c", "/a/b/c" }, + { "/a/../b", "/b" }, + { "/../a", "/a" }, + { "/../../../../../a", "/a" }, + { "/../a/../../b", "/b" }, + { "/../..a/b", "/..a/b" }, + {" /a/..", "/" }, + { "/a/.", "/a" }, + { "/.//a//./b", "/a/b" }, + { "///////.////////////./", "/" }, + { "/...././a/../././.../b/.....", "/..../.../b/....." }, + { "a/b/c", "a/b/c" }, + { "a/../b", "b" }, + { "../a", "../a" }, + { "../../../../../a", "../../../../../a" }, + { "../a/../../b", "../../b" }, + { "../..a/b", "../..a/b" }, + { "a/..", "" }, + { "a/." "a" }, +}; + +static const char* get_base_name_cases[][2] = { + { "/", "" }, + { "/a", "a" }, + { "/a/b/c", "c" }, + { "/..a/b", "b" }, + { "", "" }, + { "../a", "a" }, + { "../../../../../a", "a" }, + { "..a/b", "b" }, + { "a/b/c", "c" }, +}; + +#define ARR_LEN(x) (sizeof(x) / sizeof(x[0]) / sizeof(x[0][0])) + +#define print_err(name, i, ...) \ + do { \ + pal_printf("%s: case %lu (%s) ", name, i, cases[i][0]); \ + pal_printf(__VA_ARGS__); \ + } while (0) + +static const char* (*cases)[2]; +static size_t cases_len; +static int (*func_to_test)(const char*, char*, size_t*); +static const char* func_name; + +static int run_test(void) { + char buf[URI_MAX] = { 0 }; + + for (size_t i = 0; i < cases_len; i++) { + size_t size = sizeof(buf); + int ret = func_to_test(cases[i][0], buf, &size); + + if (ret < 0) { + print_err(func_name, i, "failed with error: %s\n", PAL_STRERROR(ret)); + return 1; + } + + if (strlen(buf) != size) { + print_err(func_name, i, "returned wrong size: %zu\n", size); + return 1; + } + + if (strcmp(cases[i][1], buf) != 0) { + print_err(func_name, i, "returned: \"%s\", instead of: %s\n", buf, cases[i][1]); + return 1; + } + } + return 0; +} + +int main(void) { + cases = get_norm_path_cases; + cases_len = ARR_LEN(get_norm_path_cases); + func_to_test = get_norm_path; + if (run_test()) { + return 1; + } + + cases = get_base_name_cases; + cases_len = ARR_LEN(get_base_name_cases); + func_to_test = get_base_name; + if (run_test()) { + return 1; + } + + pal_printf("Success!\n"); + return 0; +} diff --git a/Pal/regression/test_pal.py b/Pal/regression/test_pal.py index 148c8e2f..3f9ac50d 100644 --- a/Pal/regression/test_pal.py +++ b/Pal/regression/test_pal.py @@ -24,7 +24,7 @@ CPUINFO_FLAGS_WHITELIST = [ ] -class TC_00_AtomicMath(RegressionTestCase): +class TC_00_Basic(RegressionTestCase): def test_000_atomic_math(self): stdout, stderr = self.run_binary(['AtomicMath']) self.assertIn('Subtract INT_MIN: Both values match 2147483648', stderr) @@ -32,6 +32,11 @@ class TC_00_AtomicMath(RegressionTestCase): self.assertIn('Subtract LLONG_MIN: Both values match -9223372036854775808', stderr) self.assertIn('Subtract LLONG_MAX: Both values match -9223372036854775807', stderr) + def test_001_path_normalization(self): + stdout, stderr = self.run_binary(['normalize_path']) + + self.assertIn("Success!\n", stderr) + class TC_01_Bootstrap(RegressionTestCase): def test_100_basic_boostrapping(self): stdout, stderr = self.run_binary(['Bootstrap'])