test/src/engine_tests/NewUpdateIdAndMyselfTests.cc
author Krista 'DarthMama' Bennett <krista@pep.foundation>
Thu, 27 Jun 2019 15:12:42 +0200
branchENGINE-581
changeset 3886 ebd61a1cf6db
parent 3401 9d9142a1490e
permissions -rw-r--r--
Fixed hole in update_identity spec for extant identities without saved fprs.
     1 // This file is under GNU General Public License 3.0
     2 // see LICENSE.txt
     3 
     4 #include <stdlib.h>
     5 #include <string>
     6 #include <iostream>
     7 #include <fstream>
     8 #include <cstring> // for strcmp()
     9 #include "TestConstants.h"
    10 
    11 #include "pEpEngine.h"
    12 #include "message_api.h"
    13 #include "keymanagement.h"
    14 #include "test_util.h"
    15 
    16 #include <cpptest.h>
    17 #include "EngineTestSessionSuite.h"
    18 #include "NewUpdateIdAndMyselfTests.h"
    19 
    20 using namespace std;
    21 
    22 NewUpdateIdAndMyselfTests::NewUpdateIdAndMyselfTests(string suitename, string test_home_dir) :
    23     EngineTestSessionSuite::EngineTestSessionSuite(suitename, test_home_dir) {
    24     add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("NewUpdateIdAndMyselfTests::myself_no_record_no_input_fpr"),
    25                                                                       static_cast<Func>(&NewUpdateIdAndMyselfTests::myself_no_record_no_input_fpr)));
    26     add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("NewUpdateIdAndMyselfTests::myself_no_input_fpr_w_record"),
    27                                                                       static_cast<Func>(&NewUpdateIdAndMyselfTests::myself_no_input_fpr_w_record)));
    28     add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("NewUpdateIdAndMyselfTests::myself_no_input_fpr_diff_user_id_w_record"),
    29                                                                       static_cast<Func>(&NewUpdateIdAndMyselfTests::myself_no_input_fpr_diff_user_id_w_record)));
    30     add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("NewUpdateIdAndMyselfTests::myself_replace_fpr"),
    31                                                                       static_cast<Func>(&NewUpdateIdAndMyselfTests::myself_replace_fpr)));
    32     add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("NewUpdateIdAndMyselfTests::myself_replace_fpr_revoke_key"),
    33                                                                       static_cast<Func>(&NewUpdateIdAndMyselfTests::myself_replace_fpr_revoke_key)));
    34     add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("NewUpdateIdAndMyselfTests::update_identity_w_matching_address_user_id_username"),
    35                                                                       static_cast<Func>(&NewUpdateIdAndMyselfTests::update_identity_w_matching_address_user_id_username)));
    36     add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("NewUpdateIdAndMyselfTests::update_identity_w_matching_address_user_id_new_username"),
    37                                                                       static_cast<Func>(&NewUpdateIdAndMyselfTests::update_identity_w_matching_address_user_id_new_username)));
    38     add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("NewUpdateIdAndMyselfTests::update_identity_w_matching_address_user_id_only"),
    39                                                                       static_cast<Func>(&NewUpdateIdAndMyselfTests::update_identity_w_matching_address_user_id_only)));
    40     add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("NewUpdateIdAndMyselfTests::update_identity_use_address_username_only"),
    41                                                                       static_cast<Func>(&NewUpdateIdAndMyselfTests::update_identity_use_address_username_only)));
    42     add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("NewUpdateIdAndMyselfTests::update_identity_use_address_only"),
    43                                                                       static_cast<Func>(&NewUpdateIdAndMyselfTests::update_identity_use_address_only)));
    44     add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("NewUpdateIdAndMyselfTests::update_identity_use_address_only_on_own_ident"),
    45                                                                       static_cast<Func>(&NewUpdateIdAndMyselfTests::update_identity_use_address_only_on_own_ident)));
    46     add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("NewUpdateIdAndMyselfTests::update_identity_non_existent_user_id_address"),
    47                                                                       static_cast<Func>(&NewUpdateIdAndMyselfTests::update_identity_non_existent_user_id_address)));
    48     add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("NewUpdateIdAndMyselfTests::update_identity_address_username_userid_no_record"),
    49                                                                       static_cast<Func>(&NewUpdateIdAndMyselfTests::update_identity_address_username_userid_no_record)));
    50     add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("NewUpdateIdAndMyselfTests::update_identity_address_username_no_record"),
    51                                                                       static_cast<Func>(&NewUpdateIdAndMyselfTests::update_identity_address_username_no_record)));
    52     add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("NewUpdateIdAndMyselfTests::update_identity_address_only_multiple_records"),
    53                                                                       static_cast<Func>(&NewUpdateIdAndMyselfTests::update_identity_address_only_multiple_records)));
    54     add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("NewUpdateIdAndMyselfTests::key_elect_expired_key"),
    55                                                                       static_cast<Func>(&NewUpdateIdAndMyselfTests::key_elect_expired_key)));
    56     add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("NewUpdateIdAndMyselfTests::key_elect_only_revoked_mistrusted"),
    57                                                                       static_cast<Func>(&NewUpdateIdAndMyselfTests::key_elect_only_revoked_mistrusted)));
    58 }
    59 
    60 void NewUpdateIdAndMyselfTests::setup() {
    61     EngineTestSessionSuite::setup();
    62     if (on_test_number == 1) {
    63         uniqname = strdup("AAAAtestuser@testdomain.org");
    64         srandom(time(NULL));
    65         for(int i=0; i < 4;i++)
    66         uniqname[i] += random() & 0xf;
    67         
    68         own_user_id = get_new_uuid();
    69         start_username = strdup("Unser Testkandidat");
    70         generated_fpr = NULL;
    71         default_own_id = NULL;
    72         alias_id = NULL;
    73         new_fpr = NULL;
    74         alex_address = "pep.test.alexander@peptest.ch";
    75         alex_fpr = "3AD9F60FAEB22675DB873A1362D6981326B54E4E";
    76         alex_userid = "Alex";
    77         alex_username = "SuperDuperAlex";
    78     }    
    79 }
    80 
    81 void NewUpdateIdAndMyselfTests::tear_down() {
    82     if (on_test_number == number_of_tests) {
    83         free(uniqname);
    84         free(own_user_id);
    85         free(start_username);
    86         free(generated_fpr);
    87         free(default_own_id);
    88         free(alias_id);
    89         free(new_fpr);
    90     }
    91     EngineTestSessionSuite::tear_down();
    92 }
    93 
    94 void NewUpdateIdAndMyselfTests::myself_no_record_no_input_fpr() {
    95     PEP_STATUS status = PEP_STATUS_OK;
    96     
    97     pEp_identity * new_me = new_identity(uniqname, NULL, own_user_id, start_username);
    98     
    99     status = myself(session, new_me);
   100     TEST_ASSERT_MSG((status == PEP_STATUS_OK), tl_status_string(status));
   101     TEST_ASSERT_MSG((new_me->fpr), "new_me->fpr");
   102     
   103     generated_fpr = strdup(new_me->fpr);
   104     
   105     TEST_ASSERT_MSG((new_me->comm_type == PEP_ct_pEp), "new_me->comm_type == PEP_ct_pEp");
   106     
   107     free_identity(new_me);
   108 }
   109 
   110 void NewUpdateIdAndMyselfTests::myself_no_input_fpr_w_record() {
   111     PEP_STATUS status = PEP_STATUS_OK;
   112     
   113     pEp_identity* new_me = new_identity(uniqname, NULL, own_user_id, NULL);
   114     status = myself(session, new_me);
   115     TEST_ASSERT_MSG((status == PEP_STATUS_OK), tl_status_string(status));
   116     
   117     TEST_ASSERT_MSG((new_me->fpr), "new_me->fpr");
   118     TEST_ASSERT_MSG((strcmp(new_me->fpr, generated_fpr) == 0), "strcmp(new_me->fpr, generated_fpr) == 0");
   119     TEST_ASSERT_MSG((new_me->username), "new_me->username");
   120     TEST_ASSERT_MSG((strcmp(new_me->username, start_username) == 0), "strcmp(new_me->username, start_username) == 0");
   121     TEST_ASSERT_MSG((new_me->user_id), "new_me->user_id");
   122     TEST_ASSERT_MSG((new_me->comm_type == PEP_ct_pEp), "new_me->comm_type == PEP_ct_pEp");
   123     
   124     default_own_id = NULL;
   125     status = get_userid_alias_default(session, own_user_id, &default_own_id);
   126     if (status == PEP_CANNOT_FIND_ALIAS) {
   127         // Ok, we presume our own id above is the default (should be true if there was no existing DB as in test env)
   128         default_own_id = strdup(own_user_id);
   129     }
   130 
   131     TEST_ASSERT_MSG((strcmp(new_me->user_id, default_own_id) == 0), "strcmp(new_me->user_id, default_own_id) == 0");
   132     
   133     cout << "PASS: myself() retrieved the correct fpr, username and default user id" << endl << endl;
   134 
   135     free_identity(new_me);
   136 }
   137 
   138 void NewUpdateIdAndMyselfTests::myself_no_input_fpr_diff_user_id_w_record() {
   139     PEP_STATUS status = PEP_STATUS_OK;
   140     alias_id = strdup("Huss Es El Mejor Presidente Del Mundo!");
   141 
   142     pEp_identity* new_me = new_identity(uniqname, NULL, alias_id, NULL);
   143     status = myself(session, new_me);
   144     TEST_ASSERT_MSG((status == PEP_STATUS_OK), tl_status_string(status));
   145     
   146     TEST_ASSERT_MSG((new_me->fpr), "new_me->fpr");
   147     TEST_ASSERT_MSG((strcmp(new_me->fpr, generated_fpr) == 0), "strcmp(new_me->fpr, generated_fpr) == 0");
   148     TEST_ASSERT_MSG((new_me->username), "new_me->username");
   149     TEST_ASSERT_MSG((strcmp(new_me->username, start_username) == 0), "strcmp(new_me->username, start_username) == 0");
   150     TEST_ASSERT_MSG((new_me->user_id), "new_me->user_id");
   151     TEST_ASSERT_MSG((strcmp(new_me->user_id, default_own_id) == 0), "strcmp(new_me->user_id, default_own_id) == 0");
   152     TEST_ASSERT_MSG((new_me->comm_type == PEP_ct_pEp), "new_me->comm_type == PEP_ct_pEp");
   153     
   154     char* tmp_def = NULL;
   155     
   156     status = get_userid_alias_default(session, alias_id, &tmp_def);
   157     TEST_ASSERT_MSG((status == PEP_STATUS_OK), tl_status_string(status));
   158     TEST_ASSERT_MSG((strcmp(tmp_def, default_own_id) == 0), "strcmp(tmp_def, default_own_id) == 0");
   159 
   160     cout << "PASS: myself() retrieved the correct fpr, username and default user id, and put the right alias in for the default";
   161     cout << endl << endl;
   162     
   163     free(tmp_def);
   164     free_identity(new_me); 
   165 }
   166 
   167 void NewUpdateIdAndMyselfTests::myself_replace_fpr() {
   168     PEP_STATUS status = PEP_STATUS_OK;
   169     pEp_identity* new_me = new_identity(uniqname, NULL, alias_id, start_username);
   170     status = generate_keypair(session, new_me);
   171     TEST_ASSERT_MSG((new_me->fpr), "new_me->fpr");
   172     
   173     cout << "Generated fingerprint ";
   174     cout << new_me->fpr << "\n";
   175 
   176     new_fpr = strdup(new_me->fpr);
   177 
   178     status = set_own_key(session, new_me, new_fpr);
   179     TEST_ASSERT_MSG((status == PEP_STATUS_OK), tl_status_string(status));
   180     TEST_ASSERT_MSG((new_me->fpr), "new_me->fpr");
   181     TEST_ASSERT_MSG((strcmp(new_me->fpr, generated_fpr) != 0), "strcmp(new_me->fpr, generated_fpr) != 0");
   182     TEST_ASSERT_MSG((strcmp(new_me->fpr, new_fpr) == 0), "strcmp(new_me->fpr, new_fpr) == 0");
   183     TEST_ASSERT_MSG((new_me->username), "new_me->username");
   184     TEST_ASSERT_MSG((strcmp(new_me->username, start_username) == 0), "strcmp(new_me->username, start_username) == 0");
   185     TEST_ASSERT_MSG((new_me->user_id), "new_me->user_id");
   186     TEST_ASSERT_MSG((strcmp(new_me->user_id, default_own_id) == 0), "strcmp(new_me->user_id, default_own_id) == 0");
   187     TEST_ASSERT_MSG((new_me->me), "new_me->me");
   188     TEST_ASSERT_MSG((new_me->comm_type == PEP_ct_pEp), "new_me->comm_type == PEP_ct_pEp");
   189 
   190     cout << "PASS: myself() set and retrieved the new fpr, username and default user id, and put the right alias in for the default";
   191     cout << endl << endl;
   192 
   193     // since that worked, we'll set it back as the default
   194     free(new_me->fpr);
   195     new_me->fpr = strdup(generated_fpr);
   196     new_me->comm_type = PEP_ct_unknown;
   197     status = set_own_key(session, new_me, generated_fpr);
   198     TEST_ASSERT_MSG((status == PEP_STATUS_OK), tl_status_string(status));
   199     TEST_ASSERT_MSG((strcmp(new_me->fpr, generated_fpr) == 0), "strcmp(new_me->fpr, generated_fpr) == 0");
   200     TEST_ASSERT_MSG((new_me->comm_type == PEP_ct_pEp), "new_me->comm_type == PEP_ct_pEp");    
   201     free_identity(new_me);
   202 }
   203 
   204 void NewUpdateIdAndMyselfTests::myself_replace_fpr_revoke_key() {
   205     PEP_STATUS status = PEP_STATUS_OK;
   206     status = revoke_key(session, generated_fpr, "Because it's fun");
   207     TEST_ASSERT (status == PEP_STATUS_OK);
   208     
   209     pEp_identity* new_me = new_identity(uniqname, NULL, alias_id, start_username);
   210     
   211     status = set_own_key(session, new_me, new_fpr);
   212     TEST_ASSERT_MSG((status == PEP_STATUS_OK), tl_status_string(status));
   213     TEST_ASSERT_MSG((new_me->fpr), "new_me->fpr");
   214     TEST_ASSERT_MSG((strcmp(new_me->fpr, generated_fpr) != 0), "strcmp(new_me->fpr, generated_fpr) != 0");
   215     TEST_ASSERT_MSG((new_me->username), "new_me->username");
   216     TEST_ASSERT_MSG((strcmp(new_me->username, start_username) == 0), "strcmp(new_me->username, start_username) == 0");
   217     TEST_ASSERT_MSG((new_me->user_id), "new_me->user_id");
   218     TEST_ASSERT_MSG((strcmp(new_me->user_id, default_own_id) == 0), "strcmp(new_me->user_id, default_own_id) == 0");
   219     TEST_ASSERT_MSG((new_me->me), "new_me->me");
   220     TEST_ASSERT_MSG((new_me->comm_type == PEP_ct_pEp), "new_me->comm_type == PEP_ct_pEp");
   221     
   222     cout << "PASS: myself() retrieved the new fpr, username and default user id, and put the right alias in for the default";
   223     cout << endl << endl;
   224     free_identity(new_me);
   225 }
   226 
   227 void NewUpdateIdAndMyselfTests::update_identity_w_matching_address_user_id_username() {
   228     // 1. create original identity
   229     const char* alex_address = "pep.test.alexander@peptest.ch";
   230     const char* alex_fpr = "3AD9F60FAEB22675DB873A1362D6981326B54E4E";
   231     const char* alex_userid = "Alex";
   232     const char* alex_username = "SuperDuperAlex";
   233     const string alex_pub_key = slurp("test_keys/pub/pep.test.alexander-0x26B54E4E_pub.asc");
   234     
   235     PEP_STATUS statuspub = import_key(session, alex_pub_key.c_str(), alex_pub_key.length(), NULL);
   236     TEST_ASSERT_MSG((statuspub == PEP_TEST_KEY_IMPORT_SUCCESS), "statuspub == PEP_STATUS_OK");
   237 
   238     pEp_identity* alex = new_identity(alex_address, alex_fpr, alex_userid, alex_username);
   239 
   240     // 2. set identity
   241     PEP_STATUS status = set_identity(session, alex);
   242     TEST_ASSERT_MSG((status == PEP_STATUS_OK), tl_status_string(status));
   243     free_identity(alex);
   244             
   245     alex = new_identity(alex_address, NULL, alex_userid, alex_username); 
   246     status = update_identity(session, alex);
   247     TEST_ASSERT_MSG((status == PEP_STATUS_OK), tl_status_string(status));
   248     TEST_ASSERT_MSG((alex->fpr), "alex->fpr");
   249     TEST_ASSERT_MSG((strcmp(alex->fpr, alex_fpr) == 0), "strcmp(alex->fpr, alex_fpr) == 0");
   250     TEST_ASSERT_MSG((alex->username), "alex->username");
   251     TEST_ASSERT_MSG((strcmp(alex->username, alex_username) == 0), "strcmp(alex->username, alex_username) == 0");
   252     TEST_ASSERT_MSG((alex->user_id), "alex->user_id");
   253     TEST_ASSERT_MSG((strcmp(alex->user_id, alex_userid) == 0), "strcmp(alex->user_id, alex_userid) == 0");
   254     TEST_ASSERT_MSG((!alex->me), "!alex->me"); 
   255     TEST_ASSERT_MSG((alex->comm_type == PEP_ct_OpenPGP_unconfirmed), "alex->comm_type == PEP_ct_OpenPGP_unconfirmed");
   256     TEST_ASSERT_MSG((strcmp(alex->address, alex_address) == 0), "strcmp(alex->address, alex_address) == 0");
   257 
   258     cout << "PASS: update_identity() correctly retrieved extant record with matching address, id, and username" << endl << endl;
   259     free_identity(alex);
   260 }
   261 
   262 void NewUpdateIdAndMyselfTests::update_identity_w_matching_address_user_id_new_username() {
   263     PEP_STATUS status = PEP_STATUS_OK;
   264         
   265     const string alex_pub_key = slurp("test_keys/pub/pep.test.alexander-0x26B54E4E_pub.asc");
   266     
   267     PEP_STATUS statuspub = import_key(session, alex_pub_key.c_str(), alex_pub_key.length(), NULL);
   268     TEST_ASSERT_MSG((statuspub == PEP_TEST_KEY_IMPORT_SUCCESS), "statuspub == PEP_STATUS_OK");
   269 
   270     pEp_identity* alex = new_identity(alex_address, alex_fpr, alex_userid, alex_username);
   271 
   272     // 2. set identity
   273     status = set_identity(session, alex);
   274     TEST_ASSERT_MSG((status == PEP_STATUS_OK), tl_status_string(status));
   275     free_identity(alex);
   276             
   277     alex = new_identity(alex_address, NULL, alex_userid, alex_username); 
   278     status = update_identity(session, alex);
   279     TEST_ASSERT_MSG((status == PEP_STATUS_OK), tl_status_string(status));
   280     TEST_ASSERT_MSG((alex->fpr), "alex->fpr");
   281     TEST_ASSERT_MSG((strcmp(alex->fpr, alex_fpr) == 0), "strcmp(alex->fpr, alex_fpr) == 0");
   282     TEST_ASSERT_MSG((alex->username), "alex->username");
   283     TEST_ASSERT_MSG((strcmp(alex->username, alex_username) == 0), "strcmp(alex->username, alex_username) == 0");
   284     TEST_ASSERT_MSG((alex->user_id), "alex->user_id");
   285     TEST_ASSERT_MSG((strcmp(alex->user_id, alex_userid) == 0), "strcmp(alex->user_id, alex_userid) == 0");
   286     TEST_ASSERT_MSG((!alex->me), "!alex->me"); 
   287     TEST_ASSERT_MSG((alex->comm_type == PEP_ct_OpenPGP_unconfirmed), "alex->comm_type == PEP_ct_OpenPGP_unconfirmed");
   288     TEST_ASSERT_MSG((strcmp(alex->address, alex_address) == 0), "strcmp(alex->address, alex_address) == 0");
   289 
   290     cout << "PASS: update_identity() correctly retrieved extant record with matching address, id, and username" << endl << endl;
   291     free_identity(alex);
   292 }
   293 
   294 void NewUpdateIdAndMyselfTests::update_identity_w_matching_address_user_id_only() {
   295     PEP_STATUS status = PEP_STATUS_OK;
   296     new_username = "Test Patchy";
   297             
   298     pEp_identity* alex = new_identity(alex_address, NULL, alex_userid, new_username);
   299     status = update_identity(session, alex);
   300     TEST_ASSERT_MSG((status == PEP_STATUS_OK), tl_status_string(status));
   301     TEST_ASSERT_MSG((alex->fpr), "alex->fpr");
   302     TEST_ASSERT_MSG((strcmp(alex->fpr, alex_fpr) == 0), "strcmp(alex->fpr, alex_fpr) == 0");
   303     TEST_ASSERT_MSG((alex->username), "alex->username");
   304     TEST_ASSERT_MSG((strcmp(alex->username, new_username) == 0), "strcmp(alex->username, new_username) == 0");
   305     TEST_ASSERT_MSG((alex->user_id), "alex->user_id");
   306     TEST_ASSERT_MSG((strcmp(alex->user_id, alex_userid) == 0), "strcmp(alex->user_id, alex_userid) == 0");
   307     TEST_ASSERT_MSG((!alex->me), "!alex->me"); 
   308     TEST_ASSERT_MSG((alex->comm_type == PEP_ct_OpenPGP_unconfirmed), "alex->comm_type == PEP_ct_OpenPGP_unconfirmed");
   309     TEST_ASSERT_MSG((strcmp(alex->address, alex_address) == 0), "strcmp(alex->address, alex_address) == 0");
   310 
   311     free_identity(alex);
   312 }
   313 
   314 void NewUpdateIdAndMyselfTests::update_identity_use_address_username_only() {
   315     pEp_identity* alex = new_identity(alex_address, NULL, NULL, new_username); 
   316     PEP_STATUS status = update_identity(session, alex);
   317     TEST_ASSERT_MSG((status == PEP_STATUS_OK), tl_status_string(status));
   318     TEST_ASSERT_MSG((alex->fpr), "alex->fpr");
   319     TEST_ASSERT_MSG((strcmp(alex->fpr, alex_fpr) == 0), "strcmp(alex->fpr, alex_fpr) == 0");
   320     TEST_ASSERT_MSG((alex->username), "alex->username");
   321     TEST_ASSERT_MSG((strcmp(alex->username, new_username) == 0), "strcmp(alex->username, new_username) == 0");
   322     TEST_ASSERT_MSG((alex->user_id), "alex->user_id");
   323     TEST_ASSERT_MSG((strcmp(alex->user_id, alex_userid) == 0), "strcmp(alex->user_id, alex_userid) == 0");
   324     TEST_ASSERT_MSG((!alex->me), "!alex->me"); 
   325     TEST_ASSERT_MSG((alex->comm_type == PEP_ct_OpenPGP_unconfirmed), "alex->comm_type == PEP_ct_OpenPGP_unconfirmed");
   326     TEST_ASSERT_MSG((strcmp(alex->address, alex_address) == 0), "strcmp(alex->address, alex_address) == 0");
   327 
   328     cout << "PASS: update_identity() correctly retrieved extant record with matching address and username" << endl << endl;
   329     free_identity(alex);
   330 }
   331 
   332 void NewUpdateIdAndMyselfTests::update_identity_use_address_only() {
   333     pEp_identity* alex = new_identity(alex_address, NULL, NULL, NULL); 
   334     PEP_STATUS status = update_identity(session, alex);
   335     TEST_ASSERT_MSG((status == PEP_STATUS_OK), tl_status_string(status));
   336     TEST_ASSERT_MSG((alex->fpr), "alex->fpr");
   337     TEST_ASSERT_MSG((strcmp(alex->fpr, alex_fpr) == 0), "strcmp(alex->fpr, alex_fpr) == 0");
   338     TEST_ASSERT_MSG((alex->username), "alex->username");
   339     TEST_ASSERT_MSG((strcmp(alex->username, new_username) == 0), "strcmp(alex->username, new_username) == 0");
   340     TEST_ASSERT_MSG((alex->user_id), "alex->user_id");
   341     TEST_ASSERT_MSG((strcmp(alex->user_id, alex_userid) == 0), "strcmp(alex->user_id, alex_userid) == 0");
   342     TEST_ASSERT_MSG((!alex->me), "!alex->me"); 
   343     TEST_ASSERT_MSG((alex->comm_type == PEP_ct_OpenPGP_unconfirmed), "alex->comm_type == PEP_ct_OpenPGP_unconfirmed");
   344     TEST_ASSERT_MSG((strcmp(alex->address, alex_address) == 0), "strcmp(alex->address, alex_address) == 0");
   345 
   346     cout << "PASS: update_identity() correctly retrieved extant record with just matching address. Retrieved previously patched username." << endl << endl;
   347     free_identity(alex);
   348 }
   349 
   350 void NewUpdateIdAndMyselfTests::update_identity_use_address_only_on_own_ident() {
   351     pEp_identity* somebody = new_identity(uniqname, NULL, NULL, NULL); 
   352     PEP_STATUS status = update_identity(session, somebody);
   353     TEST_ASSERT_MSG((status == PEP_STATUS_OK), tl_status_string(status));
   354     myself(session, somebody);
   355     TEST_ASSERT_MSG((somebody->fpr), "somebody->fpr");
   356     TEST_ASSERT_MSG((strcmp(somebody->fpr, new_fpr) == 0), "strcmp(somebody->fpr, new_fpr) == 0");
   357     TEST_ASSERT_MSG((somebody->username), "somebody->username");
   358     TEST_ASSERT_MSG((strcmp(somebody->username, start_username) == 0), "strcmp(somebody->username, start_username) == 0");
   359     TEST_ASSERT_MSG((somebody->user_id), "somebody->user_id");
   360     TEST_ASSERT_MSG((strcmp(somebody->user_id, default_own_id) == 0), "strcmp(somebody->user_id, default_own_id) == 0");
   361     TEST_ASSERT_MSG((somebody->me), "somebody->me"); // true in this case, as it was an own identity
   362     TEST_ASSERT_MSG((somebody->comm_type == PEP_ct_pEp), "somebody->comm_type == PEP_ct_pEp");
   363     TEST_ASSERT_MSG((strcmp(somebody->address, uniqname) == 0), "strcmp(somebody->address, uniqname) == 0");
   364     
   365     cout << "PASS: update_identity() retrieved the right identity information given just an address";
   366     cout << endl << endl;
   367 
   368     free_identity(somebody);
   369 }
   370 
   371 void NewUpdateIdAndMyselfTests::update_identity_non_existent_user_id_address() {
   372     pEp_identity* somebody = new_identity("nope@nope.nope", NULL, "some_user_id", NULL); 
   373     PEP_STATUS status = update_identity(session, somebody);
   374     TEST_ASSERT_MSG((status == PEP_STATUS_OK), tl_status_string(status));
   375     TEST_ASSERT_MSG((!somebody->fpr), "!somebody->fpr");
   376     TEST_ASSERT_MSG((somebody->comm_type == PEP_ct_key_not_found), "somebody->comm_type == PEP_ct_key_not_found");
   377     
   378     cout << "PASS: update_identity() returns identity with no key and unknown comm type" << endl << endl;
   379 
   380     free_identity(somebody);
   381 }
   382 
   383 void NewUpdateIdAndMyselfTests::update_identity_address_username_userid_no_record() {
   384     const char* rando_name = "Pickley BoofBoof";
   385     const char* rando_userid = "Boofy";
   386     const char* rando_address = "boof@pickles.org";
   387     pEp_identity* somebody = new_identity(rando_address, NULL, rando_userid, rando_name);
   388     PEP_STATUS status = update_identity(session, somebody);
   389 
   390     TEST_ASSERT_MSG((status == PEP_STATUS_OK), tl_status_string(status));
   391     TEST_ASSERT_MSG((!somebody->fpr || somebody->fpr[0] == '\0'), "!somebody->fpr || somebody->fpr[0] == '\0'");
   392     TEST_ASSERT_MSG((somebody->username), "somebody->username");
   393     TEST_ASSERT_MSG((strcmp(somebody->username, rando_name) == 0), "strcmp(somebody->username, rando_name) == 0");
   394     TEST_ASSERT_MSG((somebody->user_id), "somebody->user_id");
   395     TEST_ASSERT_MSG((strcmp(somebody->user_id, rando_userid) == 0), "strcmp(somebody->user_id, rando_userid) == 0"); // ???
   396     TEST_ASSERT_MSG((!somebody->me), "!somebody->me"); 
   397     TEST_ASSERT_MSG((somebody->comm_type == PEP_ct_key_not_found), "somebody->comm_type == PEP_ct_key_not_found");
   398     TEST_ASSERT_MSG((strcmp(somebody->address, rando_address) == 0), "strcmp(somebody->address, rando_address) == 0");
   399 
   400     cout << "PASS: update_identity() correctly created record with no key" << endl << endl;
   401     free_identity(somebody);
   402 }
   403 
   404 void NewUpdateIdAndMyselfTests::update_identity_address_username_no_record() {
   405     const char* rando2_name = "Pickles BoofyBoof";
   406     const char* rando2_address = "boof2@pickles.org";
   407     pEp_identity* somebody = new_identity(rando2_address, NULL, NULL, rando2_name);
   408     PEP_STATUS status = update_identity(session, somebody);
   409     const char* expected_rando2_userid = "TOFU_boof2@pickles.org";
   410 
   411     TEST_ASSERT_MSG((status == PEP_STATUS_OK), tl_status_string(status));
   412     TEST_ASSERT_MSG((!somebody->fpr || somebody->fpr[0] == '\0'), "!somebody->fpr || somebody->fpr[0] == '\0'");
   413     TEST_ASSERT_MSG((somebody->username), "somebody->username");
   414     TEST_ASSERT_MSG((strcmp(somebody->username, rando2_name) == 0), "strcmp(somebody->username, rando2_name) == 0");
   415     TEST_ASSERT_MSG((somebody->user_id), "somebody->user_id");
   416     TEST_ASSERT_MSG((strcmp(somebody->user_id, expected_rando2_userid) == 0), "strcmp(somebody->user_id, expected_rando2_userid) == 0"); // ???
   417     TEST_ASSERT_MSG((!somebody->me), "!somebody->me"); 
   418     TEST_ASSERT_MSG((somebody->comm_type == PEP_ct_key_not_found), "somebody->comm_type == PEP_ct_key_not_found");
   419     TEST_ASSERT_MSG((strcmp(somebody->address, rando2_address) == 0), "strcmp(somebody->address, rando2_address) == 0");
   420 
   421     cout << "PASS: update_identity() correctly created record with no key" << endl << endl;
   422     free_identity(somebody);
   423 }
   424 
   425 
   426 void NewUpdateIdAndMyselfTests::update_identity_address_only_multiple_records() {
   427     PEP_STATUS status = PEP_STATUS_OK;
   428     // 1. create identity
   429     const char* bella_address = "pep.test.bella@peptest.ch";
   430     const char* bella_fpr = "5631BF1357326A02AA470EEEB815EF7FA4516AAE";
   431     const char* bella_userid = "TOFU_pep.test.bella@peptest.ch"; // simulate temp ID
   432     const char* bella_username = "Annabella the Great";
   433     const string bella_pub_key = slurp("test_keys/pub/pep.test.bella-0xAF516AAE_pub.asc");
   434     
   435     PEP_STATUS statuspub = import_key(session, bella_pub_key.c_str(), bella_pub_key.length(), NULL);
   436     TEST_ASSERT_MSG((statuspub == PEP_TEST_KEY_IMPORT_SUCCESS), "statuspub == PEP_STATUS_OK");
   437 
   438     pEp_identity* bella = new_identity(bella_address, bella_fpr, bella_userid, bella_username);
   439     
   440     // 2. set identity
   441     status = set_identity(session, bella);
   442     TEST_ASSERT_MSG((status == PEP_STATUS_OK), tl_status_string(status));
   443     free_identity(bella);
   444     
   445     const char* not_my_userid = "Bad Company";
   446             
   447     bella = new_identity(bella_address, NULL, not_my_userid, bella_username); 
   448     status = update_identity(session, bella);
   449     TEST_ASSERT_MSG((status == PEP_STATUS_OK), tl_status_string(status));
   450     TEST_ASSERT_MSG((bella->fpr), "bella->fpr");
   451     TEST_ASSERT_MSG((strcmp(bella->fpr, bella_fpr) == 0), "strcmp(bella->fpr, bella_fpr) == 0");
   452     TEST_ASSERT_MSG((bella->username), "bella->username");
   453     TEST_ASSERT_MSG((strcmp(bella->username, bella_username) == 0), "strcmp(bella->username, bella_username) == 0");
   454     TEST_ASSERT_MSG((bella->user_id), "bella->user_id");
   455     TEST_ASSERT_MSG((strcmp(bella->user_id, not_my_userid) == 0), "strcmp(bella->user_id, not_my_userid) == 0"); // ???
   456     TEST_ASSERT_MSG((!bella->me), "!bella->me"); 
   457     TEST_ASSERT_MSG((bella->comm_type == PEP_ct_OpenPGP_unconfirmed), "bella->comm_type == PEP_ct_OpenPGP_unconfirmed");
   458     TEST_ASSERT_MSG((strcmp(bella->address, bella_address) == 0), "strcmp(bella->address, bella_address) == 0");
   459 
   460     free_identity(bella);
   461     
   462     // ???? 
   463     const char* bella_id_2 = "Bella2";
   464     bella = new_identity(bella_address, NULL, bella_id_2, bella_username);
   465     
   466     status = set_identity(session, bella);
   467     TEST_ASSERT_MSG((status == PEP_STATUS_OK), tl_status_string(status));
   468     free_identity(bella);
   469                 
   470     bella = new_identity(bella_address, NULL, NULL, NULL); 
   471     status = update_identity(session, bella);
   472     TEST_ASSERT_MSG((status == PEP_STATUS_OK), tl_status_string(status));
   473 
   474 }
   475 
   476 void NewUpdateIdAndMyselfTests::key_elect_expired_key() {
   477     // 1. create identity
   478     const char* bernd_address = "bernd.das.brot@darthmama.org";
   479     const char* bernd_fpr = "F8CE0F7E24EB190A2FCBFD38D4B088A7CAFAA422";
   480     const char* bernd_userid = "BERND_ID"; // simulate temp ID
   481     const char* bernd_username = "Bernd das Brot der Ultimative Testkandidat";
   482     const string bernd_pub_key = slurp("test_keys/pub/bernd.das.brot-0xCAFAA422_pub.asc");
   483     
   484     PEP_STATUS statuspub = import_key(session, bernd_pub_key.c_str(), bernd_pub_key.length(), NULL);
   485     TEST_ASSERT_MSG((statuspub == PEP_TEST_KEY_IMPORT_SUCCESS), "statuspub == PEP_STATUS_OK");
   486 
   487     pEp_identity* bernd = new_identity(bernd_address, bernd_fpr, bernd_userid, bernd_username);
   488     
   489     // 2. set identity
   490     PEP_STATUS status = set_identity(session, bernd);
   491     TEST_ASSERT_MSG((status == PEP_STATUS_OK), tl_status_string(status));
   492     free_identity(bernd);
   493                 
   494     bernd = new_identity(bernd_address, NULL, bernd_userid, bernd_username); 
   495     status = update_identity(session, bernd);
   496     TEST_ASSERT_MSG((status != PEP_STATUS_OK), "status != PEP_STATUS_OK");
   497     TEST_ASSERT_MSG((!bernd->fpr || bernd->fpr[0] == '\0'), "!bernd->fpr || bernd->fpr[0] == '\0'");
   498     TEST_ASSERT_MSG((bernd->username), "bernd->username");
   499     TEST_ASSERT_MSG((strcmp(bernd->username, bernd_username) == 0), "strcmp(bernd->username, bernd_username) == 0");
   500     TEST_ASSERT_MSG((bernd->user_id), "bernd->user_id");
   501     TEST_ASSERT_MSG((strcmp(bernd->user_id, bernd_userid) == 0), "strcmp(bernd->user_id, bernd_userid) == 0"); // ???
   502     TEST_ASSERT_MSG((!bernd->me), "!bernd->me"); 
   503     TEST_ASSERT_MSG((bernd->comm_type == PEP_ct_key_not_found), tl_ct_string(bernd->comm_type));
   504     TEST_ASSERT_MSG((strcmp(bernd->address, bernd_address) == 0), "strcmp(bernd->address, bernd_address) == 0");
   505 
   506     cout << "PASS: update_identity() correctly rejected expired key with PEP_KEY_UNSUITABLE and PEP_ct_key_not_found" << endl << endl;
   507     free_identity(bernd);
   508     
   509 }
   510 
   511 void NewUpdateIdAndMyselfTests::key_elect_only_revoked_mistrusted() {
   512     // Create id with no key
   513     cout << "Creating new id with no key for : ";
   514     char *uniqname_10000 = strdup("AAAAtestfool@testdomain.org");
   515     for(int i=0; i < 4;i++)
   516         uniqname_10000[i] += random() & 0xf;
   517     
   518     cout << uniqname_10000 << "\n";
   519 
   520     char* revoke_uuid = get_new_uuid();
   521 
   522     pEp_identity * revokemaster_3000 = new_identity(uniqname_10000, NULL, revoke_uuid, start_username);
   523     
   524     cout << "Generate three keys for "  << uniqname_10000 << " who has user_id " << revoke_uuid << endl; 
   525 
   526     char* revoke_fpr_arr[3];
   527     
   528     PEP_STATUS status = generate_keypair(session, revokemaster_3000);
   529     TEST_ASSERT_MSG((status == PEP_STATUS_OK && revokemaster_3000->fpr), (string(tl_status_string(status)) + " " + revokemaster_3000->fpr).c_str());
   530     revoke_fpr_arr[0] = strdup(revokemaster_3000->fpr);
   531     free(revokemaster_3000->fpr);
   532     revokemaster_3000->fpr = NULL;
   533     cout << "revoke_fpr_arr[0] is " << revoke_fpr_arr[0] << endl; 
   534     
   535     status = generate_keypair(session, revokemaster_3000);
   536     TEST_ASSERT_MSG((status == PEP_STATUS_OK && revokemaster_3000->fpr), (string(tl_status_string(status)) + " " + revokemaster_3000->fpr).c_str());
   537     revoke_fpr_arr[1] = strdup(revokemaster_3000->fpr);
   538     free(revokemaster_3000->fpr);
   539     revokemaster_3000->fpr = NULL;
   540     cout << "revoke_fpr_arr[1] is " << revoke_fpr_arr[1] << endl; 
   541     
   542     status = generate_keypair(session, revokemaster_3000);
   543     TEST_ASSERT_MSG((status == PEP_STATUS_OK && revokemaster_3000->fpr), (string(tl_status_string(status)) + " " + revokemaster_3000->fpr).c_str());
   544     revoke_fpr_arr[2] = strdup(revokemaster_3000->fpr);
   545     free(revokemaster_3000->fpr);
   546     revokemaster_3000->fpr = NULL;
   547     cout << "revoke_fpr_arr[2] is " << revoke_fpr_arr[2] << endl; 
   548     
   549     cout << "Trust "  << revoke_fpr_arr[2] << " (default for identity) and " << revoke_fpr_arr[0] << endl;
   550     
   551     free(revokemaster_3000->fpr);
   552     revokemaster_3000->fpr = strdup(revoke_fpr_arr[2]);
   553     status = trust_personal_key(session, revokemaster_3000);
   554     TEST_ASSERT_MSG((status == PEP_STATUS_OK), tl_status_string(status));
   555     status = get_trust(session, revokemaster_3000);
   556     TEST_ASSERT_MSG((status == PEP_STATUS_OK), tl_status_string(status));
   557     TEST_ASSERT_MSG((revokemaster_3000->comm_type & PEP_ct_confirmed), tl_ct_string(revokemaster_3000->comm_type));
   558 
   559     free(revokemaster_3000->fpr);
   560     revokemaster_3000->fpr = strdup(revoke_fpr_arr[0]);
   561     status = trust_personal_key(session, revokemaster_3000);
   562     TEST_ASSERT_MSG((status == PEP_STATUS_OK), tl_status_string(status));
   563     status = get_trust(session, revokemaster_3000);
   564     TEST_ASSERT_MSG((status == PEP_STATUS_OK), tl_status_string(status));
   565     TEST_ASSERT_MSG((revokemaster_3000->comm_type & PEP_ct_confirmed), tl_ct_string(revokemaster_3000->comm_type));
   566     
   567     status = update_identity(session, revokemaster_3000);
   568     TEST_ASSERT_MSG((status == PEP_STATUS_OK), tl_status_string(status));
   569     TEST_ASSERT_MSG((revokemaster_3000->fpr), revokemaster_3000->fpr);
   570     TEST_ASSERT_MSG((strcmp(revokemaster_3000->fpr, revoke_fpr_arr[2]) == 0), (string("Expected ") + revoke_fpr_arr[2] + ", Got " + revokemaster_3000->fpr).c_str());
   571     TEST_ASSERT_MSG((revokemaster_3000->comm_type & PEP_ct_confirmed), tl_ct_string(revokemaster_3000->comm_type));
   572 
   573     cout << "update_identity returns the correct identity default." << endl;
   574     
   575     cout << "Ok, now... we revoke the default..." << endl;
   576     
   577     cout << "Revoking " << revoke_fpr_arr[2] << endl;
   578 
   579     status = revoke_key(session, revoke_fpr_arr[2], "This little pubkey went to market");
   580     TEST_ASSERT (status == PEP_STATUS_OK);
   581 
   582     bool is_revoked;
   583     status = key_revoked(session, revokemaster_3000->fpr, &is_revoked);    
   584     TEST_ASSERT_MSG((status == PEP_STATUS_OK), tl_status_string(status));
   585     TEST_ASSERT_MSG((is_revoked), "is_revoked");
   586 
   587     cout << "Success revoking " << revoke_fpr_arr[2] << "!!! get_trust for this fpr gives us " << revokemaster_3000->comm_type << endl;
   588 
   589 //  BAD ASSUMPTION - this only works if we query the trust DB in elect_pubkey, and we don't.    
   590 //    cout << "Now see if update_identity gives us " << revoke_fpr_arr[0] << ", the only trusted key left." << endl;
   591     status = update_identity(session, revokemaster_3000);
   592     TEST_ASSERT_MSG((status == PEP_STATUS_OK), tl_status_string(status));
   593     TEST_ASSERT_MSG((revokemaster_3000->fpr), revokemaster_3000->fpr);
   594     bool was_key_0 = (strcmp(revokemaster_3000->fpr, revoke_fpr_arr[0]) == 0); 
   595     bool was_key_1 = (strcmp(revokemaster_3000->fpr, revoke_fpr_arr[1]) == 0); 
   596     TEST_ASSERT_MSG(was_key_0 || was_key_1,    
   597                     (string("Expected ") + revoke_fpr_arr[0] + " or " + revoke_fpr_arr[1] + ", Got " + revokemaster_3000->fpr).c_str());                
   598     if (was_key_0) {               
   599         TEST_ASSERT_MSG((revokemaster_3000->comm_type & PEP_ct_confirmed), tl_ct_string(revokemaster_3000->comm_type));    
   600     }    
   601     else {   
   602         TEST_ASSERT_MSG((revokemaster_3000->comm_type & PEP_ct_OpenPGP_unconfirmed), tl_ct_string(revokemaster_3000->comm_type));
   603     }
   604                 
   605     cout << "Success! So let's mistrust " << revoke_fpr_arr[0] << ", because seriously, that key was so uncool." << endl;
   606     
   607     free(revokemaster_3000->fpr);
   608     revokemaster_3000->fpr = strdup(revoke_fpr_arr[0]);
   609     status = key_mistrusted(session, revokemaster_3000);
   610     TEST_ASSERT_MSG((status == PEP_STATUS_OK), tl_status_string(status));
   611 
   612     status = get_trust(session, revokemaster_3000);
   613     TEST_ASSERT_MSG((status == PEP_STATUS_OK), tl_status_string(status));
   614     TEST_ASSERT_MSG((revokemaster_3000->comm_type == PEP_ct_mistrusted), tl_ct_string(revokemaster_3000->comm_type));
   615     
   616     cout << "Success! get_trust for this fpr gives us " << revokemaster_3000->comm_type << endl;
   617 
   618     cout << "The only fpr left is an untrusted one - let's make sure this is what we get from update_identity." << endl;
   619 
   620     status = update_identity(session, revokemaster_3000);
   621     TEST_ASSERT_MSG((status == PEP_STATUS_OK), tl_status_string(status));
   622     TEST_ASSERT_MSG((revokemaster_3000->fpr), revokemaster_3000->fpr);
   623     TEST_ASSERT_MSG((strcmp(revokemaster_3000->fpr, revoke_fpr_arr[1]) == 0), (string("Expected ") + revoke_fpr_arr[1] + ", Got " + revokemaster_3000->fpr).c_str());
   624     TEST_ASSERT_MSG((!(revokemaster_3000->comm_type & PEP_ct_confirmed)), tl_ct_string(revokemaster_3000->comm_type));    
   625 
   626     cout << "Success! We got " << revoke_fpr_arr[1] << "as the fpr with comm_type " << revokemaster_3000->comm_type << endl;
   627     
   628     cout << "But, you know... let's revoke that one too and see what update_identity gives us." << endl;
   629 
   630     status = revoke_key(session, revoke_fpr_arr[1], "Because it's more fun to revoke ALL of someone's keys");
   631     TEST_ASSERT (status == PEP_STATUS_OK);
   632 
   633     status = key_revoked(session, revokemaster_3000->fpr, &is_revoked);    
   634     TEST_ASSERT_MSG((status == PEP_STATUS_OK), tl_status_string(status));
   635     TEST_ASSERT_MSG((is_revoked), "is_revoked");
   636     
   637     cout << "Success! get_trust for this fpr gives us " << revokemaster_3000->comm_type << endl;
   638 
   639     cout << "Call update_identity - we expect nothing, plus an error comm type." << endl;
   640 
   641     status = update_identity(session, revokemaster_3000);
   642     TEST_ASSERT_MSG((status != PEP_STATUS_OK), tl_status_string(status));
   643     TEST_ASSERT_MSG((!revokemaster_3000->fpr), revokemaster_3000->fpr);
   644     TEST_ASSERT_MSG((revokemaster_3000->username), "No revokemaster_3000->username");
   645     TEST_ASSERT_MSG((strcmp(revokemaster_3000->user_id, revoke_uuid) == 0), (string("Expected ") + revoke_uuid + ", Got " + revokemaster_3000->user_id).c_str());
   646     TEST_ASSERT_MSG((revokemaster_3000->comm_type == PEP_ct_key_not_found), tl_ct_string(revokemaster_3000->comm_type));
   647     cout << "Success! No key found. The comm_status error was " << revokemaster_3000->comm_type << "and the return status was " << tl_status_string(status) << endl;
   648 
   649     free_identity(revokemaster_3000);    
   650 }