# HG changeset patch # User Andre Heinecke # Date 1398270109 0 # Node ID a53286e5b12692cde581f783617fc299fc4f5563 # Parent 5ebee91c0bb8fc6e55d83fb8e4c960eeef19b68c Add failing test for certificate list creation diff -r 5ebee91c0bb8 -r a53286e5b126 ui/tests/CMakeLists.txt --- a/ui/tests/CMakeLists.txt Wed Apr 23 15:50:08 2014 +0000 +++ b/ui/tests/CMakeLists.txt Wed Apr 23 16:21:49 2014 +0000 @@ -54,6 +54,8 @@ add_dependencies(cinstprocesstest cinst) add_custom_test(commontest.cpp "") +add_custom_test(createcertlisttest.cpp "${CERTIFICATELIST_SOURCES};${CMAKE_SOURCE_DIR}/ui/sslhelp.cpp;${CMAKE_SOURCE_DIR}/ui/createcertlistdialog.cpp") + if (WIN32) add_custom_test(windowsstoretest.cpp "${CERTIFICATELIST_SOURCES};${CMAKE_SOURCE_DIR}/cinst/windowsstore.c") endif (WIN32) diff -r 5ebee91c0bb8 -r a53286e5b126 ui/tests/createcertlisttest.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ui/tests/createcertlisttest.cpp Wed Apr 23 16:21:49 2014 +0000 @@ -0,0 +1,84 @@ +/* Copyright (C) 2014 by Bundesamt für Sicherheit in der Informationstechnik + * Software engineering by Intevation GmbH + * + * This file is Free Software under the GNU GPL (v>=2) + * and comes with ABSOLUTELY NO WARRANTY! + * See LICENSE.txt for details. + */ +#include "createcertlisttest.h" +#include "sslhelp.h" +#include "createcertlistdialog.h" +#include "certificatelist.h" + +#include + +#include +#include +#include + +void CreateCertListTest::testListCreation() { + QTemporaryFile tmpFile, outputFile, keyFile; + + /* Get a valid list */ + QFile res(":/list-valid-signed.txt"); + res.open(QIODevice::ReadOnly); + tmpFile.open(); + tmpFile.write(res.readAll()); + tmpFile.close(); + CertificateList validList = CertificateList(tmpFile.fileName().toLocal8Bit().data()); + QVERIFY(validList.isValid()); + + /* Get a key */ + QFile keyRes(":/testkey-priv.pem"); + keyRes.open(QIODevice::ReadOnly); + keyFile.open(); + keyFile.write(keyRes.readAll()); + keyFile.close(); + pk_context * pk = new pk_context; + pk_init(pk); + int ret = pk_parse_keyfile(pk, keyFile.fileName().toLocal8Bit().constData(), ""); + QVERIFY(ret == 0); + + /* Write the certificates from that list to another file */ + outputFile.open(); + QDateTime current = QDateTime::currentDateTimeUtc(); + QVERIFY(CreateCertListDialog::writeList(validList.getCertificates(), + outputFile.fileName(), current, pk)); + pk_free(pk); + + CertificateList outputList = CertificateList(outputFile.fileName().toLocal8Bit().data()); + QVERIFY(outputList.isValid()); + QVERIFY(outputList.getCertificates() == validList.getCertificates()); + QVERIFY(outputList.date() == current); +} + +void CreateCertListTest::testSha256Sum() +{ + QByteArray input = "foo"; + QByteArray output = sha256sum(input); + QVERIFY(output.toBase64() == QByteArray("b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c")); +} + +void CreateCertListTest::testSignature() +{ + QByteArray hash = QByteArray::fromBase64("b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c"); + + QTemporaryFile keyFile; + QFile keyRes(":/testkey-priv.pem"); + keyRes.open(QIODevice::ReadOnly); + keyFile.open(); + keyFile.write(keyRes.readAll()); + keyFile.close(); + pk_context * pk = new pk_context; + pk_init(pk); + int ret = pk_parse_keyfile(pk, keyFile.fileName().toLocal8Bit().constData(), ""); + QVERIFY(ret == 0); + + QByteArray signature = rsaSignSHA256Hash(hash, pk); + pk_free(pk); + + QVERIFY(signature.size() == 3072 / 8); + QVERIFY(signature.toBase64() == QByteArray("KMOni98NWbt6SWd13H0JlGA1B7hBlXWH84e883s7gMrWeCCj0fUyHmdsNCyY0rmosu+o9mo2K847S3CdnxFPPJcjbfcmILZWRw0hHMtUYta1i9jypHJbz4oznuDctgXz59L4SQzzliCNUzItNoe6UpUznkS5gja4ZHbzqIj3qDVX3H86Z+qOdLICw+LXKlTs5ghsq+SdhZRAFFpHnt+URICWHjEIQKRlmIGEUIh1NgEHInHB/teFLqNGJMu1khi0MTsWDzesAEF5LQTM7Fo3fKmVxEUSbHKupluZrX1XSfnp5w3MaxBQK/t5nFvkVVdFrdEWvb68FIkMt21XqCvjyCPG2oWNh9jjfx3/R+eQ8kFbXzgUIhlZNxbB7bOCVDe2fYNxlXhy+HAqfHsIDP8qegHU+ngLck7tJHScC5dZwTCBDL6sxAvaeGyb3m6FraqaipNI+SGLii63ou9H7PlH5xWOTY9JvJDXGpfjN9U0UrZ6X5hPutOa/llT7s0pmoQb")); +} + +QTEST_GUILESS_MAIN (CreateCertListTest); diff -r 5ebee91c0bb8 -r a53286e5b126 ui/tests/createcertlisttest.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ui/tests/createcertlisttest.h Wed Apr 23 16:21:49 2014 +0000 @@ -0,0 +1,22 @@ +#ifndef CREATECERTLISTTEST_H +#define CREATECERTLISTTEST_H +/* Copyright (C) 2014 by Bundesamt für Sicherheit in der Informationstechnik + * Software engineering by Intevation GmbH + * + * This file is Free Software under the GNU GPL (v>=2) + * and comes with ABSOLUTELY NO WARRANTY! + * See LICENSE.txt for details. + */ +#include + +class CreateCertListTest: public QObject +{ + Q_OBJECT + +private Q_SLOTS: + void testListCreation(); + void testSha256Sum(); + void testSignature(); +}; + +#endif // CREATECERTLISTTEST_H diff -r 5ebee91c0bb8 -r a53286e5b126 ui/tests/data/testdata.qrc --- a/ui/tests/data/testdata.qrc Wed Apr 23 15:50:08 2014 +0000 +++ b/ui/tests/data/testdata.qrc Wed Apr 23 16:21:49 2014 +0000 @@ -10,6 +10,7 @@ import_test.pem valid_ssl_rsa.pem valid_ssl_bp.pem + testkey-priv.pem Intevation-Root-CA-2010.crt Intevation-Root-CA-2010.der