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