test/least_color_group_test.cc
author Roker <roker@pep-project.org>
Mon, 13 Mar 2017 20:31:02 +0100
branchENGINE-189
changeset 1658 047100aa9f43
parent 1657 8b06f5627683
child 1847 050f0e83c3f7
permissions -rw-r--r--
use range-based for() to ensure the array of file names is in-sync
     1 #include <iostream>
     2 #include <iostream>
     3 #include <vector>
     4 #include <cstring> // for strcmp()
     5 #include <assert.h>
     6 #include "blacklist.h"
     7 #include "keymanagement.h"
     8 #include "message_api.h"
     9 #include "mime.h"
    10 #include "test_util.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 std::vector<const char*> keynames = {
    20                               "test_keys/priv/pep.color.test.P-0x3EBE215C_priv.asc",
    21                               "test_keys/pub/pep.color.test.H-0xD17E598E_pub.asc",
    22                               "test_keys/pub/pep.color.test.L-0xE9CDB4CE_pub.asc",
    23                               "test_keys/pub/pep.color.test.P-0x3EBE215C_pub.asc",
    24                               "test_keys/pub/pep.color.test.V-0x71FC6D28_pub.asc"
    25                           };
    26     
    27     PEP_SESSION session;
    28     
    29     cout << "calling init()\n";
    30     PEP_STATUS status1 = init(&session);   
    31     assert(status1 == PEP_STATUS_OK);
    32     assert(session);
    33     cout << "init() completed.\n";
    34     
    35     for (auto name : keynames) {
    36         cout << "\t read keyfile \"" << name << "\"..." << std::endl;
    37         const string keytextkey = slurp(name);
    38         PEP_STATUS statuskey = import_key(session, keytextkey.c_str(), keytextkey.length(), NULL);
    39         assert(statuskey == PEP_STATUS_OK);
    40     }
    41     
    42     cout << "\t read keyfile mailfile \"" << mailfile << "\"..." << std::endl;
    43     const string mailtext = slurp(mailfile);
    44     cout << "\t All files read successfully." << std::endl;
    45 
    46     pEp_identity * me1 = new_identity("pep.color.test.P@kgrothoff.org", NULL, 
    47                                       PEP_OWN_USERID, "Pep Color Test P (recip)");
    48     me1->me = true;
    49     PEP_STATUS status = update_identity(session, me1);
    50     trust_personal_key(session, me1);
    51     status = update_identity(session, me1);
    52     
    53     pEp_identity * sender1 = new_identity("pep.color.test.V@kgrothoff.org",
    54                                           NULL, "TOFU_pep.color.test.V@kgrothoff.org",
    55                                           "Pep Color Test V (sender)");
    56     
    57     sender1->me = false;
    58     status = update_identity(session, sender1);
    59     trust_personal_key(session, sender1);
    60     status = update_identity(session, sender1);
    61     
    62     message* msg_ptr = nullptr;
    63     message* dest_msg = nullptr;
    64     message* final_ptr = nullptr;
    65     stringlist_t* keylist = nullptr;
    66     PEP_rating rating;
    67     PEP_decrypt_flags_t flags;
    68     
    69     status = mime_decode_message(mailtext.c_str(), mailtext.length(), &msg_ptr);
    70     assert(status == PEP_STATUS_OK);
    71     assert(msg_ptr);
    72     final_ptr = msg_ptr;
    73     status = decrypt_message(session, msg_ptr, &dest_msg, &keylist, &rating, &flags);
    74     final_ptr = dest_msg ? dest_msg : msg_ptr;
    75   
    76     cout << "shortmsg: " << final_ptr->shortmsg << endl << endl;
    77     cout << "longmsg: " << final_ptr->longmsg << endl << endl;
    78     cout << "longmsg_formatted: " << (final_ptr->longmsg_formatted ? final_ptr->longmsg_formatted : "(empty)") << endl << endl;
    79     cout << "rating: " << rating << endl << endl;
    80     cout << "keys used: " << endl;
    81     
    82     int i = 0;
    83     for (stringlist_t* k = keylist; k; k = k->next) {
    84         if (i == 0)
    85             cout << "\t Signer (key 0):\t" << k->value << endl;
    86         else
    87             cout << "\t #" << i << ":\t" << k->value << endl;
    88         i++;
    89     }
    90     
    91 //    free_identity(me1);
    92     if (final_ptr == dest_msg)
    93     	free_message(dest_msg);
    94     free_message(msg_ptr);
    95     free_stringlist(keylist);
    96     
    97     cout << "calling release()\n";
    98     release(session);
    99     return 0;
   100 }