test/src/engine_tests/ExternalRevokeTests.cc
author Krista 'DarthMama' Bennett <krista@pep.foundation>
Thu, 27 Jun 2019 15:12:42 +0200
branchENGINE-581
changeset 3886 ebd61a1cf6db
parent 3335 09af2cf0ac74
permissions -rw-r--r--
Fixed hole in update_identity spec for extant identities without saved fprs.
krista@1808
     1
// This file is under GNU General Public License 3.0
krista@1808
     2
// see LICENSE.txt
krista@1808
     3
krista@3276
     4
#include "TestConstants.h"
krista@1808
     5
#include <stdlib.h>
krista@2661
     6
#include <string>
krista@2661
     7
#include <cstring>
krista@1808
     8
#include <time.h>
krista@1808
     9
#include "platform.h"
krista@1808
    10
#include <iostream>
krista@1808
    11
#include <fstream>
krista@1808
    12
#include "mime.h"
krista@1808
    13
#include "message_api.h"
krista@3335
    14
#include "test_util.h"
krista@1808
    15
krista@2661
    16
#include "pEpEngine.h"
krista@2661
    17
krista@2661
    18
#include <cpptest.h>
krista@2661
    19
#include "EngineTestSessionSuite.h"
krista@2661
    20
#include "ExternalRevokeTests.h"
krista@2661
    21
krista@1808
    22
using namespace std;
krista@1808
    23
krista@2661
    24
ExternalRevokeTests::ExternalRevokeTests(string suitename, string test_home_dir) :
krista@2661
    25
    EngineTestSessionSuite::EngineTestSessionSuite(suitename, test_home_dir) {
krista@2661
    26
    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("ExternalRevokeTests::check_external_revoke"),
krista@2661
    27
                                                                      static_cast<Func>(&ExternalRevokeTests::check_external_revoke)));
krista@2661
    28
}
krista@1808
    29
krista@2661
    30
void ExternalRevokeTests::check_external_revoke() {
krista@2661
    31
krista@2661
    32
    PEP_STATUS status = PEP_STATUS_OK;   
krista@1808
    33
krista@1808
    34
#ifndef NETPGP
krista@1812
    35
    char* fprs[2];
krista@1817
    36
krista@1841
    37
    const string fenris_pub_key = slurp("test_keys/pub/pep.test.fenris-0x4F3D2900_pub.asc");
krista@1841
    38
    const string fenris_priv_key = slurp("test_keys/priv/pep.test.fenris-0x4F3D2900_priv.asc");
krista@1817
    39
krista@2669
    40
    TEST_ASSERT_MSG((fenris_pub_key.length() != 0), "fenris_pub_key.length() != 0");
krista@2669
    41
    TEST_ASSERT_MSG((fenris_priv_key.length() != 0), "fenris_priv_key.length() != 0");
krista@1841
    42
    
krista@1818
    43
    PEP_STATUS statuspub = import_key(session, fenris_pub_key.c_str(), fenris_pub_key.length(), NULL);
krista@1818
    44
    PEP_STATUS statuspriv = import_key(session, fenris_priv_key.c_str(), fenris_priv_key.length(), NULL);
krista@3276
    45
    TEST_ASSERT_MSG((statuspub == PEP_TEST_KEY_IMPORT_SUCCESS), "statuspub == PEP_STATUS_OK");
krista@3276
    46
    TEST_ASSERT_MSG((statuspriv == PEP_TEST_KEY_IMPORT_SUCCESS), "statuspriv == PEP_STATUS_OK");
krista@1817
    47
krista@1808
    48
    // Create sender ID
krista@1808
    49
    
krista@1841
    50
    pEp_identity * me = new_identity("pep.test.fenris@thisstilldoesntwork.lu", "0969FA229DF21C832A64A04711B1B9804F3D2900", PEP_OWN_USERID, "Fenris Hawke");
krista@1841
    51
    status = myself(session, me);
krista@1808
    52
    
krista@1808
    53
    // Create key
krista@1812
    54
    cout << "Creating new id for : ";
krista@1808
    55
    char *uniqname = strdup("AAAAtestuser@testdomain.org");
krista@1808
    56
    srandom(time(NULL));
krista@1808
    57
    for(int i=0; i < 4;i++)
krista@1808
    58
        uniqname[i] += random() & 0xf;
krista@1808
    59
    
krista@1808
    60
    cout << uniqname << "\n";
krista@1808
    61
    pEp_identity * recip1 = new_identity(uniqname, NULL, NULL, "Test User");
krista@1808
    62
krista@1808
    63
    status = generate_keypair(session, recip1);
krista@1808
    64
    
krista@1812
    65
    cout << "Generated fingerprint ";
krista@1808
    66
    cout << recip1->fpr << "\n";
krista@1808
    67
krista@1812
    68
    fprs[0] = strdup(recip1->fpr);
krista@1812
    69
    
krista@1812
    70
    cout << endl << "*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*" << endl;
krista@1812
    71
    cout << "Trust and revoke single key, ensure trust changes, then generate new key and ensure rating is correct." << endl;
krista@1812
    72
    cout << "*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*!*" << endl << endl;
krista@1812
    73
    
krista@1812
    74
    cout << endl << "---------------------------------------------------------" << endl;
krista@1812
    75
    cout << "1a. Encrypt message for trusted partner." << endl;
krista@1812
    76
    cout << "---------------------------------------------------------" << endl << endl;
krista@1812
    77
krista@1812
    78
    cout << "Trusting personal key for " << uniqname << endl;
krista@2461
    79
    recip1->me = false;
krista@1808
    80
    // Trust it
krista@1808
    81
    status = update_identity(session, recip1);
krista@1808
    82
    status = trust_personal_key(session, recip1);
krista@1808
    83
    status = update_identity(session, recip1);
krista@1812
    84
    
krista@1812
    85
    // TODO: Check trust?
krista@1812
    86
    cout << "Done! Trusted personal key with fpr " << recip1->fpr << " for " << uniqname << endl;
krista@1808
    87
krista@1808
    88
    const char* r1_userid = (recip1->user_id ? strdup(recip1->user_id) : NULL);
krista@1808
    89
krista@1812
    90
    
krista@1808
    91
    // encrypt something to the key
krista@1812
    92
    cout << "Creating message…\n";
krista@1808
    93
    identity_list* to_list = new_identity_list(identity_dup(recip1)); // to bob
krista@1812
    94
    message* outgoing_msg = new_message(PEP_dir_outgoing);
krista@2669
    95
    TEST_ASSERT_MSG((outgoing_msg), "outgoing_msg");
krista@1812
    96
    outgoing_msg->from = identity_dup(me);
krista@1812
    97
    outgoing_msg->to = to_list;
krista@1812
    98
    outgoing_msg->shortmsg = strdup("Greetings, humans!");
krista@1812
    99
    outgoing_msg->longmsg = strdup("This is a test of the emergency message system. This is only a test. BEEP.");
krista@1871
   100
    outgoing_msg->attachments = new_bloblist(NULL, 0, "application/octet-stream", NULL);
krista@1812
   101
    cout << "Message created.\n";
krista@1808
   102
krista@1808
   103
    message* encrypted_outgoing_msg = NULL;
krista@1812
   104
krista@1812
   105
    cout << "Encrypting message to " << uniqname << "…\n";    
krista@1812
   106
    status = encrypt_message(session, outgoing_msg, NULL, &encrypted_outgoing_msg, PEP_enc_PGP_MIME, 0);
krista@1812
   107
    cout << "Encrypted message with status " << tl_status_string(status) << endl;
krista@1808
   108
    // check status
krista@2669
   109
    TEST_ASSERT_MSG((status == PEP_STATUS_OK), "status == PEP_STATUS_OK");
krista@2669
   110
    TEST_ASSERT_MSG((encrypted_outgoing_msg), "encrypted_outgoing_msg");
krista@1808
   111
krista@1812
   112
    cout << "Checking message recipient comm_type from message." << endl;
krista@1808
   113
    // check comm_type
krista@1812
   114
    cout << "comm_type: " << tl_ct_string(encrypted_outgoing_msg->to->ident->comm_type) << endl;
krista@2669
   115
    TEST_ASSERT_MSG((encrypted_outgoing_msg->to->ident->comm_type == PEP_ct_OpenPGP), "encrypted_outgoing_msg->to->ident->comm_type == PEP_ct_OpenPGP");
krista@1811
   116
    
krista@1808
   117
    status = get_trust(session, recip1);
krista@1808
   118
    
krista@2670
   119
    cout << "Recip's trust DB comm_type = "  << tl_ct_string(recip1->comm_type) << endl;
krista@2669
   120
    TEST_ASSERT_MSG((recip1->comm_type == PEP_ct_OpenPGP), "recip1->comm_type == PEP_ct_OpenPGP"); // FIXME: PEP_ct_pEp???
krista@1808
   121
krista@1808
   122
    // decrypt message
krista@1812
   123
    free_message(outgoing_msg);
krista@1812
   124
    outgoing_msg = NULL;
krista@1808
   125
krista@1808
   126
    stringlist_t* keylist = nullptr;
krista@1808
   127
    PEP_rating rating;
krista@1808
   128
    PEP_decrypt_flags_t flags;
krista@1808
   129
krista@1812
   130
    cout << endl << "---------------------------------------------------------" << endl;
krista@1812
   131
    cout << "1b. Decrypt message that was encrypted for trusted partner." << endl;
krista@1812
   132
    cout << "---------------------------------------------------------" << endl << endl;
krista@1812
   133
krista@2615
   134
    flags = 0;
krista@1812
   135
    cout << "Decrypting message." << endl;
krista@1812
   136
    status = decrypt_message(session, encrypted_outgoing_msg, &outgoing_msg, &keylist, &rating, &flags);
krista@1812
   137
    cout << "Decrypted message with status " << tl_status_string(status) << endl;
krista@2669
   138
    TEST_ASSERT_MSG((status == PEP_STATUS_OK), "status == PEP_STATUS_OK");
krista@2669
   139
    TEST_ASSERT_MSG((rating == PEP_rating_trusted), "rating == PEP_rating_trusted");
krista@1808
   140
krista@1808
   141
    // check rating
krista@1812
   142
    cout << "Rating of decrypted message to trusted recip: " << tl_rating_string(rating) << endl;
krista@2669
   143
    TEST_ASSERT_MSG((rating == PEP_rating_trusted), "rating == PEP_rating_trusted"); // FIXME: trusted and anonymised?
krista@1811
   144
    
krista@1808
   145
    // check comm_type
krista@1808
   146
    status = get_trust(session, recip1);
krista@1811
   147
krista@1812
   148
    cout << "Recip's trust DB comm_type = " << tl_ct_string(recip1->comm_type) << endl;
krista@2669
   149
    TEST_ASSERT_MSG((recip1->comm_type == PEP_ct_OpenPGP), "recip1->comm_type == PEP_ct_OpenPGP"); // FIXME: PEP_ct_pEp???
krista@1808
   150
krista@1812
   151
    cout << endl << "---------------------------------------------------------" << endl;
krista@1812
   152
    cout << "2a. Revoke key for (currently) trusted partner." << endl;
krista@1812
   153
    cout << "---------------------------------------------------------" << endl << endl;
krista@1808
   154
    // externally revoke key
krista@1808
   155
    // (note - as of 23.5.17, revoke_key() doesn't touch the trust db, just the keyring, so we can do this)
krista@1808
   156
krista@1812
   157
    cout << "Revoking key." << endl;
krista@2477
   158
    status = update_identity(session, recip1);    
krista@1808
   159
    status = revoke_key(session, recip1->fpr, "encrypt_for_identity_test");
krista@1812
   160
    cout << "Status of revocation call for " << recip1->fpr << " is "<< tl_status_string(status) << endl;
krista@1812
   161
krista@1808
   162
    // free messages
krista@1812
   163
    free_message(outgoing_msg);
krista@1808
   164
    free_message(encrypted_outgoing_msg);
krista@1812
   165
    outgoing_msg = NULL;
krista@1808
   166
    encrypted_outgoing_msg = NULL;
krista@1808
   167
    
krista@1808
   168
    // encrypt something to the key
krista@1808
   169
    cout << "creating message…\n";
krista@1808
   170
    to_list = new_identity_list(identity_dup(recip1)); // to bob
krista@1812
   171
    outgoing_msg = new_message(PEP_dir_outgoing);
krista@2669
   172
    TEST_ASSERT_MSG((outgoing_msg), "outgoing_msg");
krista@1812
   173
    outgoing_msg->from = identity_dup(me);
krista@1812
   174
    outgoing_msg->to = to_list;
krista@1812
   175
    outgoing_msg->shortmsg = strdup("Greetings, humans!");
krista@1812
   176
    outgoing_msg->longmsg = strdup("This is a test of the emergency message system. This is only a test. BEEP.");
krista@1871
   177
    outgoing_msg->attachments = new_bloblist(NULL, 0, "application/octet-stream", NULL);
krista@1808
   178
    cout << "message created.\n";
krista@1808
   179
krista@1808
   180
    encrypted_outgoing_msg = NULL;
krista@1808
   181
    message* decrypted_msg = NULL;
krista@1808
   182
krista@1812
   183
    cout << endl << "---------------------------------------------------------" << endl;
krista@1812
   184
    cout << "2b. Encrypt message for recip whose key has been externally revoked in the keyring, not the app." << endl;
krista@1812
   185
    cout << "---------------------------------------------------------" << endl << endl;
krista@1808
   186
krista@2477
   187
krista@1812
   188
    status = encrypt_message(session, outgoing_msg, NULL, &encrypted_outgoing_msg, PEP_enc_PGP_MIME, 0);
krista@1812
   189
    cout << "Encryption returns with status " << tl_status_string(status) << endl;
krista@2661
   190
    TEST_ASSERT (status == PEP_UNENCRYPTED);
krista@2661
   191
    TEST_ASSERT (encrypted_outgoing_msg == NULL);
krista@2461
   192
    status = update_identity(session, recip1);
krista@2669
   193
    TEST_ASSERT_MSG((recip1->comm_type = PEP_ct_key_not_found), "recip1->comm_type = PEP_ct_key_not_found");
krista@1812
   194
krista@1812
   195
    cout << endl << "---------------------------------------------------------" << endl;
krista@1812
   196
    cout << "2c. Check trust of recip, whose only key has been revoked, once an encryption attempt has been made." << endl;
krista@1812
   197
    cout << "---------------------------------------------------------" << endl << endl;
krista@1811
   198
krista@2669
   199
    TEST_ASSERT_MSG((recip1->fpr == NULL), "recip1->fpr == NULL");
krista@2461
   200
    recip1->fpr = fprs[0];
krista@1808
   201
    status = get_trust(session, recip1);
krista@2461
   202
    recip1->fpr = NULL;
krista@1808
   203
krista@2670
   204
    cout << "Recip's trust DB comm_type = " << tl_ct_string(recip1->comm_type) << endl;
krista@2669
   205
    TEST_ASSERT_MSG((recip1->comm_type == PEP_ct_unknown || recip1->comm_type == PEP_ct_key_revoked), "recip1->comm_type == PEP_ct_unknown || recip1->comm_type == PEP_ct_key_revoked");
krista@1808
   206
krista@1812
   207
    free_message(decrypted_msg);
krista@1812
   208
    free_message(outgoing_msg);
krista@1812
   209
    outgoing_msg = NULL;
krista@1812
   210
    decrypted_msg = NULL;
krista@1812
   211
krista@1812
   212
    cout << endl << "---------------------------------------------------------" << endl;
krista@1812
   213
    cout << "3a. Generate new key, but don't explicitly trust it." << endl;
krista@1812
   214
    cout << "---------------------------------------------------------" << endl << endl;
krista@1812
   215
krista@1812
   216
    // now: generate new key
krista@1812
   217
    free(recip1->fpr);
krista@1812
   218
    recip1->fpr = NULL;
krista@1808
   219
    status = generate_keypair(session, recip1);
krista@1808
   220
    
krista@1812
   221
    cout << "Generated fingerprint \n";
krista@1808
   222
    cout << recip1->fpr << "\n";
krista@1812
   223
    fprs[1] = strdup(recip1->fpr);
krista@1808
   224
krista@1812
   225
    // try again
krista@1812
   226
    cout << endl << "---------------------------------------------------------" << endl;
krista@1812
   227
    cout << "3b. Try to send something to the email address of our revoked friend, make sure a new key is used to encrypt." << endl;
krista@1812
   228
    cout << "---------------------------------------------------------" << endl << endl;
krista@1808
   229
    
krista@1812
   230
    // encrypt something to the key
krista@1812
   231
    cout << "Creating message…\n";
krista@2147
   232
    
krista@2147
   233
    // cout << "First, update identity though!\n";
krista@2147
   234
    // status = update_identity(session, recip1);
krista@1812
   235
    to_list = new_identity_list(identity_dup(recip1)); // to bob
krista@1812
   236
    outgoing_msg = new_message(PEP_dir_outgoing);
krista@2669
   237
    TEST_ASSERT_MSG((outgoing_msg), "outgoing_msg");
krista@1812
   238
    outgoing_msg->from = identity_dup(me);
krista@1812
   239
    outgoing_msg->to = to_list;
krista@1812
   240
    outgoing_msg->shortmsg = strdup("Greetings, humans!");
krista@1812
   241
    outgoing_msg->longmsg = strdup("This is a test of the emergency message system. This is only a test. BEEP.");
krista@1871
   242
    outgoing_msg->attachments = new_bloblist(NULL, 0, "application/octet-stream", NULL);
krista@1812
   243
    cout << "Message created.\n";
krista@1808
   244
krista@1812
   245
    status = encrypt_message(session, outgoing_msg, NULL, &encrypted_outgoing_msg, PEP_enc_PGP_MIME, 0);
krista@2461
   246
    PEP_comm_type ct = (encrypted_outgoing_msg ? encrypted_outgoing_msg->to->ident->comm_type : outgoing_msg->to->ident->comm_type);
krista@2147
   247
    
krista@1812
   248
krista@1812
   249
    // CHECK STATUS???
krista@1812
   250
    cout << "Encryption returns with status " << tl_status_string(status) << endl;
krista@1812
   251
krista@1812
   252
    // check comm_type
krista@1812
   253
    cout << "comm_type: " << tl_ct_string(ct) << endl;
krista@2669
   254
    TEST_ASSERT_MSG((ct == PEP_ct_OpenPGP_unconfirmed), "ct == PEP_ct_OpenPGP_unconfirmed");
krista@1812
   255
    
krista@1812
   256
    status = get_trust(session, recip1);
krista@1812
   257
krista@3886
   258
//    cout << "Recip's trust DB comm_type (should be unknown, as we're using a keyring-only key, not in DB) = "  << tl_ct_string(recip1->comm_type) << endl;
krista@3886
   259
    cout << "Recip's trust DB comm_type (should PEP_ct_OpenPGP_unconfirmed), as we now record this when using update_identity on no-default idents = "  << tl_ct_string(recip1->comm_type) << endl;
krista@3886
   260
    TEST_ASSERT_MSG((recip1->comm_type == PEP_ct_OpenPGP_unconfirmed), tl_ct_string(recip1->comm_type));
krista@1812
   261
krista@1812
   262
    // decrypt message
krista@1812
   263
//    free_message(outgoing_msg);
krista@1812
   264
//    outgoing_msg = NULL;
krista@1812
   265
krista@1812
   266
    cout << endl << "---------------------------------------------------------" << endl;
krista@1812
   267
    cout << "3c. Decrypt... that... message!" << endl;
krista@1812
   268
    cout << "---------------------------------------------------------" << endl << endl;
krista@1812
   269
krista@1812
   270
krista@2615
   271
    flags = 0;
krista@1812
   272
    status = decrypt_message(session, encrypted_outgoing_msg, &decrypted_msg, &keylist, &rating, &flags);
krista@1812
   273
    cout << "Decryption returns with status " << tl_status_string(status) << endl;
krista@2669
   274
    TEST_ASSERT_MSG((status == PEP_STATUS_OK), "status == PEP_STATUS_OK");
krista@2669
   275
    TEST_ASSERT_MSG((decrypted_msg), "decrypted_msg");
krista@2147
   276
    
krista@1808
   277
    // check rating
krista@1812
   278
    cout << "Rating of decrypted message to trusted recip: " << tl_rating_string(rating) << endl;
krista@2669
   279
    TEST_ASSERT_MSG((rating == PEP_rating_reliable), "rating == PEP_rating_reliable");
krista@1808
   280
krista@2147
   281
    status = update_identity(session, decrypted_msg->to->ident);
krista@1812
   282
    ct = (decrypted_msg ? decrypted_msg->to->ident->comm_type : outgoing_msg->to->ident->comm_type);
krista@1808
   283
krista@1812
   284
    cout << "comm_type: " << tl_ct_string(ct) << endl;
krista@2669
   285
    TEST_ASSERT_MSG((ct == PEP_ct_OpenPGP_unconfirmed), "ct == PEP_ct_OpenPGP_unconfirmed");
krista@1808
   286
    
krista@1812
   287
    status = get_trust(session, recip1);
krista@1812
   288
    
krista@3886
   289
//    cout << "Recip's trust DB comm_type (should be unknown - there's nothing in the DB) = "  << tl_ct_string(recip1->comm_type) << endl;
krista@3886
   290
    cout << "Recip's trust DB comm_type (should be PEP_ct_OpenPGP_unconfirmed, as we now store it.) = "  << tl_ct_string(recip1->comm_type) << endl;
krista@3886
   291
    TEST_ASSERT_MSG((recip1->comm_type == PEP_ct_OpenPGP_unconfirmed), tl_ct_string(recip1->comm_type));
krista@1808
   292
krista@1812
   293
    free_message(encrypted_outgoing_msg);
krista@1812
   294
    free_message(decrypted_msg);
krista@1812
   295
    free_message(outgoing_msg);
krista@1812
   296
    outgoing_msg = NULL;
krista@1812
   297
    decrypted_msg = NULL;
krista@1812
   298
    encrypted_outgoing_msg = NULL;
krista@1808
   299
krista@1812
   300
    free_identity(me);
krista@1812
   301
    free_identity(recip1);
krista@1812
   302
    free(uniqname);
krista@3049
   303
        
krista@1812
   304
    free(fprs[0]);
krista@1812
   305
    free(fprs[1]);
krista@1808
   306
    
krista@1808
   307
#else
krista@1808
   308
    cout << "Sorry, test is not defined for NETPGP at this time." << endl;
krista@1808
   309
    
krista@1808
   310
#endif
krista@1808
   311
}