Skip to content

Add a basic test for opendir, readdir, and closedir. #266

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions tests/general/opendir.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#include <fcntl.h>
#include <string.h>
#include <stdbool.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <dirent.h>
#include <errno.h>
#include <limits.h>
#include <sys/stat.h>
#include <unistd.h>

#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 <dir>\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;
}
1 change: 1 addition & 0 deletions tests/general/opendir.c.dir/dir-symlink
1 change: 1 addition & 0 deletions tests/general/opendir.c.dir/dir/file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is a file in a directory.
1 change: 1 addition & 0 deletions tests/general/opendir.c.dir/file-symlink
1 change: 1 addition & 0 deletions tests/general/opendir.c.dir/file.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# This is a top-level file
2 changes: 1 addition & 1 deletion tests/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ set -ueo pipefail
# location. Alternatively, exporting $CC and $CXX allow more flexibility. e.g:
#
# export CXX="<wasi-sdk>/bin/clang++ --sysroot <wasi-sdk>/share/wasi-sysroot"
# export CCC="<wasi-sdk>/bin/clang --sysroot <wasi-sdk>/share/wasi-sysroot"
# export CC="<wasi-sdk>/bin/clang --sysroot <wasi-sdk>/share/wasi-sysroot"
#

# Determine the wasm runtime to use, if one is provided.
Expand Down