diff options
author | max99x <max99x@gmail.com> | 2011-07-23 05:49:48 +0300 |
---|---|---|
committer | max99x <max99x@gmail.com> | 2011-07-23 05:49:48 +0300 |
commit | 136756f734ecf14a28736900075d561e981e973e (patch) | |
tree | cf33472ea6550c6486ec7722facf34d98a16a51d /tests | |
parent | 586d229ec311daa5e89781eb6da822989e677789 (diff) |
Added unistd tests; fixed a lot of unistd bugs and deficiencies.
Diffstat (limited to 'tests')
43 files changed, 2192 insertions, 0 deletions
diff --git a/tests/runner.py b/tests/runner.py index b673b45c..3ccbb7d8 100644 --- a/tests/runner.py +++ b/tests/runner.py @@ -2415,6 +2415,139 @@ if 'benchmark' not in sys.argv: expected = open(path_from_root('tests', 'filesystem', 'output.txt'), 'r').read() self.do_test(src, expected, post_build=addJS) + def test_unistd_access(self): + def addPreRun(filename): + src = open(filename, 'r').read().replace( + '// {{PRE_RUN_ADDITIONS}}', + open(path_from_root('tests', 'unistd', 'access.js'), 'r').read() + ) + open(filename, 'w').write(src) + src = open(path_from_root('tests', 'unistd', 'access.c'), 'r').read() + expected = open(path_from_root('tests', 'unistd', 'access.out'), 'r').read() + self.do_test(src, expected, post_build=addPreRun) + + def test_unistd_curdir(self): + def addPreRun(filename): + src = open(filename, 'r').read().replace( + '// {{PRE_RUN_ADDITIONS}}', + open(path_from_root('tests', 'unistd', 'curdir.js'), 'r').read() + ) + open(filename, 'w').write(src) + src = open(path_from_root('tests', 'unistd', 'curdir.c'), 'r').read() + expected = open(path_from_root('tests', 'unistd', 'curdir.out'), 'r').read() + self.do_test(src, expected, post_build=addPreRun) + + def test_unistd_close(self): + src = open(path_from_root('tests', 'unistd', 'close.c'), 'r').read() + expected = open(path_from_root('tests', 'unistd', 'close.out'), 'r').read() + self.do_test(src, expected) + + def test_unistd_confstr(self): + src = open(path_from_root('tests', 'unistd', 'confstr.c'), 'r').read() + expected = open(path_from_root('tests', 'unistd', 'confstr.out'), 'r').read() + self.do_test(src, expected) + + def test_unistd_ttyname(self): + def addPreRun(filename): + src = open(filename, 'r').read().replace( + '// {{PRE_RUN_ADDITIONS}}', + open(path_from_root('tests', 'unistd', 'ttyname.js'), 'r').read() + ) + open(filename, 'w').write(src) + src = open(path_from_root('tests', 'unistd', 'ttyname.c'), 'r').read() + expected = open(path_from_root('tests', 'unistd', 'ttyname.out'), 'r').read() + self.do_test(src, expected, post_build=addPreRun) + + def test_unistd_dup(self): + src = open(path_from_root('tests', 'unistd', 'dup.c'), 'r').read() + expected = open(path_from_root('tests', 'unistd', 'dup.out'), 'r').read() + self.do_test(src, expected) + + def test_unistd_pathconf(self): + src = open(path_from_root('tests', 'unistd', 'pathconf.c'), 'r').read() + expected = open(path_from_root('tests', 'unistd', 'pathconf.out'), 'r').read() + self.do_test(src, expected) + + def test_unistd_truncate(self): + def addPreRun(filename): + src = open(filename, 'r').read().replace( + '// {{PRE_RUN_ADDITIONS}}', + open(path_from_root('tests', 'unistd', 'truncate.js'), 'r').read() + ) + open(filename, 'w').write(src) + src = open(path_from_root('tests', 'unistd', 'truncate.c'), 'r').read() + expected = open(path_from_root('tests', 'unistd', 'truncate.out'), 'r').read() + self.do_test(src, expected, post_build=addPreRun) + + def test_unistd_swab(self): + src = open(path_from_root('tests', 'unistd', 'swab.c'), 'r').read() + expected = open(path_from_root('tests', 'unistd', 'swab.out'), 'r').read() + self.do_test(src, expected) + + def test_unistd_isatty(self): + def addPreRun(filename): + src = open(filename, 'r').read().replace( + '// {{PRE_RUN_ADDITIONS}}', + open(path_from_root('tests', 'unistd', 'isatty.js'), 'r').read() + ) + open(filename, 'w').write(src) + src = open(path_from_root('tests', 'unistd', 'isatty.c'), 'r').read() + expected = open(path_from_root('tests', 'unistd', 'isatty.out'), 'r').read() + self.do_test(src, expected, post_build=addPreRun) + + def test_unistd_sysconf(self): + src = open(path_from_root('tests', 'unistd', 'sysconf.c'), 'r').read() + expected = open(path_from_root('tests', 'unistd', 'sysconf.out'), 'r').read() + self.do_test(src, expected) + + def test_unistd_login(self): + src = open(path_from_root('tests', 'unistd', 'login.c'), 'r').read() + expected = open(path_from_root('tests', 'unistd', 'login.out'), 'r').read() + self.do_test(src, expected) + + def test_unistd_unlink(self): + def addPreRun(filename): + src = open(filename, 'r').read().replace( + '// {{PRE_RUN_ADDITIONS}}', + open(path_from_root('tests', 'unistd', 'unlink.js'), 'r').read() + ) + open(filename, 'w').write(src) + src = open(path_from_root('tests', 'unistd', 'unlink.c'), 'r').read() + expected = open(path_from_root('tests', 'unistd', 'unlink.out'), 'r').read() + self.do_test(src, expected, post_build=addPreRun) + + def test_unistd_links(self): + def addPreRun(filename): + src = open(filename, 'r').read().replace( + '// {{PRE_RUN_ADDITIONS}}', + open(path_from_root('tests', 'unistd', 'links.js'), 'r').read() + ) + open(filename, 'w').write(src) + src = open(path_from_root('tests', 'unistd', 'links.c'), 'r').read() + expected = open(path_from_root('tests', 'unistd', 'links.out'), 'r').read() + self.do_test(src, expected, post_build=addPreRun) + + def test_unistd_sleep(self): + src = open(path_from_root('tests', 'unistd', 'sleep.c'), 'r').read() + expected = open(path_from_root('tests', 'unistd', 'sleep.out'), 'r').read() + self.do_test(src, expected) + + def test_unistd_io(self): + def addPreRun(filename): + src = open(filename, 'r').read().replace( + '// {{PRE_RUN_ADDITIONS}}', + open(path_from_root('tests', 'unistd', 'io.js'), 'r').read() + ) + open(filename, 'w').write(src) + src = open(path_from_root('tests', 'unistd', 'io.c'), 'r').read() + expected = open(path_from_root('tests', 'unistd', 'io.out'), 'r').read() + self.do_test(src, expected, post_build=addPreRun) + + 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_test(src, expected) + ### 'Big' tests def test_fannkuch(self): diff --git a/tests/unistd/access.c b/tests/unistd/access.c new file mode 100644 index 00000000..89428610 --- /dev/null +++ b/tests/unistd/access.c @@ -0,0 +1,24 @@ +#include <stdio.h> +#include <errno.h> +#include <unistd.h> + +int main() { + 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); + errno = 0; + printf("R_OK(%s): %d\n", files[i], access(files[i], R_OK)); + printf("errno: %d\n", errno); + errno = 0; + printf("X_OK(%s): %d\n", files[i], access(files[i], X_OK)); + printf("errno: %d\n", errno); + errno = 0; + printf("W_OK(%s): %d\n", files[i], access(files[i], W_OK)); + printf("errno: %d\n", errno); + errno = 0; + printf("\n"); + } + return 0; +} diff --git a/tests/unistd/access.js b/tests/unistd/access.js new file mode 100644 index 00000000..ea9e6359 --- /dev/null +++ b/tests/unistd/access.js @@ -0,0 +1,4 @@ +FS.createDataFile('/', 'forbidden', '', false, false); +FS.createDataFile('/', 'readable', '', true, false); +FS.createDataFile('/', 'writeable', '', false, true); +FS.createDataFile('/', 'allaccess', '', true, true); diff --git a/tests/unistd/access.out b/tests/unistd/access.out new file mode 100644 index 00000000..dffe0b9e --- /dev/null +++ b/tests/unistd/access.out @@ -0,0 +1,45 @@ +F_OK(/readable): 0 +errno: 0 +R_OK(/readable): 0 +errno: 0 +X_OK(/readable): 0 +errno: 0 +W_OK(/readable): -1 +errno: 13 + +F_OK(/writeable): 0 +errno: 0 +R_OK(/writeable): -1 +errno: 13 +X_OK(/writeable): -1 +errno: 13 +W_OK(/writeable): 0 +errno: 0 + +F_OK(/allaccess): 0 +errno: 0 +R_OK(/allaccess): 0 +errno: 0 +X_OK(/allaccess): 0 +errno: 0 +W_OK(/allaccess): 0 +errno: 0 + +F_OK(/forbidden): 0 +errno: 0 +R_OK(/forbidden): -1 +errno: 13 +X_OK(/forbidden): -1 +errno: 13 +W_OK(/forbidden): -1 +errno: 13 + +F_OK(/nonexistent): -1 +errno: 2 +R_OK(/nonexistent): -1 +errno: 2 +X_OK(/nonexistent): -1 +errno: 2 +W_OK(/nonexistent): -1 +errno: 2 + diff --git a/tests/unistd/close.c b/tests/unistd/close.c new file mode 100644 index 00000000..7110d18a --- /dev/null +++ b/tests/unistd/close.c @@ -0,0 +1,26 @@ +#include <stdio.h> +#include <errno.h> +#include <unistd.h> +#include <fcntl.h> + +int main() { + int f = open("/", O_RDONLY); + + printf("fsync(opened): %d\n", fsync(f)); + printf("errno: %d\n", errno); + errno = 0; + + printf("close(opened): %d\n", close(f)); + printf("errno: %d\n", errno); + errno = 0; + + printf("fsync(closed): %d\n", fsync(f)); + printf("errno: %d\n", errno); + errno = 0; + + printf("close(closed): %d\n", close(f)); + printf("errno: %d\n", errno); + errno = 0; + + return 0; +} diff --git a/tests/unistd/close.out b/tests/unistd/close.out new file mode 100644 index 00000000..66e47927 --- /dev/null +++ b/tests/unistd/close.out @@ -0,0 +1,8 @@ +fsync(opened): 0 +errno: 0 +close(opened): 0 +errno: 0 +fsync(closed): -1 +errno: 9 +close(closed): -1 +errno: 9 diff --git a/tests/unistd/confstr.c b/tests/unistd/confstr.c new file mode 100644 index 00000000..5c96c89c --- /dev/null +++ b/tests/unistd/confstr.c @@ -0,0 +1,55 @@ +#include <stdio.h> +#include <errno.h> +#include <unistd.h> + +int main() { + int vals[] = { + _CS_PATH, + _CS_POSIX_V6_WIDTH_RESTRICTED_ENVS, + _CS_GNU_LIBC_VERSION, + _CS_GNU_LIBPTHREAD_VERSION, + _CS_POSIX_V6_ILP32_OFF32_LIBS, + _CS_POSIX_V6_ILP32_OFFBIG_LIBS, + _CS_POSIX_V6_LP64_OFF64_CFLAGS, + _CS_POSIX_V6_LP64_OFF64_LDFLAGS, + _CS_POSIX_V6_LP64_OFF64_LIBS, + _CS_POSIX_V6_LPBIG_OFFBIG_CFLAGS, + _CS_POSIX_V6_LPBIG_OFFBIG_LDFLAGS, + _CS_POSIX_V6_LPBIG_OFFBIG_LIBS, + _CS_POSIX_V6_ILP32_OFF32_CFLAGS, + _CS_POSIX_V6_ILP32_OFF32_LDFLAGS, + _CS_POSIX_V6_ILP32_OFFBIG_LDFLAGS, + _CS_POSIX_V6_ILP32_OFFBIG_CFLAGS + }; + char* names[] = { + "_CS_PATH", + "_CS_POSIX_V6_WIDTH_RESTRICTED_ENVS", + "_CS_GNU_LIBC_VERSION", + "_CS_GNU_LIBPTHREAD_VERSION", + "_CS_POSIX_V6_ILP32_OFF32_LIBS", + "_CS_POSIX_V6_ILP32_OFFBIG_LIBS", + "_CS_POSIX_V6_LP64_OFF64_CFLAGS", + "_CS_POSIX_V6_LP64_OFF64_LDFLAGS", + "_CS_POSIX_V6_LP64_OFF64_LIBS", + "_CS_POSIX_V6_LPBIG_OFFBIG_CFLAGS", + "_CS_POSIX_V6_LPBIG_OFFBIG_LDFLAGS", + "_CS_POSIX_V6_LPBIG_OFFBIG_LIBS", + "_CS_POSIX_V6_ILP32_OFF32_CFLAGS", + "_CS_POSIX_V6_ILP32_OFF32_LDFLAGS", + "_CS_POSIX_V6_ILP32_OFFBIG_LDFLAGS", + "_CS_POSIX_V6_ILP32_OFFBIG_CFLAGS" + }; + char buffer[256]; + + for (int i = 0; i < sizeof vals / sizeof vals[0]; i++) { + printf("ret: %d\n", confstr(vals[i], buffer, 256)); + printf("%s: %s\n", names[i], buffer); + printf("errno: %d\n\n", errno); + errno = 0; + } + + printf("(invalid) ret: %d\n", confstr(-123, buffer, 256)); + printf("errno: %d\n", errno); + + return 0; +} diff --git a/tests/unistd/confstr.out b/tests/unistd/confstr.out new file mode 100644 index 00000000..c6019319 --- /dev/null +++ b/tests/unistd/confstr.out @@ -0,0 +1,67 @@ +ret: 2 +_CS_PATH: / +errno: 0 + +ret: 43 +_CS_POSIX_V6_WIDTH_RESTRICTED_ENVS: POSIX_V6_ILP32_OFF32 +POSIX_V6_ILP32_OFFBIG +errno: 0 + +ret: 11 +_CS_GNU_LIBC_VERSION: glibc 2.14 +errno: 0 + +ret: 1 +_CS_GNU_LIBPTHREAD_VERSION: +errno: 0 + +ret: 1 +_CS_POSIX_V6_ILP32_OFF32_LIBS: +errno: 0 + +ret: 1 +_CS_POSIX_V6_ILP32_OFFBIG_LIBS: +errno: 0 + +ret: 1 +_CS_POSIX_V6_LP64_OFF64_CFLAGS: +errno: 0 + +ret: 1 +_CS_POSIX_V6_LP64_OFF64_LDFLAGS: +errno: 0 + +ret: 1 +_CS_POSIX_V6_LP64_OFF64_LIBS: +errno: 0 + +ret: 1 +_CS_POSIX_V6_LPBIG_OFFBIG_CFLAGS: +errno: 0 + +ret: 1 +_CS_POSIX_V6_LPBIG_OFFBIG_LDFLAGS: +errno: 0 + +ret: 1 +_CS_POSIX_V6_LPBIG_OFFBIG_LIBS: +errno: 0 + +ret: 5 +_CS_POSIX_V6_ILP32_OFF32_CFLAGS: -m32 +errno: 0 + +ret: 5 +_CS_POSIX_V6_ILP32_OFF32_LDFLAGS: -m32 +errno: 0 + +ret: 5 +_CS_POSIX_V6_ILP32_OFFBIG_LDFLAGS: -m32 +errno: 0 + +ret: 48 +_CS_POSIX_V6_ILP32_OFFBIG_CFLAGS: -m32 -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 +errno: 0 + +(invalid) ret: 0 +errno: 22 diff --git a/tests/unistd/curdir.c b/tests/unistd/curdir.c new file mode 100644 index 00000000..63b9c7fe --- /dev/null +++ b/tests/unistd/curdir.c @@ -0,0 +1,95 @@ +#include <stdio.h> +#include <errno.h> +#include <unistd.h> +#include <fcntl.h> + +int main() { + char buffer[256]; + printf("getwd: %s\n", getwd(buffer)); + printf("errno: %d\n", errno); + errno = 0; + printf("getcwd: %s\n", getcwd(buffer, 256)); + printf("errno: %d\n", errno); + errno = 0; + printf("\n"); + + printf("chdir(file): %d\n", chdir("/file")); + printf("errno: %d\n", errno); + if (!errno) { + errno = 0; + printf("getwd: %s\n", getwd(buffer)); + printf("errno: %d\n", errno); + errno = 0; + printf("getcwd: %s\n", getcwd(buffer, 256)); + printf("errno: %d\n", errno); + } + errno = 0; + printf("\n"); + + printf("chdir(device): %d\n", chdir("/device")); + printf("errno: %d\n", errno); + if (!errno) { + errno = 0; + printf("getwd: %s\n", getwd(buffer)); + printf("errno: %d\n", errno); + errno = 0; + printf("getcwd: %s\n", getcwd(buffer, 256)); + printf("errno: %d\n", errno); + } + errno = 0; + printf("\n"); + + printf("chdir(folder): %d\n", chdir("/folder")); + printf("errno: %d\n", errno); + if (!errno) { + errno = 0; + printf("getwd: %s\n", getwd(buffer)); + printf("errno: %d\n", errno); + errno = 0; + printf("getcwd: %s\n", getcwd(buffer, 256)); + printf("errno: %d\n", errno); + } + errno = 0; + printf("\n"); + + printf("chdir(nonexistent): %d\n", chdir("/nonexistent")); + printf("errno: %d\n", errno); + if (!errno) { + errno = 0; + printf("getwd: %s\n", getwd(buffer)); + printf("errno: %d\n", errno); + errno = 0; + printf("getcwd: %s\n", getcwd(buffer, 256)); + printf("errno: %d\n", errno); + } + errno = 0; + printf("\n"); + + printf("chdir(link): %d\n", chdir("/link")); + printf("errno: %d\n", errno); + if (!errno) { + errno = 0; + printf("getwd: %s\n", getwd(buffer)); + printf("errno: %d\n", errno); + errno = 0; + printf("getcwd: %s\n", getcwd(buffer, 256)); + printf("errno: %d\n", errno); + } + errno = 0; + printf("\n"); + + errno = 0; + printf("fchdir(/): %d\n", fchdir(open("/", O_RDONLY, 0777))); + printf("errno: %d\n", errno); + if (!errno) { + errno = 0; + printf("getwd: %s\n", getwd(buffer)); + printf("errno: %d\n", errno); + errno = 0; + printf("getcwd: %s\n", getcwd(buffer, 256)); + printf("errno: %d\n", errno); + errno = 0; + } + + return 0; +} diff --git a/tests/unistd/curdir.js b/tests/unistd/curdir.js new file mode 100644 index 00000000..e271b9da --- /dev/null +++ b/tests/unistd/curdir.js @@ -0,0 +1,4 @@ +FS.createDataFile('/', 'file', '', true, true); +FS.createFolder('/', 'folder', true, true); +FS.createDevice('/', 'device', function() {}, function() {}); +FS.createLink('/', 'link', 'folder', true, true); diff --git a/tests/unistd/curdir.out b/tests/unistd/curdir.out new file mode 100644 index 00000000..e353f1c4 --- /dev/null +++ b/tests/unistd/curdir.out @@ -0,0 +1,34 @@ +getwd: / +errno: 0 +getcwd: / +errno: 0 + +chdir(file): -1 +errno: 20 + +chdir(device): -1 +errno: 20 + +chdir(folder): 0 +errno: 0 +getwd: /folder +errno: 0 +getcwd: /folder +errno: 0 + +chdir(nonexistent): -1 +errno: 2 + +chdir(link): 0 +errno: 0 +getwd: /folder +errno: 0 +getcwd: /folder +errno: 0 + +fchdir(/): 0 +errno: 0 +getwd: / +errno: 0 +getcwd: / +errno: 0 diff --git a/tests/unistd/dup.c b/tests/unistd/dup.c new file mode 100644 index 00000000..8b4dca34 --- /dev/null +++ b/tests/unistd/dup.c @@ -0,0 +1,38 @@ +#include <stdio.h> +#include <errno.h> +#include <unistd.h> +#include <fcntl.h> + +int main() { + int f, f2, f3; + + printf("DUP\n"); + f = open("/", O_RDONLY); + f2 = open("/", O_RDONLY); + f3 = dup(f); + printf("errno: %d\n", errno); + printf("f: %d\n", f); + printf("f2: %d\n", f2); + printf("f3: %d\n", f3); + printf("close(f1): %d\n", close(f)); + printf("close(f2): %d\n", close(f2)); + printf("close(f3): %d\n", close(f3)); + printf("\n"); + errno = 0; + + printf("DUP2\n"); + f = open("/", O_RDONLY); + f2 = open("/", O_RDONLY); + f3 = dup2(f, f2); + printf("errno: %d\n", errno); + printf("f: %d\n", f); + printf("f2: %d\n", f2); + printf("f3: %d\n", f3); + printf("close(f1): %d\n", close(f)); + printf("close(f2): %d\n", close(f2)); + printf("close(f3): %d\n", close(f3)); + printf("\n"); + errno = 0; + + return 0; +} diff --git a/tests/unistd/dup.out b/tests/unistd/dup.out new file mode 100644 index 00000000..439c6fd5 --- /dev/null +++ b/tests/unistd/dup.out @@ -0,0 +1,17 @@ +DUP +errno: 0 +f: 1 +f2: 2 +f3: 3 +close(f1): 0 +close(f2): 0 +close(f3): 0 + +DUP2 +errno: 0 +f: 4 +f2: 5 +f3: 5 +close(f1): 0 +close(f2): 0 +close(f3): -1 diff --git a/tests/unistd/io.c b/tests/unistd/io.c new file mode 100644 index 00000000..eeb80373 --- /dev/null +++ b/tests/unistd/io.c @@ -0,0 +1,101 @@ +#include <stdio.h> +#include <errno.h> +#include <unistd.h> +#include <fcntl.h> +#include <string.h> + +int main() { + char readBuffer[256] = {0}; + char writeBuffer[] = "writeme"; + + int fl = open("/folder", O_RDWR); + printf("read from folder: %d\n", read(fl, readBuffer, sizeof readBuffer)); + printf("errno: %d\n", errno); + errno = 0; + printf("write to folder: %d\n", write(fl, writeBuffer, sizeof writeBuffer)); + printf("errno: %d\n\n", errno); + errno = 0; + + int bd = open("/broken-device", O_RDWR); + printf("read from broken device: %d\n", read(bd, readBuffer, sizeof readBuffer)); + printf("errno: %d\n", errno); + errno = 0; + printf("write to broken device: %d\n", write(bd, writeBuffer, sizeof writeBuffer)); + printf("errno: %d\n\n", errno); + errno = 0; + + int d = open("/device", O_RDWR); + printf("read from device: %d\n", read(d, readBuffer, sizeof readBuffer)); + printf("data: %s\n", readBuffer); + memset(readBuffer, 0, sizeof readBuffer); + printf("errno: %d\n", errno); + errno = 0; + printf("write to device: %d\n", write(d, writeBuffer, sizeof writeBuffer)); + printf("errno: %d\n\n", errno); + errno = 0; + + int f = open("/file", O_RDWR); + printf("read from file: %d\n", read(f, readBuffer, sizeof readBuffer)); + printf("data: %s\n", readBuffer); + memset(readBuffer, 0, sizeof readBuffer); + printf("errno: %d\n\n", errno); + errno = 0; + + printf("seek: %d\n", lseek(f, 3, SEEK_SET)); + printf("errno: %d\n\n", errno); + printf("partial read from file: %d\n", read(f, readBuffer, 3)); + printf("data: %s\n", readBuffer); + memset(readBuffer, 0, sizeof readBuffer); + printf("errno: %d\n\n", errno); + errno = 0; + + printf("seek: %d\n", lseek(f, -2, SEEK_END)); + printf("errno: %d\n", errno); + errno = 0; + printf("partial read from end of file: %d\n", read(f, readBuffer, 3)); + printf("data: %s\n", readBuffer); + memset(readBuffer, 0, sizeof readBuffer); + printf("errno: %d\n\n", errno); + errno = 0; + + printf("seek: %d\n", lseek(f, -15, SEEK_CUR)); + printf("errno: %d\n", errno); + errno = 0; + printf("partial read from before start of file: %d\n", read(f, readBuffer, 3)); + printf("data: %s\n", readBuffer); + memset(readBuffer, 0, sizeof readBuffer); + printf("errno: %d\n\n", errno); + errno = 0; + + printf("seek: %d\n", lseek(f, 0, SEEK_SET)); + printf("write to start of file: %d\n", write(f, writeBuffer, 3)); + printf("errno: %d\n\n", errno); + errno = 0; + + printf("seek: %d\n", lseek(f, 0, SEEK_END)); + printf("write to end of file: %d\n", write(f, writeBuffer, 3)); + printf("errno: %d\n\n", errno); + errno = 0; + + printf("seek: %d\n", lseek(f, 10, SEEK_END)); + printf("write after end of file: %d\n", write(f, writeBuffer, sizeof writeBuffer)); + printf("errno: %d\n\n", errno); + errno = 0; + + int bytesRead; + printf("seek: %d\n", lseek(f, 0, SEEK_SET)); + printf("read after write: %d\n", bytesRead = read(f, readBuffer, sizeof readBuffer)); + printf("errno: %d\n", errno); + errno = 0; + printf("final: "); + for (int i = 0; i < bytesRead; i++) { + if (readBuffer[i] == 0) { + printf("\\0"); + } else { + printf("%c", readBuffer[i]); + } + } + printf("\n"); + + return 0; +} diff --git a/tests/unistd/io.js b/tests/unistd/io.js new file mode 100644 index 00000000..b129af44 --- /dev/null +++ b/tests/unistd/io.js @@ -0,0 +1,19 @@ +(function() { + var devicePayload = [65, 66, 67, 68]; + FS.createDevice('/', 'device', function() { + if (devicePayload.length) { + return devicePayload.shift(); + } else { + return null; + } + }, function(arg) { + print("TO DEVICE: " + arg); + }); + FS.createDevice('/', 'broken-device', function() { + throw new Error('Broken device input.'); + }, function(arg) { + throw new Error('Broken device output.'); + }); + FS.createDataFile('/', 'file', '1234567890', true, true); + FS.createFolder('/', 'folder', true, true); +})(); diff --git a/tests/unistd/io.out b/tests/unistd/io.out new file mode 100644 index 00000000..3061b94e --- /dev/null +++ b/tests/unistd/io.out @@ -0,0 +1,63 @@ +read from folder: -1 +errno: 9 +write to folder: -1 +errno: 9 + +read from broken device: -1 +errno: 5 +write to broken device: -1 +errno: 5 + +read from device: 4 +data: ABCD +errno: 0 +TO DEVICE: 119 +TO DEVICE: 114 +TO DEVICE: 105 +TO DEVICE: 116 +TO DEVICE: 101 +TO DEVICE: 109 +TO DEVICE: 101 +TO DEVICE: 0 +write to device: 8 +errno: 0 + +read from file: 10 +data: 1234567890 +errno: 0 + +seek: 3 +errno: 0 + +partial read from file: 3 +data: 456 +errno: 0 + +seek: 8 +errno: 0 +partial read from end of file: 2 +data: 90 +errno: 0 + +seek: -1 +errno: 22 +partial read from bef |