libi2ncommon: (tomj) revamped pid file handling, added unit test
[libi2ncommon] / test / test_filefunc.cpp
CommitLineData
5de0c58a
RP
1/** @file
2 *
3 * tests for the modules "filefunc", "daemonfunc"
4 *
5 * (c) Copyright 2007-2008 by Intra2net AG
6 *
7 * info@intra2net.com
8 */
9
10//#define NOISEDEBUG
11
12#include <string>
13#include <vector>
14#include <list>
15#include <iostream>
16#include <iomanip>
17#include <sstream>
18#include <algorithm>
19
20#include <cppunit/extensions/TestFactoryRegistry.h>
21#include <cppunit/ui/text/TestRunner.h>
22#include <cppunit/extensions/HelperMacros.h>
23
24#include <filefunc.hxx>
25#include <daemonfunc.hxx>
26
27#ifdef NOISEDEBUG
28#define DOUT(msg) std::cout << msg << std::endl
29#else
30#define DOUT(msg) do {} while (0)
31#endif
32
33
34using namespace I2n;
35
36using namespace CppUnit;
37
38namespace {
39
40
41} // eo namespace <anonymous>
42
43
44class TestFileFunc : public TestFixture
45{
46 CPPUNIT_TEST_SUITE(TestFileFunc);
47
48 CPPUNIT_TEST(StatTest1);
49 CPPUNIT_TEST(DirTest1);
50 CPPUNIT_TEST(PathCuts1);
51 CPPUNIT_TEST(NormalizePath1);
52 CPPUNIT_TEST(TestUserAndGroupStuff1);
53 CPPUNIT_TEST(TestFileModes1);
54 CPPUNIT_TEST(TestPidOf1);
55
56 CPPUNIT_TEST_SUITE_END();
57
58 protected:
59
60 typedef std::list< std::string > StringList;
61
62 std::set<std::string> used_check_files;
63
64 std::string get_check_file_path(std::string tag)
65 {
66 std::string result;
67 result= "__unittest__" + tag + ".dat";
68 used_check_files.insert(result);
69 return result;
70 } // eo get_check_file_path
71
72
73 void remove_check_files()
74 {
75 for(std::set<std::string>::iterator it= used_check_files.begin();
76 it != used_check_files.end();
77 ++it)
78 {
79 std::string filepath(*it);
80 if (path_exists(filepath))
81 {
82 unlink(filepath);
83 }
84 //TODO
85 }
86 used_check_files.clear();
87 } // eo remove_check_files
88
89
90
91 public:
92
93 void setUp()
94 {
95 } // eo setUp
96
97
98 void tearDown()
99 {
100 remove_check_files();
101 } // eo tearDown
102
103
104 /*
105 * the tests:
106 */
107
108
109
110 void StatTest1()
111 {
112 I2n::Stat stat("test_filefunc.cpp");
113
114 CPPUNIT_ASSERT_EQUAL( true, (bool)stat );
115 CPPUNIT_ASSERT_EQUAL( true, stat.is_regular_file() );
116 CPPUNIT_ASSERT_EQUAL( false, stat.is_directory() );
117
118 stat= Stat("/dev/null");
119
120 CPPUNIT_ASSERT_EQUAL( true, (bool)stat );
121 CPPUNIT_ASSERT_EQUAL( false, stat.is_regular_file() );
122 CPPUNIT_ASSERT_EQUAL( false, stat.is_directory() );
123 CPPUNIT_ASSERT_EQUAL( true, stat.is_device() );
124 CPPUNIT_ASSERT_EQUAL( true, stat.is_character_device() );
125 CPPUNIT_ASSERT_EQUAL( false, stat.is_block_device() );
126 } // eo StatTest1
127
128
129 void DirTest1()
130 {
131 typedef std::vector< std::string > StringVector;
132 StringVector names;
133
134 bool res= I2n::get_dir(".",names);
135
136 CPPUNIT_ASSERT_EQUAL( true, res );
137 CPPUNIT_ASSERT( ! names.empty() );
138
139 StringVector::iterator it = std::find( names.begin(), names.end(), "test_filefunc.cpp");
140 CPPUNIT_ASSERT( it != names.end() );
141
142 it = std::find( names.begin(), names.end(), "." );
143 CPPUNIT_ASSERT( it == names.end() );
144
145 names= get_dir(".",true);
146 CPPUNIT_ASSERT( ! names.empty() );
147
148 for(it= names.begin(); it!=names.end(); ++it)
149 {
150 DOUT(" \"" << *it << "\"");
151 CPPUNIT_ASSERT_EQUAL( false, it->empty() );
152 }
153
154 it = std::find( names.begin(), names.end(), "." );
155 CPPUNIT_ASSERT( it != names.end() );
156 } // eo DirTest1
157
158
159
160 void PathCuts1()
161 {
162 std::string path1("/an/absolute/path");
163
164 CPPUNIT_ASSERT_EQUAL( std::string("/an/absolute"), dirname(path1) );
165 CPPUNIT_ASSERT_EQUAL( std::string("path"), basename(path1) );
166
167 std::string path2("just.a.name");
168 CPPUNIT_ASSERT_EQUAL( std::string("just.a.name"), basename(path2) );
169 CPPUNIT_ASSERT_EQUAL( std::string("."), dirname(path2) );
170 } // eo PathCuts1()
171
172
173
174 void NormalizePath1()
175 {
176 std::string path;
177
178 path= normalize_path("/a/simple/path/");
179 CPPUNIT_ASSERT_EQUAL( std::string("/a/simple/path"), path );
180
181 path= normalize_path("//another///simple/.//path//");
182 CPPUNIT_ASSERT_EQUAL( std::string("/another/simple/path"), path );
183
184 path= normalize_path("//..//..//a/dummy///../././simple/././/path//");
185 CPPUNIT_ASSERT_EQUAL( std::string("/a/simple/path"), path );
186
187 path= normalize_path("../a/dummy//././..//simple//nice//absolute//.././..//relative/path//");
188 CPPUNIT_ASSERT_EQUAL( std::string("../a/simple/relative/path"), path );
189
190 path= normalize_path("../../a/dummy//././..//simple/../nice//absolute//../.x/..//relative/path//");
191 CPPUNIT_ASSERT_EQUAL( std::string("../../a/nice/relative/path"), path );
192
193 } // eo NormalizePath1
194
195
196
197 void TestUserAndGroupStuff1()
198 {
199 User user_root((uid_t)0);
200 CPPUNIT_ASSERT_EQUAL( true, user_root.is_valid() );
201 CPPUNIT_ASSERT_EQUAL( true, (bool)user_root );
202
203 CPPUNIT_ASSERT_EQUAL( std::string("root"), user_root.Name );
204 CPPUNIT_ASSERT_EQUAL( (uid_t)0, user_root.Uid );
205 CPPUNIT_ASSERT_EQUAL( (gid_t)0, user_root.Gid );
206
207 User user_root2("root");
208 CPPUNIT_ASSERT_EQUAL( true, user_root2.is_valid() );
209 CPPUNIT_ASSERT_EQUAL( true, (bool)user_root2 );
210
211 CPPUNIT_ASSERT_EQUAL( std::string("root"), user_root2.Name );
212 CPPUNIT_ASSERT_EQUAL( (uid_t)0, user_root2.Uid );
213 CPPUNIT_ASSERT_EQUAL( (gid_t)0, user_root2.Gid );
214
215 Group group_root("root");
216 CPPUNIT_ASSERT_EQUAL( true, group_root.is_valid() );
217 CPPUNIT_ASSERT_EQUAL( true, (bool)group_root );
218
219 CPPUNIT_ASSERT_EQUAL( std::string("root"), group_root.Name );
220 CPPUNIT_ASSERT_EQUAL( (gid_t)0, group_root.Gid );
221
222 } // TestUserAndGroupStuff1()
223
224
225
226 void TestFileModes1()
227 {
228 std::string path= get_check_file_path("FileModes1");
229
230 write_file(path,"42");
231
232 Stat stat(path, false);
233 CPPUNIT_ASSERT_EQUAL( true, stat.is_valid() );
234
235 User user( stat.uid() );
236 Group group( stat.gid() );
237
238 CPPUNIT_ASSERT_EQUAL( true, user.is_valid() );
239 CPPUNIT_ASSERT_EQUAL( true, group.is_valid() );
240
241 bool res=chown( path, user.Name.c_str(), group.Gid );
242
243 CPPUNIT_ASSERT_EQUAL( true, res );
244 } // eo TestFileModes1()
245
246
247
248 void TestPidOf1()
249 {
250 using I2n::daemon::pid_of;
251
252 std::vector< pid_t > pid_list;
253
254 bool res= pid_of("init", pid_list);
255
256 CPPUNIT_ASSERT_EQUAL( true, res);
257 CPPUNIT_ASSERT_EQUAL( false, pid_list.empty() );
258
259 std::vector< pid_t >::const_iterator pos1 =
260 std::find( pid_list.begin(), pid_list.end(), 1);
261
262 CPPUNIT_ASSERT( pos1 != pid_list.end() );
263 } // eo TestPidOf1()
264
265
266
267}; // eo class TestFileFunc
268
269CPPUNIT_TEST_SUITE_REGISTRATION(TestFileFunc);