Mercurial > trustbridge
diff ui/tests/cinstprocesstest.cpp @ 310:f758460ca437
Merged
author | Sascha Wilde <wilde@intevation.de> |
---|---|
date | Fri, 04 Apr 2014 09:54:19 +0200 |
parents | 9c5e6b142611 |
children | f17226aa2e09 |
line wrap: on
line diff
--- a/ui/tests/cinstprocesstest.cpp Fri Apr 04 09:53:55 2014 +0200 +++ b/ui/tests/cinstprocesstest.cpp Fri Apr 04 09:54:19 2014 +0200 @@ -1,17 +1,31 @@ #include "cinstprocesstest.h" #include "certificatelist.h" #include "errorcodes.h" +#include "common.h" #include <QDebug> #include <QDir> #include <QFile> #include <QProcess> -#define RELATIVE_CINST_PATH "../../cinst/cinst" +#define CINST_PATH_CANDIDATES "../../cinst/cinst" << \ + "cinst" << "../../cinst/cinst.exe" << "cinst.exe"; -QProcess *CinstProcessTest::startCinstProcess() { +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->setProgram(RELATIVE_CINST_PATH); + installerProcess->setArguments(args); + installerProcess->setProgram(processPath); installerProcess->start(); installerProcess->waitForStarted(); return installerProcess; @@ -22,6 +36,7 @@ qDebug() << "Stdout:" << proc->readAllStandardOutput(); \ qDebug() << "Stderr:" << proc->readAllStandardError(); \ qDebug() << "Exit code: " << proc->exitCode(); \ + qDebug() << "Exit status: " << proc->exitStatus(); \ } \ QVERIFY(x) @@ -34,104 +49,158 @@ } void CinstProcessTest::testValidInput() { - QProcess* installerProcess = startCinstProcess(); - QVERIFY(installerProcess->state() == QProcess::Running); + QStringList args; + args << "list=" + validListFile.fileName(); - installerProcess->write("-----BEGIN CERTIFICATE LIST-----\r\n"); - installerProcess->write(validList.rawData().toLatin1()); - installerProcess->write("-----END CERTIFICATE LIST-----\r\n"); + QTemporaryFile instructions; + instructions.open(); + foreach (const Certificate &cert, validList.getCertificates()) { + instructions.write(cert.base64Line().toLatin1()); + instructions.write("\n"); + } + instructions.close(); - foreach (const Certificate &cert, validList.getCertificates()) { - installerProcess->write(cert.base64Line().toLatin1()); - installerProcess->write("\r\n"); - } + args << "instructions=" + instructions.fileName(); + QProcess* installerProcess = startCinstProcess(args); 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()); + 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()); + +/* Set HOME or APPDATA so that nss stores are not touched + * see nsstest for the real test of that code */ +#ifdef WIN32 + QVERIFY(!setenv ("APPDATA", fakeHome.path().toLocal8Bit().constData(), 1)); +#else + QVERIFY(!setenv ("HOME", fakeHome.path().toLocal8Bit().constData(), 1)); +#endif } 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"); + QTemporaryFile emptyFile; + emptyFile.open(); + emptyFile.close(); + QStringList args; + args << "list=" + emptyFile.fileName(); + + QTemporaryFile instructions; + instructions.open(); foreach (const Certificate &cert, validList.getCertificates()) { - installerProcess->write(cert.base64Line().toLatin1()); - installerProcess->write("\r\n"); + 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 ); + QStringList args; - finishVerify(installerProcess, ERR_INVALID_INPUT); + QString garbage = getRandomDataFile(21*1024*1024); + args << "list=" + garbage; + + 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); + /* If the following failed there may be leftovers in /tmp */ + finishVerify(installerProcess, ERR_INVALID_INPUT_NO_LIST); + QVERIFY(QFile::remove(garbage)); } void CinstProcessTest::testNoInput() { - QProcess* installerProcess = startCinstProcess(); - QVERIFY(installerProcess->state() == QProcess::Running); - - /* Nothing */ - installerProcess = startCinstProcess(); + QStringList args; + args << "list=foobazbuf"; + args << "instructions=bazbuffoo"; + QProcess* installerProcess; + installerProcess = startCinstProcess(args); 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"); + QTemporaryFile emptyFile; + emptyFile.open(); + emptyFile.close(); + QStringList args; + args << "list=" + validListFile.fileName(); + args << "instructions=" + emptyFile.fileName(); + + QProcess* installerProcess = startCinstProcess(args); finishVerify(installerProcess, ERR_NO_INSTRUCTIONS); } void CinstProcessTest::testInvalidInstruction() { - QProcess* installerProcess = startCinstProcess(); - QVERIFY(installerProcess->state() == QProcess::Running); + QStringList args; + args << "list=" + validListFile.fileName(); - installerProcess->write("-----BEGIN CERTIFICATE LIST-----\r\n"); - installerProcess->write(validList.rawData().toLatin1()); - installerProcess->write("-----END CERTIFICATE LIST-----\r\n"); + QTemporaryFile instructions; + instructions.open(); + foreach (const Certificate &cert, validList.getCertificates()) { + instructions.write(cert.base64Line().toLatin1()); + instructions.write("\n"); + } + instructions.write("I:ABCDEF\n"); + instructions.close(); - installerProcess->write("I:ABCDEF\r\n"); + args << "instructions=" + instructions.fileName(); + + QProcess* installerProcess = startCinstProcess(args); finishVerify(installerProcess, ERR_INVALID_INSTRUCTIONS); } void CinstProcessTest::testUninstall() { - QProcess* installerProcess = startCinstProcess(); - QVERIFY(installerProcess->state() == QProcess::Running); + QStringList args; + args << "list=" + validListFile.fileName(); + args << "instructions=uninstall"; - installerProcess->write("-----BEGIN CERTIFICATE LIST-----\r\n"); - installerProcess->write(validList.rawData().toLatin1()); - installerProcess->write("-----END CERTIFICATE LIST-----\r\n"); - - installerProcess->write("UNINSTALL\r\n"); + QProcess* installerProcess = startCinstProcess(args); finishVerify(installerProcess, ERR_NO_ERROR); }