aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/runner.py194
-rw-r--r--tests/stat/output.txt174
-rw-r--r--tests/stat/src.c205
3 files changed, 573 insertions, 0 deletions
diff --git a/tests/runner.py b/tests/runner.py
index 25995c5c..4a738c66 100644
--- a/tests/runner.py
+++ b/tests/runner.py
@@ -2078,6 +2078,200 @@ if 'benchmark' not in sys.argv:
src = open(path_from_root('tests', 'files.cpp'), 'r').read()
self.do_test(src, 'size: 7\ndata: 100,-56,50,25,10,77,123\ninput:hi there!\ntexto\ntexte\n5 : 10,30,20,11,88\nother=some data.\nseeked=me da.\nseeked=ata.\nseeked=ta.\nfscanfed: 10 - hello\n', post_build=post)
+ def test_folders(self):
+ def addPreRun(filename):
+ src = open(filename, 'r').read().replace(
+ '// {{PRE_RUN_ADDITIONS}}',
+ '''
+ FS.createFolder('/', 'test', true, false);
+ FS.createPath('/', 'test/hello/world/', true, false);
+ FS.createPath('/test', 'goodbye/world/', true, false);
+ FS.createPath('/test/goodbye', 'noentry', false, false);
+ FS.createDataFile('/test', 'freeforall.ext', 'abc', true, true);
+ FS.createDataFile('/test', 'restricted.ext', 'def', false, false);
+ '''
+ )
+ open(filename, 'w').write(src)
+
+ src = r'''
+ #include <stdio.h>
+ #include <dirent.h>
+ #include <errno.h>
+
+ int main() {
+ struct dirent *e;
+
+ // Basic correct behaviour.
+ DIR* d = opendir("/test");
+ printf("--E: %d\n", errno);
+ while ((e = readdir(d))) puts(e->d_name);
+ printf("--E: %d\n", errno);
+
+ // Empty folder; tell/seek.
+ puts("****");
+ d = opendir("/test/hello/world/");
+ e = readdir(d);
+ puts(e->d_name);
+ int pos = telldir(d);
+ e = readdir(d);
+ puts(e->d_name);
+ seekdir(d, pos);
+ e = readdir(d);
+ puts(e->d_name);
+
+ // Errors.
+ puts("****");
+ printf("--E: %d\n", errno);
+ d = opendir("/test/goodbye/noentry");
+ printf("--E: %d, D: %d\n", errno, d);
+ d = opendir("/i/dont/exist");
+ printf("--E: %d, D: %d\n", errno, d);
+ d = opendir("/test/freeforall.ext");
+ printf("--E: %d, D: %d\n", errno, d);
+ while ((e = readdir(d))) puts(e->d_name);
+ printf("--E: %d\n", errno);
+
+ return 0;
+ }
+ '''
+ expected = '''
+ --E: 0
+ .
+ ..
+ hello
+ goodbye
+ freeforall.ext
+ restricted.ext
+ --E: 0
+ ****
+ .
+ ..
+ ..
+ ****
+ --E: 0
+ --E: 13, D: 0
+ --E: 2, D: 0
+ --E: 20, D: 0
+ --E: 9
+ '''
+ self.do_test(src, re.sub('(^|\n)\s+', '\\1', expected), post_build=addPreRun)
+
+ def test_stat(self):
+ def addPreRun(filename):
+ src = open(filename, 'r').read().replace(
+ '// {{PRE_RUN_ADDITIONS}}',
+ '''
+ var f1 = FS.createFolder('/', 'test', true, true);
+ var f2 = FS.createDataFile(f1, 'file', 'abcdef', true, true);
+ var f3 = FS.createLink(f1, 'link', 'file', true, true);
+ var f4 = FS.createDevice(f1, 'device', function(){}, function(){});
+ f1.timestamp = f2.timestamp = f3.timestamp = f4.timestamp = new Date(1200000000000);
+ '''
+ )
+ open(filename, 'w').write(src)
+ src = open(path_from_root('tests', 'stat', 'src.c'), 'r').read()
+ expected = open(path_from_root('tests', 'stat', 'output.txt'), 'r').read()
+ self.do_test(src, expected, post_build=addPreRun)
+
+ def test_libgen(self):
+ src = r'''
+ #include <stdio.h>
+ #include <libgen.h>
+
+ int main() {
+ char p1[16] = "/usr/lib", p1x[16] = "/usr/lib";
+ printf("%s -> ", p1);
+ printf("%s : %s\n", dirname(p1x), basename(p1));
+
+ char p2[16] = "/usr", p2x[16] = "/usr";
+ printf("%s -> ", p2);
+ printf("%s : %s\n", dirname(p2x), basename(p2));
+
+ char p3[16] = "/usr/", p3x[16] = "/usr/";
+ printf("%s -> ", p3);
+ printf("%s : %s\n", dirname(p3x), basename(p3));
+
+ char p4[16] = "/usr/lib///", p4x[16] = "/usr/lib///";
+ printf("%s -> ", p4);
+ printf("%s : %s\n", dirname(p4x), basename(p4));
+
+ char p5[16] = "/", p5x[16] = "/";
+ printf("%s -> ", p5);
+ printf("%s : %s\n", dirname(p5x), basename(p5));
+
+ char p6[16] = "///", p6x[16] = "///";
+ printf("%s -> ", p6);
+ printf("%s : %s\n", dirname(p6x), basename(p6));
+
+ char p7[16] = "/usr/../lib/..", p7x[16] = "/usr/../lib/..";
+ printf("%s -> ", p7);
+ printf("%s : %s\n", dirname(p7x), basename(p7));
+
+ char p8[16] = "", p8x[16] = "";
+ printf("(empty) -> %s : %s\n", dirname(p8x), basename(p8));
+
+ printf("(null) -> %s : %s\n", dirname(0), basename(0));
+
+ return 0;
+ }
+ '''
+ expected = '''
+ /usr/lib -> /usr : lib
+ /usr -> / : usr
+ /usr/ -> / : usr
+ /usr/lib/// -> /usr : lib
+ / -> / : /
+ /// -> / : /
+ /usr/../lib/.. -> /usr/../lib : ..
+ (empty) -> . : .
+ (null) -> . : .
+ '''
+ self.do_test(src, re.sub('(^|\n)\s+', '\\1', expected))
+
+ def test_utime(self):
+ def addPreRunAndChecks(filename):
+ src = open(filename, 'r').read().replace(
+ '// {{PRE_RUN_ADDITIONS}}',
+ '''
+ var TEST_F1 = FS.createFolder('/', 'writeable', true, true);
+ var TEST_F2 = FS.createFolder('/', 'unwriteable', true, false);
+ '''
+ ).replace(
+ '// {{POST_RUN_ADDITIONS}}',
+ '''
+ print('first changed: ' + (TEST_F1.timestamp.getTime() == 1200000000000));
+ print('second changed: ' + (TEST_F2.timestamp.getTime() == 1200000000000));
+ '''
+ )
+ open(filename, 'w').write(src)
+
+ src = r'''
+ #include <stdio.h>
+ #include <errno.h>
+ #include <utime.h>
+
+ int main() {
+ struct utimbuf t = {1000000000, 1200000000};
+ char* writeable = "/writeable";
+ char* unwriteable = "/unwriteable";
+
+ utime(writeable, &t);
+ printf("writeable errno: %d\n", errno);
+
+ utime(unwriteable, &t);
+ printf("unwriteable errno: %d\n", errno);
+
+ return 0;
+ }
+ '''
+ expected = '''
+ writeable errno: 0
+ unwriteable errno: 1
+ first changed: true
+ second changed: false
+ '''
+ self.do_test(src, re.sub('(^|\n)\s+', '\\1', expected), post_build=addPreRunAndChecks)
+
### 'Big' tests
def test_fannkuch(self):
diff --git a/tests/stat/output.txt b/tests/stat/output.txt
new file mode 100644
index 00000000..5253a834
--- /dev/null
+++ b/tests/stat/output.txt
@@ -0,0 +1,174 @@
+--stat FOLDER--
+ret: 0
+errno: 0
+st_dev: 1
+st_ino: 2
+st_mode: 040777
+st_nlink: 1
+st_rdev: 0
+st_size: 4096
+st_atime: 1200000000
+st_mtime: 1200000000
+st_ctime: 1200000000
+st_blksize: 4096
+st_blocks: 1
+S_ISBLK: 0
+S_ISCHR: 0
+S_ISDIR: 1
+S_ISFIFO: 0
+S_ISREG: 0
+S_ISLNK: 0
+S_ISSOCK: 0
+
+--stat FILE--
+ret: 0
+errno: 0
+st_dev: 1
+st_ino: 3
+st_mode: 0100777
+st_nlink: 1
+st_rdev: 0
+st_size: 6
+st_atime: 1200000000
+st_mtime: 1200000000
+st_ctime: 1200000000
+st_blksize: 4096
+st_blocks: 1
+S_ISBLK: 0
+S_ISCHR: 0
+S_ISDIR: 0
+S_ISFIFO: 0
+S_ISREG: 1
+S_ISLNK: 0
+S_ISSOCK: 0
+
+--stat DEVICE--
+ret: 0
+errno: 0
+st_dev: 5
+st_ino: 5
+st_mode: 020777
+st_nlink: 1
+st_rdev: 5
+st_size: 0
+st_atime: 1200000000
+st_mtime: 1200000000
+st_ctime: 1200000000
+st_blksize: 4096
+st_blocks: 0
+S_ISBLK: 0
+S_ISCHR: 1
+S_ISDIR: 0
+S_ISFIFO: 0
+S_ISREG: 0
+S_ISLNK: 0
+S_ISSOCK: 0
+
+--stat LINK--
+ret: 0
+errno: 0
+st_dev: 1
+st_ino: 3
+st_mode: 0100777
+st_nlink: 1
+st_rdev: 0
+st_size: 6
+st_atime: 1200000000
+st_mtime: 1200000000
+st_ctime: 1200000000
+st_blksize: 4096
+st_blocks: 1
+S_ISBLK: 0
+S_ISCHR: 0
+S_ISDIR: 0
+S_ISFIFO: 0
+S_ISREG: 1
+S_ISLNK: 0
+S_ISSOCK: 0
+
+--lstat LINK--
+ret: 0
+errno: 0
+st_dev: 1
+st_ino: 4
+st_mode: 0120777
+st_nlink: 1
+st_rdev: 0
+st_size: 4
+st_atime: 1200000000
+st_mtime: 1200000000
+st_ctime: 1200000000
+st_blksize: 4096
+st_blocks: 1
+S_ISBLK: 0
+S_ISCHR: 0
+S_ISDIR: 0
+S_ISFIFO: 0
+S_ISREG: 0
+S_ISLNK: 1
+S_ISSOCK: 0
+
+--chmod FILE--
+ret: 0
+errno: 0
+st_mode: 0100222
+st_mtime changed: 1
+
+--chmod FOLDER--
+ret: 0
+errno: 0
+st_mode: 040555
+st_mtime changed: 1
+
+--chmod LINK--
+ret: 0
+errno: 0
+st_mode: 0100000
+
+--mkdir--
+ret: 0
+errno: 0
+st_mode: 040777
+S_ISBLK: 0
+S_ISCHR: 0
+S_ISDIR: 1
+S_ISFIFO: 0
+S_ISREG: 0
+S_ISLNK: 0
+S_ISSOCK: 0
+
+--mknod FILE--
+ret: 0
+errno: 0
+st_mode: 0100777
+S_ISBLK: 0
+S_ISCHR: 0
+S_ISDIR: 0
+S_ISFIFO: 0
+S_ISREG: 1
+S_ISLNK: 0
+S_ISSOCK: 0
+
+--mknod FOLDER--
+ret: 0
+errno: 0
+st_mode: 040777
+S_ISBLK: 0
+S_ISCHR: 0
+S_ISDIR: 1
+S_ISFIFO: 0
+S_ISREG: 0
+S_ISLNK: 0
+S_ISSOCK: 0
+
+--mknod FIFO--
+ret: -1
+errno: 22
+
+--mknod DEVICE--
+ret: -1
+errno: 22
+
+--mkfifo--
+ret: -1
+errno: 30
diff --git a/tests/stat/src.c b/tests/stat/src.c
new file mode 100644
index 00000000..4dc78805
--- /dev/null
+++ b/tests/stat/src.c
@@ -0,0 +1,205 @@
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+#include <sys/stat.h>
+
+int main() {
+ struct stat s;
+
+ printf("--stat FOLDER--\n");
+ printf("ret: %d\n", stat("/test", &s));
+ printf("errno: %d\n", errno);
+ printf("st_dev: %llu\n", s.st_dev);
+ printf("st_ino: %lu\n", s.st_ino);
+ printf("st_mode: 0%o\n", s.st_mode);
+ printf("st_nlink: %d\n", s.st_nlink);
+ printf("st_rdev: %llu\n", s.st_rdev);
+ printf("st_size: %ld\n", s.st_size);
+ printf("st_atime: %ld\n", s.st_atime);
+ printf("st_mtime: %ld\n", s.st_mtime);
+ printf("st_ctime: %ld\n", s.st_ctime);
+ printf("st_blksize: %ld\n", s.st_blksize);
+ printf("st_blocks: %ld\n", s.st_blocks);
+ printf("S_ISBLK: %d\n", S_ISBLK(s.st_mode));
+ printf("S_ISCHR: %d\n", S_ISCHR(s.st_mode));
+ printf("S_ISDIR: %d\n", S_ISDIR(s.st_mode));
+ printf("S_ISFIFO: %d\n", S_ISFIFO(s.st_mode));
+ printf("S_ISREG: %d\n", S_ISREG(s.st_mode));
+ printf("S_ISLNK: %d\n", S_ISLNK(s.st_mode));
+ printf("S_ISSOCK: %d\n", S_ISSOCK(s.st_mode));
+ memset(&s, 0, sizeof s);
+
+ printf("\n--stat FILE--\n");
+ printf("ret: %d\n", stat("/test/file", &s));
+ printf("errno: %d\n", errno);
+ printf("st_dev: %llu\n", s.st_dev);
+ printf("st_ino: %lu\n", s.st_ino);
+ printf("st_mode: 0%o\n", s.st_mode);
+ printf("st_nlink: %d\n", s.st_nlink);
+ printf("st_rdev: %llu\n", s.st_rdev);
+ printf("st_size: %ld\n", s.st_size);
+ printf("st_atime: %ld\n", s.st_atime);
+ printf("st_mtime: %ld\n", s.st_mtime);
+ printf("st_ctime: %ld\n", s.st_ctime);
+ printf("st_blksize: %ld\n", s.st_blksize);
+ printf("st_blocks: %ld\n", s.st_blocks);
+ printf("S_ISBLK: %d\n", S_ISBLK(s.st_mode));
+ printf("S_ISCHR: %d\n", S_ISCHR(s.st_mode));
+ printf("S_ISDIR: %d\n", S_ISDIR(s.st_mode));
+ printf("S_ISFIFO: %d\n", S_ISFIFO(s.st_mode));
+ printf("S_ISREG: %d\n", S_ISREG(s.st_mode));
+ printf("S_ISLNK: %d\n", S_ISLNK(s.st_mode));
+ printf("S_ISSOCK: %d\n", S_ISSOCK(s.st_mode));
+ memset(&s, 0, sizeof s);
+
+ printf("\n--stat DEVICE--\n");
+ printf("ret: %d\n", stat("/test/device", &s));
+ printf("errno: %d\n", errno);
+ printf("st_dev: %llu\n", s.st_dev);
+ printf("st_ino: %lu\n", s.st_ino);
+ printf("st_mode: 0%o\n", s.st_mode);
+ printf("st_nlink: %d\n", s.st_nlink);
+ printf("st_rdev: %llu\n", s.st_rdev);
+ printf("st_size: %ld\n", s.st_size);
+ printf("st_atime: %ld\n", s.st_atime);
+ printf("st_mtime: %ld\n", s.st_mtime);
+ printf("st_ctime: %ld\n", s.st_ctime);
+ printf("st_blksize: %ld\n", s.st_blksize);
+ printf("st_blocks: %ld\n", s.st_blocks);
+ printf("S_ISBLK: %d\n", S_ISBLK(s.st_mode));
+ printf("S_ISCHR: %d\n", S_ISCHR(s.st_mode));
+ printf("S_ISDIR: %d\n", S_ISDIR(s.st_mode));
+ printf("S_ISFIFO: %d\n", S_ISFIFO(s.st_mode));
+ printf("S_ISREG: %d\n", S_ISREG(s.st_mode));
+ printf("S_ISLNK: %d\n", S_ISLNK(s.st_mode));
+ printf("S_ISSOCK: %d\n", S_ISSOCK(s.st_mode));
+ memset(&s, 0, sizeof s);
+
+ printf("\n--stat LINK--\n");
+ printf("ret: %d\n", stat("/test/link", &s));
+ printf("errno: %d\n", errno);
+ printf("st_dev: %llu\n", s.st_dev);
+ printf("st_ino: %lu\n", s.st_ino);
+ printf("st_mode: 0%o\n", s.st_mode);
+ printf("st_nlink: %d\n", s.st_nlink);
+ printf("st_rdev: %llu\n", s.st_rdev);
+ printf("st_size: %ld\n", s.st_size);
+ printf("st_atime: %ld\n", s.st_atime);
+ printf("st_mtime: %ld\n", s.st_mtime);
+ printf("st_ctime: %ld\n", s.st_ctime);
+ printf("st_blksize: %ld\n", s.st_blksize);
+ printf("st_blocks: %ld\n", s.st_blocks);
+ printf("S_ISBLK: %d\n", S_ISBLK(s.st_mode));
+ printf("S_ISCHR: %d\n", S_ISCHR(s.st_mode));
+ printf("S_ISDIR: %d\n", S_ISDIR(s.st_mode));
+ printf("S_ISFIFO: %d\n", S_ISFIFO(s.st_mode));
+ printf("S_ISREG: %d\n", S_ISREG(s.st_mode));
+ printf("S_ISLNK: %d\n", S_ISLNK(s.st_mode));
+ printf("S_ISSOCK: %d\n", S_ISSOCK(s.st_mode));
+ memset(&s, 0, sizeof s);
+
+ printf("\n--lstat LINK--\n");
+ printf("ret: %d\n", lstat("/test/link", &s));
+ printf("errno: %d\n", errno);
+ printf("st_dev: %llu\n", s.st_dev);
+ printf("st_ino: %lu\n", s.st_ino);
+ printf("st_mode: 0%o\n", s.st_mode);
+ printf("st_nlink: %d\n", s.st_nlink);
+ printf("st_rdev: %llu\n", s.st_rdev);
+ printf("st_size: %ld\n", s.st_size);
+ printf("st_atime: %ld\n", s.st_atime);
+ printf("st_mtime: %ld\n", s.st_mtime);
+ printf("st_ctime: %ld\n", s.st_ctime);
+ printf("st_blksize: %ld\n", s.st_blksize);
+ printf("st_blocks: %ld\n", s.st_blocks);
+ printf("S_ISBLK: %d\n", S_ISBLK(s.st_mode));
+ printf("S_ISCHR: %d\n", S_ISCHR(s.st_mode));
+ printf("S_ISDIR: %d\n", S_ISDIR(s.st_mode));
+ printf("S_ISFIFO: %d\n", S_ISFIFO(s.st_mode));
+ printf("S_ISREG: %d\n", S_ISREG(s.st_mode));
+ printf("S_ISLNK: %d\n", S_ISLNK(s.st_mode));
+ printf("S_ISSOCK: %d\n", S_ISSOCK(s.st_mode));
+ memset(&s, 0, sizeof s);
+
+ printf("\n--chmod FILE--\n");
+ printf("ret: %d\n", chmod("/test/file", 0200));
+ printf("errno: %d\n", errno);
+ stat("/test/file", &s);
+ printf("st_mode: 0%o\n", s.st_mode);
+ printf("st_mtime changed: %d\n", s.st_mtime != 1200000000l);
+ memset(&s, 0, sizeof s);
+
+ printf("\n--chmod FOLDER--\n");
+ printf("ret: %d\n", chmod("/test", 0400));
+ printf("errno: %d\n", errno);
+ stat("/test", &s);
+ printf("st_mode: 0%o\n", s.st_mode);
+ printf("st_mtime changed: %d\n", s.st_mtime != 1200000000l);
+ memset(&s, 0, sizeof s);
+
+ printf("\n--chmod LINK--\n");
+ printf("ret: %d\n", chmod("/test/link", 0000));
+ printf("errno: %d\n", errno);
+ stat("/test/file", &s);
+ printf("st_mode: 0%o\n", s.st_mode);
+ memset(&s, 0, sizeof s);
+
+ // Make sure we can create stuff in the root.
+ chmod("/", 0777);
+
+ printf("\n--mkdir--\n");
+ printf("ret: %d\n", mkdir("/test-mkdir", 0777));
+ printf("errno: %d\n", errno);
+ stat("/test-mkdir", &s);
+ printf("st_mode: 0%o\n", s.st_mode);
+ printf("S_ISBLK: %d\n", S_ISBLK(s.st_mode));
+ printf("S_ISCHR: %d\n", S_ISCHR(s.st_mode));
+ printf("S_ISDIR: %d\n", S_ISDIR(s.st_mode));
+ printf("S_ISFIFO: %d\n", S_ISFIFO(s.st_mode));
+ printf("S_ISREG: %d\n", S_ISREG(s.st_mode));
+ printf("S_ISLNK: %d\n", S_ISLNK(s.st_mode));
+ printf("S_ISSOCK: %d\n", S_ISSOCK(s.st_mode));
+ memset(&s, 0, sizeof s);
+
+ printf("\n--mknod FILE--\n");
+ printf("ret: %d\n", mknod("/test-mknod-file", S_IFREG | 0777, 0));
+ printf("errno: %d\n", errno);
+ stat("/test-mknod-file", &s);
+ printf("st_mode: 0%o\n", s.st_mode);
+ printf("S_ISBLK: %d\n", S_ISBLK(s.st_mode));
+ printf("S_ISCHR: %d\n", S_ISCHR(s.st_mode));
+ printf("S_ISDIR: %d\n", S_ISDIR(s.st_mode));
+ printf("S_ISFIFO: %d\n", S_ISFIFO(s.st_mode));
+ printf("S_ISREG: %d\n", S_ISREG(s.st_mode));
+ printf("S_ISLNK: %d\n", S_ISLNK(s.st_mode));
+ printf("S_ISSOCK: %d\n", S_ISSOCK(s.st_mode));
+ memset(&s, 0, sizeof s);
+
+ printf("\n--mknod FOLDER--\n");
+ printf("ret: %d\n", mknod("/test-mknod-dir", S_IFDIR | 0777, 0));
+ printf("errno: %d\n", errno);
+ stat("/test-mknod-dir", &s);
+ printf("st_mode: 0%o\n", s.st_mode);
+ printf("S_ISBLK: %d\n", S_ISBLK(s.st_mode));
+ printf("S_ISCHR: %d\n", S_ISCHR(s.st_mode));
+ printf("S_ISDIR: %d\n", S_ISDIR(s.st_mode));
+ printf("S_ISFIFO: %d\n", S_ISFIFO(s.st_mode));
+ printf("S_ISREG: %d\n", S_ISREG(s.st_mode));
+ printf("S_ISLNK: %d\n", S_ISLNK(s.st_mode));
+ printf("S_ISSOCK: %d\n", S_ISSOCK(s.st_mode));
+ memset(&s, 0, sizeof s);
+
+ printf("\n--mknod FIFO--\n");
+ printf("ret: %d\n", mknod("/test-mknod-fifo", S_IFIFO | 0777, 0));
+ printf("errno: %d\n", errno);
+
+ printf("\n--mknod DEVICE--\n");
+ printf("ret: %d\n", mknod("/test-mknod-device", S_IFCHR | 0777, 123));
+ printf("errno: %d\n", errno);
+
+ printf("\n--mkfifo--\n");
+ printf("ret: %d\n", mkfifo("/test-mkfifo", 0777));
+ printf("errno: %d\n", errno);
+
+ return 0;
+}