diff options
author | Ian Moffett <ian@osmora.org> | 2024-03-07 17:28:00 -0500 |
---|---|---|
committer | Ian Moffett <ian@osmora.org> | 2024-03-07 17:28:32 -0500 |
commit | bd5969fc876a10b18613302db7087ef3c40f18e1 (patch) | |
tree | 7c2b8619afe902abf99570df2873fbdf40a4d1a1 /lib/mlibc/tests/posix/realpath.c | |
parent | a95b38b1b92b172e6cc4e8e56a88a30cc65907b0 (diff) |
lib: Add mlibc
Signed-off-by: Ian Moffett <ian@osmora.org>
Diffstat (limited to 'lib/mlibc/tests/posix/realpath.c')
-rw-r--r-- | lib/mlibc/tests/posix/realpath.c | 133 |
1 files changed, 133 insertions, 0 deletions
diff --git a/lib/mlibc/tests/posix/realpath.c b/lib/mlibc/tests/posix/realpath.c new file mode 100644 index 0000000..e44de81 --- /dev/null +++ b/lib/mlibc/tests/posix/realpath.c @@ -0,0 +1,133 @@ +#include <unistd.h> +#include <sys/stat.h> +#include <assert.h> +#include <string.h> +#include <stdlib.h> +#include <stdio.h> +#include <signal.h> + +#ifdef USE_HOST_LIBC +#define TEST_BASE "/tmp/mlibc-realpath-host-libc" +#else +#define TEST_BASE "/tmp/mlibc-realpath" +#endif + +void prepare() { + assert(!mkdir(TEST_BASE "/", S_IRWXU)); + assert(!mkdir(TEST_BASE "/dir1", S_IRWXU)); + assert(!mkdir(TEST_BASE "/dir2", S_IRWXU)); + assert(!symlink(TEST_BASE "/dir2/", TEST_BASE "/dir1/abs-link")); + assert(!chdir(TEST_BASE "/dir1")); + assert(!symlink("../dir2/", TEST_BASE "/dir1/rel-link")); +} + +void cleanup(int do_assert) { + if (do_assert) { + assert(!unlink(TEST_BASE "/dir1/rel-link")); + assert(!unlink(TEST_BASE "/dir1/abs-link")); + assert(!rmdir(TEST_BASE "/dir2")); + assert(!rmdir(TEST_BASE "/dir1")); + assert(!rmdir(TEST_BASE "/")); + } else { + unlink(TEST_BASE "/dir1/rel-link"); + unlink(TEST_BASE "/dir1/abs-link"); + rmdir(TEST_BASE "/dir2"); + rmdir(TEST_BASE "/dir1"); + rmdir(TEST_BASE "/"); + } +} + +void signal_handler(int sig, siginfo_t *info, void *ctx) { + (void)sig; + (void)info; + (void)ctx; + + cleanup(0); + + struct sigaction sa; + sigemptyset(&sa.sa_mask); + sa.sa_handler = SIG_DFL; + sa.sa_flags = 0; + + sigaction(SIGABRT, &sa, NULL); + abort(); +} + +int main() { + char *path; + + struct sigaction sa; + sigemptyset(&sa.sa_mask); + sa.sa_sigaction = signal_handler; + sa.sa_flags = SA_SIGINFO; + sigaction(SIGABRT, &sa, NULL); + + prepare(); + + path = realpath(TEST_BASE "/dir1/", NULL); + assert(path); + assert(!strcmp(path, TEST_BASE "/dir1")); + free(path); + + path = realpath(TEST_BASE "/dir1/../dir2", NULL); + assert(path); + assert(!strcmp(path, TEST_BASE "/dir2")); + free(path); + + path = realpath(TEST_BASE "/dir1/abs-link/", NULL); + assert(path); + assert(!strcmp(path, TEST_BASE "/dir2")); + free(path); + + path = realpath(TEST_BASE "/dir1/rel-link/", NULL); + assert(path); + assert(!strcmp(path, TEST_BASE "/dir2")); + free(path); + + path = realpath(TEST_BASE "/dir1/abs-link/../", NULL); + assert(path); + assert(!strcmp(path, TEST_BASE "")); + free(path); + + path = realpath(TEST_BASE "/dir1/rel-link/../", NULL); + assert(path); + assert(!strcmp(path, TEST_BASE "")); + free(path); + + path = realpath(TEST_BASE "/dir1/abs-link/../dir1/abs-link/", NULL); + assert(path); + assert(!strcmp(path, TEST_BASE "/dir2")); + free(path); + + path = realpath(TEST_BASE "/dir1/rel-link/../dir1/rel-link/", NULL); + assert(path); + assert(!strcmp(path, TEST_BASE "/dir2")); + free(path); + + path = realpath(TEST_BASE "/dir1/abs-link/../dir1/rel-link/", NULL); + assert(path); + assert(!strcmp(path, TEST_BASE "/dir2")); + free(path); + + path = realpath(TEST_BASE "/dir1/rel-link/../dir1/abs-link/", NULL); + assert(path); + assert(!strcmp(path, TEST_BASE "/dir2")); + free(path); + + path = realpath("/tmp", NULL); + assert(path); + assert(!strcmp(path, "/tmp")); + free(path); + + path = realpath("/", NULL); + assert(path); + assert(!strcmp(path, "/")); + free(path); + + path = realpath("//", NULL); + assert(path); + assert(!strcmp(path, "/")); + free(path); + + cleanup(1); +} |