Skip to content

Commit 55ba743

Browse files
cjihrigaddaleax
authored andcommitted
test: add WASI test for file resizing
This commit adds a WASI test to cover the following functions: - __wasi_fd_filestat_set_size() - __wasi_fd_tell() PR-URL: #31617 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Daniel Bevenius <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent 6ddeee4 commit 55ba743

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

test/wasi/c/ftruncate.c

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include <sys/stat.h>
2+
#include <assert.h>
3+
#include <fcntl.h>
4+
#include <unistd.h>
5+
6+
#define BASE_DIR "/tmp"
7+
#define OUTPUT_DIR BASE_DIR "/testdir"
8+
#define PATH OUTPUT_DIR "/output.txt"
9+
10+
int main(void) {
11+
struct stat st;
12+
int fd;
13+
14+
(void)st;
15+
assert(0 == mkdir(OUTPUT_DIR, 0755));
16+
17+
fd = open(PATH, O_CREAT | O_WRONLY, 0666);
18+
assert(fd != -1);
19+
20+
/* Verify that the file is initially empty. */
21+
assert(0 == fstat(fd, &st));
22+
assert(st.st_size == 0);
23+
assert(0 == lseek(fd, 0, SEEK_CUR));
24+
25+
/* Increase the file size using ftruncate(). */
26+
assert(0 == ftruncate(fd, 500));
27+
assert(0 == fstat(fd, &st));
28+
assert(st.st_size == 500);
29+
assert(0 == lseek(fd, 0, SEEK_CUR));
30+
31+
/* Truncate the file using ftruncate(). */
32+
assert(0 == ftruncate(fd, 300));
33+
assert(0 == fstat(fd, &st));
34+
assert(st.st_size == 300);
35+
assert(0 == lseek(fd, 0, SEEK_CUR));
36+
37+
assert(0 == close(fd));
38+
return 0;
39+
}

test/wasi/test-wasi.js

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ if (process.argv[2] === 'wasi-child') {
7171
runWASI({ test: 'exitcode', exitCode: 120 });
7272
runWASI({ test: 'fd_prestat_get_refresh' });
7373
runWASI({ test: 'freopen', stdout: `hello from input2.txt${EOL}` });
74+
runWASI({ test: 'ftruncate' });
7475
runWASI({ test: 'getentropy' });
7576

7677
// Tests that are currently unsupported on IBM i PASE.

test/wasi/wasm/ftruncate.wasm

33.3 KB
Binary file not shown.

0 commit comments

Comments
 (0)