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("./ChangeLog")); 00103 assert(exists("./INSTALL")); 00104 assert(!exists("./lkjsdflkjsdf.h")); 00105 } 00106 00107 void test_readable(void) 00108 { 00109 assert(readable("./ChangeLog")); 00110 assert(readable("./INSTALL")); 00111 } 00112 00113 void test_writable(void) 00114 { 00115 assert(writable("./ChangeLog")); 00116 assert(writable("./INSTALL")); 00117 } 00118 00119 void test_executable(void) 00120 { 00121 assert(!exists("./test-fs") || executable("./test-fs")); 00122 assert(!exists("./test-fs.exe") || executable("./test-fs.exe")); 00123 } 00124 00125 void test_mk_dir(void) 00126 { 00127 bool thrown; 00128 00129 assert(!exists("./test-fs.dir")); 00130 TRY_COMMAND(mk_dir("./test-fs.dir")); 00131 assert(!thrown); 00132 assert(exists("./test-fs.dir")); 00133 } 00134 00135 void test_rm_dir(void) 00136 { 00137 bool thrown; 00138 00139 assert(exists("./test-fs.dir")); 00140 TRY_COMMAND(rm_dir("./test-fs.dir")); 00141 assert(!thrown); 00142 assert(!exists("./test-fs.dir")); 00143 } 00144 00145 void test_rm_file(void) 00146 { 00147 bool thrown; 00148 00149 if (!exists("./test-fs.dir")) 00150 mk_dir("./test-fs.dir"); 00151 create_file("./test-fs.dir/file", 1024); 00152 assert(exists("./test-fs.dir/file")); 00153 TRY_COMMAND(rm_file("./test-fs.dir/file")); 00154 assert(!thrown); 00155 assert(!exists("./test-fs.dir/file")); 00156 TRY_COMMAND(rm_file("./test-fs.dir")); 00157 assert(thrown); 00158 TRY_COMMAND(rm_dir("./test-fs.dir")); 00159 assert(!thrown); 00160 assert(!exists("./test-fs.dir")); 00161 } 00162 00163 void test_mk_dirhier(void) 00164 { 00165 bool thrown; 00166 00167 assert(!exists("./test-fs.dir")); 00168 TRY_COMMAND(mk_dirhier("./test-fs.dir/etc/rvm")); 00169 assert(!thrown); 00170 assert(exists("./test-fs.dir")); 00171 assert(exists("./test-fs.dir/etc")); 00172 assert(exists("./test-fs.dir/etc/rvm")); 00173 TRY_COMMAND(mk_dirhier("./test-fs.dir/var/rvm")); 00174 assert(!thrown); 00175 assert(exists("./test-fs.dir")); 00176 assert(exists("./test-fs.dir/var")); 00177 assert(exists("./test-fs.dir/var/rvm")); 00178 } 00179 00180 void test_filestatus(void) 00181 { 00182 bool thrown; 00183 filestatus f; 00184 00185 assert(exists("./test-fs.dir")); 00186 assert(exists("./test-fs.dir/etc")); 00187 assert(exists("./test-fs.dir/etc/rvm")); 00188 assert(exists("./test-fs.dir/var")); 00189 assert(exists("./test-fs.dir/var/rvm")); 00190 create_file("./test-fs.dir/file1",1024); 00191 create_file("./test-fs.dir/file2",234); 00192 create_file("./test-fs.dir/etc/file1",988); 00193 create_file("./test-fs.dir/etc/rvm/file1",333); 00194 create_file("./test-fs.dir/var/rvm/file1",4484); 00195 00196 TRY_COMMAND(f.path("./test-fs.dir")); 00197 assert(!thrown); 00198 #ifdef S_ISDIR 00199 assert(f.is_directory()); 00200 #endif 00201 00202 TRY_COMMAND(f.path("./test-fs.dir/file1")); 00203 assert(!thrown); 00204 #ifdef S_ISREG 00205 assert(f.is_regular_file()); 00206 #endif 00207 assert(f.size() == 1024); 00208 00209 TRY_COMMAND(f.path("./test-fs.dir/var/rvm/file1")); 00210 assert(!thrown); 00211 #ifdef S_ISREG 00212 assert(f.is_regular_file()); 00213 #endif 00214 assert(f.size() == 4484); 00215 } 00216 00217 void test_mk_symlink(void) 00218 { 00219 bool thrown; 00220 filestatus f; 00221 00222 assert(exists("./test-fs.dir/file2")); 00223 TRY_COMMAND(mk_symlink("file2", "./test-fs.dir/link-file2")); 00224 assert(!thrown); 00225 assert(exists("./test-fs.dir/link-file2")); 00226 TRY_COMMAND(f.path("./test-fs.dir/link-file2")); 00227 assert(!thrown); 00228 #ifdef S_ISLNK 00229 assert(f.is_link()); 00230 assert(f.link() == "file2"); 00231 #endif 00232 } 00233 00234 void test_mk_relative_symlink(void) 00235 { 00236 bool thrown; 00237 filestatus f; 00238 00239 assert(exists("./test-fs.dir/etc/rvm")); 00240 assert(exists("./test-fs.dir/file1")); 00241 assert(exists("./test-fs.dir/var/rvm")); 00242 TRY_COMMAND( 00243 mk_relative_symlink("./test-fs.dir/file1","./test-fs.dir/link-file1")); 00244 assert(!thrown); 00245 assert(exists("./test-fs.dir/link-file1")); 00246 TRY_COMMAND(f.path("./test-fs.dir/link-file1")); 00247 assert(!thrown); 00248 #ifdef S_ISLNK 00249 assert(f.is_link()); 00250 assert(f.link() == "file1"); 00251 #endif 00252 TRY_COMMAND( 00253 mk_relative_symlink("./test-fs.dir/file1", 00254 "./test-fs.dir/etc/rvm/link-file1")); 00255 assert(!thrown); 00256 assert(exists("./test-fs.dir/etc/rvm/link-file1")); 00257 TRY_COMMAND(f.path("./test-fs.dir/etc/rvm/link-file1")); 00258 assert(!thrown); 00259 #ifdef S_ISLNK 00260 assert(f.is_link()); 00261 assert(f.link() == "../../file1"); 00262 #endif 00263 TRY_COMMAND( 00264 mk_relative_symlink("./test-fs.dir/var/rvm/file1", 00265 "./test-fs.dir/etc/rvm/link-rvm-file1")); 00266 assert(!thrown); 00267 assert(exists("./test-fs.dir/etc/rvm/link-rvm-file1")); 00268 TRY_COMMAND(f.path("./test-fs.dir/etc/rvm/link-rvm-file1")); 00269 #ifdef S_INLNK 00270 assert(f.is_link()); 00271 assert(f.link() == "../../var/rvm/file1"); 00272 #endif 00273 } 00274 00275 const subdirectory test_subdirectory_sub(void) 00276 { 00277 subdirectory subdir; 00278 00279 subdir.path("./test-fs.dir/etc/rvm","l*1"); 00280 return(subdir); 00281 } 00282 00283 void test_subdirectory(void) 00284 { 00285 bool thrown; 00286 subdirectory subdir; 00287 subdirectory subdir2; 00288 subdirectory subdir3; 00289 00290 TRY_COMMAND(subdir.path("./test-fs.dir")); 00291 assert(!thrown); 00292 assert(subdir.size() == 6); 00293 assert(subdir[0] == "etc"); 00294 assert(subdir[1] == "file1"); 00295 assert(subdir[2] == "file2"); 00296 assert(subdir[3] == "link-file1"); 00297 assert(subdir[4] == "link-file2"); 00298 assert(subdir[5] == "var"); 00299 00300 TRY_COMMAND(subdir.path("./test-fs.dir","l*")); 00301 assert(!thrown); 00302 assert(subdir.size() == 2); 00303 assert(subdir[0] == "link-file1"); 00304 assert(subdir[1] == "link-file2"); 00305 00306 TRY_COMMAND(subdir.path("./test-fs.dir/etc/rvm","l*1")); 00307 assert(!thrown); 00308 assert(subdir.size() == 2); 00309 assert(subdir[0] == "link-file1"); 00310 assert(subdir[1] == "link-rvm-file1"); 00311 00312 TRY_COMMAND(subdir2 = subdir); 00313 assert(!thrown); 00314 assert(subdir2.size() == 2); 00315 assert(subdir2[0] == "link-file1"); 00316 assert(subdir2[1] == "link-rvm-file1"); 00317 00318 TRY_COMMAND(subdir3 = test_subdirectory_sub()); 00319 assert(!thrown); 00320 assert(subdir3.size() == 2); 00321 assert(subdir3[0] == "link-file1"); 00322 assert(subdir3[1] == "link-rvm-file1"); 00323 } 00324 00325 void test_directory(void) 00326 { 00327 bool thrown; 00328 directory dir; 00329 00330 TRY_COMMAND(dir.path("./test-fs.dir")); 00331 assert(!thrown); 00332 assert(dir.size() == 1); 00333 assert(dir[0] == "./test-fs.dir"); 00334 00335 TRY_COMMAND(dir.path("./test-fs.dir/*")); 00336 assert(!thrown); 00337 assert(dir.size() == 6); 00338 assert(dir[0] == "./test-fs.dir/etc"); 00339 assert(dir[1] == "./test-fs.dir/file1"); 00340 assert(dir[2] == "./test-fs.dir/file2"); 00341 assert(dir[3] == "./test-fs.dir/link-file1"); 00342 assert(dir[4] == "./test-fs.dir/link-file2"); 00343 assert(dir[5] == "./test-fs.dir/var"); 00344 00345 TRY_COMMAND(dir.path("./test-fs.dir/etc/rvm/l*1")); 00346 assert(!thrown); 00347 assert(dir.size() == 2); 00348 assert(dir[0] == "./test-fs.dir/etc/rvm/link-file1"); 00349 assert(dir[1] == "./test-fs.dir/etc/rvm/link-rvm-file1"); 00350 00351 TRY_COMMAND(dir.path("./test-fs.dir/*/*/*")); 00352 assert(!thrown); 00353 assert(dir.size() == 4); 00354 assert(dir[0] == "./test-fs.dir/etc/rvm/file1"); 00355 assert(dir[1] == "./test-fs.dir/etc/rvm/link-file1"); 00356 assert(dir[2] == "./test-fs.dir/etc/rvm/link-rvm-file1"); 00357 assert(dir[3] == "./test-fs.dir/var/rvm/file1"); 00358 } 00359 00360 void test_rm_recursive(void) 00361 { 00362 bool thrown; 00363 00364 TRY_COMMAND(rm_recursive("./test-fs.dir")); 00365 assert(!thrown); 00366 assert(!exists("./test-fs.dir")); 00367 } 00368 00369 void test_mk_relative_path(void) 00370 { 00371 assert( 00372 mk_relative_path( 00373 "./test-fs.dir/dir1a/dir2a/dir3a/file.conf", 00374 "./test-fs.dir/dir1a/dir2a/dir3a" 00375 ) 00376 == "file.conf" 00377 ); 00378 assert( 00379 mk_relative_path( 00380 "./test-fs.dir/dir1a/dir2a/dir3a/file.conf", 00381 "./test-fs.dir/dir1a/dir2a/dir3b" 00382 ) 00383 == "../dir3a/file.conf" 00384 ); 00385 assert( 00386 mk_relative_path( 00387 "./test-fs.dir/dir1a/dir2a/dir3a/file.conf", 00388 "./test-fs.dir/dir1a/dir2b/dir3b" 00389 ) 00390 == "../../dir2a/dir3a/file.conf" 00391 ); 00392 assert( 00393 mk_relative_path( 00394 "./test-fs.dir/dir1a/dir2a/dir3a/file.conf", 00395 "./test-fs.dir/dir1b/dir2b/dir3b" 00396 ) 00397 == "../../../dir1a/dir2a/dir3a/file.conf" 00398 ); 00399 assert( 00400 mk_relative_path( 00401 "./test-fs.dir/dir1a/dir2a/dir3a/file.conf", 00402 "./test-fs.dir.b/dir1b/dir2b/dir3b" 00403 ) 00404 == "../../../../test-fs.dir/dir1a/dir2a/dir3a/file.conf" 00405 ); 00406 } 00407 00408 void test_filesystem(void) 00409 { 00410 filesystem fsys; 00411 00412 fsys.path(cwd()); 00413 } 00414 00415 int main(int argc, char *argv[]) 00416 { 00417 cleanup(); 00418 try { 00419 test_cwd(); 00420 test_reform_path(); 00421 test_permute_path(); 00422 test_exists(); 00423 test_readable(); 00424 test_writable(); 00425 test_executable(); 00426 test_mk_dir(); 00427 test_rm_dir(); 00428 test_rm_file(); 00429 test_mk_dirhier(); 00430 test_filestatus(); 00431 test_mk_symlink(); 00432 test_mk_relative_symlink(); 00433 test_subdirectory(); 00434 test_directory(); 00435 test_rm_recursive(); 00436 test_mk_relative_path(); 00437 test_filesystem(); 00438 } 00439 catch(error e) { 00440 std::cerr << e; 00441 assert(0); 00442 } 00443 catch(...) { 00444 std::cerr << err_unknown; 00445 assert(0); 00446 } 00447 cleanup(); 00448 return(0); 00449 }