f00e0f50d6a1536212b68e2659cfe9929d896e47
[libi2ncommon] / test / test_filefunc.cpp
1 /** @file
2  *
3  * tests for the modules "filefunc", "daemonfunc"
4  *
5  * (c) Copyright 2007-2008 by Intra2net AG
6  *
7  * info@intra2net.com
8  */
9
10 //#define NOISEDEBUG
11
12 #include <string>
13 #include <vector>
14 #include <list>
15 #include <set>
16 #include <iostream>
17 #include <iomanip>
18 #include <fstream>
19 #include <sstream>
20 #include <algorithm>
21
22 #define BOOST_TEST_DYN_LINK
23 #include <boost/test/unit_test.hpp>
24
25 #include <filefunc.hxx>
26 #include <daemonfunc.hpp>
27
28 #ifdef NOISEDEBUG
29 #define DOUT(msg) std::cout << msg << std::endl
30 #else
31 #define DOUT(msg) do {} while (0)
32 #endif
33
34 using namespace std;
35 using namespace I2n;
36
37 class TestFileFuncFixture
38 {
39 protected:
40    typedef std::list< std::string > StringList;
41    std::set<std::string>  used_check_files;
42
43    std::string get_check_file_path(std::string tag)
44    {
45       std::string result;
46       result= "__unittest__" + tag + ".dat";
47       used_check_files.insert(result);
48       return result;
49    } // eo get_check_file_path
50
51    void remove_check_files()
52    {
53       for (std::set<std::string>::iterator it= used_check_files.begin();
54             it != used_check_files.end();
55             ++it)
56       {
57          std::string filepath(*it);
58          if (path_exists(filepath))
59          {
60             unlink(filepath);
61          }
62          //TODO
63       }
64       used_check_files.clear();
65    } // eo remove_check_files
66
67 public:
68     TestFileFuncFixture()
69     {
70     }
71
72     ~TestFileFuncFixture()
73     {
74         remove_check_files();
75     }
76 };
77
78 BOOST_FIXTURE_TEST_SUITE(TestFileFunc, TestFileFuncFixture)
79
80 BOOST_AUTO_TEST_CASE(StatTest1)
81 {
82     I2n::Stat stat("test_filefunc.cpp");
83
84     BOOST_CHECK_EQUAL( true, (bool)stat );
85     BOOST_CHECK_EQUAL( true, stat.is_regular_file() );
86     BOOST_CHECK_EQUAL( false, stat.is_directory() );
87
88     stat= Stat("/dev/null");
89
90     BOOST_CHECK_EQUAL( true, (bool)stat );
91     BOOST_CHECK_EQUAL( false, stat.is_regular_file() );
92     BOOST_CHECK_EQUAL( false, stat.is_directory() );
93     BOOST_CHECK_EQUAL( true, stat.is_device() );
94     BOOST_CHECK_EQUAL( true, stat.is_character_device() );
95     BOOST_CHECK_EQUAL( false, stat.is_block_device() );
96 } // eo StatTest1
97
98 BOOST_AUTO_TEST_CASE(StatRecheck)
99 {
100     // just to be sure
101     unlink(".foobar");
102
103     I2n::Stat stat(".foobar");
104     BOOST_CHECK_EQUAL( false, (bool)stat );
105
106     write_file(".foobar","hello world");
107
108     stat.recheck();
109
110     BOOST_CHECK_EQUAL( true, (bool)stat );
111     BOOST_CHECK_EQUAL( true, stat.size() > 0 );
112
113     unlink(".foobar");
114 }
115
116 BOOST_AUTO_TEST_CASE(DirTest1)
117 {
118     typedef std::vector< std::string > StringVector;
119     StringVector names;
120
121     bool res= I2n::get_dir(".",names);
122
123     BOOST_CHECK_EQUAL( true, res );
124     BOOST_CHECK( ! names.empty() );
125
126     StringVector::iterator it = std::find( names.begin(), names.end(), "test_filefunc.cpp");
127     BOOST_CHECK( it != names.end() );
128
129     it = std::find( names.begin(), names.end(), "." );
130     BOOST_CHECK( it == names.end() );
131
132     names= get_dir(".",true);
133     BOOST_CHECK( ! names.empty() );
134
135     for (it= names.begin(); it!=names.end(); ++it)
136     {
137         DOUT("  \"" << *it << "\"");
138         BOOST_CHECK_EQUAL( false, it->empty() );
139     }
140
141     it = std::find( names.begin(), names.end(), "." );
142     BOOST_CHECK( it != names.end() );
143 } // eo DirTest1
144
145
146
147 BOOST_AUTO_TEST_CASE(PathCuts1)
148 {
149     std::string path1("/an/absolute/path");
150
151     BOOST_CHECK_EQUAL( std::string("/an/absolute"), dirname(path1) );
152     BOOST_CHECK_EQUAL( std::string("path"), basename(path1) );
153
154     std::string path2("just.a.name");
155     BOOST_CHECK_EQUAL( std::string("just.a.name"), basename(path2) );
156     BOOST_CHECK_EQUAL( std::string("."), dirname(path2) );
157 } // eo PathCuts1()
158
159
160
161 BOOST_AUTO_TEST_CASE(NormalizePath1)
162 {
163     std::string path;
164
165     path= normalize_path("/a/simple/path/");
166     BOOST_CHECK_EQUAL( std::string("/a/simple/path"), path );
167
168     path= normalize_path("//another///simple/.//path//");
169     BOOST_CHECK_EQUAL( std::string("/another/simple/path"), path );
170
171     path= normalize_path("//..//..//a/dummy///../././simple/././/path//");
172     BOOST_CHECK_EQUAL( std::string("/a/simple/path"), path );
173
174     path= normalize_path("../a/dummy//././..//simple//nice//absolute//.././..//relative/path//");
175     BOOST_CHECK_EQUAL( std::string("../a/simple/relative/path"), path );
176
177     path= normalize_path("../../a/dummy//././..//simple/../nice//absolute//../.x/..//relative/path//");
178     BOOST_CHECK_EQUAL( std::string("../../a/nice/relative/path"), path );
179
180 } // eo NormalizePath1
181
182 BOOST_AUTO_TEST_CASE(NormalizePath2)
183 {
184     std::string path;
185
186     path= normalize_path("/");
187     BOOST_CHECK_EQUAL( std::string("/"), path );
188
189     path= normalize_path("//");
190     BOOST_CHECK_EQUAL( std::string("/"), path );
191
192     path= normalize_path("/.//");
193     BOOST_CHECK_EQUAL( std::string("/"), path );
194
195     path= normalize_path(".");
196     BOOST_CHECK_EQUAL( std::string(""), path );
197
198     path= normalize_path("./");
199     BOOST_CHECK_EQUAL( std::string(""), path );
200
201     path= normalize_path(".///");
202     BOOST_CHECK_EQUAL( std::string(""), path );
203
204     path= normalize_path("/./data/files");
205     BOOST_CHECK_EQUAL( std::string("/data/files"), path );
206
207     path= normalize_path("./data/files/");
208     BOOST_CHECK_EQUAL( std::string("data/files"), path );
209 } // eo NormalizePath2
210
211 BOOST_AUTO_TEST_CASE(TestUserAndGroupStuff1)
212 {
213     User user_root((uid_t)0);
214     BOOST_CHECK_EQUAL( true, user_root.is_valid() );
215     BOOST_CHECK_EQUAL( true, (bool)user_root );
216
217     BOOST_CHECK_EQUAL( std::string("root"), user_root.Name );
218     BOOST_CHECK_EQUAL( (uid_t)0, user_root.Uid );
219     BOOST_CHECK_EQUAL( (gid_t)0, user_root.Gid );
220
221     User user_root2("root");
222     BOOST_CHECK_EQUAL( true, user_root2.is_valid() );
223     BOOST_CHECK_EQUAL( true, (bool)user_root2 );
224
225     BOOST_CHECK_EQUAL( std::string("root"), user_root2.Name );
226     BOOST_CHECK_EQUAL( (uid_t)0, user_root2.Uid );
227     BOOST_CHECK_EQUAL( (gid_t)0, user_root2.Gid );
228
229     Group group_root("root");
230     BOOST_CHECK_EQUAL( true, group_root.is_valid() );
231     BOOST_CHECK_EQUAL( true, (bool)group_root );
232
233     BOOST_CHECK_EQUAL( std::string("root"), group_root.Name );
234     BOOST_CHECK_EQUAL( (gid_t)0, group_root.Gid );
235
236 } // TestUserAndGroupStuff1()
237
238 BOOST_AUTO_TEST_CASE(TestUserAndGroupStuff2)
239 {
240     // Test if is_valid() works correctly
241     User user_root;
242     BOOST_CHECK_EQUAL( false, user_root.is_valid() );
243
244     Group group_root;
245     BOOST_CHECK_EQUAL( false, group_root.is_valid() );
246 }
247
248 BOOST_AUTO_TEST_CASE(TestFileModes1)
249 {
250     std::string path= get_check_file_path("FileModes1");
251
252     write_file(path,"42");
253
254     Stat stat(path, false);
255     BOOST_CHECK_EQUAL( true, stat.is_valid() );
256
257     User user( stat.uid() );
258     Group group( stat.gid() );
259
260     BOOST_CHECK_EQUAL( true, user.is_valid() );
261     BOOST_CHECK_EQUAL( true, group.is_valid() );
262
263     bool res=chown( path, user.Name.c_str(), group.Gid );
264
265     BOOST_CHECK_EQUAL( true, res );
266 } // eo TestFileModes1()
267
268
269
270 BOOST_AUTO_TEST_CASE(TestPidOf1)
271 {
272     using I2n::Daemon::pid_of;
273
274     std::vector< pid_t > pid_list;
275
276     bool res= pid_of("init", pid_list);
277
278     BOOST_CHECK_EQUAL( true, res);
279     BOOST_CHECK_EQUAL( false, pid_list.empty() );
280
281     std::vector< pid_t >::const_iterator pos1 =
282         std::find( pid_list.begin(), pid_list.end(), 1);
283
284     BOOST_CHECK( pos1 != pid_list.end() );
285 } // eo TestPidOf1()
286
287 BOOST_AUTO_TEST_CASE(TestCopyFileSourceFail)
288 {
289     bool res = copy_file("does not exist", "destination");
290     BOOST_CHECK_EQUAL( false, res );
291 }
292
293 BOOST_AUTO_TEST_CASE(TestCopyFileDestFail)
294 {
295     bool res = copy_file("/etc/HOSTNAME", "/proc/not/writable");
296     BOOST_CHECK_EQUAL( false, res );
297 }
298
299 BOOST_AUTO_TEST_CASE(TestCopyFileOk)
300 {
301     string input = "copy_source";
302     string data = "test";
303
304     long input_size = 0;
305     ofstream finput(input.c_str());
306     if (finput)
307     {
308         finput << data;
309         finput.close();
310         input_size = file_size(input);
311     }
312
313     string output = "copy_dest";
314     long output_size = 0;
315     bool res = copy_file(input, output);
316     if (res)
317     {
318         output_size = file_size(output);
319     }
320     unlink(input);
321     unlink(output);
322
323     bool size_is_zero = output_size == 0 ? true : false;
324
325     BOOST_CHECK_EQUAL( true, res );
326     BOOST_CHECK_EQUAL( input_size, output_size );
327     BOOST_CHECK_EQUAL( false, size_is_zero );
328 }
329
330 BOOST_AUTO_TEST_SUITE_END()