test/src/engine_tests/MistrustUndoTests.cc
author Krista 'DarthMama' Bennett <krista@pep.foundation>
Tue, 29 Jan 2019 19:19:30 +0100
branchENGINE-448
changeset 3254 6e7f6bc9460a
parent 2669 1b103e8a95f7
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
     1 // This file is under GNU General Public License 3.0
     2 // see LICENSE.txt
     3 
     4 #include <stdlib.h>
     5 #include <string>
     6 #include <cstring>
     7 #include <time.h>
     8 #include <iostream>
     9 #include <fstream>
    10 
    11 #include "pEpEngine.h"
    12 #include "platform.h"
    13 #include "mime.h"
    14 #include "message_api.h"
    15 #include "test_util.h"
    16 
    17 #include <cpptest.h>
    18 #include "EngineTestSessionSuite.h"
    19 #include "MistrustUndoTests.h"
    20 
    21 using namespace std;
    22 
    23 MistrustUndoTests::MistrustUndoTests(string suitename, string test_home_dir) :
    24     EngineTestSessionSuite::EngineTestSessionSuite(suitename, test_home_dir) {
    25     add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("MistrustUndoTests::check_mistrust_undo"),
    26                                                                       static_cast<Func>(&MistrustUndoTests::check_mistrust_undo)));
    27 }
    28 
    29 void MistrustUndoTests::check_mistrust_undo() {
    30     PEP_STATUS status = PEP_STATUS_OK;
    31 
    32     cout << "importing key 0x39E5DAB5." << endl;
    33     const string pub_key = slurp("test_keys/pub/mistrust.undo.test-0x39E5DAB5_pub.asc");
    34 
    35     TEST_ASSERT_MSG((pub_key.length() != 0), "pub_key.length() != 0");
    36     
    37     PEP_STATUS statuspub = import_key(session, pub_key.c_str(), pub_key.length(), NULL);
    38     TEST_ASSERT_MSG((statuspub == PEP_KEY_IMPORTED), "statuspub == PEP_STATUS_OK");
    39     cout << "Key imported." << endl << endl;
    40     
    41     cout << "Setting up identity for mistrust.undo.test@pep-project.org and making comm_type PEP_ct_pEp."  << endl;
    42     pEp_identity* recip1 = new_identity("mistrust.undo.test@pep-project.org", NULL, "TOFU_mistrust.undo.test@pep-project.org", "Mistrust Undo");
    43     status = update_identity(session,recip1);
    44     TEST_ASSERT_MSG((status == PEP_STATUS_OK), "status == PEP_STATUS_OK");
    45     TEST_ASSERT_MSG((strcmp(recip1->fpr, "BACC7A60A88A39A25D99B4A545D7542F39E5DAB5") == 0), "strcmp(recip1->fpr, \"BACC7A60A88A39A25D99B4A545D7542F39E5DAB5\") == 0");
    46     
    47     // First, we need the fpr to be in the DB system.
    48     status = set_identity(session,recip1);
    49     // Then we update the trust.
    50     // This is not an external function. We use it to expedite the test since we don't do a sync exchange here.
    51     status = update_trust_for_fpr(session, recip1->fpr, PEP_ct_pEp);
    52     // Then we retrieve the new trust.
    53     status = update_identity(session,recip1);
    54     TEST_ASSERT_MSG((status == PEP_STATUS_OK), "status == PEP_STATUS_OK");
    55     TEST_ASSERT_MSG((recip1->comm_type == PEP_ct_pEp), "recip1->comm_type == PEP_ct_pEp");
    56     TEST_ASSERT_MSG((strcmp(recip1->fpr, "BACC7A60A88A39A25D99B4A545D7542F39E5DAB5") == 0), "strcmp(recip1->fpr, \"BACC7A60A88A39A25D99B4A545D7542F39E5DAB5\") == 0");
    57     cout << "mistrust.undo.test@pep-project.org set up and comm_type is PEP_ct_pEp."  << endl << endl;
    58 
    59     // Ok, mistrust away
    60     cout << "Mistrusting mistrust.undo.test@pep-project.org (BACC7A60A88A39A25D99B4A545D7542F39E5DAB5)."  << endl;   
    61     status = key_mistrusted(session, recip1);
    62     TEST_ASSERT_MSG((status == PEP_STATUS_OK), "status == PEP_STATUS_OK");
    63     status = update_identity(session,recip1);
    64     TEST_ASSERT_MSG((status == PEP_STATUS_OK), "status == PEP_STATUS_OK");
    65     TEST_ASSERT_MSG((recip1->comm_type == PEP_ct_key_not_found), "recip1->comm_type == PEP_ct_key_not_found");
    66     recip1->fpr = strdup("BACC7A60A88A39A25D99B4A545D7542F39E5DAB5");
    67     status = get_trust(session, recip1);
    68     TEST_ASSERT_MSG((recip1->comm_type == PEP_ct_mistrusted), "recip1->comm_type == PEP_ct_mistrusted");
    69      
    70     cout << "Mistrusted mistrust.undo.test@pep-project.org (BACC7A60A88A39A25D99B4A545D7542F39E5DAB5) and comm_type IN DB set to PEP_ct_mistrusted)." << endl  << endl;    
    71     
    72     cout << "Undo mistrust (restore identity and trust in DB)" << endl;
    73     // Undo it
    74     status = undo_last_mistrust(session);
    75     TEST_ASSERT_MSG((status == PEP_STATUS_OK), "status == PEP_STATUS_OK");
    76     status = update_identity(session, recip1);
    77     TEST_ASSERT_MSG((recip1->comm_type == PEP_ct_pEp), "recip1->comm_type == PEP_ct_pEp");
    78     TEST_ASSERT_MSG((strcmp(recip1->fpr, "BACC7A60A88A39A25D99B4A545D7542F39E5DAB5") == 0), "strcmp(recip1->fpr, \"BACC7A60A88A39A25D99B4A545D7542F39E5DAB5\") == 0");
    79     cout << "Undo mistrust (restore identity and trust in DB) - trust is now PEP_ct_pEp." << endl << endl;
    80 
    81     cout << "Success!!!" << endl << endl;
    82     
    83     free_identity(recip1);
    84 }