Skip to content

Commit c411566

Browse files
ethanaleemmarczell-graphisoft
authored andcommitted
[WASMFS] Comprehensive dup test (emscripten-core#15550)
Relevant Issue: emscripten-core#15041 Verifies that this issue has been fixed with the dup syscall: emscripten-core#4017
1 parent 735cf0b commit c411566

File tree

3 files changed

+80
-2
lines changed

3 files changed

+80
-2
lines changed

tests/test_other.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11153,8 +11153,10 @@ def test_unistd_fstatfs(self):
1115311153

1115411154
# WASMFS tests
1115511155

11156-
@also_with_wasmfs
11157-
def test_unistd_dup(self):
11156+
# TODO: This test will only work with the new file system.
11157+
# Addresses this issue: https://github.com/emscripten-core/emscripten/issues/4017
11158+
# The new file system also correctly identifies errors that the JS file system missed.
11159+
def test_wasmfs_dup(self):
1115811160
self.set_setting('WASMFS')
1115911161
self.do_run_in_out_file_test('wasmfs/wasmfs_dup.c')
1116011162

tests/wasmfs/wasmfs_dup.c

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <errno.h>
1010
#include <fcntl.h>
1111
#include <stdio.h>
12+
#include <sys/stat.h>
1213
#include <unistd.h>
1314

1415
// FIXME: Merge this standalone test back into dup.c after new FS can support
@@ -56,12 +57,14 @@ int main() {
5657
errno = 0;
5758
f5 = dup2(f4, -1);
5859
assert(f5 == -1);
60+
// This error is not reported in the JS filesystem.
5961
assert(errno == EBADF);
6062

6163
// Try calling dup2 with an invalid oldfd
6264
errno = 0;
6365
int f6 = dup2(-1, f5);
6466
assert(f6 == -1);
67+
// This error is not reported in the JS filesystem.
6568
assert(errno == EBADF);
6669

6770
// Try assigning a large fd
@@ -72,8 +75,57 @@ int main() {
7275

7376
errno = 0;
7477
int f9 = dup(-1);
78+
// This error is not reported in the JS filesystem.
7579
assert(f9 == -1);
7680
assert(errno == EBADF);
7781

82+
off_t offset;
83+
84+
errno = 0;
85+
printf("DUP\n");
86+
mkdir("working", 0700);
87+
f = open("working/file", O_RDWR | O_CREAT);
88+
f2 = open("working/file", O_RDONLY);
89+
f3 = dup(f);
90+
printf("errno: %d\n", errno);
91+
printf("f: %d\n", f != f2 && f != f3);
92+
printf("f2,f3: %d\n", f2 != f3);
93+
94+
// dup()ed file descriptors should share all flags and even seek position
95+
offset = lseek(f3, 0, SEEK_CUR);
96+
printf("1. f3 offset was %d. Should be 0\n", (int)offset);
97+
offset = lseek(f, 1, SEEK_SET);
98+
printf("2. f offset set to %d. Should be 1\n", (int)offset);
99+
offset = lseek(f2, 2, SEEK_SET);
100+
printf("3. f2 offset set to %d. Should be 2\n", (int)offset);
101+
offset = lseek(f, 0, SEEK_CUR);
102+
printf("4. f offset now is %d. Should be 1\n", (int)offset);
103+
offset = lseek(f2, 0, SEEK_CUR);
104+
printf("5. f2 offset now is %d. Should be 2\n", (int)offset);
105+
offset = lseek(f3, 0, SEEK_CUR);
106+
printf("6. f3 offset now is %d. Should be 1 (and not 0)\n", (int)offset);
107+
offset = lseek(f3, 3, SEEK_SET);
108+
printf("7. f3 offset set to %d. Should be 3\n", (int)offset);
109+
offset = lseek(f, 0, SEEK_CUR);
110+
printf("8. f offset now is %d. Should be 3 (and not 1)\n", (int)offset);
111+
112+
printf("close(f1): %d\n", close(f));
113+
printf("close(f2): %d\n", close(f2));
114+
printf("close(f3): %d\n", close(f3));
115+
printf("\n");
116+
errno = 0;
117+
118+
printf("DUP2\n");
119+
f = open("/", O_RDONLY);
120+
f2 = open("/", O_RDONLY);
121+
f3 = dup2(f, f2);
122+
printf("errno: %d\n", errno);
123+
printf("f: %d\n", f != f2 && f != f3);
124+
printf("f2,f3: %d\n", f2 == f3);
125+
printf("close(f1): %d\n", close(f));
126+
printf("close(f2): %d\n", close(f2));
127+
printf("close(f3): %d\n", close(f3));
128+
errno = 0;
129+
78130
return 0;
79131
}

tests/wasmfs/wasmfs_dup.out

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
DUP
12
STDOUT
23
CAN PRINT TO STDOUT WITH fd = 3
34
DUP2
@@ -6,3 +7,26 @@ CAN PRINT TO STDOUT WITH fd = 5
67
CAN PRINT TO STDOUT WITH fd = 5
78
CAN PRINT TO STDOUT WITH fd = 5
89
CAN PRINT TO STDOUT WITH f8 = 4069
10+
DUP
11+
errno: 0
12+
f: 1
13+
f2,f3: 1
14+
1. f3 offset was 0. Should be 0
15+
2. f offset set to 1. Should be 1
16+
3. f2 offset set to 2. Should be 2
17+
4. f offset now is 1. Should be 1
18+
5. f2 offset now is 2. Should be 2
19+
6. f3 offset now is 1. Should be 1 (and not 0)
20+
7. f3 offset set to 3. Should be 3
21+
8. f offset now is 3. Should be 3 (and not 1)
22+
close(f1): 0
23+
close(f2): 0
24+
close(f3): 0
25+
26+
DUP2
27+
errno: 0
28+
f: 1
29+
f2,f3: 1
30+
close(f1): 0
31+
close(f2): 0
32+
close(f3): 0

0 commit comments

Comments
 (0)