Merge branch 'daemon-ext'
[libi2ncommon] / test / test_pidfile.cpp
CommitLineData
0e23f538
TJ
1/*
2The software in this package is distributed under the GNU General
3Public License version 2 (with a special exception described below).
4
5A copy of GNU General Public License (GPL) is included in this distribution,
6in the file COPYING.GPL.
7
8As a special exception, if other files instantiate templates or use macros
9or inline functions from this file, or you compile this file and link it
10with other works to produce a work based on this file, this file
11does not by itself cause the resulting work to be covered
12by the GNU General Public License.
13
14However the source code for this file must still be made available
15in accordance with section (3) of the GNU General Public License.
16
17This exception does not invalidate any other reasons why a work based
18on this file might be covered by the GNU General Public License.
19*/
6bf07aed
TJ
20/**
21 Pid file handling unit tests
22
23 @copyright Intra2net AG
6bf07aed
TJ
24*/
25
26#include <unistd.h>
27
28#include <iostream>
29#include <string>
30#include <sstream>
31#include <stdexcept>
32
9fe0853b
TJ
33#define BOOST_TEST_DYN_LINK
34#include <boost/test/unit_test.hpp>
6bf07aed
TJ
35
36#include <pidfile.hpp>
37#include <filefunc.hxx>
38
39using namespace std;
6bf07aed 40
9fe0853b 41class TestPidFileFixture
6bf07aed 42{
9fe0853b
TJ
43protected:
44 string Filename;
6bf07aed 45
9fe0853b
TJ
46public:
47 TestPidFileFixture()
48 {
49 ostringstream out;
bb96045b 50 out << "test_pidfile.run." << getpid();
6bf07aed 51
9fe0853b
TJ
52 Filename = out.str();
53 }
6bf07aed 54
9fe0853b
TJ
55 ~TestPidFileFixture()
56 {
57 }
6bf07aed
TJ
58};
59
9fe0853b
TJ
60BOOST_FIXTURE_TEST_SUITE(TestPidFile, TestPidFileFixture)
61
62BOOST_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
70BOOST_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
83BOOST_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
97BOOST_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
105BOOST_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
122BOOST_AUTO_TEST_SUITE_END()