view ui/tests/cinstprocesstest.cpp @ 297:f733f4aa2a90

Return 0 for linux uninstall for now even if it does nothing
author Andre Heinecke <aheinecke@intevation.de>
date Wed, 02 Apr 2014 16:12:18 +0000
parents 0b1453070561
children 56507c3d5f1e
line wrap: on
line source
#include "cinstprocesstest.h"
#include "certificatelist.h"
#include "errorcodes.h"

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

#define CINST_PATH_CANDIDATES "../../cinst/cinst" << \
    "cinst" << "../../cinst/cinst.exe" << "cinst.exe";

QProcess *CinstProcessTest::startCinstProcess(const QStringList& args) {
    QStringList cinstCandidates;
    cinstCandidates << CINST_PATH_CANDIDATES;
    QString processPath;
    foreach (const QString& candidate, cinstCandidates) {
        QFileInfo fi(candidate);
        if (fi.isExecutable()) {
            processPath = candidate;
            break;
        }
    }

    QProcess *installerProcess = new QProcess();
    installerProcess->setArguments(args);
    installerProcess->setProgram(processPath);
    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() {
    QStringList args;
    args << "list=" + validListFile.fileName();

    QTemporaryFile instructions;
    instructions.open();
    foreach (const Certificate &cert, validList.getCertificates()) {
        instructions.write(cert.base64Line().toLatin1());
        instructions.write("\n");
    }
    instructions.close();

    args << "instructions=" + instructions.fileName();

    QProcess* installerProcess = startCinstProcess(args);
    finishVerify(installerProcess, ERR_NO_ERROR);
}

void CinstProcessTest::initTestCase() {
    QFile valid(":/list-valid-signed.txt");
    valid.open(QIODevice::ReadOnly);
    validListFile.open();
    validListFile.write(valid.readAll());
    valid.close();
    validListFile.close();
    validList = CertificateList(validListFile.fileName().toLocal8Bit().data());

    QVERIFY(validList.isValid());

    QFile invalid(":/list-invalid-signed.txt");
    invalid.open(QIODevice::ReadOnly);
    invalidListFile.open();
    invalidListFile.write(invalid.readAll());
    invalid.close();
    invalidListFile.close();
    invalidList = CertificateList(invalidListFile.fileName().toLocal8Bit().data());

    QVERIFY(!invalidList.isValid());

    QFile other(":/list-valid-other-signature.txt");
    other.open(QIODevice::ReadOnly);
    otherListFile.open();
    otherListFile.write(other.readAll());
    other.close();
    otherListFile.close();
    otherList = CertificateList(otherListFile.fileName().toLocal8Bit().data());

    QVERIFY(!otherList.isValid());
}

void CinstProcessTest::testNoList() {
    /* No list */
    QTemporaryFile emptyFile;
    emptyFile.open();
    emptyFile.close();

    QStringList args;
    args << "list=" + emptyFile.fileName();

    QTemporaryFile instructions;
    instructions.open();
    foreach (const Certificate &cert, validList.getCertificates()) {
        instructions.write(cert.base64Line().toLatin1());
        instructions.write("\n");
    }
    instructions.close();

    args << "instructions=" + instructions.fileName();

    QProcess* installerProcess = startCinstProcess(args);
    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/