Merge branch 'daemon-ext'
[libi2ncommon] / test / test_pidfile.cpp
1 /*
2 The software in this package is distributed under the GNU General
3 Public License version 2 (with a special exception described below).
4
5 A copy of GNU General Public License (GPL) is included in this distribution,
6 in the file COPYING.GPL.
7
8 As a special exception, if other files instantiate templates or use macros
9 or inline functions from this file, or you compile this file and link it
10 with other works to produce a work based on this file, this file
11 does not by itself cause the resulting work to be covered
12 by the GNU General Public License.
13
14 However the source code for this file must still be made available
15 in accordance with section (3) of the GNU General Public License.
16
17 This exception does not invalidate any other reasons why a work based
18 on this file might be covered by the GNU General Public License.
19 */
20 /**
21    Pid file handling unit tests
22
23    @copyright Intra2net AG
24 */
25
26 #include <unistd.h>
27
28 #include <iostream>
29 #include <string>
30 #include <sstream>
31 #include <stdexcept>
32
33 #define BOOST_TEST_DYN_LINK
34 #include <boost/test/unit_test.hpp>
35
36 #include <pidfile.hpp>
37 #include <filefunc.hxx>
38
39 using namespace std;
40
41 class TestPidFileFixture
42 {
43 protected:
44    string Filename;
45
46 public:
47     TestPidFileFixture()
48     {
49         ostringstream out;
50         out << "test_pidfile.run." << getpid();
51
52         Filename = out.str();
53     }
54
55     ~TestPidFileFixture()
56     {
57     }
58 };
59
60 BOOST_FIXTURE_TEST_SUITE(TestPidFile, TestPidFileFixture)
61
62 BOOST_AUTO_TEST_CASE(WriteFile)
63 {
64     PidFile pidfile(Filename);
65     bool rtn_write = pidfile.write();
66
67     BOOST_CHECK_EQUAL(true, rtn_write);
68 }
69
70 BOOST_AUTO_TEST_CASE(AutoRemoval)
71 {
72     bool rtn_write = false;
73     {
74         PidFile pidfile(Filename);
75         rtn_write = pidfile.write();
76     }
77     bool exists = I2n::file_exists(Filename);
78
79     BOOST_CHECK_EQUAL(true, rtn_write);
80     BOOST_CHECK_EQUAL(false, exists);
81 }
82
83 BOOST_AUTO_TEST_CASE(NoAutoRemoval)
84 {
85     bool rtn_write = false;
86     {
87         PidFile pidfile(Filename, false);
88         rtn_write = pidfile.write();
89     }
90     bool exists = I2n::file_exists(Filename);
91     I2n::unlink(Filename);
92
93     BOOST_CHECK_EQUAL(true, rtn_write);
94     BOOST_CHECK_EQUAL(true, exists);
95 }
96
97 BOOST_AUTO_TEST_CASE(NotRunning)
98 {
99     PidFile pidfile(Filename);
100     bool rtn_check = pidfile.check_already_running();
101
102     BOOST_CHECK_EQUAL(false, rtn_check);
103 }
104
105 BOOST_AUTO_TEST_CASE(IsAlreadyRunning)
106 {
107     PidFile pidfile1(Filename);
108     bool rtn_file1_check = pidfile1.check_already_running();
109     bool rtn_file1_write = pidfile1.write();
110
111     BOOST_CHECK_EQUAL(false, rtn_file1_check);
112     BOOST_CHECK_EQUAL(true, rtn_file1_write);
113
114     PidFile pidfile2(Filename);
115     pid_t my_pid = 0;
116     bool rtn_file2_check = pidfile2.check_already_running(&my_pid);
117
118     BOOST_CHECK_EQUAL(true, rtn_file2_check);
119     BOOST_CHECK_EQUAL(getpid(), my_pid);
120 }
121
122 BOOST_AUTO_TEST_SUITE_END()