test/apple_mail_test.cc
author Krista Bennett <krista@pep-project.org>
Fri, 09 Jun 2017 11:56:11 +0200
branchENGINE-9
changeset 1836 95c3f32ac151
parent 1808 121361c60644
child 1837 7d8f5f3d1561
permissions -rw-r--r--
Fixed horked key issues for external_revoke_test
     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 #include "test_util.h" // for slurp()
    12 
    13 using namespace std;
    14 
    15 int main(int argc, char** argv) {
    16 
    17     const char* mailfile = "test_mails/apple_mail_TC_signed_encrypted.eml";
    18     
    19     PEP_SESSION session;
    20     
    21     cout << "calling init()\n";
    22     PEP_STATUS status1 = init(&session);   
    23     assert(status1 == PEP_STATUS_OK);
    24     assert(session);
    25     cout << "init() completed.\n";
    26 
    27     const string keytextkey1 = slurp("test_keys/pub/pep-test-apple-0x1CCBC7D7_pub.asc");
    28     const string keytextkey2 = slurp("test_keys/priv/pep-test-recip-0x08DB0AEE_priv.asc");
    29     const string keytextkey3 = slurp("test_keys/pub/pep-test-recip-0x08DB0AEE_pub.asc");
    30 
    31     PEP_STATUS statuskey1 = import_key(session, keytextkey1.c_str(), keytextkey1.length(), NULL);
    32     PEP_STATUS statuskey2 = import_key(session, keytextkey2.c_str(), keytextkey2.length(), NULL);
    33     PEP_STATUS statuskey3 = import_key(session, keytextkey3.c_str(), keytextkey3.length(), NULL);
    34         
    35     const string mailtext = slurp(mailfile);
    36     pEp_identity * me = new_identity("pep.test.recip@kgrothoff.org", NULL, PEP_OWN_USERID, "pEp Test Recipient");    
    37     me->me = true;    
    38     PEP_STATUS status = update_identity(session, me);
    39     trust_personal_key(session, me);    
    40     status = update_identity(session, me);
    41     
    42     pEp_identity * you = new_identity("pep.test.apple@pep-project.org", NULL, "TOFU_pep.test.apple@pep-project.org", "pEp Test Recipient");    
    43     you->me = false;    
    44     status = update_identity(session, you);
    45 
    46     trust_personal_key(session, you);
    47     
    48     status = update_identity(session, you);
    49     
    50     message* msg_ptr = nullptr;
    51     message* dest_msg = nullptr;
    52     message* final_ptr = nullptr;
    53     stringlist_t* keylist = nullptr;
    54     PEP_rating rating;
    55     PEP_decrypt_flags_t flags;
    56     
    57     status = mime_decode_message(mailtext.c_str(), mailtext.length(), &msg_ptr);
    58     assert(status == PEP_STATUS_OK);
    59     assert(msg_ptr);
    60     final_ptr = msg_ptr;
    61     status = decrypt_message(session, msg_ptr, &dest_msg, &keylist, &rating, &flags);
    62     final_ptr = dest_msg ? dest_msg : msg_ptr;
    63   
    64     cout << "shortmsg: " << final_ptr->shortmsg << endl << endl;
    65     cout << "longmsg: " << final_ptr->longmsg << endl << endl;
    66     cout << "longmsg_formatted: " << (final_ptr->longmsg_formatted ? final_ptr->longmsg_formatted : "(empty)") << endl << endl;
    67 
    68     assert(color_from_rating(rating) == PEP_color_green);
    69 
    70     if (final_ptr == dest_msg)
    71     	free_message(dest_msg);
    72     free_message(msg_ptr);
    73     free_stringlist(keylist);
    74 
    75     msg_ptr = nullptr;
    76     dest_msg = nullptr;
    77     final_ptr = nullptr;
    78     keylist = nullptr;
    79     rating = PEP_rating_unreliable;
    80     
    81     const char* mailfile2 = "test_mails/apple_mail_TC_html_signed_encrypted.eml";
    82     const string mailtext2 = slurp(mailfile2);
    83     
    84     status = mime_decode_message(mailtext2.c_str(), mailtext2.length(), &msg_ptr);
    85     assert(status == PEP_STATUS_OK);
    86     assert(msg_ptr);
    87     final_ptr = msg_ptr;
    88     status = decrypt_message(session, msg_ptr, &dest_msg, &keylist, &rating, &flags);
    89     final_ptr = dest_msg ? dest_msg : msg_ptr;
    90   
    91     cout << "shortmsg: " << final_ptr->shortmsg << endl << endl;
    92     cout << "longmsg: " << final_ptr->longmsg << endl << endl;
    93     cout << "longmsg_formatted: " << (final_ptr->longmsg_formatted ? final_ptr->longmsg_formatted : "(empty)") << endl << endl;
    94 
    95     assert(color_from_rating(rating) == PEP_color_green);
    96 
    97     if (final_ptr == dest_msg)
    98     	free_message(dest_msg);
    99     free_message(msg_ptr);
   100     free_stringlist(keylist);
   101 
   102     msg_ptr = nullptr;
   103     dest_msg = nullptr;
   104     final_ptr = nullptr;
   105     keylist = nullptr;
   106     rating = PEP_rating_unreliable;
   107         
   108     cout << "calling release()\n";
   109     release(session);
   110     return 0;
   111 }