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