2 The software in this package is distributed under the GNU General
3 Public License version 2 (with a special exception described below).
5 A copy of GNU General Public License (GPL) is included in this distribution,
6 in the file COPYING.GPL.
8 As a special exception, if other files instantiate templates or use macros
9 or inline functions from this file, or you compile this file and link it
10 with other works to produce a work based on this file, this file
11 does not by itself cause the resulting work to be covered
12 by the GNU General Public License.
14 However the source code for this file must still be made available
15 in accordance with section (3) of the GNU General Public License.
17 This exception does not invalidate any other reasons why a work based
18 on this file might be covered by the GNU General Public License.
22 * tests for the modules "filefunc", "daemonfunc"
24 * (c) Copyright 2007-2008 by Intra2net AG
39 #define BOOST_TEST_DYN_LINK
40 #include <boost/test/unit_test.hpp>
42 #include <filefunc.hxx>
43 #include <daemonfunc.hpp>
46 #define DOUT(msg) std::cout << msg << std::endl
48 #define DOUT(msg) do {} while (0)
54 class TestFileFuncFixture
57 typedef std::list< std::string > StringList;
58 std::set<std::string> used_check_files;
60 std::string get_check_file_path(std::string tag)
63 result= "__unittest__" + tag + ".dat";
64 used_check_files.insert(result);
66 } // eo get_check_file_path
68 void remove_check_files()
70 for (std::set<std::string>::iterator it= used_check_files.begin();
71 it != used_check_files.end();
74 std::string filepath(*it);
75 if (path_exists(filepath))
81 used_check_files.clear();
82 } // eo remove_check_files
89 ~TestFileFuncFixture()
95 BOOST_FIXTURE_TEST_SUITE(TestFileFunc, TestFileFuncFixture)
97 BOOST_AUTO_TEST_CASE(StatTest1)
99 I2n::Stat stat("Makefile");
101 BOOST_CHECK_EQUAL( true, (bool)stat );
102 BOOST_CHECK_EQUAL( true, stat.is_regular_file() );
103 BOOST_CHECK_EQUAL( false, stat.is_directory() );
105 stat= Stat("/dev/null");
107 BOOST_CHECK_EQUAL( true, (bool)stat );
108 BOOST_CHECK_EQUAL( false, stat.is_regular_file() );
109 BOOST_CHECK_EQUAL( false, stat.is_directory() );
110 BOOST_CHECK_EQUAL( true, stat.is_device() );
111 BOOST_CHECK_EQUAL( true, stat.is_character_device() );
112 BOOST_CHECK_EQUAL( false, stat.is_block_device() );
115 BOOST_AUTO_TEST_CASE(StatRecheck)
120 I2n::Stat stat(".foobar");
121 BOOST_CHECK_EQUAL( false, (bool)stat );
123 write_file(".foobar","hello world");
127 BOOST_CHECK_EQUAL( true, (bool)stat );
128 BOOST_CHECK_EQUAL( true, stat.size() > 0 );
133 BOOST_AUTO_TEST_CASE(DirTest1)
135 typedef std::vector< std::string > StringVector;
138 bool res= I2n::get_dir(".",names);
140 BOOST_CHECK_EQUAL( true, res );
141 BOOST_CHECK( ! names.empty() );
143 StringVector::iterator it = std::find( names.begin(), names.end(), "Makefile");
144 BOOST_CHECK( it != names.end() );
146 it = std::find( names.begin(), names.end(), "." );
147 BOOST_CHECK( it == names.end() );
149 names= get_dir(".",true);
150 BOOST_CHECK( ! names.empty() );
152 for (it= names.begin(); it!=names.end(); ++it)
154 DOUT(" \"" << *it << "\"");
155 BOOST_CHECK_EQUAL( false, it->empty() );
158 it = std::find( names.begin(), names.end(), "." );
159 BOOST_CHECK( it != names.end() );
164 BOOST_AUTO_TEST_CASE(PathCuts1)
166 std::string path1("/an/absolute/path");
168 BOOST_CHECK_EQUAL( std::string("/an/absolute"), dirname(path1) );
169 BOOST_CHECK_EQUAL( std::string("path"), basename(path1) );
171 std::string path2("just.a.name");
172 BOOST_CHECK_EQUAL( std::string("just.a.name"), basename(path2) );
173 BOOST_CHECK_EQUAL( std::string("."), dirname(path2) );
178 BOOST_AUTO_TEST_CASE(NormalizePath1)
182 path= normalize_path("/a/simple/path/");
183 BOOST_CHECK_EQUAL( std::string("/a/simple/path"), path );
185 path= normalize_path("//another///simple/.//path//");
186 BOOST_CHECK_EQUAL( std::string("/another/simple/path"), path );
188 path= normalize_path("//..//..//a/dummy///../././simple/././/path//");
189 BOOST_CHECK_EQUAL( std::string("/a/simple/path"), path );
191 path= normalize_path("../a/dummy//././..//simple//nice//absolute//.././..//relative/path//");
192 BOOST_CHECK_EQUAL( std::string("../a/simple/relative/path"), path );
194 path= normalize_path("../../a/dummy//././..//simple/../nice//absolute//../.x/..//relative/path//");
195 BOOST_CHECK_EQUAL( std::string("../../a/nice/relative/path"), path );
197 } // eo NormalizePath1
199 BOOST_AUTO_TEST_CASE(NormalizePath2)
203 path= normalize_path("/");
204 BOOST_CHECK_EQUAL( std::string("/"), path );
206 path= normalize_path("//");
207 BOOST_CHECK_EQUAL( std::string("/"), path );
209 path= normalize_path("/.//");
210 BOOST_CHECK_EQUAL( std::string("/"), path );
212 path= normalize_path(".");
213 BOOST_CHECK_EQUAL( std::string(""), path );
215 path= normalize_path("./");
216 BOOST_CHECK_EQUAL( std::string(""), path );
218 path= normalize_path(".///");
219 BOOST_CHECK_EQUAL( std::string(""), path );
221 path= normalize_path("/./data/files");
222 BOOST_CHECK_EQUAL( std::string("/data/files"), path );
224 path= normalize_path("./data/files/");
225 BOOST_CHECK_EQUAL( std::string("data/files"), path );
226 } // eo NormalizePath2
228 BOOST_AUTO_TEST_CASE(TestUserAndGroupStuff1)
230 User user_root((uid_t)0);
231 BOOST_CHECK_EQUAL( true, user_root.is_valid() );
232 BOOST_CHECK_EQUAL( true, (bool)user_root );
234 BOOST_CHECK_EQUAL( std::string("root"), user_root.Name );
235 BOOST_CHECK_EQUAL( (uid_t)0, user_root.Uid );
236 BOOST_CHECK_EQUAL( (gid_t)0, user_root.Gid );
238 User user_root2("root");
239 BOOST_CHECK_EQUAL( true, user_root2.is_valid() );
240 BOOST_CHECK_EQUAL( true, (bool)user_root2 );
242 BOOST_CHECK_EQUAL( std::string("root"), user_root2.Name );
243 BOOST_CHECK_EQUAL( (uid_t)0, user_root2.Uid );
244 BOOST_CHECK_EQUAL( (gid_t)0, user_root2.Gid );
246 Group group_root("root");
247 BOOST_CHECK_EQUAL( true, group_root.is_valid() );
248 BOOST_CHECK_EQUAL( true, (bool)group_root );
250 BOOST_CHECK_EQUAL( std::string("root"), group_root.Name );
251 BOOST_CHECK_EQUAL( (gid_t)0, group_root.Gid );
253 } // TestUserAndGroupStuff1()
255 BOOST_AUTO_TEST_CASE(TestUserAndGroupStuff2)
257 // Test if is_valid() works correctly
259 BOOST_CHECK_EQUAL( false, user_root.is_valid() );
262 BOOST_CHECK_EQUAL( false, group_root.is_valid() );
265 BOOST_AUTO_TEST_CASE(TestFileModes1)
267 std::string path= get_check_file_path("FileModes1");
269 write_file(path,"42");
271 Stat stat(path, false);
272 BOOST_CHECK_EQUAL( true, stat.is_valid() );
274 User user( stat.uid() );
275 Group group( stat.gid() );
277 BOOST_CHECK_EQUAL( true, user.is_valid() );
278 BOOST_CHECK_EQUAL( true, group.is_valid() );
280 bool res=chown( path, user.Name.c_str(), group.Gid );
282 BOOST_CHECK_EQUAL( true, res );
283 } // eo TestFileModes1()
287 BOOST_AUTO_TEST_CASE(TestPidOf1)
289 using I2n::Daemon::pid_of;
291 std::vector< pid_t > pid_list;
293 bool res= pid_of("init", pid_list);
295 BOOST_CHECK_EQUAL( true, res);
296 BOOST_CHECK_EQUAL( false, pid_list.empty() );
298 std::vector< pid_t >::const_iterator pos1 =
299 std::find( pid_list.begin(), pid_list.end(), 1);
301 BOOST_CHECK( pos1 != pid_list.end() );
304 BOOST_AUTO_TEST_CASE(TestCopyFileSourceFail)
306 bool res = copy_file("does not exist", "destination");
307 BOOST_CHECK_EQUAL( false, res );
310 BOOST_AUTO_TEST_CASE(TestCopyFileDestFail)
312 bool res = copy_file("/etc/HOSTNAME", "/proc/not/writable");
313 BOOST_CHECK_EQUAL( false, res );
316 BOOST_AUTO_TEST_CASE(TestCopyFileOk)
318 string input = "copy_source";
319 string data = "test";
322 ofstream finput(input.c_str());
327 input_size = file_size(input);
330 string output = "copy_dest";
331 long output_size = 0;
332 bool res = copy_file(input, output);
335 output_size = file_size(output);
340 bool size_is_zero = output_size == 0 ? true : false;
342 BOOST_CHECK_EQUAL( true, res );
343 BOOST_CHECK_EQUAL( input_size, output_size );
344 BOOST_CHECK_EQUAL( false, size_is_zero );
347 BOOST_AUTO_TEST_CASE(TestMkdtemp)
349 // Create unique directory
350 string unique_dir = I2n::mkdtemp("foobar.XXXXXX");
351 BOOST_REQUIRE(unique_dir.size() > 0);
353 // Test if it's really a directory
354 BOOST_CHECK_EQUAL(true, Stat(unique_dir).is_directory());
357 BOOST_CHECK_EQUAL(true, I2n::recursive_delete(unique_dir));
360 BOOST_AUTO_TEST_CASE(TestMkdtempBrokenTemplate)
362 // Broken directory template -> fail
363 string unique_dir = I2n::mkdtemp("foobar.XXX");
364 BOOST_CHECK_EQUAL("", unique_dir);
367 BOOST_AUTO_TEST_CASE(DirectoryCreation)
369 string dirname = "test_directory";
371 I2n::rmdir (dirname);
373 BOOST_CHECK_EQUAL(true, I2n::mkdir(dirname));
374 BOOST_CHECK_EQUAL(true, Stat(dirname).is_directory());
375 // Verify default permissions
376 BOOST_CHECK_EQUAL(0700, Stat(dirname).mode());
378 BOOST_CHECK_EQUAL(true, I2n::rmdir(dirname));
380 // Second call must fail: Directory already deleted
381 BOOST_CHECK_EQUAL(false, I2n::rmdir(dirname));
384 BOOST_AUTO_TEST_CASE(DirectoryCreatePermission)
386 string dirname = "test_directory";
388 // TODO: Save and fix umask
390 I2n::rmdir (dirname);
391 BOOST_CHECK_EQUAL(true, I2n::mkdir(dirname, 0770));
392 // TODO: Enable check
393 // BOOST_CHECK_EQUAL(0770, Stat(dirname).mode());
394 I2n::rmdir (dirname);
396 // TODO: Restore umask
399 BOOST_AUTO_TEST_CASE(ChangeDirectory)
401 string current_directory = I2n::getcwd();
402 BOOST_REQUIRE(!current_directory.empty());
404 BOOST_CHECK_EQUAL(true, I2n::chdir("/"));
405 BOOST_CHECK_EQUAL("/", I2n::getcwd());
407 BOOST_CHECK_EQUAL(true, I2n::chdir(current_directory));
408 BOOST_REQUIRE_EQUAL(current_directory, I2n::getcwd());
411 BOOST_AUTO_TEST_SUITE_END()