From 9a7db370bf9d1e58bb2435c37cbeb064242892c3 Mon Sep 17 00:00:00 2001 From: vuurvlieg Date: Tue, 26 Mar 2024 16:52:34 +0100 Subject: [PATCH] Add and adjust test-cases --- tests/CMakeLists.txt | 2 +- tests/TestEntry.cpp | 56 ++++++++++++++++++++++++++++++++++++++++++++ tests/TestEntry.h | 3 +++ tests/TestGroup.cpp | 55 ++++++++++++++++++++++++++++++++++++++++--- tests/TestGroup.h | 1 + 5 files changed, 113 insertions(+), 4 deletions(-) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 4b26d38bc..19761378d 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 eac09f832..ec7969d80 100644 --- a/tests/TestEntry.cpp +++ b/tests/TestEntry.cpp @@ -25,13 +25,34 @@ #include "core/TimeInfo.h" #include "crypto/Crypto.h" +#include "mock/MockClock.h" + QTEST_GUILESS_MAIN(TestEntry) +namespace +{ + MockClock* m_clock = nullptr; +} + void TestEntry::initTestCase() { QVERIFY(Crypto::init()); } +void TestEntry::init() +{ + Q_ASSERT(m_clock == nullptr); + m_clock = new MockClock(2010, 5, 5, 10, 30, 10); + MockClock::setup(m_clock); +} + +void TestEntry::cleanup() +{ + MockClock::teardown(); + m_clock = nullptr; +} + + void TestEntry::testHistoryItemDeletion() { QScopedPointer entry(new Entry()); @@ -109,6 +130,8 @@ void TestEntry::testClone() QCOMPARE(entryCloneNewUuid->timeInfo().creationTime(), entryOrg->timeInfo().creationTime()); // Reset modification time + entryOrgTime.setLastAccessTime(Clock::datetimeUtc(60)); + entryOrgTime.setLocationChanged(Clock::datetimeUtc(60)); entryOrgTime.setLastModificationTime(Clock::datetimeUtc(60)); entryOrg->setTimeInfo(entryOrgTime); @@ -122,7 +145,10 @@ void TestEntry::testClone() QCOMPARE(entryCloneResetTime->uuid(), entryOrg->uuid()); QCOMPARE(entryCloneResetTime->title(), QString("New Title")); QCOMPARE(entryCloneResetTime->historyItems().size(), 0); + // Cloning with CloneResetTimeInfo should affect the CreationTime, LocationChanged, LastAccessTime QVERIFY(entryCloneResetTime->timeInfo().creationTime() != entryOrg->timeInfo().creationTime()); + QVERIFY(entryCloneResetTime->timeInfo().locationChanged() != entryOrg->timeInfo().locationChanged()); + QVERIFY(entryCloneResetTime->timeInfo().lastAccessTime() != entryOrg->timeInfo().lastAccessTime()); // Cloning with CloneResetTimeInfo should not affect the LastModificationTime QCOMPARE(entryCloneResetTime->timeInfo().lastModificationTime(), entryOrg->timeInfo().lastModificationTime()); @@ -770,3 +796,33 @@ void TestEntry::testPreviousParentGroup() QVERIFY(entry->previousParentGroupUuid() == group1->uuid()); QVERIFY(entry->previousParentGroup() == group1); } + +void TestEntry::testTimeinfoChanges() +{ + Database db; + auto* root = db.rootGroup(); + auto* subgroup = new Group(); + subgroup->setUuid(QUuid::createUuid()); + subgroup->setParent(root); + QDateTime startTime = Clock::currentDateTimeUtc(); + TimeInfo startTimeinfo; + startTimeinfo.setCreationTime(startTime); + startTimeinfo.setLastModificationTime(startTime); + startTimeinfo.setLocationChanged(startTime); + startTimeinfo.setLastAccessTime(startTime); + m_clock->advanceMinute(1); + + QScopedPointer entry(new Entry()); + entry->setUuid(QUuid::createUuid()); + entry->setGroup(root); + entry->setTimeInfo(startTimeinfo); + entry->setPreviousParentGroup(subgroup); + // setting previous parent group should not affect the LastModificationTime + QCOMPARE(entry->timeInfo().lastModificationTime(), startTime); + entry->setGroup(subgroup); + // changing group should not affect LastModicationTime, CreationTime + QCOMPARE(entry->timeInfo().creationTime(), startTime); + QCOMPARE(entry->timeInfo().lastModificationTime(), startTime); + // changing group should affect the LocationChanged time + QCOMPARE(entry->timeInfo().locationChanged(), Clock::currentDateTimeUtc()); +} diff --git a/tests/TestEntry.h b/tests/TestEntry.h index 3bfd8f52d..ac2259b6d 100644 --- a/tests/TestEntry.h +++ b/tests/TestEntry.h @@ -28,6 +28,8 @@ class TestEntry : public QObject private slots: void initTestCase(); + void init(); + void cleanup(); void testHistoryItemDeletion(); void testCopyDataFrom(); void testClone(); @@ -40,6 +42,7 @@ private slots: void testIsRecycled(); void testMoveUpDown(); void testPreviousParentGroup(); + void testTimeinfoChanges(); }; #endif // KEEPASSX_TESTENTRY_H diff --git a/tests/TestGroup.cpp b/tests/TestGroup.cpp index 22807c878..cd1dcd7d6 100644 --- a/tests/TestGroup.cpp +++ b/tests/TestGroup.cpp @@ -382,18 +382,21 @@ void TestGroup::testClone() QCOMPARE(clonedGroup->iconNumber(), 42); QCOMPARE(clonedGroup->children().size(), 1); QCOMPARE(clonedGroup->entries().size(), 1); + QCOMPARE(clonedGroup->timeInfo(), originalGroup->timeInfo()); Entry* clonedGroupEntry = clonedGroup->entries().at(0); QVERIFY(clonedGroupEntry->uuid() != originalGroupEntry->uuid()); QCOMPARE(clonedGroupEntry->title(), QString("GroupEntry")); QCOMPARE(clonedGroupEntry->iconNumber(), 43); QCOMPARE(clonedGroupEntry->historyItems().size(), 0); + QCOMPARE(clonedGroupEntry->timeInfo(), originalGroupEntry->timeInfo()); Group* clonedSubGroup = clonedGroup->children().at(0); QVERIFY(clonedSubGroup->uuid() != subGroup->uuid()); QCOMPARE(clonedSubGroup->name(), QString("SubGroup")); QCOMPARE(clonedSubGroup->children().size(), 0); QCOMPARE(clonedSubGroup->entries().size(), 1); + QCOMPARE(clonedSubGroup->timeInfo(), subGroup->timeInfo()); Entry* clonedSubGroupEntry = clonedSubGroup->entries().at(0); QVERIFY(clonedSubGroupEntry->uuid() != subGroupEntry->uuid()); @@ -411,15 +414,17 @@ void TestGroup::testClone() QCOMPARE(clonedGroupNewUuid->entries().size(), 0); QVERIFY(clonedGroupNewUuid->uuid() != originalGroup->uuid()); - // Making sure the new modification date is not the same. + // Verify Timeinfo modifications for CloneResetTimeInfo m_clock->advanceSecond(1); QScopedPointer clonedGroupResetTimeInfo( originalGroup->clone(Entry::CloneNoFlags, Group::CloneNewUuid | Group::CloneResetTimeInfo)); QCOMPARE(clonedGroupResetTimeInfo->entries().size(), 0); QVERIFY(clonedGroupResetTimeInfo->uuid() != originalGroup->uuid()); - QVERIFY(clonedGroupResetTimeInfo->timeInfo().lastModificationTime() - != originalGroup->timeInfo().lastModificationTime()); + QVERIFY(clonedGroupResetTimeInfo->timeInfo().creationTime() != originalGroup->timeInfo().creationTime()); + QVERIFY(clonedGroupResetTimeInfo->timeInfo().lastAccessTime() != originalGroup->timeInfo().lastAccessTime()); + QVERIFY(clonedGroupResetTimeInfo->timeInfo().locationChanged() != originalGroup->timeInfo().locationChanged()); + QCOMPARE(clonedGroupResetTimeInfo->timeInfo().lastModificationTime(), originalGroup->timeInfo().lastModificationTime()); } void TestGroup::testCopyCustomIcons() @@ -1319,3 +1324,47 @@ void TestGroup::testAutoTypeState() QVERIFY(!entry1->groupAutoTypeEnabled()); QVERIFY(entry2->groupAutoTypeEnabled()); } + +void TestGroup::testTimeinfoChanges() +{ + Database db, db2; + auto* root = db.rootGroup(); + auto* subgroup1 = new Group(); + auto* subgroup2 = new Group(); + subgroup1->setUuid(QUuid::createUuid()); + subgroup1->setParent(root); + subgroup2->setUuid(QUuid::createUuid()); + subgroup2->setParent(root); + QDateTime startTime = Clock::currentDateTimeUtc(); + TimeInfo startTimeinfo; + startTimeinfo.setCreationTime(startTime); + startTimeinfo.setLastModificationTime(startTime); + startTimeinfo.setLocationChanged(startTime); + startTimeinfo.setLastAccessTime(startTime); + m_clock->advanceMinute(1); + root->setTimeInfo(startTimeinfo); + subgroup1->setTimeInfo(startTimeinfo); + subgroup2->setTimeInfo(startTimeinfo); + + subgroup2->setPreviousParentGroup(subgroup1); + // setting previous parent group should not affect the LastModificationTime + QCOMPARE(subgroup2->timeInfo().lastModificationTime(), startTime); + subgroup2->setPreviousParentGroup(nullptr); + subgroup2->setParent(subgroup1); + QCOMPARE(root->timeInfo(), startTimeinfo); + QCOMPARE(subgroup1->timeInfo(), startTimeinfo); + // changing group should not affect LastModificationTime, CreationTime + QCOMPARE(subgroup2->timeInfo().creationTime(), startTime); + QCOMPARE(subgroup2->timeInfo().lastModificationTime(), startTime); + // changing group should affect the LocationChanged time + QCOMPARE(subgroup2->timeInfo().locationChanged(), Clock::currentDateTimeUtc()); + + // cross-db move + db2.rootGroup()->setTimeInfo(startTimeinfo); + m_clock->advanceMinute(1); + subgroup2->setParent(db2.rootGroup()); + QCOMPARE(subgroup2->timeInfo().creationTime(), startTime); + QCOMPARE(subgroup2->timeInfo().lastModificationTime(), startTime); + QCOMPARE(subgroup2->timeInfo().locationChanged(), Clock::currentDateTimeUtc()); + QCOMPARE(db2.rootGroup()->timeInfo(), startTimeinfo); +} diff --git a/tests/TestGroup.h b/tests/TestGroup.h index d3326e464..1f01d9491 100644 --- a/tests/TestGroup.h +++ b/tests/TestGroup.h @@ -50,6 +50,7 @@ private slots: void testMoveUpDown(); void testPreviousParentGroup(); void testAutoTypeState(); + void testTimeinfoChanges(); }; #endif // KEEPASSX_TESTGROUP_H