test/new_update_id_and_myself_test.cc
author Krista Bennett <krista@pep-project.org>
Mon, 29 Jan 2018 12:15:51 +0100
changeset 2461 85c8e5aad3bc
child 2462 48b526a0daac
permissions -rw-r--r--
Urgh. Fixing bad merge (???)
krista@2461
     1
// This file is under GNU General Public License 3.0
krista@2461
     2
// see LICENSE.txt
krista@2461
     3
krista@2461
     4
#include <iostream>
krista@2461
     5
#include <iostream>
krista@2461
     6
#include <fstream>
krista@2461
     7
#include <string>
krista@2461
     8
#include <cstring> // for strcmp()
krista@2461
     9
#include <assert.h>
krista@2461
    10
#include "pEpEngine.h"
krista@2461
    11
#include "message_api.h"
krista@2461
    12
#include "keymanagement.h"
krista@2461
    13
#include "test_util.h"
krista@2461
    14
krista@2461
    15
using namespace std;
krista@2461
    16
krista@2461
    17
int main() {
krista@2461
    18
    cout << "\n*** test update_identity and myself ***\n\n";
krista@2461
    19
    
krista@2461
    20
    PEP_SESSION session;
krista@2461
    21
    
krista@2461
    22
    cout << "calling init()\n";
krista@2461
    23
    PEP_STATUS status = init(&session);
krista@2461
    24
    assert(status == PEP_STATUS_OK);
krista@2461
    25
    assert(session);
krista@2461
    26
    cout << "init() completed.\n";
krista@2461
    27
    cout << endl;
krista@2461
    28
    cout << "***********************************************************************" << endl;
krista@2461
    29
    cout << "* Section I. myself()" << endl;
krista@2461
    30
    cout << "***********************************************************************" << endl << endl;
krista@2461
    31
krista@2461
    32
    // Create id with no key
krista@2461
    33
    cout << "Creating new own id with no key for : ";
krista@2461
    34
    char *uniqname = strdup("AAAAtestuser@testdomain.org");
krista@2461
    35
    srandom(time(NULL));
krista@2461
    36
    for(int i=0; i < 4;i++)
krista@2461
    37
        uniqname[i] += random() & 0xf;
krista@2461
    38
    
krista@2461
    39
    cout << uniqname << "\n";
krista@2461
    40
    
krista@2461
    41
    const char* own_user_id = "FineOwnIdentitiesOfBuckTFerris";
krista@2461
    42
    const char* start_username = "Unser Testkandidat";
krista@2461
    43
krista@2461
    44
    pEp_identity * new_me = new_identity(uniqname, NULL, own_user_id, start_username);
krista@2461
    45
    
krista@2461
    46
    cout << "***********************************************************************" << endl;
krista@2461
    47
    cout << "* I: 1. myself() on id with no record in the DB and no input fpr" << endl;
krista@2461
    48
    cout << "***********************************************************************" << endl << endl;
krista@2461
    49
    status = myself(session, new_me);
krista@2461
    50
    assert(status == PEP_STATUS_OK);
krista@2461
    51
    assert(new_me->fpr);
krista@2461
    52
    
krista@2461
    53
    cout << "PASS: myself() generated fingerprint ";
krista@2461
    54
    cout << new_me->fpr << endl << endl;
krista@2461
    55
krista@2461
    56
    char* generated_fpr = strdup(new_me->fpr);
krista@2461
    57
    
krista@2461
    58
    assert(new_me->comm_type == PEP_ct_pEp);
krista@2461
    59
    
krista@2461
    60
    free_identity(new_me);
krista@2461
    61
krista@2461
    62
    cout << "***********************************************************************" << endl;
krista@2461
    63
    cout << "* I: 2. myself() on id with no input fpr and a record in the DB" << endl;
krista@2461
    64
    cout << "***********************************************************************" << endl << endl;
krista@2461
    65
krista@2461
    66
    new_me = new_identity(uniqname, NULL, own_user_id, NULL);
krista@2461
    67
    status = myself(session, new_me);
krista@2461
    68
    assert(status == PEP_STATUS_OK);
krista@2461
    69
    
krista@2461
    70
    assert(new_me->fpr);
krista@2461
    71
    assert(strcmp(new_me->fpr, generated_fpr) == 0);
krista@2461
    72
    assert(new_me->username);
krista@2461
    73
    assert(strcmp(new_me->username, start_username) == 0);
krista@2461
    74
    assert(new_me->user_id);
krista@2461
    75
    assert(new_me->comm_type == PEP_ct_pEp);
krista@2461
    76
    
krista@2461
    77
    char* default_own_id = NULL;
krista@2461
    78
    status = get_userid_alias_default(session, own_user_id, &default_own_id);
krista@2461
    79
    if (status == PEP_CANNOT_FIND_ALIAS) {
krista@2461
    80
        // Ok, we presume our own id above is the default (should be true if there was no existing DB as in test env)
krista@2461
    81
        default_own_id = strdup(own_user_id);
krista@2461
    82
    }
krista@2461
    83
krista@2461
    84
    assert(strcmp(new_me->user_id, default_own_id) == 0);
krista@2461
    85
    
krista@2461
    86
    cout << "PASS: myself() retrieved the correct fpr, username and default user id" << endl << endl;
krista@2461
    87
krista@2461
    88
    free_identity(new_me);
krista@2461
    89
     
krista@2461
    90
    cout << "****************************************************************************************" << endl;
krista@2461
    91
    cout << "* I: 3. myself() on id with no input fpr, a different user_id, and a record in the DB" << endl;
krista@2461
    92
    cout << "****************************************************************************************" << endl << endl;
krista@2461
    93
krista@2461
    94
    const char* alias_id = "Huss Es El Mejor Presidente Del Mundo!";
krista@2461
    95
krista@2461
    96
    new_me = new_identity(uniqname, NULL, alias_id, NULL);
krista@2461
    97
    status = myself(session, new_me);
krista@2461
    98
    assert(status == PEP_STATUS_OK);
krista@2461
    99
    
krista@2461
   100
    assert(new_me->fpr);
krista@2461
   101
    assert(strcmp(new_me->fpr, generated_fpr) == 0);
krista@2461
   102
    assert(new_me->username);
krista@2461
   103
    assert(strcmp(new_me->username, start_username) == 0);
krista@2461
   104
    assert(new_me->user_id);
krista@2461
   105
    assert(strcmp(new_me->user_id, default_own_id) == 0);
krista@2461
   106
    assert(new_me->comm_type == PEP_ct_pEp);
krista@2461
   107
    
krista@2461
   108
    char* tmp_def = NULL;
krista@2461
   109
    
krista@2461
   110
    status = get_userid_alias_default(session, alias_id, &tmp_def);
krista@2461
   111
    assert(status == PEP_STATUS_OK);
krista@2461
   112
    assert(strcmp(tmp_def, default_own_id) == 0);
krista@2461
   113
krista@2461
   114
    cout << "PASS: myself() retrieved the correct fpr, username and default user id, and put the right alias in for the default";
krista@2461
   115
    cout << endl << endl;
krista@2461
   116
    
krista@2461
   117
    free(tmp_def);
krista@2461
   118
    free_identity(new_me);
krista@2461
   119
krista@2461
   120
    cout << "****************************************************************************************" << endl;
krista@2461
   121
    cout << "* I: 4. myself(), replace fpr" << endl;
krista@2461
   122
    cout << "****************************************************************************************" << endl << endl;
krista@2461
   123
krista@2461
   124
    new_me = new_identity(uniqname, NULL, alias_id, start_username);
krista@2461
   125
    status = generate_keypair(session, new_me);
krista@2461
   126
    assert(new_me->fpr);
krista@2461
   127
    
krista@2461
   128
    cout << "Generated fingerprint ";
krista@2461
   129
    cout << new_me->fpr << "\n";
krista@2461
   130
krista@2461
   131
    char* new_fpr = strdup(new_me->fpr);
krista@2461
   132
krista@2461
   133
    new_me = new_identity(uniqname, new_fpr, alias_id, NULL);
krista@2461
   134
krista@2461
   135
    status = myself(session, new_me);
krista@2461
   136
    assert(status == PEP_STATUS_OK);
krista@2461
   137
    assert(new_me->fpr);
krista@2461
   138
    assert(strcmp(new_me->fpr, generated_fpr) != 0);
krista@2461
   139
    assert(strcmp(new_me->fpr, new_fpr) == 0);
krista@2461
   140
    assert(new_me->username);
krista@2461
   141
    assert(strcmp(new_me->username, start_username) == 0);
krista@2461
   142
    assert(new_me->user_id);
krista@2461
   143
    assert(strcmp(new_me->user_id, default_own_id) == 0);
krista@2461
   144
    assert(new_me->me);
krista@2461
   145
    assert(new_me->comm_type == PEP_ct_pEp);
krista@2461
   146
krista@2461
   147
    cout << "PASS: myself() set and retrieved the new fpr, username and default user id, and put the right alias in for the default";
krista@2461
   148
    cout << endl << endl;
krista@2461
   149
krista@2461
   150
    // since that worked, we'll set it back as the default
krista@2461
   151
    free(new_me->fpr);
krista@2461
   152
    new_me->fpr = strdup(generated_fpr);
krista@2461
   153
    new_me->comm_type = PEP_ct_unknown;
krista@2461
   154
    status = myself(session, new_me);
krista@2461
   155
    assert(status == PEP_STATUS_OK);
krista@2461
   156
    assert(strcmp(new_me->fpr, generated_fpr) == 0);
krista@2461
   157
    assert(new_me->comm_type == PEP_ct_pEp);
krista@2461
   158
    
krista@2461
   159
    cout << "****************************************************************************************" << endl;
krista@2461
   160
    cout << "* I: 5. myself(), replace fpr, revoke key" << endl;
krista@2461
   161
    cout << "****************************************************************************************" << endl << endl;
krista@2461
   162
krista@2461
   163
    status = revoke_key(session, generated_fpr, "Because it's fun");
krista@2461
   164
    assert (status == PEP_STATUS_OK);
krista@2461
   165
    
krista@2461
   166
    new_me = new_identity(uniqname, new_fpr, alias_id, NULL);
krista@2461
   167
    
krista@2461
   168
    status = myself(session, new_me);
krista@2461
   169
    assert(status == PEP_STATUS_OK);
krista@2461
   170
    assert(new_me->fpr);
krista@2461
   171
    assert(strcmp(new_me->fpr, generated_fpr) != 0);
krista@2461
   172
    assert(new_me->username);
krista@2461
   173
    assert(strcmp(new_me->username, start_username) == 0);
krista@2461
   174
    assert(new_me->user_id);
krista@2461
   175
    assert(strcmp(new_me->user_id, default_own_id) == 0);
krista@2461
   176
    assert(new_me->me);
krista@2461
   177
    assert(new_me->comm_type == PEP_ct_pEp);
krista@2461
   178
    
krista@2461
   179
    cout << "PASS: myself() retrieved the new fpr, username and default user id, and put the right alias in for the default";
krista@2461
   180
    cout << endl << endl;
krista@2461
   181
        
krista@2461
   182
    cout << "***********************************************************************" << endl;
krista@2461
   183
    cout << "* Section II. update_identity()" << endl;
krista@2461
   184
    cout << "***********************************************************************" << endl << endl;
krista@2461
   185
krista@2461
   186
    cout << "****************************************************************************************" << endl;
krista@2461
   187
    cout << "* II: 1. update_identity() - get identity with matching address and user_id and username" << endl;
krista@2461
   188
    cout << "****************************************************************************************" << endl << endl;    
krista@2461
   189
    // 1. create original identity
krista@2461
   190
    const char* alex_address = "pep.test.alexander@peptest.ch";
krista@2461
   191
    const char* alex_fpr = "3AD9F60FAEB22675DB873A1362D6981326B54E4E";
krista@2461
   192
    const char* alex_userid = "Alex";
krista@2461
   193
    const char* alex_username = "SuperDuperAlex";
krista@2461
   194
    const string alex_pub_key = slurp("test_keys/pub/pep.test.alexander-0x26B54E4E_pub.asc");
krista@2461
   195
    
krista@2461
   196
    PEP_STATUS statuspub = import_key(session, alex_pub_key.c_str(), alex_pub_key.length(), NULL);
krista@2461
   197
    assert(statuspub == PEP_STATUS_OK);
krista@2461
   198
krista@2461
   199
    pEp_identity* alex = new_identity(alex_address, alex_fpr, alex_userid, alex_username);
krista@2461
   200
krista@2461
   201
    // 2. set identity
krista@2461
   202
    status = set_identity(session, alex);
krista@2461
   203
    assert(status == PEP_STATUS_OK);
krista@2461
   204
    free_identity(alex);
krista@2461
   205
            
krista@2461
   206
    alex = new_identity(alex_address, NULL, alex_userid, alex_username); 
krista@2461
   207
    status = update_identity(session, alex);
krista@2461
   208
    assert(status == PEP_STATUS_OK);
krista@2461
   209
    assert(alex->fpr);
krista@2461
   210
    assert(strcmp(alex->fpr, alex_fpr) == 0);
krista@2461
   211
    assert(alex->username);
krista@2461
   212
    assert(strcmp(alex->username, alex_username) == 0);
krista@2461
   213
    assert(alex->user_id);
krista@2461
   214
    assert(strcmp(alex->user_id, alex_userid) == 0);
krista@2461
   215
    assert(!alex->me); 
krista@2461
   216
    assert(alex->comm_type == PEP_ct_OpenPGP_unconfirmed);
krista@2461
   217
    assert(strcmp(alex->address, alex_address) == 0);
krista@2461
   218
krista@2461
   219
    cout << "PASS: update_identity() correctly retrieved extant record with matching address, id, and username" << endl << endl;
krista@2461
   220
    free_identity(alex);
krista@2461
   221
krista@2461
   222
    cout << "****************************************************************************************" << endl;
krista@2461
   223
    cout << "* II: 2. update_identity() - get identity with matching address and user_id and new username" << endl;
krista@2461
   224
    cout << "****************************************************************************************" << endl << endl;    
krista@2461
   225
krista@2461
   226
    const char* new_username = "Test Patchy";
krista@2461
   227
            
krista@2461
   228
    alex = new_identity(alex_address, NULL, alex_userid, new_username); 
krista@2461
   229
    status = update_identity(session, alex);
krista@2461
   230
    assert(status == PEP_STATUS_OK);
krista@2461
   231
    assert(alex->fpr);
krista@2461
   232
    assert(strcmp(alex->fpr, alex_fpr) == 0);
krista@2461
   233
    assert(alex->username);
krista@2461
   234
    assert(strcmp(alex->username, new_username) == 0);
krista@2461
   235
    assert(alex->user_id);
krista@2461
   236
    assert(strcmp(alex->user_id, alex_userid) == 0);
krista@2461
   237
    assert(!alex->me); 
krista@2461
   238
    assert(alex->comm_type == PEP_ct_OpenPGP_unconfirmed);
krista@2461
   239
    assert(strcmp(alex->address, alex_address) == 0);
krista@2461
   240
krista@2461
   241
    cout << "PASS: update_identity() correctly retrieved extant record with matching address and id, and patched username" << endl << endl;
krista@2461
   242
    free_identity(alex);
krista@2461
   243
krista@2461
   244
    cout << "****************************************************************************************" << endl;
krista@2461
   245
    cout << "* II: 3. update_identity() - get identity with matching address and user_id only" << endl;
krista@2461
   246
    cout << "****************************************************************************************" << endl << endl;    
krista@2461
   247
        
krista@2461
   248
    alex = new_identity(alex_address, NULL, alex_userid, NULL); 
krista@2461
   249
    status = update_identity(session, alex);
krista@2461
   250
    assert(status == PEP_STATUS_OK);
krista@2461
   251
    assert(alex->fpr);
krista@2461
   252
    assert(strcmp(alex->fpr, alex_fpr) == 0);
krista@2461
   253
    assert(alex->username);
krista@2461
   254
    assert(strcmp(alex->username, new_username) == 0);
krista@2461
   255
    assert(alex->user_id);
krista@2461
   256
    assert(strcmp(alex->user_id, alex_userid) == 0);
krista@2461
   257
    assert(!alex->me); 
krista@2461
   258
    assert(alex->comm_type == PEP_ct_OpenPGP_unconfirmed);
krista@2461
   259
    assert(strcmp(alex->address, alex_address) == 0);
krista@2461
   260
krista@2461
   261
    cout << "PASS: update_identity() correctly retrieved extant record with matching address and id, and patched username" << endl << endl;
krista@2461
   262
    free_identity(alex);
krista@2461
   263
krista@2461
   264
    cout << "****************************************************************************************" << endl;
krista@2461
   265
    cout << "* II: 4. update_identity() - get identity with just address and username" << endl;
krista@2461
   266
    cout << "****************************************************************************************" << endl << endl;    
krista@2461
   267
krista@2461
   268
    alex = new_identity(alex_address, NULL, NULL, new_username); 
krista@2461
   269
    status = update_identity(session, alex);
krista@2461
   270
    assert(status == PEP_STATUS_OK);
krista@2461
   271
    assert(alex->fpr);
krista@2461
   272
    assert(strcmp(alex->fpr, alex_fpr) == 0);
krista@2461
   273
    assert(alex->username);
krista@2461
   274
    assert(strcmp(alex->username, new_username) == 0);
krista@2461
   275
    assert(alex->user_id);
krista@2461
   276
    assert(strcmp(alex->user_id, alex_userid) == 0);
krista@2461
   277
    assert(!alex->me); 
krista@2461
   278
    assert(alex->comm_type == PEP_ct_OpenPGP_unconfirmed);
krista@2461
   279
    assert(strcmp(alex->address, alex_address) == 0);
krista@2461
   280
krista@2461
   281
    cout << "PASS: update_identity() correctly retrieved extant record with matching address and username" << endl << endl;
krista@2461
   282
    free_identity(alex);
krista@2461
   283
krista@2461
   284
    cout << "****************************************************************************************" << endl;
krista@2461
   285
    cout << "* II: 5. update_identity() with just address " << endl;
krista@2461
   286
    cout << "****************************************************************************************" << endl << endl;
krista@2461
   287
    
krista@2461
   288
    alex = new_identity(alex_address, NULL, NULL, NULL); 
krista@2461
   289
    status = update_identity(session, alex);
krista@2461
   290
    assert(status == PEP_STATUS_OK);
krista@2461
   291
    assert(alex->fpr);
krista@2461
   292
    assert(strcmp(alex->fpr, alex_fpr) == 0);
krista@2461
   293
    assert(alex->username);
krista@2461
   294
    assert(strcmp(alex->username, new_username) == 0);
krista@2461
   295
    assert(alex->user_id);
krista@2461
   296
    assert(strcmp(alex->user_id, alex_userid) == 0);
krista@2461
   297
    assert(!alex->me); 
krista@2461
   298
    assert(alex->comm_type == PEP_ct_OpenPGP_unconfirmed);
krista@2461
   299
    assert(strcmp(alex->address, alex_address) == 0);
krista@2461
   300
krista@2461
   301
    cout << "PASS: update_identity() correctly retrieved extant record with just matching address. Retrieved previously patched username." << endl << endl;
krista@2461
   302
    free_identity(alex);
krista@2461
   303
krista@2461
   304
krista@2461
   305
    cout << "****************************************************************************************" << endl;
krista@2461
   306
    cout << "* II: 6. update_identity() with just address on own identity (only case where this is legal)" << endl;
krista@2461
   307
    cout << "****************************************************************************************" << endl << endl;
krista@2461
   308
    
krista@2461
   309
    pEp_identity* somebody = new_identity(uniqname, NULL, NULL, NULL); 
krista@2461
   310
    status = update_identity(session, somebody);
krista@2461
   311
    assert(status == PEP_STATUS_OK);
krista@2461
   312
    myself(session, somebody);
krista@2461
   313
    assert(somebody->fpr);
krista@2461
   314
    assert(strcmp(somebody->fpr, new_fpr) == 0);
krista@2461
   315
    assert(somebody->username);
krista@2461
   316
    assert(strcmp(somebody->username, start_username) == 0);
krista@2461
   317
    assert(somebody->user_id);
krista@2461
   318
    assert(strcmp(somebody->user_id, default_own_id) == 0);
krista@2461
   319
    assert(somebody->me); // true in this case, as it was an own identity
krista@2461
   320
    assert(somebody->comm_type == PEP_ct_pEp);
krista@2461
   321
    assert(strcmp(somebody->address, uniqname) == 0);
krista@2461
   322
    
krista@2461
   323
    cout << "PASS: update_identity() retrieved the right identity information given just an address";
krista@2461
   324
    cout << endl << endl;
krista@2461
   325
krista@2461
   326
    free_identity(somebody);
krista@2461
   327
krista@2461
   328
    cout << "****************************************************************************************" << endl;
krista@2461
   329
    cout << "* II: 7. update_identity() for address and user_id that don't exist" << endl;
krista@2461
   330
    cout << "****************************************************************************************" << endl << endl;
krista@2461
   331
krista@2461
   332
    somebody = new_identity("nope@nope.nope", NULL, "some_user_id", NULL); 
krista@2461
   333
    status = update_identity(session, somebody);
krista@2461
   334
    assert(status == PEP_CANNOT_FIND_IDENTITY);
krista@2461
   335
    cout << "PASS: update_identity() returns PEP_CANNOT_FIND_IDENTITY" << endl << endl;
krista@2461
   336
krista@2461
   337
    free_identity(somebody);
krista@2461
   338
    
krista@2461
   339
    cout << "****************************************************************************************" << endl;
krista@2461
   340
    cout << "* II: 8. update_identity() for address and and username, but non-matching temp user_id" << endl;
krista@2461
   341
    cout << "****************************************************************************************" << endl << endl;
krista@2461
   342
krista@2461
   343
    // 1. create identity
krista@2461
   344
    const char* bella_address = "pep.test.bella@peptest.ch";
krista@2461
   345
    const char* bella_fpr = "5631BF1357326A02AA470EEEB815EF7FA4516AAE";
krista@2461
   346
    const char* bella_userid = "TOFU_pep.test.bella@peptest.ch"; // simulate temp ID
krista@2461
   347
    const char* bella_username = "Annabella the Great";
krista@2461
   348
    const string bella_pub_key = slurp("test_keys/pub/pep.test.bella-0xAF516AAE_pub.asc");
krista@2461
   349
    
krista@2461
   350
    statuspub = import_key(session, bella_pub_key.c_str(), bella_pub_key.length(), NULL);
krista@2461
   351
    assert(statuspub == PEP_STATUS_OK);
krista@2461
   352
krista@2461
   353
    pEp_identity* bella = new_identity(bella_address, bella_fpr, bella_userid, bella_username);
krista@2461
   354
    
krista@2461
   355
    // 2. set identity
krista@2461
   356
    status = set_identity(session, bella);
krista@2461
   357
    assert(status == PEP_STATUS_OK);
krista@2461
   358
    free_identity(bella);
krista@2461
   359
    
krista@2461
   360
    const char* not_my_userid = "Bad Company";
krista@2461
   361
            
krista@2461
   362
    bella = new_identity(bella_address, NULL, not_my_userid, bella_username); 
krista@2461
   363
    status = update_identity(session, bella);
krista@2461
   364
    assert(status == PEP_STATUS_OK);
krista@2461
   365
    assert(bella->fpr);
krista@2461
   366
    assert(strcmp(bella->fpr, bella_fpr) == 0);
krista@2461
   367
    assert(bella->username);
krista@2461
   368
    assert(strcmp(bella->username, bella_username) == 0);
krista@2461
   369
    assert(bella->user_id);
krista@2461
   370
    assert(strcmp(bella->user_id, not_my_userid) == 0); // ???
krista@2461
   371
    assert(!bella->me); 
krista@2461
   372
    assert(bella->comm_type == PEP_ct_OpenPGP_unconfirmed);
krista@2461
   373
    assert(strcmp(bella->address, bella_address) == 0);
krista@2461
   374
krista@2461
   375
    cout << "PASS: update_identity() correctly retrieved extant record with matching address and username; temp user_id in DB patched" << endl << endl;
krista@2461
   376
    free_identity(bella);
krista@2461
   377
krista@2461
   378
    cout << "****************************************************************************************" << endl;
krista@2461
   379
    cout << "* II: 9. update_identity() for address, username, and user_id, but no matching record" << endl;
krista@2461
   380
    cout << "****************************************************************************************" << endl << endl;
krista@2461
   381
    
krista@2461
   382
    const char* rando_name = "Pickley BoofBoof";
krista@2461
   383
    const char* rando_userid = "Boofy";
krista@2461
   384
    const char* rando_address = "boof@pickles.org";
krista@2461
   385
    somebody = new_identity(rando_address, NULL, rando_userid, rando_name);
krista@2461
   386
    status = update_identity(session, somebody);
krista@2461
   387
krista@2461
   388
    assert(status == PEP_STATUS_OK);
krista@2461
   389
    assert(!somebody->fpr || somebody->fpr[0] == '\0');
krista@2461
   390
    assert(somebody->username);
krista@2461
   391
    assert(strcmp(somebody->username, rando_name) == 0);
krista@2461
   392
    assert(somebody->user_id);
krista@2461
   393
    assert(strcmp(somebody->user_id, rando_userid) == 0); // ???
krista@2461
   394
    assert(!somebody->me); 
krista@2461
   395
    assert(somebody->comm_type == PEP_ct_key_not_found);
krista@2461
   396
    assert(strcmp(somebody->address, rando_address) == 0);
krista@2461
   397
krista@2461
   398
    cout << "PASS: update_identity() correctly created record with no key" << endl << endl;
krista@2461
   399
    free_identity(somebody);
krista@2461
   400
    
krista@2461
   401
    cout << "****************************************************************************************" << endl;
krista@2461
   402
    cout << "* II: 10. update_identity() for address, username, but no matching record" << endl;
krista@2461
   403
    cout << "****************************************************************************************" << endl << endl;
krista@2461
   404
krista@2461
   405
    const char* rando2_name = "Pickles BoofyBoof";
krista@2461
   406
    const char* rando2_address = "boof2@pickles.org";
krista@2461
   407
    somebody = new_identity(rando2_address, NULL, NULL, rando2_name);
krista@2461
   408
    status = update_identity(session, somebody);
krista@2461
   409
    const char* expected_rando2_userid = "TOFU_boof2@pickles.org";
krista@2461
   410
krista@2461
   411
    assert(status == PEP_STATUS_OK);
krista@2461
   412
    assert(!somebody->fpr || somebody->fpr[0] == '\0');
krista@2461
   413
    assert(somebody->username);
krista@2461
   414
    assert(strcmp(somebody->username, rando2_name) == 0);
krista@2461
   415
    assert(somebody->user_id);
krista@2461
   416
    assert(strcmp(somebody->user_id, expected_rando2_userid) == 0); // ???
krista@2461
   417
    assert(!somebody->me); 
krista@2461
   418
    assert(somebody->comm_type == PEP_ct_key_not_found);
krista@2461
   419
    assert(strcmp(somebody->address, rando2_address) == 0);
krista@2461
   420
krista@2461
   421
    cout << "PASS: update_identity() correctly created record with no key" << endl << endl;
krista@2461
   422
    free_identity(somebody);
krista@2461
   423
krista@2461
   424
    cout << "****************************************************************************************" << endl;
krista@2461
   425
    cout << "* II: 11. update_identity() for address only, but multiple matching records" << endl;
krista@2461
   426
    cout << "****************************************************************************************" << endl << endl;
krista@2461
   427
krista@2461
   428
    const char* bella_id_2 = "Bella2";
krista@2461
   429
    bella = new_identity(bella_address, NULL, bella_id_2, bella_username);
krista@2461
   430
    
krista@2461
   431
    // 2. set identity
krista@2461
   432
    status = set_identity(session, bella);
krista@2461
   433
    assert(status == PEP_STATUS_OK);
krista@2461
   434
    free_identity(bella);
krista@2461
   435
                
krista@2461
   436
    bella = new_identity(bella_address, NULL, NULL, NULL); 
krista@2461
   437
    status = update_identity(session, bella);
krista@2461
   438
    assert(status != PEP_STATUS_OK);
krista@2461
   439
krista@2461
   440
    cout << "PASS: update_identity() correctly failed with no matching records (too little info)" << endl << endl;
krista@2461
   441
    
krista@2461
   442
    cout << "****************************************************************************************" << endl;
krista@2461
   443
    cout << "* III: key election " << endl;
krista@2461
   444
    cout << "****************************************************************************************" << endl << endl;
krista@2461
   445
krista@2461
   446
    cout << "****************************************************************************************" << endl;
krista@2461
   447
    cout << "* III: 1. key election: get identity for user with expired key" << endl;
krista@2461
   448
    cout << "****************************************************************************************" << endl << endl;
krista@2461
   449
krista@2461
   450
    // 1. create identity
krista@2461
   451
    const char* bernd_address = "bernd.das.brot@darthmama.org";
krista@2461
   452
    const char* bernd_fpr = "F8CE0F7E24EB190A2FCBFD38D4B088A7CAFAA422";
krista@2461
   453
    const char* bernd_userid = "BERND_ID"; // simulate temp ID
krista@2461
   454
    const char* bernd_username = "Bernd das Brot der Ultimative Testkandidat";
krista@2461
   455
    const string bernd_pub_key = slurp("test_keys/pub/bernd.das.brot-0xCAFAA422_pub.asc");
krista@2461
   456
    
krista@2461
   457
    statuspub = import_key(session, bernd_pub_key.c_str(), bernd_pub_key.length(), NULL);
krista@2461
   458
    assert(statuspub == PEP_STATUS_OK);
krista@2461
   459
krista@2461
   460
    pEp_identity* bernd = new_identity(bernd_address, bernd_fpr, bernd_userid, bernd_username);
krista@2461
   461
    
krista@2461
   462
    // 2. set identity
krista@2461
   463
    status = set_identity(session, bernd);
krista@2461
   464
    assert(status == PEP_STATUS_OK);
krista@2461
   465
    free_identity(bernd);
krista@2461
   466
                
krista@2461
   467
    bernd = new_identity(bernd_address, NULL, bernd_userid, bernd_username); 
krista@2461
   468
    status = update_identity(session, bernd);
krista@2461
   469
    assert(status != PEP_STATUS_OK);
krista@2461
   470
    assert(!bernd->fpr || bernd->fpr[0] == '\0');
krista@2461
   471
    assert(bernd->username);
krista@2461
   472
    assert(strcmp(bernd->username, bernd_username) == 0);
krista@2461
   473
    assert(bernd->user_id);
krista@2461
   474
    assert(strcmp(bernd->user_id, bernd_userid) == 0); // ???
krista@2461
   475
    assert(!bernd->me); 
krista@2461
   476
    assert(bernd->comm_type == PEP_ct_key_expired);
krista@2461
   477
    assert(strcmp(bernd->address, bernd_address) == 0);
krista@2461
   478
krista@2461
   479
    cout << "PASS: update_identity() correctly rejected expired key with PEP_KEY_UNSUITABLE and PEP_ct_key_expired" << endl << endl;
krista@2461
   480
    free_identity(bernd);
krista@2461
   481
krista@2461
   482
krista@2461
   483
    cout << "****************************************************************************************" << endl;
krista@2461
   484
    cout << "* III: 2. key election: get identity for user with only revoked keys " << endl;
krista@2461
   485
    cout << "****************************************************************************************" << endl << endl;
krista@2461
   486
    
krista@2461
   487
    status = revoke_key(session, new_fpr, "Because it's more fun to revoke ALL of someone's keys");
krista@2461
   488
    assert (status == PEP_STATUS_OK);
krista@2461
   489
    
krista@2461
   490
    new_me = new_identity(uniqname, NULL, NULL, NULL);
krista@2461
   491
    
krista@2461
   492
    status = update_identity(session, new_me);
krista@2461
   493
    assert(status != PEP_STATUS_OK);
krista@2461
   494
    assert(!new_me->fpr);
krista@2461
   495
    assert(new_me->username);
krista@2461
   496
    assert(strcmp(new_me->username, start_username) == 0);
krista@2461
   497
    assert(new_me->user_id);
krista@2461
   498
    assert(strcmp(new_me->user_id, default_own_id) == 0);
krista@2461
   499
    assert(new_me->me);
krista@2461
   500
    assert(new_me->comm_type == PEP_ct_key_revoked);
krista@2461
   501
    
krista@2461
   502
    cout << "PASS: update_identity() correctly rejected two revoked keys with PEP_KEY_UNSUITABLE and PEP_ct_key_revoked";
krista@2461
   503
    cout << endl << endl;
krista@2461
   504
krista@2461
   505
    free_identity(new_me);
krista@2461
   506
krista@2461
   507
    cout << "****************************************************************************************" << endl;
krista@2461
   508
    cout << "* III: 100000000. key election: more to come " << endl;
krista@2461
   509
    cout << "****************************************************************************************" << endl << endl;
krista@2461
   510
krista@2461
   511
    return 0;
krista@2461
   512
}