test/apple_mail_test.cc
author Krista Grothoff <krista@pep-project.org>
Mon, 27 Feb 2017 17:43:10 +0100
branchratings
changeset 1610 1f07d18d8bcc
parent 1545 42f8c8f29df3
child 1619 c29c5805986c
permissions -rw-r--r--
ENGINE-174: stowing fixes to ratings while attempting to reactivate branch
     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     
    62     pEp_identity * you = new_identity("pep.test.apple@pep-project.org", NULL, "TOFU_pep.test.apple@pep-project.org", "pEp Test Recipient");    
    63     you->me = false;    
    64     status = update_identity(session, you);
    65 
    66     trust_personal_key(session, you);
    67     
    68     message* msg_ptr = nullptr;
    69     message* dest_msg = nullptr;
    70     message* final_ptr = nullptr;
    71     stringlist_t* keylist = nullptr;
    72     PEP_rating rating;
    73     PEP_decrypt_flags_t flags;
    74     
    75     status = mime_decode_message(mailtext.c_str(), mailtext.length(), &msg_ptr);
    76     assert(status == PEP_STATUS_OK);
    77     assert(msg_ptr);
    78     final_ptr = msg_ptr;
    79     status = decrypt_message(session, msg_ptr, &dest_msg, &keylist, &rating, &flags);
    80     final_ptr = dest_msg ? dest_msg : msg_ptr;
    81   
    82     cout << "shortmsg: " << final_ptr->shortmsg << endl << endl;
    83     cout << "longmsg: " << final_ptr->longmsg << endl << endl;
    84     cout << "longmsg_formatted: " << (final_ptr->longmsg_formatted ? final_ptr->longmsg_formatted : "(empty)") << endl << endl;
    85 
    86     assert(color_from_rating(rating) == PEP_color_green);
    87 
    88     if (final_ptr == dest_msg)
    89     	free_message(dest_msg);
    90     free_message(msg_ptr);
    91     free_stringlist(keylist);
    92 
    93     msg_ptr = nullptr;
    94     dest_msg = nullptr;
    95     final_ptr = nullptr;
    96     keylist = nullptr;
    97     rating = PEP_rating_unreliable;
    98     
    99     const char* mailfile2 = "test_mails/apple_mail_TC_html_signed_encrypted.eml";
   100     
   101     ifstream infile2(mailfile2);
   102     string mailtext2;
   103     while (!infile2.eof()) {
   104         static string line;
   105         getline(infile2, line);
   106         mailtext2 += line + "\n";
   107     }
   108     infile2.close(); 
   109     
   110     status = mime_decode_message(mailtext2.c_str(), mailtext2.length(), &msg_ptr);
   111     assert(status == PEP_STATUS_OK);
   112     assert(msg_ptr);
   113     final_ptr = msg_ptr;
   114     status = decrypt_message(session, msg_ptr, &dest_msg, &keylist, &rating, &flags);
   115     final_ptr = dest_msg ? dest_msg : msg_ptr;
   116   
   117     cout << "shortmsg: " << final_ptr->shortmsg << endl << endl;
   118     cout << "longmsg: " << final_ptr->longmsg << endl << endl;
   119     cout << "longmsg_formatted: " << (final_ptr->longmsg_formatted ? final_ptr->longmsg_formatted : "(empty)") << endl << endl;
   120 
   121     assert(color_from_rating(rating) == PEP_color_green);
   122 
   123     if (final_ptr == dest_msg)
   124     	free_message(dest_msg);
   125     free_message(msg_ptr);
   126     free_stringlist(keylist);
   127 
   128     msg_ptr = nullptr;
   129     dest_msg = nullptr;
   130     final_ptr = nullptr;
   131     keylist = nullptr;
   132     rating = PEP_rating_unreliable;
   133         
   134     cout << "calling release()\n";
   135     release(session);
   136     return 0;
   137 }