Skip to content

Commit

Permalink
Add a basic test for opendir, readdir, and closedir. (#266)
Browse files Browse the repository at this point in the history
  • Loading branch information
sunfishcode authored Dec 6, 2022
1 parent 36553bb commit cecfcf7
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 1 deletion.
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

0 comments on commit cecfcf7

Please sign in to comment.