view ui/tests/downloadertest.cpp @ 43:5910bf9016cd

Add Downloadertest
author Andre Heinecke <aheinecke@intevation.de>
date Fri, 14 Mar 2014 15:25:47 +0000
parents
children c6125d73faf4
line wrap: on
line source
#include "downloadertest.h"
#include "downloader.h"

#include <QTextStream>
#include <QFile>
#include "unistd.h"

void DownloaderTest::startServer(QString additionalOptions)
{
    QFile serverConfig(serverConfigDir.path() + "/" + "hiawatha.conf");
    QFile mimeConfig(serverConfigDir.path() + "/" + "mimetype.conf");
    if (serverProc.state() == QProcess::Running) {
        serverProc.close();
    }
    serverConfig.open(QIODevice::WriteOnly);
    mimeConfig.open(QIODevice::WriteOnly);
    mimeConfig.close(); /* just an empty file */

    QTextStream configStream(&serverConfig);
    qDebug() << "Config file name: " << serverConfig.fileName();
    configStream <<
        "Binding { " << endl <<
        "Port = 44443 " << endl <<
        "SSLcertFile = " << SOURCE_DIR <<
        "/data/valid_ssl_bp_priv.pem" << endl <<
        "Interface = 127.0.0.1" << endl <<
        "}" << endl <<
        "Hostname = 127.0.0.1" << endl <<
        "WebsiteRoot = " << SOURCE_DIR << endl;
    configStream.flush();

    serverConfig.close();
    serverProc.start();
    serverProc.waitForStarted();
}

void DownloaderTest::initTestCase() {
    QStringList arguments;
    serverProc.setProgram(HIAWATHA_EXECUTABLE);
    arguments << "-d" << "-c" << serverConfigDir.path();
    serverProc.setArguments(arguments);
    qRegisterMetaType<Downloader::ErrorCode>("Downloader::ErrorCode");
    startServer();
    QTest::qWait(1000); /* Wait for the server to settle */
}

void DownloaderTest::downloaderError(const QString &message, Downloader::ErrorCode error)
{
    qDebug() << "Downloader Error: " << error << " Msg: " << message;
}

void DownloaderTest::testNewList()
{
    QFile validCert(QString::fromLatin1(SOURCE_DIR) +
            "/data/valid_ssl_bp.pem");
    validCert.open(QIODevice::ReadOnly);

    Downloader* downloader = new Downloader(this,
            QString::fromLatin1("https://localhost:44443"),
            validCert.readAll(),
            QDateTime::currentDateTime(), // Last installed SW
            QDateTime::fromString("2010", "YYYY"),
            "/list-valid.txt",
            "/random_2MB");
    validCert.close();

    QSignalSpy newListAvailable(downloader,
            SIGNAL(newListAvailable(const QString&, const QDateTime&)));
    QSignalSpy newSoftwareAvailable(downloader,
           SIGNAL(newSoftwareAvailable(const QString&, const QDateTime&)));
    QSignalSpy errors(downloader, SIGNAL(error(const QString &,
                    Downloader::ErrorCode)));
    connect(downloader, SIGNAL(error(const QString &, Downloader::ErrorCode)),
            this, SLOT(downloaderError(const QString &, Downloader::ErrorCode)));

    downloader->start();

    int waited = 0;
    while (newListAvailable.count() == 0 &&
            errors.count() == 0  && waited < 20) {
        QTest::qWait(200);
        waited++;
    }
    QVERIFY(waited != 20);
    QVERIFY(newListAvailable.count() == 1);
    QVERIFY(newSoftwareAvailable.count() == 0);
    QVERIFY(errors.count() == 0);
    delete downloader;
}

void DownloaderTest::cleanupTestCase() {
    if (serverProc.state() == QProcess::Running) {
        serverProc.close();
    }
}

QTEST_GUILESS_MAIN (DownloaderTest);

http://wald.intevation.org/projects/trustbridge/