Mercurial > trustbridge
annotate 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 |
rev | line source |
---|---|
468
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
1 /* Copyright (C) 2014 by Bundesamt für Sicherheit in der Informationstechnik |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
2 * Software engineering by Intevation GmbH |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
3 * |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
4 * This file is Free Software under the GNU GPL (v>=2) |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
5 * and comes with ABSOLUTELY NO WARRANTY! |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
6 * See LICENSE.txt for details. |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
7 */ |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
8 #include "createcertlisttest.h" |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
9 #include "sslhelp.h" |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
10 #include "createcertlistdialog.h" |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
11 #include "certificatelist.h" |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
12 |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
13 #include <polarssl/pk.h> |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
14 |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
15 #include <QDateTime> |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
16 #include <QTemporaryFile> |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
17 #include <QTest> |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
18 |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
19 void CreateCertListTest::testListCreation() { |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
20 QTemporaryFile tmpFile, outputFile, keyFile; |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
21 |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
22 /* Get a valid list */ |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
23 QFile res(":/list-valid-signed.txt"); |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
24 res.open(QIODevice::ReadOnly); |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
25 tmpFile.open(); |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
26 tmpFile.write(res.readAll()); |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
27 tmpFile.close(); |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
28 CertificateList validList = CertificateList(tmpFile.fileName().toLocal8Bit().data()); |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
29 QVERIFY(validList.isValid()); |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
30 |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
31 /* Get a key */ |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
32 QFile keyRes(":/testkey-priv.pem"); |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
33 keyRes.open(QIODevice::ReadOnly); |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
34 keyFile.open(); |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
35 keyFile.write(keyRes.readAll()); |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
36 keyFile.close(); |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
37 pk_context * pk = new pk_context; |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
38 pk_init(pk); |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
39 int ret = pk_parse_keyfile(pk, keyFile.fileName().toLocal8Bit().constData(), ""); |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
40 QVERIFY(ret == 0); |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
41 |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
42 /* Write the certificates from that list to another file */ |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
43 outputFile.open(); |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
44 QDateTime current = QDateTime::currentDateTimeUtc(); |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
45 QVERIFY(CreateCertListDialog::writeList(validList.getCertificates(), |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
46 outputFile.fileName(), current, pk)); |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
47 pk_free(pk); |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
48 |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
49 CertificateList outputList = CertificateList(outputFile.fileName().toLocal8Bit().data()); |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
50 QVERIFY(outputList.isValid()); |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
51 QVERIFY(outputList.getCertificates() == validList.getCertificates()); |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
52 QVERIFY(outputList.date() == current); |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
53 } |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
54 |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
55 void CreateCertListTest::testSha256Sum() |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
56 { |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
57 QByteArray input = "foo"; |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
58 QByteArray output = sha256sum(input); |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
59 QVERIFY(output.toBase64() == QByteArray("b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c")); |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
60 } |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
61 |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
62 void CreateCertListTest::testSignature() |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
63 { |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
64 QByteArray hash = QByteArray::fromBase64("b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c"); |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
65 |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
66 QTemporaryFile keyFile; |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
67 QFile keyRes(":/testkey-priv.pem"); |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
68 keyRes.open(QIODevice::ReadOnly); |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
69 keyFile.open(); |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
70 keyFile.write(keyRes.readAll()); |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
71 keyFile.close(); |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
72 pk_context * pk = new pk_context; |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
73 pk_init(pk); |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
74 int ret = pk_parse_keyfile(pk, keyFile.fileName().toLocal8Bit().constData(), ""); |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
75 QVERIFY(ret == 0); |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
76 |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
77 QByteArray signature = rsaSignSHA256Hash(hash, pk); |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
78 pk_free(pk); |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
79 |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
80 QVERIFY(signature.size() == 3072 / 8); |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
81 QVERIFY(signature.toBase64() == QByteArray("KMOni98NWbt6SWd13H0JlGA1B7hBlXWH84e883s7gMrWeCCj0fUyHmdsNCyY0rmosu+o9mo2K847S3CdnxFPPJcjbfcmILZWRw0hHMtUYta1i9jypHJbz4oznuDctgXz59L4SQzzliCNUzItNoe6UpUznkS5gja4ZHbzqIj3qDVX3H86Z+qOdLICw+LXKlTs5ghsq+SdhZRAFFpHnt+URICWHjEIQKRlmIGEUIh1NgEHInHB/teFLqNGJMu1khi0MTsWDzesAEF5LQTM7Fo3fKmVxEUSbHKupluZrX1XSfnp5w3MaxBQK/t5nFvkVVdFrdEWvb68FIkMt21XqCvjyCPG2oWNh9jjfx3/R+eQ8kFbXzgUIhlZNxbB7bOCVDe2fYNxlXhy+HAqfHsIDP8qegHU+ngLck7tJHScC5dZwTCBDL6sxAvaeGyb3m6FraqaipNI+SGLii63ou9H7PlH5xWOTY9JvJDXGpfjN9U0UrZ6X5hPutOa/llT7s0pmoQb")); |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
82 } |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
83 |
a53286e5b126
Add failing test for certificate list creation
Andre Heinecke <aheinecke@intevation.de>
parents:
diff
changeset
|
84 QTEST_GUILESS_MAIN (CreateCertListTest); |