Fixed tests and improved age logic

This commit is contained in:
mattesony 2022-11-01 17:09:38 -07:00
parent 28f1cd398d
commit c14725cf97
2 changed files with 31 additions and 17 deletions

View file

@ -235,27 +235,25 @@ int Entry::getPasswordAge() const
{ {
QListIterator<Entry*> i(m_history); QListIterator<Entry*> i(m_history);
i.toBack(); i.toBack();
Entry* compare = nullptr; const Entry* curr = nullptr;
Entry* curr = nullptr; const Entry* previous = this;
while (i.hasPrevious()) { while (i.hasPrevious()) {
curr = i.previous(); curr = previous;
if (!compare) { previous = i.previous();
compare = curr; if (previous->password() != curr->password()) {
continue; // Found last change in history
} return curr->timeInfo().lastModificationTime().secsTo(Clock::currentDateTime());
if (*curr->attributes() != *compare->attributes()) {
if (curr->password() != compare->password()) {
// Found most recent password change; break out.
break;
}
} }
} }
if (!curr) { if (previous!=this) {
// If no change in history, password is from creation time // If no change in history, password is from oldest history entry.
return this->timeInfo().creationTime().secsTo(QDateTime::currentDateTime()); // 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 bool Entry::excludeFromReports() const

View file

@ -79,9 +79,25 @@ void TestEntry::testGetPasswordAge()
entry2->addHistoryItem(historyEntry2); entry2->addHistoryItem(historyEntry2);
// History entry shows password is actually // 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); QCOMPARE(entry2->getPasswordAge(), 600);
// Bury password change in history
entry2->setPassword("newpassword");
QPointer<Entry> historyEntry3 = new Entry();
historyEntry3->setPassword("newpassword");
entry->addHistoryItem(historyEntry3);
m_clock->advanceSecond(400);
QPointer<Entry> 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(); MockClock::teardown();
} }