diff options
author | saturneric <[email protected]> | 2023-12-06 13:10:48 +0000 |
---|---|---|
committer | saturneric <[email protected]> | 2023-12-06 13:18:07 +0000 |
commit | a0f0fd98efa12ef33959badcc25ff568e8b242b4 (patch) | |
tree | 860563fdbfe353e57724b08c4eb30dc03d5f8a7d /src | |
parent | feat: convert basic opera to async style (diff) | |
download | GpgFrontend-a0f0fd98efa12ef33959badcc25ff568e8b242b4.tar.gz GpgFrontend-a0f0fd98efa12ef33959badcc25ff568e8b242b4.zip |
feat: move test to src and add submodule googletest
Diffstat (limited to '')
23 files changed, 810 insertions, 144 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 31b865d6..df0ae996 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -104,6 +104,13 @@ if (BUILD_MODULE) add_subdirectory(module) endif () +# build to test gpgfrontend core +if (BUILD_TEST) + include(CTest) + enable_testing() + add_subdirectory(test) +endif () + if (BUILD_APPLICATION) # Set Resource Output Path if (${CMAKE_BUILD_TYPE} STREQUAL "Release") @@ -476,7 +483,7 @@ endif () # link options for GpgFrontend if (BUILD_APPLICATION) - target_link_libraries(${AppName} gpgfrontend_ui gpgfrontend_module) + target_link_libraries(${AppName} gpgfrontend_ui gpgfrontend_module gpgfrontend_test) if (MINGW) message(STATUS "Link Application Library For MINGW") target_link_libraries(${AppName} crypto) diff --git a/src/app.cpp b/src/app.cpp index b0c771d9..d12761fd 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -27,11 +27,9 @@ */ #include <csetjmp> -#include <csignal> #include "core/GpgConstants.h" #include "core/GpgCoreInit.h" -#include "core/function/GlobalSettingStation.h" #include "module/GpgFrontendModuleInit.h" #include "ui/GpgFrontendApplication.h" #include "ui/GpgFrontendUIInit.h" diff --git a/src/cmd.cpp b/src/cmd.cpp index 24080a08..f0d748e0 100644 --- a/src/cmd.cpp +++ b/src/cmd.cpp @@ -36,23 +36,23 @@ // GpgFrontend #include "GpgFrontendBuildInfo.h" -#include "spdlog/common.h" +#include "type.h" namespace po = boost::program_options; auto PrintVersion() -> int { std::cout << PROJECT_NAME << " " << "v" << VERSION_MAJOR << "." << VERSION_MINOR << "." - << VERSION_PATCH << std::endl; + << VERSION_PATCH << '\n'; std::cout - << "Copyright (C) 2021 Saturneric <[email protected]>" << std::endl + << "Copyright (C) 2021 Saturneric <[email protected]>" << '\n' << _("This is free software; see the source for copying conditions.") - << std::endl - << std::endl; + << '\n' + << '\n'; - std::cout << _("Build Timestamp: ") << BUILD_TIMESTAMP << std::endl - << _("Build Version: ") << BUILD_VERSION << std::endl - << _("Source Code Version: ") << GIT_VERSION << std::endl; + std::cout << _("Build Timestamp: ") << BUILD_TIMESTAMP << '\n' + << _("Build Version: ") << BUILD_VERSION << '\n' + << _("Source Code Version: ") << GIT_VERSION << '\n'; return 0; } @@ -77,4 +77,6 @@ auto ParseLogLevel(const po::variables_map& vm) -> spdlog::level::level_enum { } return spdlog::level::info; -}
\ No newline at end of file +} + +auto RunTest(int argc, char** argv) {}
\ No newline at end of file diff --git a/src/core/GpgConstants.h b/src/core/GpgConstants.h index 1f3a4981..c2125650 100644 --- a/src/core/GpgConstants.h +++ b/src/core/GpgConstants.h @@ -34,8 +34,8 @@ constexpr int kRestartCode = 1000; ///< only refresh ui constexpr int kDeepRestartCode = 1001; // refresh core and ui // Channels -constexpr int kGpgfrontendDefaultChannel = 0; ///< -constexpr int kGpgfrontendNonAsciiChannel = 2; ///< +constexpr int kGpgFrontendDefaultChannel = 0; ///< +constexpr int kGpgFrontendNonAsciiChannel = 2; ///< // HEADER constexpr const char* PGP_CRYPT_BEGIN = "-----BEGIN PGP MESSAGE-----"; ///< diff --git a/src/core/GpgCoreInit.cpp b/src/core/GpgCoreInit.cpp index 6d96a50d..e2aa50ff 100644 --- a/src/core/GpgCoreInit.cpp +++ b/src/core/GpgCoreInit.cpp @@ -303,7 +303,7 @@ void InitGpgFrontendCore() { // init ctx, also checking the basical env auto& ctx = GpgFrontend::GpgContext::CreateInstance( - kGpgfrontendDefaultChannel, [=]() -> ChannelObjectPtr { + kGpgFrontendDefaultChannel, [=]() -> ChannelObjectPtr { GpgFrontend::GpgContextInitArgs args; // set key database path diff --git a/src/core/function/GlobalSettingStation.cpp b/src/core/function/GlobalSettingStation.cpp index 5dc10dc1..5031effe 100644 --- a/src/core/function/GlobalSettingStation.cpp +++ b/src/core/function/GlobalSettingStation.cpp @@ -28,6 +28,8 @@ #include "GlobalSettingStation.h" +#include <qcoreapplication.h> + #include "core/utils/FilesystemUtils.h" #include "core/utils/IOUtils.h" @@ -216,11 +218,14 @@ class GlobalSettingStation::Impl private: std::filesystem::path app_path_ = QCoreApplication::applicationDirPath() .toStdString(); ///< Program Location + std::filesystem::path app_data_path_ = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) .toStdString(); ///< Program Data Location + std::filesystem::path app_log_path_ = app_data_path_ / "logs"; ///< Program Data Location + std::filesystem::path app_data_objs_path_ = app_data_path_ / "data_objs"; ///< Object storage path diff --git a/src/core/function/GlobalSettingStation.h b/src/core/function/GlobalSettingStation.h index 18f42db1..66fc4b6a 100644 --- a/src/core/function/GlobalSettingStation.h +++ b/src/core/function/GlobalSettingStation.h @@ -91,6 +91,11 @@ class GPGFRONTEND_CORE_EXPORT GlobalSettingStation */ [[nodiscard]] auto GetStandaloneDatabaseDir() const -> std::filesystem::path; + /** + * @brief Get the App Config Path object + * + * @return std::filesystem::path + */ [[nodiscard]] auto GetAppConfigPath() const -> std::filesystem::path; /** @@ -121,8 +126,18 @@ class GPGFRONTEND_CORE_EXPORT GlobalSettingStation */ [[nodiscard]] auto GetCertsDir() const -> std::filesystem::path; + /** + * @brief Get the Log Files Size object + * + * @return std::string + */ [[nodiscard]] auto GetLogFilesSize() const -> std::string; + /** + * @brief Get the Data Objects Files Size object + * + * @return std::string + */ [[nodiscard]] auto GetDataObjectsFilesSize() const -> std::string; /** diff --git a/src/core/function/basic/ChannelObject.h b/src/core/function/basic/ChannelObject.h index 7bbd6d94..6fb315ae 100644 --- a/src/core/function/basic/ChannelObject.h +++ b/src/core/function/basic/ChannelObject.h @@ -28,12 +28,10 @@ #pragma once +#include "core/GpgConstants.h" #include "core/function/SecureMemoryAllocator.h" namespace GpgFrontend { -static constexpr int kGpgFrontendDefaultChannel = - 0; ///< the default channel id - /** * @brief object which in channel system is called "channel" * diff --git a/src/core/function/basic/GpgFunctionObject.h b/src/core/function/basic/GpgFunctionObject.h index 619cad04..dd06608d 100644 --- a/src/core/function/basic/GpgFunctionObject.h +++ b/src/core/function/basic/GpgFunctionObject.h @@ -64,7 +64,7 @@ class SingletonFunctionObject : public ChannelObject { * @param channel * @return T& */ - static auto GetInstance(int channel = GpgFrontend::kGpgfrontendDefaultChannel) + static auto GetInstance(int channel = GpgFrontend::kGpgFrontendDefaultChannel) -> T& { static std::mutex g_channel_mutex_map_lock; static std::map<int, std::mutex> g_channel_mutex_map; diff --git a/src/core/function/gpg/GpgFileOpera.h b/src/core/function/gpg/GpgFileOpera.h index b20fa7db..569ea51a 100644 --- a/src/core/function/gpg/GpgFileOpera.h +++ b/src/core/function/gpg/GpgFileOpera.h @@ -52,7 +52,7 @@ class GPGFRONTEND_CORE_EXPORT GpgFileOpera { */ static auto EncryptFile(KeyListPtr keys, const std::string& in_path, const std::string& out_path, GpgEncrResult& result, - int channel = kGpgfrontendDefaultChannel) + int channel = kGpgFrontendDefaultChannel) -> unsigned int; /** @@ -67,7 +67,7 @@ class GPGFRONTEND_CORE_EXPORT GpgFileOpera { static auto EncryptFileSymmetric(const std::string& in_path, const std::string& out_path, GpgEncrResult& result, - int channel = kGpgfrontendDefaultChannel) + int channel = kGpgFrontendDefaultChannel) -> unsigned int; /** @@ -94,7 +94,7 @@ class GPGFRONTEND_CORE_EXPORT GpgFileOpera { */ static auto SignFile(KeyListPtr keys, const std::string& in_path, const std::string& out_path, GpgSignResult& result, - int channel = kGpgfrontendDefaultChannel) -> GpgError; + int channel = kGpgFrontendDefaultChannel) -> GpgError; /** * @brief Verify file with public key @@ -107,7 +107,7 @@ class GPGFRONTEND_CORE_EXPORT GpgFileOpera { */ static auto VerifyFile(const std::string& data_path, const std::string& sign_path, GpgVerifyResult& result, - int channel = kGpgfrontendDefaultChannel) -> GpgError; + int channel = kGpgFrontendDefaultChannel) -> GpgError; /** * @brief Encrypt and sign file with public key and private key @@ -125,7 +125,7 @@ class GPGFRONTEND_CORE_EXPORT GpgFileOpera { const std::string& in_path, const std::string& out_path, GpgEncrResult& encr_res, GpgSignResult& sign_res, - int channel = kGpgfrontendDefaultChannel) + int channel = kGpgFrontendDefaultChannel) -> GpgError; /** diff --git a/src/core/module/GlobalModuleContext.cpp b/src/core/module/GlobalModuleContext.cpp index 88250a1e..9eddb57a 100644 --- a/src/core/module/GlobalModuleContext.cpp +++ b/src/core/module/GlobalModuleContext.cpp @@ -52,8 +52,8 @@ class GlobalModuleContext::Impl { .total_milliseconds()), default_task_runner_(std::move(task_runner)) { // Initialize acquired channels with default values. - acquired_channel_.insert(kGpgfrontendDefaultChannel); - acquired_channel_.insert(kGpgfrontendNonAsciiChannel); + acquired_channel_.insert(kGpgFrontendDefaultChannel); + acquired_channel_.insert(kGpgFrontendNonAsciiChannel); } auto GetChannel(ModuleRawPtr module) -> int { @@ -74,7 +74,7 @@ class GlobalModuleContext::Impl { } static auto GetDefaultChannel(ModuleRawPtr) -> int { - return kGpgfrontendDefaultChannel; + return kGpgFrontendDefaultChannel; } auto GetTaskRunner(ModuleRawPtr module) -> std::optional<TaskRunnerPtr> { diff --git a/test/CMakeLists.txt b/src/test/CMakeLists.txt index 7cb41029..c4d7bcf8 100644 --- a/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -23,31 +23,24 @@ # # SPDX-License-Identifier: GPL-3.0-or-later - -find_package(GTest REQUIRED) - # Set configure for test -file(COPY ${CMAKE_SOURCE_DIR}/test/conf DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/ FOLLOW_SYMLINK_CHAIN) -file(COPY ${CMAKE_SOURCE_DIR}/test/data DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/ FOLLOW_SYMLINK_CHAIN) +file(COPY ${CMAKE_SOURCE_DIR}/resource/lfs/test/conf DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/test/ FOLLOW_SYMLINK_CHAIN) +file(COPY ${CMAKE_SOURCE_DIR}/resource/lfs/test/data DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/test/ FOLLOW_SYMLINK_CHAIN) +aux_source_directory(./core TEST_SOURCE) aux_source_directory(. TEST_SOURCE) -add_executable( - ${AppName} - ${TEST_SOURCE} -) +add_library(gpgfrontend_test SHARED ${TEST_SOURCE}) -if (GPG_CORE) - target_link_libraries(${AppName} gpg_core) -endif () +set(_export_file "${CMAKE_CURRENT_SOURCE_DIR}/GpgFrontendTestExport.h") +generate_export_header(gpgfrontend_test EXPORT_FILE_NAME "${_export_file}") -if (GPG_STANDALONE_MODE) - file(COPY ${CMAKE_SOURCE_DIR}/test/gpg DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/ FOLLOW_SYMLINK_CHAIN) -endif () +target_link_libraries(gpgfrontend_test PRIVATE gtest) +target_link_libraries(gpgfrontend_test PRIVATE gpgfrontend_core) +target_link_libraries(gpgfrontend_test PRIVATE spdlog) -target_link_libraries(${AppName} ${Boost_LIBRARIES} gtest gtest_main) if (APPLE) - target_link_libraries(${AppName} intl) + target_link_libraries(gpgfrontend_test intl) endif () -add_test(AllTestsInGpgFrontend ${AppName}) +add_test(AllTestsInGpgFrontend gpgfrontend_test) diff --git a/src/test/GpgFrontendCoreExport.h b/src/test/GpgFrontendCoreExport.h new file mode 100644 index 00000000..4d1b2f0b --- /dev/null +++ b/src/test/GpgFrontendCoreExport.h @@ -0,0 +1,42 @@ + +#ifndef GPGFRONTEND_TEST_EXPORT_H +#define GPGFRONTEND_TEST_EXPORT_H + +#ifdef GPGFRONTEND_TEST_STATIC_DEFINE +# define GPGFRONTEND_TEST_EXPORT +# define GPGFRONTEND_TEST_NO_EXPORT +#else +# ifndef GPGFRONTEND_TEST_EXPORT +# ifdef gpgfrontend_test_EXPORTS + /* We are building this library */ +# define GPGFRONTEND_TEST_EXPORT __attribute__((visibility("default"))) +# else + /* We are using this library */ +# define GPGFRONTEND_TEST_EXPORT __attribute__((visibility("default"))) +# endif +# endif + +# ifndef GPGFRONTEND_TEST_NO_EXPORT +# define GPGFRONTEND_TEST_NO_EXPORT __attribute__((visibility("hidden"))) +# endif +#endif + +#ifndef GPGFRONTEND_TEST_DEPRECATED +# define GPGFRONTEND_TEST_DEPRECATED __attribute__ ((__deprecated__)) +#endif + +#ifndef GPGFRONTEND_TEST_DEPRECATED_EXPORT +# define GPGFRONTEND_TEST_DEPRECATED_EXPORT GPGFRONTEND_TEST_EXPORT GPGFRONTEND_TEST_DEPRECATED +#endif + +#ifndef GPGFRONTEND_TEST_DEPRECATED_NO_EXPORT +# define GPGFRONTEND_TEST_DEPRECATED_NO_EXPORT GPGFRONTEND_TEST_NO_EXPORT GPGFRONTEND_TEST_DEPRECATED +#endif + +#if 0 /* DEFINE_NO_DEPRECATED */ +# ifndef GPGFRONTEND_TEST_NO_DEPRECATED +# define GPGFRONTEND_TEST_NO_DEPRECATED +# endif +#endif + +#endif /* GPGFRONTEND_TEST_EXPORT_H */ diff --git a/src/test/GpgFrontendTest.cpp b/src/test/GpgFrontendTest.cpp new file mode 100644 index 00000000..24e099a2 --- /dev/null +++ b/src/test/GpgFrontendTest.cpp @@ -0,0 +1,114 @@ +/** + * 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 "GpgFrontendTest.h" + +#include <boost/date_time.hpp> +#include <boost/dll.hpp> +#include <filesystem> + +#include "core/function/GlobalSettingStation.h" +#include "core/function/gpg/GpgContext.h" +#include "spdlog/spdlog.h" +#include "type.h" + +namespace GpgFrontend::Test { + +void InitTestLoggingSystem(spdlog::level::level_enum level) { + // get the log directory + auto logfile_path = + (GlobalSettingStation::GetInstance().GetLogDir() / "test"); + logfile_path.replace_extension(".log"); + + // sinks + std::vector<spdlog::sink_ptr> sinks; + sinks.push_back( + SecureCreateSharedObject<spdlog::sinks::stderr_color_sink_mt>()); + sinks.push_back( + SecureCreateSharedObject<spdlog::sinks::rotating_file_sink_mt>( + logfile_path.u8string(), 1048576 * 32, 8)); + + // logger + auto test_logger = SecureCreateSharedObject<spdlog::logger>( + "test", begin(sinks), end(sinks)); + test_logger->set_pattern( + "[%H:%M:%S.%e] [T:%t] [%=6n] %^[%=8l]%$ [%s:%#] [%!] -> %v (+%ius)"); + + // set the level of logger + test_logger->set_level(level); + + // register it as default logger + spdlog::set_default_logger(test_logger); +} + +auto GenerateRandomString(size_t length) -> std::string { + const std::string 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; + for (size_t i = 0; i < length; ++i) { + random_string += characters[distribution(generator)]; + } + + return random_string; +} + +void ConfigureGpgContext() { + auto db_path = + std::filesystem::temp_directory_path() / GenerateRandomString(12); + SPDLOG_DEBUG("setting up new database path for test case: {}", + db_path.string()); + + 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); + } + + GpgContext::CreateInstance( + kGpgFrontendDefaultChannel, [&]() -> ChannelObjectPtr { + GpgContextInitArgs args; + args.test_mode = true; + args.offline_mode = true; + args.db_path = db_path.string(); + + return ConvertToChannelObjectPtr<>(SecureCreateUniqueObject<GpgContext>( + args, kGpgFrontendDefaultChannel)); + }); +} + +void ExecuteAllTestCase(InitArgs args) { + InitTestLoggingSystem(args.log_level); + ConfigureGpgContext(); +} + +} // namespace GpgFrontend::Test
\ No newline at end of file diff --git a/test/GpgCoreTestImportExport.cpp b/src/test/GpgFrontendTest.h index 34f34ba0..55c6b734 100644 --- a/test/GpgCoreTestImportExport.cpp +++ b/src/test/GpgFrontendTest.h @@ -26,13 +26,16 @@ * */ -#include <string> -#include <vector> +#pragma once -#include "GpgFrontendTest.h" -#include "gpg/GpgConstants.h" -#include "gpg/function/GpgKeyGetter.h" -#include "gpg/function/GpgKeyImportExporter.h" -#include "gpg/model/GpgKey.h" +#include <gtest/gtest.h> +#include <spdlog/sinks/rotating_file_sink.h> +#include <spdlog/sinks/stdout_color_sinks.h> +#include <spdlog/spdlog.h> -TEST_F(GpgCoreTest, CoreExportSecretTest) {}
\ No newline at end of file + +#include <libconfig.h++> + +#include "GpgFrontendTestExport.h" + +namespace GpgFrontend::Test {} // namespace GpgFrontend::Test diff --git a/src/test/GpgFrontendTestExport.h b/src/test/GpgFrontendTestExport.h new file mode 100644 index 00000000..4d1b2f0b --- /dev/null +++ b/src/test/GpgFrontendTestExport.h @@ -0,0 +1,42 @@ + +#ifndef GPGFRONTEND_TEST_EXPORT_H +#define GPGFRONTEND_TEST_EXPORT_H + +#ifdef GPGFRONTEND_TEST_STATIC_DEFINE +# define GPGFRONTEND_TEST_EXPORT +# define GPGFRONTEND_TEST_NO_EXPORT +#else +# ifndef GPGFRONTEND_TEST_EXPORT +# ifdef gpgfrontend_test_EXPORTS + /* We are building this library */ +# define GPGFRONTEND_TEST_EXPORT __attribute__((visibility("default"))) +# else + /* We are using this library */ +# define GPGFRONTEND_TEST_EXPORT __attribute__((visibility("default"))) +# endif +# endif + +# ifndef GPGFRONTEND_TEST_NO_EXPORT +# define GPGFRONTEND_TEST_NO_EXPORT __attribute__((visibility("hidden"))) +# endif +#endif + +#ifndef GPGFRONTEND_TEST_DEPRECATED +# define GPGFRONTEND_TEST_DEPRECATED __attribute__ ((__deprecated__)) +#endif + +#ifndef GPGFRONTEND_TEST_DEPRECATED_EXPORT +# define GPGFRONTEND_TEST_DEPRECATED_EXPORT GPGFRONTEND_TEST_EXPORT GPGFRONTEND_TEST_DEPRECATED +#endif + +#ifndef GPGFRONTEND_TEST_DEPRECATED_NO_EXPORT +# define GPGFRONTEND_TEST_DEPRECATED_NO_EXPORT GPGFRONTEND_TEST_NO_EXPORT GPGFRONTEND_TEST_DEPRECATED +#endif + +#if 0 /* DEFINE_NO_DEPRECATED */ +# ifndef GPGFRONTEND_TEST_NO_DEPRECATED +# define GPGFRONTEND_TEST_NO_DEPRECATED +# endif +#endif + +#endif /* GPGFRONTEND_TEST_EXPORT_H */ diff --git a/src/test/core/GpgCoreTest.cpp b/src/test/core/GpgCoreTest.cpp new file mode 100644 index 00000000..b4cf2f73 --- /dev/null +++ b/src/test/core/GpgCoreTest.cpp @@ -0,0 +1,61 @@ +/** + * 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/function/gpg/GpgKeyImportExporter.h" +#include "core/utils/IOUtils.h" + +namespace GpgFrontend::Test { + +void GpgCoreTest::import_private_keys(const libconfig::Setting& root) { + if (root.exists("load_keys.private_keys")) { + auto& private_keys = root.lookup("load_keys.private_keys"); + for (auto& private_key : private_keys) { + 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 = std::make_unique<std::string>(data); + GpgKeyImportExporter::GetInstance(kGpgFrontendDefaultChannel) + .ImportKey(std::move(secret_key_copy)); + } + } + } +} + +void GpgCoreTest::TearDown() {} + +void GpgCoreTest::SetUp() { + libconfig::Config cfg; + ASSERT_NO_THROW(cfg.readFile(config_path_.c_str())); + auto& root = cfg.getRoot(); + import_private_keys(root); +} +} // namespace GpgFrontend::Test diff --git a/src/test/core/GpgCoreTest.h b/src/test/core/GpgCoreTest.h new file mode 100644 index 00000000..bffaf9ad --- /dev/null +++ b/src/test/core/GpgCoreTest.h @@ -0,0 +1,62 @@ +/** + * 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 <boost/date_time.hpp> +#include <boost/dll.hpp> +#include <boost/dll/runtime_symbol_info.hpp> +#include <filesystem> + +#include "GpgFrontendTest.h" +#include "core/function/GlobalSettingStation.h" +#include "core/typedef/CoreTypedef.h" + +namespace GpgFrontend::Test { + +class GpgCoreTest : public ::testing::Test { + public: + void SetUp() override; + + void TearDown() override; + + private: + void import_private_keys(const libconfig::Setting& root); + + // Configure File Location + std::filesystem::path config_path_ = + GlobalSettingStation::GetInstance().GetAppDir() / "test" / "conf" / + "core.cfg"; + + // Data File Directory Location + std::filesystem::path data_path_ = + GlobalSettingStation::GetInstance().GetAppDir() / "test" / "data"; + +}; + +} // namespace GpgFrontend::Test
\ No newline at end of file diff --git a/test/GpgCoreTestBasicOpera.cpp b/src/test/core/GpgCoreTestBasicOpera.cpp index 6980e700..59cb2fa2 100644 --- a/test/GpgCoreTestBasicOpera.cpp +++ b/src/test/core/GpgCoreTestBasicOpera.cpp @@ -26,64 +26,62 @@ * */ -#include <gtest/gtest.h> +#include "GpgCoreTest.h" +#include "core/GpgModel.h" +#include "core/function/basic/ChannelObject.h" +#include "core/function/gpg/GpgBasicOperator.h" +#include "core/function/gpg/GpgKeyGetter.h" +#include "core/function/result_analyse/GpgDecryptResultAnalyse.h" +#include "core/utils/GpgUtils.h" -#include <string> -#include <vector> - -#include "GpgFrontendTest.h" -#include "gpg/GpgConstants.h" -#include "gpg/function/GpgBasicOperator.h" -#include "gpg/function/GpgKeyGetter.h" -#include "gpg/result_analyse/GpgDecryptResultAnalyse.h" - -using namespace GpgFrontend; +namespace GpgFrontend::Test { TEST_F(GpgCoreTest, CoreEncryptDecrTest) { - auto encrypt_key = GpgKeyGetter::GetInstance(default_channel) + auto encrypt_key = GpgKeyGetter::GetInstance(kGpgFrontendDefaultChannel) .GetPubkey("467F14220CE8DCF780CF4BAD8465C55B25C9B7D1"); ByteArray encrypt_text = "Hello GpgFrontend!"; - ByteArrayPtr encr_out_data; - GpgEncrResult e_result; KeyListPtr keys = std::make_unique<KeyArgsList>(); keys->push_back(std::move(encrypt_key)); - auto err = - BasicOperator::GetInstance(default_channel) - .Encrypt(std::move(keys), encrypt_text, encr_out_data, e_result); - ASSERT_EQ(e_result->invalid_recipients, nullptr); - ASSERT_EQ(check_gpg_error_2_err_code(err), GPG_ERR_NO_ERROR); - - GpgDecrResult d_result; - ByteArrayPtr decr_out_data; - err = BasicOperator::GetInstance(default_channel) - .Decrypt(*encr_out_data, decr_out_data, d_result); - ASSERT_EQ(check_gpg_error_2_err_code(err), GPG_ERR_NO_ERROR); - ASSERT_NE(d_result->recipients, nullptr); - ASSERT_EQ(std::string(d_result->recipients->keyid), "F89C95A05088CC93"); - ASSERT_EQ(*decr_out_data, encrypt_text); -} - -TEST_F(GpgCoreTest, CoreEncryptDecrTest_KeyNotFound_1) { - ByteArrayPtr encr_out_data = std::make_unique<ByteArray>( - "-----BEGIN PGP MESSAGE-----\n" - "\n" - "hQEMA6UM/S9sZ32MAQf9Fb6gp6nvgKTQBv2mmjXia6ODXYq6kNeLsPVzLCbHyWOs\n" - "0GDED11R1NksA3EQxFf4fzLkDpbo68r5bWy7c28c99Fr68IRET19Tw6Gu65MQezD\n" - "Rdzo1oVqmK9sfKqOT3+0S2H+suFYw5kfBztMZLVGGl9R9fOXdKcj0fqGs2br3e9D\n" - "ArBFqq07Bae2DD1J8mckWB2x9Uem4vjRiY+vEJcEdAS1N5xu1n7qzzyDgcRcS34X\n" - "PNBQeTrFMc2RS7mnip2DbyZVEjORobhguK6xZyqXXbvFacStGWDLptV3dcCn4JRO\n" - "dIORyt5wugqAtgE4qEGTvr/pJ/oXPw4Wve/trece/9I/AR38vW8ntVmDa/hV75iZ\n" - "4QGAhQ8grD4kq31GHXHUOmBX51XXW9SINmplC8elEx3R460EUZJjjb0OvTih+eZH\n" - "=8n2H\n" - "-----END PGP MESSAGE-----"); - - GpgDecrResult d_result; - ByteArrayPtr decr_out_data; - auto err = BasicOperator::GetInstance(default_channel) - .Decrypt(*encr_out_data, decr_out_data, d_result); - ASSERT_EQ(check_gpg_error_2_err_code(err), GPG_ERR_NO_SECKEY); - ASSERT_NE(d_result->recipients, nullptr); - ASSERT_EQ(std::string(d_result->recipients->keyid), "A50CFD2F6C677D8C"); + GpgBasicOperator::GetInstance(kGpgFrontendDefaultChannel) + .Encrypt(std::move(keys), encrypt_text, + [](GpgError err, const DataObjectPtr&) { + + }); + + // ASSERT_EQ(e_result->invalid_recipients, nullptr); + // ASSERT_EQ(CheckGpgError(err), GPG_ERR_NO_ERROR); + + // GpgDecrResult d_result; + // ByteArrayPtr decr_out_data; + // err = GpgBasicOperator::GetInstance(kGpgFrontendDefaultChannel) + // .Decrypt(*encr_out_data, decr_out_data, d_result); + // ASSERT_EQ(CheckGpgError(err), GPG_ERR_NO_ERROR); + // ASSERT_NE(d_result->recipients, nullptr); + // ASSERT_EQ(std::string(d_result->recipients->keyid), "F89C95A05088CC93"); + // ASSERT_EQ(*decr_out_data, encrypt_text); + // } + + // TEST_F(GpgCoreTest, CoreEncryptDecrTest_KeyNotFound_1) { + // ByteArrayPtr encr_out_data = std::make_unique<ByteArray>( + // "-----BEGIN PGP MESSAGE-----\n" + // "\n" + // "hQEMA6UM/S9sZ32MAQf9Fb6gp6nvgKTQBv2mmjXia6ODXYq6kNeLsPVzLCbHyWOs\n" + // "0GDED11R1NksA3EQxFf4fzLkDpbo68r5bWy7c28c99Fr68IRET19Tw6Gu65MQezD\n" + // "Rdzo1oVqmK9sfKqOT3+0S2H+suFYw5kfBztMZLVGGl9R9fOXdKcj0fqGs2br3e9D\n" + // "ArBFqq07Bae2DD1J8mckWB2x9Uem4vjRiY+vEJcEdAS1N5xu1n7qzzyDgcRcS34X\n" + // "PNBQeTrFMc2RS7mnip2DbyZVEjORobhguK6xZyqXXbvFacStGWDLptV3dcCn4JRO\n" + // "dIORyt5wugqAtgE4qEGTvr/pJ/oXPw4Wve/trece/9I/AR38vW8ntVmDa/hV75iZ\n" + // "4QGAhQ8grD4kq31GHXHUOmBX51XXW9SINmplC8elEx3R460EUZJjjb0OvTih+eZH\n" + // "=8n2H\n" + // "-----END PGP MESSAGE-----"); + + // GpgDecrResult d_result; + // ByteArrayPtr decr_out_data; + // auto err = GpgBasicOperator::GetInstance(kGpgFrontendDefaultChannel) + // .Decrypt(*encr_out_data, decr_out_data, d_result); + // ASSERT_EQ(CheckGpgError(err), GPG_ERR_NO_SECKEY); + // ASSERT_NE(d_result->recipients, nullptr); + // ASSERT_EQ(std::string(d_result->recipients->keyid), "A50CFD2F6C677D8C"); } TEST_F(GpgCoreTest, CoreEncryptDecrTest_KeyNotFound_ResultAnalyse) { @@ -102,37 +100,37 @@ TEST_F(GpgCoreTest, CoreEncryptDecrTest_KeyNotFound_ResultAnalyse) { GpgDecrResult d_result; ByteArrayPtr decr_out_data; - auto err = BasicOperator::GetInstance(default_channel) + auto err = GpgBasicOperator::GetInstance(kGpgFrontendDefaultChannel) .Decrypt(*encr_out_data, decr_out_data, d_result); - ASSERT_EQ(check_gpg_error_2_err_code(err), GPG_ERR_NO_SECKEY); + ASSERT_EQ(CheckGpgError(err), GPG_ERR_NO_SECKEY); ASSERT_NE(d_result->recipients, nullptr); ASSERT_EQ(std::string(d_result->recipients->keyid), "A50CFD2F6C677D8C"); - DecryptResultAnalyse analyse{err, d_result}; - analyse.analyse(); - ASSERT_EQ(analyse.getStatus(), -1); - ASSERT_FALSE(analyse.getResultReport().empty()); + GpgDecryptResultAnalyse analyse{err, d_result}; + analyse.Analyse(); + ASSERT_EQ(analyse.GetStatus(), -1); + ASSERT_FALSE(analyse.GetResultReport().empty()); } TEST_F(GpgCoreTest, CoreSignVerifyNormalTest) { - auto encrypt_key = GpgKeyGetter::GetInstance(default_channel) + auto encrypt_key = GpgKeyGetter::GetInstance(kGpgFrontendDefaultChannel) .GetPubkey("467F14220CE8DCF780CF4BAD8465C55B25C9B7D1"); ByteArray sign_text = "Hello GpgFrontend!"; ByteArrayPtr sign_out_data; GpgSignResult s_result; KeyListPtr keys = std::make_unique<KeyArgsList>(); keys->push_back(std::move(encrypt_key)); - auto err = BasicOperator::GetInstance(default_channel) + auto err = GpgBasicOperator::GetInstance(kGpgFrontendDefaultChannel) .Sign(std::move(keys), sign_text, sign_out_data, GPGME_SIG_MODE_NORMAL, s_result); - ASSERT_EQ(check_gpg_error_2_err_code(err), GPG_ERR_NO_ERROR); + ASSERT_EQ(CheckGpgError(err), GPG_ERR_NO_ERROR); ASSERT_EQ(s_result->invalid_signers, nullptr); GpgVerifyResult v_result; ByteArrayPtr sign_buff = nullptr; - err = BasicOperator::GetInstance(default_channel) + err = GpgBasicOperator::GetInstance(kGpgFrontendDefaultChannel) .Verify(*sign_out_data, sign_buff, v_result); - ASSERT_EQ(check_gpg_error_2_err_code(err), GPG_ERR_NO_ERROR); + ASSERT_EQ(CheckGpgError(err), GPG_ERR_NO_ERROR); ASSERT_NE(v_result->signatures, nullptr); ASSERT_EQ(std::string(v_result->signatures->fpr), "467F14220CE8DCF780CF4BAD8465C55B25C9B7D1"); @@ -140,23 +138,23 @@ TEST_F(GpgCoreTest, CoreSignVerifyNormalTest) { } TEST_F(GpgCoreTest, CoreSignVerifyDetachTest) { - auto encrypt_key = GpgKeyGetter::GetInstance(default_channel) + auto encrypt_key = GpgKeyGetter::GetInstance(kGpgFrontendDefaultChannel) .GetPubkey("467F14220CE8DCF780CF4BAD8465C55B25C9B7D1"); ByteArray sign_text = "Hello GpgFrontend!"; ByteArrayPtr sign_out_data; GpgSignResult s_result; KeyListPtr keys = std::make_unique<KeyArgsList>(); keys->push_back(std::move(encrypt_key)); - auto err = BasicOperator::GetInstance(default_channel) + auto err = GpgBasicOperator::GetInstance(kGpgFrontendDefaultChannel) .Sign(std::move(keys), sign_text, sign_out_data, GPGME_SIG_MODE_DETACH, s_result); - ASSERT_EQ(check_gpg_error_2_err_code(err), GPG_ERR_NO_ERROR); + ASSERT_EQ(CheckGpgError(err), GPG_ERR_NO_ERROR); ASSERT_EQ(s_result->invalid_signers, nullptr); GpgVerifyResult v_result; - err = BasicOperator::GetInstance(default_channel) + err = GpgBasicOperator::GetInstance(kGpgFrontendDefaultChannel) .Verify(sign_text, sign_out_data, v_result); - ASSERT_EQ(check_gpg_error_2_err_code(err), GPG_ERR_NO_ERROR); + ASSERT_EQ(CheckGpgError(err), GPG_ERR_NO_ERROR); ASSERT_NE(v_result->signatures, nullptr); ASSERT_EQ(std::string(v_result->signatures->fpr), "467F14220CE8DCF780CF4BAD8465C55B25C9B7D1"); @@ -164,24 +162,24 @@ TEST_F(GpgCoreTest, CoreSignVerifyDetachTest) { } TEST_F(GpgCoreTest, CoreSignVerifyClearTest) { - auto sign_key = GpgKeyGetter::GetInstance(default_channel) + auto sign_key = GpgKeyGetter::GetInstance(kGpgFrontendDefaultChannel) .GetKey("467F14220CE8DCF780CF4BAD8465C55B25C9B7D1"); ByteArray sign_text = "Hello GpgFrontend!"; ByteArrayPtr sign_out_data; GpgSignResult s_result; KeyListPtr keys = std::make_unique<KeyArgsList>(); keys->push_back(std::move(sign_key)); - auto err = BasicOperator::GetInstance(default_channel) + auto err = GpgBasicOperator::GetInstance(kGpgFrontendDefaultChannel) .Sign(std::move(keys), sign_text, sign_out_data, GPGME_SIG_MODE_CLEAR, s_result); - ASSERT_EQ(check_gpg_error_2_err_code(err), GPG_ERR_NO_ERROR); + ASSERT_EQ(CheckGpgError(err), GPG_ERR_NO_ERROR); ASSERT_EQ(s_result->invalid_signers, nullptr); GpgVerifyResult v_result; ByteArrayPtr sign_buff = nullptr; - err = BasicOperator::GetInstance(default_channel) + err = GpgBasicOperator::GetInstance(kGpgFrontendDefaultChannel) .Verify(*sign_out_data, sign_buff, v_result); - ASSERT_EQ(check_gpg_error_2_err_code(err), GPG_ERR_NO_ERROR); + ASSERT_EQ(CheckGpgError(err), GPG_ERR_NO_ERROR); ASSERT_NE(v_result->signatures, nullptr); ASSERT_EQ(std::string(v_result->signatures->fpr), "467F14220CE8DCF780CF4BAD8465C55B25C9B7D1"); @@ -189,14 +187,14 @@ TEST_F(GpgCoreTest, CoreSignVerifyClearTest) { } TEST_F(GpgCoreTest, CoreEncryptSignDecrVerifyTest) { - auto encrypt_key = GpgKeyGetter::GetInstance(default_channel) + auto encrypt_key = GpgKeyGetter::GetInstance(kGpgFrontendDefaultChannel) .GetPubkey("467F14220CE8DCF780CF4BAD8465C55B25C9B7D1"); - auto sign_key = GpgKeyGetter::GetInstance(default_channel) + auto sign_key = GpgKeyGetter::GetInstance(kGpgFrontendDefaultChannel) .GetKey("8933EB283A18995F45D61DAC021D89771B680FFB"); // Question? // ASSERT_FALSE(encrypt_key.is_private_key()); - ASSERT_TRUE(sign_key.is_private_key()); - ASSERT_TRUE(sign_key.CanSignActual()); + ASSERT_TRUE(sign_key.IsPrivateKey()); + ASSERT_TRUE(sign_key.IsHasActualSigningCapability()); ByteArray encrypt_text = "Hello GpgFrontend!"; ByteArrayPtr encr_out_data; GpgEncrResult e_result; @@ -207,19 +205,19 @@ TEST_F(GpgCoreTest, CoreEncryptSignDecrVerifyTest) { keys->push_back(std::move(encrypt_key)); sign_keys->push_back(std::move(sign_key)); - auto err = BasicOperator::GetInstance(default_channel) + auto err = GpgBasicOperator::GetInstance(kGpgFrontendDefaultChannel) .EncryptSign(std::move(keys), std::move(sign_keys), encrypt_text, encr_out_data, e_result, s_result); - ASSERT_EQ(check_gpg_error_2_err_code(err), GPG_ERR_NO_ERROR); + ASSERT_EQ(CheckGpgError(err), GPG_ERR_NO_ERROR); ASSERT_EQ(e_result->invalid_recipients, nullptr); ASSERT_EQ(s_result->invalid_signers, nullptr); GpgDecrResult d_result; GpgVerifyResult v_result; ByteArrayPtr decr_out_data = nullptr; - err = BasicOperator::GetInstance(default_channel) + err = GpgBasicOperator::GetInstance(kGpgFrontendDefaultChannel) .DecryptVerify(*encr_out_data, decr_out_data, d_result, v_result); - ASSERT_EQ(check_gpg_error_2_err_code(err), GPG_ERR_NO_ERROR); + ASSERT_EQ(CheckGpgError(err), GPG_ERR_NO_ERROR); ASSERT_NE(d_result->recipients, nullptr); ASSERT_EQ(std::string(d_result->recipients->keyid), "F89C95A05088CC93"); ASSERT_EQ(*decr_out_data, encrypt_text); @@ -228,3 +226,5 @@ TEST_F(GpgCoreTest, CoreEncryptSignDecrVerifyTest) { "8933EB283A18995F45D61DAC021D89771B680FFB"); ASSERT_EQ(v_result->signatures->next, nullptr); } + +} // namespace GpgFrontend::Test diff --git a/test/GpgCoreTest.cpp b/src/test/core/GpgCoreTestImportExport.cpp index 2e18c76c..82e0bde4 100644 --- a/test/GpgCoreTest.cpp +++ b/src/test/core/GpgCoreTestImportExport.cpp @@ -26,7 +26,14 @@ * */ -#include "GpgFrontendTest.h" +#include <string> +#include <vector> -// Should be used once and once-only -INITIALIZE_EASYLOGGINGPP +#include "GpgCoreTest.h" +#include "core/GpgConstants.h" + +namespace GpgFrontend::Test { + +TEST_F(GpgCoreTest, CoreExportSecretTest) {} + +} // namespace GpgFrontend::Test
\ No newline at end of file diff --git a/src/test/core/GpgCoreTestKeyModel.cpp b/src/test/core/GpgCoreTestKeyModel.cpp new file mode 100644 index 00000000..ff3fbbf8 --- /dev/null +++ b/src/test/core/GpgCoreTestKeyModel.cpp @@ -0,0 +1,179 @@ +/** + * 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/function/gpg/GpgContext.h" +#include "core/function/gpg/GpgKeyGetter.h" +#include "core/model/GpgData.h" +#include "core/model/GpgKey.h" +#include "core/utils/GpgUtils.h" + +namespace GpgFrontend::Test { + +TEST_F(GpgCoreTest, CoreInitTest) { + auto& ctx = GpgContext::GetInstance(kGpgFrontendDefaultChannel); + auto& ctx_default = GpgContext::GetInstance(); + ASSERT_TRUE(ctx.Good()); + ASSERT_TRUE(ctx_default.Good()); +} + +TEST_F(GpgCoreTest, GpgDataTest) { + auto data_buff = std::string( + "cqEh8fyKWtmiXrW2zzlszJVGJrpXDDpzgP7ZELGxhfZYFi8rMrSVKDwrpFZBSWMG"); + + GpgData data(data_buff.data(), data_buff.size()); + + auto out_buffer = data.Read2Buffer(); + ASSERT_EQ(out_buffer->size(), 64); +} + +TEST_F(GpgCoreTest, GpgKeyTest) { + auto key = GpgKeyGetter::GetInstance(kGpgFrontendDefaultChannel) + .GetKey("9490795B78F8AFE9F93BD09281704859182661FB"); + ASSERT_TRUE(key.IsGood()); + ASSERT_TRUE(key.IsPrivateKey()); + ASSERT_TRUE(key.IsHasMasterKey()); + + ASSERT_FALSE(key.IsDisabled()); + ASSERT_FALSE(key.IsRevoked()); + + ASSERT_EQ(key.GetProtocol(), "OpenPGP"); + + ASSERT_EQ(key.GetSubKeys()->size(), 2); + ASSERT_EQ(key.GetUIDs()->size(), 1); + + ASSERT_TRUE(key.IsHasCertificationCapability()); + ASSERT_TRUE(key.IsHasEncryptionCapability()); + ASSERT_TRUE(key.IsHasSigningCapability()); + ASSERT_FALSE(key.IsHasAuthenticationCapability()); + ASSERT_TRUE(key.IsHasActualCertificationCapability()); + ASSERT_TRUE(key.IsHasActualEncryptionCapability()); + ASSERT_TRUE(key.IsHasActualSigningCapability()); + ASSERT_FALSE(key.IsHasActualAuthenticationCapability()); + + ASSERT_EQ(key.GetName(), "GpgFrontendTest"); + ASSERT_TRUE(key.GetComment().empty()); + ASSERT_EQ(key.GetEmail(), "[email protected]"); + ASSERT_EQ(key.GetId(), "81704859182661FB"); + ASSERT_EQ(key.GetFingerprint(), "9490795B78F8AFE9F93BD09281704859182661FB"); + ASSERT_EQ(key.GetExpireTime(), + boost::posix_time::from_iso_string("20230905T040000")); + ASSERT_EQ(key.GetPublicKeyAlgo(), "RSA"); + ASSERT_EQ(key.GetPrimaryKeyLength(), 3072); + ASSERT_EQ(key.GetLastUpdateTime(), + boost::posix_time::from_iso_string("19700101T000000")); + ASSERT_EQ(key.GetCreateTime(), + boost::posix_time::from_iso_string("20210905T060153")); + + ASSERT_EQ(key.GetOwnerTrust(), "Unknown"); + + ASSERT_EQ(key.IsExpired(), key.GetExpireTime() < + boost::posix_time::second_clock::local_time()); +} + +TEST_F(GpgCoreTest, GpgSubKeyTest) { + auto key = GpgKeyGetter::GetInstance(kGpgFrontendDefaultChannel) + .GetKey("9490795B78F8AFE9F93BD09281704859182661FB"); + auto sub_keys = key.GetSubKeys(); + ASSERT_EQ(sub_keys->size(), 2); + + auto& sub_key = sub_keys->back(); + + ASSERT_FALSE(sub_key.IsRevoked()); + ASSERT_FALSE(sub_key.IsDisabled()); + ASSERT_EQ(sub_key.GetCreateTime(), + boost::posix_time::from_iso_string("20210905T060153")); + + ASSERT_FALSE(sub_key.IsCardKey()); + ASSERT_TRUE(sub_key.IsPrivateKey()); + ASSERT_EQ(sub_key.GetID(), "2B36803235B5E25B"); + ASSERT_EQ(sub_key.GetFingerprint(), + "50D37E8F8EE7340A6794E0592B36803235B5E25B"); + ASSERT_EQ(sub_key.GetKeyLength(), 3072); + ASSERT_EQ(sub_key.GetPubkeyAlgo(), "RSA"); + ASSERT_FALSE(sub_key.IsHasCertificationCapability()); + ASSERT_FALSE(sub_key.IsHasAuthenticationCapability()); + ASSERT_FALSE(sub_key.IsHasSigningCapability()); + ASSERT_TRUE(sub_key.IsHasEncryptionCapability()); + ASSERT_EQ(key.GetExpireTime(), + boost::posix_time::from_iso_string("20230905T040000")); + + ASSERT_EQ( + sub_key.IsExpired(), + sub_key.GetExpireTime() < boost::posix_time::second_clock::local_time()); +} + +TEST_F(GpgCoreTest, GpgUIDTest) { + auto key = GpgKeyGetter::GetInstance(kGpgFrontendDefaultChannel) + .GetKey("9490795B78F8AFE9F93BD09281704859182661FB"); + auto uids = key.GetUIDs(); + ASSERT_EQ(uids->size(), 1); + auto& uid = uids->front(); + + ASSERT_EQ(uid.GetName(), "GpgFrontendTest"); + ASSERT_TRUE(uid.GetComment().empty()); + ASSERT_EQ(uid.GetEmail(), "[email protected]"); + ASSERT_EQ(uid.GetUID(), + "GpgFrontendTest <[email protected]>"); + ASSERT_FALSE(uid.GetInvalid()); + ASSERT_FALSE(uid.GetRevoked()); +} + +TEST_F(GpgCoreTest, GpgKeySignatureTest) { + auto key = GpgKeyGetter::GetInstance(kGpgFrontendDefaultChannel) + .GetKey("9490795B78F8AFE9F93BD09281704859182661FB"); + auto uids = key.GetUIDs(); + ASSERT_EQ(uids->size(), 1); + auto& uid = uids->front(); + + auto signatures = uid.GetSignatures(); + ASSERT_EQ(signatures->size(), 1); + auto& signature = signatures->front(); + + ASSERT_EQ(signature.GetName(), "GpgFrontendTest"); + ASSERT_TRUE(signature.GetComment().empty()); + ASSERT_EQ(signature.GetEmail(), "[email protected]"); + ASSERT_EQ(signature.GetKeyID(), "81704859182661FB"); + ASSERT_EQ(signature.GetPubkeyAlgo(), "RSA"); + + ASSERT_FALSE(signature.IsRevoked()); + ASSERT_FALSE(signature.IsInvalid()); + ASSERT_EQ(CheckGpgError(signature.GetStatus()), GPG_ERR_NO_ERROR); + ASSERT_EQ(signature.GetUID(), + "GpgFrontendTest <[email protected]>"); +} + +TEST_F(GpgCoreTest, GpgKeyGetterTest) { + auto key = GpgKeyGetter::GetInstance(kGpgFrontendDefaultChannel) + .GetKey("9490795B78F8AFE9F93BD09281704859182661FB"); + ASSERT_TRUE(key.IsGood()); + auto keys = GpgKeyGetter::GetInstance(kGpgFrontendDefaultChannel).FetchKey(); + ASSERT_TRUE(find(keys->begin(), keys->end(), key) != keys->end()); +} + +} // namespace GpgFrontend::Test
\ No newline at end of file diff --git a/src/test/core/GpgCoreTestKeygen.cpp b/src/test/core/GpgCoreTestKeygen.cpp new file mode 100644 index 00000000..62c8a771 --- /dev/null +++ b/src/test/core/GpgCoreTestKeygen.cpp @@ -0,0 +1,138 @@ +/** + * 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/function/gpg/GpgKeyGetter.h" +#include "core/function/gpg/GpgKeyOpera.h" +#include "core/function/result_analyse/GpgResultAnalyse.h" +#include "core/model/GpgGenKeyInfo.h" +#include "core/model/GpgKey.h" +#include "core/utils/GpgUtils.h" + +namespace GpgFrontend::Test { + +TEST_F(GpgCoreTest, GenerateKeyTest) { + auto& key_opera = GpgKeyOpera::GetInstance(kGpgFrontendDefaultChannel); + auto keygen_info = std::make_unique<GenKeyInfo>(); + keygen_info->SetName("foo"); + keygen_info->SetEmail("[email protected]"); + keygen_info->SetComment(""); + keygen_info->SetKeyLength(1024); + keygen_info->SetAlgo(keygen_info->GetSupportedKeyAlgo()[0]); + keygen_info->SetNonExpired(true); + keygen_info->SetNonPassPhrase(true); + + GpgGenKeyResult result = nullptr; + // auto err = CheckGpgError(key_opera.GenerateKey(keygen_info, result)); + // ASSERT_EQ(err, GPG_ERR_NO_ERROR); + + // auto* fpr = result->fpr; + // ASSERT_FALSE(fpr == nullptr); + + // auto key = + // GpgKeyGetter::GetInstance(kGpgFrontendDefaultChannel).GetKey(fpr); + // ASSERT_TRUE(key.IsGood()); + // key_opera.DeleteKey(fpr); +} + +TEST_F(GpgCoreTest, GenerateKeyTest_1) { + auto& key_opera = GpgKeyOpera::GetInstance(kGpgFrontendDefaultChannel); + auto keygen_info = std::make_unique<GenKeyInfo>(); + keygen_info->SetName("foo"); + keygen_info->SetEmail("[email protected]"); + keygen_info->SetComment("hello gpgfrontend"); + keygen_info->SetAlgo(keygen_info->GetSupportedKeyAlgo()[0]); + keygen_info->SetKeyLength(4096); + keygen_info->SetNonExpired(false); + keygen_info->SetExpireTime(boost::posix_time::second_clock::local_time() + + boost::posix_time::hours(24)); + keygen_info->SetNonPassPhrase(false); + + GpgGenKeyResult result = nullptr; + // auto err = + // CheckGpgError(key_opera.GenerateKey(keygen_info, result)); + // ASSERT_EQ(err, GPG_ERR_NO_ERROR); + + // auto fpr = result->fpr; + // ASSERT_FALSE(fpr == nullptr); + + // auto key = + // GpgKeyGetter::GetInstance(kGpgFrontendDefaultChannel).GetKey(fpr); + // ASSERT_TRUE(key.IsGood()); + // key_opera.DeleteKey(fpr); +} + +TEST_F(GpgCoreTest, GenerateKeyTest_4) { + auto& key_opera = GpgKeyOpera::GetInstance(kGpgFrontendDefaultChannel); + auto keygen_info = std::make_unique<GenKeyInfo>(); + keygen_info->SetName("foo"); + keygen_info->SetEmail("[email protected]"); + keygen_info->SetComment(""); + keygen_info->SetAlgo(keygen_info->GetSupportedKeyAlgo()[1]); + keygen_info->SetNonExpired(true); + keygen_info->SetNonPassPhrase(false); + + GpgGenKeyResult result = nullptr; + // auto err = + // CheckGpgError(key_opera.GenerateKey(keygen_info, result)); + // ASSERT_EQ(err, GPG_ERR_NO_ERROR); + + // auto* fpr = result->fpr; + // ASSERT_FALSE(fpr == nullptr); + + // auto key = + // GpgKeyGetter::GetInstance(kGpgFrontendDefaultChannel).GetKey(fpr); + // ASSERT_TRUE(key.IsGood()); + // key_opera.DeleteKey(fpr); +} + +TEST_F(GpgCoreTest, GenerateKeyTest_5) { + auto& key_opera = GpgKeyOpera::GetInstance(kGpgFrontendDefaultChannel); + auto keygen_info = std::make_unique<GenKeyInfo>(); + keygen_info->SetName("foo"); + keygen_info->SetEmail("[email protected]"); + keygen_info->SetComment(""); + keygen_info->SetAlgo(keygen_info->GetSupportedKeyAlgo()[2]); + keygen_info->SetNonExpired(true); + keygen_info->SetNonPassPhrase(false); + + GpgGenKeyResult result = nullptr; + // auto err = + // CheckGpgError(key_opera.GenerateKey(keygen_info, result)); + // ASSERT_EQ(err, GPG_ERR_NO_ERROR); + + // auto* fpr = result->fpr; + // ASSERT_FALSE(fpr == nullptr); + + // auto key = + // GpgKeyGetter::GetInstance(kGpgFrontendDefaultChannel).GetKey(fpr); + // ASSERT_TRUE(key.IsGood()); + // key_opera.DeleteKey(fpr); +} + +} // namespace GpgFrontend::Test diff --git a/src/ui/main_window/MainWindowFileSlotFunction.cpp b/src/ui/main_window/MainWindowFileSlotFunction.cpp index be0e8efe..e7a25396 100644 --- a/src/ui/main_window/MainWindowFileSlotFunction.cpp +++ b/src/ui/main_window/MainWindowFileSlotFunction.cpp @@ -183,10 +183,10 @@ void MainWindow::SlotFileEncrypt() { path = path + (file_info.isDir() ? ".tar" : ""); } - auto _channel = kGpgfrontendDefaultChannel; + auto _channel = kGpgFrontendDefaultChannel; auto _extension = ".asc"; if (non_ascii_when_export || file_info.isDir()) { - _channel = kGpgfrontendNonAsciiChannel; + _channel = kGpgFrontendNonAsciiChannel; _extension = ".gpg"; } @@ -391,10 +391,10 @@ void MainWindow::SlotFileSign() { GlobalSettingStation::GetInstance().LookupSettings( "general.non_ascii_when_export", true); - auto _channel = kGpgfrontendDefaultChannel; + auto _channel = kGpgFrontendDefaultChannel; auto _extension = ".asc"; if (non_ascii_when_export) { - _channel = kGpgfrontendNonAsciiChannel; + _channel = kGpgFrontendNonAsciiChannel; _extension = ".sig"; } @@ -464,9 +464,9 @@ void MainWindow::SlotFileVerify() { GlobalSettingStation::GetInstance().LookupSettings( "general.non_ascii_when_export", true); - auto _channel = kGpgfrontendDefaultChannel; + auto _channel = kGpgFrontendDefaultChannel; if (non_ascii_when_export) { - _channel = kGpgfrontendNonAsciiChannel; + _channel = kGpgFrontendNonAsciiChannel; } if (in_path.extension() == ".gpg") { @@ -579,10 +579,10 @@ void MainWindow::SlotFileEncryptSign() { path = path + (file_info.isDir() ? ".tar" : ""); } - auto channel = kGpgfrontendDefaultChannel; + auto channel = kGpgFrontendDefaultChannel; const auto* extension = ".asc"; if (non_ascii_when_export || file_info.isDir()) { - channel = kGpgfrontendNonAsciiChannel; + channel = kGpgFrontendNonAsciiChannel; extension = ".gpg"; } |