Merge sync
authorThomas
Fri, 30 Aug 2019 17:15:07 +0200
branchsync
changeset 4037fbc2e35b0ad2
parent 4036 9ecb9fbb5f6b
parent 4034 2fd5754a92c9
child 4038 313627399cd4
child 4041 276b75a00083
Merge
test/src/NoOwnIdentWritesOnDecryptTests.cc.FIXME
test/src/SyncTests.cc
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/gen_test_skel.py	Fri Aug 30 17:15:07 2019 +0200
     1.3 @@ -0,0 +1,93 @@
     1.4 +import argparse
     1.5 +import os
     1.6 +import subprocess
     1.7 +import re
     1.8 +
     1.9 +def decamel(name):
    1.10 +    retval = re.sub('([A-Z])', r'_\1', name).lower()
    1.11 +    return re.sub('^_', r'', retval) 
    1.12 +
    1.13 +def tb(n):
    1.14 +    retval = ""
    1.15 +    for i in range(n):
    1.16 +        retval = retval + "    "
    1.17 +    return retval
    1.18 +
    1.19 +parser = argparse.ArgumentParser()
    1.20 +parser.add_argument("suite_name", help="(convention is <NameInUpperCamelCase>, e.g. StringpairList - suite created will then be StringpairListTests)")
    1.21 +parser.add_argument("--clobber", "-c", help="Overwrite extant files (must be explicitly indicated)", action='store_true')
    1.22 +
    1.23 +args = parser.parse_args()
    1.24 +
    1.25 +suitename = args.suite_name
    1.26 +test_suite = suitename + "Test"
    1.27 +
    1.28 +nspace = "using namespace std;\n\n"
    1.29 +
    1.30 +newfile = open(os.path.join("src", test_suite + ".cc"), 'w')
    1.31 +
    1.32 +license = ("// This file is under GNU General Public License 3.0\n"
    1.33 +           "// see LICENSE.txt\n\n")
    1.34 +
    1.35 +newfile.write("#include <stdlib.h>\n")
    1.36 +newfile.write("#include <string>\n")
    1.37 +newfile.write("#include <cstring>\n\n")
    1.38 +newfile.write("#include \"pEpEngine.h\"\n")
    1.39 +newfile.write("#include \"test_util.h\"\n")
    1.40 +newfile.write("#include \"TestConstants.h\"\n")
    1.41 +newfile.write("#include \"Engine.h\"\n\n")                    
    1.42 +newfile.write("#include <gtest/gtest.h>\n\n\n")
    1.43 +newfile.write("namespace {\n\n\t//The fixture for " + test_suite + "\n")
    1.44 +newfile.write(tb(1) + "class " + test_suite + " : public ::testing::Test {\n")
    1.45 +newfile.write(tb(2) + "public:\n")
    1.46 +newfile.write(tb(3) + "Engine* engine;\n")
    1.47 +newfile.write(tb(3) + "PEP_SESSION session;\n\n")
    1.48 +newfile.write(tb(2) + "protected:\n")
    1.49 +newfile.write(tb(3) + "// You can remove any or all of the following functions if its body\n")
    1.50 +newfile.write(tb(3) + "// is empty.\n")
    1.51 +newfile.write(tb(3) + test_suite + "() {\n")
    1.52 +newfile.write(tb(4) + "// You can do set-up work for each test here.\n")
    1.53 +newfile.write(tb(4) + "test_suite_name = ::testing::UnitTest::GetInstance()->current_test_info()->test_suite_name();\n")
    1.54 +newfile.write(tb(4) + "test_name = ::testing::UnitTest::GetInstance()->current_test_info()->name();\n")
    1.55 +newfile.write(tb(4) + "test_path = get_main_test_home_dir() + \"/\" + test_suite_name + \"/\" + test_name;\n")
    1.56 +newfile.write(tb(3) + "}\n\n")
    1.57 +newfile.write(tb(3) + "~" + test_suite + "() override {\n")
    1.58 +newfile.write(tb(4) + "// You can do clean-up work that doesn't throw exceptions here.\n")
    1.59 +newfile.write(tb(3) + "}\n\n")
    1.60 +newfile.write(tb(3) + "// If the constructor and destructor are not enough for setting up\n")
    1.61 +newfile.write(tb(3) + "// and cleaning up each test, you can define the following methods:\n\n")
    1.62 +newfile.write(tb(3) + "void SetUp() override {\n")
    1.63 +newfile.write(tb(4) + "// Code here will be called immediately after the constructor (right\n")
    1.64 +newfile.write(tb(4) + "// before each test).\n")
    1.65 +newfile.write("\n" + tb(4) + "// Leave this empty if there are no files to copy to the home directory path\n")
    1.66 +newfile.write(tb(4) + "std::vector<std::pair<std::string, std::string>> init_files = std::vector<std::pair<std::string, std::string>>();\n")                                        
    1.67 +newfile.write("\n" + tb(4) + "// Get a new test Engine.\n")                    
    1.68 +newfile.write(tb(4) + "engine = new Engine(test_path);\n");
    1.69 +newfile.write(tb(4) + "ASSERT_NE(engine, nullptr);\n")
    1.70 +newfile.write("\n" + tb(4) + "// Ok, let's initialize test directories etc.\n")                                        
    1.71 +newfile.write(tb(4) + "engine->prep(NULL, NULL, init_files);\n")
    1.72 +newfile.write("\n" + tb(4) + "// Ok, try to start this bugger.\n")                    
    1.73 +newfile.write(tb(4) + "engine->start();\n")                    
    1.74 +newfile.write(tb(4) + "ASSERT_NE(engine->session, nullptr);\n")                    
    1.75 +newfile.write(tb(4) + "session = engine->session;\n") 
    1.76 +newfile.write("\n" + tb(4) + "// Engine is up. Keep on truckin\'\n");                                                            
    1.77 +newfile.write(tb(3) + "}\n\n")
    1.78 +newfile.write(tb(3) + "void TearDown() override {\n")
    1.79 +newfile.write(tb(4) + "// Code here will be called immediately after each test (right\n")
    1.80 +newfile.write(tb(4) + "// before the destructor).\n")   
    1.81 +newfile.write(tb(4) + "engine->shut_down();\n")
    1.82 +newfile.write(tb(4) + "delete engine;\n")                    
    1.83 +newfile.write(tb(4) + "engine = NULL;\n")                    
    1.84 +newfile.write(tb(4) + "session = NULL;\n")                    
    1.85 +newfile.write(tb(3) + "}\n\n")
    1.86 +newfile.write(tb(2) + "private:\n");
    1.87 +newfile.write(tb(3) + "const char* test_suite_name;\n")
    1.88 +newfile.write(tb(3) + "const char* test_name;\n")                                                            
    1.89 +newfile.write(tb(3) + "string test_path;\n") 
    1.90 +newfile.write(tb(3) + "// Objects declared here can be used by all tests in the " + test_suite + " suite.\n\n")
    1.91 +newfile.write(tb(1) + "};\n\n")
    1.92 +newfile.write("}  // namespace\n\n\n")
    1.93 +newfile.write("TEST_F(" + test_suite + ", check_" + decamel(suitename) + ") {\n")
    1.94 +newfile.write(tb(1) + "// This is just a dummy test case. The convention is check_whatever_you_are_checking\n")
    1.95 +newfile.write(tb(1) + "// so for multiple test cases in a suite, be more explicit ;)\n")
    1.96 +newfile.write("}\n")
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/test/src/NoOwnIdentWritesOnDecryptTest.cc	Fri Aug 30 17:15:07 2019 +0200
     2.3 @@ -0,0 +1,251 @@
     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 +
    2.11 +#include "pEpEngine.h"
    2.12 +#include "test_util.h"
    2.13 +
    2.14 +
    2.15 +
    2.16 +#include "Engine.h"
    2.17 +
    2.18 +#include <gtest/gtest.h>
    2.19 +
    2.20 +
    2.21 +namespace {
    2.22 +
    2.23 +	//The fixture for NoOwnIdentWritesOnDecryptTest
    2.24 +    class NoOwnIdentWritesOnDecryptTest : public ::testing::Test {
    2.25 +        public:
    2.26 +            Engine* engine;
    2.27 +            PEP_SESSION session;
    2.28 +            message* _to_decrypt;
    2.29 +            string test_path;            
    2.30 +            std::vector<std::pair<std::string, std::string>> init_files;
    2.31 +            
    2.32 +        protected:
    2.33 +            // You can remove any or all of the following functions if its body
    2.34 +            // is empty.
    2.35 +            NoOwnIdentWritesOnDecryptTest() {
    2.36 +                // You can do set-up work for each test here.
    2.37 +                test_suite_name = ::testing::UnitTest::GetInstance()->current_test_info()->test_suite_name();
    2.38 +                test_name = ::testing::UnitTest::GetInstance()->current_test_info()->name();
    2.39 +                test_path = get_main_test_home_dir() + "/" + test_suite_name + "/" + test_name;
    2.40 +                _to_decrypt = NULL;
    2.41 +            }
    2.42 +
    2.43 +            ~NoOwnIdentWritesOnDecryptTest() override {
    2.44 +                // You can do clean-up work that doesn't throw exceptions here.
    2.45 +                free_message(_to_decrypt);
    2.46 +            }
    2.47 +
    2.48 +            // If the constructor and destructor are not enough for setting up
    2.49 +            // and cleaning up each test, you can define the following methods:
    2.50 +
    2.51 +            void SetUp() override {
    2.52 +                // Code here will be called immediately after the constructor (right
    2.53 +                // before each test).
    2.54 +
    2.55 +                // Leave this empty if there are no files to copy to the home directory path
    2.56 +                init_files = std::vector<std::pair<std::string, std::string>>();
    2.57 +
    2.58 +                // Get a new test Engine.
    2.59 +                engine = new Engine(test_path);
    2.60 +                ASSERT_NE(engine, nullptr);
    2.61 +
    2.62 +                // Ok, let's initialize test directories etc.
    2.63 +                engine->prep(NULL, NULL, init_files);
    2.64 +
    2.65 +                // Ok, try to start this bugger.
    2.66 +                engine->start();
    2.67 +                ASSERT_NE(engine->session, nullptr);
    2.68 +                session = engine->session;
    2.69 +
    2.70 +                // Engine is up. Keep on truckin'
    2.71 +            }
    2.72 +
    2.73 +            void TearDown() override {
    2.74 +                // Code here will be called immediately after each test (right
    2.75 +                // before the destructor).
    2.76 +                engine->shut_down();
    2.77 +                delete engine;
    2.78 +                engine = NULL;
    2.79 +                session = NULL;
    2.80 +            }
    2.81 +
    2.82 +        private:
    2.83 +            const char* test_suite_name;
    2.84 +            const char* test_name;
    2.85 +            // Objects declared here can be used by all tests in the NoOwnIdentWritesOnDecryptTest suite.
    2.86 +
    2.87 +    };
    2.88 +
    2.89 +}  // namespace
    2.90 +
    2.91 +
    2.92 +TEST_F(NoOwnIdentWritesOnDecryptTest, check_no_own_ident_writes_on_decrypt) {
    2.93 +    
    2.94 +    // set _to_decrypt without polluting test keyrings
    2.95 +    message* msg = new_message(PEP_dir_outgoing);
    2.96 +    pEp_identity* sender = NULL;
    2.97 +    pEp_identity* me_recip = NULL;
    2.98 +    pEp_identity* other_recip = NULL;
    2.99 +
   2.100 +    ASSERT_TRUE(slurp_and_import_key(session, "test_keys/pub/pep-test-alice-0x6FF00E97_pub.asc"));
   2.101 +    ASSERT_TRUE(slurp_and_import_key(session, "test_keys/priv/pep-test-alice-0x6FF00E97_priv.asc"));
   2.102 +    ASSERT_TRUE(slurp_and_import_key(session, "test_keys/pub/pep-test-bob-0xC9C2EE39_pub.asc"));
   2.103 +    ASSERT_TRUE(slurp_and_import_key(session, "test_keys/pub/pep-test-carol-0x42A85A42_pub.asc"));
   2.104 +
   2.105 +    sender = new_identity("pep.test.alice@pep-project.org", NULL, PEP_OWN_USERID, "Alice");
   2.106 +    set_own_key(session, sender, "4ABE3AAF59AC32CFE4F86500A9411D176FF00E97");
   2.107 +    myself(session, sender);
   2.108 +
   2.109 +    me_recip = new_identity("pep.test.bob@pep-project.org", NULL, "Bob_is_hot", "Hot Bob");
   2.110 +    other_recip = new_identity("pep-test-carol@pep-project.org", NULL, "Carol_loves_me", "Carol Loves Alice");
   2.111 +
   2.112 +    identity_list* to_list = new_identity_list(other_recip);
   2.113 +    identity_list_add(to_list, me_recip);
   2.114 +
   2.115 +    msg->from = sender;
   2.116 +    msg->to = to_list;
   2.117 +
   2.118 +    msg->shortmsg = strdup("just a message");
   2.119 +    msg->longmsg = strdup("a really dumb message");
   2.120 +
   2.121 +    message* enc_msg = NULL;
   2.122 +
   2.123 +    PEP_STATUS status = encrypt_message(session, msg, NULL, &enc_msg, PEP_enc_PGP_MIME, 0);
   2.124 +    ASSERT_EQ(status , PEP_STATUS_OK);
   2.125 +    free_message(msg);
   2.126 +    enc_msg->dir = PEP_dir_incoming;
   2.127 +    _to_decrypt = enc_msg;
   2.128 +    
   2.129 +    engine->shut_down();
   2.130 +    delete engine;
   2.131 +
   2.132 +    // New Engine, new test case:
   2.133 +    engine = new Engine(test_path);
   2.134 +    ASSERT_NE(engine, nullptr);
   2.135 +
   2.136 +    // Ok, let's initialize test directories etc.
   2.137 +    engine->prep(NULL, NULL, init_files);
   2.138 +
   2.139 +    // Ok, try to start this bugger.
   2.140 +    engine->start();
   2.141 +    ASSERT_NE(engine->session, nullptr);
   2.142 +    session = engine->session;
   2.143 +
   2.144 +    // Next test: check_address_only_no_overwrite) {
   2.145 +    ASSERT_NE(_to_decrypt, nullptr);
   2.146 +    message* copy = message_dup(_to_decrypt);
   2.147 +
   2.148 +    free_identity(copy->from);
   2.149 +
   2.150 +    ASSERT_TRUE(slurp_and_import_key(session, "test_keys/pub/pep-test-alice-0x6FF00E97_pub.asc"));
   2.151 +    ASSERT_TRUE(slurp_and_import_key(session, "test_keys/pub/pep-test-bob-0xC9C2EE39_pub.asc"));
   2.152 +    ASSERT_TRUE(slurp_and_import_key(session, "test_keys/priv/pep-test-bob-0xC9C2EE39_priv.asc"));
   2.153 +    ASSERT_TRUE(slurp_and_import_key(session, "test_keys/pub/pep-test-carol-0x42A85A42_pub.asc"));
   2.154 +
   2.155 +    const char* bob_name = "STOP MESSING WITH ME ALICE";
   2.156 +    const char* bob_fpr = "BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39";
   2.157 +    pEp_identity* me = new_identity("pep.test.bob@pep-project.org", NULL, PEP_OWN_USERID, bob_name);
   2.158 +    status = set_own_key(session, me, bob_fpr);
   2.159 +    ASSERT_EQ(status , PEP_STATUS_OK);
   2.160 +    status = myself(session, me);
   2.161 +    ASSERT_EQ(status , PEP_STATUS_OK);
   2.162 +    free_identity(me);
   2.163 +    me = NULL;
   2.164 +
   2.165 +    copy->from = new_identity("pep.test.alice@pep-project.org", NULL, NULL, NULL);
   2.166 +    pEp_identity* bob_ident = copy->to->next->ident;
   2.167 +    free(bob_ident->fpr);
   2.168 +    free(bob_ident->user_id);
   2.169 +    bob_ident->fpr = NULL;
   2.170 +    bob_ident->user_id = NULL;
   2.171 +
   2.172 +    // yes, I know the test keeps the "old" user_id for carol, but it's irrelevant here/
   2.173 +
   2.174 +    message* dec_msg = NULL;
   2.175 +    stringlist_t* keylist = NULL;
   2.176 +    PEP_decrypt_flags_t flags = 0;
   2.177 +    PEP_rating rating = PEP_rating_undefined;
   2.178 +
   2.179 +    status = decrypt_message(session, copy, &dec_msg, &keylist, &rating, &flags);
   2.180 +    ASSERT_EQ(status , PEP_STATUS_OK);
   2.181 +    ASSERT_STREQ(dec_msg->to->next->ident->username, "Hot Bob");
   2.182 +
   2.183 +    // Make sure Alice calling Bob hot doesn't infiltrate his DB
   2.184 +    status = get_identity(session, "pep.test.bob@pep-project.org", PEP_OWN_USERID, &me);
   2.185 +    ASSERT_EQ(status , PEP_STATUS_OK);
   2.186 +    ASSERT_NE(me, nullptr);
   2.187 +    ASSERT_STREQ(me->username, bob_name);
   2.188 +    ASSERT_STREQ(me->fpr, bob_fpr);
   2.189 +    free_identity(me);
   2.190 +    free_message(dec_msg);
   2.191 +    free_message(copy);
   2.192 +    copy = NULL;
   2.193 +    
   2.194 +    engine->shut_down();
   2.195 +    delete engine;
   2.196 +
   2.197 +    // New Engine, new test case:
   2.198 +    engine = new Engine(test_path);
   2.199 +    ASSERT_NE(engine, nullptr);
   2.200 +
   2.201 +    // Ok, let's initialize test directories etc.
   2.202 +    engine->prep(NULL, NULL, init_files);
   2.203 +
   2.204 +    // Ok, try to start this bugger.
   2.205 +    engine->start();
   2.206 +    ASSERT_NE(engine->session, nullptr);
   2.207 +    session = engine->session;
   2.208 +
   2.209 +    // Next test case: check_full_info_no_overwrite) {
   2.210 +    ASSERT_NE(_to_decrypt, nullptr);
   2.211 +    copy = message_dup(_to_decrypt);
   2.212 +
   2.213 +    free_identity(copy->from);
   2.214 +
   2.215 +    ASSERT_TRUE(slurp_and_import_key(session, "test_keys/pub/pep-test-alice-0x6FF00E97_pub.asc"));
   2.216 +    ASSERT_TRUE(slurp_and_import_key(session, "test_keys/pub/pep-test-bob-0xC9C2EE39_pub.asc"));
   2.217 +    ASSERT_TRUE(slurp_and_import_key(session, "test_keys/priv/pep-test-bob-0xC9C2EE39_priv.asc"));
   2.218 +    ASSERT_TRUE(slurp_and_import_key(session, "test_keys/pub/pep-test-carol-0x42A85A42_pub.asc"));
   2.219 +
   2.220 +    me = new_identity("pep.test.bob@pep-project.org", NULL, PEP_OWN_USERID, bob_name);
   2.221 +    status = set_own_key(session, me, bob_fpr);
   2.222 +    ASSERT_EQ(status , PEP_STATUS_OK);
   2.223 +    status = myself(session, me);
   2.224 +    ASSERT_EQ(status , PEP_STATUS_OK);
   2.225 +    free_identity(me);
   2.226 +    me = NULL;
   2.227 +
   2.228 +    copy->from = new_identity("pep.test.alice@pep-project.org", NULL, NULL, NULL);
   2.229 +    bob_ident = copy->to->next->ident;
   2.230 +    free(bob_ident->user_id);
   2.231 +    bob_ident->user_id = strdup(PEP_OWN_USERID);
   2.232 +    bob_ident->me = true;
   2.233 +
   2.234 +    // yes, I know the test keeps the "old" user_id for carol, but it's irrelevant here
   2.235 +    dec_msg = NULL;
   2.236 +    keylist = NULL;
   2.237 +    flags = 0;
   2.238 +    rating = PEP_rating_undefined;
   2.239 +
   2.240 +    status = decrypt_message(session, copy, &dec_msg, &keylist, &rating, &flags);
   2.241 +    ASSERT_EQ(status , PEP_STATUS_OK);
   2.242 +    ASSERT_STREQ(dec_msg->to->next->ident->username, "Hot Bob");
   2.243 +
   2.244 +    // Make sure Alice calling Bob hot doesn't infiltrate his DB
   2.245 +    status = get_identity(session, "pep.test.bob@pep-project.org", PEP_OWN_USERID, &me);
   2.246 +    ASSERT_EQ(status , PEP_STATUS_OK);
   2.247 +    ASSERT_NE(me, nullptr);
   2.248 +    ASSERT_STREQ(me->username, bob_name);
   2.249 +    ASSERT_STREQ(me->fpr, bob_fpr);
   2.250 +    free_identity(me);
   2.251 +    free_message(dec_msg);
   2.252 +
   2.253 +    free_message(copy);
   2.254 +}
     3.1 --- a/test/src/NoOwnIdentWritesOnDecryptTests.cc.FIXME	Fri Aug 30 17:14:39 2019 +0200
     3.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.3 @@ -1,168 +0,0 @@
     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 <cstring>
    3.10 -#include <cpptest.h>
    3.11 -
    3.12 -#include "pEpEngine.h"
    3.13 -#include "test_util.h"
    3.14 -
    3.15 -#include "EngineTestIndividualSuite.h"
    3.16 -#include "NoOwnIdentWritesOnDecryptTests.h"
    3.17 -
    3.18 -using namespace std;
    3.19 -
    3.20 -NoOwnIdentWritesOnDecryptTests::NoOwnIdentWritesOnDecryptTests(string suitename, string test_home_dir) :
    3.21 -    EngineTestIndividualSuite::EngineTestIndividualSuite(suitename, test_home_dir) {
    3.22 -    _to_decrypt = NULL;
    3.23 -    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("NoOwnIdentWritesOnDecryptTests::check_no_own_ident_writes_on_decrypt"),
    3.24 -                                                                      static_cast<Func>(&NoOwnIdentWritesOnDecryptTests::check_no_own_ident_writes_on_decrypt)));
    3.25 -    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("NoOwnIdentWritesOnDecryptTests::check_address_only_no_overwrite"),
    3.26 -                                                                      static_cast<Func>(&NoOwnIdentWritesOnDecryptTests::check_address_only_no_overwrite)));
    3.27 -    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("NoOwnIdentWritesOnDecryptTests::check_full_info_no_overwrite"),
    3.28 -                                                                      static_cast<Func>(&NoOwnIdentWritesOnDecryptTests::check_full_info_no_overwrite)));
    3.29 -}
    3.30 -
    3.31 -NoOwnIdentWritesOnDecryptTests::~NoOwnIdentWritesOnDecryptTests() {
    3.32 -    free_message(_to_decrypt);
    3.33 -}
    3.34 -
    3.35 -void NoOwnIdentWritesOnDecryptTests::check_no_own_ident_writes_on_decrypt() {
    3.36 -    // This is a weird case - it is NOT a test case, it's just abusing the environment to
    3.37 -    // set _to_decrypt without polluting test keyrings for later tests.
    3.38 -    message* msg = new_message(PEP_dir_outgoing);
    3.39 -    pEp_identity* sender = NULL;
    3.40 -    pEp_identity* me_recip = NULL;
    3.41 -    pEp_identity* other_recip = NULL;
    3.42 -    
    3.43 -    TEST_ASSERT(slurp_and_import_key(session, "test_keys/pub/pep-test-alice-0x6FF00E97_pub.asc"));
    3.44 -    TEST_ASSERT(slurp_and_import_key(session, "test_keys/priv/pep-test-alice-0x6FF00E97_priv.asc"));	
    3.45 -    TEST_ASSERT(slurp_and_import_key(session, "test_keys/pub/pep-test-bob-0xC9C2EE39_pub.asc"));
    3.46 -    TEST_ASSERT(slurp_and_import_key(session, "test_keys/pub/pep-test-carol-0x42A85A42_pub.asc"));
    3.47 -    
    3.48 -    sender = new_identity("pep.test.alice@pep-project.org", NULL, PEP_OWN_USERID, "Alice");
    3.49 -    set_own_key(session, sender, "4ABE3AAF59AC32CFE4F86500A9411D176FF00E97");
    3.50 -    myself(session, sender);
    3.51 -    
    3.52 -    me_recip = new_identity("pep.test.bob@pep-project.org", NULL, "Bob_is_hot", "Hot Bob");
    3.53 -    other_recip = new_identity("pep-test-carol@pep-project.org", NULL, "Carol_loves_me", "Carol Loves Alice");
    3.54 -
    3.55 -    identity_list* to_list = new_identity_list(other_recip);
    3.56 -    identity_list_add(to_list, me_recip);
    3.57 -    
    3.58 -    msg->from = sender;
    3.59 -    msg->to = to_list;
    3.60 -    
    3.61 -    msg->shortmsg = strdup("just a message");
    3.62 -    msg->longmsg = strdup("a really dumb message");
    3.63 -    
    3.64 -    message* enc_msg = NULL;
    3.65 -    
    3.66 -    PEP_STATUS status = encrypt_message(session, msg, NULL, &enc_msg, PEP_enc_PGP_MIME, 0);
    3.67 -    TEST_ASSERT(status == PEP_STATUS_OK);
    3.68 -    free_message(msg);
    3.69 -    enc_msg->dir = PEP_dir_incoming;
    3.70 -    _to_decrypt = enc_msg;
    3.71 -    TEST_ASSERT(true);
    3.72 -}
    3.73 -
    3.74 -void NoOwnIdentWritesOnDecryptTests::check_address_only_no_overwrite() {
    3.75 -    TEST_ASSERT(_to_decrypt);
    3.76 -    message* copy = message_dup(_to_decrypt);
    3.77 -
    3.78 -    free_identity(copy->from);
    3.79 -    
    3.80 -    TEST_ASSERT(slurp_and_import_key(session, "test_keys/pub/pep-test-alice-0x6FF00E97_pub.asc"));
    3.81 -    TEST_ASSERT(slurp_and_import_key(session, "test_keys/pub/pep-test-bob-0xC9C2EE39_pub.asc"));
    3.82 -    TEST_ASSERT(slurp_and_import_key(session, "test_keys/priv/pep-test-bob-0xC9C2EE39_priv.asc"));
    3.83 -    TEST_ASSERT(slurp_and_import_key(session, "test_keys/pub/pep-test-carol-0x42A85A42_pub.asc"));
    3.84 -    
    3.85 -    const char* bob_name = "STOP MESSING WITH ME ALICE";
    3.86 -    const char* bob_fpr = "BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39";
    3.87 -    pEp_identity* me = new_identity("pep.test.bob@pep-project.org", NULL, PEP_OWN_USERID, bob_name);
    3.88 -    PEP_STATUS status = set_own_key(session, me, bob_fpr);
    3.89 -    TEST_ASSERT(status == PEP_STATUS_OK);
    3.90 -    status = myself(session, me);
    3.91 -    TEST_ASSERT(status == PEP_STATUS_OK);
    3.92 -    free_identity(me);
    3.93 -    me = NULL;
    3.94 -    
    3.95 -    copy->from = new_identity("pep.test.alice@pep-project.org", NULL, NULL, NULL);
    3.96 -    pEp_identity* bob_ident = copy->to->next->ident;
    3.97 -    free(bob_ident->fpr);
    3.98 -    free(bob_ident->user_id);
    3.99 -    bob_ident->fpr = NULL;
   3.100 -    bob_ident->user_id = NULL;
   3.101 -    
   3.102 -    // yes, I know the test keeps the "old" user_id for carol, but it's irrelevant here/
   3.103 -
   3.104 -    message* dec_msg = NULL;
   3.105 -    stringlist_t* keylist = NULL;
   3.106 -    PEP_decrypt_flags_t flags = 0;
   3.107 -    PEP_rating rating = PEP_rating_undefined;
   3.108 -    
   3.109 -    status = decrypt_message(session, copy, &dec_msg, &keylist, &rating, &flags);    
   3.110 -    TEST_ASSERT(status == PEP_STATUS_OK);
   3.111 -    TEST_ASSERT(strcmp(dec_msg->to->next->ident->username, "Hot Bob") == 0);
   3.112 -    
   3.113 -    // Make sure Alice calling Bob hot doesn't infiltrate his DB
   3.114 -    status = get_identity(session, "pep.test.bob@pep-project.org", PEP_OWN_USERID, &me);
   3.115 -    TEST_ASSERT(status == PEP_STATUS_OK);
   3.116 -    TEST_ASSERT(me);
   3.117 -    TEST_ASSERT(strcmp(me->username, bob_name) == 0);
   3.118 -    TEST_ASSERT(strcmp(me->fpr, bob_fpr) == 0);
   3.119 -    free_identity(me);
   3.120 -    free_message(dec_msg);
   3.121 -    free_message(copy);
   3.122 -}
   3.123 -
   3.124 -void NoOwnIdentWritesOnDecryptTests::check_full_info_no_overwrite() {
   3.125 -    TEST_ASSERT(_to_decrypt);
   3.126 -    message* copy = message_dup(_to_decrypt);
   3.127 -
   3.128 -    free_identity(copy->from);
   3.129 -    
   3.130 -    TEST_ASSERT(slurp_and_import_key(session, "test_keys/pub/pep-test-alice-0x6FF00E97_pub.asc"));
   3.131 -    TEST_ASSERT(slurp_and_import_key(session, "test_keys/pub/pep-test-bob-0xC9C2EE39_pub.asc"));
   3.132 -    TEST_ASSERT(slurp_and_import_key(session, "test_keys/priv/pep-test-bob-0xC9C2EE39_priv.asc"));
   3.133 -    TEST_ASSERT(slurp_and_import_key(session, "test_keys/pub/pep-test-carol-0x42A85A42_pub.asc"));
   3.134 -    
   3.135 -    const char* bob_name = "STOP MESSING WITH ME ALICE";
   3.136 -    const char* bob_fpr = "BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39";
   3.137 -    pEp_identity* me = new_identity("pep.test.bob@pep-project.org", NULL, PEP_OWN_USERID, bob_name);
   3.138 -    PEP_STATUS status = set_own_key(session, me, bob_fpr);
   3.139 -    TEST_ASSERT(status == PEP_STATUS_OK);
   3.140 -    status = myself(session, me);
   3.141 -    TEST_ASSERT(status == PEP_STATUS_OK);
   3.142 -    free_identity(me);
   3.143 -    me = NULL;
   3.144 -    
   3.145 -    copy->from = new_identity("pep.test.alice@pep-project.org", NULL, NULL, NULL);
   3.146 -    pEp_identity* bob_ident = copy->to->next->ident;
   3.147 -    free(bob_ident->user_id);
   3.148 -    bob_ident->user_id = strdup(PEP_OWN_USERID);
   3.149 -    bob_ident->me = true;
   3.150 -    
   3.151 -    // yes, I know the test keeps the "old" user_id for carol, but it's irrelevant here
   3.152 -    message* dec_msg = NULL;
   3.153 -    stringlist_t* keylist = NULL;
   3.154 -    PEP_decrypt_flags_t flags = 0;
   3.155 -    PEP_rating rating = PEP_rating_undefined;
   3.156 -    
   3.157 -    status = decrypt_message(session, copy, &dec_msg, &keylist, &rating, &flags);    
   3.158 -    TEST_ASSERT(status == PEP_STATUS_OK);
   3.159 -    TEST_ASSERT(strcmp(dec_msg->to->next->ident->username, "Hot Bob") == 0);
   3.160 -    
   3.161 -    // Make sure Alice calling Bob hot doesn't infiltrate his DB
   3.162 -    status = get_identity(session, "pep.test.bob@pep-project.org", PEP_OWN_USERID, &me);
   3.163 -    TEST_ASSERT(status == PEP_STATUS_OK);
   3.164 -    TEST_ASSERT(me);
   3.165 -    TEST_ASSERT(strcmp(me->username, bob_name) == 0);
   3.166 -    TEST_ASSERT(strcmp(me->fpr, bob_fpr) == 0);
   3.167 -    free_identity(me);
   3.168 -    free_message(dec_msg);
   3.169 -
   3.170 -    free_message(copy);
   3.171 -}
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/test/src/SyncTest.cc	Fri Aug 30 17:15:07 2019 +0200
     4.3 @@ -0,0 +1,265 @@
     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 +}
     5.1 --- a/test/src/SyncTests.cc	Fri Aug 30 17:14:39 2019 +0200
     5.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.3 @@ -1,265 +0,0 @@
     5.4 -// This file is under GNU General Public License 3.0
     5.5 -// see LICENSE.txt
     5.6 -
     5.7 -#include <stdlib.h>
     5.8 -#include <string>
     5.9 -#include <assert.h>
    5.10 -#include <thread>
    5.11 -
    5.12 -#include "locked_queue.hh"
    5.13 -#include "sync_api.h"
    5.14 -#include "Sync_impl.h"
    5.15 -
    5.16 -#include "test_util.h"
    5.17 -
    5.18 -#include "pEpEngine.h"
    5.19 -
    5.20 -#include "pEp_internal.h"
    5.21 -#include "KeySync_fsm.h"
    5.22 -#include "sync_codec.h"
    5.23 -
    5.24 -#include "Engine.h"
    5.25 -
    5.26 -#include <gtest/gtest.h>
    5.27 -
    5.28 -class Sync_Adapter {
    5.29 -public:
    5.30 -    utility::locked_queue< Sync_event_t * > q;
    5.31 -
    5.32 -    void processing();
    5.33 -
    5.34 -    static PEP_STATUS notifyHandshake(
    5.35 -            pEp_identity *me,
    5.36 -            pEp_identity *partner,
    5.37 -            sync_handshake_signal signal
    5.38 -        );
    5.39 -    static int inject_sync_event(SYNC_EVENT ev, void *management);
    5.40 -    static Sync_event_t *retrieve_next_sync_event(void *management, unsigned threshold);
    5.41 -    static PEP_STATUS messageToSend(struct _message *msg);
    5.42 -
    5.43 -    static void sync_thread(PEP_SESSION session, Sync_Adapter *adapter);
    5.44 -};
    5.45 -
    5.46 -
    5.47 -void Sync_Adapter::processing()
    5.48 -{
    5.49 -    output_stream << "waiting for processing\n";
    5.50 -    while (!q.empty()) {
    5.51 -        nanosleep((const struct timespec[]){{0, 100000000L}}, NULL);
    5.52 -    }
    5.53 -}
    5.54 -
    5.55 -PEP_STATUS Sync_Adapter::notifyHandshake(
    5.56 -        pEp_identity *me,
    5.57 -        pEp_identity *partner,
    5.58 -        sync_handshake_signal signal
    5.59 -    )
    5.60 -{
    5.61 -    return PEP_STATUS_OK;
    5.62 -}
    5.63 -
    5.64 -int Sync_Adapter::inject_sync_event(SYNC_EVENT ev, void *management)
    5.65 -{
    5.66 -    Sync_event_t *_ev = ev;
    5.67 -    switch (_ev->fsm) {
    5.68 -        case Sync_PR_keysync:
    5.69 -            output_stream << "injecting event " << KeySync_event_name(_ev->event) << "\n";
    5.70 -            break;
    5.71 -        default:
    5.72 -            output_stream << "unknown state machine: " << _ev->fsm << "\n";
    5.73 -            assert(0);
    5.74 -    }
    5.75 -    auto adapter = static_cast< Sync_Adapter *>(management);
    5.76 -    adapter->q.push_front(ev);
    5.77 -    return 0;
    5.78 -}
    5.79 -
    5.80 -Sync_event_t *Sync_Adapter::retrieve_next_sync_event(void *management, unsigned threshold)
    5.81 -{
    5.82 -    auto adapter = static_cast< Sync_Adapter *>(management);
    5.83 -    time_t started = time(nullptr);
    5.84 -    bool timeout = false;
    5.85 -
    5.86 -    while (adapter->q.empty()) {
    5.87 -        int i = 0;
    5.88 -        ++i;
    5.89 -        if (i > 10) {
    5.90 -            if (time(nullptr) > started + threshold) {
    5.91 -                timeout = true;
    5.92 -                break;
    5.93 -            }
    5.94 -            i = 0;
    5.95 -        }
    5.96 -        nanosleep((const struct timespec[]){{0, 100000000L}}, NULL);
    5.97 -    }
    5.98 -
    5.99 -    if (timeout)
   5.100 -        return SYNC_TIMEOUT_EVENT;
   5.101 -
   5.102 -    Sync_event_t *ev = adapter->q.pop_front();
   5.103 -    if (ev) {
   5.104 -        switch (ev->fsm) {
   5.105 -            case Sync_PR_keysync:
   5.106 -                output_stream << "sync thread: retrieving event " << KeySync_event_name(ev->event) << "\n";
   5.107 -                break;
   5.108 -            default:
   5.109 -                output_stream << "sync thread: unknown state machine: " << ev->fsm << "\n";
   5.110 -                assert(0);
   5.111 -        }
   5.112 -    }
   5.113 -    else {
   5.114 -        output_stream << "sync thread: retrieving shutdown\n";
   5.115 -    }
   5.116 -
   5.117 -    return ev;
   5.118 -}
   5.119 -
   5.120 -PEP_STATUS Sync_Adapter::messageToSend(struct _message *msg)
   5.121 -{
   5.122 -    assert(msg && msg->attachments);
   5.123 -
   5.124 -    output_stream << "sending message:\n";
   5.125 -
   5.126 -    for (bloblist_t *b = msg->attachments; b && b->value; b = b->next) {
   5.127 -        if (b->mime_type && strcasecmp(b->mime_type, "application/pEp.sync") == 0) {
   5.128 -            assert(msg->from && msg->from->address && msg->from->username);
   5.129 -            output_stream << "<!-- " << msg->from->username << " <" << msg->from->address << "> -->\n";
   5.130 -            char *text = NULL;
   5.131 -            PEP_STATUS status = PER_to_XER_Sync_msg(msg->attachments->value, msg->attachments->size, &text);
   5.132 -            assert(status == PEP_STATUS_OK);
   5.133 -            output_stream << text << "\n";
   5.134 -            free(text);
   5.135 -        }
   5.136 -    }
   5.137 -
   5.138 -    free_message(msg);
   5.139 -    return PEP_STATUS_OK;
   5.140 -}
   5.141 -
   5.142 -void Sync_Adapter::sync_thread(PEP_SESSION session, Sync_Adapter *adapter)
   5.143 -{
   5.144 -    output_stream << "sync_thread: startup\n";
   5.145 -    do_sync_protocol(session, adapter);
   5.146 -    output_stream << "sync_thread: shutdown\n";
   5.147 -}
   5.148 -
   5.149 -
   5.150 -namespace {
   5.151 -
   5.152 -	//The fixture for SyncTest
   5.153 -    class SyncTest : public ::testing::Test {
   5.154 -        public:
   5.155 -            Engine* engine;
   5.156 -            PEP_SESSION session;
   5.157 -
   5.158 -            Sync_Adapter adapter;
   5.159 -            PEP_SESSION sync = NULL;
   5.160 -            thread *sync_thread;
   5.161 -            
   5.162 -
   5.163 -        protected:
   5.164 -            // You can remove any or all of the following functions if its body
   5.165 -            // is empty.
   5.166 -            SyncTest() {
   5.167 -                // You can do set-up work for each test here.
   5.168 -                test_suite_name = ::testing::UnitTest::GetInstance()->current_test_info()->test_suite_name();
   5.169 -                test_name = ::testing::UnitTest::GetInstance()->current_test_info()->name();
   5.170 -                test_path = get_main_test_home_dir() + "/" + test_suite_name + "/" + test_name;
   5.171 -            }
   5.172 -
   5.173 -            ~SyncTest() override {
   5.174 -                // You can do clean-up work that doesn't throw exceptions here.
   5.175 -            }
   5.176 -
   5.177 -            // If the constructor and destructor are not enough for setting up
   5.178 -            // and cleaning up each test, you can define the following methods:
   5.179 -
   5.180 -            void SetUp() override {
   5.181 -                // Code here will be called immediately after the constructor (right
   5.182 -                // before each test).
   5.183 -
   5.184 -                // Leave this empty if there are no files to copy to the home directory path
   5.185 -                std::vector<std::pair<std::string, std::string>> init_files = std::vector<std::pair<std::string, std::string>>();
   5.186 -
   5.187 -                // Get a new test Engine.
   5.188 -                engine = new Engine(test_path);
   5.189 -                ASSERT_NE(engine, nullptr);
   5.190 -
   5.191 -                // Ok, let's initialize test directories etc.
   5.192 -                engine->prep(NULL, NULL, init_files);
   5.193 -
   5.194 -                // Ok, try to start this bugger. Is this totally irrelevant for this case??
   5.195 -                engine->start();
   5.196 -                ASSERT_NE(engine->session, nullptr);
   5.197 -                session = engine->session;
   5.198 -
   5.199 -                // Engine is up. Keep on truckin'
   5.200 -
   5.201 -                pEp_identity *self = new_identity("alice@synctests.pEp", nullptr, "23", "Alice Miller");
   5.202 -                assert(self);
   5.203 -                output_stream << "setting own identity for " << self->address << "\n";
   5.204 -                PEP_STATUS status = myself(session, self);
   5.205 -                assert(self->me);
   5.206 -                assert(self->fpr);
   5.207 -                output_stream << "fpr: " << self->fpr << "\n";
   5.208 -                free_identity(self);
   5.209 -
   5.210 -                status = init(&sync, Sync_Adapter::messageToSend, Sync_Adapter::inject_sync_event);
   5.211 -                if (status != PEP_STATUS_OK)
   5.212 -                    throw std::runtime_error((string("init returned ") + tl_status_string(status)).c_str());
   5.213 -
   5.214 -                output_stream << "initialize sync and start first state machine\n";
   5.215 -                status = register_sync_callbacks(
   5.216 -                    sync,
   5.217 -                    (void *) &adapter.q,
   5.218 -                    Sync_Adapter::notifyHandshake,
   5.219 -                    Sync_Adapter::retrieve_next_sync_event
   5.220 -                );
   5.221 -                if (status != PEP_STATUS_OK)
   5.222 -                    throw std::runtime_error((string("register sync status returned ") + tl_status_string(status)).c_str());
   5.223 -                if (sync->sync_state.keysync.state != Sole)
   5.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());
   5.225 -
   5.226 -                output_stream << "creating thread for sync\n";
   5.227 -                sync_thread = new thread(Sync_Adapter::sync_thread, sync, &adapter);
   5.228 -
   5.229 -            }
   5.230 -
   5.231 -            void TearDown() override {
   5.232 -                // Code here will be called immediately after each test (right
   5.233 -                // before the destructor).
   5.234 -                
   5.235 -                adapter.processing();
   5.236 -
   5.237 -                output_stream << "sending shutdown to sync thread\n";
   5.238 -                adapter.q.push_front(nullptr);
   5.239 -                sync_thread->join();
   5.240 -
   5.241 -                unregister_sync_callbacks(sync);
   5.242 -                release(sync);
   5.243 -                
   5.244 -                engine->shut_down();
   5.245 -                delete engine;
   5.246 -                engine = NULL;
   5.247 -                session = NULL;
   5.248 -            }
   5.249 -
   5.250 -        private:
   5.251 -            const char* test_suite_name;
   5.252 -            const char* test_name;
   5.253 -            string test_path;
   5.254 -            // Objects declared here can be used by all tests in the SyncTest suite.
   5.255 -
   5.256 -    };
   5.257 -
   5.258 -}  // namespace
   5.259 -
   5.260 -TEST_F(SyncTest, check_sync)
   5.261 -{
   5.262 -    output_stream << "check_sync(): trigger KeyGen event\n";
   5.263 -    signal_Sync_event(sync, Sync_PR_keysync, KeyGen, NULL);
   5.264 -    adapter.processing();
   5.265 -
   5.266 -    output_stream << "check_sync(): cry for unknown key\n";
   5.267 -    signal_Sync_event(sync, Sync_PR_keysync, CannotDecrypt, NULL);
   5.268 -}