Mercurial > trustbridge
changeset 240:c05e126b0b9e
Fix downloadertest and factor out generating file input.
author | Andre Heinecke <aheinecke@intevation.de> |
---|---|
date | Fri, 28 Mar 2014 15:21:02 +0000 |
parents | 6b4ad6ccc48e |
children | 4b67cc2d4dad |
files | ui/tests/CMakeLists.txt ui/tests/certlistparsertest.cpp ui/tests/certlistparsertest.h ui/tests/common.cpp ui/tests/common.h ui/tests/downloadertest.cpp ui/tests/downloadertest.h |
diffstat | 7 files changed, 70 insertions(+), 41 deletions(-) [+] |
line wrap: on
line diff
--- a/ui/tests/CMakeLists.txt Fri Mar 28 14:20:08 2014 +0000 +++ b/ui/tests/CMakeLists.txt Fri Mar 28 15:21:02 2014 +0000 @@ -29,13 +29,13 @@ # Add the current source dir to the definition # so that it can be used in file names in the tests. add_definitions(-DSOURCE_DIR="${CMAKE_CURRENT_SOURCE_DIR}") -add_m13_test(certlistparsertest.cpp "${CERTIFICATELIST_SOURCES}") +add_m13_test(certlistparsertest.cpp "${CERTIFICATELIST_SOURCES};${CMAKE_CURRENT_SOURCE_DIR}/common.cpp") # Downloader if (HIAWATHA_EXECUTABLE) set(DOWNLOADER_SOURCES_WITH_RESOURCES ${DOWNLOADER_SOURCES}) qt5_add_resources(DOWNLOADER_SOURCES_WITH_RESOURCES ${M13UI_RESOURCES}) - add_m13_test(downloadertest.cpp "${DOWNLOADER_SOURCES_WITH_RESOURCES}") + add_m13_test(downloadertest.cpp "${DOWNLOADER_SOURCES_WITH_RESOURCES};${CMAKE_CURRENT_SOURCE_DIR}/common.cpp") endif() # Cinstprocess
--- a/ui/tests/certlistparsertest.cpp Fri Mar 28 14:20:08 2014 +0000 +++ b/ui/tests/certlistparsertest.cpp Fri Mar 28 15:21:02 2014 +0000 @@ -1,6 +1,7 @@ #include "certlistparsertest.h" #include "certificatelist.h" #include "certificate.h" +#include "common.h" #include <QDebug> @@ -114,37 +115,6 @@ delete certList; } -QString CertListTest::getRandomDataFile(size_t size) -{ - QTemporaryFile tmpfile; - tmpfile.setAutoRemove(false); - tmpfile.open(); - size_t bufsize = 1024 * 1024; - if (bufsize > size) { - bufsize = size; - } - char buf[bufsize]; - - for (size_t i = 0; i < bufsize; i++) { - buf[i] = (char) qrand() % 255; - } - - size_t bytesWritten=0; - int retval = 0; - do { - size_t toWrite = size - bytesWritten; - if (toWrite < bufsize) { - retval = tmpfile.write(buf, toWrite); - } else { - retval = tmpfile.write(buf, bufsize); - } - bytesWritten += retval; - } while (retval != -1 && bytesWritten < size); - - tmpfile.close(); - return tmpfile.fileName(); -} - void CertListTest::testTooLarge() { QString fname = getRandomDataFile(MAX_LINE_LENGTH * MAX_LINES + 1);
--- a/ui/tests/certlistparsertest.h Fri Mar 28 14:20:08 2014 +0000 +++ b/ui/tests/certlistparsertest.h Fri Mar 28 15:21:02 2014 +0000 @@ -11,7 +11,6 @@ Q_OBJECT CertificateList* testWithFile(const char *filename); - QString getRandomDataFile(size_t size); private Q_SLOTS: void testInvalidSig();
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ui/tests/common.cpp Fri Mar 28 15:21:02 2014 +0000 @@ -0,0 +1,42 @@ +#include <QString> +#include <QTemporaryFile> +#include <QDebug> +#include "common.h" + +QString getRandomDataFile(size_t size, const QDir &inDir) +{ + QTemporaryFile *tmpfile; + if (inDir != QDir()) { + tmpfile = new QTemporaryFile(inDir.path() + "/downloadertest"); + } else { + tmpfile = new QTemporaryFile(); + } + tmpfile->setAutoRemove(false); + tmpfile->open(); + size_t bufsize = 1024 * 1024; + if (bufsize > size) { + bufsize = size; + } + char buf[bufsize]; + + for (size_t i = 0; i < bufsize; i++) { + buf[i] = (char) qrand() % 255; + } + + size_t bytesWritten=0; + int retval = 0; + do { + size_t toWrite = size - bytesWritten; + if (toWrite < bufsize) { + retval = tmpfile->write(buf, toWrite); + } else { + retval = tmpfile->write(buf, bufsize); + } + bytesWritten += retval; + } while (retval != -1 && bytesWritten < size); + + tmpfile->close(); + QString ret = tmpfile->fileName(); + delete tmpfile; + return ret; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ui/tests/common.h Fri Mar 28 15:21:02 2014 +0000 @@ -0,0 +1,11 @@ +#ifndef COMMON_H +#define COMMON_H +#include <QDir> +#include <QString> + +/** @file common.h + * @brief Common testing code used in multiple tests. */ + +/* @brief get the file name of a file filled with garbage */ +QString getRandomDataFile(size_t size, const QDir &inDir = QDir()); +#endif // COMMON_H
--- a/ui/tests/downloadertest.cpp Fri Mar 28 14:20:08 2014 +0000 +++ b/ui/tests/downloadertest.cpp Fri Mar 28 15:21:02 2014 +0000 @@ -1,10 +1,11 @@ #include "downloadertest.h" #include "downloader.h" +#include "common.h" #include <QTextStream> #include <QFile> #include <QVariant> -#include "unistd.h" +#include <unistd.h> #define MAX_WAIT 20 @@ -28,7 +29,6 @@ mimeConfig.close(); /* just an empty file */ QTextStream configStream(&serverConfig); - qDebug() << "Config file name: " << serverConfig.fileName(); configStream << "Binding { " << endl << "Port = 44443 " << endl << @@ -37,7 +37,7 @@ "Interface = 127.0.0.1" << endl << "}" << endl << "Hostname = 127.0.0.1" << endl << - "WebsiteRoot = " << SOURCE_DIR << "/data" << endl; + "WebsiteRoot = " << serverConfigDir.path() << endl; configStream.flush(); serverConfig.close(); @@ -52,6 +52,10 @@ serverProc.setArguments(arguments); qRegisterMetaType<SSLConnection::ErrorCode>("SSLConnection::ErrorCode"); startServer(); + garbageInfo = QFileInfo(getRandomDataFile(2 * 1024, serverConfigDir.path())); + QVERIFY(QFile::copy(QString::fromLocal8Bit(SOURCE_DIR"/data/list-valid.txt"), + serverConfigDir.path() + "/" + "list-valid.txt")); + QTest::qWait(1000); /* Wait for the server to settle */ } @@ -80,12 +84,14 @@ "/data/valid_ssl_rsa.pem"); otherCert.open(QIODevice::ReadOnly); + QFileInfo fi(getRandomDataFile(200)); + Downloader* downloader = new Downloader(this, QString::fromLatin1("https://localhost:44443"), otherCert.readAll(), QDateTime::currentDateTime(), // Last installed SW QDateTime::fromString("2010", "YYYY"), - "/garbage_2MB", + "/" + garbageInfo.fileName(), "/list-valid.txt"); otherCert.close(); @@ -146,7 +152,7 @@ validCert.readAll(), QDateTime::fromString("2010", "YYYY"), QDateTime::currentDateTime(), - "/garbage_2MB", + "/" + garbageInfo.fileName(), "/list-valid.txt"); validCert.close(); @@ -170,7 +176,7 @@ QList<QVariant> arguments = newSoftwareAvailable.takeFirst(); - QVERIFY(filesEqual(QString::fromLatin1(SOURCE_DIR) + "/data/garbage_2MB", + QVERIFY(filesEqual(serverConfigDir.path() + "/" + garbageInfo.fileName(), arguments.at(0).toString())); delete downloader; @@ -187,7 +193,7 @@ validCert.readAll(), QDateTime::currentDateTime(), // Last installed SW QDateTime::fromString("2010", "YYYY"), - "/garbage_2MB", + "/" + garbageInfo.fileName(), "/list-valid.txt"); validCert.close();
--- a/ui/tests/downloadertest.h Fri Mar 28 14:20:08 2014 +0000 +++ b/ui/tests/downloadertest.h Fri Mar 28 15:21:02 2014 +0000 @@ -17,6 +17,7 @@ private: QProcess serverProc; QTemporaryDir serverConfigDir; + QFileInfo garbageInfo; public Q_SLOTS: void downloaderError(const QString &message, SSLConnection::ErrorCode error);