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

« back to all changes in this revision

Viewing changes to tests/stat/test_chmod.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 <dirent.h>
 
3
#include <errno.h>
 
4
#include <fcntl.h>
 
5
#include <signal.h>
 
6
#include <stdio.h>
 
7
#include <stdlib.h>
 
8
#include <string.h>
 
9
#include <unistd.h>
 
10
#include <utime.h>
 
11
#include <sys/stat.h>
 
12
#include <sys/types.h>
 
13
 
 
14
void create_file(const char *path, const char *buffer, int mode) {
 
15
  int fd = open(path, O_WRONLY | O_CREAT | O_EXCL, mode);
 
16
  assert(fd >= 0);
 
17
 
 
18
  int err = write(fd, buffer, sizeof(char) * strlen(buffer));
 
19
  assert(err ==  (sizeof(char) * strlen(buffer)));
 
20
 
 
21
  close(fd);
 
22
}
 
23
 
 
24
void setup() {
 
25
  create_file("file", "abcdef", 0777);
 
26
  symlink("file", "file-link");
 
27
  // some platforms use 777, some use 755 by default for symlinks
 
28
  // make sure we're using 777 for the test
 
29
  lchmod("file-link", 0777);
 
30
  mkdir("folder", 0777);
 
31
}
 
32
 
 
33
void cleanup() {
 
34
  unlink("file-link");
 
35
  unlink("file");
 
36
  rmdir("folder");
 
37
}
 
38
 
 
39
void test() {
 
40
  int err;
 
41
  int lastctime;
 
42
  struct stat s;
 
43
  
 
44
  //
 
45
  // chmod a file
 
46
  //
 
47
  // get the current ctime for the file
 
48
  memset(&s, 0, sizeof s);
 
49
  stat("file", &s);
 
50
  lastctime = s.st_ctime;
 
51
  sleep(1);
 
52
 
 
53
  // do the actual chmod
 
54
  err = chmod("file", 0200);
 
55
  assert(!err);
 
56
 
 
57
  memset(&s, 0, sizeof s);
 
58
  stat("file", &s);
 
59
  assert(s.st_mode == (0200 | S_IFREG));
 
60
  assert(s.st_ctime != lastctime);
 
61
 
 
62
  //
 
63
  // fchmod a file
 
64
  //
 
65
  lastctime = s.st_ctime;
 
66
  sleep(1);
 
67
 
 
68
  err = fchmod(open("file", O_WRONLY), 0100);
 
69
  assert(!err);
 
70
 
 
71
  memset(&s, 0, sizeof s);
 
72
  stat("file", &s);
 
73
  assert(s.st_mode == (0100 | S_IFREG));
 
74
  assert(s.st_ctime != lastctime);
 
75
 
 
76
  //
 
77
  // chmod a folder
 
78
  //
 
79
  // get the current ctime for the folder
 
80
  memset(&s, 0, sizeof s);
 
81
  stat("folder", &s);
 
82
  lastctime = s.st_ctime;
 
83
  sleep(1);
 
84
 
 
85
  // do the actual chmod
 
86
  err = chmod("folder", 0300);
 
87
  assert(!err);
 
88
  memset(&s, 0, sizeof s);
 
89
  stat("folder", &s);
 
90
  assert(s.st_mode == (0300 | S_IFDIR));
 
91
  assert(s.st_ctime != lastctime);
 
92
 
 
93
  //
 
94
  // chmod a symlink's target
 
95
  //
 
96
  err = chmod("file-link", 0400);
 
97
  assert(!err);
 
98
 
 
99
  // make sure the file it references changed
 
100
  stat("file-link", &s);
 
101
  assert(s.st_mode == (0400 | S_IFREG));
 
102
 
 
103
  // but the link didn't
 
104
  lstat("file-link", &s);
 
105
  assert(s.st_mode == (0777 | S_IFLNK));
 
106
 
 
107
  //
 
108
  // chmod the actual symlink
 
109
  //
 
110
  err = lchmod("file-link", 0500);
 
111
  assert(!err);
 
112
 
 
113
  // make sure the file it references didn't change
 
114
  stat("file-link", &s);
 
115
  assert(s.st_mode == (0400 | S_IFREG));
 
116
 
 
117
  // but the link did
 
118
  lstat("file-link", &s);
 
119
  assert(s.st_mode == (0500 | S_IFLNK));
 
120
 
 
121
  puts("success");
 
122
}
 
123
 
 
124
int main() {
 
125
  atexit(cleanup);
 
126
  signal(SIGABRT, cleanup);
 
127
  setup();
 
128
  test();
 
129
  return EXIT_SUCCESS;
 
130
}