aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorAnthony Pesch <inolen@gmail.com>2013-07-15 23:44:38 -0700
committerAnthony Pesch <inolen@gmail.com>2013-08-05 12:36:46 -0700
commit0ddff9d01bb77f7cbcb054b23cb86fc0c2bd18d5 (patch)
treeb957d24e4de9e59c3e42925aeaabc603dad0899c /tests
parent4b4ca9844a7ee8e40e3effd9ca5b3d7e279a12d7 (diff)
Updated rename tests
Diffstat (limited to 'tests')
-rwxr-xr-xtests/runner.py30
-rw-r--r--tests/stdio/test_rename.c111
2 files changed, 114 insertions, 27 deletions
diff --git a/tests/runner.py b/tests/runner.py
index 84e8b69e..b57b209a 100755
--- a/tests/runner.py
+++ b/tests/runner.py
@@ -3591,33 +3591,9 @@ Exiting setjmp function, level: 0, prev_jmp: -1
self.do_run(src, 'z:1*', force_c=True)
def test_rename(self):
- src = '''
- #include <stdio.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <assert.h>
-
- int main() {
- int err;
- FILE* fid;
-
- err = mkdir("/foo", 0777);
- err = mkdir("/bar", 0777);
- fid = fopen("/foo/bar", "w+");
- fclose(fid);
-
- err = rename("/foo/bar", "/foo/bar2");
- printf("%d\\n", err);
-
- err = rename("/foo", "/foo/foo");
- printf("%d\\n", err);
-
- err = rename("/foo", "/bar/foo");
- printf("%d\\n", err);
- return 0;
- }
- '''
- self.do_run(src, '0\n-1\n0\n', force_c=True)
+ Building.COMPILER_TEST_OPTS += ['-DUSE_OLD_FS='+str(Settings.USE_OLD_FS)]
+ src = open(path_from_root('tests', 'stdio', 'test_rename.c'), 'r').read()
+ self.do_run(src, 'success', force_c=True)
def test_alloca_stack(self):
if self.emcc_args is None: return # too slow in other modes
diff --git a/tests/stdio/test_rename.c b/tests/stdio/test_rename.c
new file mode 100644
index 00000000..ddbcd339
--- /dev/null
+++ b/tests/stdio/test_rename.c
@@ -0,0 +1,111 @@
+#include <assert.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/stat.h>
+
+void create_file(const char *path, const char *buffer, int mode) {
+ int fd = open(path, O_WRONLY | O_CREAT | O_EXCL, mode);
+ assert(fd >= 0);
+
+ int err = write(fd, buffer, sizeof(char) * strlen(buffer));
+ assert(err == (sizeof(char) * strlen(buffer)));
+
+ close(fd);
+}
+
+void setup() {
+ create_file("file", "abcdef", 0777);
+ mkdir("dir", 0777);
+ create_file("dir/file", "abcdef", 0777);
+ mkdir("dir/subdir", 0777);
+ mkdir("dir-readonly", 0555);
+ mkdir("dir-nonempty", 0777);
+ create_file("dir-nonempty/file", "abcdef", 0777);
+}
+
+void cleanup() {
+ // we're hulk-smashing and removing original + renamed files to
+ // make sure we get it all regardless of anything failing
+ unlink("file");
+ unlink("dir/file");
+ unlink("dir/file1");
+ unlink("dir/file2");
+ rmdir("dir/subdir");
+ rmdir("dir/subdir1");
+ rmdir("dir/subdir2");
+ rmdir("dir");
+ rmdir("dir-readonly");
+ unlink("dir-nonempty/file");
+ rmdir("dir-nonempty");
+}
+
+void test() {
+ int err;
+
+ // can't rename something that doesn't exist
+ err = rename("noexist", "dir");
+ assert(err == -1);
+ assert(errno == ENOENT);
+
+ // can't overwrite a folder with a file
+ err = rename("file", "dir");
+ assert(err == -1);
+ assert(errno == EISDIR);
+
+#if !USE_OLD_FS
+ // can't overwrite a file with a folder
+ err = rename("dir", "file");
+ assert(err == -1);
+ assert(errno == ENOTDIR);
+
+ // can't overwrite a non-empty folder
+ err = rename("dir", "dir-nonempty");
+ assert(err == -1);
+ assert(errno == ENOTEMPTY);
+
+ // can't create anything in a read-only directory
+ err = rename("dir", "dir-readonly/dir");
+ assert(err == -1);
+ assert(errno == EACCES);
+#endif
+
+ // source should not be ancestor of target
+ err = rename("dir", "dir/somename");
+ assert(err == -1);
+ assert(errno == EINVAL);
+
+#if !USE_OLD_FS
+ // target should not be an ancestor of source
+ err = rename("dir/subdir", "dir");
+ assert(err == -1);
+ assert(errno == ENOTEMPTY);
+#endif
+
+ // do some valid renaming
+ err = rename("dir/file", "dir/file1");
+ assert(!err);
+ err = rename("dir/file1", "dir/file2");
+ assert(!err);
+ err = access("dir/file2", F_OK);
+ assert(!err);
+ err = rename("dir/subdir", "dir/subdir1");
+ assert(!err);
+ err = rename("dir/subdir1", "dir/subdir2");
+ assert(!err);
+ err = access("dir/subdir2", F_OK);
+ assert(!err);
+
+ puts("success");
+}
+
+int main() {
+ atexit(cleanup);
+ signal(SIGABRT, cleanup);
+ setup();
+ test();
+ return EXIT_SUCCESS;
+} \ No newline at end of file