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