Commit de29db89 authored by Ingo Klöcker's avatar Ingo Klöcker
Browse files

Add the possibility to fake crypto config values in tests

For now only string values can be faked. Faking is done with a
RAII-style class.

GnuPG-bug-id: 5283
parent e0f24a57
......@@ -45,6 +45,8 @@ set(libkleo_core_SRCS
utils/gnupg-registry.c
utils/hex.cpp
utils/compat.cpp
utils/cryptoconfig.cpp
utils/test.cpp
)
ecm_qt_declare_logging_category(libkleo_core_SRCS HEADER libkleo_debug.h IDENTIFIER LIBKLEO_LOG CATEGORY_NAME org.kde.pim.libkleo
DESCRIPTION "libkleo (kleo_core)"
......@@ -172,10 +174,12 @@ ecm_generate_headers(libkleo_CamelCase_utils_HEADERS
HEADER_NAMES
Algorithm
Classify
CryptoConfig
FileSystemWatcher
Formatting
GnuPG
Compat
Test
REQUIRED_HEADERS libkleo_utils_HEADERS
PREFIX Libkleo
RELATIVE utils
......
/*
utils/cryptoconfig.cpp
This file is part of libkleopatra, the KDE keymanagement library
SPDX-FileCopyrightText: 2021 g10 Code GmbH
SPDX-FileContributor: Ingo Klöcker <dev@ingo-kloecker.de>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "cryptoconfig.h"
#include "cryptoconfig_p.h"
#include "utils/compat.h"
#include <QGpgME/CryptoConfig>
#include <QGpgME/Protocol>
using namespace QGpgME;
static std::unordered_map<std::string, std::unordered_map<std::string, QString>> fakeCryptoConfigValues;
QString Kleo::getCryptoConfigStringValue(const char *componentName, const char *entryName)
{
if (!fakeCryptoConfigValues.empty()) {
const auto componentIt = fakeCryptoConfigValues.find(componentName);
if (componentIt != std::end(fakeCryptoConfigValues)) {
const auto entryIt = componentIt->second.find(entryName);
if (entryIt != std::end(componentIt->second)) {
return entryIt->second;
}
}
}
const CryptoConfig *const config = cryptoConfig();
if (!config) {
return {};
}
const CryptoConfigEntry *const entry = getCryptoConfigEntry(config, "gpg", "compliance");
if (!entry || entry->argType() != CryptoConfigEntry::ArgType_String) {
return QString();
}
return entry->stringValue();
}
void Kleo::Private::setFakeCryptoConfigStringValue(std::string componentName, std::string entryName, const QString &fakeValue)
{
fakeCryptoConfigValues[componentName][entryName] = fakeValue;
}
void Kleo::Private::clearFakeCryptoConfigStringValue(const std::string &componentName, const std::string &entryName)
{
auto &entryMap = fakeCryptoConfigValues[componentName];
entryMap.erase(entryName);
if (entryMap.empty()) {
fakeCryptoConfigValues.erase(componentName);
}
}
/*
utils/cryptoconfig.h
This file is part of libkleopatra, the KDE keymanagement library
SPDX-FileCopyrightText: 2021 g10 Code GmbH
SPDX-FileContributor: Ingo Klöcker <dev@ingo-kloecker.de>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#pragma once
#include "kleo_export.h"
class QString;
namespace Kleo
{
KLEO_EXPORT QString getCryptoConfigStringValue(const char *componentName, const char *entryName);
}
/*
utils/cryptoconfig_p.h
This file is part of libkleopatra, the KDE keymanagement library
SPDX-FileCopyrightText: 2021 g10 Code GmbH
SPDX-FileContributor: Ingo Klöcker <dev@ingo-kloecker.de>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#pragma once
#include <string>
class QString;
namespace Kleo
{
namespace Private
{
void setFakeCryptoConfigStringValue(std::string componentName, std::string entryName, const QString &fakeValue);
void clearFakeCryptoConfigStringValue(const std::string &componentName, const std::string &entryName);
}
}
/*
utils/test.cpp
This file is part of libkleopatra, the KDE keymanagement library
SPDX-FileCopyrightText: 2021 g10 Code GmbH
SPDX-FileContributor: Ingo Klöcker <dev@ingo-kloecker.de>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "test.h"
#include "cryptoconfig_p.h"
#include <QString>
using namespace Kleo::Tests;
FakeCryptoConfigStringValue::FakeCryptoConfigStringValue(const char *componentName, const char *entryName, const QString &fakeValue)
: mComponentName(componentName)
, mEntryName(entryName)
{
Kleo::Private::setFakeCryptoConfigStringValue(mComponentName, mEntryName, fakeValue);
}
FakeCryptoConfigStringValue::~FakeCryptoConfigStringValue()
{
Kleo::Private::clearFakeCryptoConfigStringValue(mComponentName, mEntryName);
}
/*
utils/test.h
This file is part of libkleopatra, the KDE keymanagement library
SPDX-FileCopyrightText: 2021 g10 Code GmbH
SPDX-FileContributor: Ingo Klöcker <dev@ingo-kloecker.de>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#pragma once
#include "kleo_export.h"
#include <string>
class QString;
namespace Kleo
{
namespace Tests
{
class KLEO_EXPORT FakeCryptoConfigStringValue
{
public:
FakeCryptoConfigStringValue(const char *componentName, const char *entryName, const QString &fakeValue);
~FakeCryptoConfigStringValue();
private:
std::string mComponentName;
std::string mEntryName;
};
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment