test/src/engine_tests/DecryptAttachPrivateKeyTrustedTests.cc
author Krista 'DarthMama' Bennett <krista@pep.foundation>
Tue, 29 Jan 2019 19:19:30 +0100
branchENGINE-448
changeset 3254 6e7f6bc9460a
parent 3222 fcc3d6543681
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
krista@2602
     1
// This file is under GNU General Public License 3.0
krista@2602
     2
// see LICENSE.txt
krista@2602
     3
krista@2654
     4
// FIXME: the private key tests should be combined
krista@2654
     5
krista@2602
     6
#include <stdlib.h>
krista@2654
     7
#include <string>
krista@2654
     8
#include <cstring>
krista@2654
     9
krista@2654
    10
#include "pEpEngine.h"
krista@2654
    11
krista@2602
    12
#include "mime.h"
krista@2602
    13
#include "message_api.h"
krista@2602
    14
#include "keymanagement.h"
krista@2602
    15
#include "test_util.h"
krista@2602
    16
krista@2654
    17
#include "EngineTestSessionSuite.h"
krista@2654
    18
#include "DecryptAttachPrivateKeyTrustedTests.h"
krista@2654
    19
krista@2602
    20
using namespace std;
krista@2602
    21
krista@2654
    22
DecryptAttachPrivateKeyTrustedTests::DecryptAttachPrivateKeyTrustedTests(string suitename, string test_home_dir) :
krista@2654
    23
    EngineTestSessionSuite::EngineTestSessionSuite(suitename, test_home_dir) {
krista@2654
    24
    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("DecryptAttachPrivateKeyTrustedTests::check_decrypt_attach_private_key_trusted"),
krista@2654
    25
                                                                      static_cast<Func>(&DecryptAttachPrivateKeyTrustedTests::check_decrypt_attach_private_key_trusted)));
krista@2654
    26
}
krista@2602
    27
krista@2654
    28
void DecryptAttachPrivateKeyTrustedTests::check_decrypt_attach_private_key_trusted() {
krista@2602
    29
krista@2602
    30
    const char* own_uid = PEP_OWN_USERID;
krista@2602
    31
krista@2602
    32
    cout << "Importing keys..." << endl;
krista@2602
    33
    
krista@2602
    34
    string input_key;
krista@2602
    35
    const char* main_addr = "priv-key-import-test-main@darthmama.cool";
krista@2602
    36
    pEp_identity* main_me = NULL;
krista@2602
    37
    const char* fpr_main_me = "13A9F97964A2B52520CAA40E51BCA783C065A213";    
krista@2602
    38
    pEp_identity* same_addr_same_uid = NULL;
krista@2602
    39
    const char* fpr_same_addr_same_uid = "8AB616A3BD51DEF714B5E688EFFB540C3276D2E5";
krista@2602
    40
        
krista@2602
    41
    PEP_STATUS status = PEP_STATUS_OK;
krista@2602
    42
krista@2602
    43
    // key for main own user
krista@2602
    44
    // 
krista@2602
    45
    // 13A9F97964A2B52520CAA40E51BCA783C065A213    
krista@2602
    46
    input_key = slurp("test_keys/pub/priv-key-import-test-main_0-0xC065A213_pub.asc");
krista@2602
    47
    status = import_key(session, input_key.c_str(), input_key.length(), NULL);
krista@3254
    48
    TEST_ASSERT_MSG((status == PEP_KEY_IMPORTED), tl_status_string(status));
krista@2602
    49
krista@2602
    50
    input_key = slurp("test_keys/priv/priv-key-import-test-main_0-0xC065A213_priv.asc");
krista@2602
    51
    status = import_key(session, input_key.c_str(), input_key.length(), NULL);
krista@3254
    52
    TEST_ASSERT_MSG((status == PEP_KEY_IMPORTED), tl_status_string(status));
krista@2602
    53
krista@2603
    54
    // ensure there's no private key - doesn't work in automated tests, sadly. Uncommon when running script manually.
krista@2602
    55
    bool has_priv = false;
krista@2602
    56
        
krista@2602
    57
    // key with same address and user_id
krista@2602
    58
    // 8AB616A3BD51DEF714B5E688EFFB540C3276D2E5
krista@2602
    59
    input_key = slurp("test_keys/pub/priv-key-import-test-main_0-0x3276D2E5_pub.asc");
krista@2602
    60
    status = import_key(session, input_key.c_str(), input_key.length(), NULL);
krista@3254
    61
    TEST_ASSERT_MSG((status == PEP_KEY_IMPORTED), tl_status_string(status));
krista@2602
    62
krista@2602
    63
    
krista@2602
    64
    cout << "Setting up own identity with default key " << fpr_main_me << endl;
krista@2602
    65
    // Own identity with default key etc
krista@2602
    66
    main_me = new_identity(main_addr, fpr_main_me, own_uid, "PrivateKey Import Test");
krista@2602
    67
    status = set_own_key(session, main_me, fpr_main_me);
krista@3222
    68
    TEST_ASSERT_MSG((status == PEP_STATUS_OK), tl_status_string(status));
krista@2602
    69
krista@2669
    70
    TEST_ASSERT_MSG((strcmp(main_me->fpr, fpr_main_me) == 0), "strcmp(main_me->fpr, fpr_main_me) == 0");
krista@2602
    71
    cout << "Done!" << endl << endl;
krista@2602
    72
    
krista@2602
    73
    cout << "Setting up sender identities and resetting key trust." << endl;
krista@2602
    74
    cout << "Same address, same user_id - address: " << main_addr << ", user_id: " << own_uid << ", fpr: " << fpr_same_addr_same_uid << endl;  
krista@2602
    75
    same_addr_same_uid = new_identity(main_addr, fpr_same_addr_same_uid, own_uid, "PrivateKey Import Test");
krista@3222
    76
    TEST_ASSERT_MSG((status == PEP_STATUS_OK || status == PEP_CANNOT_FIND_IDENTITY), tl_status_string(status));
krista@3222
    77
    TEST_ASSERT_MSG(((same_addr_same_uid->comm_type & PEP_ct_confirmed) != PEP_ct_confirmed), tl_ct_string(same_addr_same_uid->comm_type));
krista@2602
    78
krista@2602
    79
    status = key_reset_trust(session, same_addr_same_uid);
krista@2602
    80
    
krista@2602
    81
    cout << "Done!" << endl << endl;
krista@2602
    82
krista@2602
    83
    cout << "Reading in message..." << endl;
krista@2602
    84
    
krista@2602
    85
    string encoded_text = slurp("test_mails/priv_key_attach.eml");
krista@2602
    86
krista@2602
    87
    cout << "Starting test..." << endl;
krista@2602
    88
    // Case 1:
krista@2602
    89
    // Same address, same user_id, untrusted
krista@2602
    90
    cout << "decrypt with attached private key: Same address, same user_id, trusted" << endl;
krista@2602
    91
    char* decrypted_text = NULL;
krista@2602
    92
    stringlist_t* keylist_used = NULL;
krista@2602
    93
    PEP_rating rating;
krista@2615
    94
    PEP_decrypt_flags_t flags = 0;
krista@2623
    95
    char* modified_src = NULL;
krista@2602
    96
    
krista@3222
    97
    cout << "Trusting own key for " << same_addr_same_uid->user_id << " and " << same_addr_same_uid->fpr << endl;
krista@3222
    98
    status = trust_own_key(session, same_addr_same_uid);
krista@2603
    99
    cout << "Status is " << tl_status_string(status) << endl;  
krista@3222
   100
    TEST_ASSERT_MSG((status == PEP_STATUS_OK), tl_status_string(status));
krista@2602
   101
    free(decrypted_text);
krista@2602
   102
    decrypted_text = NULL;
krista@2602
   103
krista@2602
   104
    status = get_trust(session, same_addr_same_uid);
krista@2602
   105
    cout << tl_ct_string(same_addr_same_uid->comm_type) << endl;
krista@2602
   106
    
krista@2669
   107
    TEST_ASSERT_MSG((same_addr_same_uid->comm_type == PEP_ct_pEp), "same_addr_same_uid->comm_type == PEP_ct_pEp");
krista@2602
   108
    
krista@2615
   109
    flags = 0;
krista@2602
   110
    status = MIME_decrypt_message(session, encoded_text.c_str(), 
krista@2602
   111
                                  encoded_text.size(), &decrypted_text, 
krista@2623
   112
                                  &keylist_used, &rating, &flags,
krista@2623
   113
                                  &modified_src);
krista@2602
   114
krista@2602
   115
    status = get_trust(session, same_addr_same_uid);
krista@2669
   116
    TEST_ASSERT_MSG((same_addr_same_uid->comm_type == PEP_ct_pEp), "same_addr_same_uid->comm_type == PEP_ct_pEp");
krista@2602
   117
    
krista@2615
   118
    flags = 0;
krista@2602
   119
    status = MIME_decrypt_message(session, encoded_text.c_str(), 
krista@2602
   120
                                  encoded_text.size(), &decrypted_text, 
krista@2623
   121
                                  &keylist_used, &rating, &flags,
krista@2623
   122
                                  &modified_src);
krista@2602
   123
    
krista@2602
   124
    cout << "Status: " << tl_status_string(status) << endl;
krista@3222
   125
    TEST_ASSERT_MSG((status == PEP_STATUS_OK), tl_status_string(status));
krista@2602
   126
krista@2602
   127
    cout << decrypted_text << endl;
krista@2602
   128
    
krista@2602
   129
    has_priv = false;
krista@2602
   130
    status = contains_priv_key(session, fpr_same_addr_same_uid, &has_priv);
krista@2669
   131
    TEST_ASSERT_MSG((has_priv == true), "has_priv == true");
krista@2602
   132
    cout << "Private key was also imported." << endl;
krista@2602
   133
    
krista@2602
   134
    cout << "PASS!" << endl;
krista@2654
   135
    
krista@2654
   136
    // FIXME: rework this in new framework
krista@2602
   137
    status = key_reset_trust(session, main_me);      
krista@2602
   138
    status = key_reset_trust(session, same_addr_same_uid);      
krista@2602
   139
}