f3dfc1eccd582951a3dd2cce904c4d5aa1919c20
[senf.git] / 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.test 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 "../Utils/Exception.hh"
38 #include "../Utils/Backtrace.hh"
39 #include "../Scheduler/Scheduler.hh"
40
41 #include "../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         }
69
70         void run() {
71             std::cout << "Running run()" << std::endl; 
72             delay(1500);
73         }
74     };
75
76     int myMain(int argc, char const ** argv)
77     {
78         MyDaemon instance;
79         return instance.start(argc, argv);
80     }
81
82     int pid;
83
84     void backtrace(int)
85     {
86         senf::backtrace(std::cerr, 100);
87         ::signal(SIGABRT, SIG_DFL);
88         ::kill(::getpid(), SIGABRT);
89     };
90
91     int run(int argc, char const ** argv)
92     {
93         pid  = ::fork();
94         if (pid < 0) throw senf::SystemException("::fork()");
95         if (pid == 0) {
96             signal(SIGABRT, &backtrace);
97             signal(SIGCHLD, SIG_IGN);
98             try {
99                 ::_exit(myMain(argc, argv));
100             } catch (std::exception & ex) {
101                 std::cerr << "Unexpected exception: " << ex.what() << std::endl;
102             } catch (...) {
103                 std::cerr << "Unexpected exception" << std::endl;
104             }
105             ::_exit(125);
106         }
107         signal(SIGCHLD, SIG_DFL);
108         int status;
109         if (::waitpid(pid, &status, 0) < 0) 
110             throw senf::SystemException("::waitpid()");
111         if (WIFSIGNALED(status))
112             std::cerr << "Terminated with signal " 
113                       << senf::signalName(WTERMSIG(status)) << "(" << WTERMSIG(status) << ")\n";
114         else if (WIFEXITED(status))
115             std::cerr << "Exited normally with exit status " << WEXITSTATUS(status) << "\n";
116         return status;
117     }
118
119 }
120
121 BOOST_AUTO_UNIT_TEST(testDaemon)
122 {
123     char const * args[] = { "run", 
124                             "--console-log=testDaemon.log", 
125                             "--pid-file=testDaemon.pid" };
126
127     SENF_CHECK_NO_THROW( BOOST_CHECK_EQUAL( run(sizeof(args)/sizeof(*args), args), 0 ) );
128
129     BOOST_CHECK( ! boost::filesystem::exists("invalid.log") );
130     BOOST_CHECK( ! boost::filesystem::exists("invalid.pid") );
131     BOOST_CHECK( boost::filesystem::exists("testDaemon.pid") );
132     BOOST_REQUIRE( boost::filesystem::exists("testDaemon.log") );
133     
134     boost::filesystem::rename("testDaemon.log", "testDaemon.log.1");
135     {
136         std::ifstream pidFile ("testDaemon.pid");
137         int pid (0);
138         BOOST_CHECK( pidFile >> pid );
139         BOOST_CHECK( pid != 0 );
140         if (pid != 0)
141             ::kill(pid, SIGHUP);
142     }
143
144     delay(1000);
145     BOOST_CHECK( ! boost::filesystem::exists("testDaemon.pid") );
146     BOOST_CHECK( boost::filesystem::exists("testDaemon.log") );
147     BOOST_CHECK( boost::filesystem::exists("testDaemon.log.1") );
148     
149     std::ifstream log ("testDaemon.log.1");
150     std::stringstream data;
151     data << log.rdbuf();
152     BOOST_CHECK_EQUAL( data.str(), "Running init()\nRunning run()\n" );
153     BOOST_CHECK_NO_THROW( boost::filesystem::remove("testDaemon.log") );
154     BOOST_CHECK_NO_THROW( boost::filesystem::remove("testDaemon.log.1") );
155 }
156
157 ///////////////////////////////cc.e////////////////////////////////////////
158 #undef prefix_
159
160 \f
161 // Local Variables:
162 // mode: c++
163 // fill-column: 100
164 // comment-column: 40
165 // c-file-style: "senf"
166 // indent-tabs-mode: nil
167 // ispell-local-dictionary: "american"
168 // compile-command: "scons -u test"
169 // End: