view ui/tests/certlistparsertest.cpp @ 286:881ce5126f07

Add helper function to get all certificates in a list
author Andre Heinecke <aheinecke@intevation.de>
date Wed, 02 Apr 2014 13:47:24 +0000
parents 6a7eb102716d
children ee59ab0eb7ff
line wrap: on
line source
#include "certlistparsertest.h"
#include "certificatelist.h"
#include "certificate.h"
#include "common.h"

#include <QDebug>

void CertListTest::testValidList()
{
    const char *fname = "list-valid-signed.txt";

    QDir dataDir = QDir(SOURCE_DIR"/data/");
    QString fileName = dataDir.absoluteFilePath(fname);
    QFile validList(fileName);
    validList.open(QIODevice::ReadOnly);
    QString validData = QString::fromLatin1(validList.readAll());
    QStringList instLines;
    QStringList remoLines;

    CertificateList *certList = testWithFile(fname);
    QCOMPARE(certList->getStatus(), Valid);
    QVERIFY(certList->isValid());

    QVERIFY(certList->rawData() == validData.toLatin1());

    const QList<Certificate> cList = certList->getCertificates();

    foreach (QString act, validData.split("\r\n")) {
        if (act.startsWith("I:")) {
            instLines << act;
            continue;
        }
        if (act.startsWith("R:")) {
            remoLines << act;
            continue;
        }
    }

    int instCnt = 0,
        remoCnt = 0;
    foreach (const Certificate& cert, cList) {
        QVERIFY(cert.isValid());
        if (cert.isInstallCert()) {
            QVERIFY(instLines.contains(cert.base64Line()));
            instCnt++;
        } else {
            QVERIFY(remoLines.contains(cert.base64Line()));
            remoCnt++;
        }
    }

    /* Be variable if test data changes later */
    QVERIFY(instCnt >= 1);
    QVERIFY(remoCnt >= 1);
    QVERIFY(instLines.size() == instCnt);
    QVERIFY(remoLines.size() == remoCnt);

    /* Check that a default certificate is invalid */
    Certificate cert;
    QVERIFY(!cert.isValid());

    certList->readList(fileName.toLocal8Bit().data());

    const QList<Certificate> cList2 = certList->getCertificates();
    QVERIFY(instLines.size() + remoLines.size() == cList2.size());

    delete certList;
}

void CertListTest::testInvalidSig()
{
    const char *fnames[] = {"list-invalid-signed.txt",
        "list-valid-other-signature.txt",
        "list-valid-sha1-signature.txt",
        NULL};
    for (int i=0; fnames[i] != NULL; i++) {
        CertificateList *certList = testWithFile(fnames[i]);
        QCOMPARE (certList->getStatus(), InvalidSignature);
        delete certList;
    }
}

void verifyInvalidFile(const char *fName) {
    CertificateList *certList = new CertificateList(fName);
    QVERIFY (certList->getStatus() != Valid);
    delete certList;
}

void CertListTest::testInvalidFileNames()
{
    verifyInvalidFile("/dev/random");
    verifyInvalidFile("/tmp/");
    verifyInvalidFile(NULL);
    verifyInvalidFile("ä");
    verifyInvalidFile("💩 ");
}

void CertListTest::testEmptyFile()
{
    const char *fname = "empty_file";
    CertificateList *certList = testWithFile(fname);
    QCOMPARE (certList->getStatus(), SeekFailed);
    delete certList;
}

void CertListTest::testGarbage()
{
    const char *fname = "list-with-null.txt";
    QString fname2 = getRandomDataFile(200);
    CertificateList *certList = testWithFile(fname);
    QCOMPARE (certList->getStatus(), InvalidFormat);
    delete certList;
    certList = testWithFile(fname2.toLocal8Bit().constData());
    QVERIFY(QFile::remove(fname2));
    QCOMPARE (certList->getStatus(), InvalidFormat);
    delete certList;
}

void CertListTest::testTooLarge()
{
    QString fname = getRandomDataFile(MAX_LINE_LENGTH * MAX_LINES + 1);
    CertificateList *certList = testWithFile(fname.toLocal8Bit().constData());
    QVERIFY(QFile::remove(fname));
    QCOMPARE(certList->getStatus(), TooLarge);
    QVERIFY(!certList->isValid());
    delete certList;
}

void CertListTest::benchmarkValid()
{
    const char *fname = "list-valid-signed.txt";

    QBENCHMARK{
        CertificateList *certList = testWithFile(fname);
        delete certList;
    }
}

CertificateList* CertListTest::testWithFile(const char *filename)
{
    QDir dataDir = QDir(SOURCE_DIR"/data/");
    QString fileName = dataDir.absoluteFilePath(filename);
    return new CertificateList(fileName.toLocal8Bit().data());
}

int main( int argc, char **argv )
{
  CertListTest tc;
  return QTest::qExec( &tc, argc, argv );
}


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