/usr/include/wibble/sys/fs.test.h is in libwibble-dev 1.1-1build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 | /* -*- C++ -*- (c) 2007--2011 Petr Rockai <me@mornfall.net>
(c) 2007--2013 Enrico Zini <enrico@enricozini.org> */
#include "wibble/sys/fs.h"
#include <wibble/exception.h>
#include <cstdlib>
#include <set>
#include <cstdlib>
#include <unistd.h>
#include <wibble/test.h>
using namespace std;
using namespace wibble::sys::fs;
struct TestFs {
// Test directory iteration
Test directoryIterate() {
#ifdef POSIX
Directory dir("/");
set<string> files;
for (Directory::const_iterator i = dir.begin(); i != dir.end(); ++i)
files.insert(*i);
assert(files.size() > 0);
assert(files.find(".") != files.end());
assert(files.find("..") != files.end());
assert(files.find("etc") != files.end());
assert(files.find("bin") != files.end());
assert(files.find("tmp") != files.end());
files.clear();
for (Directory::const_iterator i = dir.begin(); i != dir.end(); ++i)
files.insert(*i);
assert(files.size() > 0);
assert(files.find(".") != files.end());
assert(files.find("..") != files.end());
assert(files.find("etc") != files.end());
assert(files.find("bin") != files.end());
assert(files.find("tmp") != files.end());
#endif
}
Test directoryIsdir()
{
{
Directory dir("/");
for (Directory::const_iterator i = dir.begin(); i != dir.end(); ++i)
if (*i == "etc")
{
assert(i.isdir());
assert(!i.isreg());
}
}
{
Directory dir("/etc");
for (Directory::const_iterator i = dir.begin(); i != dir.end(); ++i)
if (*i == "passwd")
{
assert(i.isreg());
assert(!i.isdir());
}
}
{
Directory dir("/dev");
for (Directory::const_iterator i = dir.begin(); i != dir.end(); ++i)
{
if (*i == "null")
{
assert(i.ischr());
assert(!i.isblk());
}
else if (*i == "sda")
{
assert(i.isblk());
assert(!i.ischr());
}
}
}
}
// Ensure that nonexisting directories and files are reported as not valid
Test invalidDirectories() {
#ifdef POSIX
Directory dir1("/antaniblindalasupercazzola123456");
assert(!dir1.exists());
try {
Directory::const_iterator i = dir1.begin();
assert(false);
} catch (wibble::exception::System& e) {
}
Directory dir2("/etc/passwd");
assert(!dir2.exists());
try {
Directory::const_iterator i = dir2.begin();
assert(false);
} catch (wibble::exception::System& e) {
}
#endif
}
Test _mkPath() {
#ifdef POSIX
// Mkpath should succeed on existing directory
mkpath(".");
// Mkpath should succeed on existing directory
mkpath("./.");
// Mkpath should succeed on existing directory
mkpath("/");
#endif
}
Test _mkPath2() {
#ifdef POSIX
// Try creating a path with mkpath
system("rm -rf test-mkpath");
mkpath("test-mkpath/test-mkpath");
assert(wibble::sys::fs::access("test-mkpath", F_OK));
assert(wibble::sys::fs::access("test-mkpath/test-mkpath", F_OK));
system("rm -rf test-mkpath");
#endif
}
Test _mkFilePath() {
#ifdef POSIX
// Try creating a path with mkFilePath
system("rm -rf test-mkpath");
mkFilePath("test-mkpath/test-mkpath/file");
assert(wibble::sys::fs::access("test-mkpath", F_OK));
assert(wibble::sys::fs::access("test-mkpath/test-mkpath", F_OK));
assert(!wibble::sys::fs::access("test-mkpath/test-mkpath/file", F_OK));
system("rm -rf test-mkpath");
#endif
}
Test _mkdirIfMissing() {
// Creating works and is idempotent
{
system("rm -rf test-mkpath");
assert(!wibble::sys::fs::access("test-mkpath", F_OK));
wibble::sys::fs::mkdirIfMissing("test-mkpath");
assert(wibble::sys::fs::access("test-mkpath", F_OK));
wibble::sys::fs::mkdirIfMissing("test-mkpath");
}
// Creating fails if it exists and it is a file
{
system("rm -rf test-mkpath; touch test-mkpath");
try {
wibble::sys::fs::mkdirIfMissing("test-mkpath");
assert(false);
} catch (wibble::exception::Consistency& e) {
assert(string(e.what()).find("exists but it is not a directory") != string::npos);
}
}
// Deal with dangling symlinks
{
system("rm -rf test-mkpath; ln -s ./tmp/tmp/tmp/DOESNOTEXISTS test-mkpath");
try {
wibble::sys::fs::mkdirIfMissing("test-mkpath");
assert(false);
} catch (wibble::exception::Consistency& e) {
assert(string(e.what()).find("looks like a dangling symlink") != string::npos);
}
}
}
Test _deleteIfExists() {
#ifdef POSIX
system("rm -f does-not-exist");
assert(!deleteIfExists("does-not-exist"));
system("touch does-exist");
assert(deleteIfExists("does-exist"));
#endif
}
Test _isdir() {
#ifdef POSIX
system("rm -rf testdir");
assert(!isdir("testdir"));
system("touch testdir");
assert(!isdir("testdir"));
system("rm testdir; mkdir testdir");
assert(isdir("testdir"));
#endif
}
Test writeFileAtomically() {
using namespace wibble::sys;
string test("ciao");
fs::writeFileAtomically("testfile", test);
string test1 = readFile("testfile");
assert_eq(test1, test);
}
Test timestamp() {
#ifdef POSIX
using namespace wibble::sys;
system("rm -f testfile");
assert_eq(fs::timestamp("testfile", 0), 0);
writeFile("testfile", "");
assert(fs::timestamp("testfile") != 0);
assert(fs::timestamp("testfile", 0) != 0);
unlink("testfile");
assert_eq(fs::timestamp("testfile", 0), 0);
#endif
}
};
// vim:set ts=4 sw=4:
|