summaryrefslogtreecommitdiff
path: root/test/wasi/c
diff options
context:
space:
mode:
Diffstat (limited to 'test/wasi/c')
-rw-r--r--test/wasi/c/cant_dotdot.c11
-rw-r--r--test/wasi/c/clock_getres.c17
-rw-r--r--test/wasi/c/exitcode.c3
-rw-r--r--test/wasi/c/fd_prestat_get_refresh.c8
-rw-r--r--test/wasi/c/follow_symlink.c14
-rw-r--r--test/wasi/c/getentropy.c18
-rw-r--r--test/wasi/c/getrusage.c34
-rw-r--r--test/wasi/c/gettimeofday.c35
-rw-r--r--test/wasi/c/notdir.c11
-rw-r--r--test/wasi/c/poll.c31
-rw-r--r--test/wasi/c/preopen_populates.c3
-rw-r--r--test/wasi/c/read_file.c14
-rw-r--r--test/wasi/c/read_file_twice.c16
-rw-r--r--test/wasi/c/stat.c53
-rw-r--r--test/wasi/c/stdin.c13
-rw-r--r--test/wasi/c/symlink_escape.c9
-rw-r--r--test/wasi/c/symlink_loop.c9
-rw-r--r--test/wasi/c/write_file.c15
18 files changed, 314 insertions, 0 deletions
diff --git a/test/wasi/c/cant_dotdot.c b/test/wasi/c/cant_dotdot.c
new file mode 100644
index 0000000000..e2722062cd
--- /dev/null
+++ b/test/wasi/c/cant_dotdot.c
@@ -0,0 +1,11 @@
+#include <assert.h>
+#include <errno.h>
+#include <stdio.h>
+
+int main() {
+ FILE* file = fopen("/sandbox/../outside.txt", "r");
+ assert(file == NULL);
+ assert(errno == ENOTCAPABLE);
+
+ return 0;
+}
diff --git a/test/wasi/c/clock_getres.c b/test/wasi/c/clock_getres.c
new file mode 100644
index 0000000000..eaac02c665
--- /dev/null
+++ b/test/wasi/c/clock_getres.c
@@ -0,0 +1,17 @@
+#include <assert.h>
+#include <time.h>
+
+int main() {
+ struct timespec ts;
+ int r;
+
+ // supported clocks
+ r = clock_getres(CLOCK_REALTIME, &ts);
+ assert(r == 0);
+ r = clock_getres(CLOCK_MONOTONIC, &ts);
+ assert(r == 0);
+ r = clock_getres(CLOCK_PROCESS_CPUTIME_ID, &ts);
+ assert(r == 0);
+ r = clock_getres(CLOCK_THREAD_CPUTIME_ID, &ts);
+ assert(r == 0);
+}
diff --git a/test/wasi/c/exitcode.c b/test/wasi/c/exitcode.c
new file mode 100644
index 0000000000..9c44b0de74
--- /dev/null
+++ b/test/wasi/c/exitcode.c
@@ -0,0 +1,3 @@
+int main() {
+ return 120;
+}
diff --git a/test/wasi/c/fd_prestat_get_refresh.c b/test/wasi/c/fd_prestat_get_refresh.c
new file mode 100644
index 0000000000..fb644ab49b
--- /dev/null
+++ b/test/wasi/c/fd_prestat_get_refresh.c
@@ -0,0 +1,8 @@
+#include <unistd.h>
+
+int main(void) {
+ isatty(1);
+ __builtin_wasm_memory_grow(0, 1);
+ isatty(1);
+ return 0;
+}
diff --git a/test/wasi/c/follow_symlink.c b/test/wasi/c/follow_symlink.c
new file mode 100644
index 0000000000..badb0ee267
--- /dev/null
+++ b/test/wasi/c/follow_symlink.c
@@ -0,0 +1,14 @@
+#include <assert.h>
+#include <stdio.h>
+
+int main() {
+ FILE* file = fopen("/sandbox/subdir/input_link.txt", "r");
+ assert(file != NULL);
+
+ char c = fgetc(file);
+ while (c != EOF) {
+ int wrote = fputc(c, stdout);
+ assert(wrote != EOF);
+ c = fgetc(file);
+ }
+}
diff --git a/test/wasi/c/getentropy.c b/test/wasi/c/getentropy.c
new file mode 100644
index 0000000000..75547e1c47
--- /dev/null
+++ b/test/wasi/c/getentropy.c
@@ -0,0 +1,18 @@
+#include <assert.h>
+#include <unistd.h>
+
+int main() {
+ char buf[256] = {0};
+ int r = getentropy(buf, 256);
+ assert(r == 0);
+
+ for (int i = 0; i < 256; i++) {
+ if (buf[i] != 0) {
+ return 0;
+ }
+ }
+
+ // if this ever is reached, we either have a bug or should buy a lottery
+ // ticket
+ return 1;
+}
diff --git a/test/wasi/c/getrusage.c b/test/wasi/c/getrusage.c
new file mode 100644
index 0000000000..ad1e430b85
--- /dev/null
+++ b/test/wasi/c/getrusage.c
@@ -0,0 +1,34 @@
+#include <assert.h>
+#include <sys/resource.h>
+
+int main() {
+ struct rusage ru1;
+ struct rusage ru2;
+ long long s1;
+ long long us1;
+ long long s2;
+ long long us2;
+ int r;
+ int success = 0;
+
+ r = getrusage(RUSAGE_SELF, &ru1);
+ assert(r == 0);
+ s1 = ru1.ru_utime.tv_sec;
+ us1 = ru1.ru_utime.tv_usec;
+
+ for (int i = 0; i < 10000; i++) {
+ r = getrusage(RUSAGE_SELF, &ru2);
+ assert(r == 0);
+ s2 = ru2.ru_utime.tv_sec;
+ us2 = ru2.ru_utime.tv_usec;
+ assert(s1 <= s2);
+
+ // Verify that some time has passed.
+ if (s2 > s1 || (s2 == s1 && us2 > us1)) {
+ success = 1;
+ break;
+ }
+ }
+
+ assert(success == 1);
+}
diff --git a/test/wasi/c/gettimeofday.c b/test/wasi/c/gettimeofday.c
new file mode 100644
index 0000000000..209a54e498
--- /dev/null
+++ b/test/wasi/c/gettimeofday.c
@@ -0,0 +1,35 @@
+#include <assert.h>
+#include <stdlib.h>
+#include <sys/time.h>
+
+int main() {
+ struct timeval tv1;
+ struct timeval tv2;
+ long long s1;
+ long long us1;
+ long long s2;
+ long long us2;
+ int r;
+ int success = 0;
+
+ r = gettimeofday(&tv1, NULL);
+ assert(r == 0);
+ s1 = tv1.tv_sec;
+ us1 = tv1.tv_usec;
+
+ for (int i = 0; i < 10000; i++) {
+ r = gettimeofday(&tv2, NULL);
+ assert(r == 0);
+ s2 = tv2.tv_sec;
+ us2 = tv2.tv_usec;
+ assert(s1 <= s2);
+
+ // Verify that some time has passed.
+ if (s2 > s1 || (s2 == s1 && us2 > us1)) {
+ success = 1;
+ break;
+ }
+ }
+
+ assert(success == 1);
+}
diff --git a/test/wasi/c/notdir.c b/test/wasi/c/notdir.c
new file mode 100644
index 0000000000..03f369ffd3
--- /dev/null
+++ b/test/wasi/c/notdir.c
@@ -0,0 +1,11 @@
+#include <assert.h>
+#include <dirent.h>
+#include <errno.h>
+
+int main() {
+ DIR* dir = opendir("/sandbox/notadir");
+ assert(dir == NULL);
+ assert(errno == ENOTDIR);
+
+ return 0;
+}
diff --git a/test/wasi/c/poll.c b/test/wasi/c/poll.c
new file mode 100644
index 0000000000..6b6ef71fd6
--- /dev/null
+++ b/test/wasi/c/poll.c
@@ -0,0 +1,31 @@
+#include <assert.h>
+#include <poll.h>
+#include <time.h>
+#include <unistd.h>
+
+int main(void) {
+ struct pollfd fds[2];
+ time_t before, now;
+ int ret;
+
+ fds[0] = (struct pollfd){.fd = 1, .events = POLLOUT, .revents = 0};
+ fds[1] = (struct pollfd){.fd = 2, .events = POLLOUT, .revents = 0};
+
+ ret = poll(fds, 2, -1);
+ assert(ret == 2);
+ assert(fds[0].revents == POLLOUT);
+ assert(fds[1].revents == POLLOUT);
+
+ fds[0] = (struct pollfd){.fd = 0, .events = POLLIN, .revents = 0};
+ time(&before);
+ ret = poll(fds, 1, 2000);
+ time(&now);
+ assert(ret == 0);
+ assert(now - before >= 2);
+
+ sleep(1);
+ time(&now);
+ assert(now - before >= 3);
+
+ return 0;
+}
diff --git a/test/wasi/c/preopen_populates.c b/test/wasi/c/preopen_populates.c
new file mode 100644
index 0000000000..03b2213bb9
--- /dev/null
+++ b/test/wasi/c/preopen_populates.c
@@ -0,0 +1,3 @@
+int main(void) {
+ return 0;
+}
diff --git a/test/wasi/c/read_file.c b/test/wasi/c/read_file.c
new file mode 100644
index 0000000000..40023e29e2
--- /dev/null
+++ b/test/wasi/c/read_file.c
@@ -0,0 +1,14 @@
+#include <assert.h>
+#include <stdio.h>
+
+int main() {
+ FILE* file = fopen("/sandbox/input.txt", "r");
+ assert(file != NULL);
+
+ char c = fgetc(file);
+ while (c != EOF) {
+ int wrote = fputc(c, stdout);
+ assert(wrote != EOF);
+ c = fgetc(file);
+ }
+}
diff --git a/test/wasi/c/read_file_twice.c b/test/wasi/c/read_file_twice.c
new file mode 100644
index 0000000000..e295e38a3b
--- /dev/null
+++ b/test/wasi/c/read_file_twice.c
@@ -0,0 +1,16 @@
+#include <assert.h>
+#include <stdio.h>
+
+int main() {
+ for (int i = 0; i < 2; i++) {
+ FILE* file = fopen("/sandbox/input.txt", "r");
+ assert(file != NULL);
+
+ char c = fgetc(file);
+ while (c != EOF) {
+ int wrote = fputc(c, stdout);
+ assert(wrote != EOF);
+ c = fgetc(file);
+ }
+ }
+}
diff --git a/test/wasi/c/stat.c b/test/wasi/c/stat.c
new file mode 100644
index 0000000000..fd3854937b
--- /dev/null
+++ b/test/wasi/c/stat.c
@@ -0,0 +1,53 @@
+#include <sys/stat.h>
+
+#include <assert.h>
+#include <fcntl.h>
+#include <time.h>
+#include <unistd.h>
+
+#define BASE_DIR "/tmp"
+#define OUTPUT_DIR BASE_DIR "/testdir"
+#define PATH OUTPUT_DIR "/output.txt"
+#define SIZE 500
+
+int main(void) {
+ struct stat st;
+ int fd;
+ int ret;
+ off_t pos;
+
+ (void)st;
+ ret = mkdir(OUTPUT_DIR, 0755);
+ assert(ret == 0);
+
+ fd = open(PATH, O_CREAT | O_WRONLY, 0666);
+ assert(fd != -1);
+
+ pos = lseek(fd, SIZE - 1, SEEK_SET);
+ assert(pos == SIZE - 1);
+
+ ret = (int)write(fd, "", 1);
+ assert(ret == 1);
+
+ ret = fstat(fd, &st);
+ assert(ret == 0);
+ assert(st.st_size == SIZE);
+
+ ret = close(fd);
+ assert(ret == 0);
+
+ ret = access(PATH, R_OK);
+ assert(ret == 0);
+
+ ret = stat(PATH, &st);
+ assert(ret == 0);
+ assert(st.st_size == SIZE);
+
+ ret = unlink(PATH);
+ assert(ret == 0);
+
+ ret = stat(PATH, &st);
+ assert(ret == -1);
+
+ return 0;
+}
diff --git a/test/wasi/c/stdin.c b/test/wasi/c/stdin.c
new file mode 100644
index 0000000000..5a81ea1265
--- /dev/null
+++ b/test/wasi/c/stdin.c
@@ -0,0 +1,13 @@
+#include <stdio.h>
+
+int main(void) {
+ char x[32];
+
+ if (fgets(x, sizeof x, stdin) == NULL) {
+ return ferror(stdin);
+ }
+ if (fputs(x, stdout) == EOF) {
+ return ferror(stdout);
+ }
+ return 0;
+}
diff --git a/test/wasi/c/symlink_escape.c b/test/wasi/c/symlink_escape.c
new file mode 100644
index 0000000000..32dcc64eeb
--- /dev/null
+++ b/test/wasi/c/symlink_escape.c
@@ -0,0 +1,9 @@
+#include <assert.h>
+#include <errno.h>
+#include <stdio.h>
+
+int main() {
+ FILE* file = fopen("/sandbox/subdir/outside.txt", "r");
+ assert(file == NULL);
+ assert(errno == ENOTCAPABLE);
+}
diff --git a/test/wasi/c/symlink_loop.c b/test/wasi/c/symlink_loop.c
new file mode 100644
index 0000000000..23bd70ba60
--- /dev/null
+++ b/test/wasi/c/symlink_loop.c
@@ -0,0 +1,9 @@
+#include <assert.h>
+#include <errno.h>
+#include <stdio.h>
+
+int main() {
+ FILE* file = fopen("/sandbox/subdir/loop1", "r");
+ assert(file == NULL);
+ assert(errno == ELOOP);
+}
diff --git a/test/wasi/c/write_file.c b/test/wasi/c/write_file.c
new file mode 100644
index 0000000000..c4cf30cf29
--- /dev/null
+++ b/test/wasi/c/write_file.c
@@ -0,0 +1,15 @@
+#include <assert.h>
+#include <stdio.h>
+#include <string.h>
+
+static char* message = "hello, file!";
+
+int main() {
+ FILE* file = fopen("/tmp/output.txt", "w");
+ assert(file != NULL);
+
+ int nwritten = fprintf(file, "%s", message);
+ assert(nwritten == strlen(message));
+ int r = fclose(file);
+ assert(r == 0);
+}