00001 /* -*- C++ -*- (c) 2007 Petr Rockai <me@mornfall.net> 00002 (c) 2007 Enrico Zini <enrico@enricozini.org> */ 00003 #include <wibble/sys/fs.h> 00004 #include <cstdlib> 00005 #include <set> 00006 #include <cstdlib> 00007 00008 #include <wibble/test.h> 00009 00010 using namespace std; 00011 using namespace wibble::sys::fs; 00012 00013 struct TestFs { 00014 00015 // Test directory iteration 00016 Test directoryIterate() { 00017 Directory dir("/"); 00018 00019 set<string> files; 00020 for (Directory::const_iterator i = dir.begin(); i != dir.end(); ++i) 00021 files.insert(*i); 00022 00023 assert(files.size() > 0); 00024 assert(files.find(".") != files.end()); 00025 assert(files.find("..") != files.end()); 00026 assert(files.find("etc") != files.end()); 00027 assert(files.find("usr") != files.end()); 00028 assert(files.find("tmp") != files.end()); 00029 } 00030 00031 // Ensure that nonexisting directories and files are reported as not valid 00032 Test invalidDirectories() { 00033 Directory dir1("/antaniblindalasupercazzola123456"); 00034 assert(!dir1.valid()); 00035 00036 Directory dir2("/etc/passwd"); 00037 assert(!dir2.valid()); 00038 } 00039 00040 Test _mkPath() { 00041 // Mkpath should succeed on existing directory 00042 mkpath("."); 00043 00044 // Mkpath should succeed on existing directory 00045 mkpath("./."); 00046 00047 // Mkpath should succeed on existing directory 00048 mkpath("/"); 00049 } 00050 00051 Test _mkPath2() { 00052 // Try creating a path with mkpath 00053 system("rm -rf test-mkpath"); 00054 mkpath("test-mkpath/test-mkpath"); 00055 assert(wibble::sys::fs::access("test-mkpath", F_OK)); 00056 assert(wibble::sys::fs::access("test-mkpath/test-mkpath", F_OK)); 00057 system("rm -rf test-mkpath"); 00058 } 00059 00060 Test _mkFilePath() { 00061 // Try creating a path with mkFilePath 00062 system("rm -rf test-mkpath"); 00063 mkFilePath("test-mkpath/test-mkpath/file"); 00064 assert(wibble::sys::fs::access("test-mkpath", F_OK)); 00065 assert(wibble::sys::fs::access("test-mkpath/test-mkpath", F_OK)); 00066 assert(!wibble::sys::fs::access("test-mkpath/test-mkpath/file", F_OK)); 00067 system("rm -rf test-mkpath"); 00068 } 00069 00070 Test _deleteIfExists() { 00071 system("rm -f does-not-exist"); 00072 assert(!deleteIfExists("does-not-exist")); 00073 system("touch does-exist"); 00074 assert(deleteIfExists("does-exist")); 00075 } 00076 00077 Test _isDirectory() { 00078 system("rm -rf testdir"); 00079 assert(!isDirectory("testdir")); 00080 system("touch testdir"); 00081 assert(!isDirectory("testdir")); 00082 system("rm testdir; mkdir testdir"); 00083 assert(isDirectory("testdir")); 00084 } 00085 }; 00086 00087 // vim:set ts=4 sw=4: