diff options
author | Alon Zakai <alonzakai@gmail.com> | 2013-09-29 11:51:22 -0700 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2013-09-29 11:51:22 -0700 |
commit | 61221f3dd86360875f1a6ece4aebaa0a20bc025c (patch) | |
tree | 7ea64acb419554505a891a2b0b06537077216268 /tests | |
parent | 51256ab4452f9b1aa3774ef2eece26faa652ab22 (diff) | |
parent | 4db117910eb13fc93d632dd4e3fb4cf127544538 (diff) |
Merge pull request #1601 from inolen/idbfs
NODEFS and IDBFS support
Diffstat (limited to 'tests')
-rw-r--r-- | tests/fs/test_idbfs_sync.c | 48 | ||||
-rw-r--r-- | tests/fs/test_nodefs_rw.c | 49 | ||||
-rw-r--r-- | tests/test_browser.py | 5 | ||||
-rw-r--r-- | tests/test_core.py | 50 | ||||
-rw-r--r-- | tests/unistd/access.c | 17 | ||||
-rw-r--r-- | tests/unistd/access.out | 40 | ||||
-rw-r--r-- | tests/unistd/io.c | 13 | ||||
-rw-r--r-- | tests/unistd/links.c | 21 | ||||
-rw-r--r-- | tests/unistd/links.out | 6 | ||||
-rw-r--r-- | tests/unistd/misc.c | 24 | ||||
-rw-r--r-- | tests/unistd/truncate.c | 23 | ||||
-rw-r--r-- | tests/unistd/unlink.c | 12 |
12 files changed, 233 insertions, 75 deletions
diff --git a/tests/fs/test_idbfs_sync.c b/tests/fs/test_idbfs_sync.c new file mode 100644 index 00000000..ff356416 --- /dev/null +++ b/tests/fs/test_idbfs_sync.c @@ -0,0 +1,48 @@ +#include <stdio.h> +#include <emscripten.h> + +#define EM_ASM_REEXPAND(x) EM_ASM(x) + +void success() { + int result = 1; + REPORT_RESULT(); +} + +int main() { + EM_ASM( + FS.mkdir('/working'); + FS.mount(IDBFS, {}, '/working'); + ); + +#if FIRST + // store local files to backing IDB + EM_ASM_REEXPAND( + FS.writeFile('/working/waka.txt', 'az'); + FS.writeFile('/working/moar.txt', SECRET); + FS.syncfs(function (err) { + assert(!err); + + ccall('success', 'v', '', []); + }); + ); +#else + // load files from backing IDB + EM_ASM_REEXPAND( + FS.syncfs(true, function (err) { + assert(!err); + + var contents = FS.readFile('/working/waka.txt', { encoding: 'utf8' }); + assert(contents === 'az'); + + var secret = FS.readFile('/working/moar.txt', { encoding: 'utf8' }); + assert(secret === SECRET); + + ccall('success', 'v', '', []); + }); + ); +#endif + + emscripten_exit_with_live_runtime(); + + return 0; +} diff --git a/tests/fs/test_nodefs_rw.c b/tests/fs/test_nodefs_rw.c new file mode 100644 index 00000000..140da332 --- /dev/null +++ b/tests/fs/test_nodefs_rw.c @@ -0,0 +1,49 @@ +#include <assert.h> +#include <stdio.h> +#include <emscripten.h> + +int main() { + FILE *file; + int res; + char buffer[512]; + + // write something locally with node + EM_ASM( + var fs = require('fs'); + fs.writeFileSync('foobar.txt', 'yeehaw'); + ); + + // mount the current folder as a NODEFS instance + // inside of emscripten + EM_ASM( + FS.mkdir('/working'); + FS.mount(NODEFS, { root: '.' }, '/working'); + ); + + // read and validate the contents of the file + file = fopen("/working/foobar.txt", "r"); + assert(file); + res = fread(buffer, sizeof(char), 6, file); + assert(res == 6); + fclose(file); + + assert(!strcmp(buffer, "yeehaw")); + + // write out something new + file = fopen("/working/foobar.txt", "w"); + assert(file); + res = fwrite("cheez", sizeof(char), 5, file); + assert(res == 5); + fclose(file); + + // validate the changes were persisted to the underlying fs + EM_ASM( + var fs = require('fs'); + var contents = fs.readFileSync('foobar.txt', { encoding: 'utf8' }); + assert(contents === 'cheez'); + ); + + puts("success"); + + return 0; +} diff --git a/tests/test_browser.py b/tests/test_browser.py index d50488ec..799759a1 100644 --- a/tests/test_browser.py +++ b/tests/test_browser.py @@ -880,6 +880,11 @@ keydown(100);keyup(100); // trigger the end self.btest('file_db.cpp', secret, args=['--preload-file', 'moar.txt']) # even with a file there, we load over it shutil.move('test.html', 'third.html') + def test_fs_idbfs_sync(self): + secret = str(time.time()) + self.btest(path_from_root('tests', 'fs', 'test_idbfs_sync.c'), '1', force_c=True, args=['-DFIRST', '-DSECRET=\'' + secret + '\'', '-s', '''EXPORTED_FUNCTIONS=['_main', '_success']''']) + self.btest(path_from_root('tests', 'fs', 'test_idbfs_sync.c'), '1', force_c=True, args=['-DSECRET=\'' + secret + '\'', '-s', '''EXPORTED_FUNCTIONS=['_main', '_success']''']) + def test_sdl_pumpevents(self): # key events should be detected using SDL_PumpEvents open(os.path.join(self.get_dir(), 'pre.js'), 'w').write(''' diff --git a/tests/test_core.py b/tests/test_core.py index d59fae40..f41d59ab 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -7742,12 +7742,18 @@ def process(filename): finally: Settings.INCLUDE_FULL_LIBRARY = 0 + def test_fs_nodefs_rw(self): + src = open(path_from_root('tests', 'fs', 'test_nodefs_rw.c'), 'r').read() + self.do_run(src, 'success', force_c=True) + def test_unistd_access(self): if Settings.ASM_JS: Settings.ASM_JS = 2 # skip validation, asm does not support random code if not self.is_le32(): return self.skip('le32 needed for inline js') - src = open(path_from_root('tests', 'unistd', 'access.c'), 'r').read() - expected = open(path_from_root('tests', 'unistd', 'access.out'), 'r').read() - self.do_run(src, expected) + for fs in ['MEMFS', 'NODEFS']: + src = open(path_from_root('tests', 'unistd', 'access.c'), 'r').read() + expected = open(path_from_root('tests', 'unistd', 'access.out'), 'r').read() + Building.COMPILER_TEST_OPTS = ['-D' + fs] + self.do_run(src, expected) def test_unistd_curdir(self): if Settings.ASM_JS: Settings.ASM_JS = 2 # skip validation, asm does not support random code @@ -7783,9 +7789,11 @@ def process(filename): def test_unistd_truncate(self): if Settings.ASM_JS: Settings.ASM_JS = 2 # skip validation, asm does not support random code if not self.is_le32(): return self.skip('le32 needed for inline js') - src = open(path_from_root('tests', 'unistd', 'truncate.c'), 'r').read() - expected = open(path_from_root('tests', 'unistd', 'truncate.out'), 'r').read() - self.do_run(src, expected) + for fs in ['MEMFS', 'NODEFS']: + src = open(path_from_root('tests', 'unistd', 'truncate.c'), 'r').read() + expected = open(path_from_root('tests', 'unistd', 'truncate.out'), 'r').read() + Building.COMPILER_TEST_OPTS = ['-D' + fs] + self.do_run(src, expected) def test_unistd_swab(self): src = open(path_from_root('tests', 'unistd', 'swab.c'), 'r').read() @@ -7807,15 +7815,19 @@ def process(filename): self.do_run(src, expected) def test_unistd_unlink(self): - src = open(path_from_root('tests', 'unistd', 'unlink.c'), 'r').read() - self.do_run(src, 'success', force_c=True) + for fs in ['MEMFS', 'NODEFS']: + src = open(path_from_root('tests', 'unistd', 'unlink.c'), 'r').read() + Building.COMPILER_TEST_OPTS = ['-D' + fs] + self.do_run(src, 'success', force_c=True) def test_unistd_links(self): if Settings.ASM_JS: Settings.ASM_JS = 2 # skip validation, asm does not support random code if not self.is_le32(): return self.skip('le32 needed for inline js') - src = open(path_from_root('tests', 'unistd', 'links.c'), 'r').read() - expected = open(path_from_root('tests', 'unistd', 'links.out'), 'r').read() - self.do_run(src, expected) + for fs in ['MEMFS', 'NODEFS']: + src = open(path_from_root('tests', 'unistd', 'links.c'), 'r').read() + expected = open(path_from_root('tests', 'unistd', 'links.out'), 'r').read() + Building.COMPILER_TEST_OPTS = ['-D' + fs] + self.do_run(src, expected) def test_unistd_sleep(self): src = open(path_from_root('tests', 'unistd', 'sleep.c'), 'r').read() @@ -7826,14 +7838,18 @@ def process(filename): if Settings.ASM_JS: Settings.ASM_JS = 2 # skip validation, asm does not support random code if not self.is_le32(): return self.skip('le32 needed for inline js') if self.run_name == 'o2': return self.skip('non-asm optimized builds can fail with inline js') - src = open(path_from_root('tests', 'unistd', 'io.c'), 'r').read() - expected = open(path_from_root('tests', 'unistd', 'io.out'), 'r').read() - self.do_run(src, expected) + for fs in ['MEMFS', 'NODEFS']: + src = open(path_from_root('tests', 'unistd', 'io.c'), 'r').read() + expected = open(path_from_root('tests', 'unistd', 'io.out'), 'r').read() + Building.COMPILER_TEST_OPTS = ['-D' + fs] + self.do_run(src, expected) def test_unistd_misc(self): - src = open(path_from_root('tests', 'unistd', 'misc.c'), 'r').read() - expected = open(path_from_root('tests', 'unistd', 'misc.out'), 'r').read() - self.do_run(src, expected) + for fs in ['MEMFS', 'NODEFS']: + src = open(path_from_root('tests', 'unistd', 'misc.c'), 'r').read() + expected = open(path_from_root('tests', 'unistd', 'misc.out'), 'r').read() + Building.COMPILER_TEST_OPTS = ['-D' + fs] + self.do_run(src, expected) def test_uname(self): src = r''' diff --git a/tests/unistd/access.c b/tests/unistd/access.c index 4d5ba08e..57d38f5c 100644 --- a/tests/unistd/access.c +++ b/tests/unistd/access.c @@ -5,14 +5,19 @@ int main() { EM_ASM( - FS.writeFile('/forbidden', ''); FS.chmod('/forbidden', 0000); - FS.writeFile('/readable', ''); FS.chmod('/readable', 0444); - FS.writeFile('/writeable', ''); FS.chmod('/writeable', 0222); - FS.writeFile('/allaccess', ''); FS.chmod('/allaccess', 0777); + FS.mkdir('working'); +#if NODEFS + FS.mount(NODEFS, { root: '.' }, 'working'); +#endif + FS.chdir('working'); + FS.writeFile('forbidden', ''); FS.chmod('forbidden', 0000); + FS.writeFile('readable', ''); FS.chmod('readable', 0444); + FS.writeFile('writeable', ''); FS.chmod('writeable', 0222); + FS.writeFile('allaccess', ''); FS.chmod('allaccess', 0777); ); - char* files[] = {"/readable", "/writeable", - "/allaccess", "/forbidden", "/nonexistent"}; + char* files[] = {"readable", "writeable", + "allaccess", "forbidden", "nonexistent"}; for (int i = 0; i < sizeof files / sizeof files[0]; i++) { printf("F_OK(%s): %d\n", files[i], access(files[i], F_OK)); printf("errno: %d\n", errno); diff --git a/tests/unistd/access.out b/tests/unistd/access.out index d462e5a5..b5e4a541 100644 --- a/tests/unistd/access.out +++ b/tests/unistd/access.out @@ -1,45 +1,45 @@ -F_OK(/readable): 0 +F_OK(readable): 0 errno: 0 -R_OK(/readable): 0 +R_OK(readable): 0 errno: 0 -X_OK(/readable): -1 +X_OK(readable): -1 errno: 13 -W_OK(/readable): -1 +W_OK(readable): -1 errno: 13 -F_OK(/writeable): 0 +F_OK(writeable): 0 errno: 0 -R_OK(/writeable): -1 +R_OK(writeable): -1 errno: 13 -X_OK(/writeable): -1 +X_OK(writeable): -1 errno: 13 -W_OK(/writeable): 0 +W_OK(writeable): 0 errno: 0 -F_OK(/allaccess): 0 +F_OK(allaccess): 0 errno: 0 -R_OK(/allaccess): 0 +R_OK(allaccess): 0 errno: 0 -X_OK(/allaccess): 0 +X_OK(allaccess): 0 errno: 0 -W_OK(/allaccess): 0 +W_OK(allaccess): 0 errno: 0 -F_OK(/forbidden): 0 +F_OK(forbidden): 0 errno: 0 -R_OK(/forbidden): -1 +R_OK(forbidden): -1 errno: 13 -X_OK(/forbidden): -1 +X_OK(forbidden): -1 errno: 13 -W_OK(/forbidden): -1 +W_OK(forbidden): -1 errno: 13 -F_OK(/nonexistent): -1 +F_OK(nonexistent): -1 errno: 2 -R_OK(/nonexistent): -1 +R_OK(nonexistent): -1 errno: 2 -X_OK(/nonexistent): -1 +X_OK(nonexistent): -1 errno: 2 -W_OK(/nonexistent): -1 +W_OK(nonexistent): -1 errno: 2 diff --git a/tests/unistd/io.c b/tests/unistd/io.c index 6bf22593..5dcdbbb6 100644 --- a/tests/unistd/io.c +++ b/tests/unistd/io.c @@ -7,6 +7,11 @@ int main() { EM_ASM( + FS.mkdir('/working'); +#if NODEFS + FS.mount(NODEFS, { root: '.' }, '/working'); +#endif + var major = 80; var device = FS.makedev(major++, 0); @@ -51,14 +56,14 @@ int main() { FS.createDevice('/', 'createDevice-read-only', function() {}); FS.createDevice('/', 'createDevice-write-only', null, function() {}); - FS.mkdir('/folder', 0777); - FS.writeFile('/file', '1234567890'); + FS.mkdir('/working/folder'); + FS.writeFile('/working/file', '1234567890'); ); char readBuffer[256] = {0}; char writeBuffer[] = "writeme"; - int fl = open("/folder", O_RDWR); + int fl = open("/working/folder", O_RDWR); printf("read from folder: %d\n", read(fl, readBuffer, sizeof readBuffer)); printf("errno: %d\n", errno); errno = 0; @@ -97,7 +102,7 @@ int main() { printf("open write-only device from createDevice for write, errno: %d\n\n", errno); errno = 0; - int f = open("/file", O_RDWR); + int f = open("/working/file", O_RDWR); printf("read from file: %d\n", read(f, readBuffer, sizeof readBuffer)); printf("data: %s\n", readBuffer); memset(readBuffer, 0, sizeof readBuffer); diff --git a/tests/unistd/links.c b/tests/unistd/links.c index 5b403c1f..c46c6294 100644 --- a/tests/unistd/links.c +++ b/tests/unistd/links.c @@ -5,12 +5,17 @@ int main() { EM_ASM( - FS.symlink('../test/../there!', '/link'); - FS.writeFile('/file', 'test'); - FS.mkdir('/folder'); + FS.mkdir('working'); +#if NODEFS + FS.mount(NODEFS, { root: '.' }, 'working'); +#endif + FS.chdir('working'); + FS.symlink('../test/../there!', 'link'); + FS.writeFile('file', 'test'); + FS.mkdir('folder'); ); - char* files[] = {"/link", "/file", "/folder"}; + char* files[] = {"link", "file", "folder"}; char buffer[256] = {0}; for (int i = 0; i < sizeof files / sizeof files[0]; i++) { @@ -22,23 +27,23 @@ int main() { } printf("symlink/overwrite\n"); - printf("ret: %d\n", symlink("new-nonexistent-path", "/link")); + printf("ret: %d\n", symlink("new-nonexistent-path", "link")); printf("errno: %d\n\n", errno); errno = 0; printf("symlink/normal\n"); - printf("ret: %d\n", symlink("new-nonexistent-path", "/folder/link")); + printf("ret: %d\n", symlink("new-nonexistent-path", "folder/link")); printf("errno: %d\n", errno); errno = 0; printf("readlink(created link)\n"); - printf("ret: %d\n", readlink("/folder/link", buffer, 256)); + printf("ret: %d\n", readlink("folder/link", buffer, 256)); printf("errno: %d\n", errno); printf("result: %s\n\n", buffer); errno = 0; printf("readlink(short buffer)\n"); - printf("ret: %d\n", readlink("/link", buffer, 4)); + printf("ret: %d\n", readlink("link", buffer, 4)); printf("errno: %d\n", errno); printf("result: %s\n", buffer); errno = 0; diff --git a/tests/unistd/links.out b/tests/unistd/links.out index 75e410cb..f2a7aed6 100644 --- a/tests/unistd/links.out +++ b/tests/unistd/links.out @@ -1,14 +1,14 @@ -readlink(/link) +readlink(link) ret: 17 errno: 0 result: ../test/../there! -readlink(/file) +readlink(file) ret: -1 errno: 22 result: ../test/../there! -readlink(/folder) +readlink(folder) ret: -1 errno: 22 result: ../test/../there! diff --git a/tests/unistd/misc.c b/tests/unistd/misc.c index 5b0d63d2..2ca5b390 100644 --- a/tests/unistd/misc.c +++ b/tests/unistd/misc.c @@ -2,9 +2,17 @@ #include <errno.h> #include <unistd.h> #include <fcntl.h> +#include <emscripten.h> int main() { - int f = open("/", O_RDONLY); + EM_ASM( + FS.mkdir('working'); +#if NODEFS + FS.mount(NODEFS, { root: '.' }, 'working'); +#endif + ); + + int f = open("working", O_RDONLY); sync(); @@ -36,7 +44,7 @@ int main() { printf(", errno: %d\n", errno); errno = 0; - printf("link: %d", link("/here", "/there")); + printf("link: %d", link("working/here", "working/there")); printf(", errno: %d\n", errno); errno = 0; @@ -65,10 +73,10 @@ int main() { char* exec_argv[] = {"arg", 0}; char* exec_env[] = {"a=b", 0}; - printf("execl: %d", execl("/program", "arg", 0)); + printf("execl: %d", execl("working/program", "arg", 0)); printf(", errno: %d\n", errno); errno = 0; - printf("execle: %d", execle("/program", "arg", 0, exec_env)); + printf("execle: %d", execle("working/program", "arg", 0, exec_env)); printf(", errno: %d\n", errno); errno = 0; printf("execlp: %d", execlp("program", "arg", 0)); @@ -84,16 +92,16 @@ int main() { printf(", errno: %d\n", errno); errno = 0; - printf("chown(good): %d", chown("/", 123, 456)); + printf("chown(good): %d", chown("working", 123, 456)); printf(", errno: %d\n", errno); errno = 0; - printf("chown(bad): %d", chown("/noexist", 123, 456)); + printf("chown(bad): %d", chown("working/noexist", 123, 456)); printf(", errno: %d\n", errno); errno = 0; - printf("lchown(good): %d", lchown("/", 123, 456)); + printf("lchown(good): %d", lchown("working", 123, 456)); printf(", errno: %d\n", errno); errno = 0; - printf("lchown(bad): %d", lchown("/noexist", 123, 456)); + printf("lchown(bad): %d", lchown("working/noexist", 123, 456)); printf(", errno: %d\n", errno); errno = 0; printf("fchown(good): %d", fchown(f, 123, 456)); diff --git a/tests/unistd/truncate.c b/tests/unistd/truncate.c index b1d9fc96..e63a4c13 100644 --- a/tests/unistd/truncate.c +++ b/tests/unistd/truncate.c @@ -8,14 +8,19 @@ int main() { EM_ASM( - FS.writeFile('/towrite', 'abcdef'); - FS.writeFile('/toread', 'abcdef'); - FS.chmod('/toread', 0444); + FS.mkdir('working'); +#if NODEFS + FS.mount(NODEFS, { root: '.' }, 'working'); +#endif + FS.chdir('working'); + FS.writeFile('towrite', 'abcdef'); + FS.writeFile('toread', 'abcdef'); + FS.chmod('toread', 0444); ); struct stat s; - int f = open("/towrite", O_WRONLY); - int f2 = open("/toread", O_RDONLY); + int f = open("towrite", O_WRONLY); + int f2 = open("toread", O_RDONLY); printf("f2: %d\n", f2); fstat(f, &s); @@ -48,17 +53,17 @@ int main() { errno = 0; printf("\n"); - printf("truncate(2): %d\n", truncate("/towrite", 2)); + printf("truncate(2): %d\n", truncate("towrite", 2)); printf("errno: %d\n", errno); - stat("/towrite", &s); + stat("towrite", &s); printf("st_size: %d\n", s.st_size); memset(&s, 0, sizeof s); errno = 0; printf("\n"); - printf("truncate(readonly, 2): %d\n", truncate("/toread", 2)); + printf("truncate(readonly, 2): %d\n", truncate("toread", 2)); printf("errno: %d\n", errno); - stat("/toread", &s); + stat("toread", &s); printf("st_size: %d\n", s.st_size); memset(&s, 0, sizeof s); errno = 0; diff --git a/tests/unistd/unlink.c b/tests/unistd/unlink.c index f0a8f4dd..9f532325 100644 --- a/tests/unistd/unlink.c +++ b/tests/unistd/unlink.c @@ -7,6 +7,9 @@ #include <string.h> #include <unistd.h> #include <sys/stat.h> +#if EMSCRIPTEN +#include <emscripten.h> +#endif static void create_file(const char *path, const char *buffer, int mode) { int fd = open(path, O_WRONLY | O_CREAT | O_EXCL, mode); @@ -19,6 +22,15 @@ static void create_file(const char *path, const char *buffer, int mode) { } void setup() { + mkdir("working", 0777); +#if EMSCRIPTEN + EM_ASM( +#if NODEFS + FS.mount(NODEFS, { root: '.' }, 'working'); +#endif + ); +#endif + chdir("working"); create_file("file", "test", 0777); create_file("file1", "test", 0777); symlink("file1", "file1-link"); |