test/apple_mail_test.cc
author Krista Bennett <krista@pep-project.org>
Mon, 29 May 2017 11:08:13 +0200
branchENGINE-209
changeset 1808 121361c60644
parent 1807 795d1b201b6f
child 1836 95c3f32ac151
child 1839 db1a28b13887
permissions -rw-r--r--
ENGINE-209: fixed test - works with only key revoked. Now to add a key and make sure trust is correct, as well as testing with multiple keys.
     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 
    30     PEP_STATUS statuskey1 = import_key(session, keytextkey1.c_str(), keytextkey1.length(), NULL);
    31     PEP_STATUS statuskey2 = import_key(session, keytextkey2.c_str(), keytextkey2.length(), NULL);
    32         
    33     const string mailtext = slurp(mailfile);
    34     pEp_identity * me = new_identity("pep.test.recip@kgrothoff.org", NULL, PEP_OWN_USERID, "pEp Test Recipient");    
    35     me->me = true;    
    36     PEP_STATUS status = update_identity(session, me);
    37     trust_personal_key(session, me);    
    38     status = update_identity(session, me);
    39     
    40     pEp_identity * you = new_identity("pep.test.apple@pep-project.org", NULL, "TOFU_pep.test.apple@pep-project.org", "pEp Test Recipient");    
    41     you->me = false;    
    42     status = update_identity(session, you);
    43 
    44     trust_personal_key(session, you);
    45     
    46     status = update_identity(session, you);
    47     
    48     message* msg_ptr = nullptr;
    49     message* dest_msg = nullptr;
    50     message* final_ptr = nullptr;
    51     stringlist_t* keylist = nullptr;
    52     PEP_rating rating;
    53     PEP_decrypt_flags_t flags;
    54     
    55     status = mime_decode_message(mailtext.c_str(), mailtext.length(), &msg_ptr);
    56     assert(status == PEP_STATUS_OK);
    57     assert(msg_ptr);
    58     final_ptr = msg_ptr;
    59     status = decrypt_message(session, msg_ptr, &dest_msg, &keylist, &rating, &flags);
    60     final_ptr = dest_msg ? dest_msg : msg_ptr;
    61   
    62     cout << "shortmsg: " << final_ptr->shortmsg << endl << endl;
    63     cout << "longmsg: " << final_ptr->longmsg << endl << endl;
    64     cout << "longmsg_formatted: " << (final_ptr->longmsg_formatted ? final_ptr->longmsg_formatted : "(empty)") << endl << endl;
    65 
    66     assert(color_from_rating(rating) == PEP_color_green);
    67 
    68     if (final_ptr == dest_msg)
    69     	free_message(dest_msg);
    70     free_message(msg_ptr);
    71     free_stringlist(keylist);
    72 
    73     msg_ptr = nullptr;
    74     dest_msg = nullptr;
    75     final_ptr = nullptr;
    76     keylist = nullptr;
    77     rating = PEP_rating_unreliable;
    78     
    79     const char* mailfile2 = "test_mails/apple_mail_TC_html_signed_encrypted.eml";
    80     const string mailtext2 = slurp(mailfile2);
    81     
    82     status = mime_decode_message(mailtext2.c_str(), mailtext2.length(), &msg_ptr);
    83     assert(status == PEP_STATUS_OK);
    84     assert(msg_ptr);
    85     final_ptr = msg_ptr;
    86     status = decrypt_message(session, msg_ptr, &dest_msg, &keylist, &rating, &flags);
    87     final_ptr = dest_msg ? dest_msg : msg_ptr;
    88   
    89     cout << "shortmsg: " << final_ptr->shortmsg << endl << endl;
    90     cout << "longmsg: " << final_ptr->longmsg << endl << endl;
    91     cout << "longmsg_formatted: " << (final_ptr->longmsg_formatted ? final_ptr->longmsg_formatted : "(empty)") << endl << endl;
    92 
    93     assert(color_from_rating(rating) == PEP_color_green);
    94 
    95     if (final_ptr == dest_msg)
    96     	free_message(dest_msg);
    97     free_message(msg_ptr);
    98     free_stringlist(keylist);
    99 
   100     msg_ptr = nullptr;
   101     dest_msg = nullptr;
   102     final_ptr = nullptr;
   103     keylist = nullptr;
   104     rating = PEP_rating_unreliable;
   105         
   106     cout << "calling release()\n";
   107     release(session);
   108     return 0;
   109 }