1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
|
/*
* Copyright (C) 2014 Florian Geyer <blueice@fobos.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 or (at your option)
* version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "TestEntrySearcher.h"
#include "core/Group.h"
#include "core/Tools.h"
#include <QTest>
QTEST_GUILESS_MAIN(TestEntrySearcher)
void TestEntrySearcher::init()
{
m_rootGroup = new Group();
m_entrySearcher = EntrySearcher();
}
void TestEntrySearcher::cleanup()
{
delete m_rootGroup;
}
void TestEntrySearcher::testSearch()
{
/**
* Root
* - group1 (search disabled)
* - group11
* - group2
* - group21
* - group211
* - group2111
*/
Group* group1 = new Group();
Group* group2 = new Group();
Group* group3 = new Group();
group1->setParent(m_rootGroup);
group2->setParent(m_rootGroup);
group3->setParent(m_rootGroup);
Group* group11 = new Group();
group11->setParent(group1);
Group* group21 = new Group();
Group* group211 = new Group();
Group* group2111 = new Group();
group21->setParent(group2);
group211->setParent(group21);
group2111->setParent(group211);
group1->setSearchingEnabled(Group::Disable);
Entry* eRoot = new Entry();
eRoot->setTitle("test search term test");
eRoot->setGroup(m_rootGroup);
Entry* eRoot2 = new Entry();
eRoot2->setNotes("test term test");
eRoot2->setGroup(m_rootGroup);
// Searching is disabled for these
Entry* e1 = new Entry();
e1->setUsername("test search term test");
e1->setGroup(group1);
Entry* e11 = new Entry();
e11->setNotes("test search term test");
e11->setGroup(group11);
// End searching disabled
Entry* e2111 = new Entry();
e2111->setTitle("test search term test");
e2111->setGroup(group2111);
Entry* e2111b = new Entry();
e2111b->setNotes("test search test");
e2111b->setUsername("user123");
e2111b->setPassword("testpass");
e2111b->setGroup(group2111);
Entry* e3 = new Entry();
e3->setUrl("test search term test");
e3->setGroup(group3);
Entry* e3b = new Entry();
e3b->setTitle("test search test 123");
e3b->setUsername("test@email.com");
e3b->setPassword("realpass");
e3b->setGroup(group3);
// Simple search term testing
m_searchResult = m_entrySearcher.search("search", m_rootGroup);
QCOMPARE(m_searchResult.count(), 5);
m_searchResult = m_entrySearcher.search("search term", m_rootGroup);
QCOMPARE(m_searchResult.count(), 3);
m_searchResult = m_entrySearcher.search("123", m_rootGroup);
QCOMPARE(m_searchResult.count(), 2);
m_searchResult = m_entrySearcher.search("search term", group211);
QCOMPARE(m_searchResult.count(), 1);
// Test advanced search terms
m_searchResult = m_entrySearcher.search("title:123", m_rootGroup);
QCOMPARE(m_searchResult.count(), 1);
m_searchResult = m_entrySearcher.search("t:123", m_rootGroup);
QCOMPARE(m_searchResult.count(), 1);
m_searchResult = m_entrySearcher.search("password:testpass", m_rootGroup);
QCOMPARE(m_searchResult.count(), 1);
m_searchResult = m_entrySearcher.search("pw:testpass", m_rootGroup);
QCOMPARE(m_searchResult.count(), 1);
m_searchResult = m_entrySearcher.search("!user:email.com", m_rootGroup);
QCOMPARE(m_searchResult.count(), 5);
m_searchResult = m_entrySearcher.search("!u:email.com", m_rootGroup);
QCOMPARE(m_searchResult.count(), 5);
m_searchResult = m_entrySearcher.search("*user:\".*@.*\\.com\"", m_rootGroup);
QCOMPARE(m_searchResult.count(), 1);
m_searchResult = m_entrySearcher.search("+user:email", m_rootGroup);
QCOMPARE(m_searchResult.count(), 0);
// Terms are logical AND together
m_searchResult = m_entrySearcher.search("password:pass user:user", m_rootGroup);
QCOMPARE(m_searchResult.count(), 1);
// Parent group has search disabled
m_searchResult = m_entrySearcher.search("search term", group11);
QCOMPARE(m_searchResult.count(), 0);
}
void TestEntrySearcher::testAndConcatenationInSearch()
{
Entry* entry = new Entry();
entry->setNotes("abc def ghi");
entry->setTitle("jkl");
entry->setGroup(m_rootGroup);
m_searchResult = m_entrySearcher.search("", m_rootGroup);
QCOMPARE(m_searchResult.count(), 1);
m_searchResult = m_entrySearcher.search("def", m_rootGroup);
QCOMPARE(m_searchResult.count(), 1);
m_searchResult = m_entrySearcher.search(" abc ghi ", m_rootGroup);
QCOMPARE(m_searchResult.count(), 1);
m_searchResult = m_entrySearcher.search("ghi ef", m_rootGroup);
QCOMPARE(m_searchResult.count(), 1);
m_searchResult = m_entrySearcher.search("abc ef xyz", m_rootGroup);
QCOMPARE(m_searchResult.count(), 0);
m_searchResult = m_entrySearcher.search("abc kl", m_rootGroup);
QCOMPARE(m_searchResult.count(), 1);
}
void TestEntrySearcher::testAllAttributesAreSearched()
{
Entry* entry = new Entry();
entry->setGroup(m_rootGroup);
entry->setTitle("testTitle");
entry->setUsername("testUsername");
entry->setUrl("testUrl");
entry->setNotes("testNote");
// Default is to AND all terms together
m_searchResult = m_entrySearcher.search("testTitle testUsername testUrl testNote", m_rootGroup);
QCOMPARE(m_searchResult.count(), 1);
}
void TestEntrySearcher::testSearchTermParser()
{
// Test standard search terms
m_entrySearcher.parseSearchTerms("-test \"quoted \\\"string\\\"\" user:user pass:\"test me\" noquote ");
auto terms = m_entrySearcher.m_searchTerms;
QCOMPARE(terms.length(), 5);
QCOMPARE(terms[0].field, EntrySearcher::Field::Undefined);
QCOMPARE(terms[0].word, QString("test"));
QCOMPARE(terms[0].exclude, true);
QCOMPARE(terms[1].field, EntrySearcher::Field::Undefined);
QCOMPARE(terms[1].word, QString("quoted \"string\""));
QCOMPARE(terms[1].exclude, false);
QCOMPARE(terms[2].field, EntrySearcher::Field::Username);
QCOMPARE(terms[2].word, QString("user"));
QCOMPARE(terms[3].field, EntrySearcher::Field::Password);
QCOMPARE(terms[3].word, QString("test me"));
QCOMPARE(terms[4].field, EntrySearcher::Field::Undefined);
QCOMPARE(terms[4].word, QString("noquote"));
// Test wildcard and regex search terms
m_entrySearcher.parseSearchTerms("+url:*.google.com *user:\\d+\\w{2}");
terms = m_entrySearcher.m_searchTerms;
QCOMPARE(terms.length(), 2);
QCOMPARE(terms[0].field, EntrySearcher::Field::Url);
QCOMPARE(terms[0].regex.pattern(), QString("^(?:.*\\.google\\.com)$"));
QCOMPARE(terms[1].field, EntrySearcher::Field::Username);
QCOMPARE(terms[1].regex.pattern(), QString("\\d+\\w{2}"));
// Test custom attribute search terms
m_entrySearcher.parseSearchTerms("+_abc:efg _def:\"ddd\"");
terms = m_entrySearcher.m_searchTerms;
QCOMPARE(terms.length(), 2);
QCOMPARE(terms[0].field, EntrySearcher::Field::AttributeValue);
QCOMPARE(terms[0].word, QString("abc"));
QCOMPARE(terms[0].regex.pattern(), QString("^(?:efg)$"));
QCOMPARE(terms[1].field, EntrySearcher::Field::AttributeValue);
QCOMPARE(terms[1].word, QString("def"));
QCOMPARE(terms[1].regex.pattern(), QString("ddd"));
}
void TestEntrySearcher::testCustomAttributesAreSearched()
{
QScopedPointer<Entry> e1(new Entry());
e1->setGroup(m_rootGroup);
e1->attributes()->set("testAttribute", "testE1");
e1->attributes()->set("testProtected", "testP", true);
QScopedPointer<Entry> e2(new Entry());
e2->setGroup(m_rootGroup);
e2->attributes()->set("testAttribute", "testE2");
e2->attributes()->set("testProtected", "testP2", true);
// search for custom entries
m_searchResult = m_entrySearcher.search("_testAttribute:test", m_rootGroup);
QCOMPARE(m_searchResult.count(), 2);
// protected attributes are ignored
m_entrySearcher = EntrySearcher(false, true);
m_searchResult = m_entrySearcher.search("_testAttribute:test _testProtected:testP2", m_rootGroup);
QCOMPARE(m_searchResult.count(), 2);
}
void TestEntrySearcher::testGroup()
{
/**
* Root
* - group1 (1 entry)
* - subgroup1 (2 entries)
* - group2
* - subgroup2 (1 entry)
*/
Group* group1 = new Group();
Group* group2 = new Group();
group1->setParent(m_rootGroup);
group1->setName("group1");
group2->setParent(m_rootGroup);
group2->setName("group2");
Group* subgroup1 = new Group();
subgroup1->setName("subgroup1");
subgroup1->setParent(group1);
Group* subgroup2 = new Group();
subgroup2->setName("subgroup2");
subgroup2->setParent(group2);
Entry* eGroup1 = new Entry();
eGroup1->setTitle("Entry Group 1");
eGroup1->setGroup(group1);
Entry* eSub1 = new Entry();
eSub1->setTitle("test search term test");
eSub1->setGroup(subgroup1);
Entry* eSub2 = new Entry();
eSub2->setNotes("test test");
eSub2->setGroup(subgroup1);
Entry* eSub3 = new Entry();
eSub3->setNotes("test term test");
eSub3->setGroup(subgroup2);
m_searchResult = m_entrySearcher.search("group:subgroup", m_rootGroup);
QCOMPARE(m_searchResult.count(), 3);
m_searchResult = m_entrySearcher.search("g:subgroup1", m_rootGroup);
QCOMPARE(m_searchResult.count(), 2);
m_searchResult = m_entrySearcher.search("g:subgroup1 search", m_rootGroup);
QCOMPARE(m_searchResult.count(), 1);
m_searchResult = m_entrySearcher.search("g:*1/sub*1", m_rootGroup);
QCOMPARE(m_searchResult.count(), 2);
m_searchResult = m_entrySearcher.search("g:/group1 search", m_rootGroup);
QCOMPARE(m_searchResult.count(), 1);
}
void TestEntrySearcher::testSkipProtected()
{
QScopedPointer<Entry> e1(new Entry());
e1->setGroup(m_rootGroup);
e1->attributes()->set("testAttribute", "testE1");
e1->attributes()->set("testProtected", "apple", true);
QScopedPointer<Entry> e2(new Entry());
e2->setGroup(m_rootGroup);
e2->attributes()->set("testAttribute", "testE2");
e2->attributes()->set("testProtected", "banana", true);
const QList<Entry*> expectE1{e1.data()};
const QList<Entry*> expectE2{e2.data()};
const QList<Entry*> expectBoth{e1.data(), e2.data()};
// when not skipping protected, empty term matches everything
m_searchResult = m_entrySearcher.search("", m_rootGroup);
QCOMPARE(m_searchResult, expectBoth);
// now test the searcher with skipProtected = true
m_entrySearcher = EntrySearcher(false, true);
// when skipping protected, empty term matches nothing
m_searchResult = m_entrySearcher.search("", m_rootGroup);
QCOMPARE(m_searchResult, {});
// having a protected entry in terms should not affect the results in anyways
m_searchResult = m_entrySearcher.search("_testProtected:apple", m_rootGroup);
QCOMPARE(m_searchResult, {});
m_searchResult = m_entrySearcher.search("_testProtected:apple _testAttribute:testE2", m_rootGroup);
QCOMPARE(m_searchResult, expectE2);
m_searchResult = m_entrySearcher.search("_testProtected:apple _testAttribute:testE1", m_rootGroup);
QCOMPARE(m_searchResult, expectE1);
m_searchResult =
m_entrySearcher.search("_testProtected:apple _testAttribute:testE1 _testAttribute:testE2", m_rootGroup);
QCOMPARE(m_searchResult, {});
// also move the protected term around to execurise the short-circut logic
m_searchResult = m_entrySearcher.search("_testAttribute:testE2 _testProtected:apple", m_rootGroup);
QCOMPARE(m_searchResult, expectE2);
m_searchResult = m_entrySearcher.search("_testAttribute:testE1 _testProtected:apple", m_rootGroup);
QCOMPARE(m_searchResult, expectE1);
m_searchResult =
m_entrySearcher.search("_testAttribute:testE1 _testProtected:apple _testAttribute:testE2", m_rootGroup);
QCOMPARE(m_searchResult, {});
}
void TestEntrySearcher::testUUIDSearch()
{
auto entry1 = new Entry();
entry1->setGroup(m_rootGroup);
entry1->setTitle("testTitle");
auto uuid1 = QUuid::createUuid();
entry1->setUuid(uuid1);
auto entry2 = new Entry();
entry2->setGroup(m_rootGroup);
entry2->setTitle("testTitle2");
auto uuid2 = QUuid::createUuid();
entry2->setUuid(uuid2);
m_searchResult = m_entrySearcher.search("uuid:", m_rootGroup);
QCOMPARE(m_searchResult.count(), 2);
m_searchResult = m_entrySearcher.search("uuid:" + Tools::uuidToHex(uuid1), m_rootGroup);
QCOMPARE(m_searchResult.count(), 1);
}
|