From: Reinhard Pfau Date: Mon, 7 Apr 2008 15:41:37 +0000 (+0000) Subject: libi2ncommon: (reinhard) added unit tests for file funcs; updated daemonfunc. X-Git-Tag: v2.6~184 X-Git-Url: http://developer.intra2net.com/git/?a=commitdiff_plain;h=5de0c58a2d261d3e0305a1f887faea362d3d872e;p=libi2ncommon libi2ncommon: (reinhard) added unit tests for file funcs; updated daemonfunc. --- diff --git a/src/daemonfunc.cpp b/src/daemonfunc.cpp index 43a4750..979920e 100644 --- a/src/daemonfunc.cpp +++ b/src/daemonfunc.cpp @@ -14,8 +14,6 @@ #include "stringfunc.hxx" #include "filefunc.hxx" -// TODO: Remove me once libi2ncommon is completly switched to I2n -using namespace i2n; namespace I2n { diff --git a/src/daemonfunc.hxx b/src/daemonfunc.hxx index a0e8011..14deeb1 100644 --- a/src/daemonfunc.hxx +++ b/src/daemonfunc.hxx @@ -11,7 +11,7 @@ #include /// Collection of functions for daemons -namespace i2n +namespace I2n { namespace daemon { diff --git a/test/Makefile.am b/test/Makefile.am index ac4c0b3..da12559 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -1,7 +1,8 @@ INCLUDES = -I$(top_srcdir)/src @CPPUNIT_CFLAGS@ METASOURCES = AUTO check_PROGRAMS = test -test_SOURCES = ip_range.cpp stringfunc.cpp test.cpp test_containerfunc.cpp +test_SOURCES = ip_range.cpp stringfunc.cpp test.cpp test_containerfunc.cpp \ + test_filefunc.cpp test_LDADD = $(top_builddir)/src/libi2ncommon.la @CPPUNIT_LIBS@ TESTS = test diff --git a/test/test_filefunc.cpp b/test/test_filefunc.cpp new file mode 100644 index 0000000..e315df3 --- /dev/null +++ b/test/test_filefunc.cpp @@ -0,0 +1,269 @@ +/** @file + * + * tests for the modules "filefunc", "daemonfunc" + * + * (c) Copyright 2007-2008 by Intra2net AG + * + * info@intra2net.com + */ + +//#define NOISEDEBUG + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include +#include + +#ifdef NOISEDEBUG +#define DOUT(msg) std::cout << msg << std::endl +#else +#define DOUT(msg) do {} while (0) +#endif + + +using namespace I2n; + +using namespace CppUnit; + +namespace { + + +} // eo namespace + + +class TestFileFunc : public TestFixture +{ + CPPUNIT_TEST_SUITE(TestFileFunc); + + CPPUNIT_TEST(StatTest1); + CPPUNIT_TEST(DirTest1); + CPPUNIT_TEST(PathCuts1); + CPPUNIT_TEST(NormalizePath1); + CPPUNIT_TEST(TestUserAndGroupStuff1); + CPPUNIT_TEST(TestFileModes1); + CPPUNIT_TEST(TestPidOf1); + + CPPUNIT_TEST_SUITE_END(); + + protected: + + typedef std::list< std::string > StringList; + + std::set used_check_files; + + std::string get_check_file_path(std::string tag) + { + std::string result; + result= "__unittest__" + tag + ".dat"; + used_check_files.insert(result); + return result; + } // eo get_check_file_path + + + void remove_check_files() + { + for(std::set::iterator it= used_check_files.begin(); + it != used_check_files.end(); + ++it) + { + std::string filepath(*it); + if (path_exists(filepath)) + { + unlink(filepath); + } + //TODO + } + used_check_files.clear(); + } // eo remove_check_files + + + + public: + + void setUp() + { + } // eo setUp + + + void tearDown() + { + remove_check_files(); + } // eo tearDown + + + /* + * the tests: + */ + + + + void StatTest1() + { + I2n::Stat stat("test_filefunc.cpp"); + + CPPUNIT_ASSERT_EQUAL( true, (bool)stat ); + CPPUNIT_ASSERT_EQUAL( true, stat.is_regular_file() ); + CPPUNIT_ASSERT_EQUAL( false, stat.is_directory() ); + + stat= Stat("/dev/null"); + + CPPUNIT_ASSERT_EQUAL( true, (bool)stat ); + CPPUNIT_ASSERT_EQUAL( false, stat.is_regular_file() ); + CPPUNIT_ASSERT_EQUAL( false, stat.is_directory() ); + CPPUNIT_ASSERT_EQUAL( true, stat.is_device() ); + CPPUNIT_ASSERT_EQUAL( true, stat.is_character_device() ); + CPPUNIT_ASSERT_EQUAL( false, stat.is_block_device() ); + } // eo StatTest1 + + + void DirTest1() + { + typedef std::vector< std::string > StringVector; + StringVector names; + + bool res= I2n::get_dir(".",names); + + CPPUNIT_ASSERT_EQUAL( true, res ); + CPPUNIT_ASSERT( ! names.empty() ); + + StringVector::iterator it = std::find( names.begin(), names.end(), "test_filefunc.cpp"); + CPPUNIT_ASSERT( it != names.end() ); + + it = std::find( names.begin(), names.end(), "." ); + CPPUNIT_ASSERT( it == names.end() ); + + names= get_dir(".",true); + CPPUNIT_ASSERT( ! names.empty() ); + + for(it= names.begin(); it!=names.end(); ++it) + { + DOUT(" \"" << *it << "\""); + CPPUNIT_ASSERT_EQUAL( false, it->empty() ); + } + + it = std::find( names.begin(), names.end(), "." ); + CPPUNIT_ASSERT( it != names.end() ); + } // eo DirTest1 + + + + void PathCuts1() + { + std::string path1("/an/absolute/path"); + + CPPUNIT_ASSERT_EQUAL( std::string("/an/absolute"), dirname(path1) ); + CPPUNIT_ASSERT_EQUAL( std::string("path"), basename(path1) ); + + std::string path2("just.a.name"); + CPPUNIT_ASSERT_EQUAL( std::string("just.a.name"), basename(path2) ); + CPPUNIT_ASSERT_EQUAL( std::string("."), dirname(path2) ); + } // eo PathCuts1() + + + + void NormalizePath1() + { + std::string path; + + path= normalize_path("/a/simple/path/"); + CPPUNIT_ASSERT_EQUAL( std::string("/a/simple/path"), path ); + + path= normalize_path("//another///simple/.//path//"); + CPPUNIT_ASSERT_EQUAL( std::string("/another/simple/path"), path ); + + path= normalize_path("//..//..//a/dummy///../././simple/././/path//"); + CPPUNIT_ASSERT_EQUAL( std::string("/a/simple/path"), path ); + + path= normalize_path("../a/dummy//././..//simple//nice//absolute//.././..//relative/path//"); + CPPUNIT_ASSERT_EQUAL( std::string("../a/simple/relative/path"), path ); + + path= normalize_path("../../a/dummy//././..//simple/../nice//absolute//../.x/..//relative/path//"); + CPPUNIT_ASSERT_EQUAL( std::string("../../a/nice/relative/path"), path ); + + } // eo NormalizePath1 + + + + void TestUserAndGroupStuff1() + { + User user_root((uid_t)0); + CPPUNIT_ASSERT_EQUAL( true, user_root.is_valid() ); + CPPUNIT_ASSERT_EQUAL( true, (bool)user_root ); + + CPPUNIT_ASSERT_EQUAL( std::string("root"), user_root.Name ); + CPPUNIT_ASSERT_EQUAL( (uid_t)0, user_root.Uid ); + CPPUNIT_ASSERT_EQUAL( (gid_t)0, user_root.Gid ); + + User user_root2("root"); + CPPUNIT_ASSERT_EQUAL( true, user_root2.is_valid() ); + CPPUNIT_ASSERT_EQUAL( true, (bool)user_root2 ); + + CPPUNIT_ASSERT_EQUAL( std::string("root"), user_root2.Name ); + CPPUNIT_ASSERT_EQUAL( (uid_t)0, user_root2.Uid ); + CPPUNIT_ASSERT_EQUAL( (gid_t)0, user_root2.Gid ); + + Group group_root("root"); + CPPUNIT_ASSERT_EQUAL( true, group_root.is_valid() ); + CPPUNIT_ASSERT_EQUAL( true, (bool)group_root ); + + CPPUNIT_ASSERT_EQUAL( std::string("root"), group_root.Name ); + CPPUNIT_ASSERT_EQUAL( (gid_t)0, group_root.Gid ); + + } // TestUserAndGroupStuff1() + + + + void TestFileModes1() + { + std::string path= get_check_file_path("FileModes1"); + + write_file(path,"42"); + + Stat stat(path, false); + CPPUNIT_ASSERT_EQUAL( true, stat.is_valid() ); + + User user( stat.uid() ); + Group group( stat.gid() ); + + CPPUNIT_ASSERT_EQUAL( true, user.is_valid() ); + CPPUNIT_ASSERT_EQUAL( true, group.is_valid() ); + + bool res=chown( path, user.Name.c_str(), group.Gid ); + + CPPUNIT_ASSERT_EQUAL( true, res ); + } // eo TestFileModes1() + + + + void TestPidOf1() + { + using I2n::daemon::pid_of; + + std::vector< pid_t > pid_list; + + bool res= pid_of("init", pid_list); + + CPPUNIT_ASSERT_EQUAL( true, res); + CPPUNIT_ASSERT_EQUAL( false, pid_list.empty() ); + + std::vector< pid_t >::const_iterator pos1 = + std::find( pid_list.begin(), pid_list.end(), 1); + + CPPUNIT_ASSERT( pos1 != pid_list.end() ); + } // eo TestPidOf1() + + + +}; // eo class TestFileFunc + +CPPUNIT_TEST_SUITE_REGISTRATION(TestFileFunc);