aboutsummaryrefslogtreecommitdiffstats
path: root/test/GpgFrontendTest.h
diff options
context:
space:
mode:
Diffstat (limited to 'test/GpgFrontendTest.h')
-rw-r--r--test/GpgFrontendTest.h99
1 files changed, 86 insertions, 13 deletions
diff --git a/test/GpgFrontendTest.h b/test/GpgFrontendTest.h
index 181c513e..84476106 100644
--- a/test/GpgFrontendTest.h
+++ b/test/GpgFrontendTest.h
@@ -29,16 +29,18 @@
#include <gpg-error.h>
#include <gtest/gtest.h>
-#include <boost/date_time/gregorian/parsers.hpp>
+#include <boost/date_time.hpp>
#include <boost/dll.hpp>
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/path.hpp>
+#include <libconfig.h++>
#include <memory>
#include <string>
#include <vector>
#include "gpg/GpgConstants.h"
-#include "gpg/function/GpgKeyImportExportor.h"
+#include "gpg/function/GpgKeyImportExporter.h"
+#include "gpg/function/GpgKeyOpera.h"
class GpgCoreTest : public ::testing::Test {
protected:
@@ -55,13 +57,24 @@ class GpgCoreTest : public ::testing::Test {
// Data File Directory Location
boost::filesystem::path data_path;
- int default_channel = 0;
+ const int default_channel = 0;
+
+ const int gpg_alone_channel = 512;
GpgCoreTest() = default;
- virtual ~GpgCoreTest() = default;
+ ~GpgCoreTest() override = default;
+
+ void SetUp() override {
+ el::Loggers::addFlag(el::LoggingFlag::AutoSpacing);
+ el::Configurations defaultConf;
+ defaultConf.setToDefault();
+ el::Loggers::reconfigureLogger("default", defaultConf);
+
+ defaultConf.setGlobally(el::ConfigurationType::Format,
+ "%datetime %level %func %msg");
+ el::Loggers::reconfigureLogger("default", defaultConf);
- virtual void SetUp() {
using namespace libconfig;
Config cfg;
ASSERT_NO_THROW(cfg.readFile(config_path.c_str()));
@@ -75,21 +88,42 @@ class GpgCoreTest : public ::testing::Test {
configure_independent_database(root);
+ configure_alone_gpg(root);
+
dealing_private_keys(root);
import_data();
+ import_data_alone();
}
- virtual void TearDown() {}
+ void TearDown() override {
+ auto key_ids = std::make_unique<GpgFrontend::KeyIdArgsList>();
+ key_ids->push_back("81704859182661FB");
+ key_ids->push_back("06F1C7E7240C94E8");
+ key_ids->push_back("8465C55B25C9B7D1");
+ key_ids->push_back("021D89771B680FFB");
+ GpgFrontend::GpgKeyOpera::GetInstance(default_channel)
+ .DeleteKeys(std::move(key_ids));
+ }
private:
void import_data() {
- GpgFrontend::GpgContext::GetInstance(default_channel)
- .SetPassphraseCb(GpgFrontend::GpgContext::test_passphrase_cb);
+ for (const auto& secret_key : secret_keys_) {
+ auto secret_key_copy =
+ std::make_unique<GpgFrontend::ByteArray>(*secret_key);
+ GpgFrontend::GpgKeyImportExporter::GetInstance(default_channel)
+ .ImportKey(std::move(secret_key_copy));
+ }
+ }
+
+ void import_data_alone() {
for (auto& secret_key : secret_keys_) {
- GpgFrontend::GpgKeyImportExportor::GetInstance(default_channel)
- .ImportKey(std::move(secret_key));
+ auto secret_key_copy =
+ std::make_unique<GpgFrontend::ByteArray>(*secret_key);
+ GpgFrontend::GpgKeyImportExporter::GetInstance(gpg_alone_channel)
+ .ImportKey(std::move(secret_key_copy));
}
}
+
void dealing_private_keys(const libconfig::Setting& root) {
if (root.exists("load_keys.private_keys")) {
auto& private_keys = root.lookup("load_keys.private_keys");
@@ -106,21 +140,60 @@ class GpgCoreTest : public ::testing::Test {
}
}
+ void configure_alone_gpg(const libconfig::Setting& root) {
+ bool alone_gpg = false;
+ if (root.exists("alone_gpg")) {
+ root.lookupValue("alone_gpg", alone_gpg);
+ if (alone_gpg && root.exists("alone_gpg")) {
+ std::string alone_gpg_path;
+ root.lookupValue("alone_gpg_path", alone_gpg_path);
+ auto gpg_path = parent_path / alone_gpg_path;
+
+ std::string relative_db_path;
+ root.lookupValue("alone_gpg_db_path", relative_db_path);
+ auto db_path = parent_path / relative_db_path;
+ if (!boost::filesystem::exists(db_path)) {
+ boost::filesystem::create_directory(db_path);
+ } else {
+ boost::filesystem::remove_all(db_path);
+ boost::filesystem::create_directory(db_path);
+ }
+ GpgFrontend::GpgContext::CreateInstance(
+ gpg_alone_channel,
+ [&]() -> std::unique_ptr<GpgFrontend::GpgContext> {
+ GpgFrontend::GpgContextInitArgs args;
+ args.gpg_alone = true;
+ args.independent_database = true;
+ args.db_path = db_path.string();
+ args.gpg_path = gpg_path.string();
+ args.test_mode = true;
+ return std::make_unique<GpgFrontend::GpgContext>(args);
+ });
+ }
+ }
+ }
+
void configure_independent_database(const libconfig::Setting& root) {
bool independent_database = false;
if (root.exists("independent_database")) {
root.lookupValue("independent_database", independent_database);
if (independent_database && root.exists("independent_db_path")) {
- default_channel = 1;
std::string relative_db_path;
root.lookupValue("independent_db_path", relative_db_path);
auto db_path = parent_path / relative_db_path;
if (!boost::filesystem::exists(db_path)) {
boost::filesystem::create_directory(db_path);
+ } else {
+ boost::filesystem::remove_all(db_path);
+ boost::filesystem::create_directory(db_path);
}
+
GpgFrontend::GpgContext::CreateInstance(
- 1,
- std::make_unique<GpgFrontend::GpgContext>(true, db_path.c_str()));
+ default_channel, [&]() -> std::unique_ptr<GpgFrontend::GpgContext> {
+ GpgFrontend::GpgContextInitArgs args;
+ args.test_mode = true;
+ return std::make_unique<GpgFrontend::GpgContext>(args);
+ });
}
}
}