diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/CMakeLists.txt | 13 | ||||
-rw-r--r-- | test/GpgCoreTest.cpp | 28 | ||||
-rw-r--r-- | test/GpgCoreTestBasicOpera.cpp | 86 | ||||
-rw-r--r-- | test/GpgCoreTestImportExport.cpp | 2 | ||||
-rw-r--r-- | test/GpgCoreTestKeyModel.cpp | 21 | ||||
-rw-r--r-- | test/GpgCoreTestKeyModelAlone.cpp | 158 | ||||
-rw-r--r-- | test/GpgCoreTestKeygen.cpp | 128 | ||||
-rw-r--r-- | test/GpgCoreTestKeygenAlone.cpp | 156 | ||||
-rw-r--r-- | test/GpgFrontendTest.h | 99 | ||||
-rw-r--r-- | test/conf/core.cfg | 7 |
10 files changed, 650 insertions, 48 deletions
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 8a5859f4..cee1720b 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -11,10 +11,17 @@ add_executable( ${TEST_SOURCE} ) -if(GPG_CORE) +if (GPG_CORE) target_link_libraries(${AppName} gpg_core) -endif() +endif () -target_link_libraries(${AppName} gtest gtest_main) +if (GPG_STANDALONE_MODE) + file(COPY ${CMAKE_SOURCE_DIR}/test/gpg DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/ FOLLOW_SYMLINK_CHAIN) +endif () + +target_link_libraries(${AppName} ${Boost_LIBRARIES} gtest gtest_main) +if (APPLE) + target_link_libraries(${AppName} intl) +endif () add_test(AllTestsInGpgFrontend ${AppName}) diff --git a/test/GpgCoreTest.cpp b/test/GpgCoreTest.cpp new file mode 100644 index 00000000..ab9dbf28 --- /dev/null +++ b/test/GpgCoreTest.cpp @@ -0,0 +1,28 @@ +/** + * 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" + +// Should be used once and once-only +INITIALIZE_EASYLOGGINGPP diff --git a/test/GpgCoreTestBasicOpera.cpp b/test/GpgCoreTestBasicOpera.cpp index 40972982..283ceb82 100644 --- a/test/GpgCoreTestBasicOpera.cpp +++ b/test/GpgCoreTestBasicOpera.cpp @@ -31,18 +31,18 @@ #include "gpg/GpgConstants.h" #include "gpg/function/BasicOperator.h" #include "gpg/function/GpgKeyGetter.h" -#include "gpg/model/GpgKey.h" +#include "gpg/result_analyse/DecryptResultAnalyse.h" using namespace GpgFrontend; TEST_F(GpgCoreTest, CoreEncryptDecrTest) { - auto encrpyt_key = GpgKeyGetter::GetInstance(default_channel) + auto encrypt_key = GpgKeyGetter::GetInstance(default_channel) .GetPubkey("467F14220CE8DCF780CF4BAD8465C55B25C9B7D1"); ByteArray encrypt_text = "Hello GpgFrontend!"; ByteArrayPtr encr_out_data; GpgEncrResult e_result; - std::vector<GpgKey> keys; - keys.push_back(std::move(encrpyt_key)); + 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); @@ -59,14 +59,65 @@ TEST_F(GpgCoreTest, CoreEncryptDecrTest) { 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"); +} + +TEST_F(GpgCoreTest, CoreEncryptDecrTest_KeyNotFound_ResultAnalyse) { + 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"); + + DecryptResultAnalyse analyse{err, d_result}; + analyse.analyse(); + ASSERT_EQ(analyse.getStatus(), -1); + ASSERT_FALSE(analyse.getResultReport().empty()); +} + TEST_F(GpgCoreTest, CoreSignVerifyNormalTest) { - auto encrpyt_key = GpgKeyGetter::GetInstance(default_channel) + auto encrypt_key = GpgKeyGetter::GetInstance(default_channel) .GetPubkey("467F14220CE8DCF780CF4BAD8465C55B25C9B7D1"); ByteArray sign_text = "Hello GpgFrontend!"; ByteArrayPtr sign_out_data; GpgSignResult s_result; - std::vector<GpgKey> keys; - keys.push_back(std::move(encrpyt_key)); + KeyListPtr keys = std::make_unique<KeyArgsList>(); + keys->push_back(std::move(encrypt_key)); auto err = BasicOperator::GetInstance(default_channel) .Sign(std::move(keys), sign_text, sign_out_data, GPGME_SIG_MODE_NORMAL, s_result); @@ -85,13 +136,13 @@ TEST_F(GpgCoreTest, CoreSignVerifyNormalTest) { } TEST_F(GpgCoreTest, CoreSignVerifyDetachTest) { - auto encrpyt_key = GpgKeyGetter::GetInstance(default_channel) + auto encrypt_key = GpgKeyGetter::GetInstance(default_channel) .GetPubkey("467F14220CE8DCF780CF4BAD8465C55B25C9B7D1"); ByteArray sign_text = "Hello GpgFrontend!"; ByteArrayPtr sign_out_data; GpgSignResult s_result; - std::vector<GpgKey> keys; - keys.push_back(std::move(encrpyt_key)); + KeyListPtr keys = std::make_unique<KeyArgsList>(); + keys->push_back(std::move(encrypt_key)); auto err = BasicOperator::GetInstance(default_channel) .Sign(std::move(keys), sign_text, sign_out_data, GPGME_SIG_MODE_DETACH, s_result); @@ -114,8 +165,8 @@ TEST_F(GpgCoreTest, CoreSignVerifyClearTest) { ByteArray sign_text = "Hello GpgFrontend!"; ByteArrayPtr sign_out_data; GpgSignResult s_result; - std::vector<GpgKey> keys; - keys.push_back(std::move(sign_key)); + KeyListPtr keys = std::make_unique<KeyArgsList>(); + keys->push_back(std::move(sign_key)); auto err = BasicOperator::GetInstance(default_channel) .Sign(std::move(keys), sign_text, sign_out_data, GPGME_SIG_MODE_CLEAR, s_result); @@ -134,12 +185,12 @@ TEST_F(GpgCoreTest, CoreSignVerifyClearTest) { } TEST_F(GpgCoreTest, CoreEncryptSignDecrVerifyTest) { - auto encrpyt_key = GpgKeyGetter::GetInstance(default_channel) + auto encrypt_key = GpgKeyGetter::GetInstance(default_channel) .GetPubkey("467F14220CE8DCF780CF4BAD8465C55B25C9B7D1"); auto sign_key = GpgKeyGetter::GetInstance(default_channel) .GetKey("8933EB283A18995F45D61DAC021D89771B680FFB"); // Question? - // ASSERT_FALSE(encrpyt_key.is_private_key()); + // ASSERT_FALSE(encrypt_key.is_private_key()); ASSERT_TRUE(sign_key.is_private_key()); ASSERT_TRUE(sign_key.CanSignActual()); ByteArray encrypt_text = "Hello GpgFrontend!"; @@ -147,9 +198,10 @@ TEST_F(GpgCoreTest, CoreEncryptSignDecrVerifyTest) { GpgEncrResult e_result; GpgSignResult s_result; - std::vector<GpgKey> keys, sign_keys; - keys.push_back(std::move(encrpyt_key)); - sign_keys.push_back(std::move(sign_key)); + KeyListPtr keys = std::make_unique<KeyArgsList>(), + sign_keys = std::make_unique<KeyArgsList>(); + keys->push_back(std::move(encrypt_key)); + sign_keys->push_back(std::move(sign_key)); auto err = BasicOperator::GetInstance(default_channel) .EncryptSign(std::move(keys), std::move(sign_keys), diff --git a/test/GpgCoreTestImportExport.cpp b/test/GpgCoreTestImportExport.cpp index 9d01cc3e..1e226247 100644 --- a/test/GpgCoreTestImportExport.cpp +++ b/test/GpgCoreTestImportExport.cpp @@ -28,7 +28,7 @@ #include "GpgFrontendTest.h" #include "gpg/GpgConstants.h" #include "gpg/function/GpgKeyGetter.h" -#include "gpg/function/GpgKeyImportExportor.h" +#include "gpg/function/GpgKeyImportExporter.h" #include "gpg/model/GpgKey.h" TEST_F(GpgCoreTest, CoreExportSecretTest) {}
\ No newline at end of file diff --git a/test/GpgCoreTestKeyModel.cpp b/test/GpgCoreTestKeyModel.cpp index 4e85a8cf..79cd7dcd 100644 --- a/test/GpgCoreTestKeyModel.cpp +++ b/test/GpgCoreTestKeyModel.cpp @@ -25,15 +25,11 @@ #include "GpgFrontendTest.h" #include "gpg/function/GpgKeyGetter.h" -// Should be used once and once-only -INITIALIZE_EASYLOGGINGPP - TEST_F(GpgCoreTest, CoreInitTest) { auto& ctx = GpgFrontend::GpgContext::GetInstance(default_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) { @@ -75,18 +71,19 @@ TEST_F(GpgCoreTest, GpgKeyTest) { ASSERT_EQ(key.email(), "[email protected]"); ASSERT_EQ(key.id(), "81704859182661FB"); ASSERT_EQ(key.fpr(), "9490795B78F8AFE9F93BD09281704859182661FB"); - ASSERT_EQ(key.expires(), boost::gregorian::from_simple_string("2023-09-05")); + 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::gregorian::from_simple_string("1970-01-01")); + boost::posix_time::from_iso_string("19700101T000000")); ASSERT_EQ(key.create_time(), - boost::gregorian::from_simple_string("2021-09-05")); + 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().date()); + ASSERT_EQ(key.expired(), key.expires() < second_clock::local_time()); } TEST_F(GpgCoreTest, GpgSubKeyTest) { @@ -100,7 +97,7 @@ TEST_F(GpgCoreTest, GpgSubKeyTest) { ASSERT_FALSE(sub_key.revoked()); ASSERT_FALSE(sub_key.disabled()); ASSERT_EQ(sub_key.timestamp(), - boost::gregorian::from_simple_string("2021-09-05")); + boost::posix_time::from_iso_string("20210905T060153")); ASSERT_FALSE(sub_key.is_cardkey()); ASSERT_TRUE(sub_key.is_private_key()); @@ -112,11 +109,11 @@ TEST_F(GpgCoreTest, GpgSubKeyTest) { ASSERT_FALSE(sub_key.can_authenticate()); ASSERT_FALSE(sub_key.can_sign()); ASSERT_TRUE(sub_key.can_encrypt()); - ASSERT_EQ(key.expires(), boost::gregorian::from_simple_string("2023-09-05")); + 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().date()); + ASSERT_EQ(sub_key.expired(), sub_key.expires() < second_clock::local_time()); } TEST_F(GpgCoreTest, GpgUIDTest) { diff --git a/test/GpgCoreTestKeyModelAlone.cpp b/test/GpgCoreTestKeyModelAlone.cpp new file mode 100644 index 00000000..3d5bd175 --- /dev/null +++ b/test/GpgCoreTestKeyModelAlone.cpp @@ -0,0 +1,158 @@ +/** + * 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" + +TEST_F(GpgCoreTest, CoreInitTestAlone) { + auto& ctx = GpgFrontend::GpgContext::GetInstance(gpg_alone_channel); + ASSERT_TRUE(ctx.good()); +} + +TEST_F(GpgCoreTest, GpgDataTestAlone) { + 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, GpgKeyFetchTestAlone) { + auto keys = + GpgFrontend::GpgKeyGetter::GetInstance(gpg_alone_channel).FetchKey(); + ASSERT_EQ(keys->size(), 4); +} + +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, GpgKeySignatureTestAlone) { + auto key = GpgFrontend::GpgKeyGetter::GetInstance(gpg_alone_channel) + .GetKey("9490795B78F8AFE9F93BD09281704859182661FB"); + auto uids = key.uids(); + ASSERT_EQ(uids->size(), 1); + auto& uid = uids->front(); + + // No key signature support + auto signatures = uid.signatures(); + ASSERT_EQ(signatures->size(), 0); +} + +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/GpgCoreTestKeygen.cpp b/test/GpgCoreTestKeygen.cpp new file mode 100644 index 00000000..b703ee40 --- /dev/null +++ b/test/GpgCoreTestKeygen.cpp @@ -0,0 +1,128 @@ +/** + * 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/GpgGenKeyInfo.h" +#include "gpg/function/GpgKeyGetter.h" +#include "gpg/function/GpgKeyOpera.h" + +TEST_F(GpgCoreTest, GenerateKeyTest) { + auto& key_opera = GpgFrontend::GpgKeyOpera::GetInstance(default_channel); + auto keygen_info = std::make_unique<GpgFrontend::GenKeyInfo>(); + keygen_info->setName("foo"); + keygen_info->setEmail("[email protected]"); + keygen_info->setComment(""); + keygen_info->setKeySize(1024); + keygen_info->setAlgo("rsa"); + keygen_info->setNonExpired(true); + keygen_info->setNonPassPhrase(true); + + GpgFrontend::GpgGenKeyResult result = nullptr; + auto err = GpgFrontend::check_gpg_error_2_err_code( + key_opera.GenerateKey(keygen_info, result)); + ASSERT_EQ(err, GPG_ERR_NO_ERROR); + + auto fpr = result->fpr; + ASSERT_FALSE(fpr == nullptr); + + auto key = + GpgFrontend::GpgKeyGetter::GetInstance(default_channel).GetKey(fpr); + ASSERT_TRUE(key.good()); + key_opera.DeleteKey(fpr); +} + +TEST_F(GpgCoreTest, GenerateKeyTest_1) { + auto& key_opera = GpgFrontend::GpgKeyOpera::GetInstance(default_channel); + auto keygen_info = std::make_unique<GpgFrontend::GenKeyInfo>(); + keygen_info->setName("foo"); + keygen_info->setEmail("[email protected]"); + keygen_info->setComment("hello gpgfrontend"); + keygen_info->setAlgo("rsa"); + keygen_info->setKeySize(4096); + keygen_info->setNonExpired(false); + keygen_info->setExpired(boost::posix_time::second_clock::local_time() + + boost::posix_time::hours(24)); + keygen_info->setNonPassPhrase(false); + + GpgFrontend::GpgGenKeyResult result = nullptr; + auto err = GpgFrontend::check_gpg_error_2_err_code( + key_opera.GenerateKey(keygen_info, result)); + ASSERT_EQ(err, GPG_ERR_NO_ERROR); + + auto fpr = result->fpr; + ASSERT_FALSE(fpr == nullptr); + + auto key = + GpgFrontend::GpgKeyGetter::GetInstance(default_channel).GetKey(fpr); + ASSERT_TRUE(key.good()); + key_opera.DeleteKey(fpr); +} + +TEST_F(GpgCoreTest, GenerateKeyTest_4) { + auto& key_opera = GpgFrontend::GpgKeyOpera::GetInstance(default_channel); + auto keygen_info = std::make_unique<GpgFrontend::GenKeyInfo>(); + keygen_info->setName("foo"); + keygen_info->setEmail("[email protected]"); + keygen_info->setComment(""); + keygen_info->setAlgo("dsa"); + keygen_info->setNonExpired(true); + keygen_info->setNonPassPhrase(false); + + GpgFrontend::GpgGenKeyResult result = nullptr; + auto err = GpgFrontend::check_gpg_error_2_err_code( + key_opera.GenerateKey(keygen_info, result)); + ASSERT_EQ(err, GPG_ERR_NO_ERROR); + + auto fpr = result->fpr; + ASSERT_FALSE(fpr == nullptr); + + auto key = + GpgFrontend::GpgKeyGetter::GetInstance(default_channel).GetKey(fpr); + ASSERT_TRUE(key.good()); + key_opera.DeleteKey(fpr); +} + +TEST_F(GpgCoreTest, GenerateKeyTest_5) { + auto& key_opera = GpgFrontend::GpgKeyOpera::GetInstance(default_channel); + auto keygen_info = std::make_unique<GpgFrontend::GenKeyInfo>(); + keygen_info->setName("foo"); + keygen_info->setEmail("[email protected]"); + keygen_info->setComment(""); + keygen_info->setAlgo("ed25519"); + keygen_info->setNonExpired(true); + keygen_info->setNonPassPhrase(false); + + GpgFrontend::GpgGenKeyResult result = nullptr; + auto err = GpgFrontend::check_gpg_error_2_err_code( + key_opera.GenerateKey(keygen_info, result)); + ASSERT_EQ(err, GPG_ERR_NO_ERROR); + + auto fpr = result->fpr; + ASSERT_FALSE(fpr == nullptr); + + auto key = + GpgFrontend::GpgKeyGetter::GetInstance(default_channel).GetKey(fpr); + ASSERT_TRUE(key.good()); + key_opera.DeleteKey(fpr); +} diff --git a/test/GpgCoreTestKeygenAlone.cpp b/test/GpgCoreTestKeygenAlone.cpp new file mode 100644 index 00000000..4a725ba7 --- /dev/null +++ b/test/GpgCoreTestKeygenAlone.cpp @@ -0,0 +1,156 @@ +/** + * 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/GpgGenKeyInfo.h" +#include "gpg/function/GpgKeyGetter.h" +#include "gpg/function/GpgKeyOpera.h" + +TEST_F(GpgCoreTest, GenerateKeyTestAlone) { + auto& key_opera = GpgFrontend::GpgKeyOpera::GetInstance(gpg_alone_channel); + auto keygen_info = std::make_unique<GpgFrontend::GenKeyInfo>(false, true); + keygen_info->setName("foobar"); + keygen_info->setEmail("[email protected]"); + keygen_info->setComment("hello"); + keygen_info->setAlgo("rsa"); + keygen_info->setNonExpired(true); + keygen_info->setNonPassPhrase(true); + + GpgFrontend::GpgGenKeyResult result = nullptr; + auto err = GpgFrontend::check_gpg_error_2_err_code( + key_opera.GenerateKey(keygen_info, result)); + ASSERT_EQ(err, GPG_ERR_NO_ERROR); + + auto fpr = result->fpr; + ASSERT_FALSE(fpr == nullptr); + + auto key = + GpgFrontend::GpgKeyGetter::GetInstance(gpg_alone_channel).GetKey(fpr); + ASSERT_TRUE(key.good()); + key_opera.DeleteKey(fpr); +} + +TEST_F(GpgCoreTest, GenerateKeyTestAlone_1) { + auto& key_opera = GpgFrontend::GpgKeyOpera::GetInstance(gpg_alone_channel); + auto keygen_info = std::make_unique<GpgFrontend::GenKeyInfo>(false, true); + keygen_info->setName("foobar"); + keygen_info->setEmail("[email protected]"); + keygen_info->setComment("hello gpgfrontend"); + keygen_info->setAlgo("rsa"); + keygen_info->setNonExpired(false); + keygen_info->setPassPhrase("abcdefg"); + keygen_info->setExpired(boost::posix_time::second_clock::local_time() + + boost::posix_time::hours(24)); + keygen_info->setNonPassPhrase(false); + + GpgFrontend::GpgGenKeyResult result = nullptr; + auto err = GpgFrontend::check_gpg_error_2_err_code( + key_opera.GenerateKey(keygen_info, result)); + ASSERT_EQ(err, GPG_ERR_NO_ERROR); + + auto fpr = result->fpr; + ASSERT_FALSE(fpr == nullptr); + + auto key = + GpgFrontend::GpgKeyGetter::GetInstance(gpg_alone_channel).GetKey(fpr); + ASSERT_TRUE(key.good()); + key_opera.DeleteKey(fpr); +} + +TEST_F(GpgCoreTest, GenerateKeyTestAlone_2) { + auto& key_opera = GpgFrontend::GpgKeyOpera::GetInstance(gpg_alone_channel); + auto keygen_info = std::make_unique<GpgFrontend::GenKeyInfo>(false, true); + keygen_info->setName("foobar"); + keygen_info->setEmail("[email protected]"); + keygen_info->setComment("hi"); + keygen_info->setAlgo("rsa"); + keygen_info->setKeySize(3072); + keygen_info->setNonExpired(true); + keygen_info->setNonPassPhrase(false); + keygen_info->setPassPhrase("abcdefg"); + + GpgFrontend::GpgGenKeyResult result = nullptr; + auto err = GpgFrontend::check_gpg_error_2_err_code( + key_opera.GenerateKey(keygen_info, result)); + ASSERT_EQ(err, GPG_ERR_NO_ERROR); + + auto fpr = result->fpr; + ASSERT_FALSE(fpr == nullptr); + + auto key = + GpgFrontend::GpgKeyGetter::GetInstance(gpg_alone_channel).GetKey(fpr); + ASSERT_TRUE(key.good()); + key_opera.DeleteKey(fpr); +} + +TEST_F(GpgCoreTest, GenerateKeyTestAlone_3) { + auto& key_opera = GpgFrontend::GpgKeyOpera::GetInstance(gpg_alone_channel); + auto keygen_info = std::make_unique<GpgFrontend::GenKeyInfo>(false, true); + keygen_info->setName("foo"); + keygen_info->setEmail("[email protected]"); + keygen_info->setComment("hello"); + keygen_info->setAlgo("rsa"); + keygen_info->setKeySize(4096); + keygen_info->setNonExpired(true); + keygen_info->setNonPassPhrase(false); + keygen_info->setPassPhrase("abcdefg"); + + GpgFrontend::GpgGenKeyResult result = nullptr; + auto err = GpgFrontend::check_gpg_error_2_err_code( + key_opera.GenerateKey(keygen_info, result)); + ASSERT_EQ(err, GPG_ERR_NO_ERROR); + + auto fpr = result->fpr; + ASSERT_FALSE(fpr == nullptr); + + auto key = + GpgFrontend::GpgKeyGetter::GetInstance(gpg_alone_channel).GetKey(fpr); + ASSERT_TRUE(key.good()); + key_opera.DeleteKey(fpr); +} + +TEST_F(GpgCoreTest, GenerateKeyTestAlone_4) { + auto& key_opera = GpgFrontend::GpgKeyOpera::GetInstance(gpg_alone_channel); + auto keygen_info = std::make_unique<GpgFrontend::GenKeyInfo>(false, true); + keygen_info->setName("foobar"); + keygen_info->setEmail("[email protected]"); + keygen_info->setComment("hello"); + keygen_info->setAlgo("dsa"); + keygen_info->setNonExpired(true); + keygen_info->setNonPassPhrase(false); + keygen_info->setPassPhrase("abcdefg"); + + GpgFrontend::GpgGenKeyResult result = nullptr; + auto err = GpgFrontend::check_gpg_error_2_err_code( + key_opera.GenerateKey(keygen_info, result)); + ASSERT_EQ(err, GPG_ERR_NO_ERROR); + + auto fpr = result->fpr; + ASSERT_FALSE(fpr == nullptr); + + auto key = + GpgFrontend::GpgKeyGetter::GetInstance(gpg_alone_channel).GetKey(fpr); + ASSERT_TRUE(key.good()); + key_opera.DeleteKey(fpr); +}
\ No newline at end of file diff --git a/test/GpgFrontendTest.h b/test/GpgFrontendTest.h index 181c513e..84476106 100644 --- a/test/GpgFrontendTest.h +++ b/test/GpgFrontendTest.h @@ -29,16 +29,18 @@ #include <gpg-error.h> #include <gtest/gtest.h> -#include <boost/date_time/gregorian/parsers.hpp> +#include <boost/date_time.hpp> #include <boost/dll.hpp> #include <boost/filesystem/operations.hpp> #include <boost/filesystem/path.hpp> +#include <libconfig.h++> #include <memory> #include <string> #include <vector> #include "gpg/GpgConstants.h" -#include "gpg/function/GpgKeyImportExportor.h" +#include "gpg/function/GpgKeyImportExporter.h" +#include "gpg/function/GpgKeyOpera.h" class GpgCoreTest : public ::testing::Test { protected: @@ -55,13 +57,24 @@ 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; - virtual ~GpgCoreTest() = default; + ~GpgCoreTest() override = default; + + void SetUp() override { + el::Loggers::addFlag(el::LoggingFlag::AutoSpacing); + el::Configurations defaultConf; + defaultConf.setToDefault(); + el::Loggers::reconfigureLogger("default", defaultConf); + + defaultConf.setGlobally(el::ConfigurationType::Format, + "%datetime %level %func %msg"); + el::Loggers::reconfigureLogger("default", defaultConf); - virtual void SetUp() { using namespace libconfig; Config cfg; ASSERT_NO_THROW(cfg.readFile(config_path.c_str())); @@ -75,21 +88,42 @@ class GpgCoreTest : public ::testing::Test { configure_independent_database(root); + configure_alone_gpg(root); + dealing_private_keys(root); import_data(); + import_data_alone(); } - virtual void TearDown() {} + void TearDown() override { + auto key_ids = std::make_unique<GpgFrontend::KeyIdArgsList>(); + key_ids->push_back("81704859182661FB"); + key_ids->push_back("06F1C7E7240C94E8"); + key_ids->push_back("8465C55B25C9B7D1"); + key_ids->push_back("021D89771B680FFB"); + GpgFrontend::GpgKeyOpera::GetInstance(default_channel) + .DeleteKeys(std::move(key_ids)); + } private: void import_data() { - GpgFrontend::GpgContext::GetInstance(default_channel) - .SetPassphraseCb(GpgFrontend::GpgContext::test_passphrase_cb); + for (const auto& secret_key : secret_keys_) { + auto secret_key_copy = + std::make_unique<GpgFrontend::ByteArray>(*secret_key); + GpgFrontend::GpgKeyImportExporter::GetInstance(default_channel) + .ImportKey(std::move(secret_key_copy)); + } + } + + void import_data_alone() { for (auto& secret_key : secret_keys_) { - GpgFrontend::GpgKeyImportExportor::GetInstance(default_channel) - .ImportKey(std::move(secret_key)); + auto secret_key_copy = + std::make_unique<GpgFrontend::ByteArray>(*secret_key); + GpgFrontend::GpgKeyImportExporter::GetInstance(gpg_alone_channel) + .ImportKey(std::move(secret_key_copy)); } } + void dealing_private_keys(const libconfig::Setting& root) { if (root.exists("load_keys.private_keys")) { auto& private_keys = root.lookup("load_keys.private_keys"); @@ -106,21 +140,60 @@ 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("alone_gpg_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); + } else { + boost::filesystem::remove_all(db_path); + boost::filesystem::create_directory(db_path); + } + GpgFrontend::GpgContext::CreateInstance( + gpg_alone_channel, + [&]() -> std::unique_ptr<GpgFrontend::GpgContext> { + GpgFrontend::GpgContextInitArgs args; + args.gpg_alone = true; + args.independent_database = true; + args.db_path = db_path.string(); + args.gpg_path = gpg_path.string(); + args.test_mode = true; + return std::make_unique<GpgFrontend::GpgContext>(args); + }); + } + } + } + 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; if (!boost::filesystem::exists(db_path)) { boost::filesystem::create_directory(db_path); + } else { + boost::filesystem::remove_all(db_path); + boost::filesystem::create_directory(db_path); } + GpgFrontend::GpgContext::CreateInstance( - 1, - std::make_unique<GpgFrontend::GpgContext>(true, db_path.c_str())); + default_channel, [&]() -> std::unique_ptr<GpgFrontend::GpgContext> { + GpgFrontend::GpgContextInitArgs args; + args.test_mode = true; + return std::make_unique<GpgFrontend::GpgContext>(args); + }); } } } diff --git a/test/conf/core.cfg b/test/conf/core.cfg index 69395963..7a9b76d6 100644 --- a/test/conf/core.cfg +++ b/test/conf/core.cfg @@ -1,8 +1,11 @@ # 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/gpg1"; +alone_gpg_db_path = "alone_db"; +data_path = "data"; load_keys = { private_keys = ( |