4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Stefan Bund <g0dil@berlios.de>
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 \brief Daemon.test unit tests */
26 //#include "Daemon.test.hh"
27 //#include "Daemon.test.ih"
31 #include <sys/types.h>
35 #include <boost/filesystem/operations.hpp>
37 #include "../Utils/Exception.hh"
38 #include "../Utils/Backtrace.hh"
40 #include "../Utils/auto_unit_test.hh"
41 #include <boost/test/test_tools.hpp>
44 ///////////////////////////////cc.p////////////////////////////////////////
48 void delay(unsigned long milliseconds)
51 ts.tv_sec = milliseconds / 1000;
52 ts.tv_nsec = (milliseconds % 1000) * 1000000;
53 while (nanosleep(&ts,&ts) < 0 && errno == EINTR) ;
56 class MyDaemon : public senf::Daemon
59 std::cout << "Running configure()" << std::endl;
60 pidFile("invalid.pid");
61 consoleLog("invalid.log");
62 senf::Daemon::configure();
66 std::cout << "Running init()" << std::endl;
70 std::cout << "Running run()" << std::endl;
75 int myMain(int argc, char ** argv)
78 return instance.start(argc, argv);
85 senf::backtrace(std::cerr, 100);
86 ::signal(SIGABRT, SIG_DFL);
87 ::kill(::getpid(), SIGABRT);
90 int run(int argc, char ** argv)
93 if (pid < 0) throw senf::SystemException("::fork()");
95 signal(SIGABRT, &backtrace);
97 ::_exit(myMain(argc, argv));
98 } catch (std::exception & ex) {
99 std::cerr << "Unexpected exception: " << ex.what() << std::endl;
101 std::cerr << "Unexpected exception" << std::endl;
106 if (::waitpid(pid, &status, 0) < 0) throw senf::SystemException("::waitpid()");
107 return WIFEXITED(status) ? WEXITSTATUS(status) : -1;
112 BOOST_AUTO_UNIT_TEST(testDaemon)
114 char * args[] = { "run",
115 "--console-log=testDaemon.log,none",
116 "--pid-file=testDaemon.pid" };
117 BOOST_CHECK_EQUAL( run(sizeof(args)/sizeof(*args),args), 0 );
119 BOOST_CHECK( ! boost::filesystem::exists("invalid.log") );
120 BOOST_CHECK( ! boost::filesystem::exists("invalid.pid") );
121 BOOST_REQUIRE( boost::filesystem::exists("testDaemon.pid") );
122 BOOST_REQUIRE( boost::filesystem::exists("testDaemon.log") );
124 boost::filesystem::rename("testDaemon.log", "testDaemon.log.1");
126 std::ifstream pidFile ("testDaemon.pid");
128 BOOST_REQUIRE( pidFile >> pid );
129 BOOST_REQUIRE( pid != 0 );
134 BOOST_CHECK( ! boost::filesystem::exists("testDaemon.pid") );
135 BOOST_CHECK( boost::filesystem::exists("testDaemon.log") );
136 BOOST_REQUIRE( boost::filesystem::exists("testDaemon.log.1") );
138 std::ifstream log ("testDaemon.log.1");
139 std::stringstream data;
141 BOOST_CHECK_EQUAL( data.str(), "Running init()\nRunning run()\n" );
142 BOOST_CHECK_NO_THROW( boost::filesystem::remove("testDaemon.log") );
143 BOOST_CHECK_NO_THROW( boost::filesystem::remove("testDaemon.log.1") );
146 ///////////////////////////////cc.e////////////////////////////////////////
153 // comment-column: 40
154 // c-file-style: "senf"
155 // indent-tabs-mode: nil
156 // ispell-local-dictionary: "american"
157 // compile-command: "scons -u test"