rvm 1.08

test-fs.cc

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