diff options
author | saturneric <[email protected]> | 2023-12-26 09:07:49 +0000 |
---|---|---|
committer | saturneric <[email protected]> | 2023-12-26 09:07:49 +0000 |
commit | f8733b9b2be66d0d8dd8a220ed972af8520192b4 (patch) | |
tree | 8b42c7c96aaeeb3ad63139d7ab6c668c9f7bc0d0 | |
parent | fix: test basical operations of gpg at both test cases and gui (diff) | |
download | GpgFrontend-f8733b9b2be66d0d8dd8a220ed972af8520192b4.tar.gz GpgFrontend-f8733b9b2be66d0d8dd8a220ed972af8520192b4.zip |
fix: add test cases and test file basical operations
-rw-r--r-- | src/core/function/gpg/GpgFileOpera.cpp | 131 | ||||
-rw-r--r-- | src/core/function/gpg/GpgFileOpera.h | 30 | ||||
-rw-r--r-- | src/core/utils/IOUtils.cpp | 39 | ||||
-rw-r--r-- | src/core/utils/IOUtils.h | 34 | ||||
-rw-r--r-- | src/test/GpgFrontendTest.cpp | 2 | ||||
-rw-r--r-- | src/test/core/GpgCoreTestBasicOpera.cpp | 133 | ||||
-rw-r--r-- | src/test/core/GpgCoreTestFileBasicOpera.cpp | 239 | ||||
-rw-r--r-- | src/ui/main_window/MainWindowFileSlotFunction.cpp | 149 |
8 files changed, 538 insertions, 219 deletions
diff --git a/src/core/function/gpg/GpgFileOpera.cpp b/src/core/function/gpg/GpgFileOpera.cpp index 1c577997..45286df2 100644 --- a/src/core/function/gpg/GpgFileOpera.cpp +++ b/src/core/function/gpg/GpgFileOpera.cpp @@ -42,20 +42,10 @@ namespace GpgFrontend { void GpgFileOpera::EncryptFile(std::vector<GpgKey> keys, - const std::string& in_path, - const std::string& out_path, bool ascii, + const std::filesystem::path& in_path, bool ascii, + const std::filesystem::path& out_path, const GpgOperationCallback& cb) { -#ifdef WINDOWS - auto in_path_std = - std::filesystem::path(QString::fromStdString(in_path).toStdU16String()); - auto out_path_std = - std::filesystem::path(QString::fromStdString(out_path).toStdU16String()); -#else - auto in_path_std = std::filesystem::path(in_path); - auto out_path_std = std::filesystem::path(out_path); -#endif - - auto read_result = ReadFileGFBuffer(in_path_std); + auto read_result = ReadFileGFBuffer(in_path); if (!std::get<0>(read_result)) { throw std::runtime_error("read file error"); } @@ -69,7 +59,7 @@ void GpgFileOpera::EncryptFile(std::vector<GpgKey> keys, auto result = ExtractParams<GpgEncryptResult>(data_object, 0); auto buffer = ExtractParams<GFBuffer>(data_object, 1); if (CheckGpgError(err) == GPG_ERR_NO_ERROR) { - if (!WriteFileGFBuffer(out_path_std, buffer)) { + if (!WriteFileGFBuffer(out_path, buffer)) { throw std::runtime_error("write buffer to file error"); } } @@ -77,20 +67,10 @@ void GpgFileOpera::EncryptFile(std::vector<GpgKey> keys, }); } -void GpgFileOpera::DecryptFile(const std::string& in_path, - const std::string& out_path, +void GpgFileOpera::DecryptFile(const std::filesystem::path& in_path, + const std::filesystem::path& out_path, const GpgOperationCallback& cb) { -#ifdef WINDOWS - auto in_path_std = - std::filesystem::path(QString::fromStdString(in_path).toStdU16String()); - auto out_path_std = - std::filesystem::path(QString::fromStdString(out_path).toStdU16String()); -#else - auto in_path_std = std::filesystem::path(in_path); - auto out_path_std = std::filesystem::path(out_path); -#endif - - auto read_result = ReadFileGFBuffer(in_path_std); + auto read_result = ReadFileGFBuffer(in_path); if (!std::get<0>(read_result)) { throw std::runtime_error("read file error"); } @@ -105,7 +85,7 @@ void GpgFileOpera::DecryptFile(const std::string& in_path, auto buffer = ExtractParams<GFBuffer>(data_object, 1); if (CheckGpgError(err) == GPG_ERR_NO_ERROR && - !WriteFileGFBuffer(out_path_std, buffer)) { + !WriteFileGFBuffer(out_path, buffer)) { throw std::runtime_error("write buffer to file error"); } @@ -113,20 +93,11 @@ void GpgFileOpera::DecryptFile(const std::string& in_path, }); } -void GpgFileOpera::SignFile(KeyArgsList keys, const std::string& in_path, - const std::string& out_path, bool ascii, +void GpgFileOpera::SignFile(KeyArgsList keys, + const std::filesystem::path& in_path, bool ascii, + const std::filesystem::path& out_path, const GpgOperationCallback& cb) { -#ifdef WINDOWS - auto in_path_std = - std::filesystem::path(QString::fromStdString(in_path).toStdU16String()); - auto out_path_std = - std::filesystem::path(QString::fromStdString(out_path).toStdU16String()); -#else - auto in_path_std = std::filesystem::path(in_path); - auto out_path_std = std::filesystem::path(out_path); -#endif - - auto read_result = ReadFileGFBuffer(in_path_std); + auto read_result = ReadFileGFBuffer(in_path); if (!std::get<0>(read_result)) { throw std::runtime_error("read file error"); } @@ -141,34 +112,24 @@ void GpgFileOpera::SignFile(KeyArgsList keys, const std::string& in_path, auto buffer = ExtractParams<GFBuffer>(data_object, 1); if (CheckGpgError(err) == GPG_ERR_NO_ERROR && - !WriteFileGFBuffer(out_path_std, buffer)) { + !WriteFileGFBuffer(out_path, buffer)) { throw std::runtime_error("write buffer to file error"); } cb(err, TransferParams(result)); }); } -void GpgFileOpera::VerifyFile(const std::string& data_path, - const std::string& sign_path, +void GpgFileOpera::VerifyFile(const std::filesystem::path& data_path, + const std::filesystem::path& sign_path, const GpgOperationCallback& cb) { -#ifdef WINDOWS - auto data_path_std = - std::filesystem::path(QString::fromStdString(data_path).toStdU16String()); - auto sign_path_std = - std::filesystem::path(QString::fromStdString(sign_path).toStdU16String()); -#else - auto data_path_std = std::filesystem::path(data_path); - auto sign_path_std = std::filesystem::path(sign_path); -#endif - - auto read_result = ReadFileGFBuffer(data_path_std); + auto read_result = ReadFileGFBuffer(data_path); if (!std::get<0>(read_result)) { throw std::runtime_error("read file error"); } GFBuffer sign_buffer; if (!sign_path.empty()) { - auto read_result = ReadFileGFBuffer(sign_path_std); + auto read_result = ReadFileGFBuffer(sign_path); if (!std::get<0>(read_result)) { throw std::runtime_error("read file error"); } @@ -187,20 +148,11 @@ void GpgFileOpera::VerifyFile(const std::string& data_path, } void GpgFileOpera::EncryptSignFile(KeyArgsList keys, KeyArgsList signer_keys, - const std::string& in_path, - const std::string& out_path, bool ascii, + const std::filesystem::path& in_path, + bool ascii, + const std::filesystem::path& out_path, const GpgOperationCallback& cb) { -#ifdef WINDOWS - auto in_path_std = - std::filesystem::path(QString::fromStdString(in_path).toStdU16String()); - auto out_path_std = - std::filesystem::path(QString::fromStdString(out_path).toStdU16String()); -#else - auto in_path_std = std::filesystem::path(in_path); - auto out_path_std = std::filesystem::path(out_path); -#endif - - auto read_result = ReadFileGFBuffer(in_path_std); + auto read_result = ReadFileGFBuffer(in_path); if (!std::get<0>(read_result)) { throw std::runtime_error("read file error"); } @@ -215,7 +167,7 @@ void GpgFileOpera::EncryptSignFile(KeyArgsList keys, KeyArgsList signer_keys, auto sign_result = ExtractParams<GpgSignResult>(data_object, 1); auto buffer = ExtractParams<GFBuffer>(data_object, 2); if (CheckGpgError(err) == GPG_ERR_NO_ERROR) { - if (!WriteFileGFBuffer(out_path_std, buffer)) { + if (!WriteFileGFBuffer(out_path, buffer)) { throw std::runtime_error("write buffer to file error"); } } @@ -223,20 +175,10 @@ void GpgFileOpera::EncryptSignFile(KeyArgsList keys, KeyArgsList signer_keys, }); } -void GpgFileOpera::DecryptVerifyFile(const std::string& in_path, - const std::string& out_path, +void GpgFileOpera::DecryptVerifyFile(const std::filesystem::path& in_path, + const std::filesystem::path& out_path, const GpgOperationCallback& cb) { -#ifdef WINDOWS - auto in_path_std = - std::filesystem::path(QString::fromStdString(in_path).toStdU16String()); - auto out_path_std = - std::filesystem::path(QString::fromStdString(out_path).toStdU16String()); -#else - auto in_path_std = std::filesystem::path(in_path); - auto out_path_std = std::filesystem::path(out_path); -#endif - - auto read_result = ReadFileGFBuffer(in_path_std); + auto read_result = ReadFileGFBuffer(in_path); if (!std::get<0>(read_result)) { throw std::runtime_error("read file error"); } @@ -252,27 +194,18 @@ void GpgFileOpera::DecryptVerifyFile(const std::string& in_path, auto verify_result = ExtractParams<GpgVerifyResult>(data_object, 1); auto buffer = ExtractParams<GFBuffer>(data_object, 2); if (CheckGpgError(err) == GPG_ERR_NO_ERROR) { - if (!WriteFileGFBuffer(out_path_std, buffer)) { + if (!WriteFileGFBuffer(out_path, buffer)) { throw std::runtime_error("write buffer to file error"); } } cb(err, TransferParams(decrypt_result, verify_result)); }); } -void GpgFileOpera::EncryptFileSymmetric(const std::string& in_path, - const std::string& out_path, bool ascii, +void GpgFileOpera::EncryptFileSymmetric(const std::filesystem::path& in_path, + bool ascii, + const std::filesystem::path& out_path, const GpgOperationCallback& cb) { -#ifdef WINDOWS - auto in_path_std = - std::filesystem::path(QString::fromStdString(in_path).toStdU16String()); - auto out_path_std = - std::filesystem::path(QString::fromStdString(out_path).toStdU16String()); -#else - auto in_path_std = std::filesystem::path(in_path); - auto out_path_std = std::filesystem::path(out_path); -#endif - - auto read_result = ReadFileGFBuffer(in_path_std); + auto read_result = ReadFileGFBuffer(in_path); if (!std::get<0>(read_result)) { throw std::runtime_error("read file error"); } @@ -290,9 +223,11 @@ void GpgFileOpera::EncryptFileSymmetric(const std::string& in_path, return; } - if (!WriteFileGFBuffer(out_path_std, buffer)) { + if (!WriteFileGFBuffer(out_path, buffer)) { throw std::runtime_error("write buffer to file error"); } + + cb(err, TransferParams(result)); }); } diff --git a/src/core/function/gpg/GpgFileOpera.h b/src/core/function/gpg/GpgFileOpera.h index 0f0d65bf..a358b60a 100644 --- a/src/core/function/gpg/GpgFileOpera.h +++ b/src/core/function/gpg/GpgFileOpera.h @@ -50,8 +50,9 @@ class GPGFRONTEND_CORE_EXPORT GpgFileOpera { * @param channel Channel in context * @return unsigned int error code */ - static void EncryptFile(KeyArgsList keys, const std::string& in_path, - const std::string& out_path, bool ascii, + static void EncryptFile(KeyArgsList keys, + const std::filesystem::path& in_path, bool ascii, + const std::filesystem::path& out_path, const GpgOperationCallback& cb); /** @@ -63,8 +64,9 @@ class GPGFRONTEND_CORE_EXPORT GpgFileOpera { * @param channel * @return unsigned int */ - static void EncryptFileSymmetric(const std::string& in_path, - const std::string& out_path, bool ascii, + static void EncryptFileSymmetric(const std::filesystem::path& in_path, + bool ascii, + const std::filesystem::path& out_path, const GpgOperationCallback& cb); /** @@ -75,8 +77,8 @@ class GPGFRONTEND_CORE_EXPORT GpgFileOpera { * @param result * @return GpgError */ - static void DecryptFile(const std::string& in_path, - const std::string& out_path, + static void DecryptFile(const std::filesystem::path& in_path, + const std::filesystem::path& out_path, const GpgOperationCallback& cb); /** @@ -89,8 +91,8 @@ class GPGFRONTEND_CORE_EXPORT GpgFileOpera { * @param channel * @return GpgError */ - static void SignFile(KeyArgsList keys, const std::string& in_path, - const std::string& out_path, bool ascii, + static void SignFile(KeyArgsList keys, const std::filesystem::path& in_path, + bool ascii, const std::filesystem::path& out_path, const GpgOperationCallback& cb); /** @@ -102,8 +104,8 @@ class GPGFRONTEND_CORE_EXPORT GpgFileOpera { * @param channel Channel in context * @return GpgError */ - static void VerifyFile(const std::string& data_path, - const std::string& sign_path, + static void VerifyFile(const std::filesystem::path& data_path, + const std::filesystem::path& sign_path, const GpgOperationCallback& cb); /** @@ -119,8 +121,8 @@ class GPGFRONTEND_CORE_EXPORT GpgFileOpera { * @return GpgError */ static void EncryptSignFile(KeyArgsList keys, KeyArgsList signer_keys, - const std::string& in_path, - const std::string& out_path, bool ascii, + const std::filesystem::path& in_path, bool ascii, + const std::filesystem::path& out_path, const GpgOperationCallback& cb); /** @@ -132,8 +134,8 @@ class GPGFRONTEND_CORE_EXPORT GpgFileOpera { * @param verify_res * @return GpgError */ - static void DecryptVerifyFile(const std::string& in_path, - const std::string& out_path, + static void DecryptVerifyFile(const std::filesystem::path& in_path, + const std::filesystem::path& out_path, const GpgOperationCallback& cb); }; diff --git a/src/core/utils/IOUtils.cpp b/src/core/utils/IOUtils.cpp index e0849a5f..d19b5581 100644 --- a/src/core/utils/IOUtils.cpp +++ b/src/core/utils/IOUtils.cpp @@ -28,6 +28,9 @@ #include "IOUtils.h" +#include <boost/uuid/uuid.hpp> +#include <boost/uuid/uuid_generators.hpp> +#include <boost/uuid/uuid_io.hpp> #include <sstream> #include "GpgModel.h" @@ -158,4 +161,40 @@ auto WriteBufferToFile(const std::string& utf8_path, const std::string& out_buffer) -> bool { return WriteFileStd(utf8_path, out_buffer); } + +auto ConvertPathByOS(const std::string& path) -> std::filesystem::path { +#ifdef WINDOWS + return {QString::fromStdString(path).toStdU16String()}; +#else + return {path}; +#endif +} + +auto ConvertPathByOS(const QString& path) -> std::filesystem::path { +#ifdef WINDOWS + return {path.toStdU16String()}; +#else + return {path.toStdString()}; +#endif +} + +auto GetTempFilePath() -> std::filesystem::path { + std::filesystem::path const temp_dir = std::filesystem::temp_directory_path(); + boost::uuids::uuid const uuid = boost::uuids::random_generator()(); + std::string const filename = boost::uuids::to_string(uuid) + ".data"; + std::filesystem::path const temp_file = temp_dir / filename; + return temp_dir / filename; +} + +auto CreateTempFileAndWriteData(const std::string& data) + -> std::filesystem::path { + auto temp_file = GetTempFilePath(); + std::ofstream file_stream(temp_file, std::ios::out | std::ios::trunc); + if (!file_stream.is_open()) { + throw std::runtime_error("Unable to open temporary file."); + } + file_stream << data; + file_stream.close(); + return temp_file.string(); +} } // namespace GpgFrontend
\ No newline at end of file diff --git a/src/core/utils/IOUtils.h b/src/core/utils/IOUtils.h index bb286cd6..b27f21fb 100644 --- a/src/core/utils/IOUtils.h +++ b/src/core/utils/IOUtils.h @@ -123,4 +123,38 @@ auto GPGFRONTEND_CORE_EXPORT ReadAllDataInFile(const std::string &path) auto GPGFRONTEND_CORE_EXPORT WriteBufferToFile(const std::string &path, const std::string &out_buffer) -> bool; + +/** + * @brief + * + * @param path + * @return std::filesystem::path + */ +auto GPGFRONTEND_CORE_EXPORT ConvertPathByOS(const std::string &path) + -> std::filesystem::path; + +/** + * @brief + * + * @return std::filesystem::path + */ +auto GPGFRONTEND_CORE_EXPORT ConvertPathByOS(const QString &) + -> std::filesystem::path; + +/** + * @brief + * + * @return std::filesystem::path + */ +auto GPGFRONTEND_CORE_EXPORT GetTempFilePath() -> std::filesystem::path; + +/** + * @brief + * + * @param data + * @return std::filesystem::path + */ +auto GPGFRONTEND_CORE_EXPORT CreateTempFileAndWriteData(const std::string &data) + -> std::filesystem::path; + } // namespace GpgFrontend diff --git a/src/test/GpgFrontendTest.cpp b/src/test/GpgFrontendTest.cpp index 73a18654..29903c39 100644 --- a/src/test/GpgFrontendTest.cpp +++ b/src/test/GpgFrontendTest.cpp @@ -122,7 +122,7 @@ void ConfigureGpgContext() { }); } -void ImportPrivateKeys(std::filesystem::path data_path, +void ImportPrivateKeys(const std::filesystem::path& data_path, const libconfig::Setting& config) { if (config.exists("load_keys.private_keys")) { auto& private_keys = config.lookup("load_keys.private_keys"); diff --git a/src/test/core/GpgCoreTestBasicOpera.cpp b/src/test/core/GpgCoreTestBasicOpera.cpp index 8c93f3f8..90645ddd 100644 --- a/src/test/core/GpgCoreTestBasicOpera.cpp +++ b/src/test/core/GpgCoreTestBasicOpera.cpp @@ -41,12 +41,14 @@ namespace GpgFrontend::Test { TEST_F(GpgCoreTest, CoreEncryptDecrTest) { + std::atomic_bool callback_called_flag{false}; + auto encrypt_key = GpgKeyGetter::GetInstance().GetPubkey( "E87C6A2D8D95C818DE93B3AE6A2764F8298DEB29"); GpgBasicOperator::GetInstance().Encrypt( {encrypt_key}, GFBuffer("Hello GpgFrontend!"), true, - [](GpgError err, const DataObjectPtr& data_obj) { + [&callback_called_flag](GpgError err, const DataObjectPtr& data_obj) { ASSERT_TRUE((data_obj->Check<GpgEncryptResult, GFBuffer>())); auto result = ExtractParams<GpgEncryptResult>(data_obj, 0); auto encr_out_buffer = ExtractParams<GFBuffer>(data_obj, 1); @@ -54,7 +56,8 @@ TEST_F(GpgCoreTest, CoreEncryptDecrTest) { ASSERT_EQ(CheckGpgError(err), GPG_ERR_NO_ERROR); GpgBasicOperator::GetInstance().Decrypt( - encr_out_buffer, [](GpgError err, const DataObjectPtr& data_obj) { + encr_out_buffer, [&callback_called_flag]( + GpgError err, const DataObjectPtr& data_obj) { auto d_result = ExtractParams<GpgDecryptResult>(data_obj, 0); auto decr_out_buffer = ExtractParams<GFBuffer>(data_obj, 1); ASSERT_EQ(CheckGpgError(err), GPG_ERR_NO_ERROR); @@ -62,15 +65,28 @@ TEST_F(GpgCoreTest, CoreEncryptDecrTest) { ASSERT_EQ(d_result.Recipients()[0].keyid, "6A2764F8298DEB29"); ASSERT_EQ(decr_out_buffer, GFBuffer("Hello GpgFrontend!")); + + // stop waiting + callback_called_flag = true; }); }); + + int retry_count = 1000; + while (!callback_called_flag && retry_count-- > 0) { + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + } + + ASSERT_TRUE(callback_called_flag); } TEST_F(GpgCoreTest, CoreEncryptSymmetricDecrTest) { + std::atomic_bool callback_called_flag{false}; + auto encrypt_text = GFBuffer("Hello GpgFrontend!"); GpgBasicOperator::GetInstance().EncryptSymmetric( - encrypt_text, true, [](GpgError err, const DataObjectPtr& data_obj) { + encrypt_text, true, + [&callback_called_flag](GpgError err, const DataObjectPtr& data_obj) { ASSERT_TRUE((data_obj->Check<GpgEncryptResult, GFBuffer>())); auto result = ExtractParams<GpgEncryptResult>(data_obj, 0); auto encr_out_buffer = ExtractParams<GFBuffer>(data_obj, 1); @@ -78,17 +94,30 @@ TEST_F(GpgCoreTest, CoreEncryptSymmetricDecrTest) { ASSERT_EQ(CheckGpgError(err), GPG_ERR_NO_ERROR); GpgBasicOperator::GetInstance().Decrypt( - encr_out_buffer, [](GpgError err, const DataObjectPtr& data_obj) { + encr_out_buffer, [&callback_called_flag]( + GpgError err, const DataObjectPtr& data_obj) { auto d_result = ExtractParams<GpgDecryptResult>(data_obj, 0); auto decr_out_buffer = ExtractParams<GFBuffer>(data_obj, 1); ASSERT_EQ(CheckGpgError(err), GPG_ERR_NO_ERROR); ASSERT_TRUE(d_result.Recipients().empty()); ASSERT_EQ(decr_out_buffer, GFBuffer("Hello GpgFrontend!")); + + // stop waiting + callback_called_flag = true; }); }); + + int retry_count = 1000; + while (!callback_called_flag && retry_count-- > 0) { + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + } + + ASSERT_TRUE(callback_called_flag); } TEST_F(GpgCoreTest, CoreEncryptDecrTest_KeyNotFound_1) { + std::atomic_bool callback_called_flag{false}; + auto encr_out_data = GFBuffer( "-----BEGIN PGP MESSAGE-----\n" "\n" @@ -103,16 +132,29 @@ TEST_F(GpgCoreTest, CoreEncryptDecrTest_KeyNotFound_1) { "-----END PGP MESSAGE-----"); GpgBasicOperator::GetInstance().Decrypt( - encr_out_data, [=](GpgError err, const DataObjectPtr& data_obj) { + encr_out_data, + [=, &callback_called_flag](GpgError err, const DataObjectPtr& data_obj) { auto d_result = ExtractParams<GpgDecryptResult>(data_obj, 0); auto decr_out_buffer = ExtractParams<GFBuffer>(data_obj, 1); ASSERT_EQ(CheckGpgError(err), GPG_ERR_NO_SECKEY); ASSERT_FALSE(d_result.Recipients().empty()); ASSERT_EQ(d_result.Recipients()[0].keyid, "A50CFD2F6C677D8C"); + + // stop waiting + callback_called_flag = true; }); + + int retry_count = 1000; + while (!callback_called_flag && retry_count-- > 0) { + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + } + + ASSERT_TRUE(callback_called_flag); } TEST_F(GpgCoreTest, CoreEncryptDecrTest_KeyNotFound_ResultAnalyse) { + std::atomic_bool callback_called_flag{false}; + auto encr_out_data = GFBuffer( "-----BEGIN PGP MESSAGE-----\n" "\n" @@ -127,7 +169,8 @@ TEST_F(GpgCoreTest, CoreEncryptDecrTest_KeyNotFound_ResultAnalyse) { "-----END PGP MESSAGE-----"); GpgBasicOperator::GetInstance().Decrypt( - encr_out_data, [=](GpgError err, const DataObjectPtr& data_obj) { + encr_out_data, + [=, &callback_called_flag](GpgError err, const DataObjectPtr& data_obj) { auto d_result = ExtractParams<GpgDecryptResult>(data_obj, 0); auto decr_out_buffer = ExtractParams<GFBuffer>(data_obj, 1); ASSERT_EQ(CheckGpgError(err), GPG_ERR_NO_SECKEY); @@ -138,17 +181,29 @@ TEST_F(GpgCoreTest, CoreEncryptDecrTest_KeyNotFound_ResultAnalyse) { analyse.Analyse(); ASSERT_EQ(analyse.GetStatus(), -1); ASSERT_FALSE(analyse.GetResultReport().empty()); + + // stop waiting + callback_called_flag = true; }); + + int retry_count = 1000; + while (!callback_called_flag && retry_count-- > 0) { + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + } + + ASSERT_TRUE(callback_called_flag); } TEST_F(GpgCoreTest, CoreSignVerifyNormalTest) { + std::atomic_bool callback_called_flag{false}; + auto sign_key = GpgKeyGetter::GetInstance().GetPubkey( "467F14220CE8DCF780CF4BAD8465C55B25C9B7D1"); auto sign_text = GFBuffer("Hello GpgFrontend!"); GpgBasicOperator::GetInstance().Sign( {sign_key}, sign_text, GPGME_SIG_MODE_NORMAL, true, - [](GpgError err, const DataObjectPtr& data_obj) { + [&callback_called_flag](GpgError err, const DataObjectPtr& data_obj) { ASSERT_TRUE((data_obj->Check<GpgSignResult, GFBuffer>())); auto result = ExtractParams<GpgSignResult>(data_obj, 0); auto sign_out_buffer = ExtractParams<GFBuffer>(data_obj, 1); @@ -157,24 +212,37 @@ TEST_F(GpgCoreTest, CoreSignVerifyNormalTest) { GpgBasicOperator::GetInstance().Verify( sign_out_buffer, GFBuffer(), - [](GpgError err, const DataObjectPtr& data_obj) { + [&callback_called_flag](GpgError err, + const DataObjectPtr& data_obj) { auto d_result = ExtractParams<GpgVerifyResult>(data_obj, 0); ASSERT_EQ(CheckGpgError(err), GPG_ERR_NO_ERROR); ASSERT_FALSE(d_result.GetSignature().empty()); ASSERT_EQ(d_result.GetSignature().at(0).GetFingerprint(), "467F14220CE8DCF780CF4BAD8465C55B25C9B7D1"); + + // stop waiting + callback_called_flag = true; }); }); + + int retry_count = 1000; + while (!callback_called_flag && retry_count-- > 0) { + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + } + + ASSERT_TRUE(callback_called_flag); } TEST_F(GpgCoreTest, CoreSignVerifyDetachTest) { + std::atomic_bool callback_called_flag{false}; + auto sign_key = GpgKeyGetter::GetInstance().GetPubkey( "467F14220CE8DCF780CF4BAD8465C55B25C9B7D1"); auto sign_text = GFBuffer("Hello GpgFrontend!"); GpgBasicOperator::GetInstance().Sign( {sign_key}, sign_text, GPGME_SIG_MODE_DETACH, true, - [=](GpgError err, const DataObjectPtr& data_obj) { + [=, &callback_called_flag](GpgError err, const DataObjectPtr& data_obj) { ASSERT_TRUE((data_obj->Check<GpgSignResult, GFBuffer>())); auto result = ExtractParams<GpgSignResult>(data_obj, 0); auto sign_out_buffer = ExtractParams<GFBuffer>(data_obj, 1); @@ -183,24 +251,37 @@ TEST_F(GpgCoreTest, CoreSignVerifyDetachTest) { GpgBasicOperator::GetInstance().Verify( sign_text, sign_out_buffer, - [](GpgError err, const DataObjectPtr& data_obj) { + [&callback_called_flag](GpgError err, + const DataObjectPtr& data_obj) { auto d_result = ExtractParams<GpgVerifyResult>(data_obj, 0); ASSERT_EQ(CheckGpgError(err), GPG_ERR_NO_ERROR); ASSERT_FALSE(d_result.GetSignature().empty()); ASSERT_EQ(d_result.GetSignature().at(0).GetFingerprint(), "467F14220CE8DCF780CF4BAD8465C55B25C9B7D1"); + + // stop waiting + callback_called_flag = true; }); }); + + int retry_count = 1000; + while (!callback_called_flag && retry_count-- > 0) { + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + } + + ASSERT_TRUE(callback_called_flag); } TEST_F(GpgCoreTest, CoreSignVerifyClearTest) { + std::atomic_bool callback_called_flag{false}; + auto sign_key = GpgKeyGetter::GetInstance().GetPubkey( "467F14220CE8DCF780CF4BAD8465C55B25C9B7D1"); auto sign_text = GFBuffer("Hello GpgFrontend!"); GpgBasicOperator::GetInstance().Sign( {sign_key}, sign_text, GPGME_SIG_MODE_CLEAR, true, - [](GpgError err, const DataObjectPtr& data_obj) { + [&callback_called_flag](GpgError err, const DataObjectPtr& data_obj) { ASSERT_TRUE((data_obj->Check<GpgSignResult, GFBuffer>())); auto result = ExtractParams<GpgSignResult>(data_obj, 0); auto sign_out_buffer = ExtractParams<GFBuffer>(data_obj, 1); @@ -209,17 +290,30 @@ TEST_F(GpgCoreTest, CoreSignVerifyClearTest) { GpgBasicOperator::GetInstance().Verify( sign_out_buffer, GFBuffer(), - [](GpgError err, const DataObjectPtr& data_obj) { + [&callback_called_flag](GpgError err, + const DataObjectPtr& data_obj) { auto verify_reult = ExtractParams<GpgVerifyResult>(data_obj, 0); ASSERT_EQ(CheckGpgError(err), GPG_ERR_NO_ERROR); ASSERT_FALSE(verify_reult.GetSignature().empty()); ASSERT_EQ(verify_reult.GetSignature().at(0).GetFingerprint(), "467F14220CE8DCF780CF4BAD8465C55B25C9B7D1"); + + // stop waiting + callback_called_flag = true; }); }); + + int retry_count = 1000; + while (!callback_called_flag && retry_count-- > 0) { + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + } + + ASSERT_TRUE(callback_called_flag); } TEST_F(GpgCoreTest, CoreEncryptSignDecrVerifyTest) { + std::atomic_bool callback_called_flag{false}; + auto encrypt_key = GpgKeyGetter::GetInstance().GetPubkey( "467F14220CE8DCF780CF4BAD8465C55B25C9B7D1"); auto sign_key = GpgKeyGetter::GetInstance().GetKey( @@ -231,7 +325,7 @@ TEST_F(GpgCoreTest, CoreEncryptSignDecrVerifyTest) { GpgBasicOperator::GetInstance().EncryptSign( {encrypt_key}, {sign_key}, encrypt_text, true, - [](GpgError err, const DataObjectPtr& data_obj) { + [&callback_called_flag](GpgError err, const DataObjectPtr& data_obj) { ASSERT_TRUE( (data_obj->Check<GpgEncryptResult, GpgSignResult, GFBuffer>())); auto encr_result = ExtractParams<GpgEncryptResult>(data_obj, 0); @@ -242,7 +336,8 @@ TEST_F(GpgCoreTest, CoreEncryptSignDecrVerifyTest) { ASSERT_EQ(CheckGpgError(err), GPG_ERR_NO_ERROR); GpgBasicOperator::GetInstance().DecryptVerify( - encr_out_buffer, [](GpgError err, const DataObjectPtr& data_obj) { + encr_out_buffer, [&callback_called_flag]( + GpgError err, const DataObjectPtr& data_obj) { ASSERT_TRUE( (data_obj ->Check<GpgDecryptResult, GpgVerifyResult, GFBuffer>())); @@ -261,8 +356,18 @@ TEST_F(GpgCoreTest, CoreEncryptSignDecrVerifyTest) { ASSERT_FALSE(verify_reult.GetSignature().empty()); ASSERT_EQ(verify_reult.GetSignature().at(0).GetFingerprint(), "8933EB283A18995F45D61DAC021D89771B680FFB"); + + // stop waiting + callback_called_flag = true; }); }); + + int retry_count = 1000; + while (!callback_called_flag && retry_count-- > 0) { + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + } + + ASSERT_TRUE(callback_called_flag); } } // namespace GpgFrontend::Test diff --git a/src/test/core/GpgCoreTestFileBasicOpera.cpp b/src/test/core/GpgCoreTestFileBasicOpera.cpp new file mode 100644 index 00000000..d07eae4f --- /dev/null +++ b/src/test/core/GpgCoreTestFileBasicOpera.cpp @@ -0,0 +1,239 @@ +/** + * 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 "GpgCoreTest.h" +#include "core/GpgModel.h" +#include "core/function/basic/ChannelObject.h" +#include "core/function/gpg/GpgBasicOperator.h" +#include "core/function/gpg/GpgFileOpera.h" +#include "core/function/gpg/GpgKeyGetter.h" +#include "core/function/result_analyse/GpgDecryptResultAnalyse.h" +#include "core/model/GpgDecryptResult.h" +#include "core/model/GpgEncryptResult.h" +#include "core/model/GpgSignResult.h" +#include "core/model/GpgVerifyResult.h" +#include "core/utils/GpgUtils.h" +#include "core/utils/IOUtils.h" + +namespace GpgFrontend::Test { + +TEST_F(GpgCoreTest, CoreFileEncryptDecrTest) { + std::atomic_bool callback_called_flag{false}; + + auto encrypt_key = GpgKeyGetter::GetInstance().GetPubkey( + "E87C6A2D8D95C818DE93B3AE6A2764F8298DEB29"); + auto input_file = CreateTempFileAndWriteData("Hello GpgFrontend!"); + auto output_file = GetTempFilePath(); + + GpgFileOpera::EncryptFile( + {encrypt_key}, input_file, true, output_file, + [output_file, &callback_called_flag](GpgError err, + const DataObjectPtr& data_obj) { + ASSERT_TRUE((data_obj->Check<GpgEncryptResult>())); + + auto result = ExtractParams<GpgEncryptResult>(data_obj, 0); + ASSERT_TRUE(result.InvalidRecipients().empty()); + ASSERT_EQ(CheckGpgError(err), GPG_ERR_NO_ERROR); + + auto decrpypt_output_file = GetTempFilePath(); + GpgFileOpera::DecryptFile( + output_file, decrpypt_output_file, + [decrpypt_output_file, &callback_called_flag]( + GpgError err, const DataObjectPtr& data_obj) { + auto d_result = ExtractParams<GpgDecryptResult>(data_obj, 0); + + ASSERT_EQ(CheckGpgError(err), GPG_ERR_NO_ERROR); + ASSERT_FALSE(d_result.Recipients().empty()); + ASSERT_EQ(d_result.Recipients()[0].keyid, "6A2764F8298DEB29"); + + const auto [read_success, buffer] = + ReadFileGFBuffer(decrpypt_output_file); + ASSERT_TRUE(read_success); + ASSERT_EQ(buffer, GFBuffer("Hello GpgFrontend!")); + + // stop waiting + callback_called_flag = true; + }); + }); + + int retry_count = 1000; + while (!callback_called_flag && retry_count-- > 0) { + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + } + + ASSERT_TRUE(callback_called_flag); +} + +TEST_F(GpgCoreTest, CoreFileEncryptSymmetricDecrTest) { + std::atomic_bool callback_called_flag{false}; + + auto input_file = CreateTempFileAndWriteData("Hello GpgFrontend!"); + auto output_file = GetTempFilePath(); + + GpgFileOpera::EncryptFileSymmetric( + input_file, true, output_file, + [&callback_called_flag, output_file](GpgError err, + const DataObjectPtr& data_obj) { + ASSERT_TRUE((data_obj->Check<GpgEncryptResult>())); + auto result = ExtractParams<GpgEncryptResult>(data_obj, 0); + ASSERT_TRUE(result.InvalidRecipients().empty()); + ASSERT_EQ(CheckGpgError(err), GPG_ERR_NO_ERROR); + + auto decrpypt_output_file = GetTempFilePath(); + GpgFileOpera::DecryptFile( + output_file, decrpypt_output_file, + [&callback_called_flag, decrpypt_output_file]( + GpgError err, const DataObjectPtr& data_obj) { + ASSERT_TRUE((data_obj->Check<GpgDecryptResult>())); + + auto decrypt_result = + ExtractParams<GpgDecryptResult>(data_obj, 0); + ASSERT_EQ(CheckGpgError(err), GPG_ERR_NO_ERROR); + ASSERT_TRUE(decrypt_result.Recipients().empty()); + + const auto [read_success, buffer] = + ReadFileGFBuffer(decrpypt_output_file); + ASSERT_TRUE(read_success); + ASSERT_EQ(buffer, GFBuffer("Hello GpgFrontend!")); + + // stop waiting + callback_called_flag = true; + }); + }); + + int retry_count = 1000; + while (!callback_called_flag && retry_count-- > 0) { + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + } + + ASSERT_TRUE(callback_called_flag); +} + +TEST_F(GpgCoreTest, CoreFileSignVerifyNormalTest) { + std::atomic_bool callback_called_flag{false}; + + auto sign_key = GpgKeyGetter::GetInstance().GetPubkey( + "467F14220CE8DCF780CF4BAD8465C55B25C9B7D1"); + auto input_file = CreateTempFileAndWriteData("Hello GpgFrontend!"); + auto output_file = GetTempFilePath(); + + GpgFileOpera::SignFile( + {sign_key}, input_file, true, output_file, + [&callback_called_flag, input_file, output_file]( + GpgError err, const DataObjectPtr& data_obj) { + ASSERT_TRUE((data_obj->Check<GpgSignResult>())); + auto result = ExtractParams<GpgSignResult>(data_obj, 0); + ASSERT_TRUE(result.InvalidSigners().empty()); + ASSERT_EQ(CheckGpgError(err), GPG_ERR_NO_ERROR); + + GpgFileOpera::VerifyFile( + input_file, output_file, + [&callback_called_flag](GpgError err, + const DataObjectPtr& data_obj) { + auto d_result = ExtractParams<GpgVerifyResult>(data_obj, 0); + ASSERT_EQ(CheckGpgError(err), GPG_ERR_NO_ERROR); + ASSERT_FALSE(d_result.GetSignature().empty()); + ASSERT_EQ(d_result.GetSignature().at(0).GetFingerprint(), + "467F14220CE8DCF780CF4BAD8465C55B25C9B7D1"); + + // stop waiting + callback_called_flag = true; + }); + }); + + int retry_count = 1000; + while (!callback_called_flag && retry_count-- > 0) { + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + } + + ASSERT_TRUE(callback_called_flag); +} + +TEST_F(GpgCoreTest, CoreFileEncryptSignDecrVerifyTest) { + std::atomic_bool callback_called_flag{false}; + + auto encrypt_key = GpgKeyGetter::GetInstance().GetPubkey( + "467F14220CE8DCF780CF4BAD8465C55B25C9B7D1"); + auto sign_key = GpgKeyGetter::GetInstance().GetKey( + "8933EB283A18995F45D61DAC021D89771B680FFB"); + auto input_file = CreateTempFileAndWriteData("Hello GpgFrontend!"); + auto output_file = GetTempFilePath(); + + ASSERT_TRUE(sign_key.IsPrivateKey()); + ASSERT_TRUE(sign_key.IsHasActualSigningCapability()); + + GpgFileOpera::EncryptSignFile( + {encrypt_key}, {sign_key}, input_file, true, output_file, + [&callback_called_flag, output_file](GpgError err, + const DataObjectPtr& data_obj) { + ASSERT_TRUE((data_obj->Check<GpgEncryptResult, GpgSignResult>())); + auto encr_result = ExtractParams<GpgEncryptResult>(data_obj, 0); + auto sign_result = ExtractParams<GpgSignResult>(data_obj, 1); + ASSERT_TRUE(encr_result.InvalidRecipients().empty()); + ASSERT_TRUE(sign_result.InvalidSigners().empty()); + ASSERT_EQ(CheckGpgError(err), GPG_ERR_NO_ERROR); + + auto decrpypt_output_file = GetTempFilePath(); + GpgFileOpera::DecryptVerifyFile( + output_file, decrpypt_output_file, + [&callback_called_flag, decrpypt_output_file]( + GpgError err, const DataObjectPtr& data_obj) { + ASSERT_TRUE( + (data_obj->Check<GpgDecryptResult, GpgVerifyResult>())); + auto decrypt_result = + ExtractParams<GpgDecryptResult>(data_obj, 0); + auto verify_reult = ExtractParams<GpgVerifyResult>(data_obj, 1); + + ASSERT_EQ(CheckGpgError(err), GPG_ERR_NO_ERROR); + + ASSERT_FALSE(decrypt_result.Recipients().empty()); + ASSERT_EQ(decrypt_result.Recipients()[0].keyid, + "F89C95A05088CC93"); + ASSERT_FALSE(verify_reult.GetSignature().empty()); + ASSERT_EQ(verify_reult.GetSignature().at(0).GetFingerprint(), + "8933EB283A18995F45D61DAC021D89771B680FFB"); + + const auto [read_success, buffer] = + ReadFileGFBuffer(decrpypt_output_file); + ASSERT_TRUE(read_success); + ASSERT_EQ(buffer, GFBuffer("Hello GpgFrontend!")); + + // stop waiting + callback_called_flag = true; + }); + }); + + int retry_count = 1000; + while (!callback_called_flag && retry_count-- > 0) { + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + } + + ASSERT_TRUE(callback_called_flag); +} + +} // namespace GpgFrontend::Test diff --git a/src/ui/main_window/MainWindowFileSlotFunction.cpp b/src/ui/main_window/MainWindowFileSlotFunction.cpp index 7447a1e7..b46ef5f5 100644 --- a/src/ui/main_window/MainWindowFileSlotFunction.cpp +++ b/src/ui/main_window/MainWindowFileSlotFunction.cpp @@ -43,12 +43,14 @@ namespace GpgFrontend::UI { -bool path_pre_check(QWidget* parent, const QString& path) { - QFileInfo file_info(path); - QFileInfo path_info(file_info.absolutePath()); +auto PathPreCheck(QWidget* parent, const std::filesystem::path& path) -> bool { + QFileInfo const file_info(path); + QFileInfo const path_info(file_info.absolutePath()); + if (!path_info.exists()) { - QMessageBox::critical(parent, _("Error"), - QString(_("The path %1 does not exist.")).arg(path)); + QMessageBox::critical( + parent, _("Error"), + QString(_("The path %1 does not exist.")).arg(path.c_str())); return false; } if (!file_info.isReadable()) { @@ -119,27 +121,21 @@ auto ProcessTarballIntoDirectory(QWidget* parent, * @param parent parent widget * @param path the tarball to be converted */ -bool process_directory_into_tarball(QWidget* parent, QString& path) { -#ifdef WINDOWS - std::filesystem::path selected_dir_path = path.toStdU16String(); -#else - std::filesystem::path selected_dir_path = path.toStdString(); -#endif - +auto ProcessDirectoryIntoTarball(QWidget* parent, std::filesystem::path path) + -> bool { try { - auto base_path = selected_dir_path.parent_path(); - auto target_path = selected_dir_path; - selected_dir_path.replace_extension(""); + auto base_path = path.parent_path(); + auto target_path = path; + path = path.replace_extension(""); SPDLOG_DEBUG("base path: {} target archive path: {} selected_dir_path: {}", - base_path.u8string(), target_path.u8string(), - selected_dir_path.u8string()); + base_path.u8string(), target_path.u8string(), path.u8string()); bool if_error = false; process_operation(parent, _("Making Tarball"), [&](DataObjectPtr) -> int { try { GpgFrontend::ArchiveFileOperator::CreateArchive(base_path, target_path, - 0, {selected_dir_path}); + 0, {path}); } catch (const std::runtime_error& e) { if_error = true; } @@ -159,9 +155,10 @@ bool process_directory_into_tarball(QWidget* parent, QString& path) { void MainWindow::SlotFileEncrypt() { auto* file_tree_view = edit_->SlotCurPageFileTreeView(); - auto path = file_tree_view->GetSelected(); + auto path_qstr = file_tree_view->GetSelected(); - if (!path_pre_check(this, path)) { + auto path = ConvertPathByOS(path_qstr); + if (!PathPreCheck(this, path)) { SPDLOG_ERROR("path pre check failed"); return; } @@ -175,7 +172,7 @@ void MainWindow::SlotFileEncrypt() { // get file info QFileInfo const file_info(path); if (file_info.isDir()) { - path = path + (file_info.isDir() ? ".tar" : ""); + path = path.replace_extension(".tar"); } const auto* extension = ".asc"; @@ -183,16 +180,11 @@ void MainWindow::SlotFileEncrypt() { extension = ".gpg"; } - auto out_path = path + extension; + auto out_path = path.replace_extension(path.extension().string() + extension); if (QFile::exists(out_path)) { -#ifdef WINDOWS - std::filesystem::path _out_path = out_path.toStdU16String(); -#else - std::filesystem::path _out_path = out_path.toStdString(); -#endif auto out_file_name = boost::format(_("The target file %1% already exists, " "do you need to overwrite it?")) % - _out_path.filename(); + out_path.filename(); auto ret = QMessageBox::warning(this, _("Warning"), out_file_name.str().c_str(), QMessageBox::Ok | QMessageBox::Cancel); @@ -202,7 +194,7 @@ void MainWindow::SlotFileEncrypt() { if (file_info.isDir()) { // stop if the process making tarball failed - if (!process_directory_into_tarball(this, path)) { + if (!ProcessDirectoryIntoTarball(this, path)) { QMessageBox::critical(this, _("Error"), _("Unable to convert the folder into tarball.")); return; @@ -221,8 +213,7 @@ void MainWindow::SlotFileEncrypt() { CommonUtils::WaitForOpera( this, _("Symmetrically Encrypting"), [=](const OperaWaitingHd& op_hd) { GpgFileOpera::EncryptFileSymmetric( - path.toStdString(), out_path.toStdString(), - !non_ascii_when_export, + path, !non_ascii_when_export, out_path, [=](GpgError err, const DataObjectPtr& data_obj) { // stop waiting op_hd(); @@ -241,8 +232,7 @@ void MainWindow::SlotFileEncrypt() { // remove xxx.tar and only left xxx.tar.gpg if (file_info.isDir()) { - auto selected_dir_path = - std::filesystem::path(path.toStdString()); + auto selected_dir_path = path; auto target_path = selected_dir_path.replace_extension(".tar"); if (exists(target_path)) { @@ -273,9 +263,8 @@ void MainWindow::SlotFileEncrypt() { CommonUtils::WaitForOpera( this, _("Encrypting"), [=](const OperaWaitingHd& op_hd) { GpgFileOpera::EncryptFile( - {p_keys->begin(), p_keys->end()}, path.toStdString(), - out_path.toStdString(), !non_ascii_when_export, - [=](GpgError err, const DataObjectPtr& data_obj) { + {p_keys->begin(), p_keys->end()}, path, !non_ascii_when_export, + out_path, [=](GpgError err, const DataObjectPtr& data_obj) { // stop waiting op_hd(); @@ -292,8 +281,7 @@ void MainWindow::SlotFileEncrypt() { // remove xxx.tar and only left xxx.tar.gpg if (file_info.isDir()) { - auto selected_dir_path = - std::filesystem::path(path.toStdString()); + auto selected_dir_path = path; auto target_path = selected_dir_path.replace_extension(".tar"); if (exists(target_path)) { std::filesystem::remove(target_path); @@ -305,16 +293,11 @@ void MainWindow::SlotFileEncrypt() { void MainWindow::SlotFileDecrypt() { auto* file_tree_view = edit_->SlotCurPageFileTreeView(); - auto path = file_tree_view->GetSelected(); - - if (!path_pre_check(this, path)) return; - -#ifdef WINDOWS - std::filesystem::path out_path = path.toStdU16String(); -#else - std::filesystem::path out_path = path.toStdString(); -#endif + auto path_qstr = file_tree_view->GetSelected(); + auto path = ConvertPathByOS(path_qstr); + if (!PathPreCheck(this, path)) return; + auto out_path = path; if (out_path.extension() == ".asc" || out_path.extension() == ".gpg") { out_path = out_path.parent_path() / out_path.stem(); } else { @@ -333,17 +316,16 @@ void MainWindow::SlotFileDecrypt() { CommonUtils::WaitForOpera( this, _("Decrypting"), [=](const OperaWaitingHd& op_hd) { GpgFileOpera::DecryptFile( - path.toStdString(), out_path.u8string(), - [=](GpgError err, const DataObjectPtr& data_obj) { + path, out_path, [=](GpgError err, const DataObjectPtr& data_obj) { // stop waiting op_hd(); - auto result = ExtractParams<GpgDecryptResult>(data_obj, 0); - if (data_obj == nullptr || !data_obj->Check<GpgDecryptResult>()) { throw std::runtime_error("data object doesn't pass checking"); } + auto result = ExtractParams<GpgDecryptResult>(data_obj, 0); + auto result_analyse = GpgDecryptResultAnalyse(err, result); result_analyse.Analyse(); @@ -379,9 +361,9 @@ void MainWindow::SlotFileDecrypt() { void MainWindow::SlotFileSign() { auto* file_tree_view = edit_->SlotCurPageFileTreeView(); - auto path = file_tree_view->GetSelected(); - - if (!path_pre_check(this, path)) return; + auto path_qstr = file_tree_view->GetSelected(); + auto path = ConvertPathByOS(path_qstr); + if (!PathPreCheck(this, path)) return; auto key_ids = m_key_list_->GetChecked(); auto keys = GpgKeyGetter::GetInstance().GetKeys(key_ids); @@ -405,7 +387,7 @@ void MainWindow::SlotFileSign() { } } - bool non_ascii_when_export = + bool const non_ascii_when_export = GlobalSettingStation::GetInstance().LookupSettings( "general.non_ascii_when_export", true); @@ -414,13 +396,7 @@ void MainWindow::SlotFileSign() { extension = ".sig"; } -#ifdef WINDOWS - std::filesystem::path in_path = path.toStdU16String(); -#else - std::filesystem::path in_path = path.toStdString(); -#endif - - auto sig_file_path = in_path; + auto sig_file_path = path; sig_file_path += extension; if (exists(sig_file_path)) { auto ret = QMessageBox::warning( @@ -436,9 +412,8 @@ void MainWindow::SlotFileSign() { CommonUtils::WaitForOpera( this, _("Signing"), [=](const OperaWaitingHd& op_hd) { GpgFileOpera::EncryptFile( - {keys->begin(), keys->end()}, in_path.u8string(), - sig_file_path.u8string(), !non_ascii_when_export, - [=](GpgError err, const DataObjectPtr& data_obj) { + {keys->begin(), keys->end()}, path, !non_ascii_when_export, + sig_file_path, [=](GpgError err, const DataObjectPtr& data_obj) { // stop waiting op_hd(); @@ -533,9 +508,9 @@ void MainWindow::SlotFileVerify() { void MainWindow::SlotFileEncryptSign() { auto* file_tree_view = edit_->SlotCurPageFileTreeView(); - auto path = file_tree_view->GetSelected(); - - if (!path_pre_check(this, path)) return; + auto path_qstr = file_tree_view->GetSelected(); + auto path = ConvertPathByOS(path_qstr); + if (!PathPreCheck(this, path)) return; // check selected keys auto key_ids = m_key_list_->GetChecked(); @@ -550,7 +525,7 @@ void MainWindow::SlotFileEncryptSign() { // check key abilities for (const auto& key : *p_keys) { - bool key_can_encrypt = key.IsHasActualEncryptionCapability(); + bool const key_can_encrypt = key.IsHasActualEncryptionCapability(); if (!key_can_encrypt) { QMessageBox::critical( @@ -568,9 +543,8 @@ void MainWindow::SlotFileEncryptSign() { // get file info QFileInfo const file_info(path); - if (file_info.isDir()) { - path = path + (file_info.isDir() ? ".tar" : ""); + path = path.replace_extension(".tar"); } const auto* extension = ".asc"; @@ -578,8 +552,7 @@ void MainWindow::SlotFileEncryptSign() { extension = ".gpg"; } - auto out_path = path + extension; - + auto out_path = path.replace_extension(path.extension().string() + extension); if (QFile::exists(out_path)) { auto ret = QMessageBox::warning( this, _("Warning"), @@ -603,7 +576,7 @@ void MainWindow::SlotFileEncryptSign() { // convert directory into tarball if (file_info.isDir()) { // stop if the process making tarball failed - if (!process_directory_into_tarball(this, path)) { + if (!ProcessDirectoryIntoTarball(this, path)) { QMessageBox::critical(this, _("Error"), _("Unable to convert the folder into tarball.")); return; @@ -614,8 +587,8 @@ void MainWindow::SlotFileEncryptSign() { this, _("Encrypting and Signing"), [=](const OperaWaitingHd& op_hd) { GpgFileOpera::EncryptSignFile( {p_keys->begin(), p_keys->end()}, - {p_signer_keys->begin(), p_signer_keys->end()}, path.toStdString(), - out_path.toStdString(), !non_ascii_when_export, + {p_signer_keys->begin(), p_signer_keys->end()}, path, + !non_ascii_when_export, out_path, [=](GpgError err, const DataObjectPtr& data_obj) { // stop waiting op_hd(); @@ -642,8 +615,7 @@ void MainWindow::SlotFileEncryptSign() { // remove xxx.tar and only left xxx.tar.gpg if (file_info.isDir()) { - auto selected_dir_path = - std::filesystem::path(path.toStdString()); + auto selected_dir_path = path; auto target_path = selected_dir_path.replace_extension(".tar"); if (exists(target_path)) { std::filesystem::remove(target_path); @@ -655,19 +627,13 @@ void MainWindow::SlotFileEncryptSign() { void MainWindow::SlotFileDecryptVerify() { auto* file_tree_view = edit_->SlotCurPageFileTreeView(); - auto path = file_tree_view->GetSelected(); - - if (!path_pre_check(this, path)) return; - -#ifdef WINDOWS - std::filesystem::path in_path = path.toStdU16String(); -#else - std::filesystem::path in_path = path.toStdString(); -#endif + auto path_qstr = file_tree_view->GetSelected(); + auto path = ConvertPathByOS(path_qstr); + if (!PathPreCheck(this, path)) return; - std::filesystem::path out_path = in_path; - if (in_path.extension() == ".asc" || in_path.extension() == ".gpg") { - out_path = in_path.parent_path() / out_path.stem(); + std::filesystem::path out_path = path; + if (path.extension() == ".asc" || path.extension() == ".gpg") { + out_path = path.parent_path() / out_path.stem(); } else { out_path += ".out"; } @@ -687,8 +653,7 @@ void MainWindow::SlotFileDecryptVerify() { CommonUtils::WaitForOpera( this, _("Decrypting and Verifying"), [=](const OperaWaitingHd& op_hd) { GpgFileOpera::DecryptVerifyFile( - path.toStdString(), out_path.u8string(), - [=](GpgError err, const DataObjectPtr& data_obj) { + path, out_path, [=](GpgError err, const DataObjectPtr& data_obj) { // stop waiting op_hd(); |