diff --git a/src/utils/cryptoconfig.cpp b/src/utils/cryptoconfig.cpp index 8fbb5478d..9434ddf00 100644 --- a/src/utils/cryptoconfig.cpp +++ b/src/utils/cryptoconfig.cpp @@ -1,60 +1,98 @@ /* utils/cryptoconfig.cpp This file is part of libkleopatra, the KDE keymanagement library SPDX-FileCopyrightText: 2021 g10 Code GmbH SPDX-FileContributor: Ingo Klöcker SPDX-License-Identifier: GPL-2.0-or-later */ #include "cryptoconfig.h" #include "cryptoconfig_p.h" #include "utils/compat.h" #include #include #include using namespace QGpgME; -static std::unordered_map> fakeCryptoConfigValues; +static std::unordered_map> fakeCryptoConfigIntValues; +static std::unordered_map> 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; } } } const CryptoConfig *const config = cryptoConfig(); if (!config) { return {}; } const CryptoConfigEntry *const entry = getCryptoConfigEntry(config, componentName, entryName); if (!entry || entry->argType() != CryptoConfigEntry::ArgType_String) { return QString(); } 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); } } diff --git a/src/utils/cryptoconfig.h b/src/utils/cryptoconfig.h index ebe6bf273..faa057f4c 100644 --- a/src/utils/cryptoconfig.h +++ b/src/utils/cryptoconfig.h @@ -1,22 +1,24 @@ /* utils/cryptoconfig.h This file is part of libkleopatra, the KDE keymanagement library SPDX-FileCopyrightText: 2021 g10 Code GmbH SPDX-FileContributor: Ingo Klöcker SPDX-License-Identifier: GPL-2.0-or-later */ #pragma once #include "kleo_export.h" 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); } diff --git a/src/utils/cryptoconfig_p.h b/src/utils/cryptoconfig_p.h index 8c1edd5ed..4feb3890e 100644 --- a/src/utils/cryptoconfig_p.h +++ b/src/utils/cryptoconfig_p.h @@ -1,28 +1,31 @@ /* 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 SPDX-License-Identifier: GPL-2.0-or-later */ #pragma once #include class QString; 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); } } diff --git a/src/utils/test.cpp b/src/utils/test.cpp index 338a9d874..cd6aeb3f8 100644 --- a/src/utils/test.cpp +++ b/src/utils/test.cpp @@ -1,29 +1,41 @@ /* utils/test.cpp This file is part of libkleopatra, the KDE keymanagement library SPDX-FileCopyrightText: 2021 g10 Code GmbH SPDX-FileContributor: Ingo Klöcker SPDX-License-Identifier: GPL-2.0-or-later */ #include "test.h" #include "cryptoconfig_p.h" #include 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) { Kleo::Private::setFakeCryptoConfigStringValue(mComponentName, mEntryName, fakeValue); } FakeCryptoConfigStringValue::~FakeCryptoConfigStringValue() { Kleo::Private::clearFakeCryptoConfigStringValue(mComponentName, mEntryName); } diff --git a/src/utils/test.h b/src/utils/test.h index 24c573ca0..bff196d40 100644 --- a/src/utils/test.h +++ b/src/utils/test.h @@ -1,38 +1,49 @@ /* utils/test.h This file is part of libkleopatra, the KDE keymanagement library SPDX-FileCopyrightText: 2021 g10 Code GmbH SPDX-FileContributor: Ingo Klöcker SPDX-License-Identifier: GPL-2.0-or-later */ #pragma once #include "kleo_export.h" #include class QString; 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: FakeCryptoConfigStringValue(const char *componentName, const char *entryName, const QString &fakeValue); ~FakeCryptoConfigStringValue(); private: std::string mComponentName; std::string mEntryName; }; } }