# HG changeset patch # User Andre Heinecke # Date 1396020062 0 # Node ID c05e126b0b9e39dff2f5a24752a494c04cdb609e # Parent 6b4ad6ccc48e3a07de7d7dc1e4303e3a5573a7f3 Fix downloadertest and factor out generating file input. diff -r 6b4ad6ccc48e -r c05e126b0b9e ui/tests/CMakeLists.txt --- 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 diff -r 6b4ad6ccc48e -r c05e126b0b9e ui/tests/certlistparsertest.cpp --- 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 @@ -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); diff -r 6b4ad6ccc48e -r c05e126b0b9e ui/tests/certlistparsertest.h --- 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(); diff -r 6b4ad6ccc48e -r c05e126b0b9e ui/tests/common.cpp --- /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 +#include +#include +#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; +} diff -r 6b4ad6ccc48e -r c05e126b0b9e ui/tests/common.h --- /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 +#include + +/** @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 diff -r 6b4ad6ccc48e -r c05e126b0b9e ui/tests/downloadertest.cpp --- 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 #include #include -#include "unistd.h" +#include #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"); 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 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(); diff -r 6b4ad6ccc48e -r c05e126b0b9e ui/tests/downloadertest.h --- 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);