00001 #include "config.h"
00002
00003 #include <iostream>
00004 #include <fstream>
00005 #include <cstdio>
00006 #include <cassert>
00007
00008 #include "asserts.h"
00009 #include "error.h"
00010 #include "fs.h"
00011
00012 #include "test-fs-cwd.h"
00013
00014 #define TRY_COMMAND(code) \
00015 thrown = false; \
00016 try { \
00017 code; \
00018 } \
00019 catch(...) { \
00020 thrown = true; \
00021 }
00022
00023 void create_file(const std::string& pathname, uint32 size)
00024 {
00025 FILE *out = 0;
00026 char alpha[] =
00027 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
00028 int alpha_len = strlen(alpha);
00029 uint32 count;
00030
00031 out = fopen(pathname.c_str(), "wb");
00032 if (out == 0) {
00033 std::string es;
00034
00035 es = "Could not open file \"";
00036 es += pathname;
00037 es += "\"";
00038
00039 throw(ERROR(0,es));
00040 }
00041 for (count = 0; count < size; count++) {
00042 if (fputc(alpha[count % alpha_len], out) == EOF) {
00043 std::string es;
00044
00045 es = "Write error on file \"";
00046 es += pathname;
00047 es += "\"";
00048
00049 throw(ERROR(0,es));
00050 }
00051 }
00052 if (fclose(out) != 0) {
00053 std::string es;
00054
00055 es = "Close error on file \"";
00056 es += pathname;
00057 es += "\"";
00058
00059 throw(ERROR(0,es));
00060 }
00061 }
00062
00063 void cleanup(void)
00064 {
00065 int r = 0;
00066
00067 r = system("rm -fr ./test-fs.dir");
00068 assert(r == 0);
00069 }
00070
00071 void test_cwd(void)
00072 {
00073 assert(cwd() == check_cwd);
00074 }
00075
00076 void test_reform_path(void)
00077 {
00078 assert(reform_path("") == "");
00079 assert(reform_path("/") == "/");
00080 assert(reform_path("//") == "/");
00081 assert(reform_path("//var") == "/var");
00082 assert(reform_path("/var") == "/var");
00083 assert(reform_path("/var/rvm") == "/var/rvm");
00084 assert(reform_path("/var/rvm//") == "/var/rvm/");
00085 assert(reform_path("//var//rvm/////") == "/var/rvm/");
00086 }
00087
00088 void test_permute_path(void)
00089 {
00090 assert(permute_path("") == "root");
00091 assert(permute_path("/") == "root");
00092 assert(permute_path("//") == "root");
00093 assert(permute_path("//var") == "var");
00094 assert(permute_path("/var") == "var");
00095 assert(permute_path("/var/rvm") == "var-rvm");
00096 assert(permute_path("/var/rvm//") == "var-rvm");
00097 assert(permute_path("//var//rvm/////") == "var-rvm");
00098 }
00099
00100 void test_exists(void)
00101 {
00102 assert(exists("./INSTALL"));
00103 assert(!exists("./lkjsdflkjsdf.h"));
00104 }
00105
00106 void test_readable(void)
00107 {
00108 assert(readable("./INSTALL"));
00109 }
00110
00111 void test_writable(void)
00112 {
00113 assert(writable("./INSTALL"));
00114 }
00115
00116 void test_executable(void)
00117 {
00118 assert(!exists("./test-fs") || executable("./test-fs"));
00119 assert(!exists("./test-fs.exe") || executable("./test-fs.exe"));
00120 }
00121
00122 void test_mk_dir(void)
00123 {
00124 bool thrown;
00125
00126 assert(!exists("./test-fs.dir"));
00127 TRY_COMMAND(mk_dir("./test-fs.dir"));
00128 assert(!thrown);
00129 assert(exists("./test-fs.dir"));
00130 }
00131
00132 void test_rm_dir(void)
00133 {
00134 bool thrown;
00135
00136 assert(exists("./test-fs.dir"));
00137 TRY_COMMAND(rm_dir("./test-fs.dir"));
00138 assert(!thrown);
00139 assert(!exists("./test-fs.dir"));
00140 }
00141
00142 void test_rm_file(void)
00143 {
00144 bool thrown;
00145
00146 if (!exists("./test-fs.dir"))
00147 mk_dir("./test-fs.dir");
00148 create_file("./test-fs.dir/file", 1024);
00149 assert(exists("./test-fs.dir/file"));
00150 TRY_COMMAND(rm_file("./test-fs.dir/file"));
00151 assert(!thrown);
00152 assert(!exists("./test-fs.dir/file"));
00153 TRY_COMMAND(rm_file("./test-fs.dir"));
00154 assert(thrown);
00155 TRY_COMMAND(rm_dir("./test-fs.dir"));
00156 assert(!thrown);
00157 assert(!exists("./test-fs.dir"));
00158 }
00159
00160 void test_mk_dirhier(void)
00161 {
00162 bool thrown;
00163
00164 assert(!exists("./test-fs.dir"));
00165 TRY_COMMAND(mk_dirhier("./test-fs.dir/etc/rvm"));
00166 assert(!thrown);
00167 assert(exists("./test-fs.dir"));
00168 assert(exists("./test-fs.dir/etc"));
00169 assert(exists("./test-fs.dir/etc/rvm"));
00170 TRY_COMMAND(mk_dirhier("./test-fs.dir/var/rvm"));
00171 assert(!thrown);
00172 assert(exists("./test-fs.dir"));
00173 assert(exists("./test-fs.dir/var"));
00174 assert(exists("./test-fs.dir/var/rvm"));
00175 }
00176
00177 void test_filestatus(void)
00178 {
00179 bool thrown;
00180 filestatus f;
00181
00182 assert(exists("./test-fs.dir"));
00183 assert(exists("./test-fs.dir/etc"));
00184 assert(exists("./test-fs.dir/etc/rvm"));
00185 assert(exists("./test-fs.dir/var"));
00186 assert(exists("./test-fs.dir/var/rvm"));
00187 create_file("./test-fs.dir/file1",1024);
00188 create_file("./test-fs.dir/file2",234);
00189 create_file("./test-fs.dir/etc/file1",988);
00190 create_file("./test-fs.dir/etc/rvm/file1",333);
00191 create_file("./test-fs.dir/var/rvm/file1",4484);
00192
00193 TRY_COMMAND(f.path("./test-fs.dir"));
00194 assert(!thrown);
00195 #ifdef S_ISDIR
00196 assert(f.is_directory());
00197 #endif
00198
00199 TRY_COMMAND(f.path("./test-fs.dir/file1"));
00200 assert(!thrown);
00201 #ifdef S_ISREG
00202 assert(f.is_regular_file());
00203 #endif
00204 assert(f.size() == 1024);
00205
00206 TRY_COMMAND(f.path("./test-fs.dir/var/rvm/file1"));
00207 assert(!thrown);
00208 #ifdef S_ISREG
00209 assert(f.is_regular_file());
00210 #endif
00211 assert(f.size() == 4484);
00212 }
00213
00214 void test_mk_symlink(void)
00215 {
00216 bool thrown;
00217 filestatus f;
00218
00219 assert(exists("./test-fs.dir/file2"));
00220 TRY_COMMAND(mk_symlink("file2", "./test-fs.dir/link-file2"));
00221 assert(!thrown);
00222 assert(exists("./test-fs.dir/link-file2"));
00223 TRY_COMMAND(f.path("./test-fs.dir/link-file2"));
00224 assert(!thrown);
00225 #ifdef S_ISLNK
00226 assert(f.is_link());
00227 assert(f.link() == "file2");
00228 #endif
00229 }
00230
00231 void test_mk_relative_symlink(void)
00232 {
00233 bool thrown;
00234 filestatus f;
00235
00236 assert(exists("./test-fs.dir/etc/rvm"));
00237 assert(exists("./test-fs.dir/file1"));
00238 assert(exists("./test-fs.dir/var/rvm"));
00239 TRY_COMMAND(
00240 mk_relative_symlink("./test-fs.dir/file1","./test-fs.dir/link-file1"));
00241 assert(!thrown);
00242 assert(exists("./test-fs.dir/link-file1"));
00243 TRY_COMMAND(f.path("./test-fs.dir/link-file1"));
00244 assert(!thrown);
00245 #ifdef S_ISLNK
00246 assert(f.is_link());
00247 assert(f.link() == "file1");
00248 #endif
00249 TRY_COMMAND(
00250 mk_relative_symlink("./test-fs.dir/file1",
00251 "./test-fs.dir/etc/rvm/link-file1"));
00252 assert(!thrown);
00253 assert(exists("./test-fs.dir/etc/rvm/link-file1"));
00254 TRY_COMMAND(f.path("./test-fs.dir/etc/rvm/link-file1"));
00255 assert(!thrown);
00256 #ifdef S_ISLNK
00257 assert(f.is_link());
00258 assert(f.link() == "../../file1");
00259 #endif
00260 TRY_COMMAND(
00261 mk_relative_symlink("./test-fs.dir/var/rvm/file1",
00262 "./test-fs.dir/etc/rvm/link-rvm-file1"));
00263 assert(!thrown);
00264 assert(exists("./test-fs.dir/etc/rvm/link-rvm-file1"));
00265 TRY_COMMAND(f.path("./test-fs.dir/etc/rvm/link-rvm-file1"));
00266 #ifdef S_INLNK
00267 assert(f.is_link());
00268 assert(f.link() == "../../var/rvm/file1");
00269 #endif
00270 }
00271
00272 const subdirectory test_subdirectory_sub(void)
00273 {
00274 subdirectory subdir;
00275
00276 subdir.path("./test-fs.dir/etc/rvm","l*1");
00277 return(subdir);
00278 }
00279
00280 void test_subdirectory(void)
00281 {
00282 bool thrown;
00283 subdirectory subdir;
00284 subdirectory subdir2;
00285 subdirectory subdir3;
00286
00287 TRY_COMMAND(subdir.path("./test-fs.dir"));
00288 assert(!thrown);
00289 assert(subdir.size() == 6);
00290 assert(subdir[0] == "etc");
00291 assert(subdir[1] == "file1");
00292 assert(subdir[2] == "file2");
00293 assert(subdir[3] == "link-file1");
00294 assert(subdir[4] == "link-file2");
00295 assert(subdir[5] == "var");
00296
00297 TRY_COMMAND(subdir.path("./test-fs.dir","l*"));
00298 assert(!thrown);
00299 assert(subdir.size() == 2);
00300 assert(subdir[0] == "link-file1");
00301 assert(subdir[1] == "link-file2");
00302
00303 TRY_COMMAND(subdir.path("./test-fs.dir/etc/rvm","l*1"));
00304 assert(!thrown);
00305 assert(subdir.size() == 2);
00306 assert(subdir[0] == "link-file1");
00307 assert(subdir[1] == "link-rvm-file1");
00308
00309 TRY_COMMAND(subdir2 = subdir);
00310 assert(!thrown);
00311 assert(subdir2.size() == 2);
00312 assert(subdir2[0] == "link-file1");
00313 assert(subdir2[1] == "link-rvm-file1");
00314
00315 TRY_COMMAND(subdir3 = test_subdirectory_sub());
00316 assert(!thrown);
00317 assert(subdir3.size() == 2);
00318 assert(subdir3[0] == "link-file1");
00319 assert(subdir3[1] == "link-rvm-file1");
00320 }
00321
00322 void test_directory(void)
00323 {
00324 bool thrown;
00325 directory dir;
00326
00327 TRY_COMMAND(dir.path("./test-fs.dir"));
00328 assert(!thrown);
00329 assert(dir.size() == 1);
00330 assert(dir[0] == "./test-fs.dir");
00331
00332 TRY_COMMAND(dir.path("./test-fs.dir/*"));
00333 assert(!thrown);
00334 assert(dir.size() == 6);
00335 assert(dir[0] == "./test-fs.dir/etc");
00336 assert(dir[1] == "./test-fs.dir/file1");
00337 assert(dir[2] == "./test-fs.dir/file2");
00338 assert(dir[3] == "./test-fs.dir/link-file1");
00339 assert(dir[4] == "./test-fs.dir/link-file2");
00340 assert(dir[5] == "./test-fs.dir/var");
00341
00342 TRY_COMMAND(dir.path("./test-fs.dir/etc/rvm/l*1"));
00343 assert(!thrown);
00344 assert(dir.size() == 2);
00345 assert(dir[0] == "./test-fs.dir/etc/rvm/link-file1");
00346 assert(dir[1] == "./test-fs.dir/etc/rvm/link-rvm-file1");
00347
00348 TRY_COMMAND(dir.path("./test-fs.dir/*/*/*"));
00349 assert(!thrown);
00350 assert(dir.size() == 4);
00351 assert(dir[0] == "./test-fs.dir/etc/rvm/file1");
00352 assert(dir[1] == "./test-fs.dir/etc/rvm/link-file1");
00353 assert(dir[2] == "./test-fs.dir/etc/rvm/link-rvm-file1");
00354 assert(dir[3] == "./test-fs.dir/var/rvm/file1");
00355 }
00356
00357 void test_rm_recursive(void)
00358 {
00359 bool thrown;
00360
00361 TRY_COMMAND(rm_recursive("./test-fs.dir"));
00362 assert(!thrown);
00363 assert(!exists("./test-fs.dir"));
00364 }
00365
00366 void test_mk_relative_path(void)
00367 {
00368 assert(
00369 mk_relative_path(
00370 "./test-fs.dir/dir1a/dir2a/dir3a/file.conf",
00371 "./test-fs.dir/dir1a/dir2a/dir3a"
00372 )
00373 == "file.conf"
00374 );
00375 assert(
00376 mk_relative_path(
00377 "./test-fs.dir/dir1a/dir2a/dir3a/file.conf",
00378 "./test-fs.dir/dir1a/dir2a/dir3b"
00379 )
00380 == "../dir3a/file.conf"
00381 );
00382 assert(
00383 mk_relative_path(
00384 "./test-fs.dir/dir1a/dir2a/dir3a/file.conf",
00385 "./test-fs.dir/dir1a/dir2b/dir3b"
00386 )
00387 == "../../dir2a/dir3a/file.conf"
00388 );
00389 assert(
00390 mk_relative_path(
00391 "./test-fs.dir/dir1a/dir2a/dir3a/file.conf",
00392 "./test-fs.dir/dir1b/dir2b/dir3b"
00393 )
00394 == "../../../dir1a/dir2a/dir3a/file.conf"
00395 );
00396 assert(
00397 mk_relative_path(
00398 "./test-fs.dir/dir1a/dir2a/dir3a/file.conf",
00399 "./test-fs.dir.b/dir1b/dir2b/dir3b"
00400 )
00401 == "../../../../test-fs.dir/dir1a/dir2a/dir3a/file.conf"
00402 );
00403 }
00404
00405 void test_filesystem(void)
00406 {
00407 filesystem fsys;
00408
00409 fsys.path(cwd());
00410 }
00411
00412 void test_simple_lock(void)
00413 {
00414 simple_lock sl;
00415 bool thrown;
00416
00417 system("rm -fr ./test-fs.dir");
00418 mk_dir("./test-fs.dir");
00419 assert(exists("./test-fs.dir"));
00420
00421 thrown = false;
00422 try {
00423 sl.lockfile("./test-fs.dir/lock");
00424 }
00425 catch(...) {
00426 thrown = true;
00427 }
00428 assert(!thrown);
00429
00430 assert(sl.locked_by() == 0);
00431 assert(sl.is_locked() == false);
00432
00433 thrown = false;
00434 std::ofstream out;
00435 out.open("./test-fs.dir/lock");
00436 assert(out.is_open());
00437 out << getpid()+1234 << std::endl;
00438 assert((out));
00439 out.close();
00440 assert(sl.locked_by() == getpid()+1234);
00441 assert(sl.is_locked() == false);
00442
00443 thrown = false;
00444 try {
00445 pid_t pid;
00446
00447 assert((pid = ::fork()) >= 0);
00448 if (pid > 0) {
00449
00450 sleep(1);
00451 assert(sl.locked_by() == pid);
00452 assert(sl.is_locked());
00453 system("rm -f ./test-fs.dir/lock");
00454 assert(!exists("./test-fs.dir/lock"));
00455 }
00456 else {
00457
00458 assert(sl.lock() == true);
00459 while (exists("./test-fs.dir/lock"));
00460 exit(0);
00461 }
00462 }
00463 catch(...) {
00464 thrown = true;
00465 }
00466 assert(!thrown);
00467
00468 assert(sl.locked_by() == 0);
00469 assert(sl.is_locked() == false);
00470 assert(!exists("./test-fs.dir/lock"));
00471
00472 thrown = false;
00473 try {
00474 assert(sl.lock() == true);
00475 }
00476 catch(...) {
00477 thrown = true;
00478 }
00479 assert(!thrown);
00480
00481 assert(sl.locked_by() == getpid());
00482 assert(sl.is_locked() == true);
00483 assert(exists("./test-fs.dir/lock"));
00484
00485 thrown = false;
00486 try {
00487 sl.unlock();
00488 }
00489 catch(...) {
00490 thrown = true;
00491 }
00492 assert(!thrown);
00493
00494 assert(sl.locked_by() == 0);
00495 assert(sl.is_locked() == false);
00496 assert(!exists("./test-fs.dir/lock"));
00497
00498 system("rm -fr ./test-fs.dir");
00499 assert(!exists("./test-fs.dir"));
00500 }
00501
00502 int main(int argc, char *argv[])
00503 {
00504 cleanup();
00505 try {
00506 test_cwd();
00507 test_reform_path();
00508 test_permute_path();
00509 test_exists();
00510 test_readable();
00511 test_writable();
00512 test_executable();
00513 test_mk_dir();
00514 test_rm_dir();
00515 test_rm_file();
00516 test_mk_dirhier();
00517 test_filestatus();
00518 test_mk_symlink();
00519 test_mk_relative_symlink();
00520 test_subdirectory();
00521 test_directory();
00522 test_rm_recursive();
00523 test_mk_relative_path();
00524 test_filesystem();
00525 test_simple_lock();
00526 }
00527 catch(error e) {
00528 std::cerr << e;
00529 assert(0);
00530 }
00531 catch(...) {
00532 std::cerr << err_unknown;
00533 assert(0);
00534 }
00535 cleanup();
00536 return(0);
00537 }