ENGINE-420: added forgotten test ENGINE-420
authorKrista Bennett <krista@pep-project.org>
Mon, 16 Apr 2018 12:21:02 +0200
branchENGINE-420
changeset 2602efda0875888a
parent 2601 9c12675c68e1
child 2603 a6a06822ec6b
ENGINE-420: added forgotten test
test/decrypt_attach_private_key_trusted_test.cc
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/decrypt_attach_private_key_trusted_test.cc	Mon Apr 16 12:21:02 2018 +0200
     1.3 @@ -0,0 +1,144 @@
     1.4 +// This file is under GNU General Public License 3.0
     1.5 +// see LICENSE.txt
     1.6 +
     1.7 +#include <stdlib.h>
     1.8 +#include <string.h>
     1.9 +#include "platform.h"
    1.10 +#include <iostream>
    1.11 +#include <fstream>
    1.12 +#include <assert.h>
    1.13 +#include "mime.h"
    1.14 +#include "message_api.h"
    1.15 +#include "keymanagement.h"
    1.16 +#include "test_util.h"
    1.17 +
    1.18 +using namespace std;
    1.19 +
    1.20 +int main() {
    1.21 +    cout << "\n*** decrypt_attach_private_key_trusted_test ***\n\n";
    1.22 +
    1.23 +    PEP_SESSION session;
    1.24 +    
    1.25 +    cout << "calling init()\n";
    1.26 +    PEP_STATUS status1 = init(&session);
    1.27 +    assert(status1 == PEP_STATUS_OK);
    1.28 +    assert(session);
    1.29 +    cout << "init() completed.\n";
    1.30 +
    1.31 +    const char* own_uid = PEP_OWN_USERID;
    1.32 +
    1.33 +    cout << "Importing keys..." << endl;
    1.34 +    
    1.35 +    string input_key;
    1.36 +    const char* main_addr = "priv-key-import-test-main@darthmama.cool";
    1.37 +    pEp_identity* main_me = NULL;
    1.38 +    const char* fpr_main_me = "13A9F97964A2B52520CAA40E51BCA783C065A213";    
    1.39 +    pEp_identity* same_addr_same_uid = NULL;
    1.40 +    const char* fpr_same_addr_same_uid = "8AB616A3BD51DEF714B5E688EFFB540C3276D2E5";
    1.41 +        
    1.42 +    PEP_STATUS status = PEP_STATUS_OK;
    1.43 +
    1.44 +    // key for main own user
    1.45 +    // 
    1.46 +    // 13A9F97964A2B52520CAA40E51BCA783C065A213    
    1.47 +    input_key = slurp("test_keys/pub/priv-key-import-test-main_0-0xC065A213_pub.asc");
    1.48 +    status = import_key(session, input_key.c_str(), input_key.length(), NULL);
    1.49 +    assert(status == PEP_STATUS_OK);
    1.50 +
    1.51 +    input_key = slurp("test_keys/priv/priv-key-import-test-main_0-0xC065A213_priv.asc");
    1.52 +    status = import_key(session, input_key.c_str(), input_key.length(), NULL);
    1.53 +    assert(status == PEP_STATUS_OK);
    1.54 +
    1.55 +    // ensure there's no private key
    1.56 +    bool has_priv = false;
    1.57 +    status = contains_priv_key(session, fpr_same_addr_same_uid, &has_priv);
    1.58 +    if (status == PEP_STATUS_OK && has_priv) {
    1.59 +        cout << "SORRY, have to delete keys here to run test correctly..." << endl;
    1.60 +        status = delete_keypair(session, fpr_same_addr_same_uid);
    1.61 +        if (status == PEP_STATUS_OK) {
    1.62 +            has_priv = false;
    1.63 +            status = contains_priv_key(session, fpr_same_addr_same_uid, &has_priv);
    1.64 +            assert(has_priv == false);
    1.65 +            cout << "Successfully deleted keypair for " << fpr_same_addr_same_uid << " - will now import the public key only" << endl;
    1.66 +        }
    1.67 +        else
    1.68 +            cout << "Warning - delete keypair returned status " << tl_status_string(status) << ". This may or may not be an error, depending on what you expect." << endl;            
    1.69 +    }
    1.70 +        
    1.71 +    // key with same address and user_id
    1.72 +    // 8AB616A3BD51DEF714B5E688EFFB540C3276D2E5
    1.73 +    input_key = slurp("test_keys/pub/priv-key-import-test-main_0-0x3276D2E5_pub.asc");
    1.74 +    status = import_key(session, input_key.c_str(), input_key.length(), NULL);
    1.75 +    assert(status == PEP_STATUS_OK);
    1.76 +
    1.77 +    
    1.78 +    cout << "Setting up own identity with default key " << fpr_main_me << endl;
    1.79 +    // Own identity with default key etc
    1.80 +    main_me = new_identity(main_addr, fpr_main_me, own_uid, "PrivateKey Import Test");
    1.81 +    status = set_own_key(session, main_me, fpr_main_me);
    1.82 +    assert(status == PEP_STATUS_OK);
    1.83 +
    1.84 +    assert(strcmp(main_me->fpr, fpr_main_me) == 0);
    1.85 +    cout << "Done!" << endl << endl;
    1.86 +    
    1.87 +    cout << "Setting up sender identities and resetting key trust." << endl;
    1.88 +    cout << "Same address, same user_id - address: " << main_addr << ", user_id: " << own_uid << ", fpr: " << fpr_same_addr_same_uid << endl;  
    1.89 +    same_addr_same_uid = new_identity(main_addr, fpr_same_addr_same_uid, own_uid, "PrivateKey Import Test");
    1.90 +    assert(status == PEP_STATUS_OK || status == PEP_CANNOT_FIND_IDENTITY);
    1.91 +    assert((same_addr_same_uid->comm_type & PEP_ct_confirmed) != PEP_ct_confirmed);
    1.92 +
    1.93 +    status = key_reset_trust(session, same_addr_same_uid);
    1.94 +    
    1.95 +    cout << "Done!" << endl << endl;
    1.96 +
    1.97 +    cout << "Reading in message..." << endl;
    1.98 +    
    1.99 +    string encoded_text = slurp("test_mails/priv_key_attach.eml");
   1.100 +
   1.101 +    cout << "Starting test..." << endl;
   1.102 +    // Case 1:
   1.103 +    // Same address, same user_id, untrusted
   1.104 +    cout << "decrypt with attached private key: Same address, same user_id, trusted" << endl;
   1.105 +    char* decrypted_text = NULL;
   1.106 +    stringlist_t* keylist_used = NULL;
   1.107 +    PEP_rating rating;
   1.108 +    PEP_decrypt_flags_t flags;
   1.109 +    
   1.110 +    status = trust_personal_key(session, same_addr_same_uid);
   1.111 +    assert(status == PEP_STATUS_OK);
   1.112 +    free(decrypted_text);
   1.113 +    decrypted_text = NULL;
   1.114 +
   1.115 +    status = get_trust(session, same_addr_same_uid);
   1.116 +    cout << tl_ct_string(same_addr_same_uid->comm_type) << endl;
   1.117 +    
   1.118 +    assert(same_addr_same_uid->comm_type == PEP_ct_pEp);
   1.119 +    
   1.120 +    status = MIME_decrypt_message(session, encoded_text.c_str(), 
   1.121 +                                  encoded_text.size(), &decrypted_text, 
   1.122 +                                  &keylist_used, &rating, &flags);
   1.123 +
   1.124 +    status = get_trust(session, same_addr_same_uid);
   1.125 +    assert(same_addr_same_uid->comm_type == PEP_ct_pEp);
   1.126 +    
   1.127 +    status = MIME_decrypt_message(session, encoded_text.c_str(), 
   1.128 +                                  encoded_text.size(), &decrypted_text, 
   1.129 +                                  &keylist_used, &rating, &flags);
   1.130 +    
   1.131 +    cout << "Status: " << tl_status_string(status) << endl;
   1.132 +    assert(status == PEP_STATUS_OK);
   1.133 +
   1.134 +    cout << decrypted_text << endl;
   1.135 +    
   1.136 +    has_priv = false;
   1.137 +    status = contains_priv_key(session, fpr_same_addr_same_uid, &has_priv);
   1.138 +    assert(has_priv == true);
   1.139 +    cout << "Private key was also imported." << endl;
   1.140 +    
   1.141 +    cout << "PASS!" << endl;
   1.142 +    status = key_reset_trust(session, main_me);      
   1.143 +    status = key_reset_trust(session, same_addr_same_uid);      
   1.144 +    release(session);
   1.145 +    
   1.146 +    return 0;
   1.147 +}