aboutsummaryrefslogtreecommitdiffstats
path: root/src/test
diff options
context:
space:
mode:
authorsaturneric <[email protected]>2024-01-12 06:02:37 +0000
committersaturneric <[email protected]>2024-01-12 06:02:37 +0000
commitbf538056b24a68b8fd235b1c50991ee8eb46a776 (patch)
treee1bab54095b80df62b321fb5bd69453f9f951b05 /src/test
parentfeat: improve api and ui of keys import and export (diff)
downloadGpgFrontend-bf538056b24a68b8fd235b1c50991ee8eb46a776.tar.gz
GpgFrontend-bf538056b24a68b8fd235b1c50991ee8eb46a776.zip
refactor: use QString instead of std::string and improve threading system
Diffstat (limited to 'src/test')
-rw-r--r--src/test/GpgFrontendTest.cpp41
-rw-r--r--src/test/core/GpgCoreTestBasicOpera.cpp2
-rw-r--r--src/test/core/GpgCoreTestFileBasicOpera.cpp17
-rw-r--r--src/test/core/GpgCoreTestKeyModel.cpp12
-rw-r--r--src/test/core/GpgCoreTestKeygen.cpp8
5 files changed, 40 insertions, 40 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 {
diff --git a/src/test/core/GpgCoreTestBasicOpera.cpp b/src/test/core/GpgCoreTestBasicOpera.cpp
index 90645ddd..e525afa9 100644
--- a/src/test/core/GpgCoreTestBasicOpera.cpp
+++ b/src/test/core/GpgCoreTestBasicOpera.cpp
@@ -180,7 +180,7 @@ TEST_F(GpgCoreTest, CoreEncryptDecrTest_KeyNotFound_ResultAnalyse) {
GpgDecryptResultAnalyse analyse{err, d_result};
analyse.Analyse();
ASSERT_EQ(analyse.GetStatus(), -1);
- ASSERT_FALSE(analyse.GetResultReport().empty());
+ ASSERT_FALSE(analyse.GetResultReport().isEmpty());
// stop waiting
callback_called_flag = true;
diff --git a/src/test/core/GpgCoreTestFileBasicOpera.cpp b/src/test/core/GpgCoreTestFileBasicOpera.cpp
index 029ff6fc..e409570f 100644
--- a/src/test/core/GpgCoreTestFileBasicOpera.cpp
+++ b/src/test/core/GpgCoreTestFileBasicOpera.cpp
@@ -51,7 +51,7 @@ TEST_F(GpgCoreTest, CoreFileEncryptDecrTest) {
auto output_file = GetTempFilePath();
GpgFileOpera::GetInstance().EncryptFile(
- {encrypt_key}, input_file, true, output_file,
+ {encrypt_key}, input_file.toStdString(), true, output_file.toStdString(),
[output_file, &callback_called_flag](GpgError err,
const DataObjectPtr& data_obj) {
ASSERT_TRUE((data_obj->Check<GpgEncryptResult>()));
@@ -62,7 +62,7 @@ TEST_F(GpgCoreTest, CoreFileEncryptDecrTest) {
auto decrpypt_output_file = GetTempFilePath();
GpgFileOpera::GetInstance().DecryptFile(
- output_file, decrpypt_output_file,
+ output_file.toStdString(), decrpypt_output_file.toStdString(),
[decrpypt_output_file, &callback_called_flag](
GpgError err, const DataObjectPtr& data_obj) {
auto d_result = ExtractParams<GpgDecryptResult>(data_obj, 0);
@@ -96,7 +96,7 @@ TEST_F(GpgCoreTest, CoreFileEncryptSymmetricDecrTest) {
auto output_file = GetTempFilePath();
GpgFileOpera::GetInstance().EncryptFileSymmetric(
- input_file, true, output_file,
+ input_file.toStdString(), true, output_file.toStdString(),
[&callback_called_flag, output_file](GpgError err,
const DataObjectPtr& data_obj) {
ASSERT_TRUE((data_obj->Check<GpgEncryptResult>()));
@@ -106,7 +106,7 @@ TEST_F(GpgCoreTest, CoreFileEncryptSymmetricDecrTest) {
auto decrpypt_output_file = GetTempFilePath();
GpgFileOpera::GetInstance().DecryptFile(
- output_file, decrpypt_output_file,
+ output_file.toStdString(), decrpypt_output_file.toStdString(),
[&callback_called_flag, decrpypt_output_file](
GpgError err, const DataObjectPtr& data_obj) {
ASSERT_TRUE((data_obj->Check<GpgDecryptResult>()));
@@ -143,7 +143,7 @@ TEST_F(GpgCoreTest, CoreFileSignVerifyNormalTest) {
auto output_file = GetTempFilePath();
GpgFileOpera::GetInstance().SignFile(
- {sign_key}, input_file, true, output_file,
+ {sign_key}, input_file.toStdString(), true, output_file.toStdString(),
[&callback_called_flag, input_file, output_file](
GpgError err, const DataObjectPtr& data_obj) {
ASSERT_TRUE((data_obj->Check<GpgSignResult>()));
@@ -152,7 +152,7 @@ TEST_F(GpgCoreTest, CoreFileSignVerifyNormalTest) {
ASSERT_EQ(CheckGpgError(err), GPG_ERR_NO_ERROR);
GpgFileOpera::GetInstance().VerifyFile(
- input_file, output_file,
+ input_file.toStdString(), output_file.toStdString(),
[&callback_called_flag](GpgError err,
const DataObjectPtr& data_obj) {
auto d_result = ExtractParams<GpgVerifyResult>(data_obj, 0);
@@ -188,7 +188,8 @@ TEST_F(GpgCoreTest, CoreFileEncryptSignDecrVerifyTest) {
ASSERT_TRUE(sign_key.IsHasActualSigningCapability());
GpgFileOpera::GetInstance().EncryptSignFile(
- {encrypt_key}, {sign_key}, input_file, true, output_file,
+ {encrypt_key}, {sign_key}, input_file.toStdString(), true,
+ output_file.toStdString(),
[&callback_called_flag, output_file](GpgError err,
const DataObjectPtr& data_obj) {
ASSERT_TRUE((data_obj->Check<GpgEncryptResult, GpgSignResult>()));
@@ -200,7 +201,7 @@ TEST_F(GpgCoreTest, CoreFileEncryptSignDecrVerifyTest) {
auto decrpypt_output_file = GetTempFilePath();
GpgFileOpera::GetInstance().DecryptVerifyFile(
- output_file, decrpypt_output_file,
+ output_file.toStdString(), decrpypt_output_file.toStdString(),
[&callback_called_flag, decrpypt_output_file](
GpgError err, const DataObjectPtr& data_obj) {
ASSERT_TRUE(
diff --git a/src/test/core/GpgCoreTestKeyModel.cpp b/src/test/core/GpgCoreTestKeyModel.cpp
index 3618bcb9..1f3a5292 100644
--- a/src/test/core/GpgCoreTestKeyModel.cpp
+++ b/src/test/core/GpgCoreTestKeyModel.cpp
@@ -45,13 +45,13 @@ TEST_F(GpgCoreTest, CoreInitTest) {
}
TEST_F(GpgCoreTest, GpgDataTest) {
- auto data_buff = std::string(
+ auto data_buff = QString(
"cqEh8fyKWtmiXrW2zzlszJVGJrpXDDpzgP7ZELGxhfZYFi8rMrSVKDwrpFZBSWMG");
GpgData data(data_buff.data(), data_buff.size());
- auto out_buffer = data.Read2Buffer();
- ASSERT_EQ(out_buffer->size(), 64);
+ auto out_buffer = data.Read2GFBuffer();
+ ASSERT_EQ(out_buffer.Size(), 64);
}
TEST_F(GpgCoreTest, GpgKeyTest) {
@@ -79,7 +79,7 @@ TEST_F(GpgCoreTest, GpgKeyTest) {
ASSERT_FALSE(key.IsHasActualAuthenticationCapability());
ASSERT_EQ(key.GetName(), "GpgFrontendTest");
- ASSERT_TRUE(key.GetComment().empty());
+ ASSERT_TRUE(key.GetComment().isEmpty());
ASSERT_EQ(key.GetEmail(), "[email protected]");
ASSERT_EQ(key.GetId(), "81704859182661FB");
ASSERT_EQ(key.GetFingerprint(), "9490795B78F8AFE9F93BD09281704859182661FB");
@@ -138,7 +138,7 @@ TEST_F(GpgCoreTest, GpgUIDTest) {
auto& uid = uids->front();
ASSERT_EQ(uid.GetName(), "GpgFrontendTest");
- ASSERT_TRUE(uid.GetComment().empty());
+ ASSERT_TRUE(uid.GetComment().isEmpty());
ASSERT_EQ(uid.GetEmail(), "[email protected]");
ASSERT_EQ(uid.GetUID(), "GpgFrontendTest <[email protected]>");
ASSERT_FALSE(uid.GetInvalid());
@@ -157,7 +157,7 @@ TEST_F(GpgCoreTest, GpgKeySignatureTest) {
auto& signature = signatures->front();
ASSERT_EQ(signature.GetName(), "GpgFrontendTest");
- ASSERT_TRUE(signature.GetComment().empty());
+ ASSERT_TRUE(signature.GetComment().isEmpty());
ASSERT_EQ(signature.GetEmail(), "[email protected]");
ASSERT_EQ(signature.GetKeyID(), "81704859182661FB");
ASSERT_EQ(signature.GetPubkeyAlgo(), "RSA");
diff --git a/src/test/core/GpgCoreTestKeygen.cpp b/src/test/core/GpgCoreTestKeygen.cpp
index 476279ac..93544d85 100644
--- a/src/test/core/GpgCoreTestKeygen.cpp
+++ b/src/test/core/GpgCoreTestKeygen.cpp
@@ -73,7 +73,7 @@ TEST_F(GpgCoreTest, GenerateKeyTest) {
GpgKeyOpera::GetInstance(kGpgFrontendDefaultChannel).DeleteKey(fpr);
callback_called_flag = true;
- ASSERT_FALSE(fpr.empty());
+ ASSERT_FALSE(fpr.isEmpty());
});
int retry_count = 1000;
@@ -118,7 +118,7 @@ TEST_F(GpgCoreTest, GenerateKeyTest_1) {
GpgKeyOpera::GetInstance(kGpgFrontendDefaultChannel).DeleteKey(fpr);
callback_called_flag = true;
- ASSERT_FALSE(fpr.empty());
+ ASSERT_FALSE(fpr.isEmpty());
});
int retry_count = 2000;
@@ -157,7 +157,7 @@ TEST_F(GpgCoreTest, GenerateKeyTest_4) {
GpgKeyOpera::GetInstance(kGpgFrontendDefaultChannel).DeleteKey(fpr);
callback_called_flag = true;
- ASSERT_FALSE(fpr.empty());
+ ASSERT_FALSE(fpr.isEmpty());
});
int retry_count = 2000;
@@ -196,7 +196,7 @@ TEST_F(GpgCoreTest, GenerateKeyTest_5) {
GpgKeyOpera::GetInstance(kGpgFrontendDefaultChannel).DeleteKey(fpr);
callback_called_flag = true;
- ASSERT_FALSE(fpr.empty());
+ ASSERT_FALSE(fpr.isEmpty());
});
int retry_count = 1000;