changeset 43:5910bf9016cd

Add Downloadertest
author Andre Heinecke <aheinecke@intevation.de>
date Fri, 14 Mar 2014 15:25:47 +0000
parents 6e7ef7e95031
children b3e8e047bc2c
files ui/tests/CMakeLists.txt ui/tests/certlistparsertest.h ui/tests/data/NOTES ui/tests/data/valid_ssl_bp.key ui/tests/data/valid_ssl_bp.pem ui/tests/data/valid_ssl_bp_priv.pem ui/tests/downloadertest.cpp ui/tests/downloadertest.h
diffstat 8 files changed, 189 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/ui/tests/CMakeLists.txt	Fri Mar 14 09:48:56 2014 +0000
+++ b/ui/tests/CMakeLists.txt	Fri Mar 14 15:25:47 2014 +0000
@@ -3,6 +3,13 @@
 include_directories(${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/..)
 
 find_package(Qt5Test)
+find_program(HIAWATHA_EXECUTABLE hiawatha)
+
+if (NOT HIAWATHA_EXECUTABLE)
+   message (STATUS "WARNING: hiawatha webserver not found. Downloader tests will fail.")
+else()
+  add_definitions(-DHIAWATHA_EXECUTABLE="${HIAWATHA_EXECUTABLE}")
+endif()
 
 macro(add_m13_test _source _additional_sources)
   set(_test ${_source})
@@ -18,5 +25,6 @@
 # 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(downloadertest.cpp "${CMAKE_SOURCE_DIR}/ui/downloader.cpp")
 #add_m13_test(${CMAKE_SOURCE_DIR}/ui/main.cpp "${M13UI_SOURCES}")
 
--- a/ui/tests/certlistparsertest.h	Fri Mar 14 09:48:56 2014 +0000
+++ b/ui/tests/certlistparsertest.h	Fri Mar 14 15:25:47 2014 +0000
@@ -1,5 +1,5 @@
-#ifndef IMAPSTREAMPARSERTEST_H
-#define IMAPSTREAMPARSERTEST_H
+#ifndef CERTLISTPARSERTEST_H
+#define CERTLISTPARSERTEST_H
 
 #include <QObject>
 #include <QtTest/QtTest>
--- a/ui/tests/data/NOTES	Fri Mar 14 09:48:56 2014 +0000
+++ b/ui/tests/data/NOTES	Fri Mar 14 15:25:47 2014 +0000
@@ -24,3 +24,20 @@
 Random Data generated with:
     openssl rand -out random_2MB 2000000
     openssl rand -out random_500k 500000
+
+# Test server certificate:
+
+    gen_key type=ec ec_curve=brainpoolP256r1 filename=valid_ssl_bp.key
+    cert_write issuer_name=CN=127.0.0.1,O=Intevation\\ Test,C=DE \
+    selfsign=1 issuer_key=valid_ssl_bp.key \
+    not_before=20130101000000 not_after=20301231235959 \
+    is_ca=1 max_pathlen=0 output_file=valid_ssl_bp.pem
+    cat valid_ssl_bp.key >> valid_ssl_bp.pem
+
+    gen_key filename=valid_ssl_rsa.key
+    cert_write issuer_name=CN=127.0.0.1,O=Do_Not_Trust_Test,C=DE \
+    selfsign=1 issuer_key=valid_ssl_rsa.key \
+    not_before=20130101000000 not_after=20151231235959 \
+    is_ca=1 max_pathlen=0 output_file=valid_ssl_rsa.pem
+    cat valid_ssl_rsa.key >> valid_ssl_bp.pem
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/tests/data/valid_ssl_bp.key	Fri Mar 14 15:25:47 2014 +0000
@@ -0,0 +1,5 @@
+-----BEGIN EC PRIVATE KEY-----
+MHgCAQEEIErtfWv3qsvHWcGn5zlLY9n5x1SoFqW8KqaNhsp5Hgx2oAsGCSskAwMC
+CAEBB6FEA0IABH2j3u3CrvjXvEC0kAsDv7PGp3gGahODLzUDYrrd9D0bFSszXxAt
+I2RyWe6GXWS7/fXuGKYo4vAsK33iJwYXLWE=
+-----END EC PRIVATE KEY-----
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/tests/data/valid_ssl_bp.pem	Fri Mar 14 15:25:47 2014 +0000
@@ -0,0 +1,12 @@
+-----BEGIN CERTIFICATE-----
+MIIBuzCCAWCgAwIBAgIBATALBgcqhkjOPQQBBQ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RyWe6GXWS7/fXuGKYo4vAsK33iJwYXLWGjUzBRMA8GA1UdEwQI
+MAYBAQECAQAwHQYDVR0OBBYEFDvRCJEFEjvwrNHVVKu7Zbjgf9xfMB8GA1UdIwQY
+MBaAFDvRCJEFEjvwrNHVVKu7Zbjgf9xfMAsGByqGSM49BAEFAANIADBFAiAOY0AO
+bceDfzmO8tDmKJUtguXZJg4h9dtYCmr+6t2QwQIhAIv5jwRwqMJGTeiqsd+7JesA
+TEWkIzW5QA1Epcl3mRuG
+-----END CERTIFICATE-----
+-----BEGIN EC PRIVATE KEY-----
+MHgCAQEEIErtfWv3qsvHWcGn5zlLY9n5x1SoFqW8KqaNhsp5Hgx2oAsGCSskAwMC
+CAEBB6FEA0IABH2j3u3CrvjXvEC0kAsDv7PGp3gGahODLzUDYrrd9D0bFSszXxAt
+I2RyWe6GXWS7/fXuGKYo4vAsK33iJwYXLWE=
+-----END EC PRIVATE KEY-----
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/tests/downloadertest.cpp	Fri Mar 14 15:25:47 2014 +0000
@@ -0,0 +1,98 @@
+#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);
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/tests/downloadertest.h	Fri Mar 14 15:25:47 2014 +0000
@@ -0,0 +1,30 @@
+#ifndef DOWNLOADERTEST_H
+#define DOWNLOADERTEST_H
+
+#include <QObject>
+#include <QtTest/QtTest>
+#include <QProcess>
+#include <QTemporaryDir>
+#include <QSignalSpy>
+#include "downloader.h"
+
+class DownloaderTest: public QObject
+{
+    Q_OBJECT
+
+    void startServer(QString additionalOptions = QString());
+
+private:
+    QProcess serverProc;
+    QTemporaryDir serverConfigDir;
+
+public Q_SLOTS:
+    void downloaderError(const QString &message, Downloader::ErrorCode error);
+
+private Q_SLOTS:
+    void initTestCase();
+    void cleanupTestCase();
+    void testNewList();
+};
+#endif
+

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