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

Add the possibility to fake int crypto config entries in tests

GnuPG-bug-id: 5362
parent 60be5b04
......@@ -20,13 +20,37 @@
using namespace QGpgME;
static std::unordered_map<std::string, std::unordered_map<std::string, QString>> fakeCryptoConfigValues;
static std::unordered_map<std::string, std::unordered_map<std::string, int>> fakeCryptoConfigIntValues;
static std::unordered_map<std::string, std::unordered_map<std::string, QString>> fakeCryptoConfigStringValues;
int Kleo::getCryptoConfigIntValue(const char *componentName, const char *entryName, int defaultValue)
{
if (!fakeCryptoConfigIntValues.empty()) {
const auto componentIt = fakeCryptoConfigIntValues.find(componentName);
if (componentIt != std::end(fakeCryptoConfigIntValues)) {
const auto entryIt = componentIt->second.find(entryName);
if (entryIt != std::end(componentIt->second)) {
return entryIt->second;
}
}
}
const CryptoConfig *const config = cryptoConfig();
if (!config) {
return defaultValue;
}
const CryptoConfigEntry *const entry = getCryptoConfigEntry(config, componentName, entryName);
if (!entry || entry->argType() != CryptoConfigEntry::ArgType_Int) {
return defaultValue;
}
return entry->intValue();
}
QString Kleo::getCryptoConfigStringValue(const char *componentName, const char *entryName)
{
if (!fakeCryptoConfigValues.empty()) {
const auto componentIt = fakeCryptoConfigValues.find(componentName);
if (componentIt != std::end(fakeCryptoConfigValues)) {
if (!fakeCryptoConfigStringValues.empty()) {
const auto componentIt = fakeCryptoConfigStringValues.find(componentName);
if (componentIt != std::end(fakeCryptoConfigStringValues)) {
const auto entryIt = componentIt->second.find(entryName);
if (entryIt != std::end(componentIt->second)) {
return entryIt->second;
......@@ -45,16 +69,30 @@ QString Kleo::getCryptoConfigStringValue(const char *componentName, const char *
return entry->stringValue();
}
void Kleo::Private::setFakeCryptoConfigIntValue(const std::string &componentName, const std::string &entryName, int fakeValue)
{
fakeCryptoConfigIntValues[componentName][entryName] = fakeValue;
}
void Kleo::Private::clearFakeCryptoConfigIntValue(const std::string &componentName, const std::string &entryName)
{
auto &entryMap = fakeCryptoConfigIntValues[componentName];
entryMap.erase(entryName);
if (entryMap.empty()) {
fakeCryptoConfigIntValues.erase(componentName);
}
}
void Kleo::Private::setFakeCryptoConfigStringValue(const std::string &componentName, const std::string &entryName, const QString &fakeValue)
{
fakeCryptoConfigValues[componentName][entryName] = fakeValue;
fakeCryptoConfigStringValues[componentName][entryName] = fakeValue;
}
void Kleo::Private::clearFakeCryptoConfigStringValue(const std::string &componentName, const std::string &entryName)
{
auto &entryMap = fakeCryptoConfigValues[componentName];
auto &entryMap = fakeCryptoConfigStringValues[componentName];
entryMap.erase(entryName);
if (entryMap.empty()) {
fakeCryptoConfigValues.erase(componentName);
fakeCryptoConfigStringValues.erase(componentName);
}
}
......@@ -17,6 +17,8 @@ class QString;
namespace Kleo
{
KLEO_EXPORT int getCryptoConfigIntValue(const char *componentName, const char *entryName, int defaultValue);
KLEO_EXPORT QString getCryptoConfigStringValue(const char *componentName, const char *entryName);
}
......@@ -20,6 +20,9 @@ namespace Kleo
namespace Private
{
void setFakeCryptoConfigIntValue(const std::string &componentName, const std::string &entryName, int fakeValue);
void clearFakeCryptoConfigIntValue(const std::string &componentName, const std::string &entryName);
void setFakeCryptoConfigStringValue(const std::string &componentName, const std::string &entryName, const QString &fakeValue);
void clearFakeCryptoConfigStringValue(const std::string &componentName, const std::string &entryName);
......
......@@ -16,6 +16,18 @@
using namespace Kleo::Tests;
FakeCryptoConfigIntValue::FakeCryptoConfigIntValue(const char *componentName, const char *entryName, int fakeValue)
: mComponentName(componentName)
, mEntryName(entryName)
{
Kleo::Private::setFakeCryptoConfigIntValue(mComponentName, mEntryName, fakeValue);
}
FakeCryptoConfigIntValue::~FakeCryptoConfigIntValue()
{
Kleo::Private::clearFakeCryptoConfigIntValue(mComponentName, mEntryName);
}
FakeCryptoConfigStringValue::FakeCryptoConfigStringValue(const char *componentName, const char *entryName, const QString &fakeValue)
: mComponentName(componentName)
, mEntryName(entryName)
......
......@@ -22,6 +22,17 @@ namespace Kleo
namespace Tests
{
class KLEO_EXPORT FakeCryptoConfigIntValue
{
public:
FakeCryptoConfigIntValue(const char *componentName, const char *entryName, int fakeValue);
~FakeCryptoConfigIntValue();
private:
std::string mComponentName;
std::string mEntryName;
};
class KLEO_EXPORT FakeCryptoConfigStringValue
{
public:
......
Supports Markdown
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