test/pEp_subject_received_test.cc
author Krista Bennett <krista@pep-project.org>
Wed, 14 Jun 2017 18:52:55 +0200
branchENGINE-215
changeset 1856 99348d336639
parent 1852 8b786dad8b60
child 1945 9503c962f746
permissions -rw-r--r--
ENGINE-215: needs to be tested, test cases impacted by the change need to be adjusted
krista@1852
     1
#include <iostream>
krista@1852
     2
#include <iostream>
krista@1852
     3
#include <fstream>
krista@1852
     4
#include <string>
krista@1852
     5
#include <cstring> // for strcmp()
krista@1852
     6
#include <assert.h>
krista@1852
     7
#include "blacklist.h"
krista@1852
     8
#include "keymanagement.h"
krista@1852
     9
#include "message_api.h"
krista@1852
    10
#include "mime.h"
krista@1852
    11
#include "test_util.h" // for slurp()
krista@1852
    12
krista@1852
    13
using namespace std;
krista@1852
    14
krista@1852
    15
int main(int argc, char** argv) {
krista@1852
    16
krista@1852
    17
    cout << "\n*** check that pEp subject is handled properly in received mails ***\n\n";
krista@1852
    18
krista@1852
    19
    PEP_SESSION session;
krista@1852
    20
    
krista@1852
    21
    cout << "calling init()\n";
krista@1852
    22
    PEP_STATUS status1 = init(&session);   
krista@1852
    23
    assert(status1 == PEP_STATUS_OK);
krista@1852
    24
    assert(session);
krista@1852
    25
    cout << "init() completed.\n";
krista@1852
    26
krista@1852
    27
    const char* keytexts[3];
krista@1852
    28
krista@1852
    29
    const string keytextkey1 = slurp("test_keys/pub/pep-test-alice-0x6FF00E97_pub.asc");
krista@1852
    30
    const string keytextkey2 = slurp("test_keys/priv/pep-test-recip-0x08DB0AEE_priv.asc");
krista@1852
    31
    const string keytextkey3 = slurp("test_keys/pub/pep-test-recip-0x08DB0AEE_pub.asc");
krista@1852
    32
    PEP_STATUS statuskey1 = import_key(session, keytextkey1.c_str(), keytextkey1.length(), NULL);
krista@1852
    33
    PEP_STATUS statuskey2 = import_key(session, keytextkey2.c_str(), keytextkey2.length(), NULL);
krista@1852
    34
    PEP_STATUS statuskey3 = import_key(session, keytextkey3.c_str(), keytextkey3.length(), NULL);
krista@1852
    35
krista@1852
    36
    pEp_identity * me = new_identity("pep.test.recip@kgrothoff.org", "93D19F24AD6F4C4BA9134AAF84D9217908DB0AEE", PEP_OWN_USERID, "pEp Test Recipient");    
krista@1852
    37
    me->me = true;    
krista@1852
    38
    PEP_STATUS status = myself(session, me);
krista@1852
    39
    
krista@1852
    40
    pEp_identity * you = new_identity("pep.test.apple@pep-project.org", NULL, "TOFU_pep.test.apple@pep-project.org", "pEp Test Recipient");    
krista@1852
    41
    you->me = false;    
krista@1852
    42
    
krista@1852
    43
    status = update_identity(session, you);
krista@1852
    44
    trust_personal_key(session, you);
krista@1852
    45
    status = update_identity(session, you);
krista@1852
    46
krista@1852
    47
krista@1852
    48
    
krista@1852
    49
    const char* mailfiles[] = {"test_mails/pEp_encrypted_subject_IS_pEp.eml",
krista@1852
    50
                                "test_mails/pEp_subject_normal.eml",
krista@1852
    51
                                "test_mails/pEp_subject_normal_signed.eml",
krista@1852
    52
                                "test_mails/pEp_subject_normal_unencrypted.eml",
krista@1852
    53
                                "test_mails/pEp_subject_pEp.eml",
krista@1852
    54
                                "test_mails/pEp_unencrypted_pEp_subject.eml"};
krista@1852
    55
                                
krista@1852
    56
krista@1852
    57
    cout << "------------------------------------------------------------------------------------------" << endl;
krista@1852
    58
    cout << "Test 1: Normal encrypted mail, pEp as substitute subject, regular subject in crypto text." << endl;
krista@1852
    59
    cout << "------------------------------------------------------------------------------------------" << endl;
krista@1852
    60
        
krista@1852
    61
    string mailtext = slurp(mailfiles[1]);
krista@1852
    62
    
krista@1852
    63
    message* msg_ptr = nullptr;
krista@1852
    64
    message* dest_msg = nullptr;
krista@1852
    65
    message* final_ptr = nullptr;
krista@1852
    66
    stringlist_t* keylist = nullptr;
krista@1852
    67
    PEP_rating rating;
krista@1852
    68
    PEP_decrypt_flags_t flags;
krista@1852
    69
    
krista@1852
    70
    status = mime_decode_message(mailtext.c_str(), mailtext.length(), &msg_ptr);
krista@1852
    71
    assert(status == PEP_STATUS_OK);
krista@1852
    72
    assert(msg_ptr);
krista@1852
    73
    final_ptr = msg_ptr;
krista@1852
    74
    status = decrypt_message(session, msg_ptr, &dest_msg, &keylist, &rating, &flags);
krista@1852
    75
    final_ptr = dest_msg ? dest_msg : msg_ptr;
krista@1852
    76
  
krista@1852
    77
    cout << "shortmsg: " << final_ptr->shortmsg << endl << endl;
krista@1852
    78
    cout << "longmsg: " << final_ptr->longmsg << endl << endl;
krista@1852
    79
    cout << "longmsg_formatted: " << (final_ptr->longmsg_formatted ? final_ptr->longmsg_formatted : "(empty)") << endl << endl;
krista@1852
    80
krista@1852
    81
    assert(strcmp("This is the usual pEp subject that should replace the above.", final_ptr->shortmsg) == 0);
krista@1852
    82
krista@1852
    83
    cout << "Test 1: Subject replaced as expected." << endl << endl;
krista@1852
    84
krista@1852
    85
    if (final_ptr == dest_msg)
krista@1852
    86
    	free_message(dest_msg);
krista@1852
    87
    free_message(msg_ptr);
krista@1852
    88
    free_stringlist(keylist);
krista@1852
    89
krista@1852
    90
    cout << "-------------------------------------------------------------------------------------------------" << endl;
krista@1852
    91
    cout << "Test 2: Normal encrypted/signed mail, pEp as substitute subject, regular subject in crypto text." << endl;
krista@1852
    92
    cout << "-------------------------------------------------------------------------------------------------" << endl;
krista@1852
    93
krista@1852
    94
    msg_ptr = nullptr;
krista@1852
    95
    dest_msg = nullptr;
krista@1852
    96
    final_ptr = nullptr;
krista@1852
    97
    keylist = nullptr;
krista@1852
    98
    rating = PEP_rating_unreliable;
krista@1852
    99
    
krista@1852
   100
    mailtext = slurp(mailfiles[2]);
krista@1852
   101
    
krista@1852
   102
    status = mime_decode_message(mailtext.c_str(), mailtext.length(), &msg_ptr);
krista@1852
   103
    assert(status == PEP_STATUS_OK);
krista@1852
   104
    assert(msg_ptr);
krista@1852
   105
    final_ptr = msg_ptr;
krista@1852
   106
    status = decrypt_message(session, msg_ptr, &dest_msg, &keylist, &rating, &flags);
krista@1852
   107
    final_ptr = dest_msg ? dest_msg : msg_ptr;
krista@1852
   108
  
krista@1852
   109
    cout << "shortmsg: " << final_ptr->shortmsg << endl << endl;
krista@1852
   110
    cout << "longmsg: " << final_ptr->longmsg << endl << endl;
krista@1852
   111
    cout << "longmsg_formatted: " << (final_ptr->longmsg_formatted ? final_ptr->longmsg_formatted : "(empty)") << endl << endl;
krista@1852
   112
krista@1852
   113
    assert(strcmp("Now signed!", final_ptr->shortmsg) == 0);
krista@1852
   114
krista@1852
   115
    cout << "Test 2: Subject replaced as expected." << endl << endl;
krista@1852
   116
krista@1852
   117
    if (final_ptr == dest_msg)
krista@1852
   118
        free_message(dest_msg);
krista@1852
   119
    free_message(msg_ptr);
krista@1852
   120
    free_stringlist(keylist);
krista@1852
   121
    
krista@1852
   122
    cout << "-----------------------------------------------------------------------" << endl;
krista@1852
   123
    cout << "Test 3: Encrypted mail, pEp as actual subject, no subject in body text." << endl;
krista@1852
   124
    cout << "-----------------------------------------------------------------------" << endl;
krista@1852
   125
krista@1852
   126
    msg_ptr = nullptr;
krista@1852
   127
    dest_msg = nullptr;
krista@1852
   128
    final_ptr = nullptr;
krista@1852
   129
    keylist = nullptr;
krista@1852
   130
    rating = PEP_rating_unreliable;
krista@1852
   131
    
krista@1852
   132
    mailtext = slurp(mailfiles[0]);
krista@1852
   133
    
krista@1852
   134
    status = mime_decode_message(mailtext.c_str(), mailtext.length(), &msg_ptr);
krista@1852
   135
    assert(status == PEP_STATUS_OK);
krista@1852
   136
    assert(msg_ptr);
krista@1852
   137
    final_ptr = msg_ptr;
krista@1852
   138
    status = decrypt_message(session, msg_ptr, &dest_msg, &keylist, &rating, &flags);
krista@1852
   139
    final_ptr = dest_msg ? dest_msg : msg_ptr;
krista@1852
   140
  
krista@1852
   141
    cout << "shortmsg: " << final_ptr->shortmsg << endl << endl;
krista@1852
   142
    cout << "longmsg: " << final_ptr->longmsg << endl << endl;
krista@1852
   143
    cout << "longmsg_formatted: " << (final_ptr->longmsg_formatted ? final_ptr->longmsg_formatted : "(empty)") << endl << endl;
krista@1852
   144
krista@1852
   145
    assert(strcmp("pEp", final_ptr->shortmsg) == 0);
krista@1852
   146
krista@1852
   147
    cout << "Test 3: Subject remains intact as desired." << endl << endl;
krista@1852
   148
krista@1852
   149
    if (final_ptr == dest_msg)
krista@1852
   150
        free_message(dest_msg);
krista@1852
   151
    free_message(msg_ptr);
krista@1852
   152
    free_stringlist(keylist);
krista@1852
   153
krista@1852
   154
    cout << "-----------------------------------------------------------------------" << endl;
krista@1852
   155
    cout << "Test 4: Encrypted mail, pEp as actual subject, pEp subject in body text." << endl;
krista@1852
   156
    cout << "-----------------------------------------------------------------------" << endl;
krista@1852
   157
krista@1852
   158
    msg_ptr = nullptr;
krista@1852
   159
    dest_msg = nullptr;
krista@1852
   160
    final_ptr = nullptr;
krista@1852
   161
    keylist = nullptr;
krista@1852
   162
    rating = PEP_rating_unreliable;
krista@1852
   163
    
krista@1852
   164
    mailtext = slurp(mailfiles[4]);
krista@1852
   165
    
krista@1852
   166
    status = mime_decode_message(mailtext.c_str(), mailtext.length(), &msg_ptr);
krista@1852
   167
    assert(status == PEP_STATUS_OK);
krista@1852
   168
    assert(msg_ptr);
krista@1852
   169
    final_ptr = msg_ptr;
krista@1852
   170
    status = decrypt_message(session, msg_ptr, &dest_msg, &keylist, &rating, &flags);
krista@1852
   171
    final_ptr = dest_msg ? dest_msg : msg_ptr;
krista@1852
   172
  
krista@1852
   173
    cout << "shortmsg: " << final_ptr->shortmsg << endl << endl;
krista@1852
   174
    cout << "longmsg: " << final_ptr->longmsg << endl << endl;
krista@1852
   175
    cout << "longmsg_formatted: " << (final_ptr->longmsg_formatted ? final_ptr->longmsg_formatted : "(empty)") << endl << endl;
krista@1852
   176
krista@1852
   177
    assert(strcmp("pEp", final_ptr->shortmsg) == 0);
krista@1852
   178
krista@1852
   179
    cout << "Test 4: Subject correct, in any event." << endl << endl;
krista@1852
   180
krista@1852
   181
    if (final_ptr == dest_msg)
krista@1852
   182
        free_message(dest_msg);
krista@1852
   183
    free_message(msg_ptr);
krista@1852
   184
    free_stringlist(keylist);
krista@1852
   185
krista@1852
   186
    cout << "-------------------------------------------------------------------------" << endl;
krista@1852
   187
    cout << "Test 5: Unencrypted variant where pEp in the subject line is the subject." << endl;
krista@1852
   188
    cout << "-------------------------------------------------------------------------" << endl;
krista@1852
   189
krista@1852
   190
    msg_ptr = nullptr;
krista@1852
   191
    dest_msg = nullptr;
krista@1852
   192
    final_ptr = nullptr;
krista@1852
   193
    keylist = nullptr;
krista@1852
   194
    rating = PEP_rating_unreliable;
krista@1852
   195
    
krista@1852
   196
    mailtext = slurp(mailfiles[5]);
krista@1852
   197
    
krista@1852
   198
    status = mime_decode_message(mailtext.c_str(), mailtext.length(), &msg_ptr);
krista@1852
   199
    assert(status == PEP_STATUS_OK);
krista@1852
   200
    assert(msg_ptr);
krista@1852
   201
    final_ptr = msg_ptr;
krista@1852
   202
    status = decrypt_message(session, msg_ptr, &dest_msg, &keylist, &rating, &flags);
krista@1852
   203
    final_ptr = dest_msg ? dest_msg : msg_ptr;
krista@1852
   204
  
krista@1852
   205
    cout << "shortmsg: " << final_ptr->shortmsg << endl << endl;
krista@1852
   206
    cout << "longmsg: " << final_ptr->longmsg << endl << endl;
krista@1852
   207
    cout << "longmsg_formatted: " << (final_ptr->longmsg_formatted ? final_ptr->longmsg_formatted : "(empty)") << endl << endl;
krista@1852
   208
krista@1852
   209
    assert(strcmp("pEp", final_ptr->shortmsg) == 0);
krista@1852
   210
krista@1852
   211
    cout << "Test 5: Subject remains intact." << endl << endl;
krista@1852
   212
krista@1852
   213
    if (final_ptr == dest_msg)
krista@1852
   214
        free_message(dest_msg);
krista@1852
   215
    free_message(msg_ptr);
krista@1852
   216
    free_stringlist(keylist);
krista@1852
   217
krista@1852
   218
        
krista@1852
   219
    cout << "calling release()\n";
krista@1852
   220
    release(session);
krista@1852
   221
    return 0;
krista@1852
   222
}