ENGINE-420: separated out trusted vs. untrusted tests, as automated test runs fail due to bad gpg key deletion nonsense (it's gpgme, not us) ENGINE-420
authorKrista Bennett <krista@pep-project.org>
Mon, 16 Apr 2018 12:18:18 +0200
branchENGINE-420
changeset 26019c12675c68e1
parent 2600 8fc17cbaa30f
child 2602 efda0875888a
ENGINE-420: separated out trusted vs. untrusted tests, as automated test runs fail due to bad gpg key deletion nonsense (it's gpgme, not us)
test/decrypt_attach_private_key_test.cc
test/decrypt_attach_private_key_untrusted_test.cc
     1.1 --- a/test/decrypt_attach_private_key_test.cc	Mon Apr 16 11:38:00 2018 +0200
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,168 +0,0 @@
     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_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 -            cout << "Successfully deleted keypair for " << fpr_same_addr_same_uid << " - will now import the public key only" << endl;
    1.63 -    }
    1.64 -        
    1.65 -    // key with same address and user_id
    1.66 -    // 8AB616A3BD51DEF714B5E688EFFB540C3276D2E5
    1.67 -    input_key = slurp("test_keys/pub/priv-key-import-test-main_0-0x3276D2E5_pub.asc");
    1.68 -    status = import_key(session, input_key.c_str(), input_key.length(), NULL);
    1.69 -    assert(status == PEP_STATUS_OK);
    1.70 -
    1.71 -    
    1.72 -    cout << "Setting up own identity with default key " << fpr_main_me << endl;
    1.73 -    // Own identity with default key etc
    1.74 -    main_me = new_identity(main_addr, fpr_main_me, own_uid, "PrivateKey Import Test");
    1.75 -    status = set_own_key(session, main_me, fpr_main_me);
    1.76 -    assert(status == PEP_STATUS_OK);
    1.77 -
    1.78 -    assert(strcmp(main_me->fpr, fpr_main_me) == 0);
    1.79 -    cout << "Done!" << endl << endl;
    1.80 -    
    1.81 -    cout << "Setting up sender identities and resetting key trust." << endl;
    1.82 -    cout << "#1: same address, same user_id - address: " << main_addr << ", user_id: " << own_uid << ", fpr: " << fpr_same_addr_same_uid << endl;  
    1.83 -    same_addr_same_uid = new_identity(main_addr, fpr_same_addr_same_uid, own_uid, "PrivateKey Import Test");
    1.84 -    assert(status == PEP_STATUS_OK || status == PEP_CANNOT_FIND_IDENTITY);
    1.85 -    assert((same_addr_same_uid->comm_type & PEP_ct_confirmed) != PEP_ct_confirmed);
    1.86 -
    1.87 -    status = key_reset_trust(session, same_addr_same_uid);
    1.88 -    
    1.89 -    cout << "Done!" << endl << endl;
    1.90 -
    1.91 -    cout << "Reading in message..." << endl;
    1.92 -    
    1.93 -    string encoded_text = slurp("test_mails/priv_key_attach.eml");
    1.94 -
    1.95 -    cout << "Starting tests..." << endl;
    1.96 -    // Case 1:
    1.97 -    // Same address, same user_id, untrusted
    1.98 -    cout << "Case 1: Same address, same user_id, untrusted" << endl;
    1.99 -    char* decrypted_text = NULL;
   1.100 -    stringlist_t* keylist_used = NULL;
   1.101 -    PEP_rating rating;
   1.102 -    PEP_decrypt_flags_t flags;
   1.103 -    
   1.104 -    status = get_trust(session, same_addr_same_uid);
   1.105 -    cout << tl_ct_string(same_addr_same_uid->comm_type) << endl;
   1.106 -    
   1.107 -    assert((same_addr_same_uid->comm_type & PEP_ct_confirmed) != PEP_ct_confirmed);
   1.108 -    
   1.109 -    status = MIME_decrypt_message(session, encoded_text.c_str(), 
   1.110 -                                  encoded_text.size(), &decrypted_text, 
   1.111 -                                  &keylist_used, &rating, &flags);
   1.112 -
   1.113 -    status = get_trust(session, same_addr_same_uid);
   1.114 -    assert(same_addr_same_uid->comm_type == PEP_ct_pEp_unconfirmed);
   1.115 -
   1.116 -    cout << "Case 1 Status: " << tl_status_string(status) << endl; 
   1.117 -    cout << "Private key is not trusted for " << same_addr_same_uid->fpr << ", as desired, as the public key was not trusted." << endl;
   1.118 -    cout << "PASS!" << endl;
   1.119 -
   1.120 -    // Case 2:
   1.121 -    cout << decrypted_text << endl;
   1.122 -    // Same address, same_user_id, trusted
   1.123 -    cout << "Case 2: Same address, same user_id, trusted" << endl;
   1.124 -    
   1.125 -    // remove private key
   1.126 -    cout << "SORRY, have to delete keys here to continue to run test..." << endl;
   1.127 -    status = delete_keypair(session, fpr_same_addr_same_uid);
   1.128 -    if (status == PEP_STATUS_OK)
   1.129 -        cout << "Successfully deleted keypair for " << fpr_same_addr_same_uid << " - will now import the public key only" << endl;
   1.130 -        
   1.131 -    // key with same address and user_id
   1.132 -    // 8AB616A3BD51DEF714B5E688EFFB540C3276D2E5
   1.133 -    status = import_key(session, input_key.c_str(), input_key.length(), NULL);
   1.134 -    assert(status == PEP_STATUS_OK);
   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 == false);
   1.139 -    cout << "(Double-checking - Private key successfully deleted.)" << endl;
   1.140 -    
   1.141 -    status = trust_personal_key(session, same_addr_same_uid);
   1.142 -    assert(status == PEP_STATUS_OK);
   1.143 -    free(decrypted_text);
   1.144 -    decrypted_text = NULL;
   1.145 -
   1.146 -    status = get_trust(session, same_addr_same_uid);
   1.147 -    cout << tl_ct_string(same_addr_same_uid->comm_type) << endl;
   1.148 -    
   1.149 -    assert(same_addr_same_uid->comm_type == PEP_ct_pEp);
   1.150 -    
   1.151 -    status = MIME_decrypt_message(session, encoded_text.c_str(), 
   1.152 -                                  encoded_text.size(), &decrypted_text, 
   1.153 -                                  &keylist_used, &rating, &flags);
   1.154 -    
   1.155 -    cout << "Case 2 Status: " << tl_status_string(status) << endl;
   1.156 -    assert(status == PEP_STATUS_OK);
   1.157 -
   1.158 -    cout << decrypted_text << endl;
   1.159 -    
   1.160 -    has_priv = false;
   1.161 -    status = contains_priv_key(session, fpr_same_addr_same_uid, &has_priv);
   1.162 -    assert(has_priv == true);
   1.163 -    cout << "Private key was also imported." << endl;
   1.164 -    
   1.165 -    cout << "PASS!" << endl;
   1.166 -    status = key_reset_trust(session, main_me);      
   1.167 -    status = key_reset_trust(session, same_addr_same_uid);      
   1.168 -    release(session);
   1.169 -    
   1.170 -    return 0;
   1.171 -}
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/test/decrypt_attach_private_key_untrusted_test.cc	Mon Apr 16 12:18:18 2018 +0200
     2.3 @@ -0,0 +1,125 @@
     2.4 +// This file is under GNU General Public License 3.0
     2.5 +// see LICENSE.txt
     2.6 +
     2.7 +#include <stdlib.h>
     2.8 +#include <string.h>
     2.9 +#include "platform.h"
    2.10 +#include <iostream>
    2.11 +#include <fstream>
    2.12 +#include <assert.h>
    2.13 +#include "mime.h"
    2.14 +#include "message_api.h"
    2.15 +#include "keymanagement.h"
    2.16 +#include "test_util.h"
    2.17 +
    2.18 +using namespace std;
    2.19 +
    2.20 +int main() {
    2.21 +    cout << "\n*** decrypt_attach_private_key_untrusted_test ***\n\n";
    2.22 +
    2.23 +    PEP_SESSION session;
    2.24 +    
    2.25 +    cout << "calling init()\n";
    2.26 +    PEP_STATUS status1 = init(&session);
    2.27 +    assert(status1 == PEP_STATUS_OK);
    2.28 +    assert(session);
    2.29 +    cout << "init() completed.\n";
    2.30 +
    2.31 +    const char* own_uid = PEP_OWN_USERID;
    2.32 +
    2.33 +    cout << "Importing keys..." << endl;
    2.34 +    
    2.35 +    string input_key;
    2.36 +    const char* main_addr = "priv-key-import-test-main@darthmama.cool";
    2.37 +    pEp_identity* main_me = NULL;
    2.38 +    const char* fpr_main_me = "13A9F97964A2B52520CAA40E51BCA783C065A213";    
    2.39 +    pEp_identity* same_addr_same_uid = NULL;
    2.40 +    const char* fpr_same_addr_same_uid = "8AB616A3BD51DEF714B5E688EFFB540C3276D2E5";
    2.41 +        
    2.42 +    PEP_STATUS status = PEP_STATUS_OK;
    2.43 +
    2.44 +    // key for main own user
    2.45 +    // 
    2.46 +    // 13A9F97964A2B52520CAA40E51BCA783C065A213    
    2.47 +    input_key = slurp("test_keys/pub/priv-key-import-test-main_0-0xC065A213_pub.asc");
    2.48 +    status = import_key(session, input_key.c_str(), input_key.length(), NULL);
    2.49 +    assert(status == PEP_STATUS_OK);
    2.50 +
    2.51 +    input_key = slurp("test_keys/priv/priv-key-import-test-main_0-0xC065A213_priv.asc");
    2.52 +    status = import_key(session, input_key.c_str(), input_key.length(), NULL);
    2.53 +    assert(status == PEP_STATUS_OK);
    2.54 +
    2.55 +    // ensure there's no private key
    2.56 +    bool has_priv = false;
    2.57 +    status = contains_priv_key(session, fpr_same_addr_same_uid, &has_priv);
    2.58 +    if (status == PEP_STATUS_OK && has_priv) {
    2.59 +        cout << "SORRY, have to delete keys here to run test correctly..." << endl;
    2.60 +        status = delete_keypair(session, fpr_same_addr_same_uid);
    2.61 +        if (status == PEP_STATUS_OK)
    2.62 +            cout << "Successfully deleted keypair for " << fpr_same_addr_same_uid << " - will now import the public key only" << endl;
    2.63 +    }
    2.64 +        
    2.65 +    // key with same address and user_id
    2.66 +    // 8AB616A3BD51DEF714B5E688EFFB540C3276D2E5
    2.67 +    input_key = slurp("test_keys/pub/priv-key-import-test-main_0-0x3276D2E5_pub.asc");
    2.68 +    status = import_key(session, input_key.c_str(), input_key.length(), NULL);
    2.69 +    assert(status == PEP_STATUS_OK);
    2.70 +
    2.71 +    
    2.72 +    cout << "Setting up own identity with default key " << fpr_main_me << endl;
    2.73 +    // Own identity with default key etc
    2.74 +    main_me = new_identity(main_addr, fpr_main_me, own_uid, "PrivateKey Import Test");
    2.75 +    status = set_own_key(session, main_me, fpr_main_me);
    2.76 +    assert(status == PEP_STATUS_OK);
    2.77 +
    2.78 +    assert(strcmp(main_me->fpr, fpr_main_me) == 0);
    2.79 +    cout << "Done!" << endl << endl;
    2.80 +    
    2.81 +    cout << "Setting up sender identities and resetting key trust." << endl;
    2.82 +    cout << "Same address, same user_id - address: " << main_addr << ", user_id: " << own_uid << ", fpr: " << fpr_same_addr_same_uid << endl;  
    2.83 +    same_addr_same_uid = new_identity(main_addr, fpr_same_addr_same_uid, own_uid, "PrivateKey Import Test");
    2.84 +    assert(status == PEP_STATUS_OK || status == PEP_CANNOT_FIND_IDENTITY);
    2.85 +    assert((same_addr_same_uid->comm_type & PEP_ct_confirmed) != PEP_ct_confirmed);
    2.86 +
    2.87 +    status = key_reset_trust(session, same_addr_same_uid);
    2.88 +    
    2.89 +    cout << "Done!" << endl << endl;
    2.90 +
    2.91 +    cout << "Reading in message..." << endl;
    2.92 +    
    2.93 +    string encoded_text = slurp("test_mails/priv_key_attach.eml");
    2.94 +
    2.95 +    cout << "Starting tests..." << endl;
    2.96 +    // Case 1:
    2.97 +    // Same address, same user_id, untrusted
    2.98 +    cout << "Same address, same user_id, untrusted" << endl;
    2.99 +    char* decrypted_text = NULL;
   2.100 +    stringlist_t* keylist_used = NULL;
   2.101 +    PEP_rating rating;
   2.102 +    PEP_decrypt_flags_t flags;
   2.103 +    
   2.104 +    status = get_trust(session, same_addr_same_uid);
   2.105 +    cout << tl_ct_string(same_addr_same_uid->comm_type) << endl;
   2.106 +    
   2.107 +    assert((same_addr_same_uid->comm_type & PEP_ct_confirmed) != PEP_ct_confirmed);
   2.108 +    
   2.109 +    status = MIME_decrypt_message(session, encoded_text.c_str(), 
   2.110 +                                  encoded_text.size(), &decrypted_text, 
   2.111 +                                  &keylist_used, &rating, &flags);
   2.112 +
   2.113 +    status = get_trust(session, same_addr_same_uid);
   2.114 +    assert(same_addr_same_uid->comm_type == PEP_ct_pEp_unconfirmed);
   2.115 +
   2.116 +    cout << "Case 1 Status: " << tl_status_string(status) << endl; 
   2.117 +    cout << "Private key is not trusted for " << same_addr_same_uid->fpr << ", as desired, as the public key was not trusted." << endl;
   2.118 +    cout << "PASS!" << endl;
   2.119 +
   2.120 +    // Case 2:
   2.121 +    cout << decrypted_text << endl;
   2.122 +    
   2.123 +    status = key_reset_trust(session, main_me);      
   2.124 +    status = key_reset_trust(session, same_addr_same_uid);      
   2.125 +    release(session);
   2.126 +    
   2.127 +    return 0;
   2.128 +}