changeset 100:8fa273791242

Add test for cinstprocess
author Andre Heinecke <aheinecke@intevation.de>
date Fri, 21 Mar 2014 12:15:29 +0000
parents bc1e6732f43c
children 7e2d08555112
files ui/tests/CMakeLists.txt ui/tests/cinstprocesstest.cpp ui/tests/cinstprocesstest.h
diffstat 3 files changed, 159 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/ui/tests/CMakeLists.txt	Fri Mar 21 11:54:35 2014 +0000
+++ b/ui/tests/CMakeLists.txt	Fri Mar 21 12:15:29 2014 +0000
@@ -1,6 +1,6 @@
 set(EXECUTABLE_OUTPUT_PATH ${CMAKE_CURRENT_BINARY_DIR})
 
-include_directories(${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/..)
+include_directories(${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/.. ${CMAKE_CURRENT_SOURCE_DIR}/../../common)
 
 find_package(Qt5Test)
 find_program(HIAWATHA_EXECUTABLE hiawatha)
@@ -31,5 +31,8 @@
 qt5_add_resources(DOWNLOADER_SOURCES_WITH_RESOURCES ${M13UI_RESOURCES})
 add_m13_test(downloadertest.cpp "${DOWNLOADER_SOURCES_WITH_RESOURCES}")
 
+# Cinstprocess
+add_m13_test(cinstprocesstest.cpp "${CERTIFICATELIST_SOURCES}")
+
 #add_m13_test(${CMAKE_SOURCE_DIR}/ui/main.cpp "${M13UI_SOURCES}")
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/tests/cinstprocesstest.cpp	Fri Mar 21 12:15:29 2014 +0000
@@ -0,0 +1,122 @@
+#include "cinstprocesstest.h"
+#include "certificatelist.h"
+#include "errorcodes.h"
+
+#include <QDebug>
+#include <QDir>
+#include <QFile>
+#include <QProcess>
+#include <QTest>
+
+#define RELATIVE_CINST_PATH "../../cinst/cinst"
+
+QProcess *CinstProcessTest::startCinstProcess() {
+    QProcess *installerProcess = new QProcess();
+    installerProcess->setProgram(RELATIVE_CINST_PATH);
+    installerProcess->start();
+    installerProcess->waitForStarted();
+    return installerProcess;
+}
+
+void finishVerify(QProcess *proc, int exitCode) {
+    proc->closeWriteChannel();
+    proc->waitForFinished();
+    QVERIFY(proc->exitStatus() == QProcess::NormalExit);
+    QVERIFY(proc->exitCode() == exitCode);
+    delete proc;
+}
+
+void CinstProcessTest::testValidInput() {
+    QProcess* installerProcess = startCinstProcess();
+    QVERIFY(installerProcess->state() == QProcess::Running);
+
+    installerProcess->write("-----BEGIN CERTIFICATE LIST-----\r\n");
+    installerProcess->write(validList.rawData().toLatin1());
+    installerProcess->write("-----END CERTIFICATE LIST-----\r\n");
+
+    foreach (const Certificate &cert, validList.getInstallCertificates()) {
+        installerProcess->write(cert.base64Line().toLatin1());
+        installerProcess->write("\r\n");
+    }
+
+    finishVerify(installerProcess, 0);
+}
+
+void CinstProcessTest::initTestCase() {
+    QDir dataDir = QDir(SOURCE_DIR"/data/");
+    QString fileName = dataDir.absoluteFilePath("list-valid-signed.txt");
+    validList = CertificateList(fileName.toLocal8Bit().data());
+}
+
+void CinstProcessTest::testNoList() {
+    /* No list */
+    QProcess* installerProcess = startCinstProcess();
+    QVERIFY(installerProcess->state() == QProcess::Running);
+    installerProcess->write("-----BEGIN CERTIFICATE LIST-----\r\n");
+    installerProcess->write("-----END CERTIFICATE LIST-----\r\n");
+
+    foreach (const Certificate &cert, validList.getInstallCertificates()) {
+        installerProcess->write(cert.base64Line().toLatin1());
+        installerProcess->write("\r\n");
+    }
+    finishVerify(installerProcess, ERR_INVALID_INPUT_NO_LIST);
+}
+
+void CinstProcessTest::testGarbageInput() {
+    QProcess* installerProcess = startCinstProcess();
+    QVERIFY(installerProcess->state() == QProcess::Running);
+    /* Garbage */
+    installerProcess = startCinstProcess();
+    installerProcess->write("-----BEGIN CERTIFICATE LIST-----\r\n");
+    int retval=0;
+    int bytesWritten=0;
+    do {
+        char garbage[1030];
+        for (int i = 0; i < 1030; i++) {
+            garbage[i] = (char) qrand() % 255;
+        }
+        retval = installerProcess->write(garbage, 1030);
+        bytesWritten += retval;
+    } while (retval != -1 && bytesWritten < 100 *1024 *1024 );
+
+    QVERIFY (bytesWritten  < 100 *1024 *1024);
+
+    finishVerify(installerProcess, ERR_TOO_MUCH_INPUT);
+}
+
+void CinstProcessTest::testNoInput() {
+    QProcess* installerProcess = startCinstProcess();
+    QVERIFY(installerProcess->state() == QProcess::Running);
+
+    /* Nothing */
+    installerProcess = startCinstProcess();
+    finishVerify(installerProcess, ERR_INVALID_INPUT_NO_LIST);
+}
+
+
+void CinstProcessTest::testNoInstructions() {
+    /* No instructions */
+    QProcess* installerProcess = startCinstProcess();
+    QVERIFY(installerProcess->state() == QProcess::Running);
+    installerProcess->write("-----BEGIN CERTIFICATE LIST-----\r\n");
+    installerProcess->write(validList.rawData().toLatin1());
+    installerProcess->write("-----END CERTIFICATE LIST-----\r\n");
+
+    finishVerify(installerProcess, ERR_NO_INSTRUCTIONS);
+}
+
+void CinstProcessTest::testInvalidInstruction() {
+    QProcess* installerProcess = startCinstProcess();
+    QVERIFY(installerProcess->state() == QProcess::Running);
+
+    /* foobar as instruction */
+    installerProcess->write("-----BEGIN CERTIFICATE LIST-----\r\n");
+    installerProcess->write(validList.rawData().toLatin1());
+    installerProcess->write("-----END CERTIFICATE LIST-----\r\n");
+
+    installerProcess->write("foobar");
+
+    finishVerify(installerProcess, ERR_INVALID_INSTRUCTIONS);
+}
+
+QTEST_GUILESS_MAIN (CinstProcessTest);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/tests/cinstprocesstest.h	Fri Mar 21 12:15:29 2014 +0000
@@ -0,0 +1,33 @@
+#ifndef CINSTPROCESSTEST_H
+#define CINSTPROCESSTEST_H
+
+/* @file Tests for the cinst process
+ * @brief Tests for the cinst process as well as the UI side
+ */
+
+#include <QObject>
+#include <QProcess>
+#include "certificatelist.h"
+
+class CinstProcessTest: public QObject
+{
+    Q_OBJECT
+
+private:
+    QProcess* startCinstProcess();
+    CertificateList validList;
+    CertificateList otherList;
+    CertificateList invalidList;
+
+private Q_SLOTS:
+    void initTestCase();
+    void testValidInput();
+    void testInvalidInstruction();
+    void testNoInput();
+    void testNoList();
+    void testGarbageInput();
+    void testNoInstructions();
+};
+#endif
+
+

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