test/least_color_group_test.cc
author Krista Grothoff <krista@pep-project.org>
Fri, 03 Mar 2017 14:24:44 +0100
branchENGINE-174
changeset 1618 0fefbfbae9e1
child 1654 60778ef1e780
permissions -rw-r--r--
ENGINE-174: added tests/keys based on Patrick Brunschwig\'s bug description.
     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     cout << "\n*** least_color_group_test.cc ***\n\n";
    16     
    17     const char* mailfile = "test_mails/color_test.eml";
    18     
    19     const char* keynames[] = {"test_keys/priv/pep.color.test.P-0x3EBE215C_priv.asc",
    20                               "test_keys/pub/pep.color.test.H-0xD17E598E_pub.asc",
    21                               "test_keys/pub/pep.color.test.L-0xE9CDB4CE_pub.asc",
    22                               "test_keys/pub/pep.color.test.P-0x3EBE215C_pub.asc",
    23                               "test_keys/pub/pep.color.test.V-0x71FC6D28_pub.asc"
    24                           };
    25     
    26     const int num_keyfiles = 5;
    27         
    28     PEP_SESSION session;
    29     
    30     cout << "calling init()\n";
    31     PEP_STATUS status1 = init(&session);   
    32     assert(status1 == PEP_STATUS_OK);
    33     assert(session);
    34     cout << "init() completed.\n";
    35     
    36     int i = 0;
    37     
    38     for ( ; i < num_keyfiles; i++) {
    39         ifstream infilekey(keynames[i]);
    40         string keytextkey;
    41         while (!infilekey.eof()) {
    42             static string line;
    43             getline(infilekey, line);
    44             keytextkey += line + "\n";
    45         }
    46         infilekey.close(); 
    47         PEP_STATUS statuskey = import_key(session, keytextkey.c_str(), keytextkey.length(), NULL);
    48         assert(statuskey == PEP_STATUS_OK);
    49     }        
    50         
    51     ifstream infile(mailfile);
    52     string mailtext;
    53     while (!infile.eof()) {
    54         static string line;
    55         getline(infile, line);
    56         mailtext += line + "\n";
    57     }
    58     infile.close(); 
    59 
    60     pEp_identity * me1 = new_identity("pep.color.test.P@kgrothoff.org", NULL, 
    61                                       PEP_OWN_USERID, "Pep Color Test P (recip)");    
    62     me1->me = true;    
    63     PEP_STATUS status = update_identity(session, me1);
    64     trust_personal_key(session, me1);
    65     status = update_identity(session, me1);        
    66     
    67     pEp_identity * sender1 = new_identity("pep.color.test.V@kgrothoff.org",
    68                                           NULL, "TOFU_pep.color.test.V@kgrothoff.org",
    69                                           "Pep Color Test V (sender)");
    70                                           
    71     sender1->me = false;
    72     status = update_identity(session, sender1);
    73     trust_personal_key(session, sender1);
    74     status = update_identity(session, sender1);    
    75     
    76     message* msg_ptr = nullptr;
    77     message* dest_msg = nullptr;
    78     message* final_ptr = nullptr;
    79     stringlist_t* keylist = nullptr;
    80     PEP_rating rating;
    81     PEP_decrypt_flags_t flags;
    82     
    83     status = mime_decode_message(mailtext.c_str(), mailtext.length(), &msg_ptr);
    84     assert(status == PEP_STATUS_OK);
    85     assert(msg_ptr);
    86     final_ptr = msg_ptr;
    87     status = decrypt_message(session, msg_ptr, &dest_msg, &keylist, &rating, &flags);
    88     final_ptr = dest_msg ? dest_msg : msg_ptr;
    89   
    90     cout << "shortmsg: " << final_ptr->shortmsg << endl << endl;
    91     cout << "longmsg: " << final_ptr->longmsg << endl << endl;
    92     cout << "longmsg_formatted: " << (final_ptr->longmsg_formatted ? final_ptr->longmsg_formatted : "(empty)") << endl << endl;
    93     cout << "rating: " << rating << endl << endl;
    94     cout << "keys used: " << endl;
    95     i = 0;
    96     for (stringlist_t* k = keylist; k; k = k->next) {
    97         if (i == 0)
    98        	    cout << "\t Signer (key 0):\t" << k->value << endl;
    99         else
   100             cout << "\t #" << i << ":\t" << k->value << endl;
   101         i++;
   102     }
   103     
   104 //    free_identity(me1);
   105     if (final_ptr == dest_msg)
   106     	free_message(dest_msg);
   107     free_message(msg_ptr);
   108     free_stringlist(keylist);
   109     
   110     cout << "calling release()\n";
   111     release(session);
   112     return 0;
   113 }