diff --git a/test/wasi/c/readdir.c b/test/wasi/c/readdir.c new file mode 100644 index 00000000000000..a18ae5de34eb8f --- /dev/null +++ b/test/wasi/c/readdir.c @@ -0,0 +1,50 @@ +#include +#include +#include +#include +#include +#include + +int main() { + DIR* dir; + struct dirent* entry; + char* platform; + int cnt; + int has_d_type; + + platform = getenv("NODE_PLATFORM"); + assert(platform != NULL); + has_d_type = (0 != strcmp(platform, "aix") && 0 != strcmp(platform, "sunos")); + + dir = opendir("/sandbox"); + assert(dir != NULL); + + cnt = 0; + errno = 0; + while (NULL != (entry = readdir(dir))) { + if (strcmp(entry->d_name, "input.txt") == 0 || + strcmp(entry->d_name, "input2.txt") == 0 || + strcmp(entry->d_name, "notadir") == 0) { + if (has_d_type) { + assert(entry->d_type == DT_REG); + } else { + assert(entry->d_type == DT_UNKNOWN); + } + } else if (strcmp(entry->d_name, "subdir") == 0) { + if (has_d_type) { + assert(entry->d_type == DT_DIR); + } else { + assert(entry->d_type == DT_UNKNOWN); + } + } else { + assert("unexpected file"); + } + + cnt++; + } + + assert(errno == 0); + assert(cnt == 4); + closedir(dir); + return 0; +} diff --git a/test/wasi/test-wasi.js b/test/wasi/test-wasi.js index 8ebc290d7b8269..b4c404e515cbb2 100644 --- a/test/wasi/test-wasi.js +++ b/test/wasi/test-wasi.js @@ -84,6 +84,11 @@ if (process.argv[2] === 'wasi-child') { runWASI({ test: 'notdir' }); runWASI({ test: 'poll' }); runWASI({ test: 'preopen_populates' }); + + if (!common.isWindows && process.platform !== 'android') { + runWASI({ test: 'readdir' }); + } + runWASI({ test: 'read_file', stdout: `hello from input.txt${EOL}` }); runWASI({ test: 'read_file_twice', diff --git a/test/wasi/wasm/readdir.wasm b/test/wasi/wasm/readdir.wasm new file mode 100755 index 00000000000000..ce6cb4999524db Binary files /dev/null and b/test/wasi/wasm/readdir.wasm differ