comparison ui/tests/nsstest.cpp @ 307:2fd69803d219

Complete InstRemove NSS test
author Andre Heinecke <andre.heinecke@intevation.de>
date Thu, 03 Apr 2014 16:35:21 +0200
parents 4a3febc6d806
children ad28f6b60e6b
comparison
equal deleted inserted replaced
306:9c5e6b142611 307:2fd69803d219
1 #include <cert.h>
2 #include <certdb.h>
3 #include <certt.h>
4
5 #include <nss.h>
6 #include <pk11pub.h>
7
1 #include "nsstest.h" 8 #include "nsstest.h"
2 #include "nssstore.h" 9 #include "nssstore.h"
3 #include "strhelp.h" 10 #include "strhelp.h"
4 11
5 #include <QTest> 12 #include <QTest>
6 13
14 QList<QByteArray> NSSTest::get_nss_certs (QTemporaryDir *nssDir)
15 {
16 CERTCertList *list;
17 CERTCertListNode *node;
18 QList<QByteArray> retval;
19
20 if (NSS_Initialize(nssDir->path().toLocal8Bit().constData(),
21 "", "", "secmod.db", NSS_INIT_READONLY)
22 == SECSuccess)
23 {
24 list = PK11_ListCerts(PK11CertListAll, NULL);
25 for (node = CERT_LIST_HEAD(list); !CERT_LIST_END(node, list);
26 node = CERT_LIST_NEXT(node)) {
27 retval << QByteArray((const char*)node->cert->derCert.data,
28 (int)node->cert->derCert.len);
29 }
30 CERT_DestroyCertList(list);
31 NSS_Shutdown();
32 }
33 else
34 {
35 qDebug("Could not open nss certificate store!\n");
36 }
37 return retval;
38 }
39
40 void NSSTest::setupTestDir(QTemporaryDir *nssDir)
41 {
42 /* Copy the empty nss db in the temporary dir */
43 QFile::copy(":/nss/cert8.db", nssDir->path() + "/" +"cert8.db");
44 QFile::copy(":/nss/key3.db", nssDir->path() + "/" +"key3.db");
45 QFile::copy(":/nss/secmod.db", nssDir->path() + "/" +"secmod.db");
46
47 QVERIFY(QFile::setPermissions(nssDir->path() + "/" +"cert8.db",
48 QFileDevice::ReadOwner | QFileDevice::WriteOwner));
49 QVERIFY(QFile::setPermissions(nssDir->path() + "/" +"key3.db",
50 QFileDevice::ReadOwner | QFileDevice::WriteOwner));
51 QVERIFY(QFile::setPermissions(nssDir->path() + "/" +"secmod.db",
52 QFileDevice::ReadOwner | QFileDevice::WriteOwner));
53 }
54
7 void NSSTest::initTestCase() { 55 void NSSTest::initTestCase() {
8 /* Copy the empty nss db in the temporary dir */
9 QFile::copy(":/nss/cert8.db", nssDir.path() + "/" +"cert8.db");
10 QFile::copy(":/nss/key3.db", nssDir.path() + "/" +"key3.db");
11 QFile::copy(":/nss/secmod.db", nssDir.path() + "/" +"secmod.db");
12
13 QVERIFY(QFile::setPermissions(nssDir.path() + "/" +"cert8.db",
14 QFileDevice::ReadOwner | QFileDevice::WriteOwner));
15 QVERIFY(QFile::setPermissions(nssDir.path() + "/" +"key3.db",
16 QFileDevice::ReadOwner | QFileDevice::WriteOwner));
17 QVERIFY(QFile::setPermissions(nssDir.path() + "/" +"secmod.db",
18 QFileDevice::ReadOwner | QFileDevice::WriteOwner));
19 56
20 /* Set up a temporary list */ 57 /* Set up a temporary list */
21 QFile res(":/list-valid-signed.txt"); 58 QFile res(":/list-valid-signed.txt");
22 res.open(QIODevice::ReadOnly); 59 res.open(QIODevice::ReadOnly);
23 validListFile.open(); 60 validListFile.open();
24 validListFile.write(res.readAll()); 61 validListFile.write(res.readAll());
25 validListFile.close(); 62 validListFile.close();
26 63
27 nssDir.setAutoRemove(false); 64 setupTestDir(&ffNSSDir);
65 setupTestDir(&tbNSSDir);
28 66
29 validList = CertificateList(validListFile.fileName().toLocal8Bit().data()); 67 validList = CertificateList(validListFile.fileName().toLocal8Bit().data());
30 68
31 /* Create the profiles.ini `s set environment variables*/ 69 /* Create the profiles.ini `s set environment variables*/
32 #ifndef WIN32 70 #ifndef WIN32
50 ffStream << endl << "[General]"<< 88 ffStream << endl << "[General]"<<
51 "StartWithLastProfile=1" << endl << 89 "StartWithLastProfile=1" << endl <<
52 "[Profile0]" << endl << 90 "[Profile0]" << endl <<
53 "Name=default" << endl << 91 "Name=default" << endl <<
54 "IsRelative=1" << endl << 92 "IsRelative=1" << endl <<
55 "Path=" << fakeFirefoxDir.relativeFilePath(nssDir.path()) << endl; 93 "Path=" << fakeFirefoxDir.relativeFilePath(ffNSSDir.path()) << endl;
56 ffStream.flush(); 94 ffStream.flush();
57 mozProfile.close(); 95 mozProfile.close();
58 96
59 QVERIFY(tbProfile.open(QIODevice::WriteOnly)); 97 QVERIFY(tbProfile.open(QIODevice::WriteOnly));
60 QTextStream tbStream(&tbProfile); 98 QTextStream tbStream(&tbProfile);
61 tbStream << endl << "[General]"<< 99 tbStream << endl << "[General]"<<
62 "StartWithLastProfile=1" << endl << 100 "StartWithLastProfile=1" << endl <<
63 "[Profile102]" << endl << 101 "[Profile102]" << endl <<
64 "Name=default" << endl << 102 "Name=default" << endl <<
65 "IsRelative=0" << endl << 103 "IsRelative=0" << endl <<
66 "Path=" << nssDir.path() << endl; 104 "Path=" << tbNSSDir.path() << endl;
67 tbStream.flush(); 105 tbStream.flush();
68 tbProfile.close(); 106 tbProfile.close();
69 } 107 }
70 108
71 void NSSTest::testInstRemove() { 109 void NSSTest::testInstRemove() {
72 char ** to_install = NULL, 110 char ** to_install = NULL,
73 ** to_remove = NULL; 111 ** to_remove = NULL;
74 112
75 QList<Certificate> instList; 113 QList<Certificate> instList;
76 114
115 /* Install all certificates */
77 foreach (const Certificate &cert, validList.getCertificates()) { 116 foreach (const Certificate &cert, validList.getCertificates()) {
78 if (!cert.isInstallCert()) 117 if (!cert.isInstallCert())
79 continue; 118 continue;
80 instList << cert; 119 instList << cert;
81 strv_append (&to_install, cert.base64Line().toLatin1().constData() + 2, 120 strv_append (&to_install, cert.base64Line().toLatin1().constData() + 2,
83 } 122 }
84 QVERIFY((size_t) instList.size() == strv_length(to_install)); 123 QVERIFY((size_t) instList.size() == strv_length(to_install));
85 124
86 QVERIFY(write_stores_nss(to_install, to_remove) == 0); 125 QVERIFY(write_stores_nss(to_install, to_remove) == 0);
87 126
127 {
128 /* Verify that everything is installed */
129 QList<QByteArray> installedCertsFF = get_nss_certs(&ffNSSDir);
130 QList<QByteArray> installedCertsTB = get_nss_certs(&tbNSSDir);
131
132 QVERIFY(installedCertsFF.size() == instList.size());
133 QVERIFY(installedCertsFF == installedCertsTB);
134
135 for (int i = 0; to_install[i]; i++) {
136 QByteArray bai = QByteArray::fromBase64(to_install[i]);
137 QVERIFY(installedCertsFF.contains(bai));
138 }
139 }
140
141 {
142 /* Remove one certificate */
143 QVERIFY(instList.size() > 2);
144 strv_append (&to_remove, to_install[1], qstrlen(to_install[1]));
145
146 QVERIFY(write_stores_nss(NULL, to_remove) == 0);
147
148 QList<QByteArray> installedCertsFF = get_nss_certs(&ffNSSDir);
149 QList<QByteArray> installedCertsTB = get_nss_certs(&tbNSSDir);
150
151 QVERIFY(installedCertsFF == installedCertsTB);
152
153 QByteArray bai = QByteArray::fromBase64(to_install[1]);
154 QVERIFY(!installedCertsTB.contains(bai));
155
156 QVERIFY((size_t)installedCertsTB.size() == strv_length(to_install) - 1);
157
158 for (int i = 0; to_install[i]; i++) {
159 if (i == 1) {
160 continue;
161 }
162 QByteArray bai = QByteArray::fromBase64(to_install[i]);
163 QVERIFY(installedCertsTB.contains(bai));
164 }
165 }
166
167 {
168 /* Readd all certificates check for duplication*/
169 QVERIFY(write_stores_nss(to_install, NULL) == 0);
170
171 QList<QByteArray> installedCertsFF = get_nss_certs(&ffNSSDir);
172 QList<QByteArray> installedCertsTB = get_nss_certs(&tbNSSDir);
173
174 QVERIFY(installedCertsFF == installedCertsTB);
175
176 QVERIFY((size_t)installedCertsTB.size() == strv_length(to_install));
177
178 for (int i = 0; to_install[i]; i++) {
179 QByteArray bai = QByteArray::fromBase64(to_install[i]);
180 QVERIFY(installedCertsTB.contains(bai));
181 }
182 }
183
184 {
185 /* Remove all certificates */
186 QVERIFY(write_stores_nss(NULL, to_install) == 0);
187
188 QList<QByteArray> installedCertsFF = get_nss_certs(&ffNSSDir);
189 QList<QByteArray> installedCertsTB = get_nss_certs(&tbNSSDir);
190
191 QVERIFY(installedCertsFF == installedCertsTB);
192
193 QVERIFY(installedCertsTB.size() == 0);
194 }
88 } 195 }
89 196
90 QTEST_GUILESS_MAIN (NSSTest); 197 QTEST_GUILESS_MAIN (NSSTest);

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