test/encrypt_missing_private_key_test.cc
author Krista Bennett <krista@pep-project.org>
Wed, 14 Jun 2017 18:52:55 +0200
branchENGINE-215
changeset 1856 99348d336639
parent 1660 e153b77f3e52
child 1925 bee52dae2cb9
permissions -rw-r--r--
ENGINE-215: needs to be tested, test cases impacted by the change need to be adjusted
vb@1513
     1
// This file is under GNU General Public License 3.0
vb@1513
     2
// see LICENSE.txt
vb@1513
     3
krista@1377
     4
#include <iostream>
krista@1377
     5
#include <iostream>
krista@1377
     6
#include <fstream>
krista@1377
     7
#include <string>
krista@1377
     8
#include <cstring> // for strcmp()
krista@1377
     9
#include <assert.h>
krista@1377
    10
#include "blacklist.h"
krista@1377
    11
#include "keymanagement.h"
krista@1377
    12
#include "message_api.h"
krista@1377
    13
#include "mime.h"
roker@1660
    14
#include "test_util.h"
krista@1377
    15
krista@1377
    16
using namespace std;
krista@1377
    17
krista@1377
    18
int main() {
krista@1377
    19
    cout << "\n*** encrypt_missing_private_key_test ***\n\n";
krista@1377
    20
krista@1377
    21
    PEP_SESSION session;
krista@1377
    22
    
krista@1377
    23
    cout << "calling init()\n";
krista@1377
    24
    PEP_STATUS status1 = init(&session);   
krista@1377
    25
    assert(status1 == PEP_STATUS_OK);
krista@1377
    26
    assert(session);
krista@1377
    27
    cout << "init() completed.\n";
krista@1377
    28
krista@1377
    29
    // blacklist test code
krista@1377
    30
krista@1377
    31
    cout << "blacklist only key for identity / add key / check which key is used" << endl;
krista@1377
    32
    
krista@1377
    33
    // B252066DE0513BECA2954F30E8E18177B28D9B9D - this is the blacklisted key in blacklisted_self.asc
krista@1377
    34
roker@1660
    35
    const string keytext = slurp("test_keys/priv/blacklist_self.asc");
krista@1377
    36
    
krista@1377
    37
    /* import it into pep */
krista@1377
    38
    PEP_STATUS status7 = import_key(session, keytext.c_str(), keytext.length(), NULL);
krista@1377
    39
    
krista@1377
    40
    const char* bl_fpr_1 = "B252066DE0513BECA2954F30E8E18177B28D9B9D";
krista@1377
    41
    bool is_blacklisted = false;
krista@1377
    42
    
krista@1377
    43
    pEp_identity* blacklisted_identity = new_identity("blacklistself@kgrothoff.org",
krista@1377
    44
                                                      bl_fpr_1,
krista@1377
    45
                                                      PEP_OWN_USERID,
krista@1377
    46
                                                      "Blacklist Self");
krista@1377
    47
    blacklisted_identity->me = true;
krista@1377
    48
    PEP_STATUS status8 = update_identity(session, blacklisted_identity);
krista@1377
    49
    PEP_STATUS status9 = blacklist_add(session, bl_fpr_1);
krista@1377
    50
    PEP_STATUS status10 = blacklist_is_listed(session, bl_fpr_1, &is_blacklisted);
krista@1377
    51
    PEP_STATUS status11 = update_identity(session, blacklisted_identity);
krista@1377
    52
krista@1377
    53
    /* identity is blacklisted. Now let's try to encrypt a message. */
krista@1377
    54
    
roker@1660
    55
    const char* new_key = NULL;
krista@1377
    56
    
roker@1660
    57
    const string mailtext = slurp("test_mails/blacklist_no_key.eml");
krista@1377
    58
    
krista@1377
    59
    message* tmp_msg = NULL;
krista@1377
    60
    message* enc_msg = NULL;
krista@1377
    61
    
krista@1377
    62
    PEP_STATUS status = mime_decode_message(mailtext.c_str(), mailtext.length(), &tmp_msg);
krista@1377
    63
    assert(status == PEP_STATUS_OK);
krista@1377
    64
    
krista@1377
    65
    
krista@1377
    66
    // This isn't incoming, though... so we need to reverse the direction
krista@1377
    67
    tmp_msg->dir = PEP_dir_outgoing;
krista@1377
    68
    status = encrypt_message(session,
krista@1377
    69
                             tmp_msg,
krista@1377
    70
                             NULL,
krista@1377
    71
                             &enc_msg,
krista@1377
    72
                             PEP_enc_PGP_MIME,
krista@1377
    73
                             0);
krista@1377
    74
    assert(status == PEP_STATUS_OK);
krista@1377
    75
    
krista@1377
    76
//    PEP_STATUS status69 = MIME_encrypt_message(session, mailtext.c_str(), mailtext.length(), NULL, &enc_msg, PEP_enc_PGP_MIME, 0);
krista@1377
    77
//    pEp_identity * me1 = new_identity("blacklist_test@kgrothoff.org", NULL, PEP_OWN_USERID, "Blacklisted Key Message Recipient");    
krista@1377
    78
krista@1377
    79
    new_key = enc_msg->from->fpr;
krista@1377
    80
    cout << "Encrypted with key " << new_key << endl;
krista@1377
    81
    assert (strcasecmp(new_key, bl_fpr_1) != 0);
krista@1377
    82
//     PEP_STATUS status = update_identity(session, me1);
krista@1377
    83
//     message* msg_ptr = nullptr;
krista@1377
    84
//     message* dest_msg = nullptr;
krista@1377
    85
//     stringlist_t* keylist = nullptr;
krista@1377
    86
//     PEP_rating rating;
krista@1377
    87
//     PEP_decrypt_flags_t flags;
krista@1377
    88
//     
krista@1377
    89
//     status = mime_decode_message(mailtext.c_str(), mailtext.length(), &msg_ptr);
krista@1377
    90
//     assert(status == PEP_STATUS_OK);
krista@1377
    91
//     status = decrypt_message(session, msg_ptr, &dest_msg, &keylist, &rating, &flags);
krista@1377
    92
// 
krista@1377
    93
//     PEP_STATUS status12 = update_identity(session, blacklisted_identity);
krista@1377
    94
// 
krista@1377
    95
//     assert(strcasecmp(blacklisted_identity->fpr, new_key) == 0);
krista@1377
    96
    
krista@1377
    97
    status = delete_keypair(session, new_key);
krista@1377
    98
    PEP_STATUS status13 = blacklist_delete(session, bl_fpr_1);
krista@1377
    99
    PEP_STATUS status14 = update_identity(session, blacklisted_identity);
krista@1377
   100
krista@1377
   101
    free_message(tmp_msg);    
krista@1377
   102
    free_message(enc_msg);
krista@1377
   103
    
krista@1377
   104
    cout << "calling release()\n";
krista@1377
   105
    release(session);
krista@1377
   106
    return 0;
krista@1377
   107
}