test/least_common_denom_color_test.cc
author Krista Bennett <krista@pep-project.org>
Fri, 19 Jan 2018 14:12:02 +0100
changeset 2418 05678e77bc6a
parent 2288 8aee10631677
child 2483 7fb3205f2893
permissions -rw-r--r--
fixed update_identity call that could be called on a myself value in outgoing_message_rating
     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 #include "test_util.h"
    12 
    13 using namespace std;
    14 
    15 int main(int argc, char** argv) {
    16         
    17     const char* mailfile = "test_mails/Test_Message_JSON-21_Color_Problems.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     // import keys
    28     const string keytextkey1 = slurp("test_keys/pub/banmeonce-0x07B29090_pub.asc");
    29     const string keytextkey2 = slurp("test_keys/pub/banmetwice-0x4080C3E7_pub.asc");
    30     const string keytextkey3 = slurp("test_keys/pub/pep.never.me.test-0x79C11D1D_pub.asc");
    31     const string keytextkey4 = slurp("test_keys/priv/pep.never.me.test-0x79C11D1D_priv.asc");
    32 
    33     PEP_STATUS statuskey1 = import_key(session, keytextkey1.c_str(), keytextkey1.length(), NULL);
    34     PEP_STATUS statuskey2 = import_key(session, keytextkey2.c_str(), keytextkey2.length(), NULL);
    35     PEP_STATUS statuskey3 = import_key(session, keytextkey3.c_str(), keytextkey3.length(), NULL);
    36     PEP_STATUS statuskey4 = import_key(session, keytextkey4.c_str(), keytextkey4.length(), NULL);
    37 
    38     pEp_identity * sender = new_identity("pep.never.me.test@kgrothoff.org", NULL, "TOFU_pep.never.me.test@kgrothoff.org", "pEp Never Me Test");    
    39     sender->me = false;    
    40     PEP_STATUS status = update_identity(session, sender);
    41         
    42     // reset the trust on both keys before we start
    43     pEp_identity * recip1 = new_identity("banmeonce@kgrothoff.org", NULL, "TOFU_banemeonce@kgrothoff.org", "Ban Me Once");    
    44     recip1->me = false;    
    45     status = update_identity(session, recip1);
    46     key_reset_trust(session, recip1);
    47     
    48     pEp_identity * recip2 = new_identity("banmetwice@kgrothoff.org", NULL, "TOFU_banemetwice@kgrothoff.org", "Ban Me Twice");    
    49     recip2->me = false;    
    50     status = update_identity(session, recip2);
    51     key_reset_trust(session, recip2);
    52         
    53     const string mailtext = slurp(mailfile);
    54 
    55     // trust_personal_key(session, you);
    56     // 
    57     // status = update_identity(session, you);
    58     
    59     message* msg_ptr = nullptr;
    60     message* dest_msg = nullptr;
    61     stringlist_t* keylist = nullptr;
    62     PEP_rating rating;
    63     PEP_decrypt_flags_t flags;
    64     
    65     status = mime_decode_message(mailtext.c_str(), mailtext.length(), &msg_ptr);
    66     assert(status == PEP_STATUS_OK);
    67     assert(msg_ptr);
    68 
    69     status = decrypt_message(session, msg_ptr, &dest_msg, &keylist, &rating, &flags);
    70     assert(status == PEP_STATUS_OK);
    71     assert(dest_msg);
    72     /* message is signed and no recip is mistrusted... */
    73     assert(color_from_rating(rating) == PEP_color_yellow);
    74 
    75     cout << "shortmsg: " << dest_msg->shortmsg << endl << endl;
    76     cout << "longmsg: " << dest_msg->longmsg << endl << endl;
    77     cout << "longmsg_formatted: " << (dest_msg->longmsg_formatted ? dest_msg->longmsg_formatted : "(empty)") << endl << endl;
    78 
    79     PEP_rating decrypt_rating = rating;
    80     
    81     /* re-evaluate rating, counting on optional fields */
    82     status = re_evaluate_message_rating(session, dest_msg, NULL, PEP_rating_undefined, &rating);
    83     assert(status == PEP_STATUS_OK);
    84     assert(color_from_rating(rating) == PEP_color_yellow);
    85 
    86     /* re-evaluate rating, without optional fields */
    87     status = re_evaluate_message_rating(session, dest_msg, keylist, decrypt_rating, &rating);
    88     assert(status == PEP_STATUS_OK);
    89     assert(color_from_rating(rating) == PEP_color_yellow);
    90 
    91     /* Ok, now mistrust one recip */
    92     key_mistrusted(session, recip2);
    93 
    94     /* re-evaluate rating, counting on optional fields */
    95     status = re_evaluate_message_rating(session, dest_msg, NULL, PEP_rating_undefined, &rating);
    96     assert(status == PEP_STATUS_OK);
    97     assert(color_from_rating(rating) == PEP_color_red);
    98 
    99     /* re-evaluate rating, without optional fields */
   100     status = re_evaluate_message_rating(session, dest_msg, keylist, decrypt_rating, &rating);
   101     assert(status == PEP_STATUS_OK);
   102     assert(color_from_rating(rating) == PEP_color_red);
   103 
   104     free_message(dest_msg);
   105     free_message(msg_ptr);
   106     free_stringlist(keylist);
   107     
   108     msg_ptr = nullptr;
   109     dest_msg = nullptr;
   110     keylist = nullptr;
   111     rating = PEP_rating_unreliable;
   112 
   113     status = mime_decode_message(mailtext.c_str(), mailtext.length(), &msg_ptr);
   114     assert(status == PEP_STATUS_OK);
   115     assert(msg_ptr);
   116     status = decrypt_message(session, msg_ptr, &dest_msg, &keylist, &rating, &flags);
   117   
   118     cout << "shortmsg: " << dest_msg->shortmsg << endl << endl;
   119     cout << "longmsg: " << dest_msg->longmsg << endl << endl;
   120     cout << "longmsg_formatted: " << (dest_msg->longmsg_formatted ? dest_msg->longmsg_formatted : "(empty)") << endl << endl;
   121 
   122     /* message is signed and no recip is mistrusted... */
   123     assert(color_from_rating(rating) == PEP_color_red);
   124 
   125     free_message(dest_msg);
   126     free_message(msg_ptr);
   127     free_stringlist(keylist);
   128 
   129     msg_ptr = nullptr;
   130     dest_msg = nullptr;
   131     keylist = nullptr;
   132     rating = PEP_rating_unreliable;
   133     
   134     cout << "calling release()\n";
   135     release(session);
   136     return 0;
   137 }