aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsaturneric <[email protected]>2023-12-23 06:05:19 +0000
committersaturneric <[email protected]>2023-12-23 06:05:19 +0000
commitf52e49d37e99be3568d20c658633f1e830b53d13 (patch)
tree2c9dacd56daba7fa19e20ffa91fe293b6edcda1c
parentfix: slove compilation issue on macos (diff)
downloadGpgFrontend-f52e49d37e99be3568d20c658633f1e830b53d13.tar.gz
GpgFrontend-f52e49d37e99be3568d20c658633f1e830b53d13.zip
fix: gen key test cases typeid rtti issues
-rw-r--r--src/core/function/gpg/GpgKeyOpera.cpp8
-rw-r--r--src/core/model/CommonStruct.h46
-rw-r--r--src/core/model/DataObject.h12
-rw-r--r--src/core/model/GpgGenerateKeyResult.cpp59
-rw-r--r--src/core/model/GpgGenerateKeyResult.h59
-rw-r--r--src/test/core/GpgCoreTestKeygen.cpp39
6 files changed, 207 insertions, 16 deletions
diff --git a/src/core/function/gpg/GpgKeyOpera.cpp b/src/core/function/gpg/GpgKeyOpera.cpp
index 3ea9b365..378a946d 100644
--- a/src/core/function/gpg/GpgKeyOpera.cpp
+++ b/src/core/function/gpg/GpgKeyOpera.cpp
@@ -47,6 +47,7 @@
#include "core/utils/AsyncUtils.h"
#include "core/utils/CommonUtils.h"
#include "core/utils/GpgUtils.h"
+#include "model/GpgGenerateKeyResult.h"
#include "typedef/GpgTypedef.h"
namespace GpgFrontend {
@@ -197,11 +198,12 @@ void GpgKeyOpera::GenerateKey(const std::shared_ptr<GenKeyInfo>& params,
err = gpgme_op_createkey(ctx.DefaultContext(), userid, algo, 0, expires,
nullptr, flags);
- GpgGenKeyResult result;
if (CheckGpgError(err) == GPG_ERR_NO_ERROR) {
- result = NewResult(gpgme_op_genkey_result(ctx.DefaultContext()));
+ data_object->Swap({GpgGenerateKeyResult{
+ gpgme_op_genkey_result(ctx.DefaultContext())}});
+ } else {
+ data_object->Swap({GpgGenerateKeyResult{}});
}
- data_object->Swap({result});
return CheckGpgError(err);
},
diff --git a/src/core/model/CommonStruct.h b/src/core/model/CommonStruct.h
new file mode 100644
index 00000000..d7d09602
--- /dev/null
+++ b/src/core/model/CommonStruct.h
@@ -0,0 +1,46 @@
+/**
+ * Copyright (C) 2021 Saturneric <[email protected]>
+ *
+ * This file is part of GpgFrontend.
+ *
+ * GpgFrontend is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * GpgFrontend is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with GpgFrontend. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * The initial version of the source code is inherited from
+ * the gpg4usb project, which is under GPL-3.0-or-later.
+ *
+ * All the source code of GpgFrontend was modified and released by
+ * Saturneric <[email protected]> starting on May 12, 2021.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ *
+ */
+
+#pragma once
+
+namespace GpgFrontend {
+
+/**
+ * @brief
+ *
+ */
+template <typename T>
+struct GPGFRONTEND_CORE_EXPORT RefDeleter {
+ void operator()(T* _key) {
+ gpgme_unre
+ }
+};
+
+template <typename T>
+using KeyRefHandler = std::unique_ptr<T, RefDeleter<T>>; ///<
+} // namespace GpgFrontend \ No newline at end of file
diff --git a/src/core/model/DataObject.h b/src/core/model/DataObject.h
index 49082d09..63c829c5 100644
--- a/src/core/model/DataObject.h
+++ b/src/core/model/DataObject.h
@@ -29,9 +29,12 @@
#pragma once
#include <any>
+#include <typeindex>
+#include <typeinfo>
#include "core/GpgFrontendCoreExport.h"
#include "core/utils/MemoryUtils.h"
+#include "spdlog/spdlog.h"
namespace GpgFrontend {
@@ -66,7 +69,14 @@ class GPGFRONTEND_CORE_EXPORT DataObject {
std::vector<std::type_info const*> type_list = {&typeid(Args)...};
for (size_t i = 0; i < type_list.size(); ++i) {
- if (type_list[i] != &((*this)[i]).type()) return false;
+ if (std::type_index(*type_list[i]) !=
+ std::type_index((*this)[i].type())) {
+ SPDLOG_ERROR(
+ "value of index {} in data object is type: {}, "
+ "not expected type: {}",
+ i, ((*this)[i]).type().name(), type_list[i]->name());
+ return false;
+ }
}
return true;
}
diff --git a/src/core/model/GpgGenerateKeyResult.cpp b/src/core/model/GpgGenerateKeyResult.cpp
new file mode 100644
index 00000000..005ea36c
--- /dev/null
+++ b/src/core/model/GpgGenerateKeyResult.cpp
@@ -0,0 +1,59 @@
+/**
+ * Copyright (C) 2021 Saturneric <[email protected]>
+ *
+ * This file is part of GpgFrontend.
+ *
+ * GpgFrontend is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * GpgFrontend is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with GpgFrontend. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * The initial version of the source code is inherited from
+ * the gpg4usb project, which is under GPL-3.0-or-later.
+ *
+ * All the source code of GpgFrontend was modified and released by
+ * Saturneric <[email protected]> starting on May 12, 2021.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ *
+ */
+
+#include "GpgGenerateKeyResult.h"
+
+#include <gpgme.h>
+
+namespace GpgFrontend {
+
+GpgGenerateKeyResult::GpgGenerateKeyResult(gpgme_genkey_result_t r)
+ : result_ref_(std::shared_ptr<struct _gpgme_op_genkey_result>(
+ (gpgme_result_ref(r), r), [](gpgme_genkey_result_t p) {
+ if (p != nullptr) {
+ gpgme_result_unref(p);
+ }
+ })) {}
+
+auto GpgGenerateKeyResult::IsGood() -> bool { return result_ref_ != nullptr; }
+
+auto GpgGenerateKeyResult::GetFingerprint() -> std::string {
+ return result_ref_->fpr;
+}
+
+GpgGenerateKeyResult::GpgGenerateKeyResult() = default;
+
+GpgGenerateKeyResult::GpgGenerateKeyResult(const GpgGenerateKeyResult &) =
+ default;
+
+auto GpgGenerateKeyResult::operator=(const GpgGenerateKeyResult &)
+ -> GpgGenerateKeyResult & = default;
+
+GpgGenerateKeyResult::~GpgGenerateKeyResult() = default;
+
+} // namespace GpgFrontend \ No newline at end of file
diff --git a/src/core/model/GpgGenerateKeyResult.h b/src/core/model/GpgGenerateKeyResult.h
new file mode 100644
index 00000000..904eeef3
--- /dev/null
+++ b/src/core/model/GpgGenerateKeyResult.h
@@ -0,0 +1,59 @@
+/**
+ * Copyright (C) 2021 Saturneric <[email protected]>
+ *
+ * This file is part of GpgFrontend.
+ *
+ * GpgFrontend is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * GpgFrontend is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with GpgFrontend. If not, see <https://www.gnu.org/licenses/>.
+ *
+ * The initial version of the source code is inherited from
+ * the gpg4usb project, which is under GPL-3.0-or-later.
+ *
+ * All the source code of GpgFrontend was modified and released by
+ * Saturneric <[email protected]> starting on May 12, 2021.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ *
+ */
+
+#pragma once
+
+#include "core/GpgFrontendCore.h"
+#include "core/GpgFrontendCoreExport.h"
+
+namespace GpgFrontend {
+
+class GPGFRONTEND_CORE_EXPORT GpgGenerateKeyResult {
+ public:
+ auto IsGood() -> bool;
+
+ auto GetFingerprint() -> std::string;
+
+ explicit GpgGenerateKeyResult(gpgme_genkey_result_t);
+
+ GpgGenerateKeyResult();
+
+ GpgGenerateKeyResult(const GpgGenerateKeyResult &);
+
+ auto operator=(const GpgGenerateKeyResult &) -> GpgGenerateKeyResult &;
+
+ virtual ~GpgGenerateKeyResult();
+
+ private:
+ using ResultRefHandler =
+ std::shared_ptr<struct _gpgme_op_genkey_result>; ///<
+
+ ResultRefHandler result_ref_ = nullptr; ///<
+};
+
+} // namespace GpgFrontend
diff --git a/src/test/core/GpgCoreTestKeygen.cpp b/src/test/core/GpgCoreTestKeygen.cpp
index f78f88dd..67c4bde6 100644
--- a/src/test/core/GpgCoreTestKeygen.cpp
+++ b/src/test/core/GpgCoreTestKeygen.cpp
@@ -26,6 +26,7 @@
*
*/
+#include <gtest/gtest.h>
#include <qeventloop.h>
#include "GpgCoreTest.h"
@@ -33,6 +34,7 @@
#include "core/function/gpg/GpgKeyOpera.h"
#include "core/function/result_analyse/GpgResultAnalyse.h"
#include "core/model/GpgGenKeyInfo.h"
+#include "core/model/GpgGenerateKeyResult.h"
#include "core/model/GpgKey.h"
#include "core/utils/GpgUtils.h"
#include "core/utils/MemoryUtils.h"
@@ -58,8 +60,12 @@ TEST_F(GpgCoreTest, GenerateKeyTest) {
ASSERT_EQ(CheckGpgError(err), GPG_ERR_NO_ERROR);
ASSERT_EQ(data_object->GetObjectSize(), 1);
- auto result = ExtractParams<GpgGenKeyResult>(data_object, 0);
- auto* fpr = result->fpr;
+ ASSERT_TRUE(data_object->Check<GpgGenerateKeyResult>());
+
+ auto result = ExtractParams<GpgGenerateKeyResult>(data_object, 0);
+ ASSERT_TRUE(result.IsGood());
+ auto fpr = result.GetFingerprint();
+
auto key =
GpgKeyGetter::GetInstance(kGpgFrontendDefaultChannel).GetKey(fpr);
ASSERT_TRUE(key.IsGood());
@@ -67,7 +73,7 @@ TEST_F(GpgCoreTest, GenerateKeyTest) {
GpgKeyOpera::GetInstance(kGpgFrontendDefaultChannel).DeleteKey(fpr);
callback_called_flag = true;
- ASSERT_FALSE(fpr == nullptr);
+ ASSERT_FALSE(fpr.empty());
});
int retry_count = 1000;
@@ -98,8 +104,13 @@ TEST_F(GpgCoreTest, GenerateKeyTest_1) {
const DataObjectPtr& data_object) {
ASSERT_EQ(CheckGpgError(err), GPG_ERR_NO_ERROR);
- auto result = ExtractParams<GpgGenKeyResult>(data_object, 0);
- auto* fpr = result->fpr;
+ ASSERT_EQ(data_object->GetObjectSize(), 1);
+ ASSERT_TRUE(data_object->Check<GpgGenerateKeyResult>());
+
+ auto result = ExtractParams<GpgGenerateKeyResult>(data_object, 0);
+ ASSERT_TRUE(result.IsGood());
+ auto fpr = result.GetFingerprint();
+
auto key =
GpgKeyGetter::GetInstance(kGpgFrontendDefaultChannel).GetKey(fpr);
ASSERT_TRUE(key.IsGood());
@@ -107,7 +118,7 @@ TEST_F(GpgCoreTest, GenerateKeyTest_1) {
GpgKeyOpera::GetInstance(kGpgFrontendDefaultChannel).DeleteKey(fpr);
callback_called_flag = true;
- ASSERT_FALSE(fpr == nullptr);
+ ASSERT_FALSE(fpr.empty());
});
int retry_count = 2000;
@@ -135,8 +146,10 @@ TEST_F(GpgCoreTest, GenerateKeyTest_4) {
const DataObjectPtr& data_object) {
ASSERT_EQ(CheckGpgError(err), GPG_ERR_NO_ERROR);
- auto result = ExtractParams<GpgGenKeyResult>(data_object, 0);
- auto* fpr = result->fpr;
+ auto result = ExtractParams<GpgGenerateKeyResult>(data_object, 0);
+ ASSERT_TRUE(result.IsGood());
+ auto fpr = result.GetFingerprint();
+
auto key =
GpgKeyGetter::GetInstance(kGpgFrontendDefaultChannel).GetKey(fpr);
ASSERT_TRUE(key.IsGood());
@@ -144,7 +157,7 @@ TEST_F(GpgCoreTest, GenerateKeyTest_4) {
GpgKeyOpera::GetInstance(kGpgFrontendDefaultChannel).DeleteKey(fpr);
callback_called_flag = true;
- ASSERT_FALSE(fpr == nullptr);
+ ASSERT_FALSE(fpr.empty());
});
int retry_count = 2000;
@@ -172,8 +185,10 @@ TEST_F(GpgCoreTest, GenerateKeyTest_5) {
const DataObjectPtr& data_object) {
ASSERT_EQ(CheckGpgError(err), GPG_ERR_NO_ERROR);
- auto result = ExtractParams<GpgGenKeyResult>(data_object, 0);
- auto* fpr = result->fpr;
+ auto result = ExtractParams<GpgGenerateKeyResult>(data_object, 0);
+ ASSERT_TRUE(result.IsGood());
+ auto fpr = result.GetFingerprint();
+
auto key =
GpgKeyGetter::GetInstance(kGpgFrontendDefaultChannel).GetKey(fpr);
ASSERT_TRUE(key.IsGood());
@@ -181,7 +196,7 @@ TEST_F(GpgCoreTest, GenerateKeyTest_5) {
GpgKeyOpera::GetInstance(kGpgFrontendDefaultChannel).DeleteKey(fpr);
callback_called_flag = true;
- ASSERT_FALSE(fpr == nullptr);
+ ASSERT_FALSE(fpr.empty());
});
int retry_count = 1000;