ENGINE-398: bug fixes ENGINE-398
authorKrista Bennett <krista@pep-project.org>
Fri, 31 Aug 2018 14:50:54 +0200
branchENGINE-398
changeset 291751f4afe41dcb
parent 2893 fb7580af7822
child 2918 b6779f7cbb3b
ENGINE-398: bug fixes
src/keymanagement.c
src/message_api.c
src/pEpEngine.c
test/Makefile
test/include/KeyResetMessageTests.h
test/include/test_util.h
test/src/SuiteMaker.cc
test/src/engine_tests/KeyResetMessageTests.cc
test/src/util/test_util.cc
     1.1 --- a/src/keymanagement.c	Mon Aug 27 15:15:59 2018 +0200
     1.2 +++ b/src/keymanagement.c	Fri Aug 31 14:50:54 2018 +0200
     1.3 @@ -1193,6 +1193,10 @@
     1.4      if (!(session && ident && ident->fpr))
     1.5          return PEP_ILLEGAL_VALUE;
     1.6  
     1.7 +    // ident is INPUT ONLY, so we need to preserve the input fpr
     1.8 +    char* preserve_fpr = ident->fpr;
     1.9 +    ident->fpr = strdup(preserve_fpr);
    1.10 +    
    1.11      if (ident->me)
    1.12      {
    1.13          revoke_key(session, ident->fpr, NULL);
    1.14 @@ -1226,7 +1230,8 @@
    1.15          if (status == PEP_STATUS_OK)
    1.16              status = add_mistrusted_key(session, ident->fpr);
    1.17      }
    1.18 -
    1.19 +    free(ident->fpr);
    1.20 +    ident->fpr = preserve_fpr;
    1.21      return status;
    1.22  }
    1.23  
     2.1 --- a/src/message_api.c	Mon Aug 27 15:15:59 2018 +0200
     2.2 +++ b/src/message_api.c	Fri Aug 31 14:50:54 2018 +0200
     2.3 @@ -1574,9 +1574,7 @@
     2.4      assert(revoke_fpr);
     2.5      assert(new_fpr);
     2.6      assert(session);
     2.7 -    assert(session->sync_session);
     2.8 -    assert(session->sync_session->inject_sync_msg);
     2.9 -    assert(session->sync_session->sync_management);
    2.10 +    assert(session->messageToSend || session->sync_session->messageToSend);
    2.11      
    2.12      if (!session || !revoke_fpr || !new_fpr)
    2.13          return PEP_ILLEGAL_VALUE;
    2.14 @@ -1600,7 +1598,7 @@
    2.15                      
    2.16      identity_list* curr_id_ptr = recent_contacts;
    2.17  
    2.18 -    while (curr_id_ptr) {
    2.19 +    for (curr_id_ptr = recent_contacts; curr_id_ptr; curr_id_ptr = curr_id_ptr->next) {
    2.20          pEp_identity* curr_id = curr_id_ptr->ident;
    2.21          
    2.22          if (!curr_id)
    2.23 @@ -1642,9 +1640,7 @@
    2.24          // Put into notified DB
    2.25          status = set_reset_contact_notified(session, revoke_fpr, user_id);
    2.26          if (status != PEP_STATUS_OK)
    2.27 -            goto pep_free;
    2.28 -            
    2.29 -        curr_id_ptr = curr_id_ptr->next;
    2.30 +            goto pep_free;            
    2.31      }
    2.32      
    2.33  pep_free:
    2.34 @@ -3956,27 +3952,24 @@
    2.35          
    2.36      PEP_STATUS status = PEP_STATUS_OK;
    2.37          
    2.38 -    char* fpr = NULL;
    2.39 +    char* fpr_copy = NULL;
    2.40      char* own_id = NULL;
    2.41      identity_list* key_idents = NULL;
    2.42      stringlist_t* keys = NULL;
    2.43      
    2.44 -    if (key_id) {
    2.45 -        fpr = strdup(key_id);
    2.46 -        if (!fpr)
    2.47 +    if (!EMPTYSTR(key_id)) {
    2.48 +        fpr_copy = strdup(key_id);
    2.49 +        if (!fpr_copy)
    2.50              return PEP_OUT_OF_MEMORY;
    2.51      }
    2.52          
    2.53      if (!ident) {
    2.54          // Get list of own identities
    2.55          status = get_default_own_userid(session, &own_id);
    2.56 -        if (status != PEP_STATUS_OK) {
    2.57 -            free(fpr);
    2.58 -            return status;
    2.59 -        }
    2.60 -        if (fpr)
    2.61 -            status = get_identities_by_main_key_id(session, fpr, &key_idents);
    2.62 -        else {
    2.63 +        if (status != PEP_STATUS_OK)
    2.64 +            goto pep_free;
    2.65 +            
    2.66 +        if (EMPTYSTR(fpr_copy)) {
    2.67              status = get_all_keys_for_user(session, own_id, &keys);
    2.68              if (status == PEP_STATUS_OK) {
    2.69                  stringlist_t* curr_key;
    2.70 @@ -3987,7 +3980,11 @@
    2.71                  }
    2.72              }
    2.73              goto pep_free;
    2.74 -        }
    2.75 +        } // otherwise, we have a specific fpr to process
    2.76 +
    2.77 +        // fpr_copy exists, so... let's go.
    2.78 +        // Process own identities with this fpr
    2.79 +        status = get_identities_by_main_key_id(session, fpr_copy, &key_idents);
    2.80          
    2.81          if (status == PEP_STATUS_OK) {
    2.82              // have ident list, or should
    2.83 @@ -3995,86 +3992,84 @@
    2.84              for (curr_ident = key_idents; curr_ident && curr_ident->ident; 
    2.85                   curr_ident = curr_ident->next) {
    2.86                  pEp_identity* this_identity = curr_ident->ident;
    2.87 -                status = key_reset(session, fpr, this_identity);
    2.88 +                status = key_reset(session, fpr_copy, this_identity);
    2.89                  if (status != PEP_STATUS_OK)
    2.90                      break;                    
    2.91              }
    2.92          }
    2.93          goto pep_free;
    2.94      }
    2.95 -    else {        
    2.96 +    else { // an identity was specified.       
    2.97          if (is_me(session, ident)) {            
    2.98              // FIXME: make sure this IS our fpr?
    2.99 -            if (!fpr) {
   2.100 -                if (ident->fpr)
   2.101 -                    fpr = strdup(ident->fpr);
   2.102 +            
   2.103 +            // If it got sent in with an empty fpr...
   2.104 +            if (EMPTYSTR(fpr_copy)) {
   2.105 +                //
   2.106 +                // if (!EMPTYSTR(ident->fpr))
   2.107 +                //     fpr_copy = strdup(ident->fpr);
   2.108 +                status = _myself(session, ident, false, true);
   2.109 +                if (status == PEP_STATUS_OK && ident->fpr)
   2.110 +                    fpr_copy = strdup(ident->fpr);
   2.111                  else {
   2.112 -                    // Note: this will choke if we've already revoked. Is that
   2.113 -                    // Ok? Or do we need per-identity revokes? Best practice
   2.114 -                    // is to always send in a damned fpr! ;)
   2.115 -                    status = _myself(session, ident, false, true);
   2.116 -                    if (status == PEP_STATUS_OK && ident->fpr) {
   2.117 -                        fpr = strdup(ident->fpr);
   2.118 -                    }
   2.119 -                    else {
   2.120 -                        // last resort?
   2.121 -                        // Get list of own identities
   2.122 -                        char* own_id = NULL;
   2.123 -                        status = get_default_own_userid(session, &own_id);
   2.124 -                        if (status == PEP_STATUS_OK)
   2.125 -                            status = get_user_default_key(session, own_id, &fpr);
   2.126 -                        if (status != PEP_STATUS_OK || !fpr)  {
   2.127 -                            free(own_id);
   2.128 -                            return (status == PEP_STATUS_OK ? PEP_KEY_NOT_FOUND : status);
   2.129 -                        }
   2.130 +                    // last resort?
   2.131 +                    // Get list of own identities
   2.132 +                    char* own_id = NULL;
   2.133 +                    status = get_default_own_userid(session, &own_id);
   2.134 +                    if (status == PEP_STATUS_OK)
   2.135 +                        status = get_user_default_key(session, own_id, &fpr_copy);
   2.136 +                    if (status != PEP_STATUS_OK || EMPTYSTR(fpr_copy))  {
   2.137 +                        free(own_id);
   2.138 +                        return (status == PEP_STATUS_OK ? PEP_KEY_NOT_FOUND : status);
   2.139                      }
   2.140                  }
   2.141              }
   2.142 -            
   2.143 -            // We now have an fpr. Be careful - it may have been an input fpr, 
   2.144 -            // and it may be connected to the ident    
   2.145 -            char* fpr_backup = ident->fpr;
   2.146 -            ident->fpr = fpr;            
   2.147 +                        
   2.148 +//            char* fpr_backup = ident->fpr;
   2.149 +            free(ident->fpr);
   2.150 +            ident->fpr = fpr_copy;            
   2.151              // Create revocation
   2.152 -            status = revoke_key(session, fpr, NULL);
   2.153 +            status = revoke_key(session, fpr_copy, NULL);
   2.154              // mistrust fpr from trust
   2.155              if (status == PEP_STATUS_OK)
   2.156                  status = key_mistrusted(session, ident);
   2.157              // Remove fpr from ALL identities
   2.158              // Remove fpr from ALL users    
   2.159              if (status == PEP_STATUS_OK)
   2.160 -                status = remove_fpr_as_default(session, ident->fpr);
   2.161 +                status = remove_fpr_as_default(session, fpr_copy);
   2.162              if (status == PEP_STATUS_OK)
   2.163 -                status = add_mistrusted_key(session, ident->fpr);
   2.164 +                status = add_mistrusted_key(session, fpr_copy);
   2.165              // generate new key
   2.166 -            if (status == PEP_STATUS_OK)
   2.167 +            if (status == PEP_STATUS_OK) {
   2.168 +                ident->fpr = NULL;
   2.169                  status = generate_keypair(session, ident);
   2.170 -            // add to revocation list
   2.171 +            }
   2.172 +            // add to revocation list (ident->fpr is now the NEW key)
   2.173              if (status == PEP_STATUS_OK) 
   2.174 -                status = set_revoked(session, fpr, ident->fpr, time(NULL));
   2.175 +                status = set_revoked(session, fpr_copy, ident->fpr, time(NULL));
   2.176              
   2.177              // for all active communication partners:
   2.178              //      active_send revocation
   2.179              if (status == PEP_STATUS_OK)
   2.180 -                status = send_key_reset_to_recents(session, fpr, ident->fpr);
   2.181 +                status = send_key_reset_to_recents(session, fpr_copy, ident->fpr);
   2.182              
   2.183 -            ident->fpr = fpr_backup;
   2.184 +//            ident->fpr = fpr_backup;
   2.185          }
   2.186          else { // not is_me
   2.187              // remove fpr from all identities
   2.188              // remove fpr from all users
   2.189              if (status == PEP_STATUS_OK)
   2.190 -                status = remove_fpr_as_default(session, fpr);
   2.191 +                status = remove_fpr_as_default(session, fpr_copy);
   2.192              // delete key from key ring
   2.193              if (status == PEP_STATUS_OK)
   2.194 -                status = delete_keypair(session, fpr);
   2.195 +                status = delete_keypair(session, fpr_copy);
   2.196              // N.B. If this key is being replaced by something else, it
   2.197              // is done outside of this function.    
   2.198          }
   2.199      }
   2.200      
   2.201  pep_free:
   2.202 -    free(fpr);
   2.203 +    free(fpr_copy);
   2.204      free(own_id);
   2.205      free_identity_list(key_idents);
   2.206      free_stringlist(keys);    
     3.1 --- a/src/pEpEngine.c	Mon Aug 27 15:15:59 2018 +0200
     3.2 +++ b/src/pEpEngine.c	Fri Aug 31 14:50:54 2018 +0200
     3.3 @@ -111,7 +111,7 @@
     3.4      "   timestamp desc; ";
     3.5  
     3.6  static const char *sql_get_identities_by_main_key_id =  
     3.7 -    "select address, user_id, username, comm_type, lang,"
     3.8 +    "select address, identity.user_id, username, comm_type, lang,"
     3.9      "   identity.flags | pgp_keypair.flags,"
    3.10      "   is_own"
    3.11      "   from identity"
    3.12 @@ -119,7 +119,7 @@
    3.13      "   join pgp_keypair on fpr = identity.main_key_id"
    3.14      "   join trust on id = trust.user_id"
    3.15      "       and pgp_keypair_fpr = identity.main_key_id"    
    3.16 -    "   where main_key_id = ?1" 
    3.17 +    "   where identity.main_key_id = ?1" 
    3.18      "   order by is_own desc, "
    3.19      "   timestamp desc; ";
    3.20  
    3.21 @@ -3693,6 +3693,15 @@
    3.22      if (!(session && fpr))
    3.23          return PEP_ILLEGAL_VALUE;
    3.24  
    3.25 +    // Check to see first if it is revoked
    3.26 +    bool revoked = false;
    3.27 +    PEP_STATUS status = key_revoked(session, fpr, &revoked);
    3.28 +    if (status != PEP_STATUS_OK)
    3.29 +        return status;
    3.30 +        
    3.31 +    if (revoked)
    3.32 +        return PEP_STATUS_OK;
    3.33 +
    3.34      return session->cryptotech[PEP_crypt_OpenPGP].revoke_key(session, fpr,
    3.35              reason);
    3.36  }
     4.1 --- a/test/Makefile	Mon Aug 27 15:15:59 2018 +0200
     4.2 +++ b/test/Makefile	Fri Aug 31 14:50:54 2018 +0200
     4.3 @@ -48,7 +48,7 @@
     4.4  	LLDB_BIN=lldb
     4.5  endif
     4.6  
     4.7 -LDLIBS += -lcpptest
     4.8 +LDLIBS+= -lcpptest
     4.9  
    4.10  
    4.11  # Create a list of the extra library paths for the loader. I do not assume that the engine (and its dependencies) are installed for testing.
     5.1 --- a/test/include/KeyResetMessageTests.h	Mon Aug 27 15:15:59 2018 +0200
     5.2 +++ b/test/include/KeyResetMessageTests.h	Fri Aug 31 14:50:54 2018 +0200
     5.3 @@ -6,17 +6,35 @@
     5.4  
     5.5  #include <string>
     5.6  #include "EngineTestIndividualSuite.h"
     5.7 +#include "sync.h"
     5.8 +#include "pEpEngine.h"
     5.9  
    5.10  using namespace std;
    5.11  
    5.12  class KeyResetMessageTests : public EngineTestIndividualSuite {
    5.13      public:
    5.14          KeyResetMessageTests(string test_suite, string test_home_dir);
    5.15 +        
    5.16 +        static PEP_STATUS message_send_callback(void *obj, message *msg);
    5.17 +        
    5.18 +        vector<message*> m_queue;
    5.19 +        
    5.20 +        static constexpr const char* alice_fpr = "4ABE3AAF59AC32CFE4F86500A9411D176FF00E97";
    5.21 +
    5.22      protected:
    5.23 -        char* sender_revoked_key_fpr;
    5.24 -        char* recip_revoked_key_fpr;
    5.25 +        void setup();
    5.26 +                
    5.27      private:
    5.28          void check_key_reset_message();        
    5.29 +        void check_reset_key_and_notify();
    5.30 +        void check_receive_revoked();
    5.31 +        void check_receive_key_reset_private();
    5.32 +        void check_receive_key_reset_wrong_signer();
    5.33 +        void check_receive_key_reset_unsigned();
    5.34 +        void check_receive_message_to_revoked_key();   
    5.35 +        
    5.36 +        void send_setup();
    5.37 +        void receive_setup();     
    5.38  };
    5.39  
    5.40  #endif
     6.1 --- a/test/include/test_util.h	Mon Aug 27 15:15:59 2018 +0200
     6.2 +++ b/test/include/test_util.h	Fri Aug 31 14:50:54 2018 +0200
     6.3 @@ -13,6 +13,16 @@
     6.4  
     6.5  bool file_exists(std::string filename);
     6.6  
     6.7 +PEP_STATUS read_file_and_import_key(PEP_SESSION session, const char* fname);
     6.8 +PEP_STATUS set_up_ident_from_scratch(PEP_SESSION session, 
     6.9 +                                     const char* key_fname,
    6.10 +                                     const char* address,
    6.11 +                                     const char* fpr,
    6.12 +                                     const char* user_id,
    6.13 +                                     const char* username,
    6.14 +                                     pEp_identity** ret_ident,
    6.15 +                                     bool is_priv);
    6.16 +
    6.17  // string equality (case and non-case sensitive)
    6.18  bool _streq(const char* str1, const char* str2);
    6.19  bool _strceq(const char* str1, const char* str2);
     7.1 --- a/test/src/SuiteMaker.cc	Mon Aug 27 15:15:59 2018 +0200
     7.2 +++ b/test/src/SuiteMaker.cc	Fri Aug 31 14:50:54 2018 +0200
     7.3 @@ -47,6 +47,7 @@
     7.4  #include "EncryptAttachPrivateKeyTests.h"
     7.5  #include "DecryptAttachPrivateKeyTrustedTests.h"
     7.6  #include "IdentityListTests.h"
     7.7 +#include "KeyResetMessageTests.h"
     7.8  #include "UserIDAliasTests.h"
     7.9  
    7.10  
    7.11 @@ -86,11 +87,12 @@
    7.12      "EncryptAttachPrivateKeyTests",
    7.13      "DecryptAttachPrivateKeyTrustedTests",
    7.14      "IdentityListTests",
    7.15 +    "KeyResetMessageTests",
    7.16      "UserIDAliasTests",
    7.17  };
    7.18  
    7.19  // This file is generated, so magic constants are ok.
    7.20 -int SuiteMaker::num_suites = 36;
    7.21 +int SuiteMaker::num_suites = 37;
    7.22  
    7.23  void SuiteMaker::suitemaker_build(const char* test_class_name, const char* test_home, Test::Suite** test_suite) {
    7.24      if (strcmp(test_class_name, "MapAsn1Tests") == 0)
    7.25 @@ -163,6 +165,8 @@
    7.26          *test_suite = new DecryptAttachPrivateKeyTrustedTests(test_class_name, test_home);
    7.27      else if (strcmp(test_class_name, "IdentityListTests") == 0)
    7.28          *test_suite = new IdentityListTests(test_class_name, test_home);
    7.29 +    else if (strcmp(test_class_name, "KeyResetMessageTests") == 0)
    7.30 +        *test_suite = new KeyResetMessageTests(test_class_name, test_home);
    7.31      else if (strcmp(test_class_name, "UserIDAliasTests") == 0)
    7.32          *test_suite = new UserIDAliasTests(test_class_name, test_home);
    7.33  }
     8.1 --- a/test/src/engine_tests/KeyResetMessageTests.cc	Mon Aug 27 15:15:59 2018 +0200
     8.2 +++ b/test/src/engine_tests/KeyResetMessageTests.cc	Fri Aug 31 14:50:54 2018 +0200
     8.3 @@ -3,9 +3,12 @@
     8.4  
     8.5  #include <stdlib.h>
     8.6  #include <string>
     8.7 +#include <assert.h>
     8.8  
     8.9  #include "pEpEngine.h"
    8.10 +#include "pEp_internal.h"
    8.11  
    8.12 +#include "test_util.h"
    8.13  #include "EngineTestIndividualSuite.h"
    8.14  #include "KeyResetMessageTests.h"
    8.15  
    8.16 @@ -15,9 +18,167 @@
    8.17      EngineTestIndividualSuite::EngineTestIndividualSuite(suitename, test_home_dir) {
    8.18      add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("KeyResetMessageTests::check_key_reset_message"),
    8.19                                                                        static_cast<Func>(&KeyResetMessageTests::check_key_reset_message)));
    8.20 +    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("KeyResetMessageTests::check_reset_key_and_notify"),
    8.21 +                                                                      static_cast<Func>(&KeyResetMessageTests::check_reset_key_and_notify)));
    8.22 +    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("KeyResetMessageTests::check_receive_revoked"),
    8.23 +                                                                      static_cast<Func>(&KeyResetMessageTests::check_receive_revoked)));
    8.24 +    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("KeyResetMessageTests::check_receive_key_reset_private"),
    8.25 +                                                                      static_cast<Func>(&KeyResetMessageTests::check_receive_key_reset_private)));
    8.26 +    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("KeyResetMessageTests::check_receive_key_reset_wrong_signer"),
    8.27 +                                                                      static_cast<Func>(&KeyResetMessageTests::check_receive_key_reset_wrong_signer)));
    8.28 +    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("KeyResetMessageTests::check_receive_key_reset_unsigned"),
    8.29 +                                                                      static_cast<Func>(&KeyResetMessageTests::check_receive_key_reset_unsigned)));
    8.30 +    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("KeyResetMessageTests::check_receive_message_to_revoked_key"),
    8.31 +                                                                      static_cast<Func>(&KeyResetMessageTests::check_receive_message_to_revoked_key)));
    8.32 +}
    8.33 +
    8.34 +PEP_STATUS KeyResetMessageTests::message_send_callback(void* obj, message* msg) {
    8.35 +    ((KeyResetMessageTests*)obj)->m_queue.push_back(msg);
    8.36 +    return PEP_STATUS_OK;    
    8.37 +}
    8.38 +
    8.39 +void KeyResetMessageTests::setup() {
    8.40 +    EngineTestIndividualSuite::setup();
    8.41 +    session->sync_obj = this;
    8.42 +    session->messageToSend = &KeyResetMessageTests::message_send_callback;
    8.43 +}
    8.44 +
    8.45 +void KeyResetMessageTests::send_setup() {
    8.46 +    // Setup own identity
    8.47 +    PEP_STATUS status = read_file_and_import_key(session,
    8.48 +                "test_keys/pub/pep-test-alice-0x6FF00E97_pub.asc");
    8.49 +    assert(status == PEP_STATUS_OK);
    8.50 +    status = set_up_ident_from_scratch(session,
    8.51 +                "test_keys/priv/pep-test-alice-0x6FF00E97_priv.asc",
    8.52 +                "pep.test.alice@pep-project.org", alice_fpr, 
    8.53 +                PEP_OWN_USERID, "Alice in Wonderland", NULL, true
    8.54 +            );
    8.55 +    assert(status == PEP_STATUS_OK);
    8.56 +    
    8.57 +    status = set_up_ident_from_scratch(session,
    8.58 +                "test_keys/pub/pep-test-bob-0xC9C2EE39_pub.asc",
    8.59 +                "pep.test.bob@pep-project.org", NULL, "BobId", "Bob's Burgers",
    8.60 +                NULL, false
    8.61 +            );
    8.62 +    assert(status == PEP_STATUS_OK);
    8.63 +            
    8.64 +    status = set_up_ident_from_scratch(session,
    8.65 +                "test_keys/pub/pep-test-carol-0x42A85A42_pub.asc",
    8.66 +                "pep-test-carol@pep-project.org", NULL, "carolId", "Carol Burnett",
    8.67 +                NULL, false
    8.68 +            );
    8.69 +    assert(status == PEP_STATUS_OK);
    8.70 +    
    8.71 +    status = set_up_ident_from_scratch(session,
    8.72 +                "test_keys/pub/pep-test-dave-0xBB5BCCF6_pub.asc",
    8.73 +                "pep-test-dave@pep-project.org", NULL, "DaveId", 
    8.74 +                "David Hasselhoff (Germans Love Me)", NULL, false
    8.75 +            );
    8.76 +    assert(status == PEP_STATUS_OK);
    8.77 +
    8.78 +    status = set_up_ident_from_scratch(session,
    8.79 +                "test_keys/pub/pep-test-erin-0x9F8D7CBA_pub.asc",
    8.80 +                "pep-test-erin@pep-project.org", NULL, "ErinErinErin", 
    8.81 +                "Éirinn go Brách", NULL, false
    8.82 +            );
    8.83 +    assert(status == PEP_STATUS_OK);
    8.84 +
    8.85 +    status = set_up_ident_from_scratch(session,
    8.86 +                "test_keys/pub/pep.test.fenris-0x4F3D2900_pub.asc",
    8.87 +                "pep.test.fenris@thisstilldoesntwork.lu", NULL, "BadWolf", 
    8.88 +                "Fenris Leto Hawke", NULL, false
    8.89 +            );
    8.90 +    assert(status == PEP_STATUS_OK);
    8.91 +}
    8.92 +
    8.93 +void KeyResetMessageTests::receive_setup() {
    8.94 +    
    8.95  }
    8.96  
    8.97  void KeyResetMessageTests::check_key_reset_message() {
    8.98      TEST_ASSERT(true);
    8.99  }
   8.100  
   8.101 +void KeyResetMessageTests::check_reset_key_and_notify() {
   8.102 +    send_setup();
   8.103 +    
   8.104 +    pEp_identity* from_ident = new_identity("pep.test.alice@pep-project.org", NULL, PEP_OWN_USERID, NULL);
   8.105 +    PEP_STATUS status = myself(session, from_ident); 
   8.106 +    TEST_ASSERT_MSG(status == PEP_STATUS_OK, tl_status_string(status));
   8.107 +    TEST_ASSERT_MSG(from_ident->fpr && strcasecmp(from_ident->fpr, alice_fpr) == 0,
   8.108 +                    from_ident->fpr);
   8.109 +    TEST_ASSERT(from_ident->me);
   8.110 +    
   8.111 +    // "send" some messages to update the social graph entries
   8.112 +    identity_list* send_idents = 
   8.113 +        new_identity_list(
   8.114 +            new_identity("pep.test.bob@pep-project.org", 
   8.115 +                         NULL, "BobId", "Bob's Burgers"));
   8.116 +                         
   8.117 +    identity_list_add(send_idents, new_identity("pep-test-carol@pep-project.org", NULL, NULL, NULL));    
   8.118 +    identity_list_add(send_idents, new_identity("pep-test-dave@pep-project.org", NULL, NULL, NULL)); 
   8.119 +    identity_list_add(send_idents, new_identity("pep-test-erin@pep-project.org", NULL, NULL, NULL)); 
   8.120 +    identity_list_add(send_idents, new_identity("pep.test.fenris@thisstilldoesntwork.lu", NULL, NULL, NULL)); 
   8.121 +    
   8.122 +    cout << "Creating outgoing message to update DB" << endl;
   8.123 +    message* outgoing_msg = new_message(PEP_dir_outgoing);
   8.124 +    TEST_ASSERT(outgoing_msg);
   8.125 +    outgoing_msg->from = from_ident;
   8.126 +    outgoing_msg->to = send_idents;
   8.127 +    outgoing_msg->shortmsg = strdup("Well isn't THIS a useless message...");
   8.128 +    outgoing_msg->longmsg = strdup("Hi Mom...\n");
   8.129 +    outgoing_msg->attachments = new_bloblist(NULL, 0, "application/octet-stream", NULL);
   8.130 +    cout << "Message created.\n\n";
   8.131 +    cout << "Encrypting message as MIME multipart…\n";
   8.132 +    message* enc_outgoing_msg = nullptr;
   8.133 +    cout << "Calling encrypt_message()\n";
   8.134 +    status = encrypt_message(session, outgoing_msg, NULL, &enc_outgoing_msg, PEP_enc_PGP_MIME, 0);
   8.135 +    TEST_ASSERT_MSG((status == PEP_STATUS_OK), tl_status_string(status));
   8.136 +    TEST_ASSERT(enc_outgoing_msg);
   8.137 +    cout << "Message encrypted.\n";
   8.138 +        
   8.139 +    // If this all worked, we should have a list of recent guys in our DB which, when we reset Alice's 
   8.140 +    // key, will get sent some nice key reset messages.
   8.141 +    // But... we need to have one look like an older message. So. Time to mess with the DB.
   8.142 +    // Dave is our victim. Because friend called Dave, who is actually a nice dude, but it amuses me.
   8.143 +    // (Note: said friend is NOT David Hasselhoff. To my knowledge. Hi Dave!)
   8.144 +    //
   8.145 +    // update identity
   8.146 +    //      set timestamp = 661008730
   8.147 +    //      where address = "pep-test-dave@pep-project.org"
   8.148 +    int int_result = sqlite3_exec(
   8.149 +        session->db,
   8.150 +        "update identity "
   8.151 +        "   set timestamp = 661008730 "
   8.152 +        "   where address = 'pep-test-dave@pep-project.org' ;",
   8.153 +        NULL,
   8.154 +        NULL,
   8.155 +        NULL
   8.156 +    );
   8.157 +    TEST_ASSERT(int_result == SQLITE_OK);
   8.158 +    
   8.159 +    status = key_reset(session, alice_fpr, from_ident);
   8.160 +    TEST_ASSERT_MSG((status == PEP_STATUS_OK), tl_status_string(status));
   8.161 +    
   8.162 +    TEST_ASSERT(true);
   8.163 +}
   8.164 +
   8.165 +void KeyResetMessageTests::check_receive_revoked() {
   8.166 +    TEST_ASSERT(true);
   8.167 +}
   8.168 +
   8.169 +void KeyResetMessageTests::check_receive_key_reset_private() {
   8.170 +    TEST_ASSERT(true);
   8.171 +}
   8.172 +
   8.173 +void KeyResetMessageTests::check_receive_key_reset_wrong_signer() {
   8.174 +    TEST_ASSERT(true);
   8.175 +}
   8.176 +
   8.177 +void KeyResetMessageTests::check_receive_key_reset_unsigned() {
   8.178 +    TEST_ASSERT(true);
   8.179 +}
   8.180 +
   8.181 +void KeyResetMessageTests::check_receive_message_to_revoked_key() {
   8.182 +    TEST_ASSERT(true);
   8.183 +}
     9.1 --- a/test/src/util/test_util.cc	Mon Aug 27 15:15:59 2018 +0200
     9.2 +++ b/test/src/util/test_util.cc	Fri Aug 31 14:50:54 2018 +0200
     9.3 @@ -2,6 +2,7 @@
     9.4  #include "pEpEngine.h"
     9.5  #include "pEp_internal.h"
     9.6  #include "message_api.h"
     9.7 +#include "test_util.h"
     9.8  
     9.9  #include <fstream>
    9.10  #include <sstream>
    9.11 @@ -13,6 +14,52 @@
    9.12  #include <unistd.h>
    9.13  #include <ftw.h>
    9.14  
    9.15 +PEP_STATUS read_file_and_import_key(PEP_SESSION session, const char* fname) {
    9.16 +    const std::string key = slurp(fname);
    9.17 +    PEP_STATUS status = (key.empty() ? PEP_KEY_NOT_FOUND : PEP_STATUS_OK);
    9.18 +    if (status == PEP_STATUS_OK)
    9.19 +        status = import_key(session, key.c_str(), key.size(), NULL);
    9.20 +    return status;    
    9.21 +}
    9.22 +
    9.23 +PEP_STATUS set_up_ident_from_scratch(PEP_SESSION session,
    9.24 +                                     const char* key_fname,
    9.25 +                                     const char* address,
    9.26 +                                     const char* fpr,
    9.27 +                                     const char* user_id,
    9.28 +                                     const char* username,
    9.29 +                                     pEp_identity** ret_ident,
    9.30 +                                     bool is_priv) {
    9.31 +    PEP_STATUS status = read_file_and_import_key(session,key_fname);
    9.32 +    if (status != PEP_STATUS_OK)
    9.33 +        return status;
    9.34 +    
    9.35 +    pEp_identity* ident = new_identity(address, fpr, user_id, username);
    9.36 +    if (is_priv && fpr) {
    9.37 +        status = set_own_key(session, ident, fpr);
    9.38 +        if (status == PEP_STATUS_OK)
    9.39 +            status = myself(session, ident);
    9.40 +    }
    9.41 +    else    
    9.42 +        status = update_identity(session, ident);
    9.43 +
    9.44 +    if (status != PEP_STATUS_OK)
    9.45 +        goto pep_free;
    9.46 +        
    9.47 +    if (!ident || !ident->fpr) {
    9.48 +        status = PEP_CANNOT_FIND_IDENTITY;
    9.49 +        goto pep_free;
    9.50 +    }
    9.51 +    
    9.52 +    if (ret_ident)
    9.53 +        *ret_ident = ident;
    9.54 +        
    9.55 +pep_free:
    9.56 +    if (!ret_ident)
    9.57 +        free_identity(ident);
    9.58 +}
    9.59 +
    9.60 +
    9.61  bool file_exists(std::string filename) {
    9.62      struct stat buffer;
    9.63      return (stat(filename.c_str(), &buffer) == 0);