view ui/tests/cinstprocesstest.cpp @ 249:6a7eb102716d

Remove code duplication by unifying the certificatelist. You should now check for isInstallCert to determine wether this certificate should be installed or removed. Leaving the getInstallCertificates and getRemoveCertificates in place for compatibilty would have been easier to keep the tests stable.
author Andre Heinecke <aheinecke@intevation.de>
date Mon, 31 Mar 2014 08:06:17 +0000
parents 37c9653b8755
children d95fd88f6616
line wrap: on
line source
#include "cinstprocesstest.h"
#include "certificatelist.h"
#include "errorcodes.h"

#include <QDebug>
#include <QDir>
#include <QFile>
#include <QProcess>

#define RELATIVE_CINST_PATH "../../cinst/cinst"

QProcess *CinstProcessTest::startCinstProcess() {
    QProcess *installerProcess = new QProcess();
    installerProcess->setProgram(RELATIVE_CINST_PATH);
    installerProcess->start();
    installerProcess->waitForStarted();
    return installerProcess;
}

#define VERIFY_PROC_DEBUG(x) \
    if (! x ) { \
        qDebug() << "Stdout:" << proc->readAllStandardOutput(); \
        qDebug() << "Stderr:" << proc->readAllStandardError(); \
        qDebug() << "Exit code: " << proc->exitCode(); \
    } \
    QVERIFY(x)

void finishVerify(QProcess *proc, int exitCode) {
    proc->closeWriteChannel();
    proc->waitForFinished();
    VERIFY_PROC_DEBUG(proc->exitStatus() == QProcess::NormalExit);
    VERIFY_PROC_DEBUG(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.getCertificates()) {
        installerProcess->write(cert.base64Line().toLatin1());
        installerProcess->write("\r\n");
    }

    finishVerify(installerProcess, ERR_NO_ERROR);
}

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.getCertificates()) {
        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 < 15 *1024 *1024 );

    finishVerify(installerProcess, ERR_INVALID_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);

    installerProcess->write("-----BEGIN CERTIFICATE LIST-----\r\n");
    installerProcess->write(validList.rawData().toLatin1());
    installerProcess->write("-----END CERTIFICATE LIST-----\r\n");

    installerProcess->write("I:ABCDEF\r\n");

    finishVerify(installerProcess, ERR_INVALID_INSTRUCTIONS);
}

void CinstProcessTest::testUninstall() {
    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");

    installerProcess->write("UNINSTALL\r\n");

    finishVerify(installerProcess, ERR_NO_ERROR);
}

QTEST_GUILESS_MAIN (CinstProcessTest);

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