comparison ui/tests/windowsstoretest.cpp @ 220:e6c5c70a67b0

Add test for windowsstores
author Andre Heinecke <andre.heinecke@intevation.de>
date Wed, 26 Mar 2014 20:21:55 +0100
parents
children 53ea9b975d1c
comparison
equal deleted inserted replaced
219:57bef180d560 220:e6c5c70a67b0
1 #include "windowsstoretest.h"
2 #include "certificatelist.h"
3 #include "strhelp.h"
4 #include "certificate.h"
5 #include "../cinst/windowsstore.h"
6
7 #include <QTest>
8
9 void WindowsStoreTest::initTestCase() {
10 testStore = CertOpenStore(
11 CERT_STORE_PROV_MEMORY, // A memory store
12 0, // Encoding type
13 // Not used with a memory store
14 0, // Use the default provider
15 0, // No flags
16 NULL); // Not needed
17 QVERIFY (testStore);
18 QFile res(":/list-valid-signed.txt");
19 res.open(QIODevice::ReadOnly);
20 tmpFile.open();
21 tmpFile.write(res.readAll());
22 tmpFile.close();
23
24 validList = CertificateList(tmpFile.fileName().toLocal8Bit().data());
25 }
26
27 void WindowsStoreTest::testInstRemove() {
28 char ** to_install = NULL,
29 ** to_remove = NULL;
30 PCCERT_CONTEXT pCert = NULL;
31 size_t i = 0;
32
33 foreach (const Certificate &cert, validList.getInstallCertificates()) {
34 strv_append (&to_install, cert.base64Line().toLatin1().constData() + 2,
35 cert.base64Line().size() - 2);
36 }
37
38 /* Just a quick check for str_append_str functionality */
39 QVERIFY((size_t) validList.getInstallCertificates().size() == strv_length(to_install));
40 for (i = 0; i < strv_length(to_install); i++) {
41 QVERIFY (validList.getInstallCertificates()[i].base64Line().right(
42 validList.getInstallCertificates()[i].base64Line().size() - 2) ==
43 QString::fromLatin1(to_install[i]));
44 }
45
46 do_install(testStore, to_install);
47
48 i = 0;
49 while((pCert = CertEnumCertificatesInStore(testStore, pCert))) {
50 bool certFound = false;
51 QByteArray data = QByteArray::fromRawData ((const char *)pCert->pbCertEncoded,
52 pCert->cbCertEncoded);
53 foreach (const Certificate &cert, validList.getInstallCertificates()) {
54 QByteArray asn1data = QByteArray::fromBase64(
55 cert.base64Line().right(cert.base64Line().size() - 2).toLatin1());
56 if (asn1data == data) {
57 certFound = true;
58 }
59 }
60 QVERIFY(certFound);
61 i++;
62 }
63 qDebug() << i;
64 QVERIFY ((size_t)validList.getInstallCertificates().size() == i);
65
66 /* Remove all except one */
67 for (i = 0; i < strv_length(to_install) - 1; i++) {
68 strv_append(&to_remove, to_install[i], qstrlen(to_install[i]));
69 }
70
71 do_remove(testStore, to_remove);
72
73 i = 0;
74 while((pCert = CertEnumCertificatesInStore(testStore, pCert))) {
75 i++;
76 }
77
78 QVERIFY(i == 1);
79
80 /* Remove that too */
81 strv_free(to_remove);
82 to_remove = NULL;
83 strv_append(&to_remove, to_install[strv_length(to_install) - 1],
84 qstrlen(to_install[strv_length(to_install) - 1]));
85
86 do_remove(testStore, to_remove);
87
88 QVERIFY (CertEnumCertificatesInStore(testStore, pCert) == NULL);
89
90 /* Install them all again */
91 do_install(testStore, to_install);
92 strv_free(to_remove);
93 to_remove = NULL;
94 strv_append(&to_remove, to_install[strv_length(to_install) - 1],
95 qstrlen(to_install[strv_length(to_install) - 1]));
96 do_remove(testStore, to_remove);
97
98 i = 0;
99 while((pCert = CertEnumCertificatesInStore(testStore, pCert))) {
100 bool certFound = false;
101 QByteArray data = QByteArray::fromRawData((const char*) pCert->pbCertEncoded,
102 pCert->cbCertEncoded);
103 qDebug() << "Have in store: " << data.toBase64();
104 for (int j = 0; j < validList.getInstallCertificates().size() - 1; j++) {
105 const Certificate &cert = validList.getInstallCertificates()[j];
106 QByteArray asn1data = QByteArray::fromBase64(
107 cert.base64Line().right(cert.base64Line().size() - 2).toLatin1());
108 if (asn1data == data) {
109 certFound = true;
110 }
111 }
112 QVERIFY(certFound);
113 i++;
114 }
115
116 QVERIFY(i == strv_length(to_install) - 1);
117
118 /* Install all again and remove them afterwards */
119 do_install(testStore, to_install);
120 do_remove(testStore, to_remove);
121 QVERIFY (CertEnumCertificatesInStore(testStore, pCert) == NULL);
122
123 strv_free(to_install);
124 strv_free(to_remove);
125 }
126
127 void WindowsStoreTest::cleanupTestCase() {
128 CertCloseStore(testStore, 0);
129 }
130 QTEST_GUILESS_MAIN (WindowsStoreTest);

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