test/src/engine_tests/KeyResetMessageTests.cc
author Krista 'DarthMama' Bennett <krista@pep.foundation>
Mon, 25 Feb 2019 12:28:51 +0100
branchsync_test_refactor
changeset 3314 18a40b81649a
parent 3296 fe7cbfc8e84f
child 3335 09af2cf0ac74
permissions -rw-r--r--
Ok, doing better, but we still have a memory problem in the single default device case.
krista@2892
     1
// This file is under GNU General Public License 3.0
krista@2892
     2
// see LICENSE.txt
krista@2892
     3
krista@2892
     4
#include <stdlib.h>
krista@2892
     5
#include <string>
krista@2920
     6
#include <vector>
krista@2920
     7
#include <unordered_map>
krista@2917
     8
#include <assert.h>
krista@2892
     9
krista@2892
    10
#include "pEpEngine.h"
krista@2917
    11
#include "pEp_internal.h"
krista@2924
    12
#include "mime.h"
krista@2933
    13
#include "keymanagement.h"
krista@2892
    14
krista@3292
    15
#include "TestUtils.h"
krista@2892
    16
#include "EngineTestIndividualSuite.h"
krista@2892
    17
#include "KeyResetMessageTests.h"
krista@2892
    18
krista@2892
    19
using namespace std;
krista@2892
    20
krista@2920
    21
const string KeyResetMessageTests::alice_user_id = PEP_OWN_USERID;
krista@2920
    22
const string KeyResetMessageTests::bob_user_id = "BobId";    
krista@2920
    23
const string KeyResetMessageTests::carol_user_id = "carolId";
krista@2920
    24
const string KeyResetMessageTests::dave_user_id = "DaveId";
krista@2920
    25
const string KeyResetMessageTests::erin_user_id = "ErinErinErin";
krista@2920
    26
const string KeyResetMessageTests::fenris_user_id = "BadWolf";
krista@2920
    27
krista@2956
    28
KeyResetMessageTests* KeyResetMessageTests::fake_this = NULL;
krista@2956
    29
krista@2892
    30
KeyResetMessageTests::KeyResetMessageTests(string suitename, string test_home_dir) :
krista@2892
    31
    EngineTestIndividualSuite::EngineTestIndividualSuite(suitename, test_home_dir) {
krista@2920
    32
        
krista@2892
    33
    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("KeyResetMessageTests::check_key_reset_message"),
krista@2892
    34
                                                                      static_cast<Func>(&KeyResetMessageTests::check_key_reset_message)));
krista@2917
    35
    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("KeyResetMessageTests::check_reset_key_and_notify"),
krista@2917
    36
                                                                      static_cast<Func>(&KeyResetMessageTests::check_reset_key_and_notify)));
krista@2940
    37
    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("KeyResetMessageTests::check_non_reset_receive_revoked"),
krista@2940
    38
                                                                      static_cast<Func>(&KeyResetMessageTests::check_non_reset_receive_revoked)));
krista@2940
    39
    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("KeyResetMessageTests::check_reset_receive_revoked"),
krista@2940
    40
                                                                      static_cast<Func>(&KeyResetMessageTests::check_reset_receive_revoked)));
krista@2942
    41
    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("KeyResetMessageTests::check_receive_message_to_revoked_key_from_unknown"),
krista@2942
    42
                                                                      static_cast<Func>(&KeyResetMessageTests::check_receive_message_to_revoked_key_from_unknown)));
krista@2942
    43
    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("KeyResetMessageTests::check_receive_message_to_revoked_key_from_contact"),
krista@2942
    44
                                                                      static_cast<Func>(&KeyResetMessageTests::check_receive_message_to_revoked_key_from_contact)));                                                                      
krista@3174
    45
    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("KeyResetMessageTests::check_multiple_resets_single_key"),
krista@3174
    46
                                                                      static_cast<Func>(&KeyResetMessageTests::check_multiple_resets_single_key)));                                                                      
krista@3174
    47
                                                                      
krista@2956
    48
    fake_this = this;                                                                  
krista@2917
    49
}
krista@2917
    50
krista@3314
    51
// PEP_STATUS KeyResetMessageTests::message_send_callback(message* msg) {
krista@3314
    52
//     fake_this->device->send_queue.push_back(msg);
krista@3314
    53
//     return PEP_STATUS_OK;    
krista@3314
    54
// }
krista@2917
    55
krista@2917
    56
void KeyResetMessageTests::setup() {
krista@2917
    57
    EngineTestIndividualSuite::setup();
krista@3314
    58
//    device->send_queue.clear();
krista@3314
    59
//    device->message_to_send = &KeyResetMessageTests::message_send_callback;
krista@3314
    60
//    session->messageToSend = device->message_to_send;
krista@3296
    61
}
krista@3296
    62
krista@3296
    63
void KeyResetMessageTests::tear_down() {
krista@3314
    64
//    device->device_messageToSend = NULL;
krista@3314
    65
//    session->messageToSend = NULL;
krista@3296
    66
    EngineTestIndividualSuite::tear_down();
krista@2917
    67
}
krista@2917
    68
krista@2917
    69
void KeyResetMessageTests::send_setup() {
krista@2917
    70
    // Setup own identity
krista@2917
    71
    PEP_STATUS status = read_file_and_import_key(session,
krista@2917
    72
                "test_keys/pub/pep-test-alice-0x6FF00E97_pub.asc");
krista@3270
    73
    assert(status == PEP_KEY_IMPORTED);
krista@2917
    74
    status = set_up_ident_from_scratch(session,
krista@2917
    75
                "test_keys/priv/pep-test-alice-0x6FF00E97_priv.asc",
krista@2917
    76
                "pep.test.alice@pep-project.org", alice_fpr, 
krista@2920
    77
                alice_user_id.c_str(), "Alice in Wonderland", NULL, true
krista@2917
    78
            );
krista@2917
    79
    assert(status == PEP_STATUS_OK);
krista@2917
    80
    
krista@2917
    81
    status = set_up_ident_from_scratch(session,
krista@2917
    82
                "test_keys/pub/pep-test-bob-0xC9C2EE39_pub.asc",
krista@2920
    83
                "pep.test.bob@pep-project.org", NULL, bob_user_id.c_str(), "Bob's Burgers",
krista@2917
    84
                NULL, false
krista@2917
    85
            );
krista@2917
    86
    assert(status == PEP_STATUS_OK);
krista@2917
    87
            
krista@2917
    88
    status = set_up_ident_from_scratch(session,
krista@2917
    89
                "test_keys/pub/pep-test-carol-0x42A85A42_pub.asc",
krista@2920
    90
                "pep-test-carol@pep-project.org", NULL, carol_user_id.c_str(), "Carol Burnett",
krista@2917
    91
                NULL, false
krista@2917
    92
            );
krista@2917
    93
    assert(status == PEP_STATUS_OK);
krista@2917
    94
    
krista@2917
    95
    status = set_up_ident_from_scratch(session,
krista@2917
    96
                "test_keys/pub/pep-test-dave-0xBB5BCCF6_pub.asc",
krista@2920
    97
                "pep-test-dave@pep-project.org", NULL, dave_user_id.c_str(), 
krista@2917
    98
                "David Hasselhoff (Germans Love Me)", NULL, false
krista@2917
    99
            );
krista@2917
   100
    assert(status == PEP_STATUS_OK);
krista@2917
   101
krista@2917
   102
    status = set_up_ident_from_scratch(session,
krista@2917
   103
                "test_keys/pub/pep-test-erin-0x9F8D7CBA_pub.asc",
krista@2920
   104
                "pep-test-erin@pep-project.org", NULL, erin_user_id.c_str(), 
krista@2917
   105
                "Éirinn go Brách", NULL, false
krista@2917
   106
            );
krista@2917
   107
    assert(status == PEP_STATUS_OK);
krista@2917
   108
krista@2917
   109
    status = set_up_ident_from_scratch(session,
krista@2917
   110
                "test_keys/pub/pep.test.fenris-0x4F3D2900_pub.asc",
krista@2920
   111
                "pep.test.fenris@thisstilldoesntwork.lu", NULL, fenris_user_id.c_str(), 
krista@2917
   112
                "Fenris Leto Hawke", NULL, false
krista@2917
   113
            );
krista@2917
   114
    assert(status == PEP_STATUS_OK);
krista@2917
   115
}
krista@2917
   116
krista@2917
   117
void KeyResetMessageTests::receive_setup() {
krista@2924
   118
    PEP_STATUS status = read_file_and_import_key(session,
krista@2924
   119
                "test_keys/pub/pep-test-bob-0xC9C2EE39_pub.asc");  
krista@3270
   120
    assert(status == PEP_KEY_IMPORTED);
krista@2924
   121
    status = set_up_ident_from_scratch(session,
krista@2924
   122
                "test_keys/priv/pep-test-bob-0xC9C2EE39_priv.asc",  
krista@2924
   123
                "pep.test.bob@pep-project.org", bob_fpr, 
krista@2924
   124
                bob_user_id.c_str(), "Robert Redford", NULL, true
krista@2924
   125
            );
krista@2924
   126
    assert(status == PEP_STATUS_OK);
krista@2917
   127
    
krista@2924
   128
    status = set_up_ident_from_scratch(session,
krista@2924
   129
                "test_keys/pub/pep-test-alice-0x6FF00E97_pub.asc",
krista@2924
   130
                "pep.test.alice@pep-project.org", NULL, alice_user_id.c_str(), "Alice is tired of Bob",
krista@2924
   131
                NULL, false
krista@2924
   132
            );
krista@2924
   133
    assert(status == PEP_STATUS_OK);    
krista@2892
   134
}
krista@2892
   135
krista@2892
   136
void KeyResetMessageTests::check_key_reset_message() {
krista@2892
   137
    TEST_ASSERT(true);
krista@2892
   138
}
krista@2892
   139
krista@2917
   140
void KeyResetMessageTests::check_reset_key_and_notify() {
krista@2917
   141
    send_setup();
krista@2917
   142
    
krista@2917
   143
    pEp_identity* from_ident = new_identity("pep.test.alice@pep-project.org", NULL, PEP_OWN_USERID, NULL);
krista@2917
   144
    PEP_STATUS status = myself(session, from_ident); 
krista@2917
   145
    TEST_ASSERT_MSG(status == PEP_STATUS_OK, tl_status_string(status));
krista@2917
   146
    TEST_ASSERT_MSG(from_ident->fpr && strcasecmp(from_ident->fpr, alice_fpr) == 0,
krista@2917
   147
                    from_ident->fpr);
krista@2917
   148
    TEST_ASSERT(from_ident->me);
krista@2917
   149
    
krista@2917
   150
    // "send" some messages to update the social graph entries
krista@2917
   151
    identity_list* send_idents = 
krista@2917
   152
        new_identity_list(
krista@2917
   153
            new_identity("pep.test.bob@pep-project.org", 
krista@2940
   154
                         NULL, bob_user_id.c_str(), "Bob's Burgers"));
krista@2917
   155
                         
krista@2917
   156
    identity_list_add(send_idents, new_identity("pep-test-carol@pep-project.org", NULL, NULL, NULL));    
krista@2917
   157
    identity_list_add(send_idents, new_identity("pep-test-dave@pep-project.org", NULL, NULL, NULL)); 
krista@2917
   158
    identity_list_add(send_idents, new_identity("pep-test-erin@pep-project.org", NULL, NULL, NULL)); 
krista@2917
   159
    identity_list_add(send_idents, new_identity("pep.test.fenris@thisstilldoesntwork.lu", NULL, NULL, NULL)); 
krista@2940
   160
krista@2940
   161
    identity_list* curr_ident;
krista@2940
   162
    
krista@2940
   163
    for (curr_ident = send_idents; curr_ident && curr_ident->ident; curr_ident = curr_ident->next) {
krista@2940
   164
        status = update_identity(session, curr_ident->ident);
krista@2940
   165
        if (strcmp(curr_ident->ident->user_id, bob_user_id.c_str()) == 0)
krista@2940
   166
            continue;
krista@2940
   167
        
krista@2956
   168
        status = set_as_pEp_user(session, curr_ident->ident);
krista@2940
   169
        TEST_ASSERT_MSG(status == PEP_STATUS_OK, tl_status_string(status));
krista@2940
   170
    }
krista@2917
   171
    
krista@2917
   172
    cout << "Creating outgoing message to update DB" << endl;
krista@2917
   173
    message* outgoing_msg = new_message(PEP_dir_outgoing);
krista@2917
   174
    TEST_ASSERT(outgoing_msg);
krista@2917
   175
    outgoing_msg->from = from_ident;
krista@2917
   176
    outgoing_msg->to = send_idents;
krista@2917
   177
    outgoing_msg->shortmsg = strdup("Well isn't THIS a useless message...");
krista@2917
   178
    outgoing_msg->longmsg = strdup("Hi Mom...\n");
krista@3314
   179
    outgoing_msg->attachments = new_bloblist(NULL, 0, "application/octet-stream", NULL);
krista@3314
   180
    // that's illegal - VB. 
krista@3314
   181
    // I got this from your tests, and IIRC, there was some reason you guys always put it in there. - KB
krista@2917
   182
    cout << "Message created.\n\n";
krista@2917
   183
    cout << "Encrypting message as MIME multipart…\n";
krista@2917
   184
    message* enc_outgoing_msg = nullptr;
krista@2917
   185
    cout << "Calling encrypt_message()\n";
krista@2917
   186
    status = encrypt_message(session, outgoing_msg, NULL, &enc_outgoing_msg, PEP_enc_PGP_MIME, 0);
krista@2917
   187
    TEST_ASSERT_MSG((status == PEP_STATUS_OK), tl_status_string(status));
krista@2917
   188
    TEST_ASSERT(enc_outgoing_msg);
krista@2917
   189
    cout << "Message encrypted.\n";
krista@2917
   190
        
krista@2917
   191
    // If this all worked, we should have a list of recent guys in our DB which, when we reset Alice's 
krista@2917
   192
    // key, will get sent some nice key reset messages.
krista@2917
   193
    // But... we need to have one look like an older message. So. Time to mess with the DB.
krista@2920
   194
    // Dave is our victim. Because I have a friend called Dave, who is actually a nice dude, but it amuses me.
krista@2933
   195
    // (Note: said friend is NOT David Hasselhoff. To my knowledge. Hi Dave! (Addendum: Dave confirms he is
krista@2933
   196
    // not Hasselhoff. But he wishes he were, sort of.))
krista@2917
   197
    //
krista@2917
   198
    // update identity
krista@2917
   199
    //      set timestamp = 661008730
krista@2917
   200
    //      where address = "pep-test-dave@pep-project.org"
krista@2917
   201
    int int_result = sqlite3_exec(
krista@2917
   202
        session->db,
krista@2917
   203
        "update identity "
krista@2917
   204
        "   set timestamp = 661008730 "
krista@2917
   205
        "   where address = 'pep-test-dave@pep-project.org' ;",
krista@2917
   206
        NULL,
krista@2917
   207
        NULL,
krista@2917
   208
        NULL
krista@2917
   209
    );
krista@2917
   210
    TEST_ASSERT(int_result == SQLITE_OK);
krista@2917
   211
    
krista@2917
   212
    status = key_reset(session, alice_fpr, from_ident);
krista@2917
   213
    TEST_ASSERT_MSG((status == PEP_STATUS_OK), tl_status_string(status));
krista@3314
   214
    TEST_ASSERT(device->send_queue.size() > 0);
krista@2936
   215
    status = myself(session, from_ident);
krista@2936
   216
    string new_fpr = from_ident->fpr;
krista@2936
   217
    TEST_ASSERT_MSG((strcmp(alice_fpr, new_fpr.c_str()) != 0), new_fpr.c_str());
krista@2917
   218
    
krista@2920
   219
    unordered_map<string, bool> hashmap;
krista@2920
   220
    hashmap[alice_user_id] = false;
krista@2920
   221
    hashmap[bob_user_id] = false;
krista@2920
   222
    hashmap[carol_user_id] = false;
krista@2920
   223
    hashmap[dave_user_id] = false;
krista@2920
   224
    hashmap[erin_user_id] = false;
krista@2920
   225
    hashmap[fenris_user_id] = false;
krista@2920
   226
    
krista@2924
   227
    // Number of messages we SHOULD be sending.
krista@3314
   228
    TEST_ASSERT(device->send_queue.size() == 4);
krista@2924
   229
    
krista@3314
   230
    for (vector<message*>::iterator it = device->send_queue.begin(); it != device->send_queue.end(); it++) {
krista@2920
   231
        message* curr_sent_msg = *it;
krista@2920
   232
        TEST_ASSERT(curr_sent_msg);
krista@2920
   233
        TEST_ASSERT(curr_sent_msg->to);
krista@2920
   234
        TEST_ASSERT(curr_sent_msg->to->ident);
krista@2920
   235
        TEST_ASSERT(!(curr_sent_msg->to->next));
krista@2920
   236
        pEp_identity* to = curr_sent_msg->to->ident;
krista@2920
   237
        TEST_ASSERT(to);
krista@2920
   238
        TEST_ASSERT(to->user_id);
krista@2920
   239
        
krista@2920
   240
        unordered_map<string, bool>::iterator jt = hashmap.find(to->user_id);
krista@2920
   241
        
krista@2920
   242
        TEST_ASSERT(jt != hashmap.end());
krista@2924
   243
        hashmap[jt->first] = true;   
krista@2936
   244
krista@2936
   245
        // Uncomment to regenerate received message - remember to update
krista@2940
   246
        // alice_receive_reset_fpr        
krista@2940
   247
        // if (strcmp(curr_sent_msg->to->ident->user_id, bob_user_id.c_str()) == 0) {
krista@2936
   248
        //     char* bob_msg = NULL;
krista@2936
   249
        //     mime_encode_message(curr_sent_msg, false, &bob_msg);
krista@2936
   250
        //     cout << bob_msg;
krista@2936
   251
        // }
krista@2940
   252
        // else if (strcmp(curr_sent_msg->to->ident->user_id, fenris_user_id.c_str()) == 0) {
krista@2940
   253
        //     char* fenris_msg = NULL;
krista@2940
   254
        //     mime_encode_message(curr_sent_msg, false, &fenris_msg);
krista@2940
   255
        //     cout << fenris_msg;
krista@2940
   256
        // }
krista@2920
   257
    }
krista@2920
   258
    
krista@2933
   259
    // MESSAGE LIST NOW INVALID.
krista@3314
   260
    device->send_queue.clear();
krista@2933
   261
    
krista@2933
   262
    // Make sure we have messages only to desired recips
krista@2920
   263
    TEST_ASSERT(hashmap[alice_user_id] == false);
krista@2920
   264
    TEST_ASSERT(hashmap[bob_user_id] == true);
krista@2920
   265
    TEST_ASSERT(hashmap[carol_user_id] == true);
krista@2920
   266
    TEST_ASSERT(hashmap[dave_user_id] == false);
krista@2920
   267
    TEST_ASSERT(hashmap[erin_user_id] == true);
krista@2920
   268
    TEST_ASSERT(hashmap[fenris_user_id] == true);
krista@2917
   269
}
krista@2917
   270
krista@2940
   271
void KeyResetMessageTests::check_non_reset_receive_revoked() {
krista@2924
   272
    receive_setup();
krista@2924
   273
    pEp_identity* alice_ident = new_identity("pep.test.alice@pep-project.org", NULL,
krista@2924
   274
                                            alice_user_id.c_str(), NULL);
krista@2924
   275
                                            
krista@2924
   276
    PEP_STATUS status = update_identity(session, alice_ident);
krista@2924
   277
    TEST_ASSERT(status == PEP_STATUS_OK);
krista@2924
   278
    TEST_ASSERT(strcmp(alice_fpr, alice_ident->fpr) == 0);
krista@2924
   279
    
krista@2924
   280
    string received_mail = slurp("test_files/398_reset_from_alice_to_bob.eml");
krista@2924
   281
    char* decrypted_msg = NULL;
krista@2924
   282
    char* modified_src = NULL;
krista@2924
   283
    stringlist_t* keylist = NULL;
krista@2924
   284
    PEP_rating rating;
krista@2924
   285
    PEP_decrypt_flags_t flags;
krista@2924
   286
    status = MIME_decrypt_message(session, received_mail.c_str(), received_mail.size(),
krista@2924
   287
                                  &decrypted_msg, &keylist, &rating, &flags, &modified_src);
krista@2924
   288
                                  
krista@2936
   289
    TEST_ASSERT_MSG(status == PEP_STATUS_OK, tl_status_string(status));
krista@2933
   290
    TEST_ASSERT(keylist);
krista@2933
   291
    if (keylist) // there's a test option to continue when asserts fail, so...
krista@2933
   292
        TEST_ASSERT_MSG(strcmp(keylist->value, alice_receive_reset_fpr) == 0,
krista@2933
   293
                        keylist->value);
krista@2924
   294
    
krista@2924
   295
    status = update_identity(session, alice_ident);
krista@2933
   296
    TEST_ASSERT(alice_ident->fpr);
krista@2933
   297
    TEST_ASSERT_MSG(strcmp(alice_receive_reset_fpr, alice_ident->fpr) == 0,
krista@2933
   298
                    alice_ident->fpr);
krista@2924
   299
    
krista@2933
   300
    keylist = NULL;
krista@2933
   301
krista@2940
   302
    free(keylist);    
krista@2940
   303
}
krista@2940
   304
krista@2940
   305
void KeyResetMessageTests::check_reset_receive_revoked() {
krista@2940
   306
    PEP_STATUS status = set_up_ident_from_scratch(session,
krista@2940
   307
                "test_keys/pub/pep.test.fenris-0x4F3D2900_pub.asc",
krista@2940
   308
                "pep.test.fenris@thisstilldoesntwork.lu", NULL, fenris_user_id.c_str(), 
krista@2940
   309
                "Fenris Leto Hawke", NULL, false
krista@2940
   310
            );
krista@2940
   311
    assert(status == PEP_STATUS_OK);
krista@2940
   312
    status = set_up_ident_from_scratch(session,
krista@2940
   313
                "test_keys/priv/pep.test.fenris-0x4F3D2900_priv.asc",
krista@2940
   314
                "pep.test.fenris@thisstilldoesntwork.lu", NULL, fenris_user_id.c_str(), 
krista@2940
   315
                "Fenris Leto Hawke", NULL, false
krista@2940
   316
            );
krista@2940
   317
    assert(status == PEP_STATUS_OK);
krista@2940
   318
    
krista@2940
   319
    status = set_up_ident_from_scratch(session,
krista@2940
   320
                "test_keys/pub/pep-test-alice-0x6FF00E97_pub.asc",
krista@2940
   321
                "pep.test.alice@pep-project.org", NULL, alice_user_id.c_str(), "Alice is tired of Bob",
krista@2940
   322
                NULL, false
krista@2940
   323
            );
krista@2940
   324
    assert(status == PEP_STATUS_OK);    
krista@2933
   325
    
krista@2940
   326
    pEp_identity* alice_ident = new_identity("pep.test.alice@pep-project.org", NULL,
krista@2940
   327
                                            alice_user_id.c_str(), NULL);
krista@2940
   328
                                            
krista@2940
   329
    status = update_identity(session, alice_ident);
krista@2940
   330
    TEST_ASSERT(status == PEP_STATUS_OK);
krista@2940
   331
    TEST_ASSERT(strcmp(alice_fpr, alice_ident->fpr) == 0);
krista@2940
   332
    
krista@2940
   333
    string received_mail = slurp("test_files/398_reset_from_alice_to_fenris.eml");
krista@2940
   334
    char* decrypted_msg = NULL;
krista@2940
   335
    char* modified_src = NULL;
krista@2940
   336
    stringlist_t* keylist = NULL;
krista@2940
   337
    PEP_rating rating;
krista@2940
   338
    PEP_decrypt_flags_t flags;
krista@2940
   339
    status = MIME_decrypt_message(session, received_mail.c_str(), received_mail.size(),
krista@2940
   340
                                  &decrypted_msg, &keylist, &rating, &flags, &modified_src);
krista@2940
   341
                                  
krista@2940
   342
    TEST_ASSERT_MSG(status == PEP_STATUS_OK, tl_status_string(status));
krista@2940
   343
    TEST_ASSERT(keylist);
krista@2940
   344
    if (keylist) // there's a test option to continue when asserts fail, so...
krista@2940
   345
        TEST_ASSERT_MSG(strcmp(keylist->value, alice_receive_reset_fpr) == 0,
krista@2940
   346
                        keylist->value);
krista@2940
   347
    
krista@2940
   348
    status = update_identity(session, alice_ident);
krista@2940
   349
    TEST_ASSERT(alice_ident->fpr);
krista@2940
   350
    TEST_ASSERT_MSG(strcmp(alice_receive_reset_fpr, alice_ident->fpr) == 0,
krista@2940
   351
                    alice_ident->fpr);
krista@2940
   352
    
krista@2940
   353
    keylist = NULL;
krista@2940
   354
krista@2940
   355
    free(keylist);    
krista@2917
   356
}
krista@2917
   357
krista@2942
   358
void KeyResetMessageTests::create_msg_for_revoked_key() {
krista@2942
   359
    PEP_STATUS status = set_up_ident_from_scratch(session,
krista@2942
   360
                "test_keys/pub/pep-test-gabrielle-0xE203586C_pub.asc",
krista@2942
   361
                "pep-test-gabrielle@pep-project.org", NULL, PEP_OWN_USERID, 
krista@2942
   362
                "Gabi", NULL, false
krista@2942
   363
            );
krista@2942
   364
    assert(status == PEP_STATUS_OK);
krista@2942
   365
    status = set_up_ident_from_scratch(session,
krista@2942
   366
                "test_keys/priv/pep-test-gabrielle-0xE203586C_priv.asc",
krista@2942
   367
                "pep-test-gabrielle@pep-project.org", NULL, PEP_OWN_USERID, 
krista@2942
   368
                "Gabi", NULL, false
krista@2942
   369
            );
krista@2942
   370
    assert(status == PEP_STATUS_OK);
krista@2942
   371
    
krista@2942
   372
    status = set_up_ident_from_scratch(session,
krista@2942
   373
                "test_keys/pub/pep-test-alice-0x6FF00E97_pub.asc",
krista@2942
   374
                "pep.test.alice@pep-project.org", NULL, "AliceOther", "Alice is tired of Bob",
krista@2942
   375
                NULL, false
krista@2942
   376
            );
krista@2942
   377
    
krista@2942
   378
    pEp_identity* from_ident = new_identity("pep-test-gabrielle@pep-project.org", NULL, PEP_OWN_USERID, NULL);
krista@2942
   379
    status = myself(session, from_ident); 
krista@2942
   380
    TEST_ASSERT_MSG(status == PEP_STATUS_OK, tl_status_string(status));
krista@2942
   381
    TEST_ASSERT_MSG(from_ident->fpr && strcasecmp(from_ident->fpr, "906C9B8349954E82C5623C3C8C541BD4E203586C") == 0,
krista@2942
   382
                    from_ident->fpr);
krista@2942
   383
    TEST_ASSERT(from_ident->me);
krista@2942
   384
    
krista@2942
   385
    // "send" some messages to update the social graph entries
krista@2942
   386
    identity_list* send_idents = 
krista@2942
   387
        new_identity_list(
krista@2942
   388
            new_identity("pep.test.alice@pep-project.org", NULL, "AliceOther", NULL));
krista@2942
   389
    status = update_identity(session, send_idents->ident);
krista@2942
   390
    TEST_ASSERT_MSG(status == PEP_STATUS_OK, tl_status_string(status));    
krista@2956
   391
    status = set_as_pEp_user(session, send_idents->ident);
krista@2942
   392
                             
krista@2942
   393
    message* outgoing_msg = new_message(PEP_dir_outgoing);
krista@2942
   394
    TEST_ASSERT(outgoing_msg);
krista@2942
   395
    outgoing_msg->from = from_ident;
krista@2942
   396
    outgoing_msg->to = send_idents;
krista@2942
   397
    outgoing_msg->shortmsg = strdup("Well isn't THIS a useless message...");
krista@2942
   398
    outgoing_msg->longmsg = strdup("Hi Mom...\n");
krista@2942
   399
    outgoing_msg->attachments = new_bloblist(NULL, 0, "application/octet-stream", NULL);
krista@2942
   400
    cout << "Message created.\n\n";
krista@2942
   401
    cout << "Encrypting message as MIME multipart…\n";
krista@2942
   402
    message* enc_outgoing_msg = nullptr;
krista@2942
   403
    cout << "Calling encrypt_message()\n";
krista@2942
   404
    status = encrypt_message(session, outgoing_msg, NULL, &enc_outgoing_msg, PEP_enc_PGP_MIME, 0);
krista@2942
   405
    TEST_ASSERT_MSG((status == PEP_STATUS_OK), tl_status_string(status));
krista@2942
   406
    TEST_ASSERT(enc_outgoing_msg);
krista@2942
   407
    cout << "Message encrypted.\n";    
krista@2942
   408
    char* outstring = NULL;
krista@2942
   409
    mime_encode_message(enc_outgoing_msg, false, &outstring);
krista@2942
   410
    cout << outstring << endl;
krista@2942
   411
    free_message(enc_outgoing_msg);
krista@2942
   412
    free(outstring);
krista@2917
   413
}
krista@2917
   414
krista@2942
   415
void KeyResetMessageTests::check_receive_message_to_revoked_key_from_unknown() {
krista@2942
   416
    // create_msg_for_revoked_key(); // call to recreate msg
krista@2942
   417
    send_setup();
krista@2942
   418
    pEp_identity* from_ident = new_identity("pep.test.alice@pep-project.org", NULL, PEP_OWN_USERID, NULL);
krista@2942
   419
    PEP_STATUS status = myself(session, from_ident); 
krista@2942
   420
    TEST_ASSERT_MSG(status == PEP_STATUS_OK, tl_status_string(status));
krista@2942
   421
    TEST_ASSERT_MSG(from_ident->fpr && strcasecmp(from_ident->fpr, alice_fpr) == 0,
krista@2942
   422
                    from_ident->fpr);
krista@2942
   423
    TEST_ASSERT(from_ident->me);
krista@2942
   424
krista@2942
   425
    status = key_reset(session, alice_fpr, from_ident);
krista@2942
   426
    TEST_ASSERT_MSG((status == PEP_STATUS_OK), tl_status_string(status));
krista@3314
   427
    device->send_queue.clear();
krista@2942
   428
    
krista@2942
   429
    string received_mail = slurp("test_files/398_gabrielle_to_alice.eml");
krista@2942
   430
    char* decrypted_msg = NULL;
krista@2942
   431
    char* modified_src = NULL;
krista@2942
   432
    stringlist_t* keylist = NULL;
krista@2942
   433
    PEP_rating rating;
krista@2942
   434
    PEP_decrypt_flags_t flags;
krista@2942
   435
    status = MIME_decrypt_message(session, received_mail.c_str(), received_mail.size(),
krista@2942
   436
                                  &decrypted_msg, &keylist, &rating, &flags, &modified_src);
krista@3314
   437
    TEST_ASSERT(device->send_queue.size() == 0);
krista@2942
   438
    free(decrypted_msg);
krista@2942
   439
    free(modified_src);
krista@2942
   440
    free_stringlist(keylist);
krista@2942
   441
    free_identity(from_ident);
krista@2942
   442
}
krista@2942
   443
krista@2942
   444
void KeyResetMessageTests::check_receive_message_to_revoked_key_from_contact() {
krista@2942
   445
    // create_msg_for_revoked_key(); // call to recreate msg
krista@2942
   446
    send_setup();
krista@2942
   447
    pEp_identity* from_ident = new_identity("pep.test.alice@pep-project.org", NULL, PEP_OWN_USERID, NULL);
krista@2942
   448
    PEP_STATUS status = myself(session, from_ident); 
krista@2942
   449
    TEST_ASSERT_MSG(status == PEP_STATUS_OK, tl_status_string(status));
krista@2942
   450
    TEST_ASSERT_MSG(from_ident->fpr && strcasecmp(from_ident->fpr, alice_fpr) == 0,
krista@2942
   451
                    from_ident->fpr);
krista@2942
   452
    TEST_ASSERT(from_ident->me);
krista@2942
   453
krista@2942
   454
    // Send Gabrielle a message
krista@2942
   455
    identity_list* send_idents = new_identity_list(new_identity("pep-test-gabrielle@pep-project.org", NULL, "Gabi", "Gabi"));
krista@2942
   456
    cout << "Creating outgoing message to update DB" << endl;
krista@2942
   457
    message* outgoing_msg = new_message(PEP_dir_outgoing);
krista@2942
   458
    TEST_ASSERT(outgoing_msg);
krista@2942
   459
    outgoing_msg->from = from_ident;
krista@2942
   460
    outgoing_msg->to = send_idents;
krista@2942
   461
    outgoing_msg->shortmsg = strdup("Well isn't THIS a useless message...");
krista@2942
   462
    outgoing_msg->longmsg = strdup("Hi Mom...\n");
krista@2942
   463
    outgoing_msg->attachments = new_bloblist(NULL, 0, "application/octet-stream", NULL);
krista@2942
   464
    cout << "Message created.\n\n";
krista@2942
   465
    cout << "Encrypting message as MIME multipart…\n";
krista@2942
   466
    message* enc_outgoing_msg = nullptr;
krista@2942
   467
    cout << "Calling encrypt_message()\n";
krista@2942
   468
    status = encrypt_message(session, outgoing_msg, NULL, &enc_outgoing_msg, PEP_enc_PGP_MIME, 0);
krista@2942
   469
    TEST_ASSERT_MSG((status == PEP_UNENCRYPTED), tl_status_string(status));
krista@2942
   470
    //
krista@2942
   471
    cout << "Message created." << endl;
krista@2942
   472
    
krista@2942
   473
    // Make the update have occurred earlier, so we don't notify her
krista@2942
   474
    // (We have no key for her yet anyway!)
krista@2942
   475
    int int_result = sqlite3_exec(
krista@2942
   476
        session->db,
krista@2942
   477
        "update identity "
krista@2942
   478
        "   set timestamp = 661008730 "
krista@2942
   479
        "   where address = 'pep-test-gabrielle@pep-project.org' ;",
krista@2942
   480
        NULL,
krista@2942
   481
        NULL,
krista@2942
   482
        NULL
krista@2942
   483
    );
krista@2942
   484
    TEST_ASSERT(int_result == SQLITE_OK);
krista@2942
   485
krista@3173
   486
    // FIXME: longer term we need to fix the test, but the key attached to the message below has expired, so for now, we give her a new key
krista@3173
   487
    slurp_and_import_key(session, "test_keys/pub/pep-test-gabrielle-0xE203586C_pub.asc");
krista@3173
   488
krista@2942
   489
    status = key_reset(session, alice_fpr, from_ident);
krista@2942
   490
    TEST_ASSERT_MSG((status == PEP_STATUS_OK), tl_status_string(status));
krista@3314
   491
    TEST_ASSERT(device->send_queue.size() == 0);
krista@3314
   492
    device->send_queue.clear();
krista@2942
   493
krista@2942
   494
    // Now we get mail from Gabi, who only has our old key AND has become
krista@2942
   495
    // a pEp user in the meantime...
krista@2942
   496
    string received_mail = slurp("test_files/398_gabrielle_to_alice.eml");
krista@2942
   497
    char* decrypted_msg = NULL;
krista@2942
   498
    char* modified_src = NULL;
krista@2942
   499
    stringlist_t* keylist = NULL;
krista@2942
   500
    PEP_rating rating;
krista@2942
   501
    PEP_decrypt_flags_t flags;
krista@2942
   502
    status = MIME_decrypt_message(session, received_mail.c_str(), received_mail.size(),
krista@2942
   503
                                  &decrypted_msg, &keylist, &rating, &flags, &modified_src);
krista@2942
   504
    
krista@3314
   505
    TEST_ASSERT(device->send_queue.size() == 1);
krista@3314
   506
    vector<message*>::iterator it = device->send_queue.begin();
krista@2942
   507
    message* reset_msg = *it;
krista@2942
   508
    TEST_ASSERT(reset_msg);    
krista@2942
   509
    TEST_ASSERT(reset_msg->from);    
krista@2942
   510
    TEST_ASSERT(reset_msg->to);    
krista@2942
   511
    TEST_ASSERT(reset_msg->to->ident);    
krista@2942
   512
    TEST_ASSERT(strcmp(reset_msg->to->ident->address, "pep-test-gabrielle@pep-project.org") == 0);
krista@2942
   513
    TEST_ASSERT(strcmp(reset_msg->to->ident->fpr, "906C9B8349954E82C5623C3C8C541BD4E203586C") == 0);    
krista@2942
   514
    TEST_ASSERT(strcmp(reset_msg->from->fpr, alice_fpr) != 0);
krista@2942
   515
    TEST_ASSERT(keylist);
krista@2942
   516
    TEST_ASSERT(keylist->value);
krista@2942
   517
    TEST_ASSERT(strcmp(keylist->value, alice_fpr) != 0);
krista@2942
   518
    TEST_ASSERT(keylist->next);
krista@2942
   519
    if (strcmp(keylist->next->value, "906C9B8349954E82C5623C3C8C541BD4E203586C") != 0)
krista@2942
   520
        TEST_ASSERT(keylist->next->next && 
krista@2942
   521
                    strcmp(keylist->next->value, 
krista@2942
   522
                           "906C9B8349954E82C5623C3C8C541BD4E203586C") == 0);
krista@3174
   523
krista@2917
   524
}
krista@3174
   525
krista@3174
   526
void KeyResetMessageTests::check_multiple_resets_single_key() {
krista@3174
   527
    send_setup();
krista@3174
   528
    
krista@3174
   529
    pEp_identity* from_ident = new_identity("pep.test.alice@pep-project.org", NULL, PEP_OWN_USERID, NULL);
krista@3174
   530
    PEP_STATUS status = myself(session, from_ident); 
krista@3174
   531
    TEST_ASSERT_MSG(status == PEP_STATUS_OK, tl_status_string(status));
krista@3174
   532
    TEST_ASSERT_MSG(from_ident->fpr && strcasecmp(from_ident->fpr, alice_fpr) == 0,
krista@3174
   533
                    from_ident->fpr);
krista@3174
   534
    TEST_ASSERT(from_ident->me);
krista@3174
   535
krista@3174
   536
    status = key_reset(session, NULL, NULL);
krista@3174
   537
    TEST_ASSERT(status == PEP_STATUS_OK);
krista@3174
   538
    
krista@3174
   539
    status = key_reset(session, NULL, NULL);
krista@3174
   540
    TEST_ASSERT_MSG(status == PEP_STATUS_OK, tl_status_string(status));
krista@3174
   541
    
krista@3174
   542
    status = myself(session, from_ident);
krista@3174
   543
    TEST_ASSERT(status == PEP_STATUS_OK);
krista@3174
   544
    TEST_ASSERT(from_ident->fpr != NULL && from_ident->fpr[0] != 0);
krista@3270
   545
}