Skip to content

Commit cecfcf7

Browse files
authored
Add a basic test for opendir, readdir, and closedir. (#266)
1 parent 36553bb commit cecfcf7

File tree

6 files changed

+77
-1
lines changed

6 files changed

+77
-1
lines changed

tests/general/opendir.c

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#include <fcntl.h>
2+
#include <string.h>
3+
#include <stdbool.h>
4+
#include <assert.h>
5+
#include <stdio.h>
6+
#include <stdlib.h>
7+
#include <stdint.h>
8+
#include <dirent.h>
9+
#include <errno.h>
10+
#include <limits.h>
11+
#include <sys/stat.h>
12+
#include <unistd.h>
13+
14+
#define perror_and_exit(message) \
15+
do { \
16+
perror(message); \
17+
return EXIT_FAILURE; \
18+
} while (0)
19+
20+
#define OFFSET 10726
21+
#define LENGTH 143
22+
23+
int main(int argc, char *argv[]) {
24+
if (argc != 2) {
25+
fprintf(stderr, "usage: %s <dir>\n", argv[0]);
26+
return EXIT_FAILURE;
27+
}
28+
DIR *dir = opendir(argv[1]);
29+
if (dir == NULL) {
30+
perror_and_exit("opendir");
31+
}
32+
33+
int count = 0;
34+
int zeros = 0;
35+
errno = 0;
36+
for (;; count += 1) {
37+
struct dirent *ent = readdir(dir);
38+
if (ent == NULL) {
39+
if (errno == 0) {
40+
break;
41+
}
42+
perror_and_exit("readdir");
43+
}
44+
45+
if (strcmp(ent->d_name, "file.md") == 0) {
46+
assert(ent->d_type == DT_REG);
47+
} else if (strcmp(ent->d_name, "dir") == 0) {
48+
assert(ent->d_type == DT_DIR);
49+
} else if (strcmp(ent->d_name, "file-symlink") == 0) {
50+
assert(ent->d_type == DT_LNK);
51+
} else if (strcmp(ent->d_name, "dir-symlink") == 0) {
52+
assert(ent->d_type == DT_LNK);
53+
} else if (strcmp(ent->d_name, ".") == 0) {
54+
assert(ent->d_type == DT_DIR);
55+
} else if (strcmp(ent->d_name, "..") == 0) {
56+
assert(ent->d_type == DT_DIR);
57+
} else {
58+
assert(false);
59+
}
60+
if (ent->d_ino == 0) {
61+
zeros += 1;
62+
}
63+
}
64+
65+
assert(count == 6);
66+
assert(zeros <= 1);
67+
68+
if (closedir(dir) != 0)
69+
perror_and_exit("closedir");
70+
71+
return EXIT_SUCCESS;
72+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dir
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is a file in a directory.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
file.md

tests/general/opendir.c.dir/file.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# This is a top-level file

tests/run.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ set -ueo pipefail
1010
# location. Alternatively, exporting $CC and $CXX allow more flexibility. e.g:
1111
#
1212
# export CXX="<wasi-sdk>/bin/clang++ --sysroot <wasi-sdk>/share/wasi-sysroot"
13-
# export CCC="<wasi-sdk>/bin/clang --sysroot <wasi-sdk>/share/wasi-sysroot"
13+
# export CC="<wasi-sdk>/bin/clang --sysroot <wasi-sdk>/share/wasi-sysroot"
1414
#
1515

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

0 commit comments

Comments
 (0)