Skip to content

Commit 8357b56

Browse files
cjihrigruyadorno
authored andcommitted
test: add wasi readdir() test
This commit provides coverage for __wasi_fd_readdir(). PR-URL: #35202 Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Jiawen Geng <technicalcute@gmail.com>
1 parent 3d78686 commit 8357b56

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

test/wasi/c/readdir.c

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include <assert.h>
2+
#include <dirent.h>
3+
#include <errno.h>
4+
#include <stdio.h>
5+
#include <stdlib.h>
6+
#include <string.h>
7+
8+
int main() {
9+
DIR* dir;
10+
struct dirent* entry;
11+
char* platform;
12+
int cnt;
13+
int has_d_type;
14+
15+
platform = getenv("NODE_PLATFORM");
16+
assert(platform != NULL);
17+
has_d_type = (0 != strcmp(platform, "aix") && 0 != strcmp(platform, "sunos"));
18+
19+
dir = opendir("/sandbox");
20+
assert(dir != NULL);
21+
22+
cnt = 0;
23+
errno = 0;
24+
while (NULL != (entry = readdir(dir))) {
25+
if (strcmp(entry->d_name, "input.txt") == 0 ||
26+
strcmp(entry->d_name, "input2.txt") == 0 ||
27+
strcmp(entry->d_name, "notadir") == 0) {
28+
if (has_d_type) {
29+
assert(entry->d_type == DT_REG);
30+
} else {
31+
assert(entry->d_type == DT_UNKNOWN);
32+
}
33+
} else if (strcmp(entry->d_name, "subdir") == 0) {
34+
if (has_d_type) {
35+
assert(entry->d_type == DT_DIR);
36+
} else {
37+
assert(entry->d_type == DT_UNKNOWN);
38+
}
39+
} else {
40+
assert("unexpected file");
41+
}
42+
43+
cnt++;
44+
}
45+
46+
assert(errno == 0);
47+
assert(cnt == 4);
48+
closedir(dir);
49+
return 0;
50+
}

test/wasi/test-wasi.js

+5
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ if (process.argv[2] === 'wasi-child') {
8484
runWASI({ test: 'notdir' });
8585
runWASI({ test: 'poll' });
8686
runWASI({ test: 'preopen_populates' });
87+
88+
if (!common.isWindows && process.platform !== 'android') {
89+
runWASI({ test: 'readdir' });
90+
}
91+
8792
runWASI({ test: 'read_file', stdout: `hello from input.txt${EOL}` });
8893
runWASI({
8994
test: 'read_file_twice',

test/wasi/wasm/readdir.wasm

35.4 KB
Binary file not shown.

0 commit comments

Comments
 (0)