diff options
author | Saturneric <[email protected]> | 2021-12-20 13:52:05 +0000 |
---|---|---|
committer | Saturneric <[email protected]> | 2021-12-20 13:52:05 +0000 |
commit | e35ba01111cf700b5369ed93f56539e89531a138 (patch) | |
tree | 78f473e856cd07fa3fccfc143344d2bc173d5214 | |
parent | Update Test Code & Added Standlone Support (diff) | |
download | GpgFrontend-e35ba01111cf700b5369ed93f56539e89531a138.tar.gz GpgFrontend-e35ba01111cf700b5369ed93f56539e89531a138.zip |
Continue to add Standalone Support.
-rw-r--r-- | CMakeLists.txt | 4 | ||||
-rw-r--r-- | src/CMakeLists.txt | 6 | ||||
-rw-r--r-- | src/main.cpp | 14 | ||||
-rw-r--r-- | src/ui/settings/GlobalSettingStation.h | 12 | ||||
-rw-r--r-- | src/ui/widgets/KeyList.cpp | 11 | ||||
-rw-r--r-- | test/GpgCoreTestKeyModelAlone.cpp | 170 | ||||
-rw-r--r-- | test/GpgFrontendTest.h | 32 | ||||
-rw-r--r-- | test/conf/core.cfg | 6 |
8 files changed, 247 insertions, 8 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index bc1ba3da..fa687fd9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -224,6 +224,10 @@ endif () message(STATUS "OS_PLATFORM ${OS_PLATFORM}") +if(GPG_STANDALONE_MODE) + add_compile_definitions(GPG_STANDALONE_MODE) +endif() + # Basic Envirnoment Configure set(BASIC_ENV_CONFIG 1) set(QT_MOC_CONFIG 1) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index fc7d8169..3810a6e8 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -119,14 +119,14 @@ if (APPLICATION_BUILD) file(COPY ${CMAKE_SOURCE_DIR}/resource/css DESTINATION ${RESOURCE_OUTPUT_DIRECTORY}/ FOLLOW_SYMLINK_CHAIN) file(COPY ${CMAKE_SOURCE_DIR}/resource/icons DESTINATION ${RESOURCE_OUTPUT_DIRECTORY}/ FOLLOW_SYMLINK_CHAIN) file(COPY ${CMAKE_SOURCE_DIR}/TRANSLATORS DESTINATION ${RESOURCE_OUTPUT_DIRECTORY} FOLLOW_SYMLINK_CHAIN) + if(GPG_STANDALONE_MODE) + file(COPY ${CMAKE_SOURCE_DIR}/resource/gpg1.4 DESTINATION ${RESOURCE_OUTPUT_DIRECTORY}/ FOLLOW_SYMLINK_CHAIN) + endif() endif () if (APPLICATION_BUILD) if (${CMAKE_BUILD_TYPE} STREQUAL "Release") if (APPLE) - if(APPLE_SANDBOX) - file(COPY ${CMAKE_SOURCE_DIR}/resource/gpg1.4 DESTINATION ${RESOURCE_OUTPUT_DIRECTORY}/ FOLLOW_SYMLINK_CHAIN) - endif() file(COPY ${CMAKE_SOURCE_DIR}/gpgfrontend.icns DESTINATION ${RESOURCE_OUTPUT_DIRECTORY}/ FOLLOW_SYMLINK_CHAIN) # Refresh App Bundle file(REMOVE ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${AppName}.app) diff --git a/src/main.cpp b/src/main.cpp index ed863f41..ae04d7db 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -28,6 +28,7 @@ #include "GpgFrontendBuildInfo.h" #include "gpg/GpgContext.h" +#include "gpg/GpgFunctionObject.h" #include "ui/MainWindow.h" #include "ui/function/CtxCheckThread.h" #include "ui/settings/GlobalSettingStation.h" @@ -88,6 +89,19 @@ int main(int argc, char* argv[]) { file.close(); #endif +#ifdef GPG_STANDALONE_MODE + LOG(INFO) << "GPG_STANDALONE_MODE Enabled"; + auto gpg_path = GpgFrontend::UI::GlobalSettingStation::GetInstance() + .GetStandaloneGpgBinDir(); + auto db_path = GpgFrontend::UI::GlobalSettingStation::GetInstance() + .GetStandaloneDatabaseDir(); + GpgFrontend::GpgContext::CreateInstance( + GpgFrontend::SingletonFunctionObject< + GpgFrontend::GpgContext>::GetDefaultChannel(), + std::make_unique<GpgFrontend::GpgContext>(true, db_path.string(), true, + gpg_path.string())); +#endif + auto* init_ctx_thread = new GpgFrontend::UI::CtxCheckThread(); QApplication::connect(init_ctx_thread, &QThread::finished, init_ctx_thread, diff --git a/src/ui/settings/GlobalSettingStation.h b/src/ui/settings/GlobalSettingStation.h index a7cbe569..a89bf32d 100644 --- a/src/ui/settings/GlobalSettingStation.h +++ b/src/ui/settings/GlobalSettingStation.h @@ -48,6 +48,18 @@ class GlobalSettingStation : public QObject { return app_log_path; } + [[nodiscard]] boost::filesystem::path GetStandaloneDatabaseDir() const { + auto db_path = app_configure_path / "db"; + if (!boost::filesystem::exists(db_path)) { + boost::filesystem::create_directory(db_path); + } + return db_path; + } + + [[nodiscard]] boost::filesystem::path GetStandaloneGpgBinDir() const { + return app_resource_path / "gpg1.4" / "gpg"; + } + [[nodiscard]] boost::filesystem::path GetLocaleDir() const { return app_locale_path; } diff --git a/src/ui/widgets/KeyList.cpp b/src/ui/widgets/KeyList.cpp index 0e4c5bba..464727dc 100644 --- a/src/ui/widgets/KeyList.cpp +++ b/src/ui/widgets/KeyList.cpp @@ -46,6 +46,17 @@ KeyList::KeyList(bool menu, QWidget* parent) } void KeyList::init() { +#ifdef GPG_STANDALONE_MODE + LOG(INFO) << "GPG_STANDALONE_MODE Enabled"; + auto gpg_path = GpgFrontend::UI::GlobalSettingStation::GetInstance() + .GetStandaloneGpgBinDir(); + auto db_path = GpgFrontend::UI::GlobalSettingStation::GetInstance() + .GetStandaloneDatabaseDir(); + GpgContext::CreateInstance( + _m_key_list_id, std::make_unique<GpgContext>(true, db_path.string(), true, + gpg_path.string())); +#endif + ui->setupUi(this); ui->menuWidget->setHidden(!menu_status); diff --git a/test/GpgCoreTestKeyModelAlone.cpp b/test/GpgCoreTestKeyModelAlone.cpp new file mode 100644 index 00000000..ecc7bcd2 --- /dev/null +++ b/test/GpgCoreTestKeyModelAlone.cpp @@ -0,0 +1,170 @@ +/** + * 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. + * + * Foobar 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 Foobar. If not, see <https://www.gnu.org/licenses/>. + * + * The initial version of the source code is inherited from gpg4usb-team. + * Their source code version also complies with GNU General Public License. + * + * The source code version of this software was modified and released + * by Saturneric<[email protected]> starting on May 12, 2021. + * + */ + +#include "GpgFrontendTest.h" +#include "gpg/function/GpgKeyGetter.h" + +// Should be used once and once-only +INITIALIZE_EASYLOGGINGPP + +TEST_F(GpgCoreTest, CoreInitTestAlone) { + auto& ctx = GpgFrontend::GpgContext::GetInstance(gpg_alone_channel); + auto& ctx_default = GpgFrontend::GpgContext::GetInstance(); + ASSERT_TRUE(ctx.good()); + ASSERT_TRUE(ctx_default.good()); + ASSERT_EQ(ctx_default.GetInfo().DatabasePath, "default"); +} + +TEST_F(GpgCoreTest, GpgDataTest) { + auto data_buff = std::string( + "cqEh8fyKWtmiXrW2zzlszJVGJrpXDDpzgP7ZELGxhfZYFi8rMrSVKDwrpFZBSWMG"); + + GpgFrontend::GpgData data(data_buff.data(), data_buff.size()); + + auto out_buffer = data.Read2Buffer(); + ASSERT_EQ(out_buffer->size(), 64); +} + +TEST_F(GpgCoreTest, GpgKeyTestAlone) { + auto key = GpgFrontend::GpgKeyGetter::GetInstance(gpg_alone_channel) + .GetKey("9490795B78F8AFE9F93BD09281704859182661FB"); + ASSERT_TRUE(key.good()); + ASSERT_TRUE(key.is_private_key()); + ASSERT_TRUE(key.has_master_key()); + + ASSERT_FALSE(key.disabled()); + ASSERT_FALSE(key.revoked()); + + ASSERT_EQ(key.protocol(), "OpenPGP"); + + ASSERT_EQ(key.subKeys()->size(), 2); + ASSERT_EQ(key.uids()->size(), 1); + + ASSERT_TRUE(key.can_certify()); + ASSERT_TRUE(key.can_encrypt()); + ASSERT_TRUE(key.can_sign()); + ASSERT_FALSE(key.can_authenticate()); + ASSERT_TRUE(key.CanEncrActual()); + ASSERT_TRUE(key.CanEncrActual()); + ASSERT_TRUE(key.CanSignActual()); + ASSERT_FALSE(key.CanAuthActual()); + + ASSERT_EQ(key.name(), "GpgFrontendTest"); + ASSERT_TRUE(key.comment().empty()); + ASSERT_EQ(key.email(), "[email protected]"); + ASSERT_EQ(key.id(), "81704859182661FB"); + ASSERT_EQ(key.fpr(), "9490795B78F8AFE9F93BD09281704859182661FB"); + ASSERT_EQ(key.expires(), + boost::posix_time::from_iso_string("20230905T040000")); + ASSERT_EQ(key.pubkey_algo(), "RSA"); + ASSERT_EQ(key.length(), 3072); + ASSERT_EQ(key.last_update(), + boost::posix_time::from_iso_string("19700101T000000")); + ASSERT_EQ(key.create_time(), + boost::posix_time::from_iso_string("20210905T060153")); + + ASSERT_EQ(key.owner_trust(), "Unknown"); + + using namespace boost::posix_time; + ASSERT_EQ(key.expired(), key.expires() < second_clock::local_time()); +} + +TEST_F(GpgCoreTest, GpgSubKeyTestAlone) { + auto key = GpgFrontend::GpgKeyGetter::GetInstance(gpg_alone_channel) + .GetKey("9490795B78F8AFE9F93BD09281704859182661FB"); + auto sub_keys = key.subKeys(); + ASSERT_EQ(sub_keys->size(), 2); + + auto& sub_key = sub_keys->back(); + + ASSERT_FALSE(sub_key.revoked()); + ASSERT_FALSE(sub_key.disabled()); + ASSERT_EQ(sub_key.timestamp(), + boost::posix_time::from_iso_string("20210905T060153")); + + ASSERT_FALSE(sub_key.is_cardkey()); + ASSERT_TRUE(sub_key.is_private_key()); + ASSERT_EQ(sub_key.id(), "2B36803235B5E25B"); + ASSERT_EQ(sub_key.fpr(), "50D37E8F8EE7340A6794E0592B36803235B5E25B"); + ASSERT_EQ(sub_key.length(), 3072); + ASSERT_EQ(sub_key.pubkey_algo(), "RSA"); + ASSERT_FALSE(sub_key.can_certify()); + ASSERT_FALSE(sub_key.can_authenticate()); + ASSERT_FALSE(sub_key.can_sign()); + ASSERT_TRUE(sub_key.can_encrypt()); + ASSERT_EQ(key.expires(), + boost::posix_time::from_iso_string("20230905T040000")); + + using namespace boost::posix_time; + ASSERT_EQ(sub_key.expired(), sub_key.expires() < second_clock::local_time()); +} + +TEST_F(GpgCoreTest, GpgUIDTestAlone) { + auto key = GpgFrontend::GpgKeyGetter::GetInstance(gpg_alone_channel) + .GetKey("9490795B78F8AFE9F93BD09281704859182661FB"); + auto uids = key.uids(); + ASSERT_EQ(uids->size(), 1); + auto& uid = uids->front(); + + ASSERT_EQ(uid.name(), "GpgFrontendTest"); + ASSERT_TRUE(uid.comment().empty()); + ASSERT_EQ(uid.email(), "[email protected]"); + ASSERT_EQ(uid.uid(), "GpgFrontendTest <[email protected]>"); + ASSERT_FALSE(uid.invalid()); + ASSERT_FALSE(uid.revoked()); +} + +TEST_F(GpgCoreTest, GpgKeySignatureTest) { + auto key = GpgFrontend::GpgKeyGetter::GetInstance(gpg_alone_channel) + .GetKey("9490795B78F8AFE9F93BD09281704859182661FB"); + auto uids = key.uids(); + ASSERT_EQ(uids->size(), 1); + auto& uid = uids->front(); + + auto signatures = uid.signatures(); + ASSERT_EQ(signatures->size(), 1); + auto& signature = signatures->front(); + + ASSERT_EQ(signature.name(), "GpgFrontendTest"); + ASSERT_TRUE(signature.comment().empty()); + ASSERT_EQ(signature.email(), "[email protected]"); + ASSERT_EQ(signature.keyid(), "81704859182661FB"); + ASSERT_EQ(signature.pubkey_algo(), "RSA"); + + ASSERT_FALSE(signature.revoked()); + ASSERT_FALSE(signature.invalid()); + ASSERT_EQ(GpgFrontend::check_gpg_error_2_err_code(signature.status()), + GPG_ERR_NO_ERROR); + ASSERT_EQ(signature.uid(), "GpgFrontendTest <[email protected]>"); +} + +TEST_F(GpgCoreTest, GpgKeyGetterTestAlone) { + auto key = GpgFrontend::GpgKeyGetter::GetInstance(gpg_alone_channel) + .GetKey("9490795B78F8AFE9F93BD09281704859182661FB"); + ASSERT_TRUE(key.good()); + auto keys = + GpgFrontend::GpgKeyGetter::GetInstance(gpg_alone_channel).FetchKey(); + ASSERT_GE(keys->size(), secret_keys_.size()); + ASSERT_TRUE(find(keys->begin(), keys->end(), key) != keys->end()); +} diff --git a/test/GpgFrontendTest.h b/test/GpgFrontendTest.h index ee872fc6..0770c786 100644 --- a/test/GpgFrontendTest.h +++ b/test/GpgFrontendTest.h @@ -56,7 +56,9 @@ class GpgCoreTest : public ::testing::Test { // Data File Directory Location boost::filesystem::path data_path; - int default_channel = 0; + const int default_channel = 0; + + const int gpg_alone_channel = 512; GpgCoreTest() = default; @@ -76,6 +78,8 @@ class GpgCoreTest : public ::testing::Test { configure_independent_database(root); + configure_alone_gpg(root); + dealing_private_keys(root); import_data(); } @@ -107,12 +111,34 @@ class GpgCoreTest : public ::testing::Test { } } + void configure_alone_gpg(const libconfig::Setting& root) { + bool alone_gpg = false; + if (root.exists("alone_gpg")) { + root.lookupValue("alone_gpg", alone_gpg); + if (alone_gpg && root.exists("alone_gpg")) { + std::string alone_gpg_path; + root.lookupValue("alone_gpg_path", alone_gpg_path); + auto gpg_path = parent_path / alone_gpg_path; + + std::string relative_db_path; + root.lookupValue("independent_db_path", relative_db_path); + auto db_path = parent_path / relative_db_path; + if (!boost::filesystem::exists(db_path)) { + boost::filesystem::create_directory(db_path); + } + GpgFrontend::GpgContext::CreateInstance( + gpg_alone_channel, + std::make_unique<GpgFrontend::GpgContext>(true, db_path.c_str(), + true, gpg_path.c_str())); + } + } + } + void configure_independent_database(const libconfig::Setting& root) { bool independent_database = false; if (root.exists("independent_database")) { root.lookupValue("independent_database", independent_database); if (independent_database && root.exists("independent_db_path")) { - default_channel = 1; std::string relative_db_path; root.lookupValue("independent_db_path", relative_db_path); auto db_path = parent_path / relative_db_path; @@ -120,7 +146,7 @@ class GpgCoreTest : public ::testing::Test { boost::filesystem::create_directory(db_path); } GpgFrontend::GpgContext::CreateInstance( - 1, + default_channel, std::make_unique<GpgFrontend::GpgContext>(true, db_path.c_str())); } } diff --git a/test/conf/core.cfg b/test/conf/core.cfg index 69395963..c49767ea 100644 --- a/test/conf/core.cfg +++ b/test/conf/core.cfg @@ -1,8 +1,10 @@ # core test configuration file version = "1.0"; independent_database = true; -independent_db_path = "db" -data_path = "data" +independent_db_path = "db"; +alone_gpg = true; +alone_gpg_path = "gpg/gpg"; +data_path = "data"; load_keys = { private_keys = ( |