Fix memory leaks in tests

This commit is contained in:
Janek Bevendorff 2018-01-07 00:30:18 +01:00 committed by Jonathan White
parent ccfd7a065c
commit 72a1c65d00
No known key found for this signature in database
GPG Key ID: 440FC65F2E0C6E01
4 changed files with 30 additions and 22 deletions

View File

@ -1031,7 +1031,7 @@ Group* Kdbx3XmlReader::getGroup(const Uuid& uuid)
if (m_groups.contains(uuid)) {
return m_groups.value(uuid);
} else {
Group* group = new Group();
auto group = new Group();
group->setUpdateTimeinfo(false);
group->setUuid(uuid);
group->setParent(m_tmpParent);

View File

@ -114,24 +114,25 @@ bool Kdbx4Writer::writeDatabase(QIODevice* device, Database* db)
CHECK_RETURN_FALSE(writeData(headerData));
QByteArray headerHash = CryptoHash::hash(headerData, CryptoHash::Sha256);
QScopedPointer<QIODevice> firstLayer, secondLayer;
QByteArray hmacKey = KeePass2::hmacKey(masterSeed, db->transformedMasterKey());
QByteArray headerHmac = CryptoHash::hmac(headerData, HmacBlockStream::getHmacKey(UINT64_MAX, hmacKey),
CryptoHash::Sha256);
CHECK_RETURN_FALSE(writeData(headerHash));
CHECK_RETURN_FALSE(writeData(headerHmac));
HmacBlockStream* hmacStream = new HmacBlockStream(device, hmacKey);
if (!hmacStream->open(QIODevice::WriteOnly)) {
raiseError(hmacStream->errorString());
QScopedPointer<HmacBlockStream> hmacBlockStream;
QScopedPointer<SymmetricCipherStream> cipherStream;
hmacBlockStream.reset(new HmacBlockStream(device, hmacKey));
if (!hmacBlockStream->open(QIODevice::WriteOnly)) {
raiseError(hmacBlockStream->errorString());
return false;
}
firstLayer.reset(static_cast<QIODevice*>(hmacStream));
SymmetricCipherStream* cipherStream = new SymmetricCipherStream(hmacStream, algo,
SymmetricCipher::algorithmMode(algo),
SymmetricCipher::Encrypt);
cipherStream.reset(new SymmetricCipherStream(hmacBlockStream.data(), algo,
SymmetricCipher::algorithmMode(algo),
SymmetricCipher::Encrypt));
if (!cipherStream->init(finalKey, encryptionIV)) {
raiseError(cipherStream->errorString());
return false;
@ -140,13 +141,12 @@ bool Kdbx4Writer::writeDatabase(QIODevice* device, Database* db)
raiseError(cipherStream->errorString());
return false;
}
secondLayer.reset(static_cast<QIODevice*>(cipherStream));
QScopedPointer<QtIOCompressor> ioCompressor;
if (db->compressionAlgo() == Database::CompressionNone) {
m_device = secondLayer.data();
m_device = cipherStream.data();
} else {
ioCompressor.reset(new QtIOCompressor(secondLayer.data()));
ioCompressor.reset(new QtIOCompressor(cipherStream.data()));
ioCompressor->setStreamFormat(QtIOCompressor::GzipFormat);
if (!ioCompressor->open(QIODevice::WriteOnly)) {
raiseError(ioCompressor->errorString());
@ -191,12 +191,12 @@ bool Kdbx4Writer::writeDatabase(QIODevice* device, Database* db)
if (ioCompressor) {
ioCompressor->close();
}
if (!secondLayer->reset()) {
raiseError(secondLayer->errorString());
if (!cipherStream->reset()) {
raiseError(cipherStream->errorString());
return false;
}
if (!firstLayer->reset()) {
raiseError(firstLayer->errorString());
if (!hmacBlockStream->reset()) {
raiseError(hmacBlockStream->errorString());
return false;
}

View File

@ -117,7 +117,14 @@ void TestGui::cleanup()
triggerAction("actionDatabaseClose");
Tools::wait(100);
if (m_db) {
delete m_db;
}
m_db = nullptr;
if (m_dbWidget) {
delete m_dbWidget;
}
m_dbWidget = nullptr;
}
@ -1060,7 +1067,7 @@ void TestGui::dragAndDropGroup(const QModelIndex& sourceIndex, const QModelIndex
QVERIFY(sourceIndex.isValid());
QVERIFY(targetIndex.isValid());
GroupModel* groupModel = qobject_cast<GroupModel*>(m_dbWidget->findChild<GroupView*>("groupView")->model());
auto groupModel = qobject_cast<GroupModel*>(m_dbWidget->findChild<GroupView*>("groupView")->model());
QMimeData mimeData;
QByteArray encoded;

View File

@ -23,6 +23,7 @@
#include <QAbstractItemModel>
#include <QObject>
#include <QPointer>
class Database;
class DatabaseTabWidget;
@ -71,14 +72,14 @@ private:
void clickIndex(const QModelIndex& index, QAbstractItemView* view, Qt::MouseButton button,
Qt::KeyboardModifiers stateKey = 0);
MainWindow* m_mainWindow;
DatabaseTabWidget* m_tabWidget;
DatabaseWidget* m_dbWidget;
QPointer<MainWindow> m_mainWindow;
QPointer<DatabaseTabWidget> m_tabWidget;
QPointer<DatabaseWidget> m_dbWidget;
QPointer<Database> m_db;
QByteArray m_dbData;
TemporaryFile m_dbFile;
QString m_dbFileName;
QString m_dbFilePath;
Database* m_db;
};
#endif // KEEPASSX_TESTGUI_H