diff ui/tests/createcertlisttest.cpp @ 468:a53286e5b126

Add failing test for certificate list creation
author Andre Heinecke <aheinecke@intevation.de>
date Wed, 23 Apr 2014 16:21:49 +0000
parents
children a166d740cb5a
line wrap: on
line diff
--- /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);

http://wald.intevation.org/projects/trustbridge/