# HG changeset patch # User Andre Heinecke # Date 1395861715 -3600 # Node ID e6c5c70a67b0290b284a4d5d74c97e6347641781 # Parent 57bef180d5607275fd638aacc4accd1c96f043bb Add test for windowsstores diff -r 57bef180d560 -r e6c5c70a67b0 ui/tests/CMakeLists.txt --- a/ui/tests/CMakeLists.txt Wed Mar 26 20:16:50 2014 +0100 +++ b/ui/tests/CMakeLists.txt Wed Mar 26 20:21:55 2014 +0100 @@ -15,7 +15,10 @@ macro(add_m13_test _source _additional_sources) set(_test ${_source}) get_filename_component(_name ${_source} NAME_WE) - add_executable(${_name} ${_test} ${_additional_sources}) + set(_test_sources_with_resources ${_test} ${_additional_sources}) + qt5_add_resources(_test_sources_with_resources + ${CMAKE_CURRENT_SOURCE_DIR}/data/testdata.qrc) + add_executable(${_name} ${_test_sources_with_resources}) add_test(m13-${_name} ${_name}) target_link_libraries(${_name} Qt5::Test Qt5::Widgets m13_common @@ -39,5 +42,9 @@ add_m13_test(cinstprocesstest.cpp "${CERTIFICATELIST_SOURCES}") add_m13_test(commontest.cpp "") +if (WIN32) + add_m13_test(windowsstoretest.cpp "${CERTIFICATELIST_SOURCES};${CMAKE_SOURCE_DIR}/cinst/windowsstore.c") +endif (WIN32) + #add_m13_test(${CMAKE_SOURCE_DIR}/ui/main.cpp "${M13UI_SOURCES}") diff -r 57bef180d560 -r e6c5c70a67b0 ui/tests/data/testdata.qrc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ui/tests/data/testdata.qrc Wed Mar 26 20:21:55 2014 +0100 @@ -0,0 +1,6 @@ + + + list-valid-signed.txt + + + diff -r 57bef180d560 -r e6c5c70a67b0 ui/tests/windowsstoretest.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ui/tests/windowsstoretest.cpp Wed Mar 26 20:21:55 2014 +0100 @@ -0,0 +1,130 @@ +#include "windowsstoretest.h" +#include "certificatelist.h" +#include "strhelp.h" +#include "certificate.h" +#include "../cinst/windowsstore.h" + +#include + +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); diff -r 57bef180d560 -r e6c5c70a67b0 ui/tests/windowsstoretest.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ui/tests/windowsstoretest.h Wed Mar 26 20:21:55 2014 +0100 @@ -0,0 +1,26 @@ +#ifndef WINDOWSSTORETEST_H +#define WINDOWSSTORETEST_H +#include +#include + +#include +#include + +#include "certificatelist.h" + +class WindowsStoreTest: public QObject +{ + Q_OBJECT + +private: + CertificateList validList; + HCERTSTORE testStore; + QTemporaryFile tmpFile; + +private Q_SLOTS: + void initTestCase(); + void cleanupTestCase(); + void testInstRemove(); +}; + +#endif // WINDOWSSTORETEST_H