From 28f1cd398db52f5a9daff379b21c248bf8c8af10 Mon Sep 17 00:00:00 2001 From: mattesony <49170923+mattesony@users.noreply.github.com> Date: Tue, 1 Nov 2022 14:05:38 -0700 Subject: [PATCH] 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. --- tests/CMakeLists.txt | 2 +- tests/TestEntry.cpp | 38 ++++++++++++++++++++++++++++++++++++++ tests/TestEntry.h | 1 + 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index db82da163..06fea2107 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -168,7 +168,7 @@ if(WITH_XC_SSHAGENT) endif() add_unit_test(NAME testentry SOURCES TestEntry.cpp - LIBS ${TEST_LIBRARIES}) + LIBS testsupport ${TEST_LIBRARIES}) add_unit_test(NAME testmerge SOURCES TestMerge.cpp LIBS testsupport ${TEST_LIBRARIES}) diff --git a/tests/TestEntry.cpp b/tests/TestEntry.cpp index 07519d94b..ffdf6cb51 100644 --- a/tests/TestEntry.cpp +++ b/tests/TestEntry.cpp @@ -19,6 +19,8 @@ #include #include "TestEntry.h" +#include "mock/MockClock.h" + #include "core/Clock.h" #include "core/Group.h" #include "core/Metadata.h" @@ -47,6 +49,42 @@ void TestEntry::testHistoryItemDeletion() 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 historyEntry = new Entry(); + historyEntry->setPassword("oldpassword"); + m_clock->advanceSecond(500); + QScopedPointer entry(new Entry()); + entry->setPassword("newpassword"); + entry->addHistoryItem(historyEntry); + m_clock->advanceSecond(100); + + QCOMPARE(entry->getPasswordAge(), 100); + + QPointer historyEntry2 = new Entry(); + historyEntry2->setPassword("oldpassword"); + m_clock->advanceSecond(500); + QScopedPointer 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() { QScopedPointer entry(new Entry()); diff --git a/tests/TestEntry.h b/tests/TestEntry.h index 3bfd8f52d..fa00fd508 100644 --- a/tests/TestEntry.h +++ b/tests/TestEntry.h @@ -29,6 +29,7 @@ class TestEntry : public QObject private slots: void initTestCase(); void testHistoryItemDeletion(); + void testGetPasswordAge(); void testCopyDataFrom(); void testClone(); void testResolveUrl();