1
/* This file is part of Strigi Desktop Search
3
* Copyright (C) 2007 Flavio Castelli <flavio.castelli@gmail.com>
5
* This library is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU Library General Public
7
* License as published by the Free Software Foundation; either
8
* version 2 of the License, or (at your option) any later version.
10
* This library is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
* Library General Public License for more details.
15
* You should have received a copy of the GNU Library General Public License
16
* along with this library; see the file COPYING.LIB. If not, write to
17
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18
* Boston, MA 02110-1301, USA.
21
#include "daemonconfiguratortest.h"
22
#include "daemonconfigurator.h"
31
* This file misses documentation on how it works and what it does.
35
using namespace strigiunittest;
37
namespace strigiunittest
39
class FindFilterRule {
45
FindFilterRule(bool value, string rule) {
50
bool operator()(pair<bool,string> filterRule) {
51
if (filterRule.first != m_value)
54
if (m_rule.compare(filterRule.second) != 0)
62
// Registers the fixture into the 'registry'
63
CPPUNIT_TEST_SUITE_REGISTRATION( DaemonConfiguratorTest );
66
* Test the equivalence of two different set of strings
68
bool DaemonConfiguratorTest::checkEq(set<string>& ori, set<string>& last,
71
for (set<string>::iterator itOri = ori.begin(); itOri != ori.end(); itOri++)
73
set<string>::iterator itLast = last.find(*itOri);
74
if (itLast == last.end())
77
errMsg += " was not found\n";
82
if (ori.size() == last.size()) {
86
str << "Found different size (" << ori.size() << " != " << last.size()
94
* Test the equivalence of two different set of filtering rules
96
bool DaemonConfiguratorTest::checkEq(Strigi::AnalyzerConfiguration& oriConf,
97
Strigi::AnalyzerConfiguration& lastConf,
100
vector<pair<bool,string> > filtersOri, filtersLast;
103
filtersOri = oriConf.filters();
104
filtersLast = lastConf.filters();
106
// check retrieved values
107
for (vector<pair<bool,string> >::iterator itOri = filtersOri.begin();
108
itOri != filtersOri.end(); itOri++)
110
FindFilterRule findRule (itOri->first, itOri->second);
112
vector<pair<bool,string> >::iterator itLast = find_if (
116
if (itLast == filtersLast.end())
118
errMsg = "filtering rule regarding ";
119
errMsg += itOri->second;
120
errMsg += " was not found\n";
125
if (filtersOri.size() == filtersLast.size()) {
129
str << "Found different size (" << filtersOri.size() << " != "
130
<< filtersLast.size() << ")" << endl;
136
void DaemonConfiguratorTest::setUp()
139
confFile1 = "StrigiDaemonConfiguratorTest.tmp";
140
ofstream file (confFile1.c_str(), ios::binary|ios::trunc);
142
defaultConf = new DaemonConfigurator(confFile1);
144
confFile2 = "StrigiDaemonConfiguratorTestSaved.tmp";
148
void DaemonConfiguratorTest::tearDown()
155
if (savedConf != NULL)
159
ret = unlink(confFile2.c_str());
163
err = "Error while removing temp file ";
165
perror (err.c_str());
169
ret = unlink(confFile1.c_str());
173
err = "Error while removing temp file ";
175
perror (err.c_str());
179
void DaemonConfiguratorTest::testDefaultConf()
181
CPPUNIT_ASSERT (defaultConf);
183
// indexed dirs for localhost repository must be different from zero
184
CPPUNIT_ASSERT (defaultConf->getIndexedDirectories().size() > 0);
186
// test default filtering rules presence
187
Strigi::AnalyzerConfiguration anConf;
188
defaultConf->loadFilteringRules(anConf);
189
CPPUNIT_ASSERT (anConf.filters().size() > 0);
192
void DaemonConfiguratorTest::testIndexedDirs()
194
set<string> dirsSet, dirsGet, dirsDelta, dirsLast;
198
dirsSet.insert ("/foo/bar");
199
dirsSet.insert ("/bar");
201
// override default indexed dirs
202
defaultConf->setIndexedDirectories( dirsSet, "localhost", false);
204
dirsGet = defaultConf->getIndexedDirectories("localhost");
206
// check retrieved dirs
207
equal = checkEq(dirsSet, dirsGet, errMsg);
208
CPPUNIT_ASSERT_MESSAGE (errMsg, equal);
210
// add new dirs with merge enabled
211
dirsDelta.insert("/tmp");
212
defaultConf->setIndexedDirectories( dirsDelta, "localhost", true);
215
for (set<string>::iterator it = dirsDelta.begin(); it != dirsDelta.end();
218
dirsLast.insert(*it);
222
dirsGet = defaultConf->getIndexedDirectories("localhost");
224
// check retrieved dirs
225
equal = checkEq(dirsLast, dirsGet, errMsg);
226
CPPUNIT_ASSERT_MESSAGE (errMsg, equal);
229
void DaemonConfiguratorTest::testFilters()
231
vector<pair<bool,string> > filtersSet, filtersGet;
233
filtersSet.push_back (make_pair(true, string("/foo")));
234
filtersSet.push_back (make_pair(false, string("bar*")));
236
// set custom filtering rules
237
defaultConf->saveFilteringRules( filtersSet);
240
Strigi::AnalyzerConfiguration anConf;
241
defaultConf->loadFilteringRules(anConf);
242
filtersGet = anConf.filters();
244
// check retrieved values
246
for (vector<pair<bool,string> >::iterator itSet = filtersSet.begin();
247
itSet != filtersSet.end(); itSet++)
249
FindFilterRule findRule (itSet->first, itSet->second);
251
vector<pair<bool,string> >::iterator itGet = find_if (
255
if (itGet == filtersGet.end())
262
CPPUNIT_ASSERT (check);
263
CPPUNIT_ASSERT (filtersSet.size() == filtersGet.size());
266
void DaemonConfiguratorTest::testRepository()
271
// retrieve directories associated to a non-existing repository
272
set<string> dirs = defaultConf->getIndexedDirectories( "newRepository");
274
CPPUNIT_ASSERT (dirs.size() == 0);
276
// save the number of directories associated to the localhost repository
277
set<string> localhostRepoEntriesIni = defaultConf->getIndexedDirectories();
278
CPPUNIT_ASSERT (localhostRepoEntriesIni.size() > 0);
281
// add new dir to newRepository list, overwrite old dir associated to this
284
setDirs.insert ("/foobar");
285
defaultConf->setIndexedDirectories( setDirs, "newRepository", false);
286
dirs = defaultConf->getIndexedDirectories( "newRepository");
287
equal = checkEq (setDirs, dirs, errMsg);
288
CPPUNIT_ASSERT_MESSAGE (errMsg, equal);
290
// default repository dirs must be the same
291
set<string> localhostRepoEntries = defaultConf->getIndexedDirectories();
292
equal = checkEq (localhostRepoEntriesIni, localhostRepoEntries, errMsg);
293
CPPUNIT_ASSERT_MESSAGE (errMsg, equal);
296
void DaemonConfiguratorTest::testPollingInterval()
302
// test polling interval configuration for default repository
303
int defRepVal= DaemonConfigurator::DEFAULT_POLLING_INTERVAL + 1;
304
defaultConf->setPollingInterval( defRepVal);
305
ret = defaultConf->getPollingInterval();
307
if (ret != defRepVal)
309
message = "Failed to set polling interval for default repository: set ";
310
snprintf (buffer, 10, "%i", defRepVal);
313
snprintf (buffer, 10, "%i", ret);
315
CPPUNIT_FAIL (message);
318
// test polling interval configuration for another repository
319
int newRepVal= DaemonConfigurator::DEFAULT_POLLING_INTERVAL + 10;
320
defaultConf->setPollingInterval( newRepVal, "newRepository");
321
ret = defaultConf->getPollingInterval("newRepository");
323
if (ret != newRepVal)
325
message = "Failed to set polling interval for \"newRepository\"";
326
message += "repository: set ";
327
snprintf (buffer, 10, "%i", newRepVal);
330
snprintf (buffer, 10, "%i", ret);
332
CPPUNIT_FAIL (message);
335
// polling interval associated to the default repository
336
// should not have been changed
337
ret = defaultConf->getPollingInterval();
339
if (ret != defRepVal)
341
message = "After configuration of polling interval for \"newRepository\"";
342
message += "repository, default repository polling interval changed!\n";
343
message += "Expected ";
344
snprintf (buffer, 10, "%i", defRepVal);
347
snprintf (buffer, 10, "%i", ret);
349
CPPUNIT_FAIL (message);
352
// test minimum polling interval value
353
defaultConf->setPollingInterval( 0);
354
ret = defaultConf->getPollingInterval();
355
if (ret != DaemonConfigurator::DEFAULT_POLLING_INTERVAL)
357
message = "Test minimum polling interval failed, expected ";
358
snprintf (buffer, 10,"%i",DaemonConfigurator::DEFAULT_POLLING_INTERVAL);
361
snprintf (buffer, 10, "%i", ret);
363
CPPUNIT_FAIL (message);
367
void DaemonConfiguratorTest::testSave()
372
// save defaultConf to disk
373
defaultConf->save(confFile2.c_str());
376
savedConf = new DaemonConfigurator(confFile2);
378
// compare filtering rules
379
Strigi::AnalyzerConfiguration anConfDefault;
380
defaultConf->loadFilteringRules(anConfDefault);
382
Strigi::AnalyzerConfiguration anConfSaved;
383
savedConf->loadFilteringRules(anConfSaved);
385
equal = checkEq (anConfDefault, anConfSaved, errMsg);
386
CPPUNIT_ASSERT_MESSAGE (errMsg, equal);
388
// compare repositories
389
set<string> localhostRepoEntriesDef = defaultConf->getIndexedDirectories();
390
set<string> localhostRepoEntriesSaved = savedConf->getIndexedDirectories();
391
set<string> newRepoEntriesDef =
392
defaultConf->getIndexedDirectories("newRepository");
393
set<string> newRepoEntriesSaved =
394
savedConf->getIndexedDirectories("newRepository");
396
equal = checkEq(localhostRepoEntriesDef, localhostRepoEntriesSaved, errMsg);
397
CPPUNIT_ASSERT_MESSAGE (errMsg, equal);
399
equal = checkEq(newRepoEntriesDef, newRepoEntriesSaved, errMsg);
400
CPPUNIT_ASSERT_MESSAGE (errMsg, equal);
402
// compare polling interval
403
int localhostRepoPollingDef = defaultConf->getPollingInterval();
404
int localhostRepoPollingSaved = savedConf->getPollingInterval();
405
int newRepoPollingDef = defaultConf->getPollingInterval("newRepository");
406
int newRepoPollingSaved = savedConf->getPollingInterval("newRepository");
408
CPPUNIT_ASSERT (localhostRepoPollingDef == localhostRepoPollingSaved);
409
CPPUNIT_ASSERT (newRepoPollingDef == newRepoPollingSaved);