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