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"
39 #include "../Utils/auto_unit_test.hh"
40 #include <boost/test/test_tools.hpp>
43 ///////////////////////////////cc.p////////////////////////////////////////
47 void delay(unsigned long milliseconds)
50 ts.tv_sec = milliseconds / 1000;
51 ts.tv_nsec = (milliseconds % 1000) * 1000000;
52 while (nanosleep(&ts,&ts) < 0 && errno == EINTR) ;
55 class MyDaemon : public senf::Daemon
58 std::cout << "Running configure()" << std::endl;
59 pidFile("invalid.pid");
60 consoleLog("invalid.log");
61 senf::Daemon::configure();
65 std::cout << "Running init()" << std::endl;
69 std::cout << "Running run()" << std::endl;
74 int myMain(int argc, char ** argv)
77 return instance.start(argc, argv);
82 int run(int argc, char ** argv)
85 if (pid < 0) throw senf::SystemException("::fork()");
88 ::_exit(myMain(argc, argv));
89 } catch (std::exception & ex) {
90 std::cerr << "Unexpected exception: " << ex.what() << std::endl;
92 std::cerr << "Unexpected exception" << std::endl;
97 if (::waitpid(pid, &status, 0) < 0) throw senf::SystemException("::waitpid()");
98 return WIFEXITED(status) ? WEXITSTATUS(status) : -1;
103 BOOST_AUTO_UNIT_TEST(testDaemon)
105 char * args[] = { "run",
106 "--console-log=testDaemon.log,none",
107 "--pid-file=testDaemon.pid" };
108 BOOST_CHECK_EQUAL( run(sizeof(args)/sizeof(*args),args), 0 );
110 BOOST_CHECK( ! boost::filesystem::exists("invalid.log") );
111 BOOST_CHECK( ! boost::filesystem::exists("invalid.pid") );
112 BOOST_REQUIRE( boost::filesystem::exists("testDaemon.pid") );
113 BOOST_REQUIRE( boost::filesystem::exists("testDaemon.log") );
115 boost::filesystem::rename("testDaemon.log", "testDaemon.log.1");
117 std::ifstream pidFile ("testDaemon.pid");
119 BOOST_REQUIRE( pidFile >> pid );
120 BOOST_REQUIRE( pid != 0 );
125 BOOST_CHECK( ! boost::filesystem::exists("testDaemon.pid") );
126 BOOST_CHECK( boost::filesystem::exists("testDaemon.log") );
127 BOOST_REQUIRE( boost::filesystem::exists("testDaemon.log.1") );
129 std::ifstream log ("testDaemon.log.1");
130 std::stringstream data;
132 BOOST_CHECK_EQUAL( data.str(), "Running init()\nRunning run()\n" );
133 BOOST_CHECK_NO_THROW( boost::filesystem::remove("testDaemon.log") );
134 BOOST_CHECK_NO_THROW( boost::filesystem::remove("testDaemon.log.1") );
137 ///////////////////////////////cc.e////////////////////////////////////////
144 // comment-column: 40
145 // c-file-style: "senf"
146 // indent-tabs-mode: nil
147 // ispell-local-dictionary: "american"
148 // compile-command: "scons -u test"