Skip to content

Commit 5476e75

Browse files
authored
Refactor test_workerfs. NFC (emscripten-core#25813)
- Split tests into separate functions. - Rename test (its not just about reading). - Remove use of "secret" terminology
1 parent c6833e4 commit 5476e75

File tree

3 files changed

+58
-30
lines changed

3 files changed

+58
-30
lines changed

src/lib/libworkerfs.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
* SPDX-License-Identifier: MIT
55
*/
66

7+
#if !ENVIRONMENT_MAY_BE_WORKER
8+
#error "libworkerfs.js requires worker to be in ENVIRONMENT"
9+
#endif
10+
11+
712
addToLibrary({
813
$WORKERFS__deps: ['$FS'],
914
$WORKERFS: {

test/fs/test_workerfs_read.c renamed to test/fs/test_workerfs.c

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,46 +15,60 @@
1515
#include <fcntl.h>
1616
#include <dirent.h>
1717

18-
#define SECRET_LEN 10
1918

20-
int main() {
21-
int fd;
22-
int fd2;
23-
int rtn;
19+
void test_no_exist() {
2420
struct stat st;
25-
struct stat st2;
26-
char buf[100];
27-
char secret2[] = SECRET2;
28-
int len2 = SECRET_LEN / 2;
29-
30-
rtn = stat("/work/notexist.txt", &st);
21+
int rtn = stat("/work/notexist.txt", &st);
3122
assert(rtn == -1 && errno == ENOENT);
23+
}
3224

33-
rtn = stat("/work/blob.txt", &st);
25+
void test_blob_txt() {
26+
// Check that /work/blob.txt contains the expected data.
27+
const char content[] = "hello blob";
28+
size_t file_len = sizeof(content) - 1;
29+
30+
struct stat st;
31+
char buf[100];
32+
int rtn = stat("/work/blob.txt", &st);
3433
assert(rtn == 0);
3534

36-
fd = open("/work/blob.txt", O_RDWR, 0666);
35+
int fd = open("/work/blob.txt", O_RDWR, 0666);
3736
assert(fd >= 0);
3837

39-
rtn = read(fd, buf, 1000);
40-
assert(rtn == SECRET_LEN);
41-
assert(strncmp(buf, SECRET, SECRET_LEN) == 0);
38+
ssize_t cnt = read(fd, buf, 1000);
39+
printf("read blob.txt: %ld, expecting: %lu\n", cnt, file_len);
40+
assert(cnt == file_len);
41+
assert(strcmp(buf, content) == 0);
42+
}
43+
44+
void test_file_txt() {
45+
// Seek half way through /work/file.txt then verify the data read from that point on.
46+
const char content[] = "hello file";
47+
size_t file_len = sizeof(content) - 1;
48+
char buf[100];
49+
off_t offset = file_len / 2;
4250

43-
fd2 = open("/work/file.txt", O_RDONLY, 0666);
44-
assert(fd2 != -1);
51+
int fd = open("/work/file.txt", O_RDONLY, 0666);
52+
assert(fd != -1);
4553

46-
rtn = lseek(fd2, len2, SEEK_SET);
47-
assert(rtn == len2);
54+
off_t rtn = lseek(fd, offset, SEEK_SET);
55+
assert(rtn == offset);
4856

49-
rtn = read(fd2, buf, len2);
50-
assert(rtn == len2);
51-
assert(strncmp(buf, secret2 + len2, len2) == 0);
57+
ssize_t cnt = read(fd, buf, 1000);
58+
printf("read file.txt: %ld, expecting: %llu\n", cnt, file_len - offset);
59+
assert(cnt == file_len - offset);
60+
assert(strncmp(buf, content + offset, file_len - offset) == 0);
61+
}
5262

63+
void test_chmod() {
64+
struct stat st, st2;
5365
stat("/work/file.txt", &st);
5466
chmod("/work/file.txt", 0640);
5567
stat("/work/file.txt", &st2);
5668
assert(st.st_mode == (0777 | S_IFREG) && st2.st_mode == (0640 | S_IFREG));
69+
}
5770

71+
void test_readdir() {
5872
DIR *pDir = opendir("/work/");
5973
assert(pDir);
6074

@@ -72,12 +86,23 @@ int main() {
7286

7387
assert(blobFileExists);
7488
assert(fileTxtExists);
89+
}
7590

91+
void test_readlink() {
7692
// attemping to read a worker node as a link should result in EINVAL
93+
char buf[100];
7794
buf[0] = '\0';
7895
assert(readlink("/work/blob.txt", buf, sizeof(buf)) == -1);
7996
assert(buf[0] == '\0');
8097
assert(errno == EINVAL);
98+
}
8199

100+
int main() {
101+
test_no_exist();
102+
test_blob_txt();
103+
test_file_txt();
104+
test_chmod();
105+
test_readdir();
106+
test_readlink();
82107
return 0;
83108
}

test/test_browser.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1420,21 +1420,19 @@ def test_fs_memfs_fsync(self):
14201420
secret = str(time.time())
14211421
self.btest_exit('fs/test_memfs_fsync.c', cflags=args + [f'-DSECRET="{secret}"'])
14221422

1423-
def test_fs_workerfs_read(self):
1424-
secret = 'a' * 10
1425-
secret2 = 'b' * 10
1423+
def test_fs_workerfs(self):
14261424
create_file('pre.js', '''
14271425
Module.preRun = () => {
1428-
var blob = new Blob(['%s']);
1429-
var file = new File(['%s'], 'file.txt');
1426+
var blob = new Blob(['hello blob']);
1427+
var file = new File(['hello file'], 'file.txt');
14301428
FS.mkdir('/work');
14311429
FS.mount(WORKERFS, {
14321430
blobs: [{ name: 'blob.txt', data: blob }],
14331431
files: [file],
14341432
}, '/work');
14351433
};
1436-
''' % (secret, secret2))
1437-
self.btest_exit('fs/test_workerfs_read.c', cflags=['-lworkerfs.js', '--pre-js', 'pre.js', f'-DSECRET="{secret}"', f'-DSECRET2="{secret2}"', '--proxy-to-worker', '-Wno-deprecated', '-lworkerfs.js'])
1434+
''')
1435+
self.btest_exit('fs/test_workerfs.c', cflags=['-lworkerfs.js', '--pre-js', 'pre.js', '--proxy-to-worker', '-Wno-deprecated', '-lworkerfs.js'])
14381436

14391437
def test_fs_workerfs_package(self):
14401438
create_file('file1.txt', 'first')

0 commit comments

Comments
 (0)