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