aheinecke@404: /* Copyright (C) 2014 by Bundesamt für Sicherheit in der Informationstechnik
aheinecke@404:  * Software engineering by Intevation GmbH
aheinecke@404:  *
aheinecke@404:  * This file is Free Software under the GNU GPL (v>=2)
aheinecke@404:  * and comes with ABSOLUTELY NO WARRANTY!
aheinecke@404:  * See LICENSE.txt for details.
aheinecke@404:  */
andre@220: #include "windowsstoretest.h"
andre@220: #include "certificatelist.h"
andre@220: #include "strhelp.h"
andre@220: #include "certificate.h"
andre@220: #include "../cinst/windowsstore.h"
andre@220: 
andre@220: #include <QTest>
andre@220: 
aheinecke@222: void WindowsStoreTest::dumpContents() {
andre@602:     wchar_t pszNameString[256];
aheinecke@222:     PCCERT_CONTEXT pCert = NULL;
aheinecke@222:     qDebug() << "Currently in store: " ;
aheinecke@222:     while((pCert = CertEnumCertificatesInStore(testStore, pCert))) {
aheinecke@222:         if(CertGetNameString(pCert,
aheinecke@222:                              CERT_NAME_SIMPLE_DISPLAY_TYPE,
aheinecke@222:                              0,
aheinecke@222:                              NULL,
aheinecke@222:                              pszNameString,
aheinecke@222:                              128)){
aheinecke@222:             qDebug() << "   " << pszNameString ;
aheinecke@222:         }
aheinecke@222:     }
aheinecke@222: }
aheinecke@222: 
andre@220: void WindowsStoreTest::initTestCase() {
andre@220:     testStore = CertOpenStore(
andre@220:       CERT_STORE_PROV_MEMORY,    // A memory store
andre@220:       0,                         // Encoding type
andre@220:                                  // Not used with a memory store
andre@220:       0,                         // Use the default provider
andre@220:       0,                         // No flags
andre@220:       NULL);                     // Not needed
andre@220:     QVERIFY (testStore);
andre@220:     QFile res(":/list-valid-signed.txt");
andre@220:     res.open(QIODevice::ReadOnly);
andre@220:     tmpFile.open();
andre@220:     tmpFile.write(res.readAll());
andre@220:     tmpFile.close();
andre@220: 
andre@220:     validList = CertificateList(tmpFile.fileName().toLocal8Bit().data());
andre@220: }
andre@220: 
andre@220: void WindowsStoreTest::testInstRemove() {
andre@220:     char ** to_install = NULL,
andre@220:          ** to_remove = NULL;
andre@220:     PCCERT_CONTEXT pCert = NULL;
andre@220:     size_t i = 0;
andre@220: 
aheinecke@249:     QList<Certificate> instList;
aheinecke@249: 
aheinecke@249:     foreach (const Certificate &cert, validList.getCertificates()) {
aheinecke@249:         if (cert.isInstallCert())
aheinecke@249:             instList << cert;
aheinecke@249:     }
aheinecke@249: 
aheinecke@249: 
aheinecke@249:     foreach (const Certificate &cert, instList) {
andre@220:         strv_append (&to_install, cert.base64Line().toLatin1().constData() + 2,
andre@220:                 cert.base64Line().size() - 2);
andre@220:     }
andre@220: 
andre@220:     /* Just a quick check for str_append_str functionality */
aheinecke@249:     QVERIFY((size_t) instList.size() == strv_length(to_install));
andre@220:     for (i = 0; i < strv_length(to_install); i++) {
aheinecke@249:         QVERIFY (instList[i].base64Line().right(
aheinecke@249:                     instList[i].base64Line().size() - 2) ==
andre@220:                 QString::fromLatin1(to_install[i]));
andre@220:     }
andre@220: 
andre@220:     do_install(testStore, to_install);
andre@220: 
andre@220:     i = 0;
andre@220:     while((pCert = CertEnumCertificatesInStore(testStore, pCert))) {
andre@220:         bool certFound = false;
andre@220:         QByteArray data = QByteArray::fromRawData ((const char *)pCert->pbCertEncoded,
andre@220:                 pCert->cbCertEncoded);
aheinecke@249:         foreach (const Certificate &cert, instList) {
andre@220:             QByteArray asn1data = QByteArray::fromBase64(
andre@220:                     cert.base64Line().right(cert.base64Line().size() - 2).toLatin1());
andre@220:             if (asn1data == data) {
andre@220:                 certFound = true;
andre@220:             }
andre@220:         }
andre@220:         QVERIFY(certFound);
andre@220:         i++;
andre@220:     }
aheinecke@249:     QVERIFY ((size_t)instList.size() == i);
andre@220: 
andre@220:     /* Remove all except one */
andre@220:     for (i = 0; i < strv_length(to_install) - 1; i++) {
andre@220:         strv_append(&to_remove, to_install[i], qstrlen(to_install[i]));
andre@220:     }
andre@220: 
andre@220:     do_remove(testStore, to_remove);
andre@220: 
andre@220:     i = 0;
andre@220:     while((pCert = CertEnumCertificatesInStore(testStore, pCert))) {
andre@220:         i++;
andre@220:     }
andre@220: 
andre@220:     QVERIFY(i == 1);
andre@220: 
andre@220:     /* Remove that too */
andre@220:     strv_free(to_remove);
andre@220:     to_remove = NULL;
andre@220:     strv_append(&to_remove, to_install[strv_length(to_install) - 1],
andre@220:                 qstrlen(to_install[strv_length(to_install) - 1]));
andre@220: 
andre@220:     do_remove(testStore, to_remove);
andre@220: 
andre@220:     QVERIFY (CertEnumCertificatesInStore(testStore, pCert) == NULL);
andre@220: 
andre@220:     /* Install them all again */
andre@220:     do_install(testStore, to_install);
andre@220:     strv_free(to_remove);
andre@220:     to_remove = NULL;
andre@220:     strv_append(&to_remove, to_install[strv_length(to_install) - 1],
andre@220:                 qstrlen(to_install[strv_length(to_install) - 1]));
andre@220:     do_remove(testStore, to_remove);
andre@220: 
andre@220:     i = 0;
andre@220:     while((pCert = CertEnumCertificatesInStore(testStore, pCert))) {
andre@220:         bool certFound = false;
andre@220:         QByteArray data = QByteArray::fromRawData((const char*) pCert->pbCertEncoded,
andre@220:                 pCert->cbCertEncoded);
aheinecke@222:         QVERIFY (data.toBase64() != to_remove[0]);
aheinecke@249:         for (int j = 0; j < instList.size() - 1; j++) {
aheinecke@249:             const Certificate &cert = instList[j];
andre@220:             QByteArray asn1data = QByteArray::fromBase64(
andre@220:                     cert.base64Line().right(cert.base64Line().size() - 2).toLatin1());
andre@220:             if (asn1data == data) {
andre@220:                 certFound = true;
andre@220:             }
andre@220:         }
andre@220:         QVERIFY(certFound);
andre@220:         i++;
andre@220:     }
andre@220: 
andre@220:     QVERIFY(i == strv_length(to_install) - 1);
andre@220: 
andre@220:     /* Install all again and remove them afterwards */
andre@220:     do_install(testStore, to_install);
aheinecke@222:     do_remove(testStore, to_install);
aheinecke@222: 
andre@220:     QVERIFY (CertEnumCertificatesInStore(testStore, pCert) == NULL);
andre@220: 
andre@220:     strv_free(to_install);
andre@220:     strv_free(to_remove);
andre@220: }
andre@220: 
andre@220: void WindowsStoreTest::cleanupTestCase() {
andre@220:     CertCloseStore(testStore, 0);
andre@220: }
andre@220: QTEST_GUILESS_MAIN (WindowsStoreTest);