test/least_common_denom_color_test.cc
author Krista Bennett <krista@pep-project.org>
Thu, 19 Apr 2018 16:03:44 +0200
branchENGINE-423
changeset 2615 5cd3625e0d91
parent 2483 7fb3205f2893
permissions -rw-r--r--
ENGINE-423: normal tests pass. Now to test reencrypt functionality.
krista@1581
     1
#include <iostream>
krista@1581
     2
#include <iostream>
krista@1581
     3
#include <fstream>
krista@1581
     4
#include <string>
krista@1581
     5
#include <cstring> // for strcmp()
krista@1581
     6
#include <assert.h>
krista@1581
     7
#include "blacklist.h"
krista@1581
     8
#include "keymanagement.h"
krista@1581
     9
#include "message_api.h"
krista@1581
    10
#include "mime.h"
roker@1660
    11
#include "test_util.h"
krista@1581
    12
krista@1581
    13
using namespace std;
krista@1581
    14
krista@1581
    15
int main(int argc, char** argv) {
krista@1581
    16
        
krista@1581
    17
    const char* mailfile = "test_mails/Test_Message_JSON-21_Color_Problems.eml";
krista@1581
    18
    
krista@1581
    19
    PEP_SESSION session;
krista@1581
    20
    
krista@1581
    21
    cout << "calling init()\n";
krista@1581
    22
    PEP_STATUS status1 = init(&session);   
krista@1581
    23
    assert(status1 == PEP_STATUS_OK);
krista@1581
    24
    assert(session);
krista@1581
    25
    cout << "init() completed.\n";
krista@1581
    26
    
krista@1581
    27
    // import keys
roker@1660
    28
    const string keytextkey1 = slurp("test_keys/pub/banmeonce-0x07B29090_pub.asc");
roker@1660
    29
    const string keytextkey2 = slurp("test_keys/pub/banmetwice-0x4080C3E7_pub.asc");
roker@1660
    30
    const string keytextkey3 = slurp("test_keys/pub/pep.never.me.test-0x79C11D1D_pub.asc");
roker@1660
    31
    const string keytextkey4 = slurp("test_keys/priv/pep.never.me.test-0x79C11D1D_priv.asc");
krista@1581
    32
krista@1581
    33
    PEP_STATUS statuskey1 = import_key(session, keytextkey1.c_str(), keytextkey1.length(), NULL);
krista@1581
    34
    PEP_STATUS statuskey2 = import_key(session, keytextkey2.c_str(), keytextkey2.length(), NULL);
krista@1581
    35
    PEP_STATUS statuskey3 = import_key(session, keytextkey3.c_str(), keytextkey3.length(), NULL);
krista@1581
    36
    PEP_STATUS statuskey4 = import_key(session, keytextkey4.c_str(), keytextkey4.length(), NULL);
krista@1581
    37
krista@1581
    38
    pEp_identity * sender = new_identity("pep.never.me.test@kgrothoff.org", NULL, "TOFU_pep.never.me.test@kgrothoff.org", "pEp Never Me Test");    
krista@2288
    39
    sender->me = false;    
krista@1581
    40
    PEP_STATUS status = update_identity(session, sender);
krista@1581
    41
        
krista@1581
    42
    // reset the trust on both keys before we start
krista@2483
    43
    pEp_identity * recip1 = new_identity("banmeonce@kgrothoff.org", NULL, "TOFU_banmeonce@kgrothoff.org", "Ban Me Once");    
krista@2288
    44
    recip1->me = false;    
krista@1581
    45
    status = update_identity(session, recip1);
krista@1584
    46
    key_reset_trust(session, recip1);
krista@1581
    47
    
krista@2483
    48
    pEp_identity * recip2 = new_identity("banmetwice@kgrothoff.org", NULL, "TOFU_banmetwice@kgrothoff.org", "Ban Me Twice");    
krista@2288
    49
    recip2->me = false;    
krista@1581
    50
    status = update_identity(session, recip2);
krista@1584
    51
    key_reset_trust(session, recip2);
krista@1581
    52
        
roker@1660
    53
    const string mailtext = slurp(mailfile);
krista@1581
    54
krista@1581
    55
    // trust_personal_key(session, you);
krista@1581
    56
    // 
krista@1581
    57
    // status = update_identity(session, you);
krista@1581
    58
    
krista@1581
    59
    message* msg_ptr = nullptr;
krista@1581
    60
    message* dest_msg = nullptr;
krista@1581
    61
    stringlist_t* keylist = nullptr;
krista@1581
    62
    PEP_rating rating;
krista@1581
    63
    PEP_decrypt_flags_t flags;
krista@1581
    64
    
krista@1581
    65
    status = mime_decode_message(mailtext.c_str(), mailtext.length(), &msg_ptr);
krista@1581
    66
    assert(status == PEP_STATUS_OK);
krista@1581
    67
    assert(msg_ptr);
edouard@1821
    68
krista@2615
    69
    flags = 0;
krista@1581
    70
    status = decrypt_message(session, msg_ptr, &dest_msg, &keylist, &rating, &flags);
edouard@1821
    71
    assert(status == PEP_STATUS_OK);
edouard@1821
    72
    assert(dest_msg);
krista@1581
    73
    /* message is signed and no recip is mistrusted... */
krista@1581
    74
    assert(color_from_rating(rating) == PEP_color_yellow);
krista@1581
    75
edouard@1821
    76
    cout << "shortmsg: " << dest_msg->shortmsg << endl << endl;
edouard@1821
    77
    cout << "longmsg: " << dest_msg->longmsg << endl << endl;
edouard@1821
    78
    cout << "longmsg_formatted: " << (dest_msg->longmsg_formatted ? dest_msg->longmsg_formatted : "(empty)") << endl << endl;
edouard@1821
    79
edouard@1821
    80
    PEP_rating decrypt_rating = rating;
edouard@1821
    81
    
edouard@1821
    82
    /* re-evaluate rating, counting on optional fields */
edouard@1821
    83
    status = re_evaluate_message_rating(session, dest_msg, NULL, PEP_rating_undefined, &rating);
edouard@1821
    84
    assert(status == PEP_STATUS_OK);
edouard@1821
    85
    assert(color_from_rating(rating) == PEP_color_yellow);
edouard@1821
    86
edouard@1821
    87
    /* re-evaluate rating, without optional fields */
edouard@1821
    88
    status = re_evaluate_message_rating(session, dest_msg, keylist, decrypt_rating, &rating);
edouard@1821
    89
    assert(status == PEP_STATUS_OK);
edouard@1821
    90
    assert(color_from_rating(rating) == PEP_color_yellow);
krista@1581
    91
krista@1581
    92
    /* Ok, now mistrust one recip */
krista@1581
    93
    key_mistrusted(session, recip2);
edouard@1821
    94
edouard@1821
    95
    /* re-evaluate rating, counting on optional fields */
edouard@1821
    96
    status = re_evaluate_message_rating(session, dest_msg, NULL, PEP_rating_undefined, &rating);
edouard@1821
    97
    assert(status == PEP_STATUS_OK);
edouard@1821
    98
    assert(color_from_rating(rating) == PEP_color_red);
edouard@1821
    99
edouard@1821
   100
    /* re-evaluate rating, without optional fields */
edouard@1821
   101
    status = re_evaluate_message_rating(session, dest_msg, keylist, decrypt_rating, &rating);
edouard@1821
   102
    assert(status == PEP_STATUS_OK);
edouard@1821
   103
    assert(color_from_rating(rating) == PEP_color_red);
edouard@1821
   104
edouard@1821
   105
    free_message(dest_msg);
edouard@1821
   106
    free_message(msg_ptr);
edouard@1821
   107
    free_stringlist(keylist);
krista@1581
   108
    
krista@1581
   109
    msg_ptr = nullptr;
krista@1581
   110
    dest_msg = nullptr;
krista@1581
   111
    keylist = nullptr;
krista@1581
   112
    rating = PEP_rating_unreliable;
krista@1581
   113
krista@1581
   114
    status = mime_decode_message(mailtext.c_str(), mailtext.length(), &msg_ptr);
krista@1581
   115
    assert(status == PEP_STATUS_OK);
krista@1581
   116
    assert(msg_ptr);
krista@2615
   117
    flags = 0;
krista@1581
   118
    status = decrypt_message(session, msg_ptr, &dest_msg, &keylist, &rating, &flags);
krista@1581
   119
  
edouard@1821
   120
    cout << "shortmsg: " << dest_msg->shortmsg << endl << endl;
edouard@1821
   121
    cout << "longmsg: " << dest_msg->longmsg << endl << endl;
edouard@1821
   122
    cout << "longmsg_formatted: " << (dest_msg->longmsg_formatted ? dest_msg->longmsg_formatted : "(empty)") << endl << endl;
krista@1581
   123
krista@1581
   124
    /* message is signed and no recip is mistrusted... */
krista@1581
   125
    assert(color_from_rating(rating) == PEP_color_red);
krista@1581
   126
edouard@1821
   127
    free_message(dest_msg);
krista@1581
   128
    free_message(msg_ptr);
krista@1581
   129
    free_stringlist(keylist);
krista@1581
   130
krista@1581
   131
    msg_ptr = nullptr;
krista@1581
   132
    dest_msg = nullptr;
krista@1581
   133
    keylist = nullptr;
krista@1581
   134
    rating = PEP_rating_unreliable;
roker@1660
   135
    
krista@1581
   136
    cout << "calling release()\n";
krista@1581
   137
    release(session);
krista@1581
   138
    return 0;
krista@1581
   139
}