view ui/tests/certlistparsertest.cpp @ 202:0861069fd6d0

Fix testing for handling garbage input and generate the data ad hoc.
author Andre Heinecke <andre.heinecke@intevation.de>
date Wed, 26 Mar 2014 12:52:53 +0100
parents b0b1375dfd40
children 60c5df8e7980
line wrap: on
line source
#include "certlistparsertest.h"
#include "certificatelist.h"
#include "certificate.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> instList = certList->getInstallCertificates();
    const QList<Certificate> remoList = certList->getRemoveCertificates();

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

    foreach (const Certificate& cert, instList) {
        QVERIFY(cert.isValid());
        QVERIFY(instLines.contains(cert.base64Line()));
    }
    foreach (const Certificate& cert, remoList) {
        QVERIFY(cert.isValid());
        QVERIFY(remoLines.contains(cert.base64Line()));
    }

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

    Certificate cert;
    QVERIFY(!cert.isValid());

    CertificateList *certList2= testWithFile(fname);
    const QList<Certificate> instList2 = certList2->getInstallCertificates();
    const QList<Certificate> remoList2 = certList2->getRemoveCertificates();
    QVERIFY(instLines.size() == instList2.size());
    QVERIFY(remoLines.size() == remoList2.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);
    printf("Status :%i\n ",certList->getStatus());
    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;
}

QString CertListTest::getRandomDataFile(size_t size)
{
    QTemporaryFile tmpfile;
    tmpfile.setAutoRemove(false);
    tmpfile.open();
    size_t bufsize = 1024 * 1024;
    if (bufsize > size) {
        bufsize = size;
    }
    char buf[bufsize];

    for (size_t i = 0; i < bufsize; i++) {
        buf[i] = (char) qrand() % 255;
    }

    size_t bytesWritten=0;
    int retval = 0;
    do {
        size_t toWrite = size - bytesWritten;
        if (toWrite < bufsize) {
            retval = tmpfile.write(buf, toWrite);
        } else {
            retval = tmpfile.write(buf, bufsize);
        }
        bytesWritten += retval;
    } while (retval != -1 && bytesWritten < size);

    tmpfile.close();
    return tmpfile.fileName();
}

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/