test/encrypt_missing_private_key_test.cc
author Krista Bennett <krista@pep-project.org>
Wed, 14 Jun 2017 18:52:55 +0200
branchENGINE-215
changeset 1856 99348d336639
parent 1660 e153b77f3e52
child 1925 bee52dae2cb9
permissions -rw-r--r--
ENGINE-215: needs to be tested, test cases impacted by the change need to be adjusted
     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 #include "test_util.h"
    15 
    16 using namespace std;
    17 
    18 int main() {
    19     cout << "\n*** encrypt_missing_private_key_test ***\n\n";
    20 
    21     PEP_SESSION session;
    22     
    23     cout << "calling init()\n";
    24     PEP_STATUS status1 = init(&session);   
    25     assert(status1 == PEP_STATUS_OK);
    26     assert(session);
    27     cout << "init() completed.\n";
    28 
    29     // blacklist test code
    30 
    31     cout << "blacklist only key for identity / add key / check which key is used" << endl;
    32     
    33     // B252066DE0513BECA2954F30E8E18177B28D9B9D - this is the blacklisted key in blacklisted_self.asc
    34 
    35     const string keytext = slurp("test_keys/priv/blacklist_self.asc");
    36     
    37     /* import it into pep */
    38     PEP_STATUS status7 = import_key(session, keytext.c_str(), keytext.length(), NULL);
    39     
    40     const char* bl_fpr_1 = "B252066DE0513BECA2954F30E8E18177B28D9B9D";
    41     bool is_blacklisted = false;
    42     
    43     pEp_identity* blacklisted_identity = new_identity("blacklistself@kgrothoff.org",
    44                                                       bl_fpr_1,
    45                                                       PEP_OWN_USERID,
    46                                                       "Blacklist Self");
    47     blacklisted_identity->me = true;
    48     PEP_STATUS status8 = update_identity(session, blacklisted_identity);
    49     PEP_STATUS status9 = blacklist_add(session, bl_fpr_1);
    50     PEP_STATUS status10 = blacklist_is_listed(session, bl_fpr_1, &is_blacklisted);
    51     PEP_STATUS status11 = update_identity(session, blacklisted_identity);
    52 
    53     /* identity is blacklisted. Now let's try to encrypt a message. */
    54     
    55     const char* new_key = NULL;
    56     
    57     const string mailtext = slurp("test_mails/blacklist_no_key.eml");
    58     
    59     message* tmp_msg = NULL;
    60     message* enc_msg = NULL;
    61     
    62     PEP_STATUS status = mime_decode_message(mailtext.c_str(), mailtext.length(), &tmp_msg);
    63     assert(status == PEP_STATUS_OK);
    64     
    65     
    66     // This isn't incoming, though... so we need to reverse the direction
    67     tmp_msg->dir = PEP_dir_outgoing;
    68     status = encrypt_message(session,
    69                              tmp_msg,
    70                              NULL,
    71                              &enc_msg,
    72                              PEP_enc_PGP_MIME,
    73                              0);
    74     assert(status == PEP_STATUS_OK);
    75     
    76 //    PEP_STATUS status69 = MIME_encrypt_message(session, mailtext.c_str(), mailtext.length(), NULL, &enc_msg, PEP_enc_PGP_MIME, 0);
    77 //    pEp_identity * me1 = new_identity("blacklist_test@kgrothoff.org", NULL, PEP_OWN_USERID, "Blacklisted Key Message Recipient");    
    78 
    79     new_key = enc_msg->from->fpr;
    80     cout << "Encrypted with key " << new_key << endl;
    81     assert (strcasecmp(new_key, bl_fpr_1) != 0);
    82 //     PEP_STATUS status = update_identity(session, me1);
    83 //     message* msg_ptr = nullptr;
    84 //     message* dest_msg = nullptr;
    85 //     stringlist_t* keylist = nullptr;
    86 //     PEP_rating rating;
    87 //     PEP_decrypt_flags_t flags;
    88 //     
    89 //     status = mime_decode_message(mailtext.c_str(), mailtext.length(), &msg_ptr);
    90 //     assert(status == PEP_STATUS_OK);
    91 //     status = decrypt_message(session, msg_ptr, &dest_msg, &keylist, &rating, &flags);
    92 // 
    93 //     PEP_STATUS status12 = update_identity(session, blacklisted_identity);
    94 // 
    95 //     assert(strcasecmp(blacklisted_identity->fpr, new_key) == 0);
    96     
    97     status = delete_keypair(session, new_key);
    98     PEP_STATUS status13 = blacklist_delete(session, bl_fpr_1);
    99     PEP_STATUS status14 = update_identity(session, blacklisted_identity);
   100 
   101     free_message(tmp_msg);    
   102     free_message(enc_msg);
   103     
   104     cout << "calling release()\n";
   105     release(session);
   106     return 0;
   107 }