diff --git a/src/core/Entry.cpp b/src/core/Entry.cpp index 3679ee71e..4d997e4ab 100644 --- a/src/core/Entry.cpp +++ b/src/core/Entry.cpp @@ -235,27 +235,25 @@ int Entry::getPasswordAge() const { QListIterator i(m_history); i.toBack(); - Entry* compare = nullptr; - Entry* curr = nullptr; + const Entry* curr = nullptr; + const Entry* previous = this; while (i.hasPrevious()) { - curr = i.previous(); - if (!compare) { - compare = curr; - continue; - } - if (*curr->attributes() != *compare->attributes()) { - if (curr->password() != compare->password()) { - // Found most recent password change; break out. - break; - } + curr = previous; + previous = i.previous(); + if (previous->password() != curr->password()) { + // Found last change in history + return curr->timeInfo().lastModificationTime().secsTo(Clock::currentDateTime()); } + } - if (!curr) { - // If no change in history, password is from creation time - return this->timeInfo().creationTime().secsTo(QDateTime::currentDateTime()); + if (previous!=this) { + // If no change in history, password is from oldest history entry. + // Not using creation time here because that changes when an entry is cloned + return previous->timeInfo().lastModificationTime().secsTo(Clock::currentDateTime()); } - return curr->timeInfo().lastModificationTime().secsTo(QDateTime::currentDateTime()); + // If no history, creation time is when the password appeared + return this->timeInfo().creationTime().secsTo(Clock::currentDateTime()); } bool Entry::excludeFromReports() const diff --git a/tests/TestEntry.cpp b/tests/TestEntry.cpp index ffdf6cb51..ea44ee6a4 100644 --- a/tests/TestEntry.cpp +++ b/tests/TestEntry.cpp @@ -79,9 +79,25 @@ void TestEntry::testGetPasswordAge() entry2->addHistoryItem(historyEntry2); // History entry shows password is actually - // 500 seconds older than the most recent update + // 500 seconds older than the creation time QCOMPARE(entry2->getPasswordAge(), 600); + // Bury password change in history + entry2->setPassword("newpassword"); + QPointer historyEntry3 = new Entry(); + historyEntry3->setPassword("newpassword"); + entry->addHistoryItem(historyEntry3); + m_clock->advanceSecond(400); + QPointer historyEntry4 = new Entry(); + historyEntry4->setPassword("newpassword"); + entry->addHistoryItem(historyEntry4); + m_clock->advanceSecond(400); + QCOMPARE(entry2->getPasswordAge(), 800); + + // Second test where current password is the latest + entry2->setPassword("newerpassword"); + QCOMPARE(entry2->getPasswordAge(), 0); + MockClock::teardown(); }