Mercurial > trustbridge
changeset 468:a53286e5b126
Add failing test for certificate list creation
author | Andre Heinecke <aheinecke@intevation.de> |
---|---|
date | Wed, 23 Apr 2014 16:21:49 +0000 (2014-04-23) |
parents | 5ebee91c0bb8 |
children | f9b0014cff97 |
files | ui/tests/CMakeLists.txt ui/tests/createcertlisttest.cpp ui/tests/createcertlisttest.h ui/tests/data/testdata.qrc |
diffstat | 4 files changed, 109 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- 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)
--- /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 <polarssl/pk.h> + +#include <QDateTime> +#include <QTemporaryFile> +#include <QTest> + +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);
--- /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 <QObject> + +class CreateCertListTest: public QObject +{ + Q_OBJECT + +private Q_SLOTS: + void testListCreation(); + void testSha256Sum(); + void testSignature(); +}; + +#endif // CREATECERTLISTTEST_H
--- 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 @@ <file>import_test.pem</file> <file>valid_ssl_rsa.pem</file> <file>valid_ssl_bp.pem</file> + <file>testkey-priv.pem</file> <file>Intevation-Root-CA-2010.crt</file> <file>Intevation-Root-CA-2010.der</file> </qresource>