ENGINE-463: the code seems to work, but I've broken the tests that force the error condition I want to examine, and I'm too tired to fix them this late without breaking more things. Use at your own risk atm. ENGINE-463
authorKrista Bennett <krista@pep-project.org>
Fri, 05 Oct 2018 00:57:35 +0200
branchENGINE-463
changeset 29956f2f9a7c060e
parent 2994 fd487cf6d90e
child 2997 ee1d19039d8d
ENGINE-463: the code seems to work, but I've broken the tests that force the error condition I want to examine, and I'm too tired to fix them this late without breaking more things. Use at your own risk atm.
src/keymanagement.c
src/pEpEngine.c
src/pEpEngine.h
test/convenience_scripts/keygen_for_test.py
test/include/UserIdCollisionTests.h
test/include/test_util.h
test/src/SuiteMaker.cc
test/src/engine_tests/UserIdCollisionTests.cc
test/src/util/test_util.cc
     1.1 --- a/src/keymanagement.c	Thu Oct 04 13:13:06 2018 +0200
     1.2 +++ b/src/keymanagement.c	Fri Oct 05 00:57:35 2018 +0200
     1.3 @@ -577,6 +577,8 @@
     1.4                              else if (input_is_TOFU && !curr_is_TOFU) {
     1.5                                  // Replace ruthlessly - this is NOT supposed to happen.
     1.6                                  // BAD APP BEHAVIOUR.
     1.7 +                                free(identity->user_id);
     1.8 +                                identity->user_id = strdup(this_id->user_id);
     1.9                                  stored_ident = this_id;
    1.10                                  // FIXME: free list.
    1.11                                  break;                                
     2.1 --- a/src/pEpEngine.c	Thu Oct 04 13:13:06 2018 +0200
     2.2 +++ b/src/pEpEngine.c	Fri Oct 05 00:57:35 2018 +0200
     2.3 @@ -2950,22 +2950,23 @@
     2.4          return PEP_ILLEGAL_VALUE;
     2.5  
     2.6      *trust_list = NULL;
     2.7 -    labeled_int_list_t* t_list = new_labeled_int_list(0, NULL); // empty
     2.8 +    labeled_int_list_t* t_list = NULL;
     2.9  
    2.10      sqlite3_reset(session->get_trust_by_userid);
    2.11      sqlite3_bind_text(session->get_trust_by_userid, 1, user_id, -1, SQLITE_STATIC);
    2.12  
    2.13      while ((result = sqlite3_step(session->get_trust_by_userid)) == SQLITE_ROW) {
    2.14 -        labeled_int_list_add(t_list, sqlite3_column_int(session->get_trust_by_userid, 1),
    2.15 -                            (const char *) sqlite3_column_text(session->get_trust_by_userid, 0));
    2.16 +        if (!t_list)
    2.17 +            t_list = new_labeled_int_list(sqlite3_column_int(session->get_trust_by_userid, 1),
    2.18 +                                         (const char *) sqlite3_column_text(session->get_trust_by_userid, 0));
    2.19 +        else
    2.20 +            labeled_int_list_add(t_list, sqlite3_column_int(session->get_trust_by_userid, 1),
    2.21 +                                (const char *) sqlite3_column_text(session->get_trust_by_userid, 0));
    2.22      }
    2.23  
    2.24      sqlite3_reset(session->get_trust_by_userid);
    2.25  
    2.26 -    if (!t_list->label)
    2.27 -        free_labeled_int_list(t_list);
    2.28 -    else
    2.29 -        *trust_list = t_list;
    2.30 +    *trust_list = t_list;
    2.31          
    2.32      return PEP_STATUS_OK;
    2.33  }
    2.34 @@ -3113,6 +3114,14 @@
    2.35      labeled_int_list_t* trust_list = NULL;
    2.36      stringlist_t* touched_keys = new_stringlist(NULL);
    2.37      char* main_user_fpr = NULL;
    2.38 +                
    2.39 +    bool new_is_pep = false;
    2.40 +    new_ident = new_identity(NULL, NULL, new_uid, NULL);
    2.41 +    status = is_pep_user(session, new_ident, &new_is_pep);
    2.42 +    if (status != PEP_STATUS_OK)
    2.43 +        goto pEp_free;
    2.44 +    free(new_ident);
    2.45 +    new_ident = NULL;
    2.46          
    2.47      status = get_identities_by_userid(session, old_uid, &old_identities);
    2.48      if (status == PEP_STATUS_OK && old_identities) {
    2.49 @@ -3129,6 +3138,12 @@
    2.50                      status = PEP_OUT_OF_MEMORY;
    2.51                      goto pEp_free;
    2.52                  }
    2.53 +                if (new_is_pep) {
    2.54 +                    PEP_comm_type confirmed_bit = old_ident->comm_type & PEP_ct_confirmed;
    2.55 +                    if ((old_ident->comm_type | PEP_ct_confirmed) == PEP_ct_OpenPGP)
    2.56 +                        old_ident->comm_type = PEP_ct_pEp_unconfirmed | confirmed_bit;
    2.57 +                }
    2.58 +                
    2.59                  status = set_identity(session, old_ident);
    2.60                  if (status != PEP_STATUS_OK)
    2.61                      goto pEp_free;
    2.62 @@ -3197,6 +3212,12 @@
    2.63          if (status == PEP_STATUS_OK) {
    2.64              new_ident->comm_type = reconcile_trust(trust_curr->value,
    2.65                                                     new_ident->comm_type);
    2.66 +            if (new_is_pep) {
    2.67 +                PEP_comm_type confirmed_bit = new_ident->comm_type & PEP_ct_confirmed;
    2.68 +                if ((new_ident->comm_type | PEP_ct_confirmed) == PEP_ct_OpenPGP)
    2.69 +                    new_ident->comm_type = PEP_ct_pEp_unconfirmed | confirmed_bit;
    2.70 +            }
    2.71 +
    2.72              status = set_trust(session, new_ident);
    2.73              if (status != PEP_STATUS_OK) {
    2.74                  goto pEp_free;
     3.1 --- a/src/pEpEngine.h	Thu Oct 04 13:13:06 2018 +0200
     3.2 +++ b/src/pEpEngine.h	Fri Oct 05 00:57:35 2018 +0200
     3.3 @@ -1281,6 +1281,10 @@
     3.4  
     3.5  PEP_STATUS set_pgp_keypair(PEP_SESSION session, const char* fpr);
     3.6  
     3.7 +// exposed for testing
     3.8 +PEP_STATUS set_person(PEP_SESSION session, pEp_identity* identity,
     3.9 +                      bool guard_transaction);
    3.10 +
    3.11  #ifdef __cplusplus
    3.12  }
    3.13  #endif
     4.1 --- a/test/convenience_scripts/keygen_for_test.py	Thu Oct 04 13:13:06 2018 +0200
     4.2 +++ b/test/convenience_scripts/keygen_for_test.py	Fri Oct 05 00:57:35 2018 +0200
     4.3 @@ -1,5 +1,5 @@
     4.4  import argparse
     4.5 -import gnupg
     4.6 +from pretty_bad_protocol import gnupg
     4.7  import os
     4.8  import subprocess
     4.9  
    4.10 @@ -19,10 +19,11 @@
    4.11  homedir = os.path.join(os.path.expanduser('~'),"gnupg")
    4.12  print("GNUPGHOME=" + homedir + "\n")
    4.13  
    4.14 +
    4.15  try:
    4.16 -    gpg = gnupg.GPG(gnupghome=homedir) 
    4.17 +    gpg = gnupg.GPG(binary='/usr/bin/gpg', gnupghome=homedir) 
    4.18  except TypeError:
    4.19 -    gpg = gnupg.GPG(homedir=homedir)
    4.20 +    gpg = gnupg.GPG(binary='/usr/bin/gpg', homedir=homedir)
    4.21  
    4.22  name = args.real_name_prefix
    4.23  email = args.email_address_prefix
    4.24 @@ -61,7 +62,7 @@
    4.25              email = e_split[0] + "_" + i_str + "@" + e_split[1]
    4.26  
    4.27      print("Generating key data for " + name + " " + email + "\n")
    4.28 -    input_data = gpg.gen_key_input(key_type="RSA", key_length=2048, subkey_type="RSA", subkey_length=2048, expire_date=0, name_real=name, name_email=email, password="")
    4.29 +    input_data = gpg.gen_key_input(key_type="RSA", key_length=2048, expire_date=0, name_real=name, name_email=email)
    4.30      if not input_data:
    4.31          raise Exception('Input data not created in iteration ' + str(i))
    4.32      
     5.1 --- a/test/include/UserIdCollisionTests.h	Thu Oct 04 13:13:06 2018 +0200
     5.2 +++ b/test/include/UserIdCollisionTests.h	Fri Oct 05 00:57:35 2018 +0200
     5.3 @@ -12,8 +12,101 @@
     5.4  class UserIdCollisionTests : public EngineTestIndividualSuite {
     5.5      public:
     5.6          UserIdCollisionTests(string test_suite, string test_home_dir);
     5.7 +    
     5.8 +    protected:
     5.9 +        void setup();
    5.10 +        void tear_down();
    5.11 +        
    5.12 +        // own user
    5.13 +        pEp_identity* user_alice;
    5.14 +        // real ID, only minimal info w/ username
    5.15 +        pEp_identity* real_alex;
    5.16 +        // tofu ID, only minimal info w/ username        
    5.17 +        pEp_identity* tofu_alex;
    5.18 +        // real ID, only minimal info w/o username
    5.19 +        pEp_identity* real_alex_0;
    5.20 +        // tofu ID, only minimal info w/o username
    5.21 +        pEp_identity* tofu_alex_0;
    5.22 +        // real ID, only minimal info w/o username
    5.23 +        pEp_identity* real_alex_1;
    5.24 +        // tofu ID, only minimal info w/o username
    5.25 +        pEp_identity* tofu_alex_1;
    5.26 +        // real ID, various kinds of info
    5.27 +        pEp_identity* real_alex_2;
    5.28 +        // tofu ID, various kinds of info
    5.29 +        pEp_identity* tofu_alex_2;
    5.30 +        // real ID, various kinds of info
    5.31 +        pEp_identity* real_alex_3;
    5.32 +        // tofu ID, various kinds of info        
    5.33 +        pEp_identity* tofu_alex_3;
    5.34 +        // tofu ID, various kinds of info        
    5.35 +        pEp_identity* tofu_alex_4;
    5.36 +        // real ID, various kinds of info
    5.37 +        pEp_identity* real_alex_4;
    5.38 +        // tofu ID, various kinds of info        
    5.39 +        pEp_identity* tofu_alex_5;
    5.40 +        // real ID, various kinds of info
    5.41 +        pEp_identity* real_alex_5;
    5.42 +        // tofu ID, various kinds of info        
    5.43 +        pEp_identity* tofu_alex_6a;
    5.44 +        // real ID, various kinds of info
    5.45 +        pEp_identity* real_alex_6a;
    5.46 +        // tofu ID, various kinds of info        
    5.47 +        pEp_identity* tofu_alex_6b;
    5.48 +        // real ID, various kinds of info
    5.49 +        pEp_identity* real_alex_6b;
    5.50 +        // tofu ID, various kinds of info        
    5.51 +        pEp_identity* tofu_alex_6c;
    5.52 +        // real ID, various kinds of info
    5.53 +        pEp_identity* real_alex_6c;
    5.54 +        // tofu ID, various kinds of info        
    5.55 +        pEp_identity* tofu_alex_6d;
    5.56 +        // real ID, various kinds of info
    5.57 +        pEp_identity* real_alex_6d;
    5.58 +        
    5.59 +        const char* const alice_keyfile = "test_keys/pub/pep-test-alice-0x6FF00E97_pub.asc";
    5.60 +        const char* const alice_keyid = "4ABE3AAF59AC32CFE4F86500A9411D176FF00E97";
    5.61 +        const char* const alice_email = "pep.test.alice@pep-project.org";
    5.62 +        const char* const alex_keyfile = "test_keys/pub/pep.test.alexander-0x26B54E4E_pub.asc";
    5.63 +        const char* const alex_keyid = "3AD9F60FAEB22675DB873A1362D6981326B54E4E";        
    5.64 +        const char* const alex_email = "pep.test.alexander@peptest.ch";        
    5.65 +        const char* const alex0_keyfile = "test_keys/pub/pep.test.alexander0-0x3B7302DB_pub.asc";
    5.66 +        const char* const alex0_keyid = "F4598A17D4690EB3B5B0F6A344F04E963B7302DB";
    5.67 +        const char* const alex0_email = "pep.test.alexander0@darthmama.org";                
    5.68 +        const char* const alex1_keyfile = "test_keys/pub/pep.test.alexander1-0x541260F6_pub.asc";
    5.69 +        const char* const alex1_keyid = "59AF4C51492283522F6904531C09730A541260F6";        
    5.70 +        const char* const alex1_email = "pep.test.alexander1@darthmama.org";                                
    5.71 +        const char* const alex2_keyfile = "test_keys/pub/pep.test.alexander2-0xA6512F30_pub.asc";
    5.72 +        const char* const alex2_keyid = "46A994F19077C05610870273C4B8AB0BA6512F30";
    5.73 +        const char* const alex2_email = "pep.test.alexander2@darthmama.org";                                
    5.74 +        const char* const alex3_keyfile = "test_keys/pub/pep.test.alexander3-0x724B3975_pub.asc";
    5.75 +        const char* const alex3_keyid = "5F7076BBD92E14EA49F0DF7C2CE49419724B3975";        
    5.76 +        const char* const alex3_email = "pep.test.alexander3@darthmama.org";                
    5.77 +        const char* const alex4_keyfile = "test_keys/pub/pep.test.alexander4-0x844B9DCF_pub.asc";
    5.78 +        const char* const alex4_keyid = "E95FFF95B8E2FDD4A12C3374395F1485844B9DCF";        
    5.79 +        const char* const alex4_email = "pep.test.alexander4@darthmama.org";                
    5.80 +        const char* const alex5_keyfile = "test_keys/pub/pep.test.alexander5-0x0773CD29_pub.asc";
    5.81 +        const char* const alex5_keyid = "58BCC2BF2AE1E3C4FBEAB89AD7838ACA0773CD29";        
    5.82 +        const char* const alex5_email = "pep.test.alexander5@darthmama.org";                
    5.83 +        const char* const alex6a_keyfile = "test_keys/pub/pep.test.alexander6-0xBDA17020_pub.asc";
    5.84 +        const char* const alex6a_keyid = "B4CE2F6947B6947C500F0687AEFDE530BDA17020";        
    5.85 +        const char* const alex6_email = "pep.test.alexander6@darthmama.org";                
    5.86 +        const char* const alex6b_keyfile = "test_keys/pub/pep.test.alexander6-0x503B14D8_pub.asc";
    5.87 +        const char* const alex6b_keyid = "2E21325D202A44BFD9C607FCF095B202503B14D8";        
    5.88 +        const char* const alex6c_keyfile = "test_keys/pub/pep.test.alexander6-0xA216E95A_pub.asc";
    5.89 +        const char* const alex6c_keyid = "3C1E713D8519D7F907E3142D179EAA24A216E95A";        
    5.90 +        const char* const alex6d_keyfile = "test_keys/pub/pep.test.alexander6-0x0019697D_pub.asc";
    5.91 +        const char* const alex6d_keyid = "74D79B4496E289BD8A71B70BA8E2C4530019697D";        
    5.92 +        
    5.93      private:
    5.94 -        void check_user_id_collision();
    5.95 +        void simple_tofu_collision();        
    5.96 +        void simple_tofu_collision_different_usernames();
    5.97 +        void tofu_collision_two_tofus();        
    5.98 +        void tofu_collision_two_tofus_diff_usernames();
    5.99 +        void real_followed_by_explicit_tofu();
   5.100 +        void merge_records_normal();
   5.101 +        void merge_records_set();
   5.102 +        void merge_records_set_2();
   5.103  };
   5.104  
   5.105  #endif
     6.1 --- a/test/include/test_util.h	Thu Oct 04 13:13:06 2018 +0200
     6.2 +++ b/test/include/test_util.h	Fri Oct 05 00:57:35 2018 +0200
     6.3 @@ -37,6 +37,8 @@
     6.4  // Grabs a new uuid for your randomish string needs.
     6.5  char* get_new_uuid();
     6.6  
     6.7 +bool slurp_and_import_key(PEP_SESSION session, const char* key_filename);
     6.8 +
     6.9  bool slurp_message_and_import_key(PEP_SESSION session, const char* message_fname, std::string& message, const char* key_filename);
    6.10  
    6.11  int util_delete_filepath(const char *filepath, 
     7.1 --- a/test/src/SuiteMaker.cc	Thu Oct 04 13:13:06 2018 +0200
     7.2 +++ b/test/src/SuiteMaker.cc	Fri Oct 05 00:57:35 2018 +0200
     7.3 @@ -13,6 +13,7 @@
     7.4  
     7.5  // Begin where we generate stuff
     7.6  #include "DecorateTests.h"
     7.7 +#include "UserIdCollisionTests.h"
     7.8  #include "ReencryptPlusExtraKeysTests.h"
     7.9  #include "BlacklistTests.h"
    7.10  #include "AppleMailTests.h"
    7.11 @@ -54,6 +55,7 @@
    7.12  
    7.13  const char* SuiteMaker::all_suites[] = {
    7.14      "DecorateTests",
    7.15 +    "UserIdCollisionTests",
    7.16      "ReencryptPlusExtraKeysTests",
    7.17      "BlacklistTests",
    7.18      "AppleMailTests",
    7.19 @@ -94,11 +96,13 @@
    7.20  };
    7.21  
    7.22  // This file is generated, so magic constants are ok.
    7.23 -int SuiteMaker::num_suites = 38;
    7.24 +int SuiteMaker::num_suites = 39;
    7.25  
    7.26  void SuiteMaker::suitemaker_build(const char* test_class_name, const char* test_home, Test::Suite** test_suite) {
    7.27      if (strcmp(test_class_name, "DecorateTests") == 0)
    7.28          *test_suite = new DecorateTests(test_class_name, test_home);
    7.29 +    else if (strcmp(test_class_name, "UserIdCollisionTests") == 0)
    7.30 +        *test_suite = new UserIdCollisionTests(test_class_name, test_home);
    7.31      else if (strcmp(test_class_name, "ReencryptPlusExtraKeysTests") == 0)
    7.32          *test_suite = new ReencryptPlusExtraKeysTests(test_class_name, test_home);
    7.33      else if (strcmp(test_class_name, "BlacklistTests") == 0)
     8.1 --- a/test/src/engine_tests/UserIdCollisionTests.cc	Thu Oct 04 13:13:06 2018 +0200
     8.2 +++ b/test/src/engine_tests/UserIdCollisionTests.cc	Fri Oct 05 00:57:35 2018 +0200
     8.3 @@ -3,9 +3,11 @@
     8.4  
     8.5  #include <stdlib.h>
     8.6  #include <string>
     8.7 +#include <cstring>
     8.8  
     8.9  #include "pEpEngine.h"
    8.10  
    8.11 +#include "test_util.h"
    8.12  #include "EngineTestIndividualSuite.h"
    8.13  #include "UserIdCollisionTests.h"
    8.14  
    8.15 @@ -13,11 +15,322 @@
    8.16  
    8.17  UserIdCollisionTests::UserIdCollisionTests(string suitename, string test_home_dir) :
    8.18      EngineTestIndividualSuite::EngineTestIndividualSuite(suitename, test_home_dir) {
    8.19 -    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("UserIdCollisionTests::check_user_id_collision"),
    8.20 -                                                                      static_cast<Func>(&UserIdCollisionTests::check_user_id_collision)));
    8.21 +    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("UserIdCollisionTests::simple_tofu_collision"),
    8.22 +                                                                      static_cast<Func>(&UserIdCollisionTests::simple_tofu_collision)));                                                                  
    8.23 +    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("UserIdCollisionTests::simple_tofu_collision_different_usernames"),
    8.24 +                                                                      static_cast<Func>(&UserIdCollisionTests::simple_tofu_collision_different_usernames)));                                                                                                                                        
    8.25 +    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("UserIdCollisionTests::tofu_collision_two_tofus"),
    8.26 +                                                                      static_cast<Func>(&UserIdCollisionTests::tofu_collision_two_tofus)));                                                                                                                                                                                                              
    8.27 +    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("UserIdCollisionTests::tofu_collision_two_tofus_diff_usernames"),
    8.28 +                                                                      static_cast<Func>(&UserIdCollisionTests::tofu_collision_two_tofus_diff_usernames)));
    8.29 +    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("UserIdCollisionTests::real_followed_by_explicit_tofu"),
    8.30 +                                                                      static_cast<Func>(&UserIdCollisionTests::real_followed_by_explicit_tofu)));
    8.31 +    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("UserIdCollisionTests::merge_records_normal"),
    8.32 +                                                                      static_cast<Func>(&UserIdCollisionTests::merge_records_normal)));                                                                  
    8.33 +    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("UserIdCollisionTests::merge_records_set"),
    8.34 +                                                                      static_cast<Func>(&UserIdCollisionTests::merge_records_set)));                                                                  
    8.35 +    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("UserIdCollisionTests::merge_records_set_2"),
    8.36 +                                                                      static_cast<Func>(&UserIdCollisionTests::merge_records_set_2)));                                                                                                                                        
    8.37  }
    8.38  
    8.39 -void UserIdCollisionTests::check_user_id_collision() {
    8.40 -    TEST_ASSERT(true);
    8.41 +void UserIdCollisionTests::setup() {
    8.42 +    EngineTestIndividualSuite::setup();
    8.43 +    user_alice = new_identity(alice_email, alice_keyid, PEP_OWN_USERID, "Alice from Mel's Diner");
    8.44 +    slurp_and_import_key(session, alice_keyfile);    
    8.45 +    
    8.46 +    real_alex = new_identity(alex_email, alex_keyid, "AlexID", "Alexander the Mediocre");
    8.47 +    
    8.48 +    tofu_alex = new_identity(alex_email, alex_keyid, NULL, NULL);
    8.49 +
    8.50 +    real_alex_0 = new_identity(alex_email, alex_keyid, "Alex0", "Alexander the Mediocre");
    8.51 +    
    8.52 +    tofu_alex_0 = new_identity(alex_email, alex_keyid, NULL, NULL);
    8.53 +    
    8.54 +    real_alex_1 = new_identity(alex1_email, alex_keyid, "Alex1", NULL);
    8.55 +    
    8.56 +    tofu_alex_1 = new_identity(alex1_email, alex1_keyid, NULL, NULL);
    8.57 +
    8.58 +    real_alex_2 = new_identity(alex2_email, alex1_keyid, "Alex2", NULL);
    8.59 +
    8.60 +    tofu_alex_2 = new_identity(alex2_email, alex2_keyid, NULL, NULL);
    8.61 +
    8.62 +    real_alex_3 = new_identity(alex3_email, alex2_keyid, "Alex3", NULL);
    8.63 +
    8.64 +    tofu_alex_3 = new_identity(alex3_email, alex3_keyid, NULL, NULL);
    8.65 +
    8.66 +    real_alex_4 = new_identity(alex4_email, alex3_keyid, "Alex4", NULL);
    8.67 +
    8.68 +    tofu_alex_4 = new_identity(alex4_email, alex4_keyid, NULL, NULL);
    8.69 +
    8.70 +    real_alex_5 = new_identity(alex5_email, alex4_keyid, "Alex5", NULL);
    8.71 +
    8.72 +    tofu_alex_5 = new_identity(alex5_email, alex5_keyid, NULL, NULL);
    8.73 +
    8.74 +    real_alex_6a = new_identity(alex6_email, alex5_keyid, "Alex6", NULL);
    8.75 +
    8.76 +    tofu_alex_6a = new_identity(alex6_email, alex6a_keyid, NULL, NULL);
    8.77 +
    8.78 +    real_alex_6b = new_identity(alex6_email, alex6a_keyid, "Alex6", NULL);
    8.79 +
    8.80 +    tofu_alex_6b = new_identity(alex6_email, alex6b_keyid, NULL, NULL);
    8.81 +
    8.82 +    real_alex_6c = new_identity(alex6_email, alex6b_keyid, "Alex6", NULL);
    8.83 +
    8.84 +    tofu_alex_6c = new_identity(alex6_email, alex6c_keyid, NULL, NULL);
    8.85 +
    8.86 +    real_alex_6d = new_identity(alex6_email, alex6c_keyid, "Alex6", NULL);
    8.87 +
    8.88 +    tofu_alex_6d = new_identity(alex6_email, alex6d_keyid, NULL, NULL);
    8.89  }
    8.90  
    8.91 +void UserIdCollisionTests::tear_down() {
    8.92 +    free_identity(real_alex);
    8.93 +    free_identity(real_alex_0);
    8.94 +    free_identity(real_alex_1);
    8.95 +    free_identity(real_alex_2);
    8.96 +    free_identity(real_alex_3);
    8.97 +    free_identity(real_alex_4);
    8.98 +    free_identity(real_alex_5);
    8.99 +    free_identity(real_alex_6a);
   8.100 +    free_identity(real_alex_6b);
   8.101 +    free_identity(real_alex_6c);
   8.102 +    free_identity(real_alex_6d);
   8.103 +    free_identity(tofu_alex);
   8.104 +    free_identity(tofu_alex_0);
   8.105 +    free_identity(tofu_alex_1);
   8.106 +    free_identity(tofu_alex_2);
   8.107 +    free_identity(tofu_alex_3);
   8.108 +    free_identity(tofu_alex_4);
   8.109 +    free_identity(tofu_alex_5);
   8.110 +    free_identity(tofu_alex_6a);
   8.111 +    free_identity(tofu_alex_6b);
   8.112 +    free_identity(tofu_alex_6c);
   8.113 +    free_identity(tofu_alex_6d);
   8.114 +    EngineTestIndividualSuite::tear_down();    
   8.115 +}
   8.116 +
   8.117 +void UserIdCollisionTests::simple_tofu_collision() {
   8.118 +    slurp_and_import_key(session,alex_keyfile);
   8.119 +    tofu_alex->username = strdup("Alexander the Mediocre");
   8.120 +    PEP_STATUS status = update_identity(session, tofu_alex);
   8.121 +    TEST_ASSERT(status == PEP_STATUS_OK);
   8.122 +    TEST_ASSERT_MSG(strcmp(tofu_alex->fpr, alex_keyid) == 0, tofu_alex->fpr);
   8.123 +    string tofu_id = string("TOFU_") + alex_email;
   8.124 +    TEST_ASSERT_MSG(strcmp(tofu_alex->user_id, tofu_id.c_str()) == 0, tofu_alex->user_id);    
   8.125 +    status = update_identity(session, real_alex);
   8.126 +    TEST_ASSERT(status == PEP_STATUS_OK);
   8.127 +    TEST_ASSERT_MSG(strcmp(real_alex->fpr, alex_keyid) == 0, real_alex->fpr);
   8.128 +    bool tofu_still_exists = false;
   8.129 +    status = exists_person(session, tofu_alex, &tofu_still_exists);
   8.130 +    TEST_ASSERT(status == PEP_STATUS_OK);
   8.131 +    TEST_ASSERT(!tofu_still_exists);    
   8.132 +}
   8.133 +
   8.134 +void UserIdCollisionTests::simple_tofu_collision_different_usernames() {
   8.135 +    slurp_and_import_key(session,alex_keyfile);
   8.136 +    tofu_alex->username = strdup("Alexander Hamilton");
   8.137 +    PEP_STATUS status = update_identity(session, tofu_alex);
   8.138 +    TEST_ASSERT(status == PEP_STATUS_OK);
   8.139 +    TEST_ASSERT_MSG(strcmp(tofu_alex->fpr, alex_keyid) == 0, tofu_alex->fpr);
   8.140 +    string tofu_id = string("TOFU_") + alex_email;
   8.141 +    TEST_ASSERT_MSG(strcmp(tofu_alex->user_id, tofu_id.c_str()) == 0, tofu_alex->user_id);    
   8.142 +    status = update_identity(session, real_alex);
   8.143 +    TEST_ASSERT(status == PEP_STATUS_OK);
   8.144 +    TEST_ASSERT_MSG(strcmp(real_alex->fpr, alex_keyid) == 0, real_alex->fpr);
   8.145 +    bool tofu_still_exists = false;
   8.146 +    status = exists_person(session, tofu_alex, &tofu_still_exists);
   8.147 +    TEST_ASSERT(status == PEP_STATUS_OK);
   8.148 +    // SHOULD still exist, because we don't replace when usernames differ
   8.149 +    TEST_ASSERT(tofu_still_exists);    
   8.150 +}
   8.151 +
   8.152 +void UserIdCollisionTests::tofu_collision_two_tofus() {
   8.153 +    slurp_and_import_key(session,alex6a_keyfile);
   8.154 +    
   8.155 +    tofu_alex_6a->username = strdup("Alexander Hamilton");
   8.156 +    tofu_alex_6b->username = strdup("Alexander Hamilton");
   8.157 +
   8.158 +    tofu_alex_6a->lang[0] = 'j';
   8.159 +    tofu_alex_6a->lang[1] = 'p';
   8.160 +    
   8.161 +    PEP_STATUS status = update_identity(session, tofu_alex_6a);
   8.162 +    TEST_ASSERT(status == PEP_STATUS_OK);
   8.163 +    TEST_ASSERT_MSG(strcmp(tofu_alex_6a->fpr, alex6a_keyid) == 0, tofu_alex_6a->fpr);
   8.164 +    string tofu_id = string("TOFU_") + alex6_email;
   8.165 +    TEST_ASSERT_MSG(strcmp(tofu_alex_6a->user_id, tofu_id.c_str()) == 0, tofu_alex_6a->user_id);    
   8.166 +
   8.167 +    // Ok, NOW we put in an explicit TOFU
   8.168 +    tofu_alex_6b->user_id = strdup(tofu_id.c_str());
   8.169 +    status = update_identity(session, tofu_alex_6b);
   8.170 +    TEST_ASSERT(status == PEP_STATUS_OK);
   8.171 +    TEST_ASSERT_MSG(strcmp(tofu_alex_6b->fpr, alex6a_keyid) == 0, tofu_alex_6b->fpr);
   8.172 +    bool tofu_still_exists = false;
   8.173 +    status = exists_person(session, tofu_alex_6a, &tofu_still_exists);
   8.174 +    TEST_ASSERT(status == PEP_STATUS_OK);
   8.175 +    // SHOULD still exist, because we don't replace when usernames differ
   8.176 +    TEST_ASSERT(tofu_still_exists);    
   8.177 +    TEST_ASSERT(tofu_alex_6b->lang[0] == 'j');
   8.178 +    TEST_ASSERT(tofu_alex_6b->lang[1] == 'p');    
   8.179 +}
   8.180 +
   8.181 +void UserIdCollisionTests::tofu_collision_two_tofus_diff_usernames() {
   8.182 +    slurp_and_import_key(session,alex6a_keyfile);
   8.183 +    
   8.184 +    tofu_alex_6a->username = strdup("Alexander Hamilton");
   8.185 +    tofu_alex_6b->username = strdup("Alexander the Not-Cool-At-All");
   8.186 +
   8.187 +    tofu_alex_6a->lang[0] = 'j';
   8.188 +    tofu_alex_6a->lang[1] = 'p';
   8.189 +    
   8.190 +    PEP_STATUS status = update_identity(session, tofu_alex_6a);
   8.191 +    TEST_ASSERT(status == PEP_STATUS_OK);
   8.192 +    TEST_ASSERT_MSG(strcmp(tofu_alex_6a->fpr, alex6a_keyid) == 0, tofu_alex_6a->fpr);
   8.193 +    string tofu_id = string("TOFU_") + alex6_email;
   8.194 +    TEST_ASSERT_MSG(strcmp(tofu_alex_6a->user_id, tofu_id.c_str()) == 0, tofu_alex_6a->user_id);    
   8.195 +
   8.196 +    // Ok, NOW we put in an explicit TOFU
   8.197 +    tofu_alex_6b->user_id = strdup(tofu_id.c_str());
   8.198 +    status = update_identity(session, tofu_alex_6b);
   8.199 +    TEST_ASSERT(status == PEP_STATUS_OK);
   8.200 +    TEST_ASSERT_MSG(strcmp(tofu_alex_6b->fpr, alex6a_keyid) == 0, tofu_alex_6b->fpr);
   8.201 +    bool tofu_still_exists = false;
   8.202 +    status = exists_person(session, tofu_alex_6a, &tofu_still_exists);
   8.203 +    TEST_ASSERT(status == PEP_STATUS_OK);
   8.204 +    // SHOULD still exist, because we don't replace when usernames differ
   8.205 +    TEST_ASSERT(tofu_still_exists);    
   8.206 +    TEST_ASSERT(tofu_alex_6b->lang[0] == 'j');
   8.207 +    TEST_ASSERT(tofu_alex_6b->lang[1] == 'p');    
   8.208 +    TEST_ASSERT(strcmp(tofu_alex_6b->username,"Alexander the Not-Cool-At-All") == 0);    
   8.209 +}
   8.210 +
   8.211 +void UserIdCollisionTests::real_followed_by_explicit_tofu() {
   8.212 +    slurp_and_import_key(session,alex_keyfile);
   8.213 +    real_alex->username = strdup("Alexander the Mediocre");
   8.214 +    PEP_STATUS status = update_identity(session, real_alex);
   8.215 +    TEST_ASSERT(status == PEP_STATUS_OK);
   8.216 +    TEST_ASSERT_MSG(strcmp(real_alex->fpr, alex_keyid) == 0, real_alex->fpr);
   8.217 +    string tofu_id = string("TOFU_") + alex_email;
   8.218 +    tofu_alex->username = strdup(real_alex->username);
   8.219 +    tofu_alex->user_id = strdup(tofu_id.c_str());
   8.220 +    status = update_identity(session, tofu_alex);
   8.221 +    TEST_ASSERT(status == PEP_STATUS_OK);
   8.222 +    TEST_ASSERT_MSG(strcmp(tofu_alex->user_id, "AlexID") == 0, tofu_alex->user_id);
   8.223 +    bool tofu_still_exists = false;
   8.224 +    free(tofu_alex->user_id);
   8.225 +    tofu_alex->user_id = strdup(tofu_id.c_str());    
   8.226 +    status = exists_person(session, tofu_alex, &tofu_still_exists);
   8.227 +    TEST_ASSERT(status == PEP_STATUS_OK);
   8.228 +    TEST_ASSERT(!tofu_still_exists);    
   8.229 +}
   8.230 +
   8.231 +void UserIdCollisionTests::merge_records_normal() {
   8.232 +    // Tofu 6a has lots of stuff.
   8.233 +    slurp_and_import_key(session,alex6a_keyfile);    
   8.234 +    tofu_alex_6a->username = strdup("Alexander Hamilton");
   8.235 +    tofu_alex_6a->lang[0] = 'e';
   8.236 +    tofu_alex_6a->lang[1] = 's';
   8.237 +    PEP_STATUS status = update_identity(session, tofu_alex_6a);
   8.238 +    slurp_and_import_key(session,alex6c_keyfile);
   8.239 +    free(tofu_alex_6a->fpr);
   8.240 +    tofu_alex_6a->fpr = strdup(alex6c_keyid);
   8.241 +    tofu_alex_6a->comm_type = PEP_ct_OpenPGP;    
   8.242 +    status = set_identity(session, tofu_alex_6a);
   8.243 +    slurp_and_import_key(session,alex6d_keyfile);
   8.244 +    free(tofu_alex_6a->fpr);
   8.245 +    tofu_alex_6a->fpr = strdup(alex6d_keyid);
   8.246 +    tofu_alex_6a->comm_type = PEP_ct_pEp_unconfirmed; // ???
   8.247 +    status = set_identity(session, tofu_alex_6a);
   8.248 +    real_alex_6a->username = strdup(tofu_alex_6a->username);
   8.249 +    status = update_identity(session, real_alex_6a);                        
   8.250 +    TEST_ASSERT(status == PEP_STATUS_OK);
   8.251 +    TEST_ASSERT(real_alex_6a->lang[0] = 'e');
   8.252 +    TEST_ASSERT_MSG(real_alex_6a->comm_type == PEP_ct_pEp_unconfirmed, tl_ct_string(real_alex_6a->comm_type));
   8.253 +    free(real_alex_6a->fpr);
   8.254 +    real_alex_6a->fpr = strdup(alex6c_keyid);
   8.255 +    status = get_trust(session, real_alex_6a);
   8.256 +    TEST_ASSERT(status == PEP_STATUS_OK);
   8.257 +    TEST_ASSERT_MSG(real_alex_6a->comm_type == PEP_ct_OpenPGP, tl_ct_string(real_alex_6a->comm_type));    
   8.258 +}
   8.259 +
   8.260 +void UserIdCollisionTests::merge_records_set() {
   8.261 +    // Tofu 6a has lots of stuff.
   8.262 +    slurp_and_import_key(session,alex6a_keyfile);    
   8.263 +    tofu_alex_6a->username = strdup("Alexander Hamilton");
   8.264 +    tofu_alex_6a->lang[0] = 'e';
   8.265 +    tofu_alex_6a->lang[1] = 's';
   8.266 +    PEP_STATUS status = update_identity(session, tofu_alex_6a);
   8.267 +    slurp_and_import_key(session,alex6b_keyfile);        
   8.268 +    slurp_and_import_key(session,alex6c_keyfile);
   8.269 +    free(tofu_alex_6a->fpr);
   8.270 +    tofu_alex_6a->fpr = strdup(alex6c_keyid);
   8.271 +    tofu_alex_6a->comm_type = PEP_ct_pEp_unconfirmed;    
   8.272 +    status = set_identity(session, tofu_alex_6a);
   8.273 +    status = set_as_pep_user(session, tofu_alex_6a);     
   8.274 +    slurp_and_import_key(session,alex6d_keyfile);
   8.275 +    free(tofu_alex_6a->fpr);
   8.276 +    tofu_alex_6a->fpr = strdup(alex6d_keyid);
   8.277 +    tofu_alex_6a->comm_type = PEP_ct_OpenPGP;
   8.278 +    status = set_identity(session, tofu_alex_6a);    
   8.279 +    real_alex_6a->username = strdup(tofu_alex_6a->username);
   8.280 +    free(real_alex_6a->fpr);
   8.281 +    real_alex_6a->fpr = strdup(alex6d_keyid);
   8.282 +    status = set_person(session, real_alex_6a, true); // NOT identit
   8.283 +    TEST_ASSERT(status == PEP_STATUS_OK);   
   8.284 +    status = update_identity(session, real_alex_6a);
   8.285 +    TEST_ASSERT(status == PEP_STATUS_OK);
   8.286 +    TEST_ASSERT(real_alex_6a->lang[0] = 'e');
   8.287 +    TEST_ASSERT_MSG(real_alex_6a->comm_type == PEP_ct_pEp, tl_ct_string(real_alex_6a->comm_type));    
   8.288 +    bool pEp_peep = false;
   8.289 +    status = is_pep_user(session, real_alex_6a, &pEp_peep);
   8.290 +    TEST_ASSERT(pEp_peep);
   8.291 +    free(real_alex_6a->fpr);
   8.292 +    real_alex_6a->fpr = strdup(alex6c_keyid);
   8.293 +    status = get_trust(session, real_alex_6a);
   8.294 +    TEST_ASSERT(real_alex_6a->comm_type == PEP_ct_pEp_unconfirmed);
   8.295 +    free(real_alex_6a->fpr);
   8.296 +    real_alex_6a->fpr = strdup(alex6d_keyid);
   8.297 +    status = get_trust(session, real_alex_6a);
   8.298 +    TEST_ASSERT(real_alex_6a->comm_type == PEP_ct_pEp);    
   8.299 +}
   8.300 +
   8.301 +void UserIdCollisionTests::merge_records_set_2() {
   8.302 +    // Tofu 6a has lots of stuff.
   8.303 +    slurp_and_import_key(session,alex6a_keyfile);    
   8.304 +    tofu_alex_6a->username = strdup("Alexander Hamilton");
   8.305 +    tofu_alex_6a->lang[0] = 'e';
   8.306 +    tofu_alex_6a->lang[1] = 's';
   8.307 +    PEP_STATUS status = update_identity(session, tofu_alex_6a);
   8.308 +    slurp_and_import_key(session,alex6b_keyfile);        
   8.309 +    slurp_and_import_key(session,alex6c_keyfile);
   8.310 +    free(tofu_alex_6a->fpr);
   8.311 +    tofu_alex_6a->fpr = strdup(alex6c_keyid);
   8.312 +    tofu_alex_6a->comm_type = PEP_ct_OpenPGP_unconfirmed;    
   8.313 +    status = set_identity(session, tofu_alex_6a);
   8.314 +    slurp_and_import_key(session,alex6d_keyfile);
   8.315 +    free(tofu_alex_6a->fpr);
   8.316 +    tofu_alex_6a->fpr = strdup(alex6d_keyid);
   8.317 +    tofu_alex_6a->comm_type = PEP_ct_OpenPGP;
   8.318 +    status = set_identity(session, tofu_alex_6a);    
   8.319 +    real_alex_6a->username = strdup(tofu_alex_6a->username);
   8.320 +    free(real_alex_6a->fpr);
   8.321 +    real_alex_6a->fpr = strdup(alex6d_keyid);
   8.322 +    status = set_person(session, real_alex_6a, true); // NOT identity   
   8.323 +    TEST_ASSERT(status == PEP_STATUS_OK);
   8.324 +    status = set_as_pep_user(session, real_alex_6a);     
   8.325 +    TEST_ASSERT(status == PEP_STATUS_OK);    
   8.326 +    status = update_identity(session, real_alex_6a);
   8.327 +    TEST_ASSERT(status == PEP_STATUS_OK);
   8.328 +    TEST_ASSERT(real_alex_6a->lang[0] = 'e');
   8.329 +    TEST_ASSERT_MSG(real_alex_6a->comm_type == PEP_ct_pEp, tl_ct_string(real_alex_6a->comm_type));    
   8.330 +    bool pEp_peep = false;
   8.331 +    status = is_pep_user(session, real_alex_6a, &pEp_peep);
   8.332 +    TEST_ASSERT(pEp_peep);
   8.333 +    free(real_alex_6a->fpr);
   8.334 +    real_alex_6a->fpr = strdup(alex6c_keyid);
   8.335 +    status = get_trust(session, real_alex_6a);
   8.336 +    TEST_ASSERT(real_alex_6a->comm_type == PEP_ct_pEp_unconfirmed);
   8.337 +    free(real_alex_6a->fpr);
   8.338 +    real_alex_6a->fpr = strdup(alex6d_keyid);
   8.339 +    status = get_trust(session, real_alex_6a);
   8.340 +    TEST_ASSERT(real_alex_6a->comm_type == PEP_ct_pEp);    
   8.341 +}
     9.1 --- a/test/src/util/test_util.cc	Thu Oct 04 13:13:06 2018 +0200
     9.2 +++ b/test/src/util/test_util.cc	Fri Oct 05 00:57:35 2018 +0200
     9.3 @@ -336,14 +336,19 @@
     9.4      }
     9.5  }
     9.6  
     9.7 +bool slurp_and_import_key(PEP_SESSION session, const char* key_filename) {
     9.8 +    std::string keyfile = slurp(key_filename);
     9.9 +    if (import_key(session, keyfile.c_str(), keyfile.size(), NULL) != PEP_STATUS_OK)
    9.10 +        return false;
    9.11 +    return true;
    9.12 +}
    9.13 +
    9.14  bool slurp_message_and_import_key(PEP_SESSION session, const char* message_fname, std::string& message, const char* key_filename) {
    9.15 +    bool ok = true;
    9.16      message = slurp(message_fname);
    9.17 -    if (key_filename) {
    9.18 -        std::string keyfile = slurp(key_filename);
    9.19 -        if (import_key(session, keyfile.c_str(), keyfile.size(), NULL) != PEP_STATUS_OK)
    9.20 -            return false;
    9.21 -    }
    9.22 -    return true;
    9.23 +    if (key_filename)
    9.24 +        ok = slurp_and_import_key(session, key_filename);
    9.25 +    return ok;
    9.26  }
    9.27  
    9.28