~ubuntu-branches/ubuntu/oneiric/strigi/oneiric

« back to all changes in this revision

Viewing changes to tests/daemon/daemonconfiguratortest.cpp

  • Committer: Package Import Robot
  • Author(s): Fathi Boudra
  • Date: 2011-09-20 08:50:25 UTC
  • mto: (1.1.20 upstream) (5.1.6 sid)
  • mto: This revision was merged to the branch mainline in revision 44.
  • Revision ID: package-import@ubuntu.com-20110920085025-wszfu6x8rshrjq0e
ImportĀ upstreamĀ versionĀ 0.7.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* This file is part of Strigi Desktop Search
2
 
 *
3
 
 * Copyright (C) 2007 Flavio Castelli <flavio.castelli@gmail.com>
4
 
 *
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.
9
 
 *
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.
14
 
 *
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.
19
 
 */
20
 
 
21
 
#include "daemonconfiguratortest.h"
22
 
#include "daemonconfigurator.h"
23
 
 
24
 
#include <algorithm>
25
 
#include <fstream>
26
 
#include <set>
27
 
 
28
 
#include <unistd.h>
29
 
 
30
 
/**
31
 
 * This file misses documentation on how it works and what it does.
32
 
 **/
33
 
 
34
 
using namespace std;
35
 
using namespace strigiunittest;
36
 
 
37
 
namespace strigiunittest
38
 
{
39
 
    class FindFilterRule {
40
 
        private:
41
 
            string m_rule;
42
 
            bool   m_value;
43
 
    
44
 
        public:
45
 
            FindFilterRule(bool value, string rule) {
46
 
                m_value = value;
47
 
                m_rule = rule;
48
 
            }
49
 
    
50
 
            bool operator()(pair<bool,string> filterRule) {
51
 
                if (filterRule.first != m_value)
52
 
                    return false;
53
 
                
54
 
                if (m_rule.compare(filterRule.second) != 0)
55
 
                    return false;
56
 
 
57
 
                return true;
58
 
            }
59
 
    };
60
 
}
61
 
 
62
 
// Registers the fixture into the 'registry'
63
 
CPPUNIT_TEST_SUITE_REGISTRATION( DaemonConfiguratorTest );
64
 
 
65
 
/*!
66
 
* Test the equivalence of two different set of strings
67
 
*/
68
 
bool DaemonConfiguratorTest::checkEq(set<string>& ori, set<string>& last, 
69
 
                                     string& errMsg)
70
 
{
71
 
    for (set<string>::iterator itOri = ori.begin(); itOri != ori.end(); itOri++)
72
 
    {
73
 
        set<string>::iterator itLast = last.find(*itOri);
74
 
        if (itLast == last.end())
75
 
        {
76
 
            errMsg = *itOri;
77
 
            errMsg += " was not found\n";
78
 
            return false;
79
 
        }
80
 
    }
81
 
    
82
 
    if (ori.size() == last.size()) {
83
 
        return true;
84
 
    } else {
85
 
        ostringstream str;
86
 
        str << "Found different size (" << ori.size() << " != " << last.size()
87
 
            << ")" << endl;
88
 
        errMsg += str.str();
89
 
        return false;
90
 
    }
91
 
}
92
 
 
93
 
/*!
94
 
 * Test the equivalence of two different set of filtering rules
95
 
 */
96
 
bool DaemonConfiguratorTest::checkEq(Strigi::AnalyzerConfiguration& oriConf,
97
 
                                     Strigi::AnalyzerConfiguration& lastConf,
98
 
                                     string& errMsg)
99
 
{
100
 
    vector<pair<bool,string> > filtersOri, filtersLast;
101
 
    
102
 
    // retrieve rules
103
 
    filtersOri  = oriConf.filters();
104
 
    filtersLast = lastConf.filters();
105
 
    
106
 
    // check retrieved values
107
 
    for (vector<pair<bool,string> >::iterator itOri = filtersOri.begin();
108
 
         itOri != filtersOri.end(); itOri++)
109
 
    {
110
 
        FindFilterRule findRule (itOri->first, itOri->second);
111
 
        
112
 
        vector<pair<bool,string> >::iterator itLast = find_if ( 
113
 
                                                            filtersLast.begin(),
114
 
                                                            filtersLast.end(),
115
 
                                                            findRule);
116
 
        if (itLast == filtersLast.end())
117
 
        {
118
 
            errMsg = "filtering rule regarding ";
119
 
            errMsg += itOri->second;
120
 
            errMsg += " was not found\n";
121
 
            return false;
122
 
        }
123
 
    }
124
 
    
125
 
    if (filtersOri.size() == filtersLast.size()) {
126
 
        return true;
127
 
    } else {
128
 
        ostringstream str;
129
 
        str << "Found different size (" << filtersOri.size() << " != "
130
 
            << filtersLast.size() << ")" << endl;
131
 
        errMsg += str.str();
132
 
        return false;
133
 
    }
134
 
}
135
 
 
136
 
void DaemonConfiguratorTest::setUp()
137
 
{
138
 
    //touch file
139
 
    confFile1 = "StrigiDaemonConfiguratorTest.tmp";
140
 
    ofstream file (confFile1.c_str(), ios::binary|ios::trunc);
141
 
    file.close();
142
 
    defaultConf = new DaemonConfigurator(confFile1);
143
 
    
144
 
    confFile2 = "StrigiDaemonConfiguratorTestSaved.tmp";
145
 
    savedConf   = NULL;
146
 
}
147
 
 
148
 
void DaemonConfiguratorTest::tearDown()
149
 
{
150
 
    int ret;
151
 
    string err;
152
 
    
153
 
    delete defaultConf;
154
 
 
155
 
    if (savedConf != NULL)
156
 
    {
157
 
        delete savedConf;
158
 
        
159
 
        ret = unlink(confFile2.c_str());
160
 
    
161
 
        if (ret == -1)
162
 
        {
163
 
            err = "Error while removing temp file ";
164
 
            err += confFile2;
165
 
            perror (err.c_str());
166
 
        }
167
 
    }
168
 
    
169
 
    ret = unlink(confFile1.c_str());
170
 
    
171
 
    if (ret == -1)
172
 
    {
173
 
        err = "Error while removing temp file ";
174
 
        err += confFile1;
175
 
        perror (err.c_str());
176
 
    }
177
 
}
178
 
 
179
 
void DaemonConfiguratorTest::testDefaultConf()
180
 
{
181
 
    CPPUNIT_ASSERT (defaultConf);
182
 
    
183
 
    // indexed dirs for localhost repository must be different from zero
184
 
    CPPUNIT_ASSERT (defaultConf->getIndexedDirectories().size() > 0);
185
 
    
186
 
    // test default filtering rules presence
187
 
    Strigi::AnalyzerConfiguration anConf;
188
 
    defaultConf->loadFilteringRules(anConf);
189
 
    CPPUNIT_ASSERT (anConf.filters().size() > 0);
190
 
}
191
 
 
192
 
void DaemonConfiguratorTest::testIndexedDirs()
193
 
{
194
 
    set<string> dirsSet, dirsGet, dirsDelta, dirsLast;
195
 
    bool equal;
196
 
    string errMsg;
197
 
    
198
 
    dirsSet.insert ("/foo/bar");
199
 
    dirsSet.insert ("/bar");
200
 
    
201
 
    // override default indexed dirs
202
 
    defaultConf->setIndexedDirectories( dirsSet, "localhost", false);
203
 
    
204
 
    dirsGet = defaultConf->getIndexedDirectories("localhost");
205
 
    
206
 
    // check retrieved dirs
207
 
    equal = checkEq(dirsSet, dirsGet, errMsg);
208
 
    CPPUNIT_ASSERT_MESSAGE (errMsg, equal);
209
 
    
210
 
    // add new dirs with merge enabled
211
 
    dirsDelta.insert("/tmp");
212
 
    defaultConf->setIndexedDirectories( dirsDelta, "localhost", true);
213
 
    
214
 
    dirsLast = dirsSet;
215
 
    for (set<string>::iterator it = dirsDelta.begin(); it != dirsDelta.end();
216
 
         it++)
217
 
    {
218
 
        dirsLast.insert(*it);
219
 
    }
220
 
    
221
 
    dirsGet.clear();
222
 
    dirsGet = defaultConf->getIndexedDirectories("localhost");
223
 
    
224
 
    // check retrieved dirs
225
 
    equal = checkEq(dirsLast, dirsGet, errMsg);
226
 
    CPPUNIT_ASSERT_MESSAGE (errMsg, equal);
227
 
}
228
 
 
229
 
void DaemonConfiguratorTest::testFilters()
230
 
{
231
 
    vector<pair<bool,string> > filtersSet, filtersGet;
232
 
    
233
 
    filtersSet.push_back (make_pair(true, string("/foo")));
234
 
    filtersSet.push_back (make_pair(false, string("bar*")));
235
 
    
236
 
    // set custom filtering rules
237
 
    defaultConf->saveFilteringRules( filtersSet);
238
 
    
239
 
    // retrieve rules
240
 
    Strigi::AnalyzerConfiguration anConf;
241
 
    defaultConf->loadFilteringRules(anConf);
242
 
    filtersGet = anConf.filters();
243
 
    
244
 
    // check retrieved values
245
 
    bool check = true;
246
 
    for (vector<pair<bool,string> >::iterator itSet = filtersSet.begin();
247
 
         itSet != filtersSet.end(); itSet++)
248
 
    {
249
 
        FindFilterRule findRule (itSet->first, itSet->second);
250
 
        
251
 
        vector<pair<bool,string> >::iterator itGet = find_if (
252
 
                                                            filtersGet.begin(),
253
 
                                                            filtersGet.end(),
254
 
                                                            findRule);
255
 
        if (itGet == filtersGet.end())
256
 
        {
257
 
            check = false;
258
 
            break;
259
 
        }
260
 
    }
261
 
    
262
 
    CPPUNIT_ASSERT (check);
263
 
    CPPUNIT_ASSERT (filtersSet.size() == filtersGet.size());
264
 
}
265
 
 
266
 
void DaemonConfiguratorTest::testRepository()
267
 
{
268
 
    bool equal;
269
 
    string errMsg;
270
 
    
271
 
    // retrieve directories associated to a non-existing repository
272
 
    set<string> dirs = defaultConf->getIndexedDirectories( "newRepository");
273
 
    
274
 
    CPPUNIT_ASSERT (dirs.size() == 0);
275
 
    
276
 
    // save the number of directories associated to the localhost repository
277
 
    set<string> localhostRepoEntriesIni = defaultConf->getIndexedDirectories();
278
 
    CPPUNIT_ASSERT (localhostRepoEntriesIni.size() > 0);
279
 
    
280
 
    
281
 
    // add new dir to newRepository list, overwrite old dir associated to this
282
 
    // repository
283
 
    set<string> setDirs;
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);
289
 
    
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);
294
 
}
295
 
 
296
 
void DaemonConfiguratorTest::testPollingInterval()
297
 
{
298
 
    char buffer [10];
299
 
    int ret;
300
 
    string message;
301
 
 
302
 
    // test polling interval configuration for default repository
303
 
    int defRepVal= DaemonConfigurator::DEFAULT_POLLING_INTERVAL + 1;
304
 
    defaultConf->setPollingInterval( defRepVal);
305
 
    ret = defaultConf->getPollingInterval();
306
 
 
307
 
    if (ret != defRepVal)
308
 
    {
309
 
        message = "Failed to set polling interval for default repository: set ";
310
 
        snprintf (buffer, 10, "%i", defRepVal);
311
 
        message += buffer;
312
 
        message += ", got ";
313
 
        snprintf (buffer, 10, "%i", ret);
314
 
        message += buffer;
315
 
        CPPUNIT_FAIL (message);
316
 
    }
317
 
 
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");
322
 
 
323
 
    if (ret != newRepVal)
324
 
    {
325
 
        message = "Failed to set polling interval for \"newRepository\"";
326
 
        message += "repository: set ";
327
 
        snprintf (buffer, 10, "%i", newRepVal);
328
 
        message += buffer;
329
 
        message += ", got ";
330
 
        snprintf (buffer, 10, "%i", ret);
331
 
        message += buffer;
332
 
        CPPUNIT_FAIL (message);
333
 
    }
334
 
 
335
 
    // polling interval associated to the default repository
336
 
    // should not have been changed
337
 
    ret = defaultConf->getPollingInterval();
338
 
 
339
 
    if (ret != defRepVal)
340
 
    {
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);
345
 
        message += buffer;
346
 
        message += ", got ";
347
 
        snprintf (buffer, 10, "%i", ret);
348
 
        message += buffer;
349
 
        CPPUNIT_FAIL (message);
350
 
    }
351
 
    
352
 
    // test minimum polling interval value
353
 
    defaultConf->setPollingInterval( 0);
354
 
    ret = defaultConf->getPollingInterval();
355
 
    if (ret != DaemonConfigurator::DEFAULT_POLLING_INTERVAL)
356
 
    {
357
 
        message = "Test minimum polling interval failed, expected ";
358
 
        snprintf (buffer, 10,"%i",DaemonConfigurator::DEFAULT_POLLING_INTERVAL);
359
 
        message += buffer;
360
 
        message += ", got ";
361
 
        snprintf (buffer, 10, "%i", ret);
362
 
        message += buffer;
363
 
        CPPUNIT_FAIL (message);
364
 
    }
365
 
}
366
 
 
367
 
void DaemonConfiguratorTest::testSave()
368
 
{
369
 
    bool equal;
370
 
    string errMsg;
371
 
    
372
 
    // save defaultConf to disk
373
 
    defaultConf->save(confFile2.c_str());
374
 
    
375
 
    // load saved conf
376
 
    savedConf = new DaemonConfigurator(confFile2);
377
 
    
378
 
    // compare filtering rules
379
 
    Strigi::AnalyzerConfiguration anConfDefault;
380
 
    defaultConf->loadFilteringRules(anConfDefault);
381
 
    
382
 
    Strigi::AnalyzerConfiguration anConfSaved;
383
 
    savedConf->loadFilteringRules(anConfSaved);
384
 
    
385
 
    equal = checkEq (anConfDefault, anConfSaved, errMsg);
386
 
    CPPUNIT_ASSERT_MESSAGE (errMsg, equal);
387
 
    
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");
395
 
    
396
 
    equal = checkEq(localhostRepoEntriesDef, localhostRepoEntriesSaved, errMsg);
397
 
    CPPUNIT_ASSERT_MESSAGE (errMsg, equal);
398
 
    
399
 
    equal = checkEq(newRepoEntriesDef, newRepoEntriesSaved, errMsg);
400
 
    CPPUNIT_ASSERT_MESSAGE (errMsg, equal);
401
 
    
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");
407
 
    
408
 
    CPPUNIT_ASSERT (localhostRepoPollingDef == localhostRepoPollingSaved);
409
 
    CPPUNIT_ASSERT (newRepoPollingDef == newRepoPollingSaved);
410
 
}