rvm  1.11
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
test-fs.cc
Go to the documentation of this file.
1 #include "config.h"
2 
3 #include <iostream>
4 #include <fstream>
5 #include <cstdio>
6 #include <cassert>
7 
8 #include "asserts.h"
9 #include "error.h"
10 #include "fs.h"
11 
12 #include "test-fs-cwd.h"
13 
14 #define TRY_COMMAND(code) \
15  thrown = false; \
16  try { \
17  code; \
18  } \
19  catch(...) { \
20  thrown = true; \
21  }
22 
23 void create_file(const std::string& pathname, uint32 size)
24 {
25  FILE *out = 0;
26  char alpha[] =
27  "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
28  int alpha_len = strlen(alpha);
29  uint32 count;
30 
31  out = fopen(pathname.c_str(), "wb");
32  if (out == 0) {
33  std::string es;
34 
35  es = "Could not open file \"";
36  es += pathname;
37  es += "\"";
38 
39  throw(ERROR(0,es));
40  }
41  for (count = 0; count < size; count++) {
42  if (fputc(alpha[count % alpha_len], out) == EOF) {
43  std::string es;
44 
45  es = "Write error on file \"";
46  es += pathname;
47  es += "\"";
48 
49  throw(ERROR(0,es));
50  }
51  }
52  if (fclose(out) != 0) {
53  std::string es;
54 
55  es = "Close error on file \"";
56  es += pathname;
57  es += "\"";
58 
59  throw(ERROR(0,es));
60  }
61 }
62 
63 void cleanup(void)
64 {
65  int r = 0;
66 
67  r = system("rm -fr ./test-fs.dir");
68  assert(r == 0);
69 }
70 
71 void test_cwd(void)
72 {
73  if (cwd() != check_cwd) {
74  std::cerr << "NOTICE: The following check failed: test_cwd()" << std::endl;
75  std::cerr << "ceck_cwd = " << check_cwd << std::endl;
76  std::cerr << " cwd() = " << cwd() << std::endl;
77  }
78  assert(cwd() == check_cwd);
79 }
80 
81 void test_reform_path(void)
82 {
83  assert(reform_path("") == "");
84  assert(reform_path("/") == "/");
85  assert(reform_path("//") == "/");
86  assert(reform_path("//var") == "/var");
87  assert(reform_path("/var") == "/var");
88  assert(reform_path("/var/rvm") == "/var/rvm");
89  assert(reform_path("/var/rvm//") == "/var/rvm/");
90  assert(reform_path("//var//rvm/////") == "/var/rvm/");
91 }
92 
94 {
95  assert(permute_path("") == "root");
96  assert(permute_path("/") == "root");
97  assert(permute_path("//") == "root");
98  assert(permute_path("//var") == "var");
99  assert(permute_path("/var") == "var");
100  assert(permute_path("/var/rvm") == "var-rvm");
101  assert(permute_path("/var/rvm//") == "var-rvm");
102  assert(permute_path("//var//rvm/////") == "var-rvm");
103 }
104 
105 void test_exists(void)
106 {
107  assert(exists("./INSTALL"));
108  assert(!exists("./lkjsdflkjsdf.h"));
109 }
110 
111 void test_readable(void)
112 {
113  assert(readable("./INSTALL"));
114 }
115 
116 void test_writable(void)
117 {
118  assert(writable("./INSTALL"));
119 }
120 
121 void test_executable(void)
122 {
123  assert(!exists("./test-fs") || executable("./test-fs"));
124  assert(!exists("./test-fs.exe") || executable("./test-fs.exe"));
125 }
126 
127 void test_mk_dir(void)
128 {
129  bool thrown;
130 
131  assert(!exists("./test-fs.dir"));
132  TRY_COMMAND(mk_dir("./test-fs.dir"));
133  assert(!thrown);
134  assert(exists("./test-fs.dir"));
135 }
136 
137 void test_rm_dir(void)
138 {
139  bool thrown;
140 
141  assert(exists("./test-fs.dir"));
142  TRY_COMMAND(rm_dir("./test-fs.dir"));
143  assert(!thrown);
144  assert(!exists("./test-fs.dir"));
145 }
146 
147 void test_rm_file(void)
148 {
149  bool thrown;
150 
151  if (!exists("./test-fs.dir"))
152  mk_dir("./test-fs.dir");
153  create_file("./test-fs.dir/file", 1024);
154  assert(exists("./test-fs.dir/file"));
155  TRY_COMMAND(rm_file("./test-fs.dir/file"));
156  assert(!thrown);
157  assert(!exists("./test-fs.dir/file"));
158  TRY_COMMAND(rm_file("./test-fs.dir"));
159  assert(thrown);
160  TRY_COMMAND(rm_dir("./test-fs.dir"));
161  assert(!thrown);
162  assert(!exists("./test-fs.dir"));
163 }
164 
165 void test_mk_dirhier(void)
166 {
167  bool thrown;
168 
169  assert(!exists("./test-fs.dir"));
170  TRY_COMMAND(mk_dirhier("./test-fs.dir/etc/rvm"));
171  assert(!thrown);
172  assert(exists("./test-fs.dir"));
173  assert(exists("./test-fs.dir/etc"));
174  assert(exists("./test-fs.dir/etc/rvm"));
175  TRY_COMMAND(mk_dirhier("./test-fs.dir/var/rvm"));
176  assert(!thrown);
177  assert(exists("./test-fs.dir"));
178  assert(exists("./test-fs.dir/var"));
179  assert(exists("./test-fs.dir/var/rvm"));
180 }
181 
182 void test_filestatus(void)
183 {
184  bool thrown;
185  filestatus f;
186 
187  assert(exists("./test-fs.dir"));
188  assert(exists("./test-fs.dir/etc"));
189  assert(exists("./test-fs.dir/etc/rvm"));
190  assert(exists("./test-fs.dir/var"));
191  assert(exists("./test-fs.dir/var/rvm"));
192  create_file("./test-fs.dir/file1",1024);
193  create_file("./test-fs.dir/file2",234);
194  create_file("./test-fs.dir/etc/file1",988);
195  create_file("./test-fs.dir/etc/rvm/file1",333);
196  create_file("./test-fs.dir/var/rvm/file1",4484);
197 
198  TRY_COMMAND(f.path("./test-fs.dir"));
199  assert(!thrown);
200 #ifdef S_ISDIR
201  assert(f.is_directory());
202 #endif
203 
204  TRY_COMMAND(f.path("./test-fs.dir/file1"));
205  assert(!thrown);
206 #ifdef S_ISREG
207  assert(f.is_regular_file());
208 #endif
209  assert(f.size() == 1024);
210 
211  TRY_COMMAND(f.path("./test-fs.dir/var/rvm/file1"));
212  assert(!thrown);
213 #ifdef S_ISREG
214  assert(f.is_regular_file());
215 #endif
216  assert(f.size() == 4484);
217 }
218 
219 void test_mk_symlink(void)
220 {
221  bool thrown;
222  filestatus f;
223 
224  assert(exists("./test-fs.dir/file2"));
225  TRY_COMMAND(mk_symlink("file2", "./test-fs.dir/link-file2"));
226  assert(!thrown);
227  assert(exists("./test-fs.dir/link-file2"));
228  TRY_COMMAND(f.path("./test-fs.dir/link-file2"));
229  assert(!thrown);
230 #ifdef S_ISLNK
231  assert(f.is_link());
232  assert(f.link() == "file2");
233 #endif
234 }
235 
237 {
238  bool thrown;
239  filestatus f;
240 
241  assert(exists("./test-fs.dir/etc/rvm"));
242  assert(exists("./test-fs.dir/file1"));
243  assert(exists("./test-fs.dir/var/rvm"));
244  TRY_COMMAND(
245  mk_relative_symlink("./test-fs.dir/file1","./test-fs.dir/link-file1"));
246  assert(!thrown);
247  assert(exists("./test-fs.dir/link-file1"));
248  TRY_COMMAND(f.path("./test-fs.dir/link-file1"));
249  assert(!thrown);
250 #ifdef S_ISLNK
251  assert(f.is_link());
252  assert(f.link() == "file1");
253 #endif
254  TRY_COMMAND(
255  mk_relative_symlink("./test-fs.dir/file1",
256  "./test-fs.dir/etc/rvm/link-file1"));
257  assert(!thrown);
258  assert(exists("./test-fs.dir/etc/rvm/link-file1"));
259  TRY_COMMAND(f.path("./test-fs.dir/etc/rvm/link-file1"));
260  assert(!thrown);
261 #ifdef S_ISLNK
262  assert(f.is_link());
263  assert(f.link() == "../../file1");
264 #endif
265  TRY_COMMAND(
266  mk_relative_symlink("./test-fs.dir/var/rvm/file1",
267  "./test-fs.dir/etc/rvm/link-rvm-file1"));
268  assert(!thrown);
269  assert(exists("./test-fs.dir/etc/rvm/link-rvm-file1"));
270  TRY_COMMAND(f.path("./test-fs.dir/etc/rvm/link-rvm-file1"));
271 #ifdef S_INLNK
272  assert(f.is_link());
273  assert(f.link() == "../../var/rvm/file1");
274 #endif
275 }
276 
278 {
279  subdirectory subdir;
280 
281  subdir.path("./test-fs.dir/etc/rvm","l*1");
282  return(subdir);
283 }
284 
286 {
287  bool thrown;
288  subdirectory subdir;
289  subdirectory subdir2;
290  subdirectory subdir3;
291 
292  TRY_COMMAND(subdir.path("./test-fs.dir"));
293  assert(!thrown);
294  assert(subdir.size() == 6);
295  assert(subdir[0] == "etc");
296  assert(subdir[1] == "file1");
297  assert(subdir[2] == "file2");
298  assert(subdir[3] == "link-file1");
299  assert(subdir[4] == "link-file2");
300  assert(subdir[5] == "var");
301 
302  TRY_COMMAND(subdir.path("./test-fs.dir","l*"));
303  assert(!thrown);
304  assert(subdir.size() == 2);
305  assert(subdir[0] == "link-file1");
306  assert(subdir[1] == "link-file2");
307 
308  TRY_COMMAND(subdir.path("./test-fs.dir/etc/rvm","l*1"));
309  assert(!thrown);
310  assert(subdir.size() == 2);
311  assert(subdir[0] == "link-file1");
312  assert(subdir[1] == "link-rvm-file1");
313 
314  TRY_COMMAND(subdir2 = subdir);
315  assert(!thrown);
316  assert(subdir2.size() == 2);
317  assert(subdir2[0] == "link-file1");
318  assert(subdir2[1] == "link-rvm-file1");
319 
320  TRY_COMMAND(subdir3 = test_subdirectory_sub());
321  assert(!thrown);
322  assert(subdir3.size() == 2);
323  assert(subdir3[0] == "link-file1");
324  assert(subdir3[1] == "link-rvm-file1");
325 }
326 
327 void test_directory(void)
328 {
329  bool thrown;
330  directory dir;
331 
332  TRY_COMMAND(dir.path("./test-fs.dir"));
333  assert(!thrown);
334  assert(dir.size() == 1);
335  assert(dir[0] == "./test-fs.dir");
336 
337  TRY_COMMAND(dir.path("./test-fs.dir/*"));
338  assert(!thrown);
339  assert(dir.size() == 6);
340  assert(dir[0] == "./test-fs.dir/etc");
341  assert(dir[1] == "./test-fs.dir/file1");
342  assert(dir[2] == "./test-fs.dir/file2");
343  assert(dir[3] == "./test-fs.dir/link-file1");
344  assert(dir[4] == "./test-fs.dir/link-file2");
345  assert(dir[5] == "./test-fs.dir/var");
346 
347  TRY_COMMAND(dir.path("./test-fs.dir/etc/rvm/l*1"));
348  assert(!thrown);
349  assert(dir.size() == 2);
350  assert(dir[0] == "./test-fs.dir/etc/rvm/link-file1");
351  assert(dir[1] == "./test-fs.dir/etc/rvm/link-rvm-file1");
352 
353  TRY_COMMAND(dir.path("./test-fs.dir/*/*/*"));
354  assert(!thrown);
355  assert(dir.size() == 4);
356  assert(dir[0] == "./test-fs.dir/etc/rvm/file1");
357  assert(dir[1] == "./test-fs.dir/etc/rvm/link-file1");
358  assert(dir[2] == "./test-fs.dir/etc/rvm/link-rvm-file1");
359  assert(dir[3] == "./test-fs.dir/var/rvm/file1");
360 }
361 
363 {
364  bool thrown;
365 
366  TRY_COMMAND(rm_recursive("./test-fs.dir"));
367  assert(!thrown);
368  assert(!exists("./test-fs.dir"));
369 }
370 
372 {
373  assert(
375  "./test-fs.dir/dir1a/dir2a/dir3a/file.conf",
376  "./test-fs.dir/dir1a/dir2a/dir3a"
377  )
378  == "file.conf"
379  );
380  assert(
382  "./test-fs.dir/dir1a/dir2a/dir3a/file.conf",
383  "./test-fs.dir/dir1a/dir2a/dir3b"
384  )
385  == "../dir3a/file.conf"
386  );
387  assert(
389  "./test-fs.dir/dir1a/dir2a/dir3a/file.conf",
390  "./test-fs.dir/dir1a/dir2b/dir3b"
391  )
392  == "../../dir2a/dir3a/file.conf"
393  );
394  assert(
396  "./test-fs.dir/dir1a/dir2a/dir3a/file.conf",
397  "./test-fs.dir/dir1b/dir2b/dir3b"
398  )
399  == "../../../dir1a/dir2a/dir3a/file.conf"
400  );
401  assert(
403  "./test-fs.dir/dir1a/dir2a/dir3a/file.conf",
404  "./test-fs.dir.b/dir1b/dir2b/dir3b"
405  )
406  == "../../../../test-fs.dir/dir1a/dir2a/dir3a/file.conf"
407  );
408 }
409 
410 void test_filesystem(void)
411 {
412  filesystem fsys;
413 
414  fsys.path(cwd());
415 }
416 
418 {
419  simple_lock sl;
420  bool thrown;
421 
422  assert(system("rm -fr ./test-fs.dir") == 0);
423  mk_dir("./test-fs.dir");
424  assert(exists("./test-fs.dir"));
425 
426  thrown = false;
427  try {
428  sl.lockfile("./test-fs.dir/lock");
429  }
430  catch(...) {
431  thrown = true;
432  }
433  assert(!thrown);
434 
435  assert(sl.locked_by() == 0);
436  assert(sl.is_locked() == false);
437 
438  thrown = false;
439  std::ofstream out;
440  out.open("./test-fs.dir/lock");
441  assert(out.is_open());
442  out << getpid()+1234 << std::endl;
443  assert((out));
444  out.close();
445  assert(sl.locked_by() == getpid()+1234);
446  assert(sl.is_locked() == false);
447 
448  thrown = false;
449  try {
450  pid_t pid;
451 
452  assert((pid = ::fork()) >= 0);
453  if (pid > 0) {
454  // Parent
455  sleep(1);
456  assert(sl.locked_by() == pid);
457  assert(sl.is_locked());
458  assert(system("rm -f ./test-fs.dir/lock") == 0);
459  assert(!exists("./test-fs.dir/lock"));
460  }
461  else {
462  // Child
463  assert(sl.lock() == true);
464  while (exists("./test-fs.dir/lock"));
465  exit(0);
466  }
467  }
468  catch(...) {
469  thrown = true;
470  }
471  assert(!thrown);
472 
473  assert(sl.locked_by() == 0);
474  assert(sl.is_locked() == false);
475  assert(!exists("./test-fs.dir/lock"));
476 
477  thrown = false;
478  try {
479  assert(sl.lock() == true);
480  }
481  catch(...) {
482  thrown = true;
483  }
484  assert(!thrown);
485 
486  assert(sl.locked_by() == getpid());
487  assert(sl.is_locked() == true);
488  assert(exists("./test-fs.dir/lock"));
489 
490  thrown = false;
491  try {
492  sl.unlock();
493  }
494  catch(...) {
495  thrown = true;
496  }
497  assert(!thrown);
498 
499  assert(sl.locked_by() == 0);
500  assert(sl.is_locked() == false);
501  assert(!exists("./test-fs.dir/lock"));
502 
503  assert(system("rm -fr ./test-fs.dir") == 0);
504  assert(!exists("./test-fs.dir"));
505 }
506 
507 int main(int argc, char const * argv[])
508 {
509  cleanup();
510  try {
511  // test_cwd();
514  test_exists();
515  test_readable();
516  test_writable();
517  test_executable();
518  test_mk_dir();
519  test_rm_dir();
520  test_rm_file();
521  test_mk_dirhier();
522  test_filestatus();
523  test_mk_symlink();
526  test_directory();
529  test_filesystem();
531  }
532  catch(error e) {
533  std::cerr << e;
534  assert(0);
535  }
536  catch(...) {
537  std::cerr << err_unknown;
538  assert(0);
539  }
540  cleanup();
541  return(0);
542 }
#define TRY_COMMAND(code)
Definition: test-fs.cc:14
const pid_t pid(void)
Return the PID of this process.
Definition: fs.cc:162
std::string reform_path(const std::string &a_path)
Reformat a path to remove double slashes.
Definition: fs.cc:205
const std::string cwd(void)
Return the current working directory.
Definition: fs.cc:148
Retrieve information about a filesystem.
Definition: fs.h:316
void mk_dir(const std::string &a_path)
Create a directory.
Definition: fs.cc:599
void path(const std::string a_path)
Retrieve information about a pathname.
Definition: fs.cc:824
void test_simple_lock(void)
Definition: test-fs.cc:417
const type & path(const std::string &a_path)
Retrieve a list of paths that match the wildcard path given.
Definition: fs.cc:1510
void test_mk_relative_path(void)
Definition: test-fs.cc:371
const subdirectory test_subdirectory_sub(void)
Definition: test-fs.cc:277
void test_exists(void)
Definition: test-fs.cc:105
void path(const std::string &a_path)
Retrieve information about the filesystem on which the given path resides.
Definition: fs.cc:1600
bool executable(const std::string &a_path)
Return true if the file or directory exists and is executable.
Definition: fs.cc:437
void test_rm_file(void)
Definition: test-fs.cc:147
void create_file(const std::string &pathname, uint32 size)
Definition: test-fs.cc:23
void mk_dirhier(const std::string a_path)
Recursively create a directory heirarchy.
Definition: fs.cc:683
void test_mk_relative_symlink(void)
Definition: test-fs.cc:236
void cleanup(void)
Definition: test-fs.cc:63
bool writable(const std::string &a_path)
Return true if the file or directory exists and is writable.
Definition: fs.cc:427
bool lock(void)
Lock.
Definition: fs.cc:1778
const std::string link(void) const
If the pathname is a link, return the path it is linked to.
Definition: fs.cc:940
void test_reform_path(void)
Definition: test-fs.cc:81
bool readable(const std::string &a_path)
Return true if the file or directory exists and is readable.
Definition: fs.cc:417
void test_mk_symlink(void)
Definition: test-fs.cc:219
#define err_unknown
Definition: error.h:114
Retrieve a list of pathnames that match a given wildcard path.
Definition: fs.h:300
void lockfile(const std::string &a_lockfile)
Set the lockfile path.
Definition: fs.cc:1733
bool exists(const std::string &a_path)
Return true if the file or directory exists.
Definition: fs.cc:385
void test_executable(void)
Definition: test-fs.cc:121
void test_subdirectory(void)
Definition: test-fs.cc:285
void unlock(void)
Unlock.
Definition: fs.cc:1798
void rm_dir(const std::string a_path)
Remove a directory.
Definition: fs.cc:612
A simple locking mechanism.
Definition: fs.h:343
An error class.
Definition: error.h:72
const type & path(const std::string a_path, const std::string a_filter="*")
Return a vector of strings of a list of files in a subdirectory.
Definition: fs.cc:1361
void test_mk_dirhier(void)
Definition: test-fs.cc:165
void test_rm_recursive(void)
Definition: test-fs.cc:362
void mk_relative_symlink(const std::string a_from, const std::string a_to)
Given a from and to path, create a relative symbolic link.
Definition: fs.cc:770
const pid_type locked_by(void) const
Get the PID of the locking process.
Definition: fs.cc:1746
void test_cwd(void)
Definition: test-fs.cc:71
void rm_recursive(const std::string a_path)
Recursively delete the contents of a directory.
Definition: fs.cc:1414
Retrieve information about a file or directory.
Definition: fs.h:122
std::string permute_path(const std::string &a_path)
Reformat a path to remove the begining and trailing slashes, and replace all other slashes with under...
Definition: fs.cc:224
std::string mk_relative_path(const std::string a_path_to, const std::string a_path_from)
Make the path a_path_to relative from a_path_from, where a_path_to and a_path_from are directory name...
Definition: fs.cc:314
void test_filestatus(void)
Definition: test-fs.cc:182
void test_writable(void)
Definition: test-fs.cc:116
void test_permute_path(void)
Definition: test-fs.cc:93
int main(int argc, char const *argv[])
Definition: test-fs.cc:507
const size_type size(void) const
Return the file size in bytes.
Definition: fs.cc:1000
void rm_file(const std::string a_path)
Remove a file.
Definition: fs.cc:637
void test_readable(void)
Definition: test-fs.cc:111
void test_rm_dir(void)
Definition: test-fs.cc:137
#define ERROR(e, s)
Definition: error.h:120
void test_mk_dir(void)
Definition: test-fs.cc:127
const char * check_cwd
Retrieve a list of files in a subdirectory that match a given wildcard filename.
Definition: fs.h:273
void test_filesystem(void)
Definition: test-fs.cc:410
const bool is_locked(void) const
Find out whether or not the lock is in place.
Definition: fs.cc:1765
void test_directory(void)
Definition: test-fs.cc:327
void mk_symlink(const std::string a_from, const std::string a_to)
Create a symbolic link.
Definition: fs.cc:754