Fixed memory leaks in non-gui tests

Fixed 2 memory leaks in production code and a few in testcases. As a
result leak_check_at_exit ASAN option does not need to turned off for
non-gui tests.
Smart pointers should be used elsewhere for consistency, but the sooner
this fixes are delivered, the lesser memory leaks are introduced.
This commit is contained in:
Michal Kaptur 2017-11-27 21:41:58 +01:00 committed by Janek Bevendorff
parent b20918b60e
commit 0ff75e7a88
10 changed files with 125 additions and 167 deletions

View File

@ -15,8 +15,8 @@ compiler:
- gcc - gcc
env: env:
- CONFIG=Release ASAN_OPTIONS=detect_odr_violation=1:leak_check_at_exit=0 - CONFIG=Release ASAN_OPTIONS=detect_odr_violation=1
- CONFIG=Debug ASAN_OPTIONS=detect_odr_violation=1:leak_check_at_exit=0 - CONFIG=Debug ASAN_OPTIONS=detect_odr_violation=1
git: git:
depth: 3 depth: 3
@ -37,7 +37,7 @@ script:
- cmake -DCMAKE_BUILD_TYPE=${CONFIG} -DWITH_GUI_TESTS=ON -DWITH_ASAN=ON -DWITH_XC_HTTP=ON -DWITH_XC_AUTOTYPE=ON -DWITH_XC_YUBIKEY=ON $CMAKE_ARGS .. - cmake -DCMAKE_BUILD_TYPE=${CONFIG} -DWITH_GUI_TESTS=ON -DWITH_ASAN=ON -DWITH_XC_HTTP=ON -DWITH_XC_AUTOTYPE=ON -DWITH_XC_YUBIKEY=ON $CMAKE_ARGS ..
- make -j2 - make -j2
- if [ "$TRAVIS_OS_NAME" = "linux" ]; then make test ARGS+="-E testgui --output-on-failure"; fi - if [ "$TRAVIS_OS_NAME" = "linux" ]; then make test ARGS+="-E testgui --output-on-failure"; fi
- if [ "$TRAVIS_OS_NAME" = "linux" ]; then xvfb-run -a --server-args="-screen 0 800x600x24" make test ARGS+="-R testgui --output-on-failure"; fi - if [ "$TRAVIS_OS_NAME" = "linux" ]; then ASAN_OPTIONS=${ASAN_OPTIONS}:leak_check_at_exit=0 xvfb-run -a --server-args="-screen 0 800x600x24" make test ARGS+="-R testgui --output-on-failure"; fi
- if [ "$TRAVIS_OS_NAME" = "osx" ]; then make test ARGS+="--output-on-failure"; fi - if [ "$TRAVIS_OS_NAME" = "osx" ]; then make test ARGS+="--output-on-failure"; fi
# Generate snapcraft build when merging into master/develop branches # Generate snapcraft build when merging into master/develop branches

View File

@ -86,6 +86,8 @@ bool SymmetricCipherGcrypt::init()
gcry_error_t error; gcry_error_t error;
if(m_ctx != nullptr)
gcry_cipher_close(m_ctx);
error = gcry_cipher_open(&m_ctx, m_algo, m_mode, 0); error = gcry_cipher_open(&m_ctx, m_algo, m_mode, 0);
if (error != 0) { if (error != 0) {
setErrorString(error); setErrorString(error);

View File

@ -1,5 +1,6 @@
/* /*
* Copyright (C) 2016 Felix Geyer <debfx@fobos.de> * Copyright (C) 2016 Felix Geyer <debfx@fobos.de>
* Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -18,36 +19,29 @@
#include "KeePass2Repair.h" #include "KeePass2Repair.h"
#include <QBuffer> #include <QBuffer>
#include <QScopedPointer>
#include <QRegExp> #include <QRegExp>
#include "format/KeePass2RandomStream.h" #include "format/KeePass2RandomStream.h"
#include "format/KeePass2Reader.h" #include "format/KeePass2Reader.h"
#include "format/KeePass2XmlReader.h" #include "format/KeePass2XmlReader.h"
KeePass2Repair::KeePass2Repair() KeePass2Repair::RepairOutcome KeePass2Repair::repairDatabase(QIODevice* device, const CompositeKey& key)
: m_db(nullptr)
{ {
}
KeePass2Repair::RepairResult KeePass2Repair::repairDatabase(QIODevice* device, const CompositeKey& key)
{
m_db = nullptr;
m_errorStr.clear(); m_errorStr.clear();
KeePass2Reader reader; KeePass2Reader reader;
reader.setSaveXml(true); reader.setSaveXml(true);
Database* db = reader.readDatabase(device, key, true); QScopedPointer<Database> db(reader.readDatabase(device, key, true));
if (!reader.hasError()) { if (!reader.hasError()) {
delete db; return qMakePair(NothingTodo, nullptr);
return NothingTodo;
} }
QByteArray xmlData = reader.xmlData(); QByteArray xmlData = reader.xmlData();
if (!db || xmlData.isEmpty()) { if (!db || xmlData.isEmpty()) {
delete db;
m_errorStr = reader.errorString(); m_errorStr = reader.errorString();
return UnableToOpen; return qMakePair(UnableToOpen, nullptr);
} }
bool repairAction = false; bool repairAction = false;
@ -59,8 +53,7 @@ KeePass2Repair::RepairResult KeePass2Repair::repairDatabase(QIODevice* device, c
&& encodingRegExp.cap(1).compare("utf8", Qt::CaseInsensitive) != 0) && encodingRegExp.cap(1).compare("utf8", Qt::CaseInsensitive) != 0)
{ {
// database is not utf-8 encoded, we don't support repairing that // database is not utf-8 encoded, we don't support repairing that
delete db; return qMakePair(RepairFailed, nullptr);
return RepairFailed;
} }
} }
@ -75,8 +68,7 @@ KeePass2Repair::RepairResult KeePass2Repair::repairDatabase(QIODevice* device, c
if (!repairAction) { if (!repairAction) {
// we were unable to find the problem // we were unable to find the problem
delete db; return qMakePair(RepairFailed, nullptr);
return RepairFailed;
} }
KeePass2RandomStream randomStream; KeePass2RandomStream randomStream;
@ -84,23 +76,16 @@ KeePass2Repair::RepairResult KeePass2Repair::repairDatabase(QIODevice* device, c
KeePass2XmlReader xmlReader; KeePass2XmlReader xmlReader;
QBuffer buffer(&xmlData); QBuffer buffer(&xmlData);
buffer.open(QIODevice::ReadOnly); buffer.open(QIODevice::ReadOnly);
xmlReader.readDatabase(&buffer, db, &randomStream); xmlReader.readDatabase(&buffer, db.data(), &randomStream);
if (xmlReader.hasError()) { if (xmlReader.hasError()) {
delete db; return qMakePair(RepairFailed, nullptr);
return RepairFailed;
} }
else { else {
m_db = db; return qMakePair(RepairSuccess, db.take());
return RepairSuccess;
} }
} }
Database* KeePass2Repair::database() const
{
return m_db;
}
QString KeePass2Repair::errorString() const QString KeePass2Repair::errorString() const
{ {
return m_errorStr; return m_errorStr;

View File

@ -1,5 +1,6 @@
/* /*
* Copyright (C) 2016 Felix Geyer <debfx@fobos.de> * Copyright (C) 2016 Felix Geyer <debfx@fobos.de>
* Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -20,6 +21,7 @@
#include <QCoreApplication> #include <QCoreApplication>
#include <QIODevice> #include <QIODevice>
#include <QPair>
#include "core/Database.h" #include "core/Database.h"
#include "keys/CompositeKey.h" #include "keys/CompositeKey.h"
@ -36,14 +38,12 @@ public:
RepairSuccess, RepairSuccess,
RepairFailed RepairFailed
}; };
using RepairOutcome = QPair<RepairResult, Database*>;
KeePass2Repair(); RepairOutcome repairDatabase(QIODevice* device, const CompositeKey& key);
RepairResult repairDatabase(QIODevice* device, const CompositeKey& key);
Database* database() const;
QString errorString() const; QString errorString() const;
private: private:
Database* m_db;
QString m_errorStr; QString m_errorStr;
}; };

View File

@ -1,5 +1,6 @@
/* /*
* Copyright (C) 2016 Felix Geyer <debfx@fobos.de> * Copyright (C) 2016 Felix Geyer <debfx@fobos.de>
* Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -69,7 +70,8 @@ void DatabaseRepairWidget::openDatabase()
delete m_db; delete m_db;
} }
QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
KeePass2Repair::RepairResult repairResult = repair.repairDatabase(&file, masterKey); auto repairOutcome = repair.repairDatabase(&file, masterKey);
KeePass2Repair::RepairResult repairResult = repairOutcome.first;
QApplication::restoreOverrideCursor(); QApplication::restoreOverrideCursor();
switch (repairResult) { switch (repairResult) {
@ -83,7 +85,7 @@ void DatabaseRepairWidget::openDatabase()
emit editFinished(false); emit editFinished(false);
return; return;
case KeePass2Repair::RepairSuccess: case KeePass2Repair::RepairSuccess:
m_db = repair.database(); m_db = repairOutcome.second;
MessageBox::warning(this, tr("Success"), tr("The database has been successfully repaired\nYou can now save it.")); MessageBox::warning(this, tr("Success"), tr("The database has been successfully repaired\nYou can now save it."));
emit editFinished(true); emit editFinished(true);
return; return;

View File

@ -24,17 +24,12 @@ QTEST_GUILESS_MAIN(TestCsvParser)
void TestCsvParser::initTestCase() void TestCsvParser::initTestCase()
{ {
parser = new CsvParser(); parser.reset(new CsvParser());
}
void TestCsvParser::cleanupTestCase()
{
delete parser;
} }
void TestCsvParser::init() void TestCsvParser::init()
{ {
file = new QTemporaryFile(); file.reset(new QTemporaryFile());
if (not file->open()) if (not file->open())
QFAIL("Cannot open file!"); QFAIL("Cannot open file!");
parser->setBackslashSyntax(false); parser->setBackslashSyntax(false);
@ -51,20 +46,20 @@ void TestCsvParser::cleanup()
/****************** TEST CASES ******************/ /****************** TEST CASES ******************/
void TestCsvParser::testMissingQuote() { void TestCsvParser::testMissingQuote() {
parser->setTextQualifier(':'); parser->setTextQualifier(':');
QTextStream out(file); QTextStream out(file.data());
out << "A,B\n:BM,1"; out << "A,B\n:BM,1";
QEXPECT_FAIL("", "Bad format", Continue); QEXPECT_FAIL("", "Bad format", Continue);
QVERIFY(parser->parse(file)); QVERIFY(parser->parse(file.data()));
t = parser->getCsvTable(); t = parser->getCsvTable();
QWARN(parser->getStatus().toLatin1()); QWARN(parser->getStatus().toLatin1());
} }
void TestCsvParser::testMalformed() { void TestCsvParser::testMalformed() {
parser->setTextQualifier(':'); parser->setTextQualifier(':');
QTextStream out(file); QTextStream out(file.data());
out << "A,B,C\n:BM::,1,:2:"; out << "A,B,C\n:BM::,1,:2:";
QEXPECT_FAIL("", "Bad format", Continue); QEXPECT_FAIL("", "Bad format", Continue);
QVERIFY(parser->parse(file)); QVERIFY(parser->parse(file.data()));
t = parser->getCsvTable(); t = parser->getCsvTable();
QWARN(parser->getStatus().toLatin1()); QWARN(parser->getStatus().toLatin1());
} }
@ -72,14 +67,14 @@ void TestCsvParser::testMalformed() {
void TestCsvParser::testBackslashSyntax() { void TestCsvParser::testBackslashSyntax() {
parser->setBackslashSyntax(true); parser->setBackslashSyntax(true);
parser->setTextQualifier(QChar('X')); parser->setTextQualifier(QChar('X'));
QTextStream out(file); QTextStream out(file.data());
//attended result: one"\t\"wo //attended result: one"\t\"wo
out << "Xone\\\"\\\\t\\\\\\\"w\noX\n" out << "Xone\\\"\\\\t\\\\\\\"w\noX\n"
<< "X13X,X2\\X,X,\"\"3\"X\r" << "X13X,X2\\X,X,\"\"3\"X\r"
<< "3,X\"4\"X,,\n" << "3,X\"4\"X,,\n"
<< "XX\n" << "XX\n"
<< "\\"; << "\\";
QVERIFY(parser->parse(file)); QVERIFY(parser->parse(file.data()));
t = parser->getCsvTable(); t = parser->getCsvTable();
QVERIFY(t.at(0).at(0) == "one\"\\t\\\"w\no"); QVERIFY(t.at(0).at(0) == "one\"\\t\\\"w\no");
QVERIFY(t.at(1).at(0) == "13"); QVERIFY(t.at(1).at(0) == "13");
@ -94,10 +89,10 @@ void TestCsvParser::testBackslashSyntax() {
} }
void TestCsvParser::testQuoted() { void TestCsvParser::testQuoted() {
QTextStream out(file); QTextStream out(file.data());
out << "ro,w,\"end, of \"\"\"\"\"\"row\"\"\"\"\"\n" out << "ro,w,\"end, of \"\"\"\"\"\"row\"\"\"\"\"\n"
<< "2\n"; << "2\n";
QVERIFY(parser->parse(file)); QVERIFY(parser->parse(file.data()));
t = parser->getCsvTable(); t = parser->getCsvTable();
QVERIFY(t.at(0).at(0) == "ro"); QVERIFY(t.at(0).at(0) == "ro");
QVERIFY(t.at(0).at(1) == "w"); QVERIFY(t.at(0).at(1) == "w");
@ -107,41 +102,41 @@ void TestCsvParser::testQuoted() {
} }
void TestCsvParser::testEmptySimple() { void TestCsvParser::testEmptySimple() {
QTextStream out(file); QTextStream out(file.data());
out <<""; out <<"";
QVERIFY(parser->parse(file)); QVERIFY(parser->parse(file.data()));
t = parser->getCsvTable(); t = parser->getCsvTable();
QVERIFY(t.size() == 0); QVERIFY(t.size() == 0);
} }
void TestCsvParser::testEmptyQuoted() { void TestCsvParser::testEmptyQuoted() {
QTextStream out(file); QTextStream out(file.data());
out <<"\"\""; out <<"\"\"";
QVERIFY(parser->parse(file)); QVERIFY(parser->parse(file.data()));
t = parser->getCsvTable(); t = parser->getCsvTable();
QVERIFY(t.size() == 0); QVERIFY(t.size() == 0);
} }
void TestCsvParser::testEmptyNewline() { void TestCsvParser::testEmptyNewline() {
QTextStream out(file); QTextStream out(file.data());
out <<"\"\n\""; out <<"\"\n\"";
QVERIFY(parser->parse(file)); QVERIFY(parser->parse(file.data()));
t = parser->getCsvTable(); t = parser->getCsvTable();
QVERIFY(t.size() == 0); QVERIFY(t.size() == 0);
} }
void TestCsvParser::testEmptyFile() void TestCsvParser::testEmptyFile()
{ {
QVERIFY(parser->parse(file)); QVERIFY(parser->parse(file.data()));
t = parser->getCsvTable(); t = parser->getCsvTable();
QVERIFY(t.size() == 0); QVERIFY(t.size() == 0);
} }
void TestCsvParser::testNewline() void TestCsvParser::testNewline()
{ {
QTextStream out(file); QTextStream out(file.data());
out << "1,2\n\n\n"; out << "1,2\n\n\n";
QVERIFY(parser->parse(file)); QVERIFY(parser->parse(file.data()));
t = parser->getCsvTable(); t = parser->getCsvTable();
QVERIFY(t.size() == 1); QVERIFY(t.size() == 1);
QVERIFY(t.at(0).at(0) == "1"); QVERIFY(t.at(0).at(0) == "1");
@ -150,9 +145,9 @@ void TestCsvParser::testNewline()
void TestCsvParser::testCR() void TestCsvParser::testCR()
{ {
QTextStream out(file); QTextStream out(file.data());
out << "1,2\r3,4"; out << "1,2\r3,4";
QVERIFY(parser->parse(file)); QVERIFY(parser->parse(file.data()));
t = parser->getCsvTable(); t = parser->getCsvTable();
QVERIFY(t.size() == 2); QVERIFY(t.size() == 2);
QVERIFY(t.at(0).at(0) == "1"); QVERIFY(t.at(0).at(0) == "1");
@ -163,9 +158,9 @@ void TestCsvParser::testCR()
void TestCsvParser::testLF() void TestCsvParser::testLF()
{ {
QTextStream out(file); QTextStream out(file.data());
out << "1,2\n3,4"; out << "1,2\n3,4";
QVERIFY(parser->parse(file)); QVERIFY(parser->parse(file.data()));
t = parser->getCsvTable(); t = parser->getCsvTable();
QVERIFY(t.size() == 2); QVERIFY(t.size() == 2);
QVERIFY(t.at(0).at(0) == "1"); QVERIFY(t.at(0).at(0) == "1");
@ -176,9 +171,9 @@ void TestCsvParser::testLF()
void TestCsvParser::testCRLF() void TestCsvParser::testCRLF()
{ {
QTextStream out(file); QTextStream out(file.data());
out << "1,2\r\n3,4"; out << "1,2\r\n3,4";
QVERIFY(parser->parse(file)); QVERIFY(parser->parse(file.data()));
t = parser->getCsvTable(); t = parser->getCsvTable();
QVERIFY(t.size() == 2); QVERIFY(t.size() == 2);
QVERIFY(t.at(0).at(0) == "1"); QVERIFY(t.at(0).at(0) == "1");
@ -189,13 +184,13 @@ void TestCsvParser::testCRLF()
void TestCsvParser::testComments() void TestCsvParser::testComments()
{ {
QTextStream out(file); QTextStream out(file.data());
out << " #one\n" out << " #one\n"
<< " \t # two, three \r\n" << " \t # two, three \r\n"
<< " #, sing\t with\r" << " #, sing\t with\r"
<< " #\t me!\n" << " #\t me!\n"
<< "useful,text #1!"; << "useful,text #1!";
QVERIFY(parser->parse(file)); QVERIFY(parser->parse(file.data()));
t = parser->getCsvTable(); t = parser->getCsvTable();
QVERIFY(t.size() == 1); QVERIFY(t.size() == 1);
QVERIFY(t.at(0).at(0) == "useful"); QVERIFY(t.at(0).at(0) == "useful");
@ -203,21 +198,21 @@ void TestCsvParser::testComments()
} }
void TestCsvParser::testColumns() { void TestCsvParser::testColumns() {
QTextStream out(file); QTextStream out(file.data());
out << "1,2\n" out << "1,2\n"
<< ",,,,,,,,,a\n" << ",,,,,,,,,a\n"
<< "a,b,c,d\n"; << "a,b,c,d\n";
QVERIFY(parser->parse(file)); QVERIFY(parser->parse(file.data()));
t = parser->getCsvTable(); t = parser->getCsvTable();
QVERIFY(parser->getCsvCols() == 10); QVERIFY(parser->getCsvCols() == 10);
} }
void TestCsvParser::testSimple() { void TestCsvParser::testSimple() {
QTextStream out(file); QTextStream out(file.data());
out << ",,2\r,2,3\n" out << ",,2\r,2,3\n"
<< "A,,B\"\n" << "A,,B\"\n"
<< " ,,\n"; << " ,,\n";
QVERIFY(parser->parse(file)); QVERIFY(parser->parse(file.data()));
t = parser->getCsvTable(); t = parser->getCsvTable();
QVERIFY(t.size() == 4); QVERIFY(t.size() == 4);
QVERIFY(t.at(0).at(0) == ""); QVERIFY(t.at(0).at(0) == "");
@ -236,11 +231,11 @@ void TestCsvParser::testSimple() {
void TestCsvParser::testSeparator() { void TestCsvParser::testSeparator() {
parser->setFieldSeparator('\t'); parser->setFieldSeparator('\t');
QTextStream out(file); QTextStream out(file.data());
out << "\t\t2\r\t2\t3\n" out << "\t\t2\r\t2\t3\n"
<< "A\t\tB\"\n" << "A\t\tB\"\n"
<< " \t\t\n"; << " \t\t\n";
QVERIFY(parser->parse(file)); QVERIFY(parser->parse(file.data()));
t = parser->getCsvTable(); t = parser->getCsvTable();
QVERIFY(t.size() == 4); QVERIFY(t.size() == 4);
QVERIFY(t.at(0).at(0) == ""); QVERIFY(t.at(0).at(0) == "");
@ -260,10 +255,10 @@ void TestCsvParser::testSeparator() {
void TestCsvParser::testMultiline() void TestCsvParser::testMultiline()
{ {
parser->setTextQualifier(QChar(':')); parser->setTextQualifier(QChar(':'));
QTextStream out(file); QTextStream out(file.data());
out << ":1\r\n2a::b:,:3\r4:\n" out << ":1\r\n2a::b:,:3\r4:\n"
<< "2\n"; << "2\n";
QVERIFY(parser->parse(file)); QVERIFY(parser->parse(file.data()));
t = parser->getCsvTable(); t = parser->getCsvTable();
QVERIFY(t.at(0).at(0) == "1\n2a:b"); QVERIFY(t.at(0).at(0) == "1\n2a:b");
QVERIFY(t.at(0).at(1) == "3\n4"); QVERIFY(t.at(0).at(1) == "3\n4");
@ -281,10 +276,10 @@ void TestCsvParser::testEmptyReparsing()
void TestCsvParser::testReparsing() void TestCsvParser::testReparsing()
{ {
QTextStream out(file); QTextStream out(file.data());
out << ":te\r\nxt1:,:te\rxt2:,:end of \"this\n string\":\n" out << ":te\r\nxt1:,:te\rxt2:,:end of \"this\n string\":\n"
<< "2\n"; << "2\n";
QVERIFY(parser->parse(file)); QVERIFY(parser->parse(file.data()));
t = parser->getCsvTable(); t = parser->getCsvTable();
QEXPECT_FAIL("", "Wrong qualifier", Continue); QEXPECT_FAIL("", "Wrong qualifier", Continue);
@ -303,10 +298,10 @@ void TestCsvParser::testReparsing()
void TestCsvParser::testQualifier() { void TestCsvParser::testQualifier() {
parser->setTextQualifier(QChar('X')); parser->setTextQualifier(QChar('X'));
QTextStream out(file); QTextStream out(file.data());
out << "X1X,X2XX,X,\"\"3\"\"\"X\r" out << "X1X,X2XX,X,\"\"3\"\"\"X\r"
<< "3,X\"4\"X,,\n"; << "3,X\"4\"X,,\n";
QVERIFY(parser->parse(file)); QVERIFY(parser->parse(file.data()));
t = parser->getCsvTable(); t = parser->getCsvTable();
QVERIFY(t.size() == 2); QVERIFY(t.size() == 2);
QVERIFY(t.at(0).at(0) == "1"); QVERIFY(t.at(0).at(0) == "1");
@ -324,10 +319,10 @@ void TestCsvParser::testUnicode() {
//CORRECT QChar g(0x20AC); //CORRECT QChar g(0x20AC);
//ERROR QChar g("\u20AC"); //ERROR QChar g("\u20AC");
parser->setFieldSeparator(QChar('A')); parser->setFieldSeparator(QChar('A'));
QTextStream out(file); QTextStream out(file.data());
out << QString("€1A2śA\"3śAż\"Ażac"); out << QString("€1A2śA\"3śAż\"Ażac");
QVERIFY(parser->parse(file)); QVERIFY(parser->parse(file.data()));
t = parser->getCsvTable(); t = parser->getCsvTable();
QVERIFY(t.size() == 1); QVERIFY(t.size() == 1);
QVERIFY(t.at(0).at(0) == "€1"); QVERIFY(t.at(0).at(0) == "€1");

View File

@ -22,6 +22,7 @@
#include <QObject> #include <QObject>
#include <QFile> #include <QFile>
#include <QTemporaryFile> #include <QTemporaryFile>
#include <QScopedPointer>
#include "core/CsvParser.h" #include "core/CsvParser.h"
@ -37,7 +38,6 @@ private slots:
void init(); void init();
void cleanup(); void cleanup();
void initTestCase(); void initTestCase();
void cleanupTestCase();
void testUnicode(); void testUnicode();
void testLF(); void testLF();
@ -62,8 +62,8 @@ private slots:
void testColumns(); void testColumns();
private: private:
QTemporaryFile* file; QScopedPointer<QTemporaryFile> file;
CsvParser* parser; QScopedPointer<CsvParser> parser;
CsvTable t; CsvTable t;
void dumpRow(CsvTable table, int row); void dumpRow(CsvTable table, int row);
}; };

View File

@ -18,9 +18,10 @@
#include "TestGroup.h" #include "TestGroup.h"
#include <QPointer>
#include <QSignalSpy>
#include <QDebug> #include <QDebug>
#include <QPointer>
#include <QScopedPointer>
#include <QSignalSpy>
#include <QTest> #include <QTest>
#include "core/Database.h" #include "core/Database.h"
@ -75,6 +76,7 @@ void TestGroup::testParenting()
QCOMPARE(g3->children().size(), 1); QCOMPARE(g3->children().size(), 1);
QCOMPARE(g4->children().size(), 0); QCOMPARE(g4->children().size(), 0);
QVERIFY(rootGroup->children().at(0) == g1);
QVERIFY(rootGroup->children().at(0) == g1); QVERIFY(rootGroup->children().at(0) == g1);
QVERIFY(g1->children().at(0) == g2); QVERIFY(g1->children().at(0) == g2);
QVERIFY(g1->children().at(1) == g3); QVERIFY(g1->children().at(1) == g3);
@ -99,7 +101,6 @@ void TestGroup::testParenting()
g3->setIcon(Uuid::random()); g3->setIcon(Uuid::random());
g1->setIcon(2); g1->setIcon(2);
QCOMPARE(spy.count(), 6); QCOMPARE(spy.count(), 6);
delete db; delete db;
QVERIFY(rootGroup.isNull()); QVERIFY(rootGroup.isNull());
@ -107,7 +108,6 @@ void TestGroup::testParenting()
QVERIFY(g2.isNull()); QVERIFY(g2.isNull());
QVERIFY(g3.isNull()); QVERIFY(g3.isNull());
QVERIFY(g4.isNull()); QVERIFY(g4.isNull());
delete tmpRoot; delete tmpRoot;
} }
@ -251,7 +251,7 @@ void TestGroup::testEntries()
void TestGroup::testDeleteSignals() void TestGroup::testDeleteSignals()
{ {
Database* db = new Database(); QScopedPointer<Database> db(new Database());
Group* groupRoot = db->rootGroup(); Group* groupRoot = db->rootGroup();
Group* groupChild = new Group(); Group* groupChild = new Group();
Group* groupChildChild = new Group(); Group* groupChildChild = new Group();
@ -260,15 +260,13 @@ void TestGroup::testDeleteSignals()
groupChildChild->setObjectName("groupChildChild"); groupChildChild->setObjectName("groupChildChild");
groupChild->setParent(groupRoot); groupChild->setParent(groupRoot);
groupChildChild->setParent(groupChild); groupChildChild->setParent(groupChild);
QSignalSpy spyAboutToRemove(db, SIGNAL(groupAboutToRemove(Group*))); QSignalSpy spyAboutToRemove(db.data(), SIGNAL(groupAboutToRemove(Group*)));
QSignalSpy spyRemoved(db, SIGNAL(groupRemoved())); QSignalSpy spyRemoved(db.data(), SIGNAL(groupRemoved()));
delete groupChild; delete groupChild;
QVERIFY(groupRoot->children().isEmpty()); QVERIFY(groupRoot->children().isEmpty());
QCOMPARE(spyAboutToRemove.count(), 2); QCOMPARE(spyAboutToRemove.count(), 2);
QCOMPARE(spyRemoved.count(), 2); QCOMPARE(spyRemoved.count(), 2);
delete db;
Group* group = new Group(); Group* group = new Group();
Entry* entry = new Entry(); Entry* entry = new Entry();
@ -282,7 +280,7 @@ void TestGroup::testDeleteSignals()
QCOMPARE(spyEntryRemoved.count(), 1); QCOMPARE(spyEntryRemoved.count(), 1);
delete group; delete group;
Database* db2 = new Database(); QScopedPointer<Database> db2(new Database());
Group* groupRoot2 = db2->rootGroup(); Group* groupRoot2 = db2->rootGroup();
Group* group2 = new Group(); Group* group2 = new Group();
group2->setParent(groupRoot2); group2->setParent(groupRoot2);
@ -294,12 +292,11 @@ void TestGroup::testDeleteSignals()
delete group2; delete group2;
QCOMPARE(spyEntryAboutToRemove2.count(), 1); QCOMPARE(spyEntryAboutToRemove2.count(), 1);
QCOMPARE(spyEntryRemoved2.count(), 1); QCOMPARE(spyEntryRemoved2.count(), 1);
delete db2;
} }
void TestGroup::testCopyCustomIcon() void TestGroup::testCopyCustomIcon()
{ {
Database* dbSource = new Database(); QScopedPointer<Database> dbSource(new Database());
Uuid groupIconUuid = Uuid::random(); Uuid groupIconUuid = Uuid::random();
QImage groupIcon(16, 16, QImage::Format_RGB32); QImage groupIcon(16, 16, QImage::Format_RGB32);
@ -321,7 +318,7 @@ void TestGroup::testCopyCustomIcon()
entry->setIcon(entryIconUuid); entry->setIcon(entryIconUuid);
QCOMPARE(entry->icon(), entryIcon); QCOMPARE(entry->icon(), entryIcon);
Database* dbTarget = new Database(); QScopedPointer<Database> dbTarget(new Database());
group->setParent(dbTarget->rootGroup()); group->setParent(dbTarget->rootGroup());
QVERIFY(dbTarget->metadata()->containsCustomIcon(groupIconUuid)); QVERIFY(dbTarget->metadata()->containsCustomIcon(groupIconUuid));
@ -332,37 +329,34 @@ void TestGroup::testCopyCustomIcon()
QVERIFY(dbTarget->metadata()->containsCustomIcon(entryIconUuid)); QVERIFY(dbTarget->metadata()->containsCustomIcon(entryIconUuid));
QCOMPARE(dbTarget->metadata()->customIcon(entryIconUuid), entryIcon); QCOMPARE(dbTarget->metadata()->customIcon(entryIconUuid), entryIcon);
QCOMPARE(entry->icon(), entryIcon); QCOMPARE(entry->icon(), entryIcon);
delete dbSource;
delete dbTarget;
} }
void TestGroup::testClone() void TestGroup::testClone()
{ {
Database* db = new Database(); QScopedPointer<Database> db(new Database());
Group* originalGroup = new Group(); QScopedPointer<Group> originalGroup(new Group());
originalGroup->setParent(db->rootGroup()); originalGroup->setParent(db->rootGroup());
originalGroup->setName("Group"); originalGroup->setName("Group");
originalGroup->setIcon(42); originalGroup->setIcon(42);
Entry* originalGroupEntry = new Entry(); QScopedPointer<Entry> originalGroupEntry(new Entry());
originalGroupEntry->setGroup(originalGroup); originalGroupEntry->setGroup(originalGroup.data());
originalGroupEntry->setTitle("GroupEntryOld"); originalGroupEntry->setTitle("GroupEntryOld");
originalGroupEntry->setIcon(43); originalGroupEntry->setIcon(43);
originalGroupEntry->beginUpdate(); originalGroupEntry->beginUpdate();
originalGroupEntry->setTitle("GroupEntry"); originalGroupEntry->setTitle("GroupEntry");
originalGroupEntry->endUpdate(); originalGroupEntry->endUpdate();
Group* subGroup = new Group(); QScopedPointer<Group> subGroup(new Group());
subGroup->setParent(originalGroup); subGroup->setParent(originalGroup.data());
subGroup->setName("SubGroup"); subGroup->setName("SubGroup");
Entry* subGroupEntry = new Entry(); QScopedPointer<Entry> subGroupEntry(new Entry());
subGroupEntry->setGroup(subGroup); subGroupEntry->setGroup(subGroup.data());
subGroupEntry->setTitle("SubGroupEntry"); subGroupEntry->setTitle("SubGroupEntry");
Group* clonedGroup = originalGroup->clone(); QScopedPointer<Group> clonedGroup(originalGroup->clone());
QVERIFY(!clonedGroup->parentGroup()); QVERIFY(!clonedGroup->parentGroup());
QVERIFY(!clonedGroup->database()); QVERIFY(!clonedGroup->database());
QVERIFY(clonedGroup->uuid() != originalGroup->uuid()); QVERIFY(clonedGroup->uuid() != originalGroup->uuid());
@ -387,19 +381,15 @@ void TestGroup::testClone()
QVERIFY(clonedSubGroupEntry->uuid() != subGroupEntry->uuid()); QVERIFY(clonedSubGroupEntry->uuid() != subGroupEntry->uuid());
QCOMPARE(clonedSubGroupEntry->title(), QString("SubGroupEntry")); QCOMPARE(clonedSubGroupEntry->title(), QString("SubGroupEntry"));
Group* clonedGroupKeepUuid = originalGroup->clone(Entry::CloneNoFlags); QScopedPointer<Group> clonedGroupKeepUuid(originalGroup->clone(Entry::CloneNoFlags));
QCOMPARE(clonedGroupKeepUuid->entries().at(0)->uuid(), originalGroupEntry->uuid()); QCOMPARE(clonedGroupKeepUuid->entries().at(0)->uuid(), originalGroupEntry->uuid());
QCOMPARE(clonedGroupKeepUuid->children().at(0)->entries().at(0)->uuid(), subGroupEntry->uuid()); QCOMPARE(clonedGroupKeepUuid->children().at(0)->entries().at(0)->uuid(), subGroupEntry->uuid());
delete clonedGroup;
delete clonedGroupKeepUuid;
delete db;
} }
void TestGroup::testCopyCustomIcons() void TestGroup::testCopyCustomIcons()
{ {
Database* dbSource = new Database(); QScopedPointer<Database> dbSource(new Database());
Database* dbTarget = new Database(); QScopedPointer<Database> dbTarget(new Database());
QImage iconImage1(1, 1, QImage::Format_RGB32); QImage iconImage1(1, 1, QImage::Format_RGB32);
iconImage1.setPixel(0, 0, qRgb(1, 2, 3)); iconImage1.setPixel(0, 0, qRgb(1, 2, 3));
@ -407,20 +397,20 @@ void TestGroup::testCopyCustomIcons()
QImage iconImage2(1, 1, QImage::Format_RGB32); QImage iconImage2(1, 1, QImage::Format_RGB32);
iconImage2.setPixel(0, 0, qRgb(4, 5, 6)); iconImage2.setPixel(0, 0, qRgb(4, 5, 6));
Group* group1 = new Group(); QScopedPointer<Group> group1(new Group());
group1->setParent(dbSource->rootGroup()); group1->setParent(dbSource->rootGroup());
Uuid group1Icon = Uuid::random(); Uuid group1Icon = Uuid::random();
dbSource->metadata()->addCustomIcon(group1Icon, iconImage1); dbSource->metadata()->addCustomIcon(group1Icon, iconImage1);
group1->setIcon(group1Icon); group1->setIcon(group1Icon);
Group* group2 = new Group(); QScopedPointer<Group> group2(new Group());
group2->setParent(group1); group2->setParent(group1.data());
Uuid group2Icon = Uuid::random(); Uuid group2Icon = Uuid::random();
dbSource->metadata()->addCustomIcon(group2Icon, iconImage1); dbSource->metadata()->addCustomIcon(group2Icon, iconImage1);
group2->setIcon(group2Icon); group2->setIcon(group2Icon);
Entry* entry1 = new Entry(); QScopedPointer<Entry> entry1(new Entry());
entry1->setGroup(group2); entry1->setGroup(group2.data());
Uuid entry1IconOld = Uuid::random(); Uuid entry1IconOld = Uuid::random();
dbSource->metadata()->addCustomIcon(entry1IconOld, iconImage1); dbSource->metadata()->addCustomIcon(entry1IconOld, iconImage1);
entry1->setIcon(entry1IconOld); entry1->setIcon(entry1IconOld);
@ -447,27 +437,24 @@ void TestGroup::testCopyCustomIcons()
QCOMPARE(metaTarget->customIcon(group1Icon).pixel(0, 0), qRgb(1, 2, 3)); QCOMPARE(metaTarget->customIcon(group1Icon).pixel(0, 0), qRgb(1, 2, 3));
QCOMPARE(metaTarget->customIcon(group2Icon).pixel(0, 0), qRgb(4, 5, 6)); QCOMPARE(metaTarget->customIcon(group2Icon).pixel(0, 0), qRgb(4, 5, 6));
delete dbTarget;
delete dbSource;
} }
void TestGroup::testMerge() void TestGroup::testMerge()
{ {
Group* group1 = new Group(); QScopedPointer<Group> group1(new Group());
group1->setName("group 1"); group1->setName("group 1");
Group* group2 = new Group(); QScopedPointer<Group> group2(new Group());
group2->setName("group 2"); group2->setName("group 2");
Entry* entry1 = new Entry(); QScopedPointer<Entry> entry1(new Entry());
Entry* entry2 = new Entry(); QScopedPointer<Entry> entry2(new Entry());
entry1->setGroup(group1); entry1->setGroup(group1.data());
entry1->setUuid(Uuid::random()); entry1->setUuid(Uuid::random());
entry2->setGroup(group1); entry2->setGroup(group1.data());
entry2->setUuid(Uuid::random()); entry2->setUuid(Uuid::random());
group2->merge(group1); group2->merge(group1.data());
QCOMPARE(group1->entries().size(), 2); QCOMPARE(group1->entries().size(), 2);
QCOMPARE(group2->entries().size(), 2); QCOMPARE(group2->entries().size(), 2);
@ -475,25 +462,22 @@ void TestGroup::testMerge()
void TestGroup::testMergeDatabase() void TestGroup::testMergeDatabase()
{ {
Database* dbSource = createMergeTestDatabase(); QScopedPointer<Database> dbSource(createMergeTestDatabase());
Database* dbDest = new Database(); QScopedPointer<Database> dbDest(new Database());
dbDest->merge(dbSource); dbDest->merge(dbSource.data());
QCOMPARE(dbDest->rootGroup()->children().size(), 2); QCOMPARE(dbDest->rootGroup()->children().size(), 2);
QCOMPARE(dbDest->rootGroup()->children().at(0)->entries().size(), 2); QCOMPARE(dbDest->rootGroup()->children().at(0)->entries().size(), 2);
delete dbDest;
delete dbSource;
} }
void TestGroup::testMergeConflict() void TestGroup::testMergeConflict()
{ {
Database* dbSource = createMergeTestDatabase(); QScopedPointer<Database> dbSource(createMergeTestDatabase());
// test merging updated entries // test merging updated entries
// falls back to KeepBoth mode // falls back to KeepBoth mode
Database* dbCopy = new Database(); QScopedPointer<Database> dbCopy(new Database());
dbCopy->setRootGroup(dbSource->rootGroup()->clone(Entry::CloneNoFlags)); dbCopy->setRootGroup(dbSource->rootGroup()->clone(Entry::CloneNoFlags));
// sanity check // sanity check
@ -505,22 +489,19 @@ void TestGroup::testMergeConflict()
updatedTimeInfo.setLastModificationTime(updatedTimeInfo.lastModificationTime().addYears(1)); updatedTimeInfo.setLastModificationTime(updatedTimeInfo.lastModificationTime().addYears(1));
updatedEntry->setTimeInfo(updatedTimeInfo); updatedEntry->setTimeInfo(updatedTimeInfo);
dbCopy->merge(dbSource); dbCopy->merge(dbSource.data());
// one entry is duplicated because of mode // one entry is duplicated because of mode
QCOMPARE(dbCopy->rootGroup()->children().at(0)->entries().size(), 2); QCOMPARE(dbCopy->rootGroup()->children().at(0)->entries().size(), 2);
delete dbSource;
delete dbCopy;
} }
void TestGroup::testMergeConflictKeepBoth() void TestGroup::testMergeConflictKeepBoth()
{ {
Database* dbSource = createMergeTestDatabase(); QScopedPointer<Database> dbSource(createMergeTestDatabase());
// test merging updated entries // test merging updated entries
// falls back to KeepBoth mode // falls back to KeepBoth mode
Database* dbCopy = new Database(); QScopedPointer<Database> dbCopy(new Database());
dbCopy->setRootGroup(dbSource->rootGroup()->clone(Entry::CloneNoFlags)); dbCopy->setRootGroup(dbSource->rootGroup()->clone(Entry::CloneNoFlags));
// sanity check // sanity check
@ -534,21 +515,18 @@ void TestGroup::testMergeConflictKeepBoth()
dbCopy->rootGroup()->setMergeMode(Group::MergeMode::KeepBoth); dbCopy->rootGroup()->setMergeMode(Group::MergeMode::KeepBoth);
dbCopy->merge(dbSource); dbCopy->merge(dbSource.data());
// one entry is duplicated because of mode // one entry is duplicated because of mode
QCOMPARE(dbCopy->rootGroup()->children().at(0)->entries().size(), 3); QCOMPARE(dbCopy->rootGroup()->children().at(0)->entries().size(), 3);
// the older entry was merged from the other db as last in the group // the older entry was merged from the other db as last in the group
Entry* olderEntry = dbCopy->rootGroup()->children().at(0)->entries().at(2); Entry* olderEntry = dbCopy->rootGroup()->children().at(0)->entries().at(2);
QVERIFY2(olderEntry->attributes()->hasKey("merged"), "older entry is marked with an attribute \"merged\""); QVERIFY2(olderEntry->attributes()->hasKey("merged"), "older entry is marked with an attribute \"merged\"");
delete dbSource;
delete dbCopy;
} }
Database* TestGroup::createMergeTestDatabase() Database* TestGroup::createMergeTestDatabase()
{ {
Database* db = new Database(); QScopedPointer<Database> db(new Database());
Group* group1 = new Group(); Group* group1 = new Group();
group1->setName("group 1"); group1->setName("group 1");
@ -566,12 +544,12 @@ Database* TestGroup::createMergeTestDatabase()
group1->setParent(db->rootGroup()); group1->setParent(db->rootGroup());
group2->setParent(db->rootGroup()); group2->setParent(db->rootGroup());
return db; return db.take();
} }
void TestGroup::testFindEntry() void TestGroup::testFindEntry()
{ {
Database* db = new Database(); QScopedPointer<Database> db(new Database());
Entry* entry1 = new Entry(); Entry* entry1 = new Entry();
entry1->setTitle(QString("entry1")); entry1->setTitle(QString("entry1"));
@ -642,13 +620,11 @@ void TestGroup::testFindEntry()
// An invalid UUID. // An invalid UUID.
entry = db->rootGroup()->findEntry(QString("febfb01ebcdf9dbd90a3f1579dc")); entry = db->rootGroup()->findEntry(QString("febfb01ebcdf9dbd90a3f1579dc"));
QVERIFY(entry == nullptr); QVERIFY(entry == nullptr);
delete db;
} }
void TestGroup::testFindGroupByPath() void TestGroup::testFindGroupByPath()
{ {
Database* db = new Database(); QScopedPointer<Database> db(new Database());
Group* group1 = new Group(); Group* group1 = new Group();
group1->setName("group1"); group1->setName("group1");
@ -706,13 +682,11 @@ void TestGroup::testFindGroupByPath()
group = db->rootGroup()->findGroupByPath("invalid"); group = db->rootGroup()->findGroupByPath("invalid");
QVERIFY(group == nullptr); QVERIFY(group == nullptr);
delete db;
} }
void TestGroup::testPrint() void TestGroup::testPrint()
{ {
Database* db = new Database(); QScopedPointer<Database> db(new Database());
QString output = db->rootGroup()->print(); QString output = db->rootGroup()->print();
QCOMPARE(output, QString("[empty]\n")); QCOMPARE(output, QString("[empty]\n"));
@ -731,7 +705,6 @@ void TestGroup::testPrint()
output = db->rootGroup()->print(true); output = db->rootGroup()->print(true);
QCOMPARE(output, QString("entry1 " + entry1->uuid().toHex() + "\n")); QCOMPARE(output, QString("entry1 " + entry1->uuid().toHex() + "\n"));
Group* group1 = new Group(); Group* group1 = new Group();
group1->setName("group1"); group1->setName("group1");
@ -752,5 +725,4 @@ void TestGroup::testPrint()
QVERIFY(output.contains(QString("entry1 " + entry1->uuid().toHex() + "\n"))); QVERIFY(output.contains(QString("entry1 " + entry1->uuid().toHex() + "\n")));
QVERIFY(output.contains(QString("group1/ " + group1->uuid().toHex() + "\n"))); QVERIFY(output.contains(QString("group1/ " + group1->uuid().toHex() + "\n")));
QVERIFY(output.contains(QString(" entry2 " + entry2->uuid().toHex() + "\n"))); QVERIFY(output.contains(QString(" entry2 " + entry2->uuid().toHex() + "\n")));
delete db;
} }

View File

@ -148,13 +148,15 @@ void TestKeePass2Writer::testRepair()
KeePass2Repair repair; KeePass2Repair repair;
QFile file(brokenDbFilename); QFile file(brokenDbFilename);
file.open(QIODevice::ReadOnly); file.open(QIODevice::ReadOnly);
QCOMPARE(repair.repairDatabase(&file, key), KeePass2Repair::RepairSuccess); auto result = repair.repairDatabase(&file, key);
Database* dbRepaired = repair.database(); QCOMPARE(result.first, KeePass2Repair::RepairSuccess);
Database* dbRepaired = result.second;
QVERIFY(dbRepaired); QVERIFY(dbRepaired);
QCOMPARE(dbRepaired->rootGroup()->entries().size(), 1); QCOMPARE(dbRepaired->rootGroup()->entries().size(), 1);
QCOMPARE(dbRepaired->rootGroup()->entries().at(0)->username(), QString("testuser").append(QChar(0x20AC))); QCOMPARE(dbRepaired->rootGroup()->entries().at(0)->username(), QString("testuser").append(QChar(0x20AC)));
QCOMPARE(dbRepaired->rootGroup()->entries().at(0)->password(), QString("testpw")); QCOMPARE(dbRepaired->rootGroup()->entries().at(0)->password(), QString("testpw"));
delete dbRepaired;
} }
void TestKeePass2Writer::cleanupTestCase() void TestKeePass2Writer::cleanupTestCase()

View File

@ -162,7 +162,7 @@ void TestSymmetricCipher::testTwofish256CbcEncryption()
bool ok; bool ok;
for (int i = 0; i < keys.size(); ++i) { for (int i = 0; i < keys.size(); ++i) {
cipher.init(keys[i], ivs[i]); QVERIFY(cipher.init(keys[i], ivs[i]));
QByteArray ptNext = plainTexts[i]; QByteArray ptNext = plainTexts[i];
QByteArray ctPrev = ivs[i]; QByteArray ctPrev = ivs[i];
QByteArray ctCur; QByteArray ctCur;