test/src/engine_tests/EncryptMissingPrivateKeyTests.cc
author Krista 'DarthMama' Bennett <krista@pep.foundation>
Tue, 29 Jan 2019 19:19:30 +0100
branchENGINE-448
changeset 3254 6e7f6bc9460a
parent 3049 edbd93b17440
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 <iostream>
     6 #include <fstream>
     7 #include <string>
     8 #include <cstring> // for strcmp()
     9 #include <assert.h>
    10 #include <cpptest.h>
    11 
    12 #include "blacklist.h"
    13 #include "keymanagement.h"
    14 #include "message_api.h"
    15 #include "mime.h"
    16 #include "test_util.h"
    17 
    18 #include "pEpEngine.h"
    19 
    20 using namespace std;
    21 
    22 #include "EngineTestSessionSuite.h"
    23 #include "EncryptMissingPrivateKeyTests.h"
    24 
    25 using namespace std;
    26 
    27 EncryptMissingPrivateKeyTests::EncryptMissingPrivateKeyTests(string suitename, string test_home_dir) :
    28     EngineTestSessionSuite::EngineTestSessionSuite(suitename, test_home_dir) {
    29     add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("EncryptMissingPrivateKeyTests::check_encrypt_missing_private_key"),
    30                                                                       static_cast<Func>(&EncryptMissingPrivateKeyTests::check_encrypt_missing_private_key)));
    31 }
    32 
    33 void EncryptMissingPrivateKeyTests::setup() {
    34     EngineTestSessionSuite::setup();
    35     string recip_key = slurp("test_keys/pub/pep-test-bob-0xC9C2EE39_pub.asc");
    36     PEP_STATUS status = import_key(session, recip_key.c_str(), recip_key.size(), NULL);
    37     assert(status == PEP_KEY_IMPORTED);
    38 }
    39 
    40 void EncryptMissingPrivateKeyTests::check_encrypt_missing_private_key() {
    41     
    42     pEp_identity* no_key_identity = new_identity("blacklistself@kgrothoff.org",
    43                                                       NULL,
    44                                                       PEP_OWN_USERID,
    45                                                       "Blacklist Self");
    46     no_key_identity->me = true;
    47     PEP_STATUS status8 = myself(session, no_key_identity);
    48     TEST_ASSERT (status8 == PEP_STATUS_OK);
    49 
    50     /* Now let's try to encrypt a message. */
    51         
    52     message* tmp_msg = NULL;
    53     message* enc_msg = NULL;
    54     
    55     const string mailtext = slurp("test_mails/blacklist_no_key.eml");
    56 
    57     PEP_STATUS status = mime_decode_message(mailtext.c_str(), mailtext.length(), &tmp_msg);
    58     TEST_ASSERT_MSG((status == PEP_STATUS_OK), "status == PEP_STATUS_OK");
    59     
    60     status = update_identity(session, tmp_msg->from);
    61     identity_list* to_list = tmp_msg->to;
    62 
    63     while (to_list) {
    64         if (to_list->ident)
    65             update_identity(session, to_list->ident);
    66         to_list = to_list->next;
    67     }
    68     
    69     // This isn't incoming, though... so we need to reverse the direction
    70     tmp_msg->dir = PEP_dir_outgoing;
    71     status = encrypt_message(session,
    72                              tmp_msg,
    73                              NULL,
    74                              &enc_msg,
    75                              PEP_enc_PGP_MIME,
    76                              0);
    77     TEST_ASSERT_MSG((status == PEP_STATUS_OK), "status == PEP_STATUS_OK");
    78     
    79 
    80     char* new_key = enc_msg->from->fpr;
    81     cout << "Encrypted with key " << new_key << endl;
    82     
    83     free_message(tmp_msg);    
    84     free_message(enc_msg);
    85 }