Mercurial > trustbridge
view 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 |
line wrap: on
line source
#include "windowsstoretest.h" #include "certificatelist.h" #include "strhelp.h" #include "certificate.h" #include "../cinst/windowsstore.h" #include <QTest> void WindowsStoreTest::initTestCase() { testStore = CertOpenStore( CERT_STORE_PROV_MEMORY, // A memory store 0, // Encoding type // Not used with a memory store 0, // Use the default provider 0, // No flags NULL); // Not needed QVERIFY (testStore); QFile res(":/list-valid-signed.txt"); res.open(QIODevice::ReadOnly); tmpFile.open(); tmpFile.write(res.readAll()); tmpFile.close(); validList = CertificateList(tmpFile.fileName().toLocal8Bit().data()); } void WindowsStoreTest::testInstRemove() { char ** to_install = NULL, ** to_remove = NULL; PCCERT_CONTEXT pCert = NULL; size_t i = 0; foreach (const Certificate &cert, validList.getInstallCertificates()) { strv_append (&to_install, cert.base64Line().toLatin1().constData() + 2, cert.base64Line().size() - 2); } /* Just a quick check for str_append_str functionality */ QVERIFY((size_t) validList.getInstallCertificates().size() == strv_length(to_install)); for (i = 0; i < strv_length(to_install); i++) { QVERIFY (validList.getInstallCertificates()[i].base64Line().right( validList.getInstallCertificates()[i].base64Line().size() - 2) == QString::fromLatin1(to_install[i])); } do_install(testStore, to_install); i = 0; while((pCert = CertEnumCertificatesInStore(testStore, pCert))) { bool certFound = false; QByteArray data = QByteArray::fromRawData ((const char *)pCert->pbCertEncoded, pCert->cbCertEncoded); foreach (const Certificate &cert, validList.getInstallCertificates()) { QByteArray asn1data = QByteArray::fromBase64( cert.base64Line().right(cert.base64Line().size() - 2).toLatin1()); if (asn1data == data) { certFound = true; } } QVERIFY(certFound); i++; } qDebug() << i; QVERIFY ((size_t)validList.getInstallCertificates().size() == i); /* Remove all except one */ for (i = 0; i < strv_length(to_install) - 1; i++) { strv_append(&to_remove, to_install[i], qstrlen(to_install[i])); } do_remove(testStore, to_remove); i = 0; while((pCert = CertEnumCertificatesInStore(testStore, pCert))) { i++; } QVERIFY(i == 1); /* Remove that too */ strv_free(to_remove); to_remove = NULL; strv_append(&to_remove, to_install[strv_length(to_install) - 1], qstrlen(to_install[strv_length(to_install) - 1])); do_remove(testStore, to_remove); QVERIFY (CertEnumCertificatesInStore(testStore, pCert) == NULL); /* Install them all again */ do_install(testStore, to_install); strv_free(to_remove); to_remove = NULL; strv_append(&to_remove, to_install[strv_length(to_install) - 1], qstrlen(to_install[strv_length(to_install) - 1])); do_remove(testStore, to_remove); i = 0; while((pCert = CertEnumCertificatesInStore(testStore, pCert))) { bool certFound = false; QByteArray data = QByteArray::fromRawData((const char*) pCert->pbCertEncoded, pCert->cbCertEncoded); qDebug() << "Have in store: " << data.toBase64(); for (int j = 0; j < validList.getInstallCertificates().size() - 1; j++) { const Certificate &cert = validList.getInstallCertificates()[j]; QByteArray asn1data = QByteArray::fromBase64( cert.base64Line().right(cert.base64Line().size() - 2).toLatin1()); if (asn1data == data) { certFound = true; } } QVERIFY(certFound); i++; } QVERIFY(i == strv_length(to_install) - 1); /* Install all again and remove them afterwards */ do_install(testStore, to_install); do_remove(testStore, to_remove); QVERIFY (CertEnumCertificatesInStore(testStore, pCert) == NULL); strv_free(to_install); strv_free(to_remove); } void WindowsStoreTest::cleanupTestCase() { CertCloseStore(testStore, 0); } QTEST_GUILESS_MAIN (WindowsStoreTest);