--- /dev/null
+/** @file
+ *
+ * tests for the modules "filefunc", "daemonfunc"
+ *
+ * (c) Copyright 2007-2008 by Intra2net AG
+ *
+ * info@intra2net.com
+ */
+
+//#define NOISEDEBUG
+
+#include <string>
+#include <vector>
+#include <list>
+#include <iostream>
+#include <iomanip>
+#include <sstream>
+#include <algorithm>
+
+#include <cppunit/extensions/TestFactoryRegistry.h>
+#include <cppunit/ui/text/TestRunner.h>
+#include <cppunit/extensions/HelperMacros.h>
+
+#include <filefunc.hxx>
+#include <daemonfunc.hxx>
+
+#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 <anonymous>
+
+
+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<std::string> 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<std::string>::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);