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