From cecfcf7f919dada96dfd6dd7fba115eecb21d2e0 Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Tue, 6 Dec 2022 12:42:01 -0800 Subject: [PATCH] Add a basic test for opendir, readdir, and closedir. (#266) --- tests/general/opendir.c | 72 ++++++++++++++++++++++++ tests/general/opendir.c.dir/dir-symlink | 1 + tests/general/opendir.c.dir/dir/file.txt | 1 + tests/general/opendir.c.dir/file-symlink | 1 + tests/general/opendir.c.dir/file.md | 1 + tests/run.sh | 2 +- 6 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 tests/general/opendir.c create mode 120000 tests/general/opendir.c.dir/dir-symlink create mode 100644 tests/general/opendir.c.dir/dir/file.txt create mode 120000 tests/general/opendir.c.dir/file-symlink create mode 100644 tests/general/opendir.c.dir/file.md diff --git a/tests/general/opendir.c b/tests/general/opendir.c new file mode 100644 index 000000000..941e7cee3 --- /dev/null +++ b/tests/general/opendir.c @@ -0,0 +1,72 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define perror_and_exit(message) \ + do { \ + perror(message); \ + return EXIT_FAILURE; \ + } while (0) + +#define OFFSET 10726 +#define LENGTH 143 + +int main(int argc, char *argv[]) { + if (argc != 2) { + fprintf(stderr, "usage: %s \n", argv[0]); + return EXIT_FAILURE; + } + DIR *dir = opendir(argv[1]); + if (dir == NULL) { + perror_and_exit("opendir"); + } + + int count = 0; + int zeros = 0; + errno = 0; + for (;; count += 1) { + struct dirent *ent = readdir(dir); + if (ent == NULL) { + if (errno == 0) { + break; + } + perror_and_exit("readdir"); + } + + if (strcmp(ent->d_name, "file.md") == 0) { + assert(ent->d_type == DT_REG); + } else if (strcmp(ent->d_name, "dir") == 0) { + assert(ent->d_type == DT_DIR); + } else if (strcmp(ent->d_name, "file-symlink") == 0) { + assert(ent->d_type == DT_LNK); + } else if (strcmp(ent->d_name, "dir-symlink") == 0) { + assert(ent->d_type == DT_LNK); + } else if (strcmp(ent->d_name, ".") == 0) { + assert(ent->d_type == DT_DIR); + } else if (strcmp(ent->d_name, "..") == 0) { + assert(ent->d_type == DT_DIR); + } else { + assert(false); + } + if (ent->d_ino == 0) { + zeros += 1; + } + } + + assert(count == 6); + assert(zeros <= 1); + + if (closedir(dir) != 0) + perror_and_exit("closedir"); + + return EXIT_SUCCESS; +} diff --git a/tests/general/opendir.c.dir/dir-symlink b/tests/general/opendir.c.dir/dir-symlink new file mode 120000 index 000000000..872451932 --- /dev/null +++ b/tests/general/opendir.c.dir/dir-symlink @@ -0,0 +1 @@ +dir \ No newline at end of file diff --git a/tests/general/opendir.c.dir/dir/file.txt b/tests/general/opendir.c.dir/dir/file.txt new file mode 100644 index 000000000..27f8ec843 --- /dev/null +++ b/tests/general/opendir.c.dir/dir/file.txt @@ -0,0 +1 @@ +This is a file in a directory. diff --git a/tests/general/opendir.c.dir/file-symlink b/tests/general/opendir.c.dir/file-symlink new file mode 120000 index 000000000..4e87f2908 --- /dev/null +++ b/tests/general/opendir.c.dir/file-symlink @@ -0,0 +1 @@ +file.md \ No newline at end of file diff --git a/tests/general/opendir.c.dir/file.md b/tests/general/opendir.c.dir/file.md new file mode 100644 index 000000000..654066fc8 --- /dev/null +++ b/tests/general/opendir.c.dir/file.md @@ -0,0 +1 @@ +# This is a top-level file diff --git a/tests/run.sh b/tests/run.sh index 8d5a99330..6379b581c 100755 --- a/tests/run.sh +++ b/tests/run.sh @@ -10,7 +10,7 @@ set -ueo pipefail # location. Alternatively, exporting $CC and $CXX allow more flexibility. e.g: # # export CXX="/bin/clang++ --sysroot /share/wasi-sysroot" -# export CCC="/bin/clang --sysroot /share/wasi-sysroot" +# export CC="/bin/clang --sysroot /share/wasi-sysroot" # # Determine the wasm runtime to use, if one is provided.