Update SENF to compile using g++ 4.3.2 (Ubuntu 8.10)
[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 ** 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 ** argv)
92     {
93         pid  = ::fork();
94         if (pid < 0) throw senf::SystemException("::fork()");
95         if (pid == 0) {
96             signal(SIGABRT, &backtrace);
97             try {
98                 ::_exit(myMain(argc, argv));
99             } catch (std::exception & ex) {
100                 std::cerr << "Unexpected exception: " << ex.what() << std::endl;
101             } catch (...) {
102                 std::cerr << "Unexpected exception" << std::endl;
103             }
104             ::_exit(125);
105         }
106         int status;
107         if (::waitpid(pid, &status, 0) < 0) 
108             throw senf::SystemException("::waitpid()");
109         if (WIFSIGNALED(status))
110             std::cerr << "Terminated with signal " 
111                       << senf::signalName(WTERMSIG(status)) << "(" << WTERMSIG(status) << ")\n";
112         else if (WIFEXITED(status))
113             std::cerr << "Exited normally with exit status " << WEXITSTATUS(status) << "\n";
114         return status;
115     }
116
117 }
118
119 BOOST_AUTO_UNIT_TEST(testDaemon)
120 {
121     char const * args[] = { "run", 
122                             "--console-log=testDaemon.log", 
123                             "--pid-file=testDaemon.pid" };
124
125     BOOST_CHECK_EQUAL( run(sizeof(args)/sizeof(*args), const_cast<char **>(args)), 0 );
126
127     BOOST_CHECK( ! boost::filesystem::exists("invalid.log") );
128     BOOST_CHECK( ! boost::filesystem::exists("invalid.pid") );
129     BOOST_CHECK( boost::filesystem::exists("testDaemon.pid") );
130     BOOST_REQUIRE( boost::filesystem::exists("testDaemon.log") );
131     
132     boost::filesystem::rename("testDaemon.log", "testDaemon.log.1");
133     {
134         std::ifstream pidFile ("testDaemon.pid");
135         int pid (0);
136         BOOST_CHECK( pidFile >> pid );
137         BOOST_CHECK( pid != 0 );
138         if (pid != 0)
139             ::kill(pid, SIGHUP);
140     }
141
142     delay(1000);
143     BOOST_CHECK( ! boost::filesystem::exists("testDaemon.pid") );
144     BOOST_CHECK( boost::filesystem::exists("testDaemon.log") );
145     BOOST_CHECK( boost::filesystem::exists("testDaemon.log.1") );
146     
147     std::ifstream log ("testDaemon.log.1");
148     std::stringstream data;
149     data << log.rdbuf();
150     BOOST_CHECK_EQUAL( data.str(), "Running init()\nRunning run()\n" );
151     BOOST_CHECK_NO_THROW( boost::filesystem::remove("testDaemon.log") );
152     BOOST_CHECK_NO_THROW( boost::filesystem::remove("testDaemon.log.1") );
153 }
154
155 ///////////////////////////////cc.e////////////////////////////////////////
156 #undef prefix_
157
158 \f
159 // Local Variables:
160 // mode: c++
161 // fill-column: 100
162 // comment-column: 40
163 // c-file-style: "senf"
164 // indent-tabs-mode: nil
165 // ispell-local-dictionary: "american"
166 // compile-command: "scons -u test"
167 // End: