test/decrypt_attach_private_key_untrusted_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_untrusted_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     //         cout << "Successfully deleted keypair for " << fpr_same_addr_same_uid << " - will now import the public key only" << endl;
    60     // }
    61         
    62     // key with same address and user_id
    63     // 8AB616A3BD51DEF714B5E688EFFB540C3276D2E5
    64     input_key = slurp("test_keys/pub/priv-key-import-test-main_0-0x3276D2E5_pub.asc");
    65     status = import_key(session, input_key.c_str(), input_key.length(), NULL);
    66     assert(status == PEP_STATUS_OK);
    67 
    68     
    69     cout << "Setting up own identity with default key " << fpr_main_me << endl;
    70     // Own identity with default key etc
    71     main_me = new_identity(main_addr, fpr_main_me, own_uid, "PrivateKey Import Test");
    72     status = set_own_key(session, main_me, fpr_main_me);
    73     assert(status == PEP_STATUS_OK);
    74 
    75     assert(strcmp(main_me->fpr, fpr_main_me) == 0);
    76     cout << "Done!" << endl << endl;
    77     
    78     cout << "Setting up sender identities and resetting key trust." << endl;
    79     cout << "Same address, same user_id - address: " << main_addr << ", user_id: " << own_uid << ", fpr: " << fpr_same_addr_same_uid << endl;  
    80     same_addr_same_uid = new_identity(main_addr, fpr_same_addr_same_uid, own_uid, "PrivateKey Import Test");
    81     assert(status == PEP_STATUS_OK || status == PEP_CANNOT_FIND_IDENTITY);
    82     assert((same_addr_same_uid->comm_type & PEP_ct_confirmed) != PEP_ct_confirmed);
    83 
    84     status = key_reset_trust(session, same_addr_same_uid);
    85     
    86     cout << "Done!" << endl << endl;
    87 
    88     cout << "Reading in message..." << endl;
    89     
    90     string encoded_text = slurp("test_mails/priv_key_attach.eml");
    91 
    92     cout << "Starting tests..." << endl;
    93     // Case 1:
    94     // Same address, same user_id, untrusted
    95     cout << "Same address, same user_id, untrusted" << endl;
    96     char* decrypted_text = NULL;
    97     stringlist_t* keylist_used = NULL;
    98     PEP_rating rating;
    99     PEP_decrypt_flags_t flags;
   100     
   101     status = get_trust(session, same_addr_same_uid);
   102     cout << tl_ct_string(same_addr_same_uid->comm_type) << endl;
   103     
   104     assert((same_addr_same_uid->comm_type & PEP_ct_confirmed) != PEP_ct_confirmed);
   105     
   106     flags = 0;
   107     status = MIME_decrypt_message(session, encoded_text.c_str(), 
   108                                   encoded_text.size(), &decrypted_text, 
   109                                   &keylist_used, &rating, &flags);
   110 
   111     status = get_trust(session, same_addr_same_uid);
   112     assert(same_addr_same_uid->comm_type == PEP_ct_pEp_unconfirmed);
   113 
   114     cout << "Case 1 Status: " << tl_status_string(status) << endl; 
   115     cout << "Private key is not trusted for " << same_addr_same_uid->fpr << ", as desired, as the public key was not trusted." << endl;
   116     cout << "PASS!" << endl;
   117 
   118     // Case 2:
   119     cout << decrypted_text << endl;
   120     
   121     status = key_reset_trust(session, main_me);      
   122     status = key_reset_trust(session, same_addr_same_uid);      
   123     release(session);
   124     
   125     return 0;
   126 }