~ubuntu-branches/ubuntu/vivid/emscripten/vivid

« back to all changes in this revision

Viewing changes to tests/stdio/test_rename.c

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2013-09-20 22:44:35 UTC
  • mfrom: (4.1.1 sid)
  • Revision ID: package-import@ubuntu.com-20130920224435-apuwj4fsl3fqv1a6
Tags: 1.5.6~20130920~6010666-1
* New snapshot release
* Update the list of supported architectures to the same as libv8
  (Closes: #723129)
* emlibtool has been removed from upstream.
* Fix warning syntax-error-in-dep5-copyright
* Refresh of the patches

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <assert.h>
 
2
#include <errno.h>
 
3
#include <fcntl.h>
 
4
#include <signal.h>
 
5
#include <stdio.h>
 
6
#include <stdlib.h>
 
7
#include <string.h>
 
8
#include <unistd.h>
 
9
#include <sys/stat.h>
 
10
 
 
11
void create_file(const char *path, const char *buffer, int mode) {
 
12
  int fd = open(path, O_WRONLY | O_CREAT | O_EXCL, mode);
 
13
  assert(fd >= 0);
 
14
 
 
15
  int err = write(fd, buffer, sizeof(char) * strlen(buffer));
 
16
  assert(err ==  (sizeof(char) * strlen(buffer)));
 
17
 
 
18
  close(fd);
 
19
}
 
20
 
 
21
void setup() {
 
22
  create_file("file", "abcdef", 0777);
 
23
  mkdir("dir", 0777);
 
24
  create_file("dir/file", "abcdef", 0777);
 
25
  mkdir("dir/subdir", 0777);
 
26
  mkdir("dir-readonly", 0555);
 
27
  mkdir("dir-nonempty", 0777);
 
28
  create_file("dir-nonempty/file", "abcdef", 0777);
 
29
}
 
30
 
 
31
void cleanup() {
 
32
  // we're hulk-smashing and removing original + renamed files to
 
33
  // make sure we get it all regardless of anything failing
 
34
  unlink("file");
 
35
  unlink("dir/file");
 
36
  unlink("dir/file1");
 
37
  unlink("dir/file2");
 
38
  rmdir("dir/subdir");
 
39
  rmdir("dir/subdir1");
 
40
  rmdir("dir/subdir2");
 
41
  rmdir("dir");
 
42
  rmdir("dir-readonly");
 
43
  unlink("dir-nonempty/file");
 
44
  rmdir("dir-nonempty");
 
45
}
 
46
 
 
47
void test() {
 
48
  int err;
 
49
 
 
50
  // can't rename something that doesn't exist
 
51
  err = rename("noexist", "dir");
 
52
  assert(err == -1);
 
53
  assert(errno == ENOENT);
 
54
 
 
55
  // can't overwrite a folder with a file
 
56
  err = rename("file", "dir");
 
57
  assert(err == -1);
 
58
  assert(errno == EISDIR);
 
59
 
 
60
  // can't overwrite a file with a folder
 
61
  err = rename("dir", "file");
 
62
  assert(err == -1);
 
63
  assert(errno == ENOTDIR);
 
64
 
 
65
  // can't overwrite a non-empty folder
 
66
  err = rename("dir", "dir-nonempty");
 
67
  assert(err == -1);
 
68
  assert(errno == ENOTEMPTY);
 
69
 
 
70
  // can't create anything in a read-only directory
 
71
  err = rename("dir", "dir-readonly/dir");
 
72
  assert(err == -1);
 
73
  assert(errno == EACCES);
 
74
 
 
75
  // source should not be ancestor of target
 
76
  err = rename("dir", "dir/somename");
 
77
  assert(err == -1);
 
78
  assert(errno == EINVAL);
 
79
 
 
80
  // target should not be an ancestor of source
 
81
  err = rename("dir/subdir", "dir");
 
82
  assert(err == -1);
 
83
  assert(errno == ENOTEMPTY);
 
84
 
 
85
  // do some valid renaming
 
86
  err = rename("dir/file", "dir/file1");
 
87
  assert(!err);
 
88
  err = rename("dir/file1", "dir/file2");
 
89
  assert(!err);
 
90
  err = access("dir/file2", F_OK);
 
91
  assert(!err);
 
92
  err = rename("dir/subdir", "dir/subdir1");
 
93
  assert(!err);
 
94
  err = rename("dir/subdir1", "dir/subdir2");
 
95
  assert(!err);
 
96
  err = access("dir/subdir2", F_OK);
 
97
  assert(!err);
 
98
 
 
99
  puts("success");
 
100
}
 
101
 
 
102
int main() {
 
103
  atexit(cleanup);
 
104
  signal(SIGABRT, cleanup);
 
105
  setup();
 
106
  test();
 
107
  return EXIT_SUCCESS;
 
108
}