test/apple_mail_test.cc
author Edouard Tisserant <edouard@pep-project.org>
Mon, 13 Mar 2017 15:03:14 +0100
changeset 1648 158e65c83311
parent 1619 c29c5805986c
parent 1638 6474eeaee8e0
child 1660 e153b77f3e52
permissions -rw-r--r--
merged
krista@1543
     1
#include <iostream>
krista@1543
     2
#include <iostream>
krista@1543
     3
#include <fstream>
krista@1543
     4
#include <string>
krista@1543
     5
#include <cstring> // for strcmp()
krista@1543
     6
#include <assert.h>
krista@1543
     7
#include "blacklist.h"
krista@1543
     8
#include "keymanagement.h"
krista@1543
     9
#include "message_api.h"
krista@1543
    10
#include "mime.h"
krista@1543
    11
krista@1543
    12
using namespace std;
krista@1543
    13
krista@1543
    14
int main(int argc, char** argv) {
krista@1543
    15
    
krista@1545
    16
krista@1543
    17
    const char* mailfile = "test_mails/apple_mail_TC_signed_encrypted.eml";
krista@1543
    18
    
krista@1543
    19
    PEP_SESSION session;
krista@1543
    20
    
krista@1543
    21
    cout << "calling init()\n";
krista@1543
    22
    PEP_STATUS status1 = init(&session);   
krista@1543
    23
    assert(status1 == PEP_STATUS_OK);
krista@1543
    24
    assert(session);
krista@1543
    25
    cout << "init() completed.\n";
krista@1543
    26
krista@1545
    27
    ifstream infilekey1("test_keys/pub/pep-test-apple-0x1CCBC7D7_pub.asc");
krista@1545
    28
    string keytextkey1;
krista@1545
    29
    while (!infilekey1.eof()) {
krista@1545
    30
        static string line;
krista@1545
    31
        getline(infilekey1, line);
krista@1545
    32
        keytextkey1 += line + "\n";
krista@1545
    33
    }
krista@1545
    34
    infilekey1.close(); 
krista@1545
    35
    
krista@1545
    36
    ifstream infilekey2("test_keys/priv/pep-test-recip-0x08DB0AEE_priv.asc");
krista@1545
    37
    string keytextkey2;
krista@1545
    38
    while (!infilekey2.eof()) {
krista@1545
    39
        static string line;
krista@1545
    40
        getline(infilekey2, line);
krista@1545
    41
        keytextkey2 += line + "\n";
krista@1545
    42
    }
krista@1545
    43
    infilekey2.close(); 
krista@1545
    44
krista@1545
    45
    PEP_STATUS statuskey1 = import_key(session, keytextkey1.c_str(), keytextkey1.length(), NULL);
krista@1545
    46
    PEP_STATUS statuskey2 = import_key(session, keytextkey2.c_str(), keytextkey2.length(), NULL);
krista@1543
    47
        
krista@1543
    48
    ifstream infile(mailfile);
krista@1543
    49
    string mailtext;
krista@1543
    50
    while (!infile.eof()) {
krista@1543
    51
        static string line;
krista@1543
    52
        getline(infile, line);
krista@1543
    53
        mailtext += line + "\n";
krista@1543
    54
    }
krista@1543
    55
    infile.close(); 
krista@1543
    56
krista@1543
    57
    pEp_identity * me = new_identity("pep.test.recip@kgrothoff.org", NULL, PEP_OWN_USERID, "pEp Test Recipient");    
krista@1543
    58
    me->me = true;    
krista@1543
    59
    PEP_STATUS status = update_identity(session, me);
krista@1616
    60
    trust_personal_key(session, me);    
krista@1616
    61
    status = update_identity(session, me);
krista@1638
    62
    
krista@1543
    63
    pEp_identity * you = new_identity("pep.test.apple@pep-project.org", NULL, "TOFU_pep.test.apple@pep-project.org", "pEp Test Recipient");    
krista@1543
    64
    you->me = false;    
krista@1543
    65
    status = update_identity(session, you);
krista@1543
    66
krista@1543
    67
    trust_personal_key(session, you);
krista@1543
    68
    
krista@1543
    69
    status = update_identity(session, you);
krista@1543
    70
    
krista@1543
    71
    message* msg_ptr = nullptr;
krista@1543
    72
    message* dest_msg = nullptr;
krista@1543
    73
    message* final_ptr = nullptr;
krista@1543
    74
    stringlist_t* keylist = nullptr;
krista@1543
    75
    PEP_rating rating;
krista@1543
    76
    PEP_decrypt_flags_t flags;
krista@1543
    77
    
krista@1543
    78
    status = mime_decode_message(mailtext.c_str(), mailtext.length(), &msg_ptr);
krista@1543
    79
    assert(status == PEP_STATUS_OK);
krista@1543
    80
    assert(msg_ptr);
krista@1543
    81
    final_ptr = msg_ptr;
krista@1543
    82
    status = decrypt_message(session, msg_ptr, &dest_msg, &keylist, &rating, &flags);
krista@1543
    83
    final_ptr = dest_msg ? dest_msg : msg_ptr;
krista@1543
    84
  
krista@1543
    85
    cout << "shortmsg: " << final_ptr->shortmsg << endl << endl;
krista@1543
    86
    cout << "longmsg: " << final_ptr->longmsg << endl << endl;
krista@1543
    87
    cout << "longmsg_formatted: " << (final_ptr->longmsg_formatted ? final_ptr->longmsg_formatted : "(empty)") << endl << endl;
krista@1543
    88
krista@1543
    89
    assert(color_from_rating(rating) == PEP_color_green);
krista@1543
    90
krista@1543
    91
    if (final_ptr == dest_msg)
krista@1543
    92
    	free_message(dest_msg);
krista@1543
    93
    free_message(msg_ptr);
krista@1543
    94
    free_stringlist(keylist);
krista@1543
    95
krista@1543
    96
    msg_ptr = nullptr;
krista@1543
    97
    dest_msg = nullptr;
krista@1543
    98
    final_ptr = nullptr;
krista@1543
    99
    keylist = nullptr;
krista@1543
   100
    rating = PEP_rating_unreliable;
krista@1543
   101
    
krista@1543
   102
    const char* mailfile2 = "test_mails/apple_mail_TC_html_signed_encrypted.eml";
krista@1543
   103
    
krista@1543
   104
    ifstream infile2(mailfile2);
krista@1543
   105
    string mailtext2;
krista@1543
   106
    while (!infile2.eof()) {
krista@1543
   107
        static string line;
krista@1543
   108
        getline(infile2, line);
krista@1543
   109
        mailtext2 += line + "\n";
krista@1543
   110
    }
krista@1543
   111
    infile2.close(); 
krista@1543
   112
    
krista@1543
   113
    status = mime_decode_message(mailtext2.c_str(), mailtext2.length(), &msg_ptr);
krista@1543
   114
    assert(status == PEP_STATUS_OK);
krista@1543
   115
    assert(msg_ptr);
krista@1543
   116
    final_ptr = msg_ptr;
krista@1543
   117
    status = decrypt_message(session, msg_ptr, &dest_msg, &keylist, &rating, &flags);
krista@1543
   118
    final_ptr = dest_msg ? dest_msg : msg_ptr;
krista@1543
   119
  
krista@1543
   120
    cout << "shortmsg: " << final_ptr->shortmsg << endl << endl;
krista@1543
   121
    cout << "longmsg: " << final_ptr->longmsg << endl << endl;
krista@1543
   122
    cout << "longmsg_formatted: " << (final_ptr->longmsg_formatted ? final_ptr->longmsg_formatted : "(empty)") << endl << endl;
krista@1543
   123
krista@1543
   124
    assert(color_from_rating(rating) == PEP_color_green);
krista@1543
   125
krista@1543
   126
    if (final_ptr == dest_msg)
krista@1543
   127
    	free_message(dest_msg);
krista@1543
   128
    free_message(msg_ptr);
krista@1543
   129
    free_stringlist(keylist);
krista@1543
   130
krista@1545
   131
    msg_ptr = nullptr;
krista@1545
   132
    dest_msg = nullptr;
krista@1545
   133
    final_ptr = nullptr;
krista@1545
   134
    keylist = nullptr;
krista@1545
   135
    rating = PEP_rating_unreliable;
krista@1545
   136
        
krista@1543
   137
    cout << "calling release()\n";
krista@1543
   138
    release(session);
krista@1543
   139
    return 0;
krista@1543
   140
}