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