Intermittent commit ENGINE-250
authorKrista Bennett <krista@pep-project.org>
Mon, 05 Feb 2018 10:58:03 +0100
branchENGINE-250
changeset 247745e8ed722fec
parent 2476 5a4da2a7f99b
child 2478 0460dfb7f3dc
Intermittent commit
src/keymanagement.c
src/message_api.c
src/message_api.h
src/pEpEngine.c
src/pEpEngine.h
test/Makefile
test/external_revoke_test.cc
test/new_update_id_and_myself_test.cc
test/test_util.cc
test/test_util.h
     1.1 --- a/src/keymanagement.c	Sun Feb 04 15:26:28 2018 +0100
     1.2 +++ b/src/keymanagement.c	Mon Feb 05 10:58:03 2018 +0100
     1.3 @@ -436,6 +436,9 @@
     1.4      
     1.5      transfer_ident_lang_and_flags(return_id, stored_ident);
     1.6      
     1.7 +    if (return_id->comm_type == PEP_ct_unknown)
     1.8 +        return_id->comm_type = PEP_ct_key_not_found;
     1.9 +    
    1.10      return status;
    1.11  }
    1.12  
    1.13 @@ -1219,6 +1222,10 @@
    1.14      if (input_default_ct < PEP_ct_strong_but_unconfirmed)
    1.15          return PEP_KEY_UNSUITABLE;
    1.16  
    1.17 +    status = set_pgp_keypair(session, ident->fpr);
    1.18 +    if (status != PEP_STATUS_OK)
    1.19 +        return status;
    1.20 +        
    1.21      // Save the input fpr
    1.22      char* cached_fpr = strdup(ident->fpr);
    1.23      ident->fpr = NULL;
     2.1 --- a/src/message_api.c	Sun Feb 04 15:26:28 2018 +0100
     2.2 +++ b/src/message_api.c	Mon Feb 05 10:58:03 2018 +0100
     2.3 @@ -1532,7 +1532,7 @@
     2.4          else
     2.5              _status = myself(session, _il->ident);
     2.6          if (_status != PEP_STATUS_OK) {
     2.7 -            status = _status;
     2.8 +            status = PEP_UNENCRYPTED;
     2.9              GOTO(pep_error);
    2.10          }
    2.11  
    2.12 @@ -1564,7 +1564,7 @@
    2.13              else
    2.14                  _status = myself(session, _il->ident);
    2.15              if (_status != PEP_STATUS_OK) {
    2.16 -                status = _status;
    2.17 +                status = PEP_UNENCRYPTED;
    2.18                  GOTO(pep_error);
    2.19              }
    2.20  
    2.21 @@ -1596,7 +1596,7 @@
    2.22                  _status = myself(session, _il->ident);
    2.23              if (_status != PEP_STATUS_OK)
    2.24              {
    2.25 -                status = _status;
    2.26 +                status = PEP_UNENCRYPTED;
    2.27                  GOTO(pep_error);
    2.28              }
    2.29  
    2.30 @@ -1609,7 +1609,6 @@
    2.31              }
    2.32              else {
    2.33                  dest_keys_found = false;
    2.34 -                status = PEP_KEY_NOT_FOUND;
    2.35              }
    2.36          }
    2.37      }
     3.1 --- a/src/message_api.h	Sun Feb 04 15:26:28 2018 +0100
     3.2 +++ b/src/message_api.h	Mon Feb 05 10:58:03 2018 +0100
     3.3 @@ -56,11 +56,8 @@
     3.4  //
     3.5  //  return value:
     3.6  //      PEP_STATUS_OK                   on success
     3.7 -//      PEP_KEY_NOT_FOUND               at least one of the receipient keys
     3.8 -//                                      could not be found
     3.9  //      PEP_KEY_HAS_AMBIG_NAME          at least one of the receipient keys has
    3.10  //                                      an ambiguous name
    3.11 -//      PEP_GET_KEY_FAILED              cannot retrieve key
    3.12  //      PEP_UNENCRYPTED                 on demand or no recipients with usable
    3.13  //                                      key, is left unencrypted, and key is
    3.14  //                                      attached to it
     4.1 --- a/src/pEpEngine.c	Sun Feb 04 15:26:28 2018 +0100
     4.2 +++ b/src/pEpEngine.c	Mon Feb 05 10:58:03 2018 +0100
     4.3 @@ -98,12 +98,23 @@
     4.4  // Set person, but if already exist, only update.
     4.5  // if main_key_id already set, don't touch.
     4.6  static const char *sql_set_person = 
     4.7 -    "insert or replace into person (id, username, lang, main_key_id, device_group)"
     4.8 -    "  values (?1, ?2, ?3,"
     4.9 -    "    (select coalesce((select main_key_id from person "
    4.10 -    "      where id = ?1), upper(replace(?4,' ','')))),"
    4.11 -    "    (select device_group from person where id = ?1)) ;";
    4.12 -
    4.13 +     "insert or ignore into person (id, username, lang, main_key_id, device_group)"
    4.14 +     "  values (?1, ?2, ?3,"
    4.15 +     "    (select coalesce( "
    4.16 +     "          (select main_key_id from person where id = ?1), " 
    4.17 +     "           upper(replace(?4,' ','')))),"     
    4.18 +     "    (select device_group from person where id = ?1)) ;"
    4.19 +    "update person "
    4.20 +    "   set username = ?2, "
    4.21 +    "       lang = ?3, "
    4.22 +    "       main_key_id =  "
    4.23 +    "           (select coalesce( "
    4.24 +    "               (select main_key_id from person where id = ?1), " 
    4.25 +    "                upper(replace(?4,' ','')))),"         
    4.26 +    "       device_group = "
    4.27 +    "           (select device_group from person where id = ?1)"
    4.28 +    "   where id = ?1 ;";
    4.29 +    
    4.30  static const char *sql_set_as_pep_user =
    4.31      "update person set is_pep_user = 1 "
    4.32      "   where id = ?1 ; ";
    4.33 @@ -151,17 +162,26 @@
    4.34      "where id = ?1;";
    4.35  
    4.36  static const char *sql_set_pgp_keypair = 
    4.37 -    "insert or replace into pgp_keypair (fpr) "
    4.38 +    "insert or ignore into pgp_keypair (fpr) "
    4.39      "values (upper(replace(?1,' ',''))) ;";
    4.40  
    4.41  static const char *sql_set_identity = 
    4.42 -    "insert or replace into identity ("
    4.43 -    " address, main_key_id, "
    4.44 -    " user_id, flags, is_own"
    4.45 -    ") values ("
    4.46 -    " ?1,"
    4.47 -    " upper(replace(?2,' ','')),"
    4.48 -    " ?3,"
    4.49 +    "insert or ignore into identity ("
    4.50 +    "       address, main_key_id, "
    4.51 +    "       user_id, flags, is_own"
    4.52 +    "   ) values ("
    4.53 +    "       ?1,"
    4.54 +    "       upper(replace(?2,' ','')),"
    4.55 +    "       ?3,"
    4.56 +    "       ?4,"
    4.57 +    "       ?5"
    4.58 +    "   );"
    4.59 +    "update identity "
    4.60 +    "   set main_key_id = upper(replace(?2,' ','')), "
    4.61 +    "       flags = ?4, " 
    4.62 +    "       is_own = ?5 "
    4.63 +    "   where address = ?1 and user_id = ?3 ;";
    4.64 +
    4.65      // " (select"
    4.66      // "   coalesce("
    4.67      // "    (select flags from identity"
    4.68 @@ -170,9 +190,8 @@
    4.69      // "    0)"
    4.70      // " ) | (?4 & 255)"
    4.71      /* set_identity ignores previous flags, and doesn't filter machine flags */
    4.72 -    " ?4,"
    4.73 -    " ?5"
    4.74 -    ");";
    4.75 +
    4.76 +
    4.77          
    4.78  static const char *sql_set_identity_flags = 
    4.79      "update identity set flags = "
    4.80 @@ -187,9 +206,11 @@
    4.81      "where address = ?2 and user_id = ?3 ;";
    4.82  
    4.83  static const char *sql_set_trust =
    4.84 -    "insert or replace into trust (user_id, pgp_keypair_fpr, comm_type) "
    4.85 -    "values (?1, upper(replace(?2,' ','')), ?3) ;";
    4.86 -
    4.87 +    "insert or ignore into trust (user_id, pgp_keypair_fpr, comm_type) "
    4.88 +    "values (?1, upper(replace(?2,' ','')), ?3) ;"
    4.89 +    "update trust set comm_type = ?3 " 
    4.90 +    "   where user_id = ?1 and pgp_keypair_fpr = upper(replace(?2,' ',''));";
    4.91 +    
    4.92  static const char *sql_update_trust_for_fpr =
    4.93      "update trust "
    4.94      "set comm_type = ?1 "
    4.95 @@ -222,7 +243,7 @@
    4.96  
    4.97  // blacklist
    4.98  static const char *sql_blacklist_add = 
    4.99 -    "insert or replace into blacklist_keys (fpr) values (upper(replace(?1,' ',''))) ;"
   4.100 +    "insert or ignore into blacklist_keys (fpr) values (upper(replace(?1,' ',''))) ;"
   4.101      "delete from identity where main_key_id = upper(replace(?1,' ','')) ;"
   4.102      "delete from pgp_keypair where fpr = upper(replace(?1,' ','')) ;";
   4.103  
   4.104 @@ -308,7 +329,7 @@
   4.105  
   4.106  // Revocation tracking
   4.107  static const char *sql_add_mistrusted_key =
   4.108 -    "insert or replace into mistrusted_keys (fpr) "
   4.109 +    "insert or ignore into mistrusted_keys (fpr) "
   4.110      "   values (upper(replace(?1,' ',''))) ;";
   4.111          
   4.112  static const char *sql_delete_mistrusted_key = 
   4.113 @@ -318,7 +339,7 @@
   4.114      "select count(*) from mistrusted_keys where fpr = upper(replace(?1,' ','')) ;";
   4.115  
   4.116  static const char *sql_add_userid_alias =
   4.117 -    "insert or replace into alternate_user_id (default_id, alternate_id) "
   4.118 +    "insert or ignore into alternate_user_id (default_id, alternate_id) "
   4.119      "values (?1, ?2) ;";
   4.120      
   4.121  static int user_version(void *_version, int count, char **text, char **name)
   4.122 @@ -384,6 +405,10 @@
   4.123      return retval;
   4.124  }
   4.125  
   4.126 +void errorLogCallback(void *pArg, int iErrCode, const char *zMsg){
   4.127 +  fprintf(stderr, "(%d) %s\n", iErrCode, zMsg);
   4.128 +}
   4.129 +
   4.130  DYNAMIC_API PEP_STATUS init(PEP_SESSION *session)
   4.131  {
   4.132      PEP_STATUS status = PEP_STATUS_OK;
   4.133 @@ -441,6 +466,8 @@
   4.134          status = PEP_INIT_CANNOT_OPEN_DB;
   4.135          goto pep_error;
   4.136      }
   4.137 +    
   4.138 +    sqlite3_config(SQLITE_CONFIG_LOG, errorLogCallback, NULL);
   4.139  
   4.140      int_result = sqlite3_open_v2(
   4.141              LOCAL_DB,
   4.142 @@ -584,8 +611,8 @@
   4.143                  ");\n"
   4.144                  // user id aliases
   4.145                  "create table if not exists alternate_user_id (\n"
   4.146 -                "    default_id text references person (id)\n"
   4.147 -                "       on delete cascade on update cascade,\n"
   4.148 +                "    default_id text references person (id),\n"
   4.149 +//                "       on delete cascade on update cascade,\n"
   4.150                  "    alternate_id text primary key\n"
   4.151                  ");\n"
   4.152                  // mistrusted keys
   4.153 @@ -623,6 +650,17 @@
   4.154              NULL,
   4.155              NULL);
   4.156          assert(int_result == SQLITE_OK);
   4.157 +
   4.158 +        int_result = sqlite3_exec(
   4.159 +            _session->db,
   4.160 +            "pragma foreign_keys=ON;\n",
   4.161 +            NULL,
   4.162 +            NULL,
   4.163 +            NULL
   4.164 +        );
   4.165 +
   4.166 +        assert(int_result == SQLITE_OK);
   4.167 +
   4.168          
   4.169          // Sometimes the user_version wasn't set correctly. Check to see if this
   4.170          // is really necessary...
   4.171 @@ -797,8 +835,8 @@
   4.172                      "\n"
   4.173                      "PRAGMA foreign_keys=on;\n"
   4.174                      "create table if not exists alternate_user_id (\n"
   4.175 -                    "    default_id text references person (id)\n"
   4.176 -                    "       on delete cascade on update cascade,\n"
   4.177 +                    "    default_id text references person (id),\n"
   4.178 +//                    "       on delete cascade on update cascade,\n"
   4.179                      "    alternate_id text primary key\n"
   4.180                      ");\n"
   4.181                      ,
   4.182 @@ -1888,6 +1926,25 @@
   4.183      return PEP_STATUS_OK;
   4.184  }
   4.185  
   4.186 +// FIXME: We can rollback in set_identity on the return status,
   4.187 +// so we should probably do that.
   4.188 +PEP_STATUS set_pgp_keypair(PEP_SESSION session, const char* fpr) {
   4.189 +    if (!session || EMPTYSTR(fpr))
   4.190 +        return PEP_ILLEGAL_VALUE;
   4.191 +        
   4.192 +    int result;
   4.193 +    
   4.194 +    sqlite3_reset(session->set_pgp_keypair);
   4.195 +    sqlite3_bind_text(session->set_pgp_keypair, 1, fpr, -1,
   4.196 +            SQLITE_STATIC);
   4.197 +    result = sqlite3_step(session->set_pgp_keypair);
   4.198 +    sqlite3_reset(session->set_pgp_keypair);
   4.199 +    if (result != SQLITE_DONE) {
   4.200 +        return PEP_CANNOT_SET_PGP_KEYPAIR;
   4.201 +    }
   4.202 +    
   4.203 +    return PEP_STATUS_OK;
   4.204 +}
   4.205  
   4.206  DYNAMIC_API PEP_STATUS set_identity(
   4.207          PEP_SESSION session, const pEp_identity *identity
   4.208 @@ -2768,6 +2825,10 @@
   4.209      if (status != PEP_STATUS_OK)
   4.210          return status;
   4.211  
   4.212 +    if (identity->fpr)
   4.213 +        status = set_pgp_keypair(session, identity->fpr);
   4.214 +
   4.215 +    // add to known keypair DB, as this might not end up being a default
   4.216      return status;
   4.217  }
   4.218  
     5.1 --- a/src/pEpEngine.h	Sun Feb 04 15:26:28 2018 +0100
     5.2 +++ b/src/pEpEngine.h	Mon Feb 05 10:58:03 2018 +0100
     5.3 @@ -1245,6 +1245,7 @@
     5.4  // the *person* table as a pEp user. This *does not check comm_type*                         
     5.5  PEP_STATUS is_pep_user(PEP_SESSION session, pEp_identity *identity, bool* is_pep);
     5.6  
     5.7 +PEP_STATUS set_pgp_keypair(PEP_SESSION session, const char* fpr);
     5.8  
     5.9  #ifdef __cplusplus
    5.10  }
     6.1 --- a/test/Makefile	Sun Feb 04 15:26:28 2018 +0100
     6.2 +++ b/test/Makefile	Mon Feb 05 10:58:03 2018 +0100
     6.3 @@ -55,7 +55,7 @@
     6.4  
     6.5  UNIT_TESTS_SOURCE=$(wildcard *_test.cc)
     6.6  UNIT_TESTS=$(subst .cc,,$(UNIT_TESTS_SOURCE))
     6.7 -UNIT_TESTS_RUN=$(subst .cc,_run,$(UNIT_TESTS_SOURCE))
     6.8 +UNIT_TESTS_RUN=$(subst .cc,_gdb,$(UNIT_TESTS_SOURCE))
     6.9  
    6.10  CFLAGS:=$(filter-out -DNDEBUG,$(CFLAGS))
    6.11  ifneq ($(MAKECMDGOALS),clean)
     7.1 --- a/test/external_revoke_test.cc	Sun Feb 04 15:26:28 2018 +0100
     7.2 +++ b/test/external_revoke_test.cc	Mon Feb 05 10:58:03 2018 +0100
     7.3 @@ -148,7 +148,7 @@
     7.4      // (note - as of 23.5.17, revoke_key() doesn't touch the trust db, just the keyring, so we can do this)
     7.5  
     7.6      cout << "Revoking key." << endl;
     7.7 -    status = get_identity(session, uniqname, r1_userid, &recip1);    
     7.8 +    status = update_identity(session, recip1);    
     7.9      status = revoke_key(session, recip1->fpr, "encrypt_for_identity_test");
    7.10      cout << "Status of revocation call for " << recip1->fpr << " is "<< tl_status_string(status) << endl;
    7.11  
    7.12 @@ -177,12 +177,13 @@
    7.13      cout << "2b. Encrypt message for recip whose key has been externally revoked in the keyring, not the app." << endl;
    7.14      cout << "---------------------------------------------------------" << endl << endl;
    7.15  
    7.16 +
    7.17      status = encrypt_message(session, outgoing_msg, NULL, &encrypted_outgoing_msg, PEP_enc_PGP_MIME, 0);
    7.18      cout << "Encryption returns with status " << tl_status_string(status) << endl;
    7.19 -    assert (status == PEP_KEY_UNSUITABLE);
    7.20 +    assert (status == PEP_UNENCRYPTED);
    7.21      assert (encrypted_outgoing_msg == NULL);
    7.22      status = update_identity(session, recip1);
    7.23 -    assert (recip1->comm_type = PEP_ct_key_revoked);
    7.24 +    assert(recip1->comm_type = PEP_ct_key_not_found);
    7.25  
    7.26      cout << endl << "---------------------------------------------------------" << endl;
    7.27      cout << "2c. Check trust of recip, whose only key has been revoked, once an encryption attempt has been made." << endl;
    7.28 @@ -194,7 +195,7 @@
    7.29      recip1->fpr = NULL;
    7.30  
    7.31      cout << "Recip's trust DB comm_type = " << hex << tl_ct_string(recip1->comm_type) << endl;
    7.32 -    assert(recip1->comm_type == PEP_ct_key_revoked);
    7.33 +    assert(recip1->comm_type == PEP_ct_unknown || recip1->comm_type == PEP_ct_key_revoked);
    7.34  
    7.35      free_message(decrypted_msg);
    7.36      free_message(outgoing_msg);
     8.1 --- a/test/new_update_id_and_myself_test.cc	Sun Feb 04 15:26:28 2018 +0100
     8.2 +++ b/test/new_update_id_and_myself_test.cc	Mon Feb 05 10:58:03 2018 +0100
     8.3 @@ -17,6 +17,8 @@
     8.4  int main() {
     8.5      cout << "\n*** test update_identity and myself ***\n\n";
     8.6      
     8.7 +    test_init();
     8.8 +    
     8.9      PEP_SESSION session;
    8.10      
    8.11      cout << "calling init()\n";
    8.12 @@ -225,7 +227,8 @@
    8.13  
    8.14      const char* new_username = "Test Patchy";
    8.15              
    8.16 -    alex = new_identity(alex_address, NULL, alex_userid, new_username); 
    8.17 +    alex = new_identity(alex_address, NULL, alex_userid, new_username);
    8.18 +    cout << "Timing is everything" << endl; 
    8.19      status = update_identity(session, alex);
    8.20      assert(status == PEP_STATUS_OK);
    8.21      assert(alex->fpr);
     9.1 --- a/test/test_util.cc	Sun Feb 04 15:26:28 2018 +0100
     9.2 +++ b/test/test_util.cc	Mon Feb 05 10:58:03 2018 +0100
     9.3 @@ -6,6 +6,13 @@
     9.4  #include <sstream>
     9.5  #include <stdexcept>
     9.6  
     9.7 +
     9.8 +void test_init() {
     9.9 +    unlink ("../test_home/.pEp_management.db");
    9.10 +    unlink ("../test_home/.pEp_management.db-shm");
    9.11 +    unlink ("../test_home/.pEp_management.db-wal");
    9.12 +}
    9.13 +
    9.14  std::string slurp(const std::string& filename)
    9.15  {
    9.16  	std::ifstream input(filename.c_str());
    10.1 --- a/test/test_util.h	Sun Feb 04 15:26:28 2018 +0100
    10.2 +++ b/test/test_util.h	Mon Feb 05 10:58:03 2018 +0100
    10.3 @@ -2,6 +2,8 @@
    10.4  #include "pEpEngine.h"
    10.5  #include "message_api.h"
    10.6  
    10.7 +void test_init();
    10.8 +
    10.9  // reads a whole file and returns it as std::string
   10.10  // throws std::runtime_error() if the file cannot be read. Empty file is not an error.
   10.11  std::string slurp(const std::string& filename);