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