getPasswordAge unit tests.

Tests fail because QDateTime::currentDateTime() is not using the MockClock instance.
The time is correct, but as it changes every time it makes the test non-deterministic.
This commit is contained in:
mattesony 2022-11-01 14:05:38 -07:00
parent d85f3a2b82
commit 28f1cd398d
3 changed files with 40 additions and 1 deletions

View File

@ -168,7 +168,7 @@ if(WITH_XC_SSHAGENT)
endif() endif()
add_unit_test(NAME testentry SOURCES TestEntry.cpp add_unit_test(NAME testentry SOURCES TestEntry.cpp
LIBS ${TEST_LIBRARIES}) LIBS testsupport ${TEST_LIBRARIES})
add_unit_test(NAME testmerge SOURCES TestMerge.cpp add_unit_test(NAME testmerge SOURCES TestMerge.cpp
LIBS testsupport ${TEST_LIBRARIES}) LIBS testsupport ${TEST_LIBRARIES})

View File

@ -19,6 +19,8 @@
#include <QTest> #include <QTest>
#include "TestEntry.h" #include "TestEntry.h"
#include "mock/MockClock.h"
#include "core/Clock.h" #include "core/Clock.h"
#include "core/Group.h" #include "core/Group.h"
#include "core/Metadata.h" #include "core/Metadata.h"
@ -47,6 +49,42 @@ void TestEntry::testHistoryItemDeletion()
QVERIFY(historyEntry.isNull()); QVERIFY(historyEntry.isNull());
} }
void TestEntry::testGetPasswordAge()
{
MockClock* m_clock = new MockClock(2022, 2, 4, 17, 00, 00);
MockClock::setup(m_clock);
// Old password updated 100 seconds ago
QPointer<Entry> historyEntry = new Entry();
historyEntry->setPassword("oldpassword");
m_clock->advanceSecond(500);
QScopedPointer<Entry> entry(new Entry());
entry->setPassword("newpassword");
entry->addHistoryItem(historyEntry);
m_clock->advanceSecond(100);
QCOMPARE(entry->getPasswordAge(), 100);
QPointer<Entry> historyEntry2 = new Entry();
historyEntry2->setPassword("oldpassword");
m_clock->advanceSecond(500);
QScopedPointer<Entry> entry2(new Entry());
entry2->setPassword("oldpassword");
// No history, password just created
QCOMPARE(entry2->getPasswordAge(), 0);
m_clock->advanceSecond(100);
// 100 seconds pass since creation
QCOMPARE(entry2->getPasswordAge(), 100);
entry2->addHistoryItem(historyEntry2);
// History entry shows password is actually
// 500 seconds older than the most recent update
QCOMPARE(entry2->getPasswordAge(), 600);
MockClock::teardown();
}
void TestEntry::testCopyDataFrom() void TestEntry::testCopyDataFrom()
{ {
QScopedPointer<Entry> entry(new Entry()); QScopedPointer<Entry> entry(new Entry());

View File

@ -29,6 +29,7 @@ class TestEntry : public QObject
private slots: private slots:
void initTestCase(); void initTestCase();
void testHistoryItemDeletion(); void testHistoryItemDeletion();
void testGetPasswordAge();
void testCopyDataFrom(); void testCopyDataFrom();
void testClone(); void testClone();
void testResolveUrl(); void testResolveUrl();