From 847ea2eff6dc6751fce4c137beaa059d5cfc63e9 Mon Sep 17 00:00:00 2001 From: Reinhard Pfau Date: Thu, 4 Sep 2008 14:06:07 +0000 Subject: [PATCH] libsimpleio: (reinhard) copied unit test for global config module from connd. --- test/Makefile.am | 8 +- test/test_global_config.cpp | 252 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 257 insertions(+), 3 deletions(-) create mode 100644 test/test_global_config.cpp diff --git a/test/Makefile.am b/test/Makefile.am index a4793d0..0cc3379 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -1,8 +1,10 @@ -INCLUDES = -I$(top_srcdir)/src @CPPUNIT_CFLAGS@ +INCLUDES = -I$(top_srcdir)/configlib -I$(top_srcdir)/src @CPPUNIT_CFLAGS@ METASOURCES = AUTO check_PROGRAMS = test test_SOURCES = ip_range.cpp stringfunc.cpp test.cpp test_containerfunc.cpp \ - test_filefunc.cpp test_logging.cpp test_pidfile.cpp test_timefunc.cpp -test_LDADD = $(top_builddir)/src/libi2ncommon.la @CPPUNIT_LIBS@ + test_filefunc.cpp test_global_config.cpp test_logging.cpp test_pidfile.cpp \ + test_timefunc.cpp +test_LDADD = $(top_builddir)/src/libi2ncommon.la \ + $(top_builddir)/configlib/libi2ncommon_config.la @CPPUNIT_LIBS@ TESTS = test diff --git a/test/test_global_config.cpp b/test/test_global_config.cpp new file mode 100644 index 0000000..a889f97 --- /dev/null +++ b/test/test_global_config.cpp @@ -0,0 +1,252 @@ +/** @file + * + * (c) Copyright 2008 by Intra2net AG + * + * info@intra2net.com + */ + +#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; + + +class TestGlobalConfig : public TestFixture +{ + CPPUNIT_TEST_SUITE(TestGlobalConfig); + + CPPUNIT_TEST(Basics); + CPPUNIT_TEST(MultipleValues); + + CPPUNIT_TEST_SUITE_END(); + + protected: + + std::set used_check_files; + + + std::string getCheckFilepath(std::string tag) + { + std::string result; + result= "__unittest__" + tag + ".dat"; + used_check_files.insert(result); + return result; + } // eo get_check_file_path + + + void removeCheckFiles() + { + 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); + } + } + used_check_files.clear(); + } // eo removeCheckFiles + + + + public: + + void setUp() + { + //Logger::enable_stderr_log(true); + //Logger::set_log_level( Logger::LogLevel::Debug ); + used_check_files.clear(); + } // eo setUp + + + void tearDown() + { + removeCheckFiles(); + //Logger::enable_stderr_log(false); + //Logger::set_log_level( Logger::LogLevel::Error ); + } // eo tearDown + + + /* + * the tests: + */ + + + + void Basics() + { + std::string filename= getCheckFilepath("Basics"); + std::string cfg( + "a path= /var/run/my.pid\n" + "\n" + "[sec1]\n" + "s1 = ein Text\n" + "i1 = 100\n" + "f1 = 2.5\n" + ); + std::string cfg2( + "a path= /var/run/my.pid\n" + "\n" + "[sec1]\n" + "s1 = ein Text\n" + "i1 = 10\n" + "f1 = 3.5\n" + "i2 = 010\n" + "i3 = 0x10\n" + ); + bool res= write_file(filename,cfg); + CPPUNIT_ASSERT_EQUAL( true, res ); + + Config::Var var_s1("sec1", "s1", "nix"); + + res= Config::set_config_file(filename); + CPPUNIT_ASSERT_EQUAL( true, res ); + + CPPUNIT_ASSERT_EQUAL( std::string("ein Text"), (std::string)var_s1 ); + + Config::Var var_i1("sec1","i1",0); + CPPUNIT_ASSERT_EQUAL( 100, var_i1() ); + + Config::Var var_f1("sec1","f1", 0.0); + CPPUNIT_ASSERT_EQUAL( 2.5, var_f1() ); + + Config::Var pid_path("a path", "/var/run/x.pid"); + CPPUNIT_ASSERT_EQUAL( std::string("/var/run/my.pid"), pid_path() ); + + // try an already used var again as different type: + Config::Var var_i1s("sec1","i1","0"); + CPPUNIT_ASSERT_EQUAL( std::string("100"), var_i1s() ); + + + // make a copy from a given var: + Config::Var< int > var_i1_2 ( var_i1 ); + CPPUNIT_ASSERT_EQUAL( 100, var_i1_2() ); + + // now write a modified config + res= write_file(filename,cfg2); + CPPUNIT_ASSERT_EQUAL( true, res ); + + // .. and reload. + res= Config::reload(); + CPPUNIT_ASSERT_EQUAL( true, res ); + + // check if the (right) vars changed.. + + CPPUNIT_ASSERT_EQUAL( std::string("ein Text"), (std::string)var_s1 ); + CPPUNIT_ASSERT_EQUAL( 10, var_i1() ); + CPPUNIT_ASSERT_EQUAL( std::string("10"), var_i1s() ); + CPPUNIT_ASSERT_EQUAL( 3.5, var_f1() ); + CPPUNIT_ASSERT_EQUAL( 10, var_i1_2() ); + + // check for the new vars + + Config::Var > var_i2("sec1","i2",0); + CPPUNIT_ASSERT_EQUAL( 8, var_i2() ); + + Config::Var > var_i3("sec1","i3",0); + CPPUNIT_ASSERT_EQUAL( 16, var_i3() ); + } // eo Basics + + + + void MultipleValues() + { + std::string filename= getCheckFilepath("MultipleValues"); + std::string cfg( + "[sec1]\n" + "g1 = 11\n" + "g1 = 12\n" + "g2 = 21\n" + "g2 = 22\n" + "g1 = 13\n" + "g1 = 14\n" + ); + std::string cfg2( + "[sec1]\n" + "g1 = 11\n" + "g1 = 12\n" + "g2 = 20\n" + "g2 = 22\n" + "g1 = 13\n" + ); + + bool res= write_file(filename,cfg); + CPPUNIT_ASSERT_EQUAL( true, res ); + + typedef std::list< int > IntList; + typedef std::vector< int > IntVector; + + res= Config::set_config_file(filename); + CPPUNIT_ASSERT_EQUAL( true, res ); + + Config::Var< IntList > list1("sec1","g1", IntList()); + Config::Var< IntList > list2("sec1","g2", IntList()); + + CPPUNIT_ASSERT_EQUAL( 4u, list1().size() ); + CPPUNIT_ASSERT_EQUAL( 2u, list2().size() ); + + CPPUNIT_ASSERT_EQUAL( 11, list1().front() ); + CPPUNIT_ASSERT_EQUAL( 14, list1().back() ); + CPPUNIT_ASSERT_EQUAL( 21, list2().front() ); + CPPUNIT_ASSERT_EQUAL( 22, list2().back() ); + + Config::Var< IntVector > vector1("sec1","g1", IntVector()); + Config::Var< IntVector > vector2("sec1","g2", IntVector()); + + CPPUNIT_ASSERT_EQUAL( 4u, vector1().size() ); + CPPUNIT_ASSERT_EQUAL( 2u, vector2().size() ); + + CPPUNIT_ASSERT_EQUAL( 11, vector1().front() ); + CPPUNIT_ASSERT_EQUAL( 14, vector1().back() ); + CPPUNIT_ASSERT_EQUAL( 21, vector2().front() ); + CPPUNIT_ASSERT_EQUAL( 22, vector2().back() ); + + // now write a modified config + res= write_file(filename,cfg2); + CPPUNIT_ASSERT_EQUAL( true, res ); + + // .. and reload. + res= Config::reload(); + CPPUNIT_ASSERT_EQUAL( true, res ); + + // check if the (right) vars changed.. + + CPPUNIT_ASSERT_EQUAL( 3u, list1().size() ); + CPPUNIT_ASSERT_EQUAL( 2u, list2().size() ); + + CPPUNIT_ASSERT_EQUAL( 11, list1().front() ); + CPPUNIT_ASSERT_EQUAL( 13, list1().back() ); + CPPUNIT_ASSERT_EQUAL( 20, list2().front() ); + CPPUNIT_ASSERT_EQUAL( 22, list2().back() ); + + } // eo MultipleValues + + +}; // eo class TestGlobalConfig + +CPPUNIT_TEST_SUITE_REGISTRATION(TestGlobalConfig); -- 1.7.1