Add browser service search for entries via UUID

This commit is contained in:
piegames 2020-05-20 14:53:42 +02:00 committed by Jonathan White
parent f947c96462
commit 6a35bbea2f
4 changed files with 65 additions and 2 deletions

View File

@ -597,7 +597,7 @@ BrowserService::searchEntries(const QSharedPointer<Database>& db, const QString&
} }
} }
if (!handleURL(entry->url(), url, submitUrl)) { if (!handleEntry(entry, url, submitUrl)) {
continue; continue;
} }
@ -1004,6 +1004,17 @@ bool BrowserService::removeFirstDomain(QString& hostname)
return false; return false;
} }
/* Test if a search URL matches a custom entry. If the URL has the schema "keepassxc", some special checks will be made.
* Otherwise, this simply delegates to handleURL(). */
bool BrowserService::handleEntry(Entry* entry, const QString& url, const QString& submitUrl)
{
// Use this special scheme to find entries by UUID
if (url.startsWith("keepassxc://")) {
return ("keepassxc://by-uuid/" + entry->uuidToHex()) == url;
}
return handleURL(entry->url(), url, submitUrl);
}
bool BrowserService::handleURL(const QString& entryUrl, const QString& url, const QString& submitUrl) bool BrowserService::handleURL(const QString& entryUrl, const QString& url, const QString& submitUrl)
{ {
if (entryUrl.isEmpty()) { if (entryUrl.isEmpty()) {
@ -1022,7 +1033,7 @@ bool BrowserService::handleURL(const QString& entryUrl, const QString& url, cons
} }
// Make a direct compare if a local file is used // Make a direct compare if a local file is used
if (url.contains("file://")) { if (url.startsWith("file://")) {
return entryUrl == submitUrl; return entryUrl == submitUrl;
} }

View File

@ -139,6 +139,7 @@ private:
const QString& fullUrl) const; const QString& fullUrl) const;
bool schemeFound(const QString& url); bool schemeFound(const QString& url);
bool removeFirstDomain(QString& hostname); bool removeFirstDomain(QString& hostname);
bool handleEntry(Entry* entry, const QString& url, const QString& submitUrl);
bool handleURL(const QString& entryUrl, const QString& url, const QString& submitUrl); bool handleURL(const QString& entryUrl, const QString& url, const QString& submitUrl);
QString baseDomain(const QString& hostname) const; QString baseDomain(const QString& hostname) const;
QSharedPointer<Database> getDatabase(); QSharedPointer<Database> getDatabase();

View File

@ -223,6 +223,55 @@ void TestBrowser::testSearchEntries()
QCOMPARE(result[3]->url(), QString("github.com/login")); QCOMPARE(result[3]->url(), QString("github.com/login"));
} }
void TestBrowser::testSearchEntriesByUUID()
{
auto db = QSharedPointer<Database>::create();
auto* root = db->rootGroup();
/* The URLs don't really matter for this test, we just need some entries */
QStringList urls = {"https://github.com/login_page",
"https://github.com/login",
"https://github.com/",
"github.com/login",
"http://github.com",
"http://github.com/login",
"github.com",
"github.com/login",
"https://github",
"github.com",
"",
"not an URL"};
auto entries = createEntries(urls, root);
for (Entry* entry : entries) {
QString testUrl = "keepassxc://by-uuid/" + entry->uuidToHex();
/* Look for an entry with that UUID. First using handleEntry, then through the search */
QCOMPARE(m_browserService->handleEntry(entry, testUrl, ""), true);
auto result = m_browserService->searchEntries(db, testUrl, "");
QCOMPARE(result.length(), 1);
QCOMPARE(result[0], entry);
}
/* Test for entries that don't exist */
QStringList uuids = {"00000000000000000000000000000000",
"00000000000000000000000000000001",
"00000000000000000000000000000002/",
"invalid uuid",
"000000000000000000000000000000000000000"
"00000000000000000000000"};
for (QString uuid : uuids) {
QString testUrl = "keepassxc://by-uuid/" + uuid;
for (Entry* entry : entries) {
QCOMPARE(m_browserService->handleEntry(entry, testUrl, ""), false);
}
auto result = m_browserService->searchEntries(db, testUrl, "");
QCOMPARE(result.length(), 0);
}
}
void TestBrowser::testSearchEntriesWithPort() void TestBrowser::testSearchEntriesWithPort()
{ {
auto db = QSharedPointer<Database>::create(); auto db = QSharedPointer<Database>::create();
@ -419,6 +468,7 @@ QList<Entry*> TestBrowser::createEntries(QStringList& urls, Group* root) const
entry->beginUpdate(); entry->beginUpdate();
entry->setUrl(urls[i]); entry->setUrl(urls[i]);
entry->setUsername(QString("User %1").arg(i)); entry->setUsername(QString("User %1").arg(i));
entry->setUuid(QUuid::createUuid());
entry->endUpdate(); entry->endUpdate();
entries.push_back(entry); entries.push_back(entry);
} }

View File

@ -41,6 +41,7 @@ private slots:
void testBaseDomain(); void testBaseDomain();
void testSortPriority(); void testSortPriority();
void testSearchEntries(); void testSearchEntries();
void testSearchEntriesByUUID();
void testSearchEntriesWithPort(); void testSearchEntriesWithPort();
void testSearchEntriesWithAdditionalURLs(); void testSearchEntriesWithAdditionalURLs();
void testInvalidEntries(); void testInvalidEntries();