view ui/tests/certlistparsertest.cpp @ 47:7e37c6033c81

Verify that the downloaded file matches the served one
author Andre Heinecke <aheinecke@intevation.de>
date Mon, 17 Mar 2014 11:03:17 +0000
parents 6e7ef7e95031
children a7b8cb29f1f6
line wrap: on
line source
#include "certlistparsertest.h"
#include "certificatelist.h"

#include <QDebug>

void CertListTest::testValidList()
{
    const char *fname = "list-valid-signed.txt";
    CertificateList *certList = testWithFile(fname);
    QCOMPARE (certList->getStatus(), Valid);
    QVERIFY (certList->isValid());
    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(), UnknownError);
    delete certList;
}

void CertListTest::testGarbage()
{
    const char *fnames[] = {"random_500KB",
        "list-with-null.txt",
        NULL};
    for (int i=0; fnames[i] != NULL; i++) {
        CertificateList *certList = testWithFile(fnames[i]);
        QCOMPARE (certList->getStatus(), InvalidFormat);
        delete certList;
    }
}

void CertListTest::testTooLarge()
{
    const char *fname = "random_2MB";
    CertificateList *certList = testWithFile(fname);
    QCOMPARE (certList->getStatus(), TooLarge);
    QVERIFY (!certList->isValid());
    delete certList;
}

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

    QBENCHMARK_ONCE {
        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/