keepassxc/tests/TestModified.cpp

471 lines
14 KiB
C++
Raw Normal View History

2012-04-18 07:27:16 -04:00
/*
* Copyright (C) 2012 Felix Geyer <debfx@fobos.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 or (at your option)
* version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "TestModified.h"
#include <QSignalSpy>
#include <QTest>
2012-04-18 07:27:16 -04:00
#include "tests.h"
#include "core/Database.h"
#include "core/Group.h"
2012-05-11 05:04:20 -04:00
#include "core/Metadata.h"
#include "core/Tools.h"
2012-04-18 07:27:16 -04:00
#include "crypto/Crypto.h"
void TestModified::initTestCase()
{
Crypto::init();
}
void TestModified::testSignals()
{
int spyCount = 0;
int spyCount2 = 0;
2012-04-18 11:26:32 -04:00
CompositeKey compositeKey;
2012-04-18 07:27:16 -04:00
Database* db = new Database();
2012-04-18 11:26:32 -04:00
Group* root = db->rootGroup();
2012-06-24 11:53:01 -04:00
QSignalSpy spyModified(db, SIGNAL(modifiedImmediate()));
2012-04-18 07:27:16 -04:00
2012-04-18 11:26:32 -04:00
db->setKey(compositeKey);
QCOMPARE(spyModified.count(), ++spyCount);
2012-04-18 07:27:16 -04:00
Group* g1 = new Group();
g1->setParent(root);
2012-04-18 11:26:32 -04:00
QCOMPARE(spyModified.count(), ++spyCount);
2012-04-18 07:27:16 -04:00
Group* g2 = new Group();
g2->setParent(root);
2012-04-18 11:26:32 -04:00
QCOMPARE(spyModified.count(), ++spyCount);
2012-04-18 07:27:16 -04:00
g2->setParent(root, 0);
QCOMPARE(spyModified.count(), ++spyCount);
2012-04-18 11:26:32 -04:00
Entry* entry1 = new Entry();
2012-04-18 07:27:16 -04:00
entry1->setGroup(g1);
2012-04-18 11:26:32 -04:00
QCOMPARE(spyModified.count(), ++spyCount);
2012-04-18 07:27:16 -04:00
Database* db2 = new Database();
2012-04-18 11:26:32 -04:00
Group* root2 = db2->rootGroup();
2012-06-24 11:53:01 -04:00
QSignalSpy spyModified2(db2, SIGNAL(modifiedImmediate()));
2012-04-18 07:27:16 -04:00
g1->setParent(root2);
2012-04-18 11:26:32 -04:00
QCOMPARE(spyModified.count(), ++spyCount);
QCOMPARE(spyModified2.count(), ++spyCount2);
2012-04-18 07:27:16 -04:00
entry1->setTitle("test");
QCOMPARE(spyModified.count(), spyCount);
2012-04-18 11:26:32 -04:00
QCOMPARE(spyModified2.count(), ++spyCount2);
2012-04-18 11:26:32 -04:00
Entry* entry2 = new Entry();
2012-04-18 07:27:16 -04:00
entry2->setGroup(g2);
2012-04-18 11:26:32 -04:00
QCOMPARE(spyModified.count(), ++spyCount);
2012-04-18 07:27:16 -04:00
QCOMPARE(spyModified2.count(), spyCount2);
entry2->setGroup(root2);
2012-04-18 11:26:32 -04:00
QCOMPARE(spyModified.count(), ++spyCount);
QCOMPARE(spyModified2.count(), ++spyCount2);
2012-04-18 07:27:16 -04:00
entry2->setTitle("test2");
QCOMPARE(spyModified.count(), spyCount);
2012-04-18 11:26:32 -04:00
QCOMPARE(spyModified2.count(), ++spyCount2);
2012-04-18 07:27:16 -04:00
Group* g3 = new Group();
g3->setParent(root);
2012-04-18 11:26:32 -04:00
QCOMPARE(spyModified.count(), ++spyCount);
2012-04-18 07:27:16 -04:00
Group* g4 = new Group();
g4->setParent(g3);
2012-04-18 11:26:32 -04:00
QCOMPARE(spyModified.count(), ++spyCount);
2012-04-18 07:27:16 -04:00
delete g4;
QCOMPARE(spyModified.count(), ++spyCount);
delete entry2;
QCOMPARE(spyModified2.count(), ++spyCount2);
QCOMPARE(spyModified.count(), spyCount);
2012-04-18 07:27:16 -04:00
QCOMPARE(spyModified2.count(), spyCount2);
2012-04-18 11:26:32 -04:00
delete db;
delete db2;
2012-04-18 07:27:16 -04:00
}
void TestModified::testGroupSets()
{
int spyCount = 0;
Database* db = new Database();
2012-04-18 11:26:32 -04:00
Group* root = db->rootGroup();
2012-04-18 07:27:16 -04:00
Group* g = new Group();
g->setParent(root);
2012-06-24 11:53:01 -04:00
QSignalSpy spyModified(db, SIGNAL(modifiedImmediate()));
2012-04-18 07:27:16 -04:00
root->setUuid(Uuid::random());
2012-04-18 11:26:32 -04:00
QCOMPARE(spyModified.count(), ++spyCount);
2012-04-18 07:27:16 -04:00
root->setUuid(root->uuid());
QCOMPARE(spyModified.count(), spyCount);
root->setName("test");
2012-04-18 11:26:32 -04:00
QCOMPARE(spyModified.count(), ++spyCount);
2012-04-18 07:27:16 -04:00
root->setName(root->name());
QCOMPARE(spyModified.count(), spyCount);
root->setNotes("test");
2012-04-18 11:26:32 -04:00
QCOMPARE(spyModified.count(), ++spyCount);
2012-04-18 07:27:16 -04:00
root->setNotes(root->notes());
QCOMPARE(spyModified.count(), spyCount);
root->setIcon(1);
2012-04-18 11:26:32 -04:00
QCOMPARE(spyModified.count(), ++spyCount);
2012-04-18 07:27:16 -04:00
root->setIcon(root->iconNumber());
QCOMPARE(spyModified.count(), spyCount);
root->setIcon(Uuid::random());
2012-04-18 11:26:32 -04:00
QCOMPARE(spyModified.count(), ++spyCount);
2012-04-18 07:27:16 -04:00
root->setIcon(root->iconUuid());
QCOMPARE(spyModified.count(), spyCount);
g->setUuid(Uuid::random());
2012-04-18 11:26:32 -04:00
QCOMPARE(spyModified.count(), ++spyCount);
2012-04-18 07:27:16 -04:00
g->setUuid(g->uuid());
QCOMPARE(spyModified.count(), spyCount);
g->setName("test");
2012-04-18 11:26:32 -04:00
QCOMPARE(spyModified.count(), ++spyCount);
2012-04-18 07:27:16 -04:00
g->setName(g->name());
QCOMPARE(spyModified.count(), spyCount);
g->setNotes("test");
2012-04-18 11:26:32 -04:00
QCOMPARE(spyModified.count(), ++spyCount);
2012-04-18 07:27:16 -04:00
g->setNotes(g->notes());
QCOMPARE(spyModified.count(), spyCount);
g->setIcon(1);
2012-04-18 11:26:32 -04:00
QCOMPARE(spyModified.count(), ++spyCount);
2012-04-18 07:27:16 -04:00
g->setIcon(g->iconNumber());
QCOMPARE(spyModified.count(), spyCount);
g->setIcon(Uuid::random());
2012-04-18 11:26:32 -04:00
QCOMPARE(spyModified.count(), ++spyCount);
2012-04-18 07:27:16 -04:00
g->setIcon(g->iconUuid());
QCOMPARE(spyModified.count(), spyCount);
2012-04-18 11:26:32 -04:00
delete db;
2012-04-18 07:27:16 -04:00
}
void TestModified::testEntrySets()
{
int spyCount = 0;
Database* db = new Database();
2012-04-18 11:26:32 -04:00
Group* root = db->rootGroup();
2012-04-18 07:27:16 -04:00
Group* g = new Group();
g->setParent(root);
2012-04-18 11:26:32 -04:00
Entry* entry = new Entry();
2012-04-18 07:27:16 -04:00
entry->setGroup(g);
2012-06-24 11:53:01 -04:00
QSignalSpy spyModified(db, SIGNAL(modifiedImmediate()));
2012-04-18 07:27:16 -04:00
entry->setUuid(Uuid::random());
2012-04-18 11:26:32 -04:00
QCOMPARE(spyModified.count(), ++spyCount);
2012-04-18 07:27:16 -04:00
entry->setUuid(entry->uuid());
QCOMPARE(spyModified.count(), spyCount);
entry->setTitle("test");
2012-04-18 11:26:32 -04:00
QCOMPARE(spyModified.count(), ++spyCount);
2012-04-18 07:27:16 -04:00
entry->setTitle(entry->title());
QCOMPARE(spyModified.count(), spyCount);
entry->setUrl("test");
2012-04-18 11:26:32 -04:00
QCOMPARE(spyModified.count(), ++spyCount);
2012-04-18 07:27:16 -04:00
entry->setUrl(entry->url());
QCOMPARE(spyModified.count(), spyCount);
entry->setUsername("test");
2012-04-18 11:26:32 -04:00
QCOMPARE(spyModified.count(), ++spyCount);
2012-04-18 07:27:16 -04:00
entry->setUsername(entry->username());
QCOMPARE(spyModified.count(), spyCount);
entry->setPassword("test");
2012-04-18 11:26:32 -04:00
QCOMPARE(spyModified.count(), ++spyCount);
2012-04-18 07:27:16 -04:00
entry->setPassword(entry->password());
QCOMPARE(spyModified.count(), spyCount);
entry->setNotes("test");
2012-04-18 11:26:32 -04:00
QCOMPARE(spyModified.count(), ++spyCount);
2012-04-18 07:27:16 -04:00
entry->setNotes(entry->notes());
QCOMPARE(spyModified.count(), spyCount);
entry->setIcon(1);
2012-04-18 11:26:32 -04:00
QCOMPARE(spyModified.count(), ++spyCount);
2012-04-18 07:27:16 -04:00
entry->setIcon(entry->iconNumber());
QCOMPARE(spyModified.count(), spyCount);
entry->setIcon(Uuid::random());
2012-04-18 11:26:32 -04:00
QCOMPARE(spyModified.count(), ++spyCount);
2012-04-18 07:27:16 -04:00
entry->setIcon(entry->iconUuid());
QCOMPARE(spyModified.count(), spyCount);
entry->setTags("test");
2012-04-18 11:26:32 -04:00
QCOMPARE(spyModified.count(), ++spyCount);
2012-04-18 07:27:16 -04:00
entry->setTags(entry->tags());
QCOMPARE(spyModified.count(), spyCount);
entry->setExpires(true);
2012-04-18 11:26:32 -04:00
QCOMPARE(spyModified.count(), ++spyCount);
2012-04-18 07:27:16 -04:00
entry->setExpires(entry->timeInfo().expires());
QCOMPARE(spyModified.count(), spyCount);
entry->setExpiryTime(Tools::currentDateTimeUtc().addYears(1));
2012-04-18 11:26:32 -04:00
QCOMPARE(spyModified.count(), ++spyCount);
2012-04-18 07:27:16 -04:00
entry->setExpiryTime(entry->timeInfo().expiryTime());
QCOMPARE(spyModified.count(), spyCount);
entry->setAutoTypeEnabled(false);
2012-04-18 11:26:32 -04:00
QCOMPARE(spyModified.count(), ++spyCount);
2012-04-18 07:27:16 -04:00
entry->setAutoTypeEnabled(entry->autoTypeEnabled());
QCOMPARE(spyModified.count(), spyCount);
entry->setAutoTypeObfuscation(1);
2012-04-18 11:26:32 -04:00
QCOMPARE(spyModified.count(), ++spyCount);
2012-04-18 07:27:16 -04:00
entry->setAutoTypeObfuscation(entry->autoTypeObfuscation());
QCOMPARE(spyModified.count(), spyCount);
entry->setDefaultAutoTypeSequence("test");
2012-04-18 11:26:32 -04:00
QCOMPARE(spyModified.count(), ++spyCount);
2012-04-18 07:27:16 -04:00
entry->setDefaultAutoTypeSequence(entry->defaultAutoTypeSequence());
QCOMPARE(spyModified.count(), spyCount);
entry->setForegroundColor(Qt::red);
2012-04-18 11:26:32 -04:00
QCOMPARE(spyModified.count(), ++spyCount);
2012-04-18 07:27:16 -04:00
entry->setForegroundColor(entry->foregroundColor());
QCOMPARE(spyModified.count(), spyCount);
entry->setBackgroundColor(Qt::red);
2012-04-18 11:26:32 -04:00
QCOMPARE(spyModified.count(), ++spyCount);
2012-04-18 07:27:16 -04:00
entry->setBackgroundColor(entry->backgroundColor());
QCOMPARE(spyModified.count(), spyCount);
entry->setOverrideUrl("test");
2012-04-18 11:26:32 -04:00
QCOMPARE(spyModified.count(), ++spyCount);
2012-04-18 07:27:16 -04:00
entry->setOverrideUrl(entry->overrideUrl());
QCOMPARE(spyModified.count(), spyCount);
entry->attributes()->set("test key", "test value", false);
2012-04-18 11:26:32 -04:00
QCOMPARE(spyModified.count(), ++spyCount);
2012-04-18 07:27:16 -04:00
entry->attributes()->set("test key", entry->attributes()->value("test key"), false);
QCOMPARE(spyModified.count(), spyCount);
entry->attributes()->set("test key", entry->attributes()->value("test key"), true);
2012-04-18 11:26:32 -04:00
QCOMPARE(spyModified.count(), ++spyCount);
2012-04-18 07:27:16 -04:00
entry->attributes()->set("test key", "new test value", true);
2012-04-18 11:26:32 -04:00
QCOMPARE(spyModified.count(), ++spyCount);
2012-04-18 07:27:16 -04:00
entry->attributes()->set("test key2", "test value2", true);
2012-04-18 11:26:32 -04:00
QCOMPARE(spyModified.count(), ++spyCount);
2012-04-18 07:27:16 -04:00
entry->attributes()->set("test key2", entry->attributes()->value("test key2"), true);
QCOMPARE(spyModified.count(), spyCount);
2012-04-18 11:26:32 -04:00
delete db;
2012-04-18 07:27:16 -04:00
}
void TestModified::testHistoryItem()
{
Entry* entry = new Entry();
QDateTime created = entry->timeInfo().creationTime();
entry->setUuid(Uuid::random());
entry->setTitle("a");
entry->setTags("a");
EntryAttributes* attributes = new EntryAttributes();
attributes->copyCustomKeysFrom(entry->attributes());
Entry* historyEntry;
int historyItemsSize = 0;
entry->beginUpdate();
entry->setTitle("a");
entry->setTags("a");
entry->setOverrideUrl("");
entry->endUpdate();
QCOMPARE(entry->historyItems().size(), historyItemsSize);
QDateTime modified = entry->timeInfo().lastModificationTime();
QTest::qSleep(10);
entry->beginUpdate();
entry->setTitle("b");
entry->endUpdate();
QCOMPARE(entry->historyItems().size(), ++historyItemsSize);
historyEntry = entry->historyItems().at(historyItemsSize - 1);
QCOMPARE(historyEntry->title(), QString("a"));
QCOMPARE(historyEntry->uuid(), entry->uuid());
QCOMPARE(historyEntry->tags(), entry->tags());
QCOMPARE(historyEntry->overrideUrl(), entry->overrideUrl());
QCOMPARE(historyEntry->timeInfo().creationTime(), created);
QCOMPARE(historyEntry->timeInfo().lastModificationTime(), modified);
2012-05-11 05:04:20 -04:00
QCOMPARE(historyEntry->historyItems().size(), 0);
entry->beginUpdate();
entry->setTags("b");
entry->endUpdate();
QCOMPARE(entry->historyItems().size(), ++historyItemsSize);
QCOMPARE(entry->historyItems().at(historyItemsSize - 1)->tags(), QString("a"));
entry->beginUpdate();
entry->attachments()->set("test", QByteArray("value"));
entry->endUpdate();
QCOMPARE(entry->historyItems().size(), ++historyItemsSize);
QCOMPARE(entry->historyItems().at(historyItemsSize - 1)->attachments()->keys().size(), 0);
attributes->set("k", "myvalue");
entry->beginUpdate();
entry->attributes()->copyCustomKeysFrom(attributes);
entry->endUpdate();
QCOMPARE(entry->historyItems().size(), ++historyItemsSize);
QVERIFY(!entry->historyItems().at(historyItemsSize - 1)->attributes()->keys().contains("k"));
delete attributes;
delete entry;
2012-05-11 05:04:20 -04:00
Database* db = new Database();
Group* root = db->rootGroup();
db->metadata()->setHistoryMaxItems(3);
db->metadata()->setHistoryMaxSize(-1);
Entry* historyEntry2;
Entry* entry2 = new Entry();
entry2->setGroup(root);
entry2->beginUpdate();
entry2->setTitle("1");
entry2->endUpdate();
entry2->beginUpdate();
entry2->setTitle("2");
entry2->endUpdate();
entry2->beginUpdate();
entry2->setTitle("3");
entry2->endUpdate();
QCOMPARE(entry2->historyItems().size(), 3);
entry2->beginUpdate();
entry2->setTitle("4");
entry2->endUpdate();
QCOMPARE(entry2->historyItems().size(), 3);
db->metadata()->setHistoryMaxItems(1);
entry2->beginUpdate();
entry2->setTitle("5");
entry2->endUpdate();
QCOMPARE(entry2->historyItems().size(), 1);
historyEntry2 = entry2->historyItems().at(0);
QCOMPARE(historyEntry2->title(), QString("4"));
db->metadata()->setHistoryMaxItems(-1);
for (int i = 0; i < 20; i++) {
entry2->beginUpdate();
entry2->setTitle("6");
entry2->endUpdate();
entry2->beginUpdate();
entry2->setTitle("6b");
entry2->endUpdate();
}
QCOMPARE(entry2->historyItems().size(), 41);
db->metadata()->setHistoryMaxItems(0);
entry2->beginUpdate();
entry2->setTitle("7");
entry2->endUpdate();
QCOMPARE(entry2->historyItems().size(), 0);
db->metadata()->setHistoryMaxItems(-1);
db->metadata()->setHistoryMaxSize(17000);
entry2->beginUpdate();
entry2->attachments()->set("test", QByteArray(18000, 'X'));
entry2->endUpdate();
QCOMPARE(entry2->historyItems().size(), 1);
historyEntry2 = entry2->historyItems().at(0);
QCOMPARE(historyEntry2->title(), QString("7"));
entry2->beginUpdate();
entry2->setTitle("8");
entry2->endUpdate();
QCOMPARE(entry2->historyItems().size(), 2);
entry2->beginUpdate();
entry2->attachments()->remove("test");
entry2->endUpdate();
QCOMPARE(entry2->historyItems().size(), 0);
entry2->beginUpdate();
entry2->attachments()->set("test2", QByteArray(6000, 'a'));
entry2->endUpdate();
QCOMPARE(entry2->historyItems().size(), 1);
entry2->beginUpdate();
entry2->attachments()->set("test3", QByteArray(6000, 'b'));
entry2->endUpdate();
QCOMPARE(entry2->historyItems().size(), 2);
entry2->beginUpdate();
entry2->attachments()->set("test4", QByteArray(6000, 'c'));
entry2->endUpdate();
QCOMPARE(entry2->historyItems().size(), 3);
entry2->beginUpdate();
entry2->attachments()->set("test5", QByteArray(6000, 'd'));
entry2->endUpdate();
QCOMPARE(entry2->historyItems().size(), 4);
Entry* entry3 = new Entry();
entry3->setGroup(root);
QCOMPARE(entry3->historyItems().size(), 0);
entry3->beginUpdate();
entry3->attachments()->set("test", QByteArray(6000, 'a'));
entry3->endUpdate();
QCOMPARE(entry3->historyItems().size(), 1);
entry3->beginUpdate();
entry3->attachments()->set("test", QByteArray(6000, 'b'));
entry3->endUpdate();
QCOMPARE(entry3->historyItems().size(), 2);
entry3->beginUpdate();
entry3->attachments()->set("test", QByteArray(6000, 'c'));
entry3->endUpdate();
QCOMPARE(entry3->historyItems().size(), 3);
entry3->beginUpdate();
entry3->attachments()->set("test", QByteArray(6000, 'd'));
entry3->endUpdate();
QCOMPARE(entry3->historyItems().size(), 2);
delete db;
}
QTEST_GUILESS_MAIN(TestModified)