From d4d0f4c131a1e8cba473e1142ef6a577db17376a Mon Sep 17 00:00:00 2001 From: Martin Unzner Date: Sun, 12 Nov 2017 18:11:52 +0100 Subject: [PATCH 1/2] Add failing test for complete entry resolution --- tests/TestEntry.cpp | 82 +++++++++++++++++++++++++++++++++++++++++++++ tests/TestEntry.h | 1 + 2 files changed, 83 insertions(+) diff --git a/tests/TestEntry.cpp b/tests/TestEntry.cpp index 90da71d56..802a27b72 100644 --- a/tests/TestEntry.cpp +++ b/tests/TestEntry.cpp @@ -344,3 +344,85 @@ void TestEntry::testResolveReferencePlaceholders() QCOMPARE(tstEntry->resolveMultiplePlaceholders(QString("{REF:A@I:%1}").arg(entry3->uuid().toHex())), QString("UrlValue")); QCOMPARE(tstEntry->resolveMultiplePlaceholders(QString("{REF:N@I:%1}").arg(entry3->uuid().toHex())), QString("NotesValue")); } + +static const char placeholders[] = { +'T', +'U', +'P', +'A', +'N', +}; + +void TestEntry::testResolveNonIdPlaceholdersToUuid() +{ + Database db; + auto* root = db.rootGroup(); + + Entry referencedEntryTitle; + referencedEntryTitle.setGroup(root); + referencedEntryTitle.setTitle("myTitle"); + referencedEntryTitle.setUuid(Uuid::random()); + + Entry referencedEntryUsername; + referencedEntryUsername.setGroup(root); + referencedEntryUsername.setUsername("myUser"); + referencedEntryUsername.setUuid(Uuid::random()); + + Entry referencedEntryPassword; + referencedEntryPassword.setGroup(root); + referencedEntryPassword.setPassword("myPassword"); + referencedEntryPassword.setUuid(Uuid::random()); + + Entry referencedEntryUrl; + referencedEntryUrl.setGroup(root); + referencedEntryUrl.setUrl("myUrl"); + referencedEntryUrl.setUuid(Uuid::random()); + + Entry referencedEntryNotes; + referencedEntryNotes.setGroup(root); + referencedEntryNotes.setNotes("myNotes"); + referencedEntryNotes.setUuid(Uuid::random()); + + for (const auto searchIn : placeholders) { + const Entry* referencedEntry = nullptr; + QString newEntryNotesRaw("{REF:I@%1:%2}"); + + switch(searchIn) { + case 'T': + referencedEntry = &referencedEntryTitle; + newEntryNotesRaw = newEntryNotesRaw.arg( + QString(searchIn), referencedEntry->title()); + break; + case 'U': + referencedEntry = &referencedEntryUsername; + newEntryNotesRaw = newEntryNotesRaw.arg( + QString(searchIn), referencedEntry->username()); + break; + case 'P': + referencedEntry = &referencedEntryPassword; + newEntryNotesRaw = newEntryNotesRaw.arg( + QString(searchIn), referencedEntry->password()); + break; + case 'A': + referencedEntry = &referencedEntryUrl; + newEntryNotesRaw = newEntryNotesRaw.arg( + QString(searchIn), referencedEntry->url()); + break; + case 'N': + referencedEntry = &referencedEntryNotes; + newEntryNotesRaw = newEntryNotesRaw.arg( + QString(searchIn), referencedEntry->notes()); + break; + default: + break; + } + + Entry newEntry; + newEntry.setGroup(root); + newEntry.setNotes(newEntryNotesRaw); + + const auto newEntryNotesResolved = + newEntry.resolveMultiplePlaceholders(newEntry.notes()); + QCOMPARE(newEntryNotesResolved, QString(referencedEntry->uuid().toHex())); + } +} diff --git a/tests/TestEntry.h b/tests/TestEntry.h index c6a72b05c..44a371ba6 100644 --- a/tests/TestEntry.h +++ b/tests/TestEntry.h @@ -35,6 +35,7 @@ private slots: void testResolveUrlPlaceholders(); void testResolveRecursivePlaceholders(); void testResolveReferencePlaceholders(); + void testResolveNonIdPlaceholdersToUuid(); }; #endif // KEEPASSX_TESTENTRY_H From b44092ac43478af6fb80593e5b278ab08278359a Mon Sep 17 00:00:00 2001 From: Martin Unzner Date: Sun, 12 Nov 2017 18:58:42 +0100 Subject: [PATCH 2/2] Add capability to resolve UUID --- src/core/Entry.cpp | 2 ++ src/core/EntryAttributes.cpp | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/core/Entry.cpp b/src/core/Entry.cpp index 05f24b5a1..65e2454e2 100644 --- a/src/core/Entry.cpp +++ b/src/core/Entry.cpp @@ -838,6 +838,8 @@ QString Entry::referenceFieldValue(EntryReferenceType referenceType) const return url(); case EntryReferenceType::Notes: return notes(); + case EntryReferenceType::Uuid: + return uuid().toHex(); default: break; } diff --git a/src/core/EntryAttributes.cpp b/src/core/EntryAttributes.cpp index b1b4fad6a..21dcccf6e 100644 --- a/src/core/EntryAttributes.cpp +++ b/src/core/EntryAttributes.cpp @@ -34,7 +34,7 @@ const QString EntryAttributes::RememberCmdExecAttr = "_EXEC_CMD"; EntryAttributes::EntryAttributes(QObject* parent) : QObject(parent) - , m_referenceRegExp("\\{REF:(?[TUPAN])@(?[TUPANIO]):(?[^}]+)\\}", + , m_referenceRegExp("\\{REF:(?[TUPANI])@(?[TUPANIO]):(?[^}]+)\\}", QRegularExpression::CaseInsensitiveOption) { clear();