diff options
author | saturneric <[email protected]> | 2024-01-12 06:02:37 +0000 |
---|---|---|
committer | saturneric <[email protected]> | 2024-01-12 06:02:37 +0000 |
commit | bf538056b24a68b8fd235b1c50991ee8eb46a776 (patch) | |
tree | e1bab54095b80df62b321fb5bd69453f9f951b05 /src/test/GpgFrontendTest.cpp | |
parent | feat: improve api and ui of keys import and export (diff) | |
download | GpgFrontend-bf538056b24a68b8fd235b1c50991ee8eb46a776.tar.gz GpgFrontend-bf538056b24a68b8fd235b1c50991ee8eb46a776.zip |
refactor: use QString instead of std::string and improve threading system
Diffstat (limited to 'src/test/GpgFrontendTest.cpp')
-rw-r--r-- | src/test/GpgFrontendTest.cpp | 41 |
1 files changed, 20 insertions, 21 deletions
diff --git a/src/test/GpgFrontendTest.cpp b/src/test/GpgFrontendTest.cpp index f030ed12..b7f59336 100644 --- a/src/test/GpgFrontendTest.cpp +++ b/src/test/GpgFrontendTest.cpp @@ -35,7 +35,6 @@ #include <filesystem> #include "core/GpgConstants.h" -#include "core/GpgCoreInit.h" #include "core/function/GlobalSettingStation.h" #include "core/function/basic/ChannelObject.h" #include "core/function/gpg/GpgContext.h" @@ -44,14 +43,14 @@ namespace GpgFrontend::Test { -auto GenerateRandomString(size_t length) -> std::string { - const std::string characters = +auto GenerateRandomString(size_t length) -> QString { + const QString characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; std::random_device random_device; std::mt19937 generator(random_device()); std::uniform_int_distribution<> distribution(0, characters.size() - 1); - std::string random_string; + QString random_string; for (size_t i = 0; i < length; ++i) { random_string += characters[distribution(generator)]; } @@ -60,31 +59,26 @@ auto GenerateRandomString(size_t length) -> std::string { } void ConfigureGpgContext() { - auto db_path = - std::filesystem::temp_directory_path() / GenerateRandomString(12); + auto db_path = QDir(QDir::tempPath() + "/" + GenerateRandomString(12)); GF_TEST_LOG_DEBUG("setting up new database path for test case: {}", - db_path.string()); + db_path.path()); - if (!std::filesystem::exists(db_path)) { - std::filesystem::create_directory(db_path); - } else { - std::filesystem::remove_all(db_path); - std::filesystem::create_directory(db_path); - } + if (db_path.exists()) db_path.rmdir("."); + db_path.mkdir("."); GpgContext::CreateInstance( kGpgFrontendDefaultChannel, [=]() -> ChannelObjectPtr { GpgContextInitArgs args; args.test_mode = true; args.offline_mode = true; - args.db_path = db_path.string(); + args.db_path = db_path.path(); return ConvertToChannelObjectPtr<>(SecureCreateUniqueObject<GpgContext>( args, kGpgFrontendDefaultChannel)); }); } -void ImportPrivateKeys(const std::filesystem::path& data_path, +void ImportPrivateKeys(const QString& data_path, const libconfig::Setting& config) { if (config.exists("load_keys.private_keys")) { auto& private_keys = config.lookup("load_keys.private_keys"); @@ -92,11 +86,16 @@ void ImportPrivateKeys(const std::filesystem::path& data_path, if (private_key.exists("filename")) { std::string filename; private_key.lookupValue("filename", filename); - auto data_file_path = data_path / filename; - std::string data = ReadAllDataInFile(data_file_path.string()); - auto secret_key_copy = SecureCreateSharedObject<std::string>(data); - GpgKeyImportExporter::GetInstance(kGpgFrontendDefaultChannel) - .ImportKey(secret_key_copy); + auto data_file_path = + data_path + "/" + QString::fromStdString(filename); + + auto [success, gf_buffer] = ReadFileGFBuffer(data_file_path); + if (success) { + GpgKeyImportExporter::GetInstance(kGpgFrontendDefaultChannel) + .ImportKey(gf_buffer); + } else { + GF_TEST_LOG_ERROR("read from file faild: {}", data_file_path); + } } } } @@ -115,7 +114,7 @@ void SetupGlobalTestEnv() { ASSERT_NO_THROW(cfg.readFile(test_config_path.c_str())); auto& root = cfg.getRoot(); - ImportPrivateKeys(test_data_path, root); + ImportPrivateKeys(test_data_path.c_str(), root); } auto ExecuteAllTestCase(GpgFrontendContext args) -> int { |