test/blacklist_accept_new_key_test.cc
author Krista Bennett <krista@pep-project.org>
Fri, 01 Jun 2018 10:30:21 +0200
branchENGINE-217
changeset 2732 2d177ec3804b
parent 1660 e153b77f3e52
child 2548 3bfda8f2136e
permissions -rw-r--r--
close branch
     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*** blacklist_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     // 2797 65A2 FEB5 B7C7 31B8  61D9 3E4C EFD9 F7AF 4684 - this is the blacklisted key in blacklisted_pub.asc
    34 
    35     /* read the key into memory */
    36     const string keytext = slurp("blacklisted_pub.asc");
    37     
    38     /* import it into pep */
    39     PEP_STATUS status7 = import_key(session, keytext.c_str(), keytext.length(), NULL);
    40     
    41     const char* bl_fpr_1 = "279765A2FEB5B7C731B861D93E4CEFD9F7AF4684";
    42     bool is_blacklisted = false;
    43     
    44     pEp_identity* blacklisted_identity = new_identity("blacklistedkeys@kgrothoff.org",
    45                                                       bl_fpr_1,
    46                                                       NULL,
    47                                                       "Blacklist Keypair");
    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 read in a message which contains a new key for that ID. */
    54     
    55     const char* new_key = "634FAC4417E9B2A5DC2BD4AAC4AEEBBE7E62701B";
    56     const string mailtext = slurp("test_mails/blacklist_new_key_attached.eml");
    57     pEp_identity * me1 = new_identity("blacklist_test@kgrothoff.org", NULL, PEP_OWN_USERID, "Blacklisted Key Message Recipient");    
    58 
    59     PEP_STATUS status = update_identity(session, me1);
    60     message* msg_ptr = nullptr;
    61     message* dest_msg = nullptr;
    62     stringlist_t* keylist = nullptr;
    63     PEP_rating rating;
    64     PEP_decrypt_flags_t flags;
    65     
    66     status = mime_decode_message(mailtext.c_str(), mailtext.length(), &msg_ptr);
    67     assert(status == PEP_STATUS_OK);
    68     status = decrypt_message(session, msg_ptr, &dest_msg, &keylist, &rating, &flags);
    69 
    70     PEP_STATUS status12 = update_identity(session, blacklisted_identity);
    71 
    72     assert(strcasecmp(blacklisted_identity->fpr, new_key) == 0);
    73 
    74     PEP_STATUS status13 = blacklist_delete(session, bl_fpr_1);
    75     PEP_STATUS status14 = update_identity(session, blacklisted_identity);
    76 
    77     status = delete_keypair(session, new_key);
    78     update_identity(session, blacklisted_identity);
    79     status = delete_keypair(session, bl_fpr_1);
    80     update_identity(session, blacklisted_identity);
    81     
    82     free_message(msg_ptr);
    83     free_message(dest_msg);
    84     free_stringlist(keylist);
    85     
    86     cout << "calling release()\n";
    87     release(session);
    88     return 0;
    89 }