test/encrypt_missing_private_key_test.cc
author Krista Grothoff <krista@pep-project.org>
Fri, 11 Nov 2016 11:42:23 +0100
changeset 1373 74865a1019ac
permissions -rw-r--r--
stowing changes
krista@1373
     1
#include <iostream>
krista@1373
     2
#include <iostream>
krista@1373
     3
#include <fstream>
krista@1373
     4
#include <string>
krista@1373
     5
#include <cstring> // for strcmp()
krista@1373
     6
#include <assert.h>
krista@1373
     7
#include "blacklist.h"
krista@1373
     8
#include "keymanagement.h"
krista@1373
     9
#include "message_api.h"
krista@1373
    10
#include "mime.h"
krista@1373
    11
krista@1373
    12
using namespace std;
krista@1373
    13
krista@1373
    14
int main() {
krista@1373
    15
    cout << "\n*** blacklist_test ***\n\n";
krista@1373
    16
krista@1373
    17
    PEP_SESSION session;
krista@1373
    18
    
krista@1373
    19
    cout << "calling init()\n";
krista@1373
    20
    PEP_STATUS status1 = init(&session);   
krista@1373
    21
    assert(status1 == PEP_STATUS_OK);
krista@1373
    22
    assert(session);
krista@1373
    23
    cout << "init() completed.\n";
krista@1373
    24
krista@1373
    25
    // blacklist test code
krista@1373
    26
krista@1373
    27
    cout << "blacklist only key for identity / add key / check which key is used" << endl;
krista@1373
    28
    
krista@1373
    29
    // 2797 65A2 FEB5 B7C7 31B8  61D9 3E4C EFD9 F7AF 4684 - this is the blacklisted key in blacklisted_pub.asc
krista@1373
    30
krista@1373
    31
    /* read the key into memory */
krista@1373
    32
    ifstream infile("blacklisted_pub.asc");
krista@1373
    33
    string keytext;
krista@1373
    34
    while (!infile.eof()) {
krista@1373
    35
        static string line;
krista@1373
    36
        getline(infile, line);
krista@1373
    37
        keytext += line + "\n";
krista@1373
    38
    }
krista@1373
    39
    infile.close(); 
krista@1373
    40
    
krista@1373
    41
    /* import it into pep */
krista@1373
    42
    PEP_STATUS status7 = import_key(session, keytext.c_str(), keytext.length(), NULL);
krista@1373
    43
    
krista@1373
    44
    const char* bl_fpr_1 = "279765A2FEB5B7C731B861D93E4CEFD9F7AF4684";
krista@1373
    45
    bool is_blacklisted = false;
krista@1373
    46
    
krista@1373
    47
    pEp_identity* blacklisted_identity = new_identity("blacklistedkeys@kgrothoff.org",
krista@1373
    48
                                                      bl_fpr_1,
krista@1373
    49
                                                      NULL,
krista@1373
    50
                                                      "Blacklist Keypair");
krista@1373
    51
    PEP_STATUS status8 = update_identity(session, blacklisted_identity);
krista@1373
    52
    PEP_STATUS status9 = blacklist_add(session, bl_fpr_1);
krista@1373
    53
    PEP_STATUS status10 = blacklist_is_listed(session, bl_fpr_1, &is_blacklisted);
krista@1373
    54
    PEP_STATUS status11 = update_identity(session, blacklisted_identity);
krista@1373
    55
krista@1373
    56
    /* identity is blacklisted. Now let's try to encrypt a message. */
krista@1373
    57
    
krista@1373
    58
    const char* new_key = NULL;    
krista@1373
    59
    
krista@1373
    60
    ifstream infile2("test_mails/blacklist_no_key.eml");
krista@1373
    61
    string mailtext;
krista@1373
    62
    while (!infile2.eof()) {
krista@1373
    63
        static string line;
krista@1373
    64
        getline(infile2, line);
krista@1373
    65
        mailtext += line + "\n";
krista@1373
    66
    }     infile2.close(); 
krista@1373
    67
krista@1373
    68
    message* enc_msg = NULL;
krista@1373
    69
    
krista@1373
    70
    PEP_STATUS status69 = MIME_encrypt_message(session, mailtext.c_str(), mailtext.length(), NULL, &enc_msg, PEP_enc_PGP_MIME, 0);
krista@1373
    71
//    pEp_identity * me1 = new_identity("blacklist_test@kgrothoff.org", NULL, PEP_OWN_USERID, "Blacklisted Key Message Recipient");    
krista@1373
    72
krista@1373
    73
    new_key = enc_msg->from->fpr;
krista@1373
    74
    cout << "Encrypted with key " << new_key << endl;
krista@1373
    75
//     PEP_STATUS status = update_identity(session, me1);
krista@1373
    76
//     message* msg_ptr = nullptr;
krista@1373
    77
//     message* dest_msg = nullptr;
krista@1373
    78
//     stringlist_t* keylist = nullptr;
krista@1373
    79
//     PEP_rating rating;
krista@1373
    80
//     PEP_decrypt_flags_t flags;
krista@1373
    81
//     
krista@1373
    82
//     status = mime_decode_message(mailtext.c_str(), mailtext.length(), &msg_ptr);
krista@1373
    83
//     assert(status == PEP_STATUS_OK);
krista@1373
    84
//     status = decrypt_message(session, msg_ptr, &dest_msg, &keylist, &rating, &flags);
krista@1373
    85
// 
krista@1373
    86
//     PEP_STATUS status12 = update_identity(session, blacklisted_identity);
krista@1373
    87
// 
krista@1373
    88
//     assert(strcasecmp(blacklisted_identity->fpr, new_key) == 0);
krista@1373
    89
    
krista@1373
    90
    PEP_STATUS status = delete_keypair(session, new_key);
krista@1373
    91
    PEP_STATUS status13 = blacklist_delete(session, bl_fpr_1);
krista@1373
    92
    PEP_STATUS status14 = update_identity(session, blacklisted_identity);
krista@1373
    93
krista@1373
    94
    
krista@1373
    95
    free_message(enc_msg);
krista@1373
    96
    
krista@1373
    97
    cout << "calling release()\n";
krista@1373
    98
    release(session);
krista@1373
    99
    return 0;
krista@1373
   100
}
krista@1373
   101