test/encrypt_missing_private_key_test.cc
author Krista Grothoff <krista@pep-project.org>
Fri, 11 Nov 2016 11:42:23 +0100
changeset 1373 74865a1019ac
permissions -rw-r--r--
stowing changes
     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*** blacklist_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     // 2797 65A2 FEB5 B7C7 31B8  61D9 3E4C EFD9 F7AF 4684 - this is the blacklisted key in blacklisted_pub.asc
    30 
    31     /* read the key into memory */
    32     ifstream infile("blacklisted_pub.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 = "279765A2FEB5B7C731B861D93E4CEFD9F7AF4684";
    45     bool is_blacklisted = false;
    46     
    47     pEp_identity* blacklisted_identity = new_identity("blacklistedkeys@kgrothoff.org",
    48                                                       bl_fpr_1,
    49                                                       NULL,
    50                                                       "Blacklist Keypair");
    51     PEP_STATUS status8 = update_identity(session, blacklisted_identity);
    52     PEP_STATUS status9 = blacklist_add(session, bl_fpr_1);
    53     PEP_STATUS status10 = blacklist_is_listed(session, bl_fpr_1, &is_blacklisted);
    54     PEP_STATUS status11 = update_identity(session, blacklisted_identity);
    55 
    56     /* identity is blacklisted. Now let's try to encrypt a message. */
    57     
    58     const char* new_key = NULL;    
    59     
    60     ifstream infile2("test_mails/blacklist_no_key.eml");
    61     string mailtext;
    62     while (!infile2.eof()) {
    63         static string line;
    64         getline(infile2, line);
    65         mailtext += line + "\n";
    66     }     infile2.close(); 
    67 
    68     message* enc_msg = NULL;
    69     
    70     PEP_STATUS status69 = MIME_encrypt_message(session, mailtext.c_str(), mailtext.length(), NULL, &enc_msg, PEP_enc_PGP_MIME, 0);
    71 //    pEp_identity * me1 = new_identity("blacklist_test@kgrothoff.org", NULL, PEP_OWN_USERID, "Blacklisted Key Message Recipient");    
    72 
    73     new_key = enc_msg->from->fpr;
    74     cout << "Encrypted with key " << new_key << endl;
    75 //     PEP_STATUS status = update_identity(session, me1);
    76 //     message* msg_ptr = nullptr;
    77 //     message* dest_msg = nullptr;
    78 //     stringlist_t* keylist = nullptr;
    79 //     PEP_rating rating;
    80 //     PEP_decrypt_flags_t flags;
    81 //     
    82 //     status = mime_decode_message(mailtext.c_str(), mailtext.length(), &msg_ptr);
    83 //     assert(status == PEP_STATUS_OK);
    84 //     status = decrypt_message(session, msg_ptr, &dest_msg, &keylist, &rating, &flags);
    85 // 
    86 //     PEP_STATUS status12 = update_identity(session, blacklisted_identity);
    87 // 
    88 //     assert(strcasecmp(blacklisted_identity->fpr, new_key) == 0);
    89     
    90     PEP_STATUS status = delete_keypair(session, new_key);
    91     PEP_STATUS status13 = blacklist_delete(session, bl_fpr_1);
    92     PEP_STATUS status14 = update_identity(session, blacklisted_identity);
    93 
    94     
    95     free_message(enc_msg);
    96     
    97     cout << "calling release()\n";
    98     release(session);
    99     return 0;
   100 }
   101