Mercurial > trustbridge
view ui/tests/windowsstoretest.cpp @ 240:c05e126b0b9e
Fix downloadertest and factor out generating file input.
author | Andre Heinecke <aheinecke@intevation.de> |
---|---|
date | Fri, 28 Mar 2014 15:21:02 +0000 |
parents | 53ea9b975d1c |
children | 6a7eb102716d |
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::dumpContents() { char pszNameString[256]; PCCERT_CONTEXT pCert = NULL; qDebug() << "Currently in store: " ; while((pCert = CertEnumCertificatesInStore(testStore, pCert))) { if(CertGetNameString(pCert, CERT_NAME_SIMPLE_DISPLAY_TYPE, 0, NULL, pszNameString, 128)){ qDebug() << " " << pszNameString ; } } } 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++; } 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); QVERIFY (data.toBase64() != to_remove[0]); 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_install); QVERIFY (CertEnumCertificatesInStore(testStore, pCert) == NULL); strv_free(to_install); strv_free(to_remove); } void WindowsStoreTest::cleanupTestCase() { CertCloseStore(testStore, 0); } QTEST_GUILESS_MAIN (WindowsStoreTest);