view ui/tests/certlistparsertest.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 c05e126b0b9e
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/