diff --git a/tests/general/stat.c b/tests/general/stat.c new file mode 100644 index 0000000..dc007c1 --- /dev/null +++ b/tests/general/stat.c @@ -0,0 +1,75 @@ +#include +#include +#include +#include +#include +#include +#include + +int main(int argc, char *argv[]) { + if (argc != 2) { + fprintf(stderr, "usage: %s \n", argv[0]); + return EXIT_FAILURE; + } + + char *filename; + int n = asprintf(&filename, "%s/file", argv[1]); + assert(n > 0); + + n = creat(filename, S_IRUSR|S_IWUSR); + assert(n >= 0); + + char *linkname; + n = asprintf(&linkname, "%s/symlink", argv[1]); + assert(n > 0); + + n = symlink("file", linkname); + assert(n == 0); + + struct stat file_statbuf; + struct stat link_statbuf; + + // Test stat. + + n = stat(filename, &file_statbuf); + assert(n == 0); + assert(file_statbuf.st_size == 0); + assert(S_ISREG(file_statbuf.st_mode)); + + n = stat(linkname, &link_statbuf); + assert(n == 0); + assert(link_statbuf.st_size == 0); + assert(S_ISREG(link_statbuf.st_mode)); + + assert(file_statbuf.st_dev == link_statbuf.st_dev); + + // Clear out the access time fields, and they should be the same. + memset(&file_statbuf.st_atim, 0, sizeof(struct timespec)); + memset(&link_statbuf.st_atim, 0, sizeof(struct timespec)); + assert(memcmp(&file_statbuf, &link_statbuf, sizeof(struct stat)) == 0); + + // Test lstat. + + n = lstat(filename, &file_statbuf); + assert(n == 0); + assert(file_statbuf.st_size == 0); + assert(S_ISREG(file_statbuf.st_mode)); + + n = lstat(linkname, &link_statbuf); + assert(n == 0); + assert(link_statbuf.st_size != 0); + assert(S_ISLNK(link_statbuf.st_mode)); + + assert(file_statbuf.st_dev == link_statbuf.st_dev); + assert(link_statbuf.st_ino != file_statbuf.st_ino); + + n = unlink(filename); + assert(n == 0); + n = unlink(linkname); + assert(n == 0); + + free(filename); + free(linkname); + + return 0; +} diff --git a/tests/general/stat.c.dir/empty b/tests/general/stat.c.dir/empty new file mode 100644 index 0000000..e69de29 diff --git a/tests/testcase.sh b/tests/testcase.sh index e072243..2589be8 100755 --- a/tests/testcase.sh +++ b/tests/testcase.sh @@ -48,9 +48,18 @@ else env="" fi +# Determine a preopened directory to provide. +if [ -e "$input.dir" ]; then + dir="--dir $input.dir" + dirarg="$input.dir" +else + dir="" + dirarg="" +fi + # Run the test, capturing stdout, stderr, and the exit status. exit_status=0 -"$runwasi" $env "$wasm" \ +"$runwasi" $env $dir "$wasm" $dirarg \ < "$stdin" \ > "$stdout_observed" \ 2> "$stderr_observed" \