test/src/engine_tests/LeastColorGroupTests.cc
author Krista 'DarthMama' Bennett <krista@pep.foundation>
Tue, 29 Jan 2019 19:19:30 +0100
branchENGINE-448
changeset 3254 6e7f6bc9460a
parent 2928 6d8e51098e7a
child 3276 c0b3430f1f1d
permissions -rw-r--r--
ENGINE-448: made key removal much less aggressive. NetPGP will still be a problem, but versions built against gpg will now only remove actual keys
     1 // This file is under GNU General Public License 3.0
     2 // see LICENSE.txt
     3 
     4 #include <stdlib.h>
     5 #include <string>
     6 #include <iostream>
     7 #include <vector>
     8 #include <cstring> // for strcmp()
     9 #include "keymanagement.h"
    10 #include "message_api.h"
    11 #include "mime.h"
    12 #include "test_util.h"
    13 
    14 #include "pEpEngine.h"
    15 
    16 #include <cpptest.h>
    17 #include "EngineTestSessionSuite.h"
    18 #include "LeastColorGroupTests.h"
    19 
    20 using namespace std;
    21 
    22 LeastColorGroupTests::LeastColorGroupTests(string suitename, string test_home_dir) :
    23     EngineTestSessionSuite::EngineTestSessionSuite(suitename, test_home_dir) {
    24     add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("LeastColorGroupTests::check_least_color_group"),
    25                                                                       static_cast<Func>(&LeastColorGroupTests::check_least_color_group)));
    26 }
    27 
    28 void LeastColorGroupTests::check_least_color_group() {
    29     
    30     const char* mailfile = "test_mails/color_test.eml";
    31     
    32     const std::vector<const char*> keynames = {
    33                               "test_keys/priv/pep.color.test.P-0x3EBE215C_priv.asc",
    34                               "test_keys/pub/pep.color.test.H-0xD17E598E_pub.asc",
    35                               "test_keys/pub/pep.color.test.L-0xE9CDB4CE_pub.asc",
    36                               "test_keys/pub/pep.color.test.P-0x3EBE215C_pub.asc",
    37                               "test_keys/pub/pep.color.test.V-0x71FC6D28_pub.asc"
    38                           };
    39             
    40     for (auto name : keynames) {
    41         cout << "\t read keyfile \"" << name << "\"..." << std::endl;
    42         const string keytextkey = slurp(name);
    43         PEP_STATUS statuskey = import_key(session, keytextkey.c_str(), keytextkey.length(), NULL);
    44         TEST_ASSERT_MSG((statuskey == PEP_KEY_IMPORTED), "statuskey == PEP_STATUS_OK");
    45     }
    46     
    47     cout << "\t read keyfile mailfile \"" << mailfile << "\"..." << std::endl;
    48     const string mailtext = slurp(mailfile);
    49     cout << "\t All files read successfully." << std::endl;
    50 
    51     pEp_identity * me1 = new_identity("pep.color.test.P@kgrothoff.org", 
    52                                       "7EE6C60C68851954E1797F81EA59715E3EBE215C", 
    53                                       PEP_OWN_USERID, "Pep Color Test P (recip)");
    54     me1->me = true;
    55     PEP_STATUS status = myself(session, me1);
    56     
    57     pEp_identity * sender1 = new_identity("pep.color.test.V@kgrothoff.org",
    58                                           NULL, "TOFU_pep.color.test.V@kgrothoff.org",
    59                                           "Pep Color Test V (sender)");
    60     status = update_identity(session, sender1);
    61     trust_personal_key(session, sender1);
    62     status = update_identity(session, sender1);
    63     
    64     message* msg_ptr = nullptr;
    65     message* dest_msg = nullptr;
    66     message* final_ptr = nullptr;
    67     stringlist_t* keylist = nullptr;
    68     PEP_rating rating;
    69     PEP_decrypt_flags_t flags;
    70     
    71     status = mime_decode_message(mailtext.c_str(), mailtext.length(), &msg_ptr);
    72     TEST_ASSERT_MSG((status == PEP_STATUS_OK), "status == PEP_STATUS_OK");
    73     TEST_ASSERT_MSG((msg_ptr), "msg_ptr");
    74     final_ptr = msg_ptr;
    75     flags = 0;
    76     status = decrypt_message(session, msg_ptr, &dest_msg, &keylist, &rating, &flags);
    77     final_ptr = dest_msg ? dest_msg : msg_ptr;
    78   
    79     cout << "shortmsg: " << final_ptr->shortmsg << endl << endl;
    80     cout << "longmsg: " << final_ptr->longmsg << endl << endl;
    81     cout << "longmsg_formatted: " << (final_ptr->longmsg_formatted ? final_ptr->longmsg_formatted : "(empty)") << endl << endl;
    82     cout << "rating: " << rating << endl << endl;
    83     cout << "keys used: " << endl;
    84     
    85     int i = 0;
    86     for (stringlist_t* k = keylist; k; k = k->next) {
    87         if (i == 0)
    88             cout << "\t Signer (key 0):\t" << k->value << endl;
    89         else
    90             cout << "\t #" << i << ":\t" << k->value << endl;
    91         i++;
    92     }
    93     
    94 //    free_identity(me1);
    95     if (final_ptr == dest_msg)
    96     	free_message(dest_msg);
    97     free_message(msg_ptr);
    98     free_stringlist(keylist);    
    99 }