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