1 #include "FilesystemTest.hpp"
3 #include "io/filesystem.hpp"
7 CPPUNIT_TEST_SUITE_REGISTRATION(blobs::io::test::FilesystemTest);
16 void FilesystemTest::setUp() {
17 test_dir = "test-dir";
18 CPPUNIT_ASSERT_MESSAGE(
19 "failed to create test dir",
23 void FilesystemTest::tearDown() {
24 CPPUNIT_ASSERT_MESSAGE(
25 "failed to remove test dir",
26 remove_dir(test_dir));
30 void FilesystemTest::testFile() {
32 const string test_file = test_dir + "\\test-file.txt";
34 const string test_file = test_dir + "/test-file";
37 CPPUNIT_ASSERT_MESSAGE(
38 "inexistant file is file",
40 CPPUNIT_ASSERT_EQUAL_MESSAGE(
41 "mtime of inexistant file should be zero",
42 time_t(0), file_mtime(test_file));
43 CPPUNIT_ASSERT_MESSAGE(
44 "inexistant file is a directory",
48 ofstream file(test_file);
49 file << "hello" << endl;
51 time_t now = time(nullptr);
52 CPPUNIT_ASSERT_MESSAGE(
53 "existing file not a file",
55 CPPUNIT_ASSERT_MESSAGE(
56 "mtime of existing file should be somewhere around now",
57 // let's assume that creating the file takes less than five seconds
58 abs(now - file_mtime(test_file) < 5));
59 CPPUNIT_ASSERT_MESSAGE(
60 "regular file is a directory",
63 CPPUNIT_ASSERT_MESSAGE(
64 "failed to remove test file",
65 remove_file(test_file));
67 CPPUNIT_ASSERT_MESSAGE(
68 "removed file is still a file",
70 CPPUNIT_ASSERT_EQUAL_MESSAGE(
71 "mtime of removed file should be zero",
72 time_t(0), file_mtime(test_file));
73 CPPUNIT_ASSERT_MESSAGE(
74 "removed file became a directory",
78 void FilesystemTest::testDirectory() {
80 const string test_subdir = test_dir + "\\a";
81 const string test_subsubdir = test_subdir + "\\b";
82 const string test_file = test_subsubdir + "\\c.txt";
84 const string test_subdir = test_dir + "/a";
85 const string test_subsubdir = test_subdir + "/b";
86 const string test_file = test_subsubdir + "/c";
89 CPPUNIT_ASSERT_MESSAGE(
90 "inexistant directory is a file",
91 !is_file(test_subdir));
92 CPPUNIT_ASSERT_MESSAGE(
93 "inexistant directory is a directory",
94 !is_dir(test_subdir));
96 CPPUNIT_ASSERT_MESSAGE(
97 "failed to create test subdir",
98 make_dir(test_subdir));
99 CPPUNIT_ASSERT_MESSAGE(
100 "created directory is a file",
101 !is_file(test_subdir));
102 CPPUNIT_ASSERT_MESSAGE(
103 "created directory is not a directory",
104 is_dir(test_subdir));
106 CPPUNIT_ASSERT_MESSAGE(
107 "failed to remove test subdir",
108 remove_dir(test_subdir));
109 CPPUNIT_ASSERT_MESSAGE(
110 "removed directory became a file",
111 !is_file(test_subdir));
112 CPPUNIT_ASSERT_MESSAGE(
113 "removed directory is still a directory",
114 !is_dir(test_subdir));
116 CPPUNIT_ASSERT_MESSAGE(
117 "failed to create test subdirs",
118 make_dirs(test_subsubdir));
119 CPPUNIT_ASSERT_MESSAGE(
120 "created directory is a file",
121 !is_file(test_subdir));
122 CPPUNIT_ASSERT_MESSAGE(
123 "created directory is not a directory",
124 is_dir(test_subdir));
125 CPPUNIT_ASSERT_MESSAGE(
126 "created directory is a file",
127 !is_file(test_subsubdir));
128 CPPUNIT_ASSERT_MESSAGE(
129 "created directory is not a directory",
130 is_dir(test_subsubdir));
133 ofstream file(test_file);
134 file << "hello" << endl;
136 CPPUNIT_ASSERT_MESSAGE(
137 "failed to create test file",
140 CPPUNIT_ASSERT_MESSAGE(
141 "failed to remove test subdir",
142 remove_dir(test_subdir));
143 CPPUNIT_ASSERT_MESSAGE(
144 "removed directory became a file",
145 !is_file(test_subdir));
146 CPPUNIT_ASSERT_MESSAGE(
147 "removed directory is still a directory",
148 !is_dir(test_subdir));
149 CPPUNIT_ASSERT_MESSAGE(
150 "removed directory became a file",
151 !is_file(test_subsubdir));
152 CPPUNIT_ASSERT_MESSAGE(
153 "removed directory is still a directory",
154 !is_dir(test_subsubdir));
155 CPPUNIT_ASSERT_MESSAGE(
156 "removed file became a directory",
158 CPPUNIT_ASSERT_MESSAGE(
159 "removed file is still a file",
160 !is_file(test_file));