test/apple_mail_test.cc
author Krista Grothoff <krista@pep-project.org>
Fri, 10 Mar 2017 14:17:43 +0100
changeset 1638 6474eeaee8e0
parent 1616 e515db731314
child 1648 158e65c83311
permissions -rw-r--r--
copied single file from ENGINE-174 to default
     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(int argc, char** argv) {
    15     
    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     ifstream infilekey1("test_keys/pub/pep-test-apple-0x1CCBC7D7_pub.asc");
    28     string keytextkey1;
    29     while (!infilekey1.eof()) {
    30         static string line;
    31         getline(infilekey1, line);
    32         keytextkey1 += line + "\n";
    33     }
    34     infilekey1.close(); 
    35     
    36     ifstream infilekey2("test_keys/priv/pep-test-recip-0x08DB0AEE_priv.asc");
    37     string keytextkey2;
    38     while (!infilekey2.eof()) {
    39         static string line;
    40         getline(infilekey2, line);
    41         keytextkey2 += line + "\n";
    42     }
    43     infilekey2.close(); 
    44 
    45     PEP_STATUS statuskey1 = import_key(session, keytextkey1.c_str(), keytextkey1.length(), NULL);
    46     PEP_STATUS statuskey2 = import_key(session, keytextkey2.c_str(), keytextkey2.length(), NULL);
    47         
    48     ifstream infile(mailfile);
    49     string mailtext;
    50     while (!infile.eof()) {
    51         static string line;
    52         getline(infile, line);
    53         mailtext += line + "\n";
    54     }
    55     infile.close(); 
    56 
    57     pEp_identity * me = new_identity("pep.test.recip@kgrothoff.org", NULL, PEP_OWN_USERID, "pEp Test Recipient");    
    58     me->me = true;    
    59     PEP_STATUS status = update_identity(session, me);
    60     trust_personal_key(session, me);    
    61     status = update_identity(session, me);
    62     
    63     pEp_identity * you = new_identity("pep.test.apple@pep-project.org", NULL, "TOFU_pep.test.apple@pep-project.org", "pEp Test Recipient");    
    64     you->me = false;    
    65     status = update_identity(session, you);
    66 
    67     trust_personal_key(session, you);
    68     
    69     status = update_identity(session, you);
    70     
    71     message* msg_ptr = nullptr;
    72     message* dest_msg = nullptr;
    73     message* final_ptr = nullptr;
    74     stringlist_t* keylist = nullptr;
    75     PEP_rating rating;
    76     PEP_decrypt_flags_t flags;
    77     
    78     status = mime_decode_message(mailtext.c_str(), mailtext.length(), &msg_ptr);
    79     assert(status == PEP_STATUS_OK);
    80     assert(msg_ptr);
    81     final_ptr = msg_ptr;
    82     status = decrypt_message(session, msg_ptr, &dest_msg, &keylist, &rating, &flags);
    83     final_ptr = dest_msg ? dest_msg : msg_ptr;
    84   
    85     cout << "shortmsg: " << final_ptr->shortmsg << endl << endl;
    86     cout << "longmsg: " << final_ptr->longmsg << endl << endl;
    87     cout << "longmsg_formatted: " << (final_ptr->longmsg_formatted ? final_ptr->longmsg_formatted : "(empty)") << endl << endl;
    88 
    89     assert(color_from_rating(rating) == PEP_color_green);
    90 
    91     if (final_ptr == dest_msg)
    92     	free_message(dest_msg);
    93     free_message(msg_ptr);
    94     free_stringlist(keylist);
    95 
    96     msg_ptr = nullptr;
    97     dest_msg = nullptr;
    98     final_ptr = nullptr;
    99     keylist = nullptr;
   100     rating = PEP_rating_unreliable;
   101     
   102     const char* mailfile2 = "test_mails/apple_mail_TC_html_signed_encrypted.eml";
   103     
   104     ifstream infile2(mailfile2);
   105     string mailtext2;
   106     while (!infile2.eof()) {
   107         static string line;
   108         getline(infile2, line);
   109         mailtext2 += line + "\n";
   110     }
   111     infile2.close(); 
   112     
   113     status = mime_decode_message(mailtext2.c_str(), mailtext2.length(), &msg_ptr);
   114     assert(status == PEP_STATUS_OK);
   115     assert(msg_ptr);
   116     final_ptr = msg_ptr;
   117     status = decrypt_message(session, msg_ptr, &dest_msg, &keylist, &rating, &flags);
   118     final_ptr = dest_msg ? dest_msg : msg_ptr;
   119   
   120     cout << "shortmsg: " << final_ptr->shortmsg << endl << endl;
   121     cout << "longmsg: " << final_ptr->longmsg << endl << endl;
   122     cout << "longmsg_formatted: " << (final_ptr->longmsg_formatted ? final_ptr->longmsg_formatted : "(empty)") << endl << endl;
   123 
   124     assert(color_from_rating(rating) == PEP_color_green);
   125 
   126     if (final_ptr == dest_msg)
   127     	free_message(dest_msg);
   128     free_message(msg_ptr);
   129     free_stringlist(keylist);
   130 
   131     msg_ptr = nullptr;
   132     dest_msg = nullptr;
   133     final_ptr = nullptr;
   134     keylist = nullptr;
   135     rating = PEP_rating_unreliable;
   136         
   137     cout << "calling release()\n";
   138     release(session);
   139     return 0;
   140 }