diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 0c9c6078..494069c6 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,49 +1,51 @@ find_package(Qt5Test REQUIRED) set(test_LIBS kgpg_lib Qt5::Test) set(COMMON common.h common.cpp) function(create_test name) add_executable(${name} ${ARGN} ${COMMON}) target_link_libraries(${name} ${test_LIBS}) add_test(NAME ${name} COMMAND ${name}) set_property(TEST ${name} PROPERTY ENVIRONMENT "XDG_CONFIG_HOME=${CMAKE_BINARY_DIR}/tests") set_property(TEST ${name} PROPERTY RUN_SERIAL TRUE) #since gpg config home is shared between each test add_dependencies(${name} test-data) endfunction() create_test(kgpg-import kgpgimport.h kgpgimport.cpp) create_test(kgpg-verify kgpgverify.h kgpgverify.cpp) create_test(kgpg-change-trust kgpgchangetrust.h kgpgchangetrust.cpp) create_test(kgpg-disable kgpgchangedisable.h kgpgchangedisable.cpp) create_test(kgpg-interface kgpginterface.h kgpginterface.cpp) create_test(kgpg-encrypt kgpgencrypt.h kgpgencrypt.cpp) create_test(kgpg-decrypt kgpgdecrypt.h kgpgdecrypt.cpp) create_test(kgpg-del-key kgpgdelkey.h kgpgdelkey.cpp) +create_test(kgpg-add-photo kgpgaddphoto.h kgpgaddphoto.cpp) set(GpgConf .gnupg/gpg.conf) set(KgpgConf kgpgrc) set(Keys keys/kgpgtest_BA7695F3C550DF14.asc keys/kgpgtest_BA7695F3C550DF14.pass keys/kgpgtest_BA7695F3C550DF14_pub.asc keys/signed_bad_sig keys/signed_text keys/encrypted_text.txt keys/encrypted_text_hide_key_id.txt + keys/image.jpg ) add_custom_target(test-data) add_custom_command(TARGET test-data PRE_BUILD COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_SOURCE_DIR}/tests/.gnupg ${CMAKE_BINARY_DIR}/tests/gnupg) add_custom_command(TARGET test-data PRE_BUILD COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_SOURCE_DIR}/tests/keys ${CMAKE_BINARY_DIR}/tests/keys) add_custom_command(TARGET test-data PRE_BUILD COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/tests/kgpgrc ${CMAKE_BINARY_DIR}/tests/kgpgrc) diff --git a/tests/common.cpp b/tests/common.cpp index 74066c90..53f5b838 100644 --- a/tests/common.cpp +++ b/tests/common.cpp @@ -1,71 +1,84 @@ #include "common.h" #include "../kgpginterface.h" #include "../kgpgsettings.h" #include "../transactions/kgpgtransaction.h" #include #include #include #include #include #include bool resetGpgConf() { const QString dot_gpg(QLatin1String(".gnupg")); QDir dir(dot_gpg); dir.removeRecursively(); if (!QDir::current().mkdir(dot_gpg)) return false; if (!QFile::setPermissions(QDir::currentPath() + QLatin1Char('/') + dot_gpg, - QFileDevice::ReadOwner | QFileDevice::WriteOwner | QFileDevice::ExeOwner)) + QFileDevice::ReadOwner | QFileDevice::WriteOwner | + QFileDevice::ExeOwner)) return false; return QFile::copy(QLatin1String("gnupg/gpg.conf"), QLatin1String(".gnupg/gpg.conf")); } QString readFile(const QString &filename) { QFile file(filename); if (file.open(QIODevice::ReadOnly)) return QLatin1String(file.readAll()); else return QString(); } void addGpgKey(const QString &file, const QString &password) { QString conf = QLatin1String(".gnupg/gpg.conf"); QString gpgHome = QLatin1String(".gnupg"); QString command = QLatin1String("gpg"); QStringList args; args.push_back(QLatin1String("--no-secmem-warning")); args.push_back(QLatin1String("--no-tty")); args.push_back(QLatin1String("--batch")); if (!password.isEmpty()) { args.push_back(QLatin1String("--passphrase")); args.push_back(password); } args.push_back(QLatin1String("--options")); args.push_back(conf); args.push_back(QLatin1String("--homedir")); args.push_back(gpgHome); args.push_back(QLatin1String("--debug-level")); args.push_back(QLatin1String("none")); args.push_back(QLatin1String("--status-fd=1")); args.push_back(QLatin1String("--import")); args.push_back(QLatin1String("--allow-secret-key-import")); args.push_back(QLatin1String("--command-fd=0")); args.push_back(file); QProcess process; process.execute(command, args); qDebug() << "Added Gpg key: " << file; } void addPasswordArguments(KGpgTransaction *transaction, const QString &passphrase) { transaction->addArgument(QLatin1String("--batch")); transaction->addArgument(QLatin1String("--passphrase")); transaction->addArgument(passphrase); transaction->addArgument(QLatin1String("--pinentry-mode")); transaction->addArgument(QLatin1String("loopback")); } + +bool hasPhoto(QString id) +{ + QStringList args{ QLatin1String("--list-key"), id }; + QString command = QLatin1String("gpg"); + QProcess process; + process.start(command, args); + process.waitForFinished(); + QString output = QLatin1String(process.readAllStandardOutput()); + qDebug()<< output; + return output.contains(QLatin1String("image")); +} diff --git a/tests/common.h b/tests/common.h index 7bd93bec..537f60d4 100644 --- a/tests/common.h +++ b/tests/common.h @@ -1,13 +1,13 @@ #ifndef COMMON_TEST_H #define COMMON_TEST_H #include class KGpgTransaction; bool resetGpgConf(); QString readFile(const QString& filename); void addGpgKey(const QString& file, const QString& password = QString()); void addPasswordArguments(KGpgTransaction *transaction, const QString &passphrase); - +bool hasPhoto(QString id); #endif diff --git a/tests/keys/image.jpg b/tests/keys/image.jpg new file mode 100644 index 00000000..4f794f0e Binary files /dev/null and b/tests/keys/image.jpg differ diff --git a/tests/kgpgaddphoto.cpp b/tests/kgpgaddphoto.cpp new file mode 100644 index 00000000..924bcfa4 --- /dev/null +++ b/tests/kgpgaddphoto.cpp @@ -0,0 +1,27 @@ +#include "kgpgaddphoto.h" +#include "../transactions/kgpgaddphoto.h" +#include "../kgpginterface.h" +#include "common.h" + +#include +#include + +void KGpgAddPhotoTest::init() +{ + QVERIFY(resetGpgConf()); +} + +void KGpgAddPhotoTest::testAddPhoto() +{ + addGpgKey(QLatin1String("keys/kgpgtest_BA7695F3C550DF14.asc"), + readFile(QLatin1String("keys/kgpgtest_BA7695F3C550DF14.pass"))); + QString keyID = QLatin1String("BA7695F3C550DF14"); + QString imagepath = QLatin1String("keys/image.jpg"); + KGpgAddPhoto *transaction = new KGpgAddPhoto(this, keyID, imagepath); + QSignalSpy spy(transaction, &KGpgAddPhoto::done); + transaction->start(); + QVERIFY(spy.wait()); + QVERIFY(hasPhoto(keyID)); +} + +QTEST_MAIN(KGpgAddPhotoTest) diff --git a/tests/kgpgaddphoto.h b/tests/kgpgaddphoto.h new file mode 100644 index 00000000..7e521db4 --- /dev/null +++ b/tests/kgpgaddphoto.h @@ -0,0 +1,13 @@ +#ifndef KGPGADDPHOTO_TEST_H +#define KGPGADDPHOTO_TEST_H + +#include + +class KGpgAddPhotoTest : public QObject { + Q_OBJECT +private slots: + void init(); + void testAddPhoto(); +}; + +#endif