test/decrypt_attach_private_key_trusted_test.cc
author Krista Bennett <krista@pep-project.org>
Thu, 19 Apr 2018 16:03:44 +0200
branchENGINE-423
changeset 2615 5cd3625e0d91
parent 2603 a6a06822ec6b
child 2623 009950d133c1
permissions -rw-r--r--
ENGINE-423: normal tests pass. Now to test reencrypt functionality.
     1 // This file is under GNU General Public License 3.0
     2 // see LICENSE.txt
     3 
     4 #include <stdlib.h>
     5 #include <string.h>
     6 #include "platform.h"
     7 #include <iostream>
     8 #include <fstream>
     9 #include <assert.h>
    10 #include "mime.h"
    11 #include "message_api.h"
    12 #include "keymanagement.h"
    13 #include "test_util.h"
    14 
    15 using namespace std;
    16 
    17 int main() {
    18     cout << "\n*** decrypt_attach_private_key_trusted_test ***\n\n";
    19 
    20     PEP_SESSION session;
    21     
    22     cout << "calling init()\n";
    23     PEP_STATUS status1 = init(&session);
    24     assert(status1 == PEP_STATUS_OK);
    25     assert(session);
    26     cout << "init() completed.\n";
    27 
    28     const char* own_uid = PEP_OWN_USERID;
    29 
    30     cout << "Importing keys..." << endl;
    31     
    32     string input_key;
    33     const char* main_addr = "priv-key-import-test-main@darthmama.cool";
    34     pEp_identity* main_me = NULL;
    35     const char* fpr_main_me = "13A9F97964A2B52520CAA40E51BCA783C065A213";    
    36     pEp_identity* same_addr_same_uid = NULL;
    37     const char* fpr_same_addr_same_uid = "8AB616A3BD51DEF714B5E688EFFB540C3276D2E5";
    38         
    39     PEP_STATUS status = PEP_STATUS_OK;
    40 
    41     // key for main own user
    42     // 
    43     // 13A9F97964A2B52520CAA40E51BCA783C065A213    
    44     input_key = slurp("test_keys/pub/priv-key-import-test-main_0-0xC065A213_pub.asc");
    45     status = import_key(session, input_key.c_str(), input_key.length(), NULL);
    46     assert(status == PEP_STATUS_OK);
    47 
    48     input_key = slurp("test_keys/priv/priv-key-import-test-main_0-0xC065A213_priv.asc");
    49     status = import_key(session, input_key.c_str(), input_key.length(), NULL);
    50     assert(status == PEP_STATUS_OK);
    51 
    52     // ensure there's no private key - doesn't work in automated tests, sadly. Uncommon when running script manually.
    53     bool has_priv = false;
    54     // status = contains_priv_key(session, fpr_same_addr_same_uid, &has_priv);
    55     // if (status == PEP_STATUS_OK && has_priv) {
    56     //     cout << "SORRY, have to delete keys here to run test correctly..." << endl;
    57     //     status = delete_keypair(session, fpr_same_addr_same_uid);
    58     //     if (status == PEP_STATUS_OK) {
    59     //         has_priv = false;
    60     //         status = contains_priv_key(session, fpr_same_addr_same_uid, &has_priv);
    61     //         assert(has_priv == false);
    62     //         cout << "Successfully deleted keypair for " << fpr_same_addr_same_uid << " - will now import the public key only" << endl;
    63     //     }
    64     //     else
    65     //         cout << "Warning - delete keypair returned status " << tl_status_string(status) << ". This may or may not be an error, depending on what you expect." << endl;            
    66     // }
    67         
    68     // key with same address and user_id
    69     // 8AB616A3BD51DEF714B5E688EFFB540C3276D2E5
    70     input_key = slurp("test_keys/pub/priv-key-import-test-main_0-0x3276D2E5_pub.asc");
    71     status = import_key(session, input_key.c_str(), input_key.length(), NULL);
    72     assert(status == PEP_STATUS_OK);
    73 
    74     
    75     cout << "Setting up own identity with default key " << fpr_main_me << endl;
    76     // Own identity with default key etc
    77     main_me = new_identity(main_addr, fpr_main_me, own_uid, "PrivateKey Import Test");
    78     status = set_own_key(session, main_me, fpr_main_me);
    79     assert(status == PEP_STATUS_OK);
    80 
    81     assert(strcmp(main_me->fpr, fpr_main_me) == 0);
    82     cout << "Done!" << endl << endl;
    83     
    84     cout << "Setting up sender identities and resetting key trust." << endl;
    85     cout << "Same address, same user_id - address: " << main_addr << ", user_id: " << own_uid << ", fpr: " << fpr_same_addr_same_uid << endl;  
    86     same_addr_same_uid = new_identity(main_addr, fpr_same_addr_same_uid, own_uid, "PrivateKey Import Test");
    87     assert(status == PEP_STATUS_OK || status == PEP_CANNOT_FIND_IDENTITY);
    88     assert((same_addr_same_uid->comm_type & PEP_ct_confirmed) != PEP_ct_confirmed);
    89 
    90     status = key_reset_trust(session, same_addr_same_uid);
    91     
    92     cout << "Done!" << endl << endl;
    93 
    94     cout << "Reading in message..." << endl;
    95     
    96     string encoded_text = slurp("test_mails/priv_key_attach.eml");
    97 
    98     cout << "Starting test..." << endl;
    99     // Case 1:
   100     // Same address, same user_id, untrusted
   101     cout << "decrypt with attached private key: Same address, same user_id, trusted" << endl;
   102     char* decrypted_text = NULL;
   103     stringlist_t* keylist_used = NULL;
   104     PEP_rating rating;
   105     PEP_decrypt_flags_t flags = 0;
   106     
   107     cout << "Trusting personal key for " << same_addr_same_uid->user_id << " and " << same_addr_same_uid->fpr << endl;
   108     status = trust_personal_key(session, same_addr_same_uid);
   109     cout << "Status is " << tl_status_string(status) << endl;  
   110     assert(status == PEP_STATUS_OK);
   111     free(decrypted_text);
   112     decrypted_text = NULL;
   113 
   114     status = get_trust(session, same_addr_same_uid);
   115     cout << tl_ct_string(same_addr_same_uid->comm_type) << endl;
   116     
   117     assert(same_addr_same_uid->comm_type == PEP_ct_pEp);
   118     
   119     flags = 0;
   120     status = MIME_decrypt_message(session, encoded_text.c_str(), 
   121                                   encoded_text.size(), &decrypted_text, 
   122                                   &keylist_used, &rating, &flags);
   123 
   124     status = get_trust(session, same_addr_same_uid);
   125     assert(same_addr_same_uid->comm_type == PEP_ct_pEp);
   126     
   127     flags = 0;
   128     status = MIME_decrypt_message(session, encoded_text.c_str(), 
   129                                   encoded_text.size(), &decrypted_text, 
   130                                   &keylist_used, &rating, &flags);
   131     
   132     cout << "Status: " << tl_status_string(status) << endl;
   133     assert(status == PEP_STATUS_OK);
   134 
   135     cout << decrypted_text << endl;
   136     
   137     has_priv = false;
   138     status = contains_priv_key(session, fpr_same_addr_same_uid, &has_priv);
   139     assert(has_priv == true);
   140     cout << "Private key was also imported." << endl;
   141     
   142     cout << "PASS!" << endl;
   143     status = key_reset_trust(session, main_me);      
   144     status = key_reset_trust(session, same_addr_same_uid);      
   145     release(session);
   146     
   147     return 0;
   148 }