test/encrypt_missing_private_key_test.cc
branchENGINE-84
changeset 1377 20be7d9a7536
child 1513 e7f7e42385b5
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/encrypt_missing_private_key_test.cc	Fri Nov 11 14:08:20 2016 +0100
     1.3 @@ -0,0 +1,121 @@
     1.4 +#include <iostream>
     1.5 +#include <iostream>
     1.6 +#include <fstream>
     1.7 +#include <string>
     1.8 +#include <cstring> // for strcmp()
     1.9 +#include <assert.h>
    1.10 +#include "blacklist.h"
    1.11 +#include "keymanagement.h"
    1.12 +#include "message_api.h"
    1.13 +#include "mime.h"
    1.14 +
    1.15 +using namespace std;
    1.16 +
    1.17 +int main() {
    1.18 +    cout << "\n*** encrypt_missing_private_key_test ***\n\n";
    1.19 +
    1.20 +    PEP_SESSION session;
    1.21 +    
    1.22 +    cout << "calling init()\n";
    1.23 +    PEP_STATUS status1 = init(&session);   
    1.24 +    assert(status1 == PEP_STATUS_OK);
    1.25 +    assert(session);
    1.26 +    cout << "init() completed.\n";
    1.27 +
    1.28 +    // blacklist test code
    1.29 +
    1.30 +    cout << "blacklist only key for identity / add key / check which key is used" << endl;
    1.31 +    
    1.32 +    // B252066DE0513BECA2954F30E8E18177B28D9B9D - this is the blacklisted key in blacklisted_self.asc
    1.33 +
    1.34 +    /* read the key into memory */
    1.35 +    ifstream infile("test_keys/priv/blacklist_self.asc");
    1.36 +    string keytext;
    1.37 +    while (!infile.eof()) {
    1.38 +        static string line;
    1.39 +        getline(infile, line);
    1.40 +        keytext += line + "\n";
    1.41 +    }
    1.42 +    infile.close(); 
    1.43 +    
    1.44 +    /* import it into pep */
    1.45 +    PEP_STATUS status7 = import_key(session, keytext.c_str(), keytext.length(), NULL);
    1.46 +    
    1.47 +    const char* bl_fpr_1 = "B252066DE0513BECA2954F30E8E18177B28D9B9D";
    1.48 +    bool is_blacklisted = false;
    1.49 +    
    1.50 +    pEp_identity* blacklisted_identity = new_identity("blacklistself@kgrothoff.org",
    1.51 +                                                      bl_fpr_1,
    1.52 +                                                      PEP_OWN_USERID,
    1.53 +                                                      "Blacklist Self");
    1.54 +    blacklisted_identity->me = true;
    1.55 +    PEP_STATUS status8 = update_identity(session, blacklisted_identity);
    1.56 +    PEP_STATUS status9 = blacklist_add(session, bl_fpr_1);
    1.57 +    PEP_STATUS status10 = blacklist_is_listed(session, bl_fpr_1, &is_blacklisted);
    1.58 +    PEP_STATUS status11 = update_identity(session, blacklisted_identity);
    1.59 +
    1.60 +    /* identity is blacklisted. Now let's try to encrypt a message. */
    1.61 +    
    1.62 +    const char* new_key = NULL;    
    1.63 +    
    1.64 +    ifstream infile2("test_mails/blacklist_no_key.eml");
    1.65 +    string mailtext;
    1.66 +    while (!infile2.eof()) {
    1.67 +        static string line;
    1.68 +        getline(infile2, line);
    1.69 +        mailtext += line + "\n";
    1.70 +    }     infile2.close(); 
    1.71 +
    1.72 +    
    1.73 +    message* tmp_msg = NULL;
    1.74 +    message* enc_msg = NULL;
    1.75 +    
    1.76 +    PEP_STATUS status = mime_decode_message(mailtext.c_str(), mailtext.length(), &tmp_msg);
    1.77 +    assert(status == PEP_STATUS_OK);
    1.78 +    
    1.79 +    
    1.80 +    
    1.81 +    // This isn't incoming, though... so we need to reverse the direction
    1.82 +    tmp_msg->dir = PEP_dir_outgoing;
    1.83 +    status = encrypt_message(session,
    1.84 +                             tmp_msg,
    1.85 +                             NULL,
    1.86 +                             &enc_msg,
    1.87 +                             PEP_enc_PGP_MIME,
    1.88 +                             0);
    1.89 +    assert(status == PEP_STATUS_OK);
    1.90 +    
    1.91 +    
    1.92 +//    PEP_STATUS status69 = MIME_encrypt_message(session, mailtext.c_str(), mailtext.length(), NULL, &enc_msg, PEP_enc_PGP_MIME, 0);
    1.93 +//    pEp_identity * me1 = new_identity("blacklist_test@kgrothoff.org", NULL, PEP_OWN_USERID, "Blacklisted Key Message Recipient");    
    1.94 +
    1.95 +    new_key = enc_msg->from->fpr;
    1.96 +    cout << "Encrypted with key " << new_key << endl;
    1.97 +    assert (strcasecmp(new_key, bl_fpr_1) != 0);
    1.98 +//     PEP_STATUS status = update_identity(session, me1);
    1.99 +//     message* msg_ptr = nullptr;
   1.100 +//     message* dest_msg = nullptr;
   1.101 +//     stringlist_t* keylist = nullptr;
   1.102 +//     PEP_rating rating;
   1.103 +//     PEP_decrypt_flags_t flags;
   1.104 +//     
   1.105 +//     status = mime_decode_message(mailtext.c_str(), mailtext.length(), &msg_ptr);
   1.106 +//     assert(status == PEP_STATUS_OK);
   1.107 +//     status = decrypt_message(session, msg_ptr, &dest_msg, &keylist, &rating, &flags);
   1.108 +// 
   1.109 +//     PEP_STATUS status12 = update_identity(session, blacklisted_identity);
   1.110 +// 
   1.111 +//     assert(strcasecmp(blacklisted_identity->fpr, new_key) == 0);
   1.112 +    
   1.113 +    status = delete_keypair(session, new_key);
   1.114 +    PEP_STATUS status13 = blacklist_delete(session, bl_fpr_1);
   1.115 +    PEP_STATUS status14 = update_identity(session, blacklisted_identity);
   1.116 +
   1.117 +    free_message(tmp_msg);    
   1.118 +    free_message(enc_msg);
   1.119 +    
   1.120 +    cout << "calling release()\n";
   1.121 +    release(session);
   1.122 +    return 0;
   1.123 +}
   1.124 +