ENGINE-332: a few tests and some bug fixes ENGINE-332
authorKrista Bennett <krista@pep-project.org>
Wed, 24 Jan 2018 18:22:36 +0100
branchENGINE-332
changeset 2444a97dd32908e5
parent 2441 eef4dbecb648
child 2445 c061bd2500bd
ENGINE-332: a few tests and some bug fixes
src/keymanagement.c
src/keymanagement.h
test/test_util.cc
test/test_util.h
test/trust_manipulation_test.cc
     1.1 --- a/src/keymanagement.c	Wed Jan 24 11:57:10 2018 +0100
     1.2 +++ b/src/keymanagement.c	Wed Jan 24 18:22:36 2018 +0100
     1.3 @@ -1097,34 +1097,36 @@
     1.4  
     1.5      assert(session);
     1.6      assert(ident);
     1.7 -    assert(!ident->me);
     1.8      assert(!EMPTYSTR(ident->fpr));
     1.9      assert(!EMPTYSTR(ident->address));
    1.10      assert(!EMPTYSTR(ident->user_id));
    1.11  
    1.12 -    // Nope. We should be able to reset trust on an own key.
    1.13      if (!(session && ident && ident->fpr && ident->fpr[0] != '\0' && ident->address &&
    1.14              ident->user_id))
    1.15          return PEP_ILLEGAL_VALUE;
    1.16 +
    1.17 +    // we do not change the input struct at ALL.
    1.18 +    pEp_identity* input_copy = identity_dup(ident);
    1.19      
    1.20      pEp_identity* tmp_ident = NULL;
    1.21      
    1.22 -    status = get_trust(session, ident);
    1.23 +    status = get_trust(session, input_copy);
    1.24      
    1.25      if (status != PEP_STATUS_OK)
    1.26 -        return status;
    1.27 +        goto pep_free;
    1.28          
    1.29      PEP_comm_type new_trust = PEP_ct_unknown;
    1.30  
    1.31 -    if (ident->comm_type != PEP_ct_mistrusted)
    1.32 -        new_trust = ident->comm_type & ~PEP_ct_confirmed;
    1.33 +    if (input_copy->comm_type != PEP_ct_mistrusted)
    1.34 +        new_trust = input_copy->comm_type & ~PEP_ct_confirmed;
    1.35  
    1.36      status = set_trust(session, ident->user_id, ident->fpr, new_trust);
    1.37      
    1.38      if (status != PEP_STATUS_OK)
    1.39 -        return status;
    1.40 +        goto pep_free;
    1.41 +
    1.42          
    1.43 -    ident->comm_type = new_trust;
    1.44 +    input_copy->comm_type = new_trust;
    1.45          
    1.46      tmp_ident = new_identity(ident->address, NULL, ident->user_id, NULL);
    1.47  
    1.48 @@ -1158,6 +1160,7 @@
    1.49              
    1.50  pep_free:
    1.51      free_identity(tmp_ident);
    1.52 +    free_identity(input_copy);
    1.53      return status;
    1.54  }
    1.55  
    1.56 @@ -1178,7 +1181,7 @@
    1.57              EMPTYSTR(ident->fpr))
    1.58          return PEP_ILLEGAL_VALUE;
    1.59  
    1.60 -    bool ident_has_trusted_default = false;
    1.61 +    //bool ident_has_trusted_default = false;
    1.62      char* ident_default_fpr = NULL;
    1.63  
    1.64      // Before we do anything, be sure the input fpr is even eligible to be trusted
    1.65 @@ -1239,7 +1242,7 @@
    1.66                  status = set_identity(session, ident);
    1.67                  trusted_default = true;
    1.68              }
    1.69 -            if (status == PEP_STATUS_OK) {
    1.70 +            if (status == PEP_STATUS_OK && !trusted_default) {
    1.71                  // Ok, there wasn't a trusted default, so we replaced. Thus, we also
    1.72                  // make sure there's a trusted default on the user_id. If there
    1.73                  // is not, we make this the default.
     2.1 --- a/src/keymanagement.h	Wed Jan 24 11:57:10 2018 +0100
     2.2 +++ b/src/keymanagement.h	Wed Jan 24 18:22:36 2018 +0100
     2.3 @@ -233,7 +233,7 @@
     2.4  //                     its accompanying key/user_id pair.
     2.5  //  parameters:
     2.6  //      session (in)        session to use
     2.7 -//      ident (in)          person and key which was compromized
     2.8 +//      ident (in)          identity for person and key whose trust status is to be reset
     2.9  
    2.10  DYNAMIC_API PEP_STATUS key_reset_trust(
    2.11          PEP_SESSION session,
     3.1 --- a/test/test_util.cc	Wed Jan 24 11:57:10 2018 +0100
     3.2 +++ b/test/test_util.cc	Wed Jan 24 18:22:36 2018 +0100
     3.3 @@ -1,5 +1,6 @@
     3.4  #include "pEpEngine_test.h"
     3.5  #include "pEpEngine.h"
     3.6 +#include "pEp_internal.h"
     3.7  #include "message_api.h"
     3.8  #include <fstream>
     3.9  #include <sstream>
    3.10 @@ -30,6 +31,13 @@
    3.11      outfile.close();
    3.12  }
    3.13  
    3.14 +char* get_new_uuid() {
    3.15 +    char* new_uuid = (char*)calloc(37, 1);
    3.16 +    pEpUUID uuid;
    3.17 +    uuid_generate_random(uuid);
    3.18 +    uuid_unparse_upper(uuid, new_uuid);
    3.19 +    return new_uuid;
    3.20 +}
    3.21  
    3.22  const char* tl_status_string(PEP_STATUS status) {
    3.23      switch (status) {
     4.1 --- a/test/test_util.h	Wed Jan 24 11:57:10 2018 +0100
     4.2 +++ b/test/test_util.h	Wed Jan 24 18:22:36 2018 +0100
     4.3 @@ -18,3 +18,6 @@
     4.4  
     4.5  // Returns the string value of the input status enum value. 
     4.6  const char* tl_status_string(PEP_STATUS status);
     4.7 +
     4.8 +// Grabs a new uuid for your randomish string needs.
     4.9 +char* get_new_uuid();
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/test/trust_manipulation_test.cc	Wed Jan 24 18:22:36 2018 +0100
     5.3 @@ -0,0 +1,101 @@
     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.h>
     5.9 +#include <time.h>
    5.10 +#include "platform.h"
    5.11 +#include <iostream>
    5.12 +#include <fstream>
    5.13 +#include <assert.h>
    5.14 +#include "mime.h"
    5.15 +#include "message_api.h"
    5.16 +#include "test_util.h"
    5.17 +
    5.18 +using namespace std;
    5.19 +
    5.20 +int main() {
    5.21 +    cout << "\n*** trust manipulation test ***\n\n";
    5.22 +
    5.23 +    PEP_SESSION session;
    5.24 +
    5.25 +    char* user_id = get_new_uuid();
    5.26 +    
    5.27 +    cout << "calling init()\n";
    5.28 +    PEP_STATUS status = init(&session);   
    5.29 +    assert(status == PEP_STATUS_OK);
    5.30 +    assert(session);
    5.31 +    cout << "init() completed.\n";
    5.32 +
    5.33 +    cout << "creating id for : ";
    5.34 +    char *uniqname = strdup("AAAAtestuser@testdomain.org");
    5.35 +    srandom(time(NULL));
    5.36 +    for(int i=0; i < 4;i++)
    5.37 +        uniqname[i] += random() & 0xf;
    5.38 +    
    5.39 +    cout << uniqname << "\n";
    5.40 +    pEp_identity * user = new_identity(uniqname, NULL, user_id, "Test User");
    5.41 +    status = generate_keypair(session, user);
    5.42 +    assert(user->fpr);
    5.43 +
    5.44 +    char* keypair1 = strdup(user->fpr);
    5.45 +    cout << "generated fingerprint \n";
    5.46 +    cout << user->fpr << "\n";
    5.47 +
    5.48 +    cout << "Setting key 1 (" << user->fpr << ") as the default for the identity." << endl;
    5.49 +    // Put identity in the DB
    5.50 +    status = set_identity(session, user);
    5.51 +
    5.52 +    cout << "creating second keypair for : " << uniqname << endl;
    5.53 +    
    5.54 +    pEp_identity * user_again = new_identity(uniqname, NULL, user_id, "Test User");
    5.55 +    status = generate_keypair(session, user_again);
    5.56 +    assert(user_again->fpr);
    5.57 +
    5.58 +    char* keypair2 = strdup(user_again->fpr);
    5.59 +    cout << "generated fingerprint \n";
    5.60 +    cout << user_again->fpr << "\n";
    5.61 +
    5.62 +    assert(strcmp(user->fpr, user_again->fpr) != 0);
    5.63 +    update_identity(session, user);
    5.64 +    assert(strcmp(user->fpr, keypair1) == 0);
    5.65 +    cout << "Key 1 (" << user->fpr << ") is still the default for the identity after update_identity." << endl;
    5.66 +
    5.67 +    // First, trust the SECOND key; make sure it replaces as the default
    5.68 +    cout << "Set trust bit for key 2 (" << keypair2 << ") and ensure it replaces key 1 as the default." << endl;
    5.69 +    status = trust_personal_key(session, user_again);
    5.70 +    status = update_identity(session, user);
    5.71 +    assert(user->comm_type == PEP_ct_OpenPGP);
    5.72 +    assert(strcmp(user->fpr, keypair2) == 0);
    5.73 +    cout << "Key 2 (" << user->fpr << ") is now the default for the identity after update_identity, and its comm_type is PEP_ct_OpenPGP (trust bit set!)." << endl;
    5.74 +
    5.75 +    cout << "Now make key 2 not trusted (which also removes it as a default everywhere)." << endl;
    5.76 +    status = key_reset_trust(session, user);
    5.77 +    status = get_trust(session, user);
    5.78 +    assert(strcmp(user->fpr, keypair2) == 0);
    5.79 +    assert(user->comm_type == PEP_ct_OpenPGP_unconfirmed);
    5.80 +    cout << "Key 2 is untrusted in the DB." << endl;
    5.81 +
    5.82 +    cout << "Now let's mistrust key 2 in the DB." << endl;
    5.83 +    // Now let's mistrust the second key.
    5.84 +    status = key_mistrusted(session, user);
    5.85 +    status = get_trust(session, user);
    5.86 +    assert(strcmp(user->fpr, keypair2) == 0);
    5.87 +    assert(user->comm_type == PEP_ct_mistrusted);
    5.88 +    cout << "Hoorah, we now do not trust key 2. (We never liked key 2 anyway.)" << endl;
    5.89 +    cout << "Now we call update_identity to see what gifts it gives us (should be key 1 with key 1's initial trust.)" << endl;    
    5.90 +    status = update_identity(session, user);
    5.91 +    assert(strcmp(user->fpr, keypair1) == 0);
    5.92 +    assert(user->comm_type == PEP_ct_OpenPGP_unconfirmed);
    5.93 +    cout << "Yup, got key 1, and the trust status is PEP_ct_OpenPGP_unconfirmed." << endl;
    5.94 +    
    5.95 +    cout << "Passed all of our exciting messing with the trust DB. Moving on..." << endl;
    5.96 + 
    5.97 +    free(user_id);
    5.98 +    free(keypair1);
    5.99 +    free(uniqname);
   5.100 +    free_identity(user);
   5.101 +    release(session);
   5.102 +    
   5.103 +    return 0;
   5.104 +}