Last test converted.
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/test/src/NoOwnIdentWritesOnDecryptTest.cc Fri Aug 30 13:51:15 2019 +0200
1.3 @@ -0,0 +1,251 @@
1.4 +// This file is under GNU General Public License 3.0
1.5 +// see LICENSE.txt
1.6 +
1.7 +#include <stdlib.h>
1.8 +#include <string>
1.9 +#include <cstring>
1.10 +
1.11 +#include "pEpEngine.h"
1.12 +#include "test_util.h"
1.13 +
1.14 +
1.15 +
1.16 +#include "Engine.h"
1.17 +
1.18 +#include <gtest/gtest.h>
1.19 +
1.20 +
1.21 +namespace {
1.22 +
1.23 + //The fixture for NoOwnIdentWritesOnDecryptTest
1.24 + class NoOwnIdentWritesOnDecryptTest : public ::testing::Test {
1.25 + public:
1.26 + Engine* engine;
1.27 + PEP_SESSION session;
1.28 + message* _to_decrypt;
1.29 + string test_path;
1.30 + std::vector<std::pair<std::string, std::string>> init_files;
1.31 +
1.32 + protected:
1.33 + // You can remove any or all of the following functions if its body
1.34 + // is empty.
1.35 + NoOwnIdentWritesOnDecryptTest() {
1.36 + // You can do set-up work for each test here.
1.37 + test_suite_name = ::testing::UnitTest::GetInstance()->current_test_info()->test_suite_name();
1.38 + test_name = ::testing::UnitTest::GetInstance()->current_test_info()->name();
1.39 + test_path = get_main_test_home_dir() + "/" + test_suite_name + "/" + test_name;
1.40 + _to_decrypt = NULL;
1.41 + }
1.42 +
1.43 + ~NoOwnIdentWritesOnDecryptTest() override {
1.44 + // You can do clean-up work that doesn't throw exceptions here.
1.45 + free_message(_to_decrypt);
1.46 + }
1.47 +
1.48 + // If the constructor and destructor are not enough for setting up
1.49 + // and cleaning up each test, you can define the following methods:
1.50 +
1.51 + void SetUp() override {
1.52 + // Code here will be called immediately after the constructor (right
1.53 + // before each test).
1.54 +
1.55 + // Leave this empty if there are no files to copy to the home directory path
1.56 + init_files = std::vector<std::pair<std::string, std::string>>();
1.57 +
1.58 + // Get a new test Engine.
1.59 + engine = new Engine(test_path);
1.60 + ASSERT_NE(engine, nullptr);
1.61 +
1.62 + // Ok, let's initialize test directories etc.
1.63 + engine->prep(NULL, NULL, init_files);
1.64 +
1.65 + // Ok, try to start this bugger.
1.66 + engine->start();
1.67 + ASSERT_NE(engine->session, nullptr);
1.68 + session = engine->session;
1.69 +
1.70 + // Engine is up. Keep on truckin'
1.71 + }
1.72 +
1.73 + void TearDown() override {
1.74 + // Code here will be called immediately after each test (right
1.75 + // before the destructor).
1.76 + engine->shut_down();
1.77 + delete engine;
1.78 + engine = NULL;
1.79 + session = NULL;
1.80 + }
1.81 +
1.82 + private:
1.83 + const char* test_suite_name;
1.84 + const char* test_name;
1.85 + // Objects declared here can be used by all tests in the NoOwnIdentWritesOnDecryptTest suite.
1.86 +
1.87 + };
1.88 +
1.89 +} // namespace
1.90 +
1.91 +
1.92 +TEST_F(NoOwnIdentWritesOnDecryptTest, check_no_own_ident_writes_on_decrypt) {
1.93 +
1.94 + // set _to_decrypt without polluting test keyrings
1.95 + message* msg = new_message(PEP_dir_outgoing);
1.96 + pEp_identity* sender = NULL;
1.97 + pEp_identity* me_recip = NULL;
1.98 + pEp_identity* other_recip = NULL;
1.99 +
1.100 + ASSERT_TRUE(slurp_and_import_key(session, "test_keys/pub/pep-test-alice-0x6FF00E97_pub.asc"));
1.101 + ASSERT_TRUE(slurp_and_import_key(session, "test_keys/priv/pep-test-alice-0x6FF00E97_priv.asc"));
1.102 + ASSERT_TRUE(slurp_and_import_key(session, "test_keys/pub/pep-test-bob-0xC9C2EE39_pub.asc"));
1.103 + ASSERT_TRUE(slurp_and_import_key(session, "test_keys/pub/pep-test-carol-0x42A85A42_pub.asc"));
1.104 +
1.105 + sender = new_identity("pep.test.alice@pep-project.org", NULL, PEP_OWN_USERID, "Alice");
1.106 + set_own_key(session, sender, "4ABE3AAF59AC32CFE4F86500A9411D176FF00E97");
1.107 + myself(session, sender);
1.108 +
1.109 + me_recip = new_identity("pep.test.bob@pep-project.org", NULL, "Bob_is_hot", "Hot Bob");
1.110 + other_recip = new_identity("pep-test-carol@pep-project.org", NULL, "Carol_loves_me", "Carol Loves Alice");
1.111 +
1.112 + identity_list* to_list = new_identity_list(other_recip);
1.113 + identity_list_add(to_list, me_recip);
1.114 +
1.115 + msg->from = sender;
1.116 + msg->to = to_list;
1.117 +
1.118 + msg->shortmsg = strdup("just a message");
1.119 + msg->longmsg = strdup("a really dumb message");
1.120 +
1.121 + message* enc_msg = NULL;
1.122 +
1.123 + PEP_STATUS status = encrypt_message(session, msg, NULL, &enc_msg, PEP_enc_PGP_MIME, 0);
1.124 + ASSERT_EQ(status , PEP_STATUS_OK);
1.125 + free_message(msg);
1.126 + enc_msg->dir = PEP_dir_incoming;
1.127 + _to_decrypt = enc_msg;
1.128 +
1.129 + engine->shut_down();
1.130 + delete engine;
1.131 +
1.132 + // New Engine, new test case:
1.133 + engine = new Engine(test_path);
1.134 + ASSERT_NE(engine, nullptr);
1.135 +
1.136 + // Ok, let's initialize test directories etc.
1.137 + engine->prep(NULL, NULL, init_files);
1.138 +
1.139 + // Ok, try to start this bugger.
1.140 + engine->start();
1.141 + ASSERT_NE(engine->session, nullptr);
1.142 + session = engine->session;
1.143 +
1.144 + // Next test: check_address_only_no_overwrite) {
1.145 + ASSERT_NE(_to_decrypt, nullptr);
1.146 + message* copy = message_dup(_to_decrypt);
1.147 +
1.148 + free_identity(copy->from);
1.149 +
1.150 + ASSERT_TRUE(slurp_and_import_key(session, "test_keys/pub/pep-test-alice-0x6FF00E97_pub.asc"));
1.151 + ASSERT_TRUE(slurp_and_import_key(session, "test_keys/pub/pep-test-bob-0xC9C2EE39_pub.asc"));
1.152 + ASSERT_TRUE(slurp_and_import_key(session, "test_keys/priv/pep-test-bob-0xC9C2EE39_priv.asc"));
1.153 + ASSERT_TRUE(slurp_and_import_key(session, "test_keys/pub/pep-test-carol-0x42A85A42_pub.asc"));
1.154 +
1.155 + const char* bob_name = "STOP MESSING WITH ME ALICE";
1.156 + const char* bob_fpr = "BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39";
1.157 + pEp_identity* me = new_identity("pep.test.bob@pep-project.org", NULL, PEP_OWN_USERID, bob_name);
1.158 + status = set_own_key(session, me, bob_fpr);
1.159 + ASSERT_EQ(status , PEP_STATUS_OK);
1.160 + status = myself(session, me);
1.161 + ASSERT_EQ(status , PEP_STATUS_OK);
1.162 + free_identity(me);
1.163 + me = NULL;
1.164 +
1.165 + copy->from = new_identity("pep.test.alice@pep-project.org", NULL, NULL, NULL);
1.166 + pEp_identity* bob_ident = copy->to->next->ident;
1.167 + free(bob_ident->fpr);
1.168 + free(bob_ident->user_id);
1.169 + bob_ident->fpr = NULL;
1.170 + bob_ident->user_id = NULL;
1.171 +
1.172 + // yes, I know the test keeps the "old" user_id for carol, but it's irrelevant here/
1.173 +
1.174 + message* dec_msg = NULL;
1.175 + stringlist_t* keylist = NULL;
1.176 + PEP_decrypt_flags_t flags = 0;
1.177 + PEP_rating rating = PEP_rating_undefined;
1.178 +
1.179 + status = decrypt_message(session, copy, &dec_msg, &keylist, &rating, &flags);
1.180 + ASSERT_EQ(status , PEP_STATUS_OK);
1.181 + ASSERT_STREQ(dec_msg->to->next->ident->username, "Hot Bob");
1.182 +
1.183 + // Make sure Alice calling Bob hot doesn't infiltrate his DB
1.184 + status = get_identity(session, "pep.test.bob@pep-project.org", PEP_OWN_USERID, &me);
1.185 + ASSERT_EQ(status , PEP_STATUS_OK);
1.186 + ASSERT_NE(me, nullptr);
1.187 + ASSERT_STREQ(me->username, bob_name);
1.188 + ASSERT_STREQ(me->fpr, bob_fpr);
1.189 + free_identity(me);
1.190 + free_message(dec_msg);
1.191 + free_message(copy);
1.192 + copy = NULL;
1.193 +
1.194 + engine->shut_down();
1.195 + delete engine;
1.196 +
1.197 + // New Engine, new test case:
1.198 + engine = new Engine(test_path);
1.199 + ASSERT_NE(engine, nullptr);
1.200 +
1.201 + // Ok, let's initialize test directories etc.
1.202 + engine->prep(NULL, NULL, init_files);
1.203 +
1.204 + // Ok, try to start this bugger.
1.205 + engine->start();
1.206 + ASSERT_NE(engine->session, nullptr);
1.207 + session = engine->session;
1.208 +
1.209 + // Next test case: check_full_info_no_overwrite) {
1.210 + ASSERT_NE(_to_decrypt, nullptr);
1.211 + copy = message_dup(_to_decrypt);
1.212 +
1.213 + free_identity(copy->from);
1.214 +
1.215 + ASSERT_TRUE(slurp_and_import_key(session, "test_keys/pub/pep-test-alice-0x6FF00E97_pub.asc"));
1.216 + ASSERT_TRUE(slurp_and_import_key(session, "test_keys/pub/pep-test-bob-0xC9C2EE39_pub.asc"));
1.217 + ASSERT_TRUE(slurp_and_import_key(session, "test_keys/priv/pep-test-bob-0xC9C2EE39_priv.asc"));
1.218 + ASSERT_TRUE(slurp_and_import_key(session, "test_keys/pub/pep-test-carol-0x42A85A42_pub.asc"));
1.219 +
1.220 + me = new_identity("pep.test.bob@pep-project.org", NULL, PEP_OWN_USERID, bob_name);
1.221 + status = set_own_key(session, me, bob_fpr);
1.222 + ASSERT_EQ(status , PEP_STATUS_OK);
1.223 + status = myself(session, me);
1.224 + ASSERT_EQ(status , PEP_STATUS_OK);
1.225 + free_identity(me);
1.226 + me = NULL;
1.227 +
1.228 + copy->from = new_identity("pep.test.alice@pep-project.org", NULL, NULL, NULL);
1.229 + bob_ident = copy->to->next->ident;
1.230 + free(bob_ident->user_id);
1.231 + bob_ident->user_id = strdup(PEP_OWN_USERID);
1.232 + bob_ident->me = true;
1.233 +
1.234 + // yes, I know the test keeps the "old" user_id for carol, but it's irrelevant here
1.235 + dec_msg = NULL;
1.236 + keylist = NULL;
1.237 + flags = 0;
1.238 + rating = PEP_rating_undefined;
1.239 +
1.240 + status = decrypt_message(session, copy, &dec_msg, &keylist, &rating, &flags);
1.241 + ASSERT_EQ(status , PEP_STATUS_OK);
1.242 + ASSERT_STREQ(dec_msg->to->next->ident->username, "Hot Bob");
1.243 +
1.244 + // Make sure Alice calling Bob hot doesn't infiltrate his DB
1.245 + status = get_identity(session, "pep.test.bob@pep-project.org", PEP_OWN_USERID, &me);
1.246 + ASSERT_EQ(status , PEP_STATUS_OK);
1.247 + ASSERT_NE(me, nullptr);
1.248 + ASSERT_STREQ(me->username, bob_name);
1.249 + ASSERT_STREQ(me->fpr, bob_fpr);
1.250 + free_identity(me);
1.251 + free_message(dec_msg);
1.252 +
1.253 + free_message(copy);
1.254 +}
2.1 --- a/test/src/NoOwnIdentWritesOnDecryptTests.cc.FIXME Fri Aug 30 10:23:31 2019 +0200
2.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
2.3 @@ -1,168 +0,0 @@
2.4 -// This file is under GNU General Public License 3.0
2.5 -// see LICENSE.txt
2.6 -
2.7 -#include <stdlib.h>
2.8 -#include <string>
2.9 -#include <cstring>
2.10 -#include <cpptest.h>
2.11 -
2.12 -#include "pEpEngine.h"
2.13 -#include "test_util.h"
2.14 -
2.15 -#include "EngineTestIndividualSuite.h"
2.16 -#include "NoOwnIdentWritesOnDecryptTests.h"
2.17 -
2.18 -using namespace std;
2.19 -
2.20 -NoOwnIdentWritesOnDecryptTests::NoOwnIdentWritesOnDecryptTests(string suitename, string test_home_dir) :
2.21 - EngineTestIndividualSuite::EngineTestIndividualSuite(suitename, test_home_dir) {
2.22 - _to_decrypt = NULL;
2.23 - add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("NoOwnIdentWritesOnDecryptTests::check_no_own_ident_writes_on_decrypt"),
2.24 - static_cast<Func>(&NoOwnIdentWritesOnDecryptTests::check_no_own_ident_writes_on_decrypt)));
2.25 - add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("NoOwnIdentWritesOnDecryptTests::check_address_only_no_overwrite"),
2.26 - static_cast<Func>(&NoOwnIdentWritesOnDecryptTests::check_address_only_no_overwrite)));
2.27 - add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("NoOwnIdentWritesOnDecryptTests::check_full_info_no_overwrite"),
2.28 - static_cast<Func>(&NoOwnIdentWritesOnDecryptTests::check_full_info_no_overwrite)));
2.29 -}
2.30 -
2.31 -NoOwnIdentWritesOnDecryptTests::~NoOwnIdentWritesOnDecryptTests() {
2.32 - free_message(_to_decrypt);
2.33 -}
2.34 -
2.35 -void NoOwnIdentWritesOnDecryptTests::check_no_own_ident_writes_on_decrypt() {
2.36 - // This is a weird case - it is NOT a test case, it's just abusing the environment to
2.37 - // set _to_decrypt without polluting test keyrings for later tests.
2.38 - message* msg = new_message(PEP_dir_outgoing);
2.39 - pEp_identity* sender = NULL;
2.40 - pEp_identity* me_recip = NULL;
2.41 - pEp_identity* other_recip = NULL;
2.42 -
2.43 - TEST_ASSERT(slurp_and_import_key(session, "test_keys/pub/pep-test-alice-0x6FF00E97_pub.asc"));
2.44 - TEST_ASSERT(slurp_and_import_key(session, "test_keys/priv/pep-test-alice-0x6FF00E97_priv.asc"));
2.45 - TEST_ASSERT(slurp_and_import_key(session, "test_keys/pub/pep-test-bob-0xC9C2EE39_pub.asc"));
2.46 - TEST_ASSERT(slurp_and_import_key(session, "test_keys/pub/pep-test-carol-0x42A85A42_pub.asc"));
2.47 -
2.48 - sender = new_identity("pep.test.alice@pep-project.org", NULL, PEP_OWN_USERID, "Alice");
2.49 - set_own_key(session, sender, "4ABE3AAF59AC32CFE4F86500A9411D176FF00E97");
2.50 - myself(session, sender);
2.51 -
2.52 - me_recip = new_identity("pep.test.bob@pep-project.org", NULL, "Bob_is_hot", "Hot Bob");
2.53 - other_recip = new_identity("pep-test-carol@pep-project.org", NULL, "Carol_loves_me", "Carol Loves Alice");
2.54 -
2.55 - identity_list* to_list = new_identity_list(other_recip);
2.56 - identity_list_add(to_list, me_recip);
2.57 -
2.58 - msg->from = sender;
2.59 - msg->to = to_list;
2.60 -
2.61 - msg->shortmsg = strdup("just a message");
2.62 - msg->longmsg = strdup("a really dumb message");
2.63 -
2.64 - message* enc_msg = NULL;
2.65 -
2.66 - PEP_STATUS status = encrypt_message(session, msg, NULL, &enc_msg, PEP_enc_PGP_MIME, 0);
2.67 - TEST_ASSERT(status == PEP_STATUS_OK);
2.68 - free_message(msg);
2.69 - enc_msg->dir = PEP_dir_incoming;
2.70 - _to_decrypt = enc_msg;
2.71 - TEST_ASSERT(true);
2.72 -}
2.73 -
2.74 -void NoOwnIdentWritesOnDecryptTests::check_address_only_no_overwrite() {
2.75 - TEST_ASSERT(_to_decrypt);
2.76 - message* copy = message_dup(_to_decrypt);
2.77 -
2.78 - free_identity(copy->from);
2.79 -
2.80 - TEST_ASSERT(slurp_and_import_key(session, "test_keys/pub/pep-test-alice-0x6FF00E97_pub.asc"));
2.81 - TEST_ASSERT(slurp_and_import_key(session, "test_keys/pub/pep-test-bob-0xC9C2EE39_pub.asc"));
2.82 - TEST_ASSERT(slurp_and_import_key(session, "test_keys/priv/pep-test-bob-0xC9C2EE39_priv.asc"));
2.83 - TEST_ASSERT(slurp_and_import_key(session, "test_keys/pub/pep-test-carol-0x42A85A42_pub.asc"));
2.84 -
2.85 - const char* bob_name = "STOP MESSING WITH ME ALICE";
2.86 - const char* bob_fpr = "BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39";
2.87 - pEp_identity* me = new_identity("pep.test.bob@pep-project.org", NULL, PEP_OWN_USERID, bob_name);
2.88 - PEP_STATUS status = set_own_key(session, me, bob_fpr);
2.89 - TEST_ASSERT(status == PEP_STATUS_OK);
2.90 - status = myself(session, me);
2.91 - TEST_ASSERT(status == PEP_STATUS_OK);
2.92 - free_identity(me);
2.93 - me = NULL;
2.94 -
2.95 - copy->from = new_identity("pep.test.alice@pep-project.org", NULL, NULL, NULL);
2.96 - pEp_identity* bob_ident = copy->to->next->ident;
2.97 - free(bob_ident->fpr);
2.98 - free(bob_ident->user_id);
2.99 - bob_ident->fpr = NULL;
2.100 - bob_ident->user_id = NULL;
2.101 -
2.102 - // yes, I know the test keeps the "old" user_id for carol, but it's irrelevant here/
2.103 -
2.104 - message* dec_msg = NULL;
2.105 - stringlist_t* keylist = NULL;
2.106 - PEP_decrypt_flags_t flags = 0;
2.107 - PEP_rating rating = PEP_rating_undefined;
2.108 -
2.109 - status = decrypt_message(session, copy, &dec_msg, &keylist, &rating, &flags);
2.110 - TEST_ASSERT(status == PEP_STATUS_OK);
2.111 - TEST_ASSERT(strcmp(dec_msg->to->next->ident->username, "Hot Bob") == 0);
2.112 -
2.113 - // Make sure Alice calling Bob hot doesn't infiltrate his DB
2.114 - status = get_identity(session, "pep.test.bob@pep-project.org", PEP_OWN_USERID, &me);
2.115 - TEST_ASSERT(status == PEP_STATUS_OK);
2.116 - TEST_ASSERT(me);
2.117 - TEST_ASSERT(strcmp(me->username, bob_name) == 0);
2.118 - TEST_ASSERT(strcmp(me->fpr, bob_fpr) == 0);
2.119 - free_identity(me);
2.120 - free_message(dec_msg);
2.121 - free_message(copy);
2.122 -}
2.123 -
2.124 -void NoOwnIdentWritesOnDecryptTests::check_full_info_no_overwrite() {
2.125 - TEST_ASSERT(_to_decrypt);
2.126 - message* copy = message_dup(_to_decrypt);
2.127 -
2.128 - free_identity(copy->from);
2.129 -
2.130 - TEST_ASSERT(slurp_and_import_key(session, "test_keys/pub/pep-test-alice-0x6FF00E97_pub.asc"));
2.131 - TEST_ASSERT(slurp_and_import_key(session, "test_keys/pub/pep-test-bob-0xC9C2EE39_pub.asc"));
2.132 - TEST_ASSERT(slurp_and_import_key(session, "test_keys/priv/pep-test-bob-0xC9C2EE39_priv.asc"));
2.133 - TEST_ASSERT(slurp_and_import_key(session, "test_keys/pub/pep-test-carol-0x42A85A42_pub.asc"));
2.134 -
2.135 - const char* bob_name = "STOP MESSING WITH ME ALICE";
2.136 - const char* bob_fpr = "BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39";
2.137 - pEp_identity* me = new_identity("pep.test.bob@pep-project.org", NULL, PEP_OWN_USERID, bob_name);
2.138 - PEP_STATUS status = set_own_key(session, me, bob_fpr);
2.139 - TEST_ASSERT(status == PEP_STATUS_OK);
2.140 - status = myself(session, me);
2.141 - TEST_ASSERT(status == PEP_STATUS_OK);
2.142 - free_identity(me);
2.143 - me = NULL;
2.144 -
2.145 - copy->from = new_identity("pep.test.alice@pep-project.org", NULL, NULL, NULL);
2.146 - pEp_identity* bob_ident = copy->to->next->ident;
2.147 - free(bob_ident->user_id);
2.148 - bob_ident->user_id = strdup(PEP_OWN_USERID);
2.149 - bob_ident->me = true;
2.150 -
2.151 - // yes, I know the test keeps the "old" user_id for carol, but it's irrelevant here
2.152 - message* dec_msg = NULL;
2.153 - stringlist_t* keylist = NULL;
2.154 - PEP_decrypt_flags_t flags = 0;
2.155 - PEP_rating rating = PEP_rating_undefined;
2.156 -
2.157 - status = decrypt_message(session, copy, &dec_msg, &keylist, &rating, &flags);
2.158 - TEST_ASSERT(status == PEP_STATUS_OK);
2.159 - TEST_ASSERT(strcmp(dec_msg->to->next->ident->username, "Hot Bob") == 0);
2.160 -
2.161 - // Make sure Alice calling Bob hot doesn't infiltrate his DB
2.162 - status = get_identity(session, "pep.test.bob@pep-project.org", PEP_OWN_USERID, &me);
2.163 - TEST_ASSERT(status == PEP_STATUS_OK);
2.164 - TEST_ASSERT(me);
2.165 - TEST_ASSERT(strcmp(me->username, bob_name) == 0);
2.166 - TEST_ASSERT(strcmp(me->fpr, bob_fpr) == 0);
2.167 - free_identity(me);
2.168 - free_message(dec_msg);
2.169 -
2.170 - free_message(copy);
2.171 -}
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
3.2 +++ b/test/src/SyncTest.cc Fri Aug 30 13:51:15 2019 +0200
3.3 @@ -0,0 +1,265 @@
3.4 +// This file is under GNU General Public License 3.0
3.5 +// see LICENSE.txt
3.6 +
3.7 +#include <stdlib.h>
3.8 +#include <string>
3.9 +#include <assert.h>
3.10 +#include <thread>
3.11 +
3.12 +#include "locked_queue.hh"
3.13 +#include "sync_api.h"
3.14 +#include "Sync_impl.h"
3.15 +
3.16 +#include "test_util.h"
3.17 +
3.18 +#include "pEpEngine.h"
3.19 +
3.20 +#include "pEp_internal.h"
3.21 +#include "KeySync_fsm.h"
3.22 +#include "sync_codec.h"
3.23 +
3.24 +#include "Engine.h"
3.25 +
3.26 +#include <gtest/gtest.h>
3.27 +
3.28 +class Sync_Adapter {
3.29 +public:
3.30 + utility::locked_queue< Sync_event_t * > q;
3.31 +
3.32 + void processing();
3.33 +
3.34 + static PEP_STATUS notifyHandshake(
3.35 + pEp_identity *me,
3.36 + pEp_identity *partner,
3.37 + sync_handshake_signal signal
3.38 + );
3.39 + static int inject_sync_event(SYNC_EVENT ev, void *management);
3.40 + static Sync_event_t *retrieve_next_sync_event(void *management, unsigned threshold);
3.41 + static PEP_STATUS messageToSend(struct _message *msg);
3.42 +
3.43 + static void sync_thread(PEP_SESSION session, Sync_Adapter *adapter);
3.44 +};
3.45 +
3.46 +
3.47 +void Sync_Adapter::processing()
3.48 +{
3.49 + output_stream << "waiting for processing\n";
3.50 + while (!q.empty()) {
3.51 + nanosleep((const struct timespec[]){{0, 100000000L}}, NULL);
3.52 + }
3.53 +}
3.54 +
3.55 +PEP_STATUS Sync_Adapter::notifyHandshake(
3.56 + pEp_identity *me,
3.57 + pEp_identity *partner,
3.58 + sync_handshake_signal signal
3.59 + )
3.60 +{
3.61 + return PEP_STATUS_OK;
3.62 +}
3.63 +
3.64 +int Sync_Adapter::inject_sync_event(SYNC_EVENT ev, void *management)
3.65 +{
3.66 + Sync_event_t *_ev = ev;
3.67 + switch (_ev->fsm) {
3.68 + case Sync_PR_keysync:
3.69 + output_stream << "injecting event " << KeySync_event_name(_ev->event) << "\n";
3.70 + break;
3.71 + default:
3.72 + output_stream << "unknown state machine: " << _ev->fsm << "\n";
3.73 + assert(0);
3.74 + }
3.75 + auto adapter = static_cast< Sync_Adapter *>(management);
3.76 + adapter->q.push_front(ev);
3.77 + return 0;
3.78 +}
3.79 +
3.80 +Sync_event_t *Sync_Adapter::retrieve_next_sync_event(void *management, unsigned threshold)
3.81 +{
3.82 + auto adapter = static_cast< Sync_Adapter *>(management);
3.83 + time_t started = time(nullptr);
3.84 + bool timeout = false;
3.85 +
3.86 + while (adapter->q.empty()) {
3.87 + int i = 0;
3.88 + ++i;
3.89 + if (i > 10) {
3.90 + if (time(nullptr) > started + threshold) {
3.91 + timeout = true;
3.92 + break;
3.93 + }
3.94 + i = 0;
3.95 + }
3.96 + nanosleep((const struct timespec[]){{0, 100000000L}}, NULL);
3.97 + }
3.98 +
3.99 + if (timeout)
3.100 + return SYNC_TIMEOUT_EVENT;
3.101 +
3.102 + Sync_event_t *ev = adapter->q.pop_front();
3.103 + if (ev) {
3.104 + switch (ev->fsm) {
3.105 + case Sync_PR_keysync:
3.106 + output_stream << "sync thread: retrieving event " << KeySync_event_name(ev->event) << "\n";
3.107 + break;
3.108 + default:
3.109 + output_stream << "sync thread: unknown state machine: " << ev->fsm << "\n";
3.110 + assert(0);
3.111 + }
3.112 + }
3.113 + else {
3.114 + output_stream << "sync thread: retrieving shutdown\n";
3.115 + }
3.116 +
3.117 + return ev;
3.118 +}
3.119 +
3.120 +PEP_STATUS Sync_Adapter::messageToSend(struct _message *msg)
3.121 +{
3.122 + assert(msg && msg->attachments);
3.123 +
3.124 + output_stream << "sending message:\n";
3.125 +
3.126 + for (bloblist_t *b = msg->attachments; b && b->value; b = b->next) {
3.127 + if (b->mime_type && strcasecmp(b->mime_type, "application/pEp.sync") == 0) {
3.128 + assert(msg->from && msg->from->address && msg->from->username);
3.129 + output_stream << "<!-- " << msg->from->username << " <" << msg->from->address << "> -->\n";
3.130 + char *text = NULL;
3.131 + PEP_STATUS status = PER_to_XER_Sync_msg(msg->attachments->value, msg->attachments->size, &text);
3.132 + assert(status == PEP_STATUS_OK);
3.133 + output_stream << text << "\n";
3.134 + free(text);
3.135 + }
3.136 + }
3.137 +
3.138 + free_message(msg);
3.139 + return PEP_STATUS_OK;
3.140 +}
3.141 +
3.142 +void Sync_Adapter::sync_thread(PEP_SESSION session, Sync_Adapter *adapter)
3.143 +{
3.144 + output_stream << "sync_thread: startup\n";
3.145 + do_sync_protocol(session, adapter);
3.146 + output_stream << "sync_thread: shutdown\n";
3.147 +}
3.148 +
3.149 +
3.150 +namespace {
3.151 +
3.152 + //The fixture for SyncTest
3.153 + class SyncTest : public ::testing::Test {
3.154 + public:
3.155 + Engine* engine;
3.156 + PEP_SESSION session;
3.157 +
3.158 + Sync_Adapter adapter;
3.159 + PEP_SESSION sync = NULL;
3.160 + thread *sync_thread;
3.161 +
3.162 +
3.163 + protected:
3.164 + // You can remove any or all of the following functions if its body
3.165 + // is empty.
3.166 + SyncTest() {
3.167 + // You can do set-up work for each test here.
3.168 + test_suite_name = ::testing::UnitTest::GetInstance()->current_test_info()->test_suite_name();
3.169 + test_name = ::testing::UnitTest::GetInstance()->current_test_info()->name();
3.170 + test_path = get_main_test_home_dir() + "/" + test_suite_name + "/" + test_name;
3.171 + }
3.172 +
3.173 + ~SyncTest() override {
3.174 + // You can do clean-up work that doesn't throw exceptions here.
3.175 + }
3.176 +
3.177 + // If the constructor and destructor are not enough for setting up
3.178 + // and cleaning up each test, you can define the following methods:
3.179 +
3.180 + void SetUp() override {
3.181 + // Code here will be called immediately after the constructor (right
3.182 + // before each test).
3.183 +
3.184 + // Leave this empty if there are no files to copy to the home directory path
3.185 + std::vector<std::pair<std::string, std::string>> init_files = std::vector<std::pair<std::string, std::string>>();
3.186 +
3.187 + // Get a new test Engine.
3.188 + engine = new Engine(test_path);
3.189 + ASSERT_NE(engine, nullptr);
3.190 +
3.191 + // Ok, let's initialize test directories etc.
3.192 + engine->prep(NULL, NULL, init_files);
3.193 +
3.194 + // Ok, try to start this bugger. Is this totally irrelevant for this case??
3.195 + engine->start();
3.196 + ASSERT_NE(engine->session, nullptr);
3.197 + session = engine->session;
3.198 +
3.199 + // Engine is up. Keep on truckin'
3.200 +
3.201 + pEp_identity *self = new_identity("alice@synctests.pEp", nullptr, "23", "Alice Miller");
3.202 + assert(self);
3.203 + output_stream << "setting own identity for " << self->address << "\n";
3.204 + PEP_STATUS status = myself(session, self);
3.205 + assert(self->me);
3.206 + assert(self->fpr);
3.207 + output_stream << "fpr: " << self->fpr << "\n";
3.208 + free_identity(self);
3.209 +
3.210 + status = init(&sync, Sync_Adapter::messageToSend, Sync_Adapter::inject_sync_event);
3.211 + if (status != PEP_STATUS_OK)
3.212 + throw std::runtime_error((string("init returned ") + tl_status_string(status)).c_str());
3.213 +
3.214 + output_stream << "initialize sync and start first state machine\n";
3.215 + status = register_sync_callbacks(
3.216 + sync,
3.217 + (void *) &adapter.q,
3.218 + Sync_Adapter::notifyHandshake,
3.219 + Sync_Adapter::retrieve_next_sync_event
3.220 + );
3.221 + if (status != PEP_STATUS_OK)
3.222 + throw std::runtime_error((string("register sync status returned ") + tl_status_string(status)).c_str());
3.223 + if (sync->sync_state.keysync.state != Sole)
3.224 + throw std::runtime_error((string("keysync.state was supposed to be ") + to_string((int)Sole) + " but was " + to_string((int)(sync->sync_state.keysync.state))).c_str());
3.225 +
3.226 + output_stream << "creating thread for sync\n";
3.227 + sync_thread = new thread(Sync_Adapter::sync_thread, sync, &adapter);
3.228 +
3.229 + }
3.230 +
3.231 + void TearDown() override {
3.232 + // Code here will be called immediately after each test (right
3.233 + // before the destructor).
3.234 +
3.235 + adapter.processing();
3.236 +
3.237 + output_stream << "sending shutdown to sync thread\n";
3.238 + adapter.q.push_front(nullptr);
3.239 + sync_thread->join();
3.240 +
3.241 + unregister_sync_callbacks(sync);
3.242 + release(sync);
3.243 +
3.244 + engine->shut_down();
3.245 + delete engine;
3.246 + engine = NULL;
3.247 + session = NULL;
3.248 + }
3.249 +
3.250 + private:
3.251 + const char* test_suite_name;
3.252 + const char* test_name;
3.253 + string test_path;
3.254 + // Objects declared here can be used by all tests in the SyncTest suite.
3.255 +
3.256 + };
3.257 +
3.258 +} // namespace
3.259 +
3.260 +TEST_F(SyncTest, check_sync)
3.261 +{
3.262 + output_stream << "check_sync(): trigger KeyGen event\n";
3.263 + signal_Sync_event(sync, Sync_PR_keysync, KeyGen, NULL);
3.264 + adapter.processing();
3.265 +
3.266 + output_stream << "check_sync(): cry for unknown key\n";
3.267 + signal_Sync_event(sync, Sync_PR_keysync, CannotDecrypt, NULL);
3.268 +}
4.1 --- a/test/src/SyncTests.cc Fri Aug 30 10:23:31 2019 +0200
4.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
4.3 @@ -1,265 +0,0 @@
4.4 -// This file is under GNU General Public License 3.0
4.5 -// see LICENSE.txt
4.6 -
4.7 -#include <stdlib.h>
4.8 -#include <string>
4.9 -#include <assert.h>
4.10 -#include <thread>
4.11 -
4.12 -#include "locked_queue.hh"
4.13 -#include "sync_api.h"
4.14 -#include "Sync_impl.h"
4.15 -
4.16 -#include "test_util.h"
4.17 -
4.18 -#include "pEpEngine.h"
4.19 -
4.20 -#include "pEp_internal.h"
4.21 -#include "KeySync_fsm.h"
4.22 -#include "sync_codec.h"
4.23 -
4.24 -#include "Engine.h"
4.25 -
4.26 -#include <gtest/gtest.h>
4.27 -
4.28 -class Sync_Adapter {
4.29 -public:
4.30 - utility::locked_queue< Sync_event_t * > q;
4.31 -
4.32 - void processing();
4.33 -
4.34 - static PEP_STATUS notifyHandshake(
4.35 - pEp_identity *me,
4.36 - pEp_identity *partner,
4.37 - sync_handshake_signal signal
4.38 - );
4.39 - static int inject_sync_event(SYNC_EVENT ev, void *management);
4.40 - static Sync_event_t *retrieve_next_sync_event(void *management, unsigned threshold);
4.41 - static PEP_STATUS messageToSend(struct _message *msg);
4.42 -
4.43 - static void sync_thread(PEP_SESSION session, Sync_Adapter *adapter);
4.44 -};
4.45 -
4.46 -
4.47 -void Sync_Adapter::processing()
4.48 -{
4.49 - output_stream << "waiting for processing\n";
4.50 - while (!q.empty()) {
4.51 - nanosleep((const struct timespec[]){{0, 100000000L}}, NULL);
4.52 - }
4.53 -}
4.54 -
4.55 -PEP_STATUS Sync_Adapter::notifyHandshake(
4.56 - pEp_identity *me,
4.57 - pEp_identity *partner,
4.58 - sync_handshake_signal signal
4.59 - )
4.60 -{
4.61 - return PEP_STATUS_OK;
4.62 -}
4.63 -
4.64 -int Sync_Adapter::inject_sync_event(SYNC_EVENT ev, void *management)
4.65 -{
4.66 - Sync_event_t *_ev = ev;
4.67 - switch (_ev->fsm) {
4.68 - case Sync_PR_keysync:
4.69 - output_stream << "injecting event " << KeySync_event_name(_ev->event) << "\n";
4.70 - break;
4.71 - default:
4.72 - output_stream << "unknown state machine: " << _ev->fsm << "\n";
4.73 - assert(0);
4.74 - }
4.75 - auto adapter = static_cast< Sync_Adapter *>(management);
4.76 - adapter->q.push_front(ev);
4.77 - return 0;
4.78 -}
4.79 -
4.80 -Sync_event_t *Sync_Adapter::retrieve_next_sync_event(void *management, unsigned threshold)
4.81 -{
4.82 - auto adapter = static_cast< Sync_Adapter *>(management);
4.83 - time_t started = time(nullptr);
4.84 - bool timeout = false;
4.85 -
4.86 - while (adapter->q.empty()) {
4.87 - int i = 0;
4.88 - ++i;
4.89 - if (i > 10) {
4.90 - if (time(nullptr) > started + threshold) {
4.91 - timeout = true;
4.92 - break;
4.93 - }
4.94 - i = 0;
4.95 - }
4.96 - nanosleep((const struct timespec[]){{0, 100000000L}}, NULL);
4.97 - }
4.98 -
4.99 - if (timeout)
4.100 - return SYNC_TIMEOUT_EVENT;
4.101 -
4.102 - Sync_event_t *ev = adapter->q.pop_front();
4.103 - if (ev) {
4.104 - switch (ev->fsm) {
4.105 - case Sync_PR_keysync:
4.106 - output_stream << "sync thread: retrieving event " << KeySync_event_name(ev->event) << "\n";
4.107 - break;
4.108 - default:
4.109 - output_stream << "sync thread: unknown state machine: " << ev->fsm << "\n";
4.110 - assert(0);
4.111 - }
4.112 - }
4.113 - else {
4.114 - output_stream << "sync thread: retrieving shutdown\n";
4.115 - }
4.116 -
4.117 - return ev;
4.118 -}
4.119 -
4.120 -PEP_STATUS Sync_Adapter::messageToSend(struct _message *msg)
4.121 -{
4.122 - assert(msg && msg->attachments);
4.123 -
4.124 - output_stream << "sending message:\n";
4.125 -
4.126 - for (bloblist_t *b = msg->attachments; b && b->value; b = b->next) {
4.127 - if (b->mime_type && strcasecmp(b->mime_type, "application/pEp.sync") == 0) {
4.128 - assert(msg->from && msg->from->address && msg->from->username);
4.129 - output_stream << "<!-- " << msg->from->username << " <" << msg->from->address << "> -->\n";
4.130 - char *text = NULL;
4.131 - PEP_STATUS status = PER_to_XER_Sync_msg(msg->attachments->value, msg->attachments->size, &text);
4.132 - assert(status == PEP_STATUS_OK);
4.133 - output_stream << text << "\n";
4.134 - free(text);
4.135 - }
4.136 - }
4.137 -
4.138 - free_message(msg);
4.139 - return PEP_STATUS_OK;
4.140 -}
4.141 -
4.142 -void Sync_Adapter::sync_thread(PEP_SESSION session, Sync_Adapter *adapter)
4.143 -{
4.144 - output_stream << "sync_thread: startup\n";
4.145 - do_sync_protocol(session, adapter);
4.146 - output_stream << "sync_thread: shutdown\n";
4.147 -}
4.148 -
4.149 -
4.150 -namespace {
4.151 -
4.152 - //The fixture for SyncTest
4.153 - class SyncTest : public ::testing::Test {
4.154 - public:
4.155 - Engine* engine;
4.156 - PEP_SESSION session;
4.157 -
4.158 - Sync_Adapter adapter;
4.159 - PEP_SESSION sync = NULL;
4.160 - thread *sync_thread;
4.161 -
4.162 -
4.163 - protected:
4.164 - // You can remove any or all of the following functions if its body
4.165 - // is empty.
4.166 - SyncTest() {
4.167 - // You can do set-up work for each test here.
4.168 - test_suite_name = ::testing::UnitTest::GetInstance()->current_test_info()->test_suite_name();
4.169 - test_name = ::testing::UnitTest::GetInstance()->current_test_info()->name();
4.170 - test_path = get_main_test_home_dir() + "/" + test_suite_name + "/" + test_name;
4.171 - }
4.172 -
4.173 - ~SyncTest() override {
4.174 - // You can do clean-up work that doesn't throw exceptions here.
4.175 - }
4.176 -
4.177 - // If the constructor and destructor are not enough for setting up
4.178 - // and cleaning up each test, you can define the following methods:
4.179 -
4.180 - void SetUp() override {
4.181 - // Code here will be called immediately after the constructor (right
4.182 - // before each test).
4.183 -
4.184 - // Leave this empty if there are no files to copy to the home directory path
4.185 - std::vector<std::pair<std::string, std::string>> init_files = std::vector<std::pair<std::string, std::string>>();
4.186 -
4.187 - // Get a new test Engine.
4.188 - engine = new Engine(test_path);
4.189 - ASSERT_NE(engine, nullptr);
4.190 -
4.191 - // Ok, let's initialize test directories etc.
4.192 - engine->prep(NULL, NULL, init_files);
4.193 -
4.194 - // Ok, try to start this bugger. Is this totally irrelevant for this case??
4.195 - engine->start();
4.196 - ASSERT_NE(engine->session, nullptr);
4.197 - session = engine->session;
4.198 -
4.199 - // Engine is up. Keep on truckin'
4.200 -
4.201 - pEp_identity *self = new_identity("alice@synctests.pEp", nullptr, "23", "Alice Miller");
4.202 - assert(self);
4.203 - output_stream << "setting own identity for " << self->address << "\n";
4.204 - PEP_STATUS status = myself(session, self);
4.205 - assert(self->me);
4.206 - assert(self->fpr);
4.207 - output_stream << "fpr: " << self->fpr << "\n";
4.208 - free_identity(self);
4.209 -
4.210 - status = init(&sync, Sync_Adapter::messageToSend, Sync_Adapter::inject_sync_event);
4.211 - if (status != PEP_STATUS_OK)
4.212 - throw std::runtime_error((string("init returned ") + tl_status_string(status)).c_str());
4.213 -
4.214 - output_stream << "initialize sync and start first state machine\n";
4.215 - status = register_sync_callbacks(
4.216 - sync,
4.217 - (void *) &adapter.q,
4.218 - Sync_Adapter::notifyHandshake,
4.219 - Sync_Adapter::retrieve_next_sync_event
4.220 - );
4.221 - if (status != PEP_STATUS_OK)
4.222 - throw std::runtime_error((string("register sync status returned ") + tl_status_string(status)).c_str());
4.223 - if (sync->sync_state.keysync.state != Sole)
4.224 - throw std::runtime_error((string("keysync.state was supposed to be ") + to_string((int)Sole) + " but was " + to_string((int)(sync->sync_state.keysync.state))).c_str());
4.225 -
4.226 - output_stream << "creating thread for sync\n";
4.227 - sync_thread = new thread(Sync_Adapter::sync_thread, sync, &adapter);
4.228 -
4.229 - }
4.230 -
4.231 - void TearDown() override {
4.232 - // Code here will be called immediately after each test (right
4.233 - // before the destructor).
4.234 -
4.235 - adapter.processing();
4.236 -
4.237 - output_stream << "sending shutdown to sync thread\n";
4.238 - adapter.q.push_front(nullptr);
4.239 - sync_thread->join();
4.240 -
4.241 - unregister_sync_callbacks(sync);
4.242 - release(sync);
4.243 -
4.244 - engine->shut_down();
4.245 - delete engine;
4.246 - engine = NULL;
4.247 - session = NULL;
4.248 - }
4.249 -
4.250 - private:
4.251 - const char* test_suite_name;
4.252 - const char* test_name;
4.253 - string test_path;
4.254 - // Objects declared here can be used by all tests in the SyncTest suite.
4.255 -
4.256 - };
4.257 -
4.258 -} // namespace
4.259 -
4.260 -TEST_F(SyncTest, check_sync)
4.261 -{
4.262 - output_stream << "check_sync(): trigger KeyGen event\n";
4.263 - signal_Sync_event(sync, Sync_PR_keysync, KeyGen, NULL);
4.264 - adapter.processing();
4.265 -
4.266 - output_stream << "check_sync(): cry for unknown key\n";
4.267 - signal_Sync_event(sync, Sync_PR_keysync, CannotDecrypt, NULL);
4.268 -}