Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/lib/libworkerfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
* SPDX-License-Identifier: MIT
*/

#if !ENVIRONMENT_MAY_BE_WORKER
#error "libworkerfs.js requires worker to be in ENVIRONMENT"
#endif


addToLibrary({
$WORKERFS__deps: ['$FS'],
$WORKERFS: {
Expand Down
71 changes: 48 additions & 23 deletions test/fs/test_workerfs_read.c → test/fs/test_workerfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,46 +15,60 @@
#include <fcntl.h>
#include <dirent.h>

#define SECRET_LEN 10

int main() {
int fd;
int fd2;
int rtn;
void test_no_exist() {
struct stat st;
struct stat st2;
char buf[100];
char secret2[] = SECRET2;
int len2 = SECRET_LEN / 2;

rtn = stat("/work/notexist.txt", &st);
int rtn = stat("/work/notexist.txt", &st);
assert(rtn == -1 && errno == ENOENT);
}

rtn = stat("/work/blob.txt", &st);
void test_blob_txt() {
// Check that /work/blob.txt contains the expected data.
const char content[] = "hello blob";
size_t file_len = sizeof(content) - 1;

struct stat st;
char buf[100];
int rtn = stat("/work/blob.txt", &st);
assert(rtn == 0);

fd = open("/work/blob.txt", O_RDWR, 0666);
int fd = open("/work/blob.txt", O_RDWR, 0666);
assert(fd >= 0);

rtn = read(fd, buf, 1000);
assert(rtn == SECRET_LEN);
assert(strncmp(buf, SECRET, SECRET_LEN) == 0);
ssize_t cnt = read(fd, buf, 1000);
printf("read blob.txt: %ld, expecting: %lu\n", cnt, file_len);
assert(cnt == file_len);
assert(strcmp(buf, content) == 0);
}

void test_file_txt() {
// Seek half way through /work/file.txt then verify the data read from that point on.
const char content[] = "hello file";
size_t file_len = sizeof(content) - 1;
char buf[100];
off_t offset = file_len / 2;

fd2 = open("/work/file.txt", O_RDONLY, 0666);
assert(fd2 != -1);
int fd = open("/work/file.txt", O_RDONLY, 0666);
assert(fd != -1);

rtn = lseek(fd2, len2, SEEK_SET);
assert(rtn == len2);
off_t rtn = lseek(fd, offset, SEEK_SET);
assert(rtn == offset);

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

void test_chmod() {
struct stat st, st2;
stat("/work/file.txt", &st);
chmod("/work/file.txt", 0640);
stat("/work/file.txt", &st2);
assert(st.st_mode == (0777 | S_IFREG) && st2.st_mode == (0640 | S_IFREG));
}

void test_readdir() {
DIR *pDir = opendir("/work/");
assert(pDir);

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

assert(blobFileExists);
assert(fileTxtExists);
}

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

int main() {
test_no_exist();
test_blob_txt();
test_file_txt();
test_chmod();
test_readdir();
test_readlink();
return 0;
}
12 changes: 5 additions & 7 deletions test/test_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1420,21 +1420,19 @@ def test_fs_memfs_fsync(self):
secret = str(time.time())
self.btest_exit('fs/test_memfs_fsync.c', cflags=args + [f'-DSECRET="{secret}"'])

def test_fs_workerfs_read(self):
secret = 'a' * 10
secret2 = 'b' * 10
def test_fs_workerfs(self):
create_file('pre.js', '''
Module.preRun = () => {
var blob = new Blob(['%s']);
var file = new File(['%s'], 'file.txt');
var blob = new Blob(['hello blob']);
var file = new File(['hello file'], 'file.txt');
FS.mkdir('/work');
FS.mount(WORKERFS, {
blobs: [{ name: 'blob.txt', data: blob }],
files: [file],
}, '/work');
};
''' % (secret, secret2))
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'])
''')
self.btest_exit('fs/test_workerfs.c', cflags=['-lworkerfs.js', '--pre-js', 'pre.js', '--proxy-to-worker', '-Wno-deprecated', '-lworkerfs.js'])

def test_fs_workerfs_package(self):
create_file('file1.txt', 'first')
Expand Down