Skip to content

Commit

Permalink
Add search 'by-path' url for browser
Browse files Browse the repository at this point in the history
  • Loading branch information
droidmonkey committed Oct 17, 2020
1 parent 0c5dd15 commit eb6f0eb
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/browser/BrowserService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1009,8 +1009,10 @@ bool BrowserService::removeFirstDomain(QString& hostname)
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;
if (url.startsWith("keepassxc://by-uuid/")) {
return url.endsWith("by-uuid/" + entry->uuidToHex());
} else if (url.startsWith("keepassxc://by-path/")) {
return url.endsWith("by-path/" + entry->path());
}
return handleURL(entry->url(), url, submitUrl);
}
Expand Down
7 changes: 7 additions & 0 deletions src/core/Entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,13 @@ QString Entry::totpSettingsString() const
return {};
}

QString Entry::path() const
{
auto path = group()->hierarchy();
path << title();
return path.mid(1).join("/");
}

void Entry::setUuid(const QUuid& uuid)
{
Q_ASSERT(!uuid.isNull());
Expand Down
1 change: 1 addition & 0 deletions src/core/Entry.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ class Entry : public QObject
QString totpSettingsString() const;
QSharedPointer<Totp::Settings> totpSettings() const;
int size() const;
QString path() const;

bool hasTotp() const;
bool isExpired() const;
Expand Down
38 changes: 38 additions & 0 deletions tests/TestBrowser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,43 @@ void TestBrowser::testSearchEntries()
QCOMPARE(result[3]->url(), QString("github.com/login"));
}

void TestBrowser::testSearchEntriesByPath()
{
auto db = QSharedPointer<Database>::create();
auto* root = db->rootGroup();

QStringList urlsRoot = {"https://root.example.com/", "root.example.com/login"};
auto entriesRoot = createEntries(urlsRoot, root);

auto* groupLevel1 = new Group();
groupLevel1->setParent(root);
groupLevel1->setName("TestGroup1");
QStringList urlsLevel1 = {"https://1.example.com/", "1.example.com/login"};
auto entriesLevel1 = createEntries(urlsLevel1, groupLevel1);

auto* groupLevel2 = new Group();
groupLevel2->setParent(groupLevel1);
groupLevel2->setName("TestGroup2");
QStringList urlsLevel2 = {"https://2.example.com/", "2.example.com/login"};
auto entriesLevel2 = createEntries(urlsLevel2, groupLevel2);

compareEntriesByPath(db, entriesRoot, "");
compareEntriesByPath(db, entriesLevel1, "TestGroup1/");
compareEntriesByPath(db, entriesLevel2, "TestGroup1/TestGroup2/");
}

void TestBrowser::compareEntriesByPath(QSharedPointer<Database> db, QList<Entry*> entries, QString path)
{
for (Entry* entry : entries) {
QString testUrl = "keepassxc://by-path/" + path + entry->title();
/* Look for an entry with that path. 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);
}
}

void TestBrowser::testSearchEntriesByUUID()
{
auto db = QSharedPointer<Database>::create();
Expand Down Expand Up @@ -461,6 +498,7 @@ QList<Entry*> TestBrowser::createEntries(QStringList& urls, Group* root) const
entry->setUrl(urls[i]);
entry->setUsername(QString("User %1").arg(i));
entry->setUuid(QUuid::createUuid());
entry->setTitle(QString("Name_%1").arg(entry->uuidToHex()));
entry->endUpdate();
entries.push_back(entry);
}
Expand Down
3 changes: 2 additions & 1 deletion tests/TestBrowser.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ private slots:
void testSortPriority();
void testSortPriority_data();
void testSearchEntries();
void testSearchEntriesByPath();
void testSearchEntriesByUUID();
void testSearchEntriesWithPort();
void testSearchEntriesWithAdditionalURLs();
Expand All @@ -54,9 +55,9 @@ private slots:

private:
QList<Entry*> createEntries(QStringList& urls, Group* root) const;
void compareEntriesByPath(QSharedPointer<Database> db, QList<Entry*> entries, QString path);

QScopedPointer<BrowserAction> m_browserAction;
QPointer<BrowserService> m_browserService;
};

#endif // KEEPASSXC_TESTBROWSER_H

0 comments on commit eb6f0eb

Please sign in to comment.