bea493ca476bf6ab8e220a6d02e9254fe4fc5bc7
[senf.git] / senf / Utils / Daemon / Daemon.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2007
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
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.
12 //
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.
17 //
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.
22
23 /** \file
24     \brief Daemon unit tests */
25
26 //#include "Daemon.test.hh"
27 //#include "Daemon.test.ih"
28
29 // Custom includes
30 #include <unistd.h>
31 #include <sys/types.h>
32 #include <sys/wait.h>
33 #include <iostream>
34 #include <fstream>
35 #include <boost/filesystem/operations.hpp>
36 #include "Daemon.hh"
37 #include <senf/Utils/Exception.hh>
38 #include <senf/Utils/Backtrace.hh>
39 #include <senf/Scheduler/Scheduler.hh>
40
41 #include <senf/Utils/auto_unit_test.hh>
42 #include <boost/test/test_tools.hpp>
43
44 #define prefix_
45 ///////////////////////////////cc.p////////////////////////////////////////
46
47 namespace {
48
49     void delay(unsigned long milliseconds)
50     {
51         struct timespec ts;
52         ts.tv_sec = milliseconds / 1000;
53         ts.tv_nsec = (milliseconds % 1000) * 1000000;
54         while (nanosleep(&ts,&ts) < 0 && errno == EINTR) ;
55     }
56
57     class MyDaemon : public senf::Daemon
58     {
59         void configure() {
60             std::cout << "Running configure()" << std::endl;
61             pidFile("invalid.pid");
62             consoleLog("invalid.log");
63             senf::Daemon::configure();
64         }
65
66         void init() {
67             std::cout << "Running init()" << std::endl;
68             std::cerr << "(stderr)" << std::endl;
69         }
70
71         void run() {
72             std::cout << "Running run()" << std::endl;
73             delay(2000);
74         }
75     };
76
77     int myMain(int argc, char const ** argv)
78     {
79         MyDaemon instance;
80         return instance.start(argc, argv);
81     }
82
83     int pid;
84
85     void backtrace(int)
86     {
87         senf::backtrace(std::cerr, 100);
88         ::signal(SIGABRT, SIG_DFL);
89         ::kill(::getpid(), SIGABRT);
90     };
91
92     int run(int argc, char const ** argv)
93     {
94         pid  = ::fork();
95         if (pid < 0) throw senf::SystemException("::fork()");
96         if (pid == 0) {
97             signal(SIGABRT, &backtrace);
98             signal(SIGCHLD, SIG_IGN);
99             try {
100                 ::_exit(myMain(argc, argv));
101             } catch (std::exception & ex) {
102                 std::cerr << "Unexpected exception: " << ex.what() << std::endl;
103             } catch (...) {
104                 std::cerr << "Unexpected exception" << std::endl;
105             }
106             ::_exit(125);
107         }
108         signal(SIGCHLD, SIG_DFL);
109         int status;
110         if (::waitpid(pid, &status, 0) < 0)
111             throw senf::SystemException("::waitpid()");
112         if (WIFSIGNALED(status))
113             std::cerr << "Terminated with signal "
114                       << senf::signalName(WTERMSIG(status)) << "(" << WTERMSIG(status) << ")\n";
115         else if (WIFEXITED(status))
116             std::cerr << "Exited normally with exit status " << WEXITSTATUS(status) << "\n";
117         return status;
118     }
119
120 }
121
122 SENF_AUTO_UNIT_TEST(testDaemon)
123 {
124     char const * args[] = { "run",
125                             "--console-log=testDaemon.log",
126                             "--pid-file=testDaemon.pid" };
127
128     SENF_CHECK_NO_THROW( BOOST_CHECK_EQUAL( run(sizeof(args)/sizeof(*args), args), 0 ) );
129
130     BOOST_CHECK( ! boost::filesystem::exists("invalid.log") );
131     BOOST_CHECK( ! boost::filesystem::exists("invalid.pid") );
132     BOOST_CHECK( boost::filesystem::exists("testDaemon.pid") );
133     BOOST_REQUIRE( boost::filesystem::exists("testDaemon.log") );
134
135     boost::filesystem::rename("testDaemon.log", "testDaemon.log.1");
136     {
137         std::ifstream pidFile ("testDaemon.pid");
138         int pid (0);
139         BOOST_CHECK( pidFile >> pid );
140         BOOST_CHECK( pid != 0 );
141         if (pid != 0)
142             ::kill(pid, SIGHUP);
143     }
144
145     delay(3000);
146     BOOST_CHECK( ! boost::filesystem::exists("testDaemon.pid") );
147     BOOST_CHECK( boost::filesystem::exists("testDaemon.log") );
148     BOOST_CHECK( boost::filesystem::exists("testDaemon.log.1") );
149
150     std::ifstream log ("testDaemon.log.1");
151     std::stringstream data;
152     data << log.rdbuf();
153     BOOST_CHECK_EQUAL( data.str(), "Running init()\n(stderr)\nRunning run()\n" );
154     SENF_CHECK_NO_THROW( boost::filesystem::remove("testDaemon.log") );
155     SENF_CHECK_NO_THROW( boost::filesystem::remove("testDaemon.log.1") );
156 }
157
158 ///////////////////////////////cc.e////////////////////////////////////////
159 #undef prefix_
160
161 \f
162 // Local Variables:
163 // mode: c++
164 // fill-column: 100
165 // comment-column: 40
166 // c-file-style: "senf"
167 // indent-tabs-mode: nil
168 // ispell-local-dictionary: "american"
169 // compile-command: "scons -u test"
170 // End: