test/blacklist_accept_new_key_test.cc
author Krista Bennett <krista@pep-project.org>
Fri, 01 Jun 2018 10:27:30 +0200
branchENGINE-135
changeset 2708 11e836fe7f0a
parent 1522 57c2dbcb70dd
child 1533 ca90b6245a51
child 1660 e153b77f3e52
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 
    15 using namespace std;
    16 
    17 int main() {
    18     cout << "\n*** blacklist_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     // 2797 65A2 FEB5 B7C7 31B8  61D9 3E4C EFD9 F7AF 4684 - this is the blacklisted key in blacklisted_pub.asc
    33 
    34     /* read the key into memory */
    35     ifstream infile("blacklisted_pub.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 = "279765A2FEB5B7C731B861D93E4CEFD9F7AF4684";
    48     bool is_blacklisted = false;
    49     
    50     pEp_identity* blacklisted_identity = new_identity("blacklistedkeys@kgrothoff.org",
    51                                                       bl_fpr_1,
    52                                                       NULL,
    53                                                       "Blacklist Keypair");
    54     PEP_STATUS status8 = update_identity(session, blacklisted_identity);
    55     PEP_STATUS status9 = blacklist_add(session, bl_fpr_1);
    56     PEP_STATUS status10 = blacklist_is_listed(session, bl_fpr_1, &is_blacklisted);
    57     PEP_STATUS status11 = update_identity(session, blacklisted_identity);
    58 
    59     /* identity is blacklisted. Now let's read in a message which contains a new key for that ID. */
    60     
    61     const char* new_key = "634FAC4417E9B2A5DC2BD4AAC4AEEBBE7E62701B";    
    62     
    63     ifstream infile2("test_mails/blacklist_new_key_attached.eml");
    64     string mailtext;
    65     while (!infile2.eof()) {
    66         static string line;
    67         getline(infile2, line);
    68         mailtext += line + "\n";
    69     }
    70     infile2.close(); 
    71 
    72     pEp_identity * me1 = new_identity("blacklist_test@kgrothoff.org", NULL, PEP_OWN_USERID, "Blacklisted Key Message Recipient");    
    73 
    74     PEP_STATUS status = update_identity(session, me1);
    75     message* msg_ptr = nullptr;
    76     message* dest_msg = nullptr;
    77     stringlist_t* keylist = nullptr;
    78     PEP_rating rating;
    79     PEP_decrypt_flags_t flags;
    80     
    81     status = mime_decode_message(mailtext.c_str(), mailtext.length(), &msg_ptr);
    82     assert(status == PEP_STATUS_OK);
    83     status = decrypt_message(session, msg_ptr, &dest_msg, &keylist, &rating, &flags);
    84 
    85     PEP_STATUS status12 = update_identity(session, blacklisted_identity);
    86 
    87     assert(strcasecmp(blacklisted_identity->fpr, new_key) == 0);
    88 
    89     PEP_STATUS status13 = blacklist_delete(session, bl_fpr_1);
    90     PEP_STATUS status14 = update_identity(session, blacklisted_identity);
    91 
    92     status = delete_keypair(session, new_key);
    93     update_identity(session, blacklisted_identity);
    94     status = delete_keypair(session, bl_fpr_1);
    95     update_identity(session, blacklisted_identity);
    96     
    97     free_message(msg_ptr);
    98     free_message(dest_msg);
    99     free_stringlist(keylist);
   100     
   101     cout << "calling release()\n";
   102     release(session);
   103     return 0;
   104 }
   105