2014-05-15 17:48:54 -04:00
|
|
|
/*
|
|
|
|
* 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"
|
2018-01-24 07:22:20 -05:00
|
|
|
#include "TestGlobal.h"
|
2014-05-15 17:48:54 -04:00
|
|
|
|
|
|
|
QTEST_GUILESS_MAIN(TestEntrySearcher)
|
|
|
|
|
2018-11-17 09:55:57 -05:00
|
|
|
void TestEntrySearcher::init()
|
2014-05-15 17:48:54 -04:00
|
|
|
{
|
2018-03-25 16:24:30 -04:00
|
|
|
m_rootGroup = new Group();
|
2014-05-15 17:59:26 -04:00
|
|
|
}
|
2014-05-15 17:48:54 -04:00
|
|
|
|
2018-11-17 09:55:57 -05:00
|
|
|
void TestEntrySearcher::cleanup()
|
2014-05-15 17:59:26 -04:00
|
|
|
{
|
2018-03-25 16:24:30 -04:00
|
|
|
delete m_rootGroup;
|
2014-05-15 17:48:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestEntrySearcher::testSearch()
|
|
|
|
{
|
2018-03-25 16:24:30 -04:00
|
|
|
/**
|
|
|
|
* Root
|
|
|
|
* - group1 (search disabled)
|
|
|
|
* - group11
|
|
|
|
* - group2
|
|
|
|
* - group21
|
|
|
|
* - group211
|
|
|
|
* - group2111
|
|
|
|
*/
|
2014-05-15 17:48:54 -04:00
|
|
|
Group* group1 = new Group();
|
|
|
|
Group* group2 = new Group();
|
|
|
|
Group* group3 = new Group();
|
|
|
|
|
2018-03-25 16:24:30 -04:00
|
|
|
group1->setParent(m_rootGroup);
|
|
|
|
group2->setParent(m_rootGroup);
|
|
|
|
group3->setParent(m_rootGroup);
|
2014-05-15 17:48:54 -04:00
|
|
|
|
|
|
|
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();
|
2018-03-25 16:24:30 -04:00
|
|
|
eRoot->setTitle("test search term test");
|
|
|
|
eRoot->setGroup(m_rootGroup);
|
2014-05-15 17:48:54 -04:00
|
|
|
|
|
|
|
Entry* eRoot2 = new Entry();
|
|
|
|
eRoot2->setNotes("test term test");
|
2018-03-25 16:24:30 -04:00
|
|
|
eRoot2->setGroup(m_rootGroup);
|
2014-05-15 17:48:54 -04:00
|
|
|
|
2018-11-17 09:55:57 -05:00
|
|
|
// Searching is disabled for these
|
2014-05-15 17:48:54 -04:00
|
|
|
Entry* e1 = new Entry();
|
2018-03-25 16:24:30 -04:00
|
|
|
e1->setUsername("test search term test");
|
2014-05-15 17:48:54 -04:00
|
|
|
e1->setGroup(group1);
|
|
|
|
|
|
|
|
Entry* e11 = new Entry();
|
|
|
|
e11->setNotes("test search term test");
|
|
|
|
e11->setGroup(group11);
|
2018-11-17 09:55:57 -05:00
|
|
|
// End searching disabled
|
2014-05-15 17:48:54 -04:00
|
|
|
|
|
|
|
Entry* e2111 = new Entry();
|
2018-03-25 16:24:30 -04:00
|
|
|
e2111->setTitle("test search term test");
|
2014-05-15 17:48:54 -04:00
|
|
|
e2111->setGroup(group2111);
|
|
|
|
|
|
|
|
Entry* e2111b = new Entry();
|
|
|
|
e2111b->setNotes("test search test");
|
2018-11-17 09:55:57 -05:00
|
|
|
e2111b->setUsername("user123");
|
2018-03-25 16:24:30 -04:00
|
|
|
e2111b->setPassword("testpass");
|
2014-05-15 17:48:54 -04:00
|
|
|
e2111b->setGroup(group2111);
|
|
|
|
|
|
|
|
Entry* e3 = new Entry();
|
2018-03-25 16:24:30 -04:00
|
|
|
e3->setUrl("test search term test");
|
2014-05-15 17:48:54 -04:00
|
|
|
e3->setGroup(group3);
|
|
|
|
|
|
|
|
Entry* e3b = new Entry();
|
2018-03-25 16:24:30 -04:00
|
|
|
e3b->setTitle("test search test");
|
2018-11-17 09:55:57 -05:00
|
|
|
e3b->setUsername("test@email.com");
|
2018-03-25 16:24:30 -04:00
|
|
|
e3b->setPassword("realpass");
|
2014-05-15 17:48:54 -04:00
|
|
|
e3b->setGroup(group3);
|
|
|
|
|
2018-11-17 09:55:57 -05:00
|
|
|
// Simple search term testing
|
2018-03-25 16:24:30 -04:00
|
|
|
m_searchResult = m_entrySearcher.search("search", m_rootGroup);
|
|
|
|
QCOMPARE(m_searchResult.count(), 5);
|
2014-05-15 17:48:54 -04:00
|
|
|
|
2018-03-25 16:24:30 -04:00
|
|
|
m_searchResult = m_entrySearcher.search("search term", m_rootGroup);
|
|
|
|
QCOMPARE(m_searchResult.count(), 3);
|
|
|
|
|
|
|
|
m_searchResult = m_entrySearcher.search("search term", group211);
|
|
|
|
QCOMPARE(m_searchResult.count(), 1);
|
|
|
|
|
2018-11-17 09:55:57 -05:00
|
|
|
// Test advanced search terms
|
2018-03-25 16:24:30 -04:00
|
|
|
m_searchResult = m_entrySearcher.search("password:testpass", m_rootGroup);
|
2014-05-15 18:26:09 -04:00
|
|
|
QCOMPARE(m_searchResult.count(), 1);
|
2014-05-15 17:48:54 -04:00
|
|
|
|
2018-11-17 09:55:57 -05:00
|
|
|
m_searchResult = m_entrySearcher.search("!user: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);
|
|
|
|
|
2018-03-25 16:24:30 -04:00
|
|
|
// Parent group has search disabled
|
|
|
|
m_searchResult = m_entrySearcher.search("search term", group11);
|
2014-05-15 18:26:09 -04:00
|
|
|
QCOMPARE(m_searchResult.count(), 0);
|
2014-05-15 17:48:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestEntrySearcher::testAndConcatenationInSearch()
|
|
|
|
{
|
|
|
|
Entry* entry = new Entry();
|
|
|
|
entry->setNotes("abc def ghi");
|
|
|
|
entry->setTitle("jkl");
|
2018-03-25 16:24:30 -04:00
|
|
|
entry->setGroup(m_rootGroup);
|
2014-05-15 17:48:54 -04:00
|
|
|
|
2018-03-25 16:24:30 -04:00
|
|
|
m_searchResult = m_entrySearcher.search("", m_rootGroup);
|
2014-05-15 18:26:09 -04:00
|
|
|
QCOMPARE(m_searchResult.count(), 1);
|
2014-05-15 17:48:54 -04:00
|
|
|
|
2018-03-25 16:24:30 -04:00
|
|
|
m_searchResult = m_entrySearcher.search("def", m_rootGroup);
|
2014-05-15 18:26:09 -04:00
|
|
|
QCOMPARE(m_searchResult.count(), 1);
|
2014-05-15 17:48:54 -04:00
|
|
|
|
2018-03-25 16:24:30 -04:00
|
|
|
m_searchResult = m_entrySearcher.search(" abc ghi ", m_rootGroup);
|
2014-05-15 18:26:09 -04:00
|
|
|
QCOMPARE(m_searchResult.count(), 1);
|
2014-05-15 17:48:54 -04:00
|
|
|
|
2018-03-25 16:24:30 -04:00
|
|
|
m_searchResult = m_entrySearcher.search("ghi ef", m_rootGroup);
|
2014-05-15 18:26:09 -04:00
|
|
|
QCOMPARE(m_searchResult.count(), 1);
|
2014-05-15 17:48:54 -04:00
|
|
|
|
2018-03-25 16:24:30 -04:00
|
|
|
m_searchResult = m_entrySearcher.search("abc ef xyz", m_rootGroup);
|
2014-05-15 18:26:09 -04:00
|
|
|
QCOMPARE(m_searchResult.count(), 0);
|
2014-05-15 17:48:54 -04:00
|
|
|
|
2018-03-25 16:24:30 -04:00
|
|
|
m_searchResult = m_entrySearcher.search("abc kl", m_rootGroup);
|
2014-05-15 18:26:09 -04:00
|
|
|
QCOMPARE(m_searchResult.count(), 1);
|
2014-05-15 17:48:54 -04:00
|
|
|
}
|
2014-05-16 04:51:22 -04:00
|
|
|
|
|
|
|
void TestEntrySearcher::testAllAttributesAreSearched()
|
|
|
|
{
|
|
|
|
Entry* entry = new Entry();
|
2018-03-25 16:24:30 -04:00
|
|
|
entry->setGroup(m_rootGroup);
|
2014-05-16 04:51:22 -04:00
|
|
|
|
|
|
|
entry->setTitle("testTitle");
|
|
|
|
entry->setUsername("testUsername");
|
|
|
|
entry->setUrl("testUrl");
|
|
|
|
entry->setNotes("testNote");
|
|
|
|
|
2018-03-25 16:24:30 -04:00
|
|
|
// Default is to AND all terms together
|
|
|
|
m_searchResult = m_entrySearcher.search("testTitle testUsername testUrl testNote", m_rootGroup);
|
2014-05-16 04:51:22 -04:00
|
|
|
QCOMPARE(m_searchResult.count(), 1);
|
|
|
|
}
|
2018-03-25 16:24:30 -04:00
|
|
|
|
|
|
|
void TestEntrySearcher::testSearchTermParser()
|
|
|
|
{
|
|
|
|
// Test standard search terms
|
2019-01-20 09:50:20 -05:00
|
|
|
auto terms =
|
|
|
|
m_entrySearcher.parseSearchTerms("-test \"quoted \\\"string\\\"\" user:user pass:\"test me\" noquote ");
|
2018-03-25 16:24:30 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
terms = m_entrySearcher.parseSearchTerms("+url:*.google.com *user:\\d+\\w{2}");
|
|
|
|
|
|
|
|
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}"));
|
|
|
|
}
|