Skip to content

Commit

Permalink
Fix tests on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
phoerious committed Oct 19, 2018
1 parent 722c5c1 commit b3c90e9
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 22 deletions.
2 changes: 1 addition & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ set(TEST_LIBRARIES
${ZLIB_LIBRARIES}
)

set(testsupport_SOURCES TestGlobal.h modeltest.cpp FailDevice.cpp stub/TestClock.cpp)
set(testsupport_SOURCES TestGlobal.h modeltest.cpp FailDevice.cpp stub/TestClock.cpp util/TemporaryFile.cpp)
add_library(testsupport STATIC ${testsupport_SOURCES})
target_link_libraries(testsupport Qt5::Core Qt5::Concurrent Qt5::Widgets Qt5::Test)

Expand Down
24 changes: 13 additions & 11 deletions tests/TestCli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,22 +71,22 @@ void TestCli::initTestCase()

void TestCli::init()
{
m_dbFile.reset(new QTemporaryFile());
m_dbFile.reset(new TemporaryFile());
m_dbFile->open();
m_dbFile->write(m_dbData);
m_dbFile->flush();
m_dbFile->close();

m_stdinFile.reset(new QTemporaryFile());
m_stdinFile.reset(new TemporaryFile());
m_stdinFile->open();
m_stdinHandle = fdopen(m_stdinFile->handle(), "r+");
Utils::STDIN = m_stdinHandle;

m_stdoutFile.reset(new QTemporaryFile());
m_stdoutFile.reset(new TemporaryFile());
m_stdoutFile->open();
m_stdoutHandle = fdopen(m_stdoutFile->handle(), "r+");
Utils::STDOUT = m_stdoutHandle;

m_stderrFile.reset(new QTemporaryFile());
m_stderrFile.reset(new TemporaryFile());
m_stderrFile->open();
m_stderrHandle = fdopen(m_stderrFile->handle(), "r+");
Utils::STDERR = m_stderrHandle;
Expand Down Expand Up @@ -147,6 +147,7 @@ void TestCli::testAdd()

Utils::setNextPassword("a");
addCmd.execute({"add", "-u", "newuser", "--url", "https://example.com/", "-g", "-l", "20", m_dbFile->fileName(), "/newuser-entry"});
m_stderrFile->reset();

auto db = readTestDatabase();
auto* entry = db->rootGroup()->findEntryByPath("/newuser-entry");
Expand Down Expand Up @@ -220,7 +221,7 @@ void TestCli::testDiceware()
passphrase = m_stdoutFile->readLine();
QCOMPARE(passphrase.split(" ").size(), 10);

QTemporaryFile wordFile;
TemporaryFile wordFile;
wordFile.open();
for (int i = 0; i < 4500; ++i) {
wordFile.write(QString("word" + QString::number(i) + "\n").toLatin1());
Expand Down Expand Up @@ -548,7 +549,7 @@ void TestCli::testLocate()
entry->setUuid(QUuid::createUuid());
entry->setTitle("New Entry");
group->addEntry(entry);
QTemporaryFile tmpFile;
TemporaryFile tmpFile;
tmpFile.open();
Kdbx4Writer writer;
writer.writeDatabase(&tmpFile, db.data());
Expand Down Expand Up @@ -581,13 +582,13 @@ void TestCli::testMerge()
// load test database and save a copy
auto db = readTestDatabase();
QVERIFY(db);
QTemporaryFile targetFile1;
TemporaryFile targetFile1;
targetFile1.open();
writer.writeDatabase(&targetFile1, db.data());
targetFile1.close();

// save another copy with a different password
QTemporaryFile targetFile2;
TemporaryFile targetFile2;
targetFile2.open();
auto oldKey = db->key();
auto key = QSharedPointer<CompositeKey>::create();
Expand All @@ -605,7 +606,7 @@ void TestCli::testMerge()
auto* group = db->rootGroup()->findGroupByPath("/Internet/");
QVERIFY(group);
group->addEntry(entry);
QTemporaryFile sourceFile;
TemporaryFile sourceFile;
sourceFile.open();
writer.writeDatabase(&sourceFile, db.data());
sourceFile.close();
Expand All @@ -615,6 +616,7 @@ void TestCli::testMerge()
mergeCmd.execute({"merge", "-s", targetFile1.fileName(), sourceFile.fileName()});
m_stdoutFile->seek(pos);
m_stdoutFile->readLine();
m_stderrFile->reset();
QCOMPARE(m_stdoutFile->readAll(), QByteArray("Successfully merged the database files.\n"));

QFile readBack(targetFile1.fileName());
Expand Down Expand Up @@ -660,7 +662,7 @@ void TestCli::testRemove()
// load test database and save a copy with disabled recycle bin
auto db = readTestDatabase();
QVERIFY(db);
QTemporaryFile fileCopy;
TemporaryFile fileCopy;
fileCopy.open();
db->metadata()->setRecycleBinEnabled(false);
writer.writeDatabase(&fileCopy, db.data());
Expand Down
9 changes: 5 additions & 4 deletions tests/TestCli.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#define KEEPASSXC_TESTCLI_H

#include "core/Database.h"
#include "util/TemporaryFile.h"

#include <QTest>
#include <QTextStream>
Expand Down Expand Up @@ -57,10 +58,10 @@ private slots:

private:
QByteArray m_dbData;
QScopedPointer<QTemporaryFile> m_dbFile;
QScopedPointer<QTemporaryFile> m_stdoutFile;
QScopedPointer<QTemporaryFile> m_stderrFile;
QScopedPointer<QTemporaryFile> m_stdinFile;
QScopedPointer<TemporaryFile> m_dbFile;
QScopedPointer<TemporaryFile> m_stdoutFile;
QScopedPointer<TemporaryFile> m_stderrFile;
QScopedPointer<TemporaryFile> m_stdinFile;
FILE* m_stdoutHandle = stdout;
FILE* m_stderrHandle = stderr;
FILE* m_stdinHandle = stdin;
Expand Down
2 changes: 1 addition & 1 deletion tests/gui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/..)

add_unit_test(NAME testgui SOURCES TestGui.cpp LIBS ${TEST_LIBRARIES})
add_unit_test(NAME testgui SOURCES TestGui.cpp ../util/TemporaryFile.cpp LIBS ${TEST_LIBRARIES})

add_unit_test(NAME testguipixmaps SOURCES TestGuiPixmaps.cpp LIBS ${TEST_LIBRARIES})
6 changes: 3 additions & 3 deletions tests/gui/TestGui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ void TestGui::initTestCase()
// Every test starts with opening the temp database
void TestGui::init()
{
m_dbFile.reset(new QTemporaryFile());
m_dbFile.reset(new TemporaryFile());
// Write the temp storage to a temp database file for use in our tests
QVERIFY(m_dbFile->open());
QCOMPARE(m_dbFile->write(m_dbData), static_cast<qint64>((m_dbData.size())));
Expand Down Expand Up @@ -289,7 +289,7 @@ void TestGui::createDatabaseCallback()
QCOMPARE(fileCombo->currentText(), QString("%1/%2").arg(QString(KEEPASSX_TEST_DATA_DIR), "FileKeyHashed.key"));

// save database to temporary file
QTemporaryFile tmpFile;
TemporaryFile tmpFile;
QVERIFY(tmpFile.open());
tmpFile.close();
fileDialog()->setNextFileName(tmpFile.fileName());
Expand Down Expand Up @@ -1071,7 +1071,7 @@ void TestGui::testSaveAs()
m_db->metadata()->setName("testSaveAs");

// open temporary file so it creates a filename
QTemporaryFile tmpFile;
TemporaryFile tmpFile;
QVERIFY(tmpFile.open());
QString tmpFileName = tmpFile.fileName();
tmpFile.remove();
Expand Down
4 changes: 2 additions & 2 deletions tests/gui/TestGui.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
#define KEEPASSX_TESTGUI_H

#include "gui/MainWindow.h"
#include "util/TemporaryFile.h"

#include <QAbstractItemModel>
#include <QObject>
#include <QPointer>
#include <QScopedPointer>
#include <QTemporaryFile>

class Database;
class DatabaseTabWidget;
Expand Down Expand Up @@ -90,7 +90,7 @@ private slots:
QPointer<DatabaseWidget> m_dbWidget;
QPointer<Database> m_db;
QByteArray m_dbData;
QScopedPointer<QTemporaryFile> m_dbFile;
QScopedPointer<TemporaryFile> m_dbFile;
QString m_dbFileName;
QString m_dbFilePath;
};
Expand Down
55 changes: 55 additions & 0 deletions tests/util/TemporaryFile.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (C) 2018 KeePassXC Team <team@keepassxc.org>
*
* 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 "TemporaryFile.h"

#ifdef Q_OS_WIN

TemporaryFile::TemporaryFile()
: TemporaryFile(nullptr)
{
}

TemporaryFile::TemporaryFile(const QString& templateName)
: TemporaryFile(templateName, nullptr)
{
}

TemporaryFile::TemporaryFile(QObject* parent)
: QFile(parent)
{
QTemporaryFile tmp;
tmp.open();
QFile::setFileName(tmp.fileName());
tmp.close();
}

TemporaryFile::TemporaryFile(const QString& templateName, QObject* parent)
: QFile(parent)
{
QTemporaryFile tmp(templateName);
tmp.open();
QFile::setFileName(tmp.fileName());
tmp.close();
}

bool TemporaryFile::open()
{
return QFile::open(QIODevice::ReadWrite);
}

#endif
50 changes: 50 additions & 0 deletions tests/util/TemporaryFile.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright (C) 2018 KeePassXC Team <team@keepassxc.org>
*
* 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/>.
*/

#ifndef KEEPASSXC_TEMPORARYFILE_H
#define KEEPASSXC_TEMPORARYFILE_H

#include <QTemporaryFile>

#ifdef Q_OS_WIN
/**
* QTemporaryFile does not actually close a file when close() is
* called, which causes the file to be locked on Windows.
* This class extends a QFile with the extra functionality
* of a QTemporaryFile to circumvent this problem.
*/
class TemporaryFile : public QFile
#elif
class TemporaryFile : public QTemporaryFile
#endif
{
Q_OBJECT

#ifdef Q_OS_WIN
public:
TemporaryFile();
explicit TemporaryFile(const QString& templateName);
explicit TemporaryFile(QObject* parent);
TemporaryFile(const QString& templateName, QObject* parent);
~TemporaryFile() override = default;

using QFile::open;
bool open();
#endif
};

#endif //KEEPASSXC_TEMPORARYFILE_H

0 comments on commit b3c90e9

Please sign in to comment.