# HG changeset patch # User Andre Heinecke # Date 1396528189 -7200 # Node ID eecc06f714fd1f6ac1c247eb2b41294ec0074c96 # Parent b54ab152a57e054f58aad65ee300816502f6d22c Add first nsstest. diff -r b54ab152a57e -r eecc06f714fd ui/tests/CMakeLists.txt --- a/ui/tests/CMakeLists.txt Thu Apr 03 14:28:58 2014 +0200 +++ b/ui/tests/CMakeLists.txt Thu Apr 03 14:29:49 2014 +0200 @@ -1,6 +1,9 @@ set(EXECUTABLE_OUTPUT_PATH ${CMAKE_CURRENT_BINARY_DIR}) -include_directories(${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/.. ${CMAKE_CURRENT_SOURCE_DIR}/../../common) +include_directories(${CMAKE_CURRENT_BINARY_DIR} + ${CMAKE_SOURCE_DIR}/ui + ${CMAKE_SOURCE_DIR}/common + ${CMAKE_SOURCE_DIR}/cinst) find_package(Qt5Test) include_directories(${Qt5Test_INCLUDE_DIRS}) @@ -47,5 +50,7 @@ add_m13_test(windowsstoretest.cpp "${CERTIFICATELIST_SOURCES};${CMAKE_SOURCE_DIR}/cinst/windowsstore.c") endif (WIN32) +add_m13_test(nsstest.cpp "${CERTIFICATELIST_SOURCES};${CMAKE_SOURCE_DIR}/cinst/nssstore_linux.c") + #add_m13_test(${CMAKE_SOURCE_DIR}/ui/main.cpp "${M13UI_SOURCES}") diff -r b54ab152a57e -r eecc06f714fd ui/tests/nsstest.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ui/tests/nsstest.cpp Thu Apr 03 14:29:49 2014 +0200 @@ -0,0 +1,89 @@ +#include "nsstest.h" +#include "nssstore.h" +#include "strhelp.h" + +#include + +void NSSTest::initTestCase() { + /* Copy the empty nss db in the temporary dir */ + QFile::copy(":/nss/cert8.db", nssDir.path() + "/" +"cert8.db"); + QFile::copy(":/nss/key3.db", nssDir.path() + "/" +"key3.db"); + QFile::copy(":/nss/secmod.db", nssDir.path() + "/" +"secmod.db"); + + QVERIFY(QFile::setPermissions(nssDir.path() + "/" +"cert8.db", + QFileDevice::ReadOwner | QFileDevice::WriteOwner)); + QVERIFY(QFile::setPermissions(nssDir.path() + "/" +"key3.db", + QFileDevice::ReadOwner | QFileDevice::WriteOwner)); + QVERIFY(QFile::setPermissions(nssDir.path() + "/" +"secmod.db", + QFileDevice::ReadOwner | QFileDevice::WriteOwner)); + + /* Set up a temporary list */ + QFile res(":/list-valid-signed.txt"); + res.open(QIODevice::ReadOnly); + validListFile.open(); + validListFile.write(res.readAll()); + validListFile.close(); + + nssDir.setAutoRemove(false); + + validList = CertificateList(validListFile.fileName().toLocal8Bit().data()); + + /* Create the profiles.ini `s set environment variables*/ +#ifndef WIN32 + QVERIFY(!setenv ("HOME", fakeHome.path().toLocal8Bit().constData(), 1)); + fakeFirefoxDir = QDir(fakeHome.path() + "/.mozilla/firefox"); + fakeThunderbirdDir = QDir(fakeHome.path() + "/.thunderbird"); +#else + QVERIFY(!setenv ("APPDATA", fakeHome.path().toLocal8Bit().constData(), 1)); + fakeFirefoxDir = QDir(fakeHome.path() + "/Mozilla/firefox"); + fakeThunderbirdDir = QDir(fakeHome.path() + "/Thunderbird"); +#endif + QVERIFY(fakeFirefoxDir.mkpath(fakeFirefoxDir.path())); + QVERIFY(fakeThunderbirdDir.mkpath(fakeThunderbirdDir.path())); + + QFile mozProfile(fakeFirefoxDir.absoluteFilePath("profiles.ini")); + QFile tbProfile(fakeThunderbirdDir.absoluteFilePath("profiles.ini")); + + /* Write profiles */ + QVERIFY(mozProfile.open(QIODevice::WriteOnly)); + QTextStream ffStream(&mozProfile); + ffStream << endl << "[General]"<< + "StartWithLastProfile=1" << endl << + "[Profile0]" << endl << + "Name=default" << endl << + "IsRelative=1" << endl << + "Path=" << fakeFirefoxDir.relativeFilePath(nssDir.path()) << endl; + ffStream.flush(); + mozProfile.close(); + + QVERIFY(tbProfile.open(QIODevice::WriteOnly)); + QTextStream tbStream(&tbProfile); + tbStream << endl << "[General]"<< + "StartWithLastProfile=1" << endl << + "[Profile102]" << endl << + "Name=default" << endl << + "IsRelative=0" << endl << + "Path=" << nssDir.path() << endl; + tbStream.flush(); + tbProfile.close(); +} + +void NSSTest::testInstRemove() { + char ** to_install = NULL, + ** to_remove = NULL; + + QList instList; + + foreach (const Certificate &cert, validList.getCertificates()) { + if (!cert.isInstallCert()) + continue; + instList << cert; + strv_append (&to_install, cert.base64Line().toLatin1().constData() + 2, + cert.base64Line().size() - 2); + } + QVERIFY((size_t) instList.size() == strv_length(to_install)); + + write_stores_nss(to_install, to_remove); +} + +QTEST_GUILESS_MAIN (NSSTest); diff -r b54ab152a57e -r eecc06f714fd ui/tests/nsstest.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ui/tests/nsstest.h Thu Apr 03 14:29:49 2014 +0200 @@ -0,0 +1,26 @@ +#ifndef NSSTEST_H +#define NSSTEST_H + +#include +#include +#include + +#include "certificatelist.h" + +class NSSTest: public QObject +{ + Q_OBJECT + + QTemporaryDir fakeHome; + QDir fakeFirefoxDir; + QDir fakeThunderbirdDir; + QTemporaryDir nssDir; + CertificateList validList; + QTemporaryFile validListFile; + +private Q_SLOTS: + void initTestCase(); + void testInstRemove(); +}; + +#endif // NSSTEST_H