Move FileWatcher into Database class

* Fix #3506
* Fix #2389
* Fix #2536
* Fix #2230

Every database that has been opened now watch's it's own file. This allows the database class to manage file changes and detect fail conditions during saving. Additionally, all stakeholders of the database can listen for the database file changed notification and respond accordingly.

Performed significant cleanup of the autoreload code within DatabaseWidget. Fixed several issues with handling changes due to merging, not merging, and other scenarios while reloading.

Prevent database saves to the same file if there are changes on disk that have not been merged with the open database.
This commit is contained in:
Jonathan White 2019-10-14 09:31:23 -04:00
parent 6b746913e4
commit 744b4abce8
10 changed files with 208 additions and 122 deletions

View file

@ -20,13 +20,14 @@
#include "TestGlobal.h"
#include <QSignalSpy>
#include <QTemporaryFile>
#include "config-keepassx-tests.h"
#include "core/Metadata.h"
#include "core/Tools.h"
#include "crypto/Crypto.h"
#include "format/KeePass2Writer.h"
#include "keys/PasswordKey.h"
#include "util/TemporaryFile.h"
QTEST_GUILESS_MAIN(TestDatabase)
@ -35,6 +36,60 @@ void TestDatabase::initTestCase()
QVERIFY(Crypto::init());
}
void TestDatabase::testOpen()
{
auto db = QSharedPointer<Database>::create();
QVERIFY(!db->isInitialized());
QVERIFY(!db->isModified());
auto key = QSharedPointer<CompositeKey>::create();
key->addKey(QSharedPointer<PasswordKey>::create("a"));
bool ok = db->open(QString(KEEPASSX_TEST_DATA_DIR).append("/NewDatabase.kdbx"), key);
QVERIFY(ok);
QVERIFY(db->isInitialized());
QVERIFY(!db->isModified());
db->metadata()->setName("test");
QVERIFY(db->isModified());
}
void TestDatabase::testSave()
{
QByteArray data;
QFile sourceDbFile(QString(KEEPASSX_TEST_DATA_DIR).append("/NewDatabase.kdbx"));
QVERIFY(sourceDbFile.open(QIODevice::ReadOnly));
QVERIFY(Tools::readAllFromDevice(&sourceDbFile, data));
sourceDbFile.close();
TemporaryFile tempFile;
QVERIFY(tempFile.open());
QCOMPARE(tempFile.write(data), static_cast<qint64>((data.size())));
tempFile.close();
auto db = QSharedPointer<Database>::create();
auto key = QSharedPointer<CompositeKey>::create();
key->addKey(QSharedPointer<PasswordKey>::create("a"));
QString error;
bool ok = db->open(tempFile.fileName(), key, &error);
QVERIFY(ok);
// Test safe saves
db->metadata()->setName("test");
QVERIFY(db->isModified());
// Test unsafe saves
QVERIFY2(db->save(&error, false, false), error.toLatin1());
QVERIFY2(db->save(&error), error.toLatin1());
QVERIFY(!db->isModified());
// Test save backups
QVERIFY2(db->save(&error, true, true), error.toLatin1());
}
void TestDatabase::testEmptyRecycleBinOnDisabled()
{
QString filename = QString(KEEPASSX_TEST_DATA_DIR).append("/RecycleBinDisabled.kdbx");