test/src/engine_tests/TrustManipulationTests.cc
author Neal H. Walfield <neal@pep.foundation>
Sat, 15 Dec 2018 17:00:09 +0100
branchsequoia
changeset 3190 92c7a348b84f
parent 3121 f33a2ae77177
child 3276 c0b3430f1f1d
child 3281 676ee202da9b
permissions -rw-r--r--
Use TEST_ASSERT_MSG to avoid seg faulting on error.
krista@2461
     1
// This file is under GNU General Public License 3.0
krista@2461
     2
// see LICENSE.txt
krista@2461
     3
krista@2461
     4
#include <stdlib.h>
krista@2461
     5
#include <string.h>
krista@2461
     6
#include <time.h>
krista@2461
     7
#include "platform.h"
krista@2461
     8
#include <iostream>
krista@2461
     9
#include <fstream>
krista@2461
    10
#include "mime.h"
krista@2461
    11
#include "message_api.h"
krista@2461
    12
#include "test_util.h"
krista@2461
    13
krista@2650
    14
#include "EngineTestSuite.h"
krista@2653
    15
#include "EngineTestSessionSuite.h"
krista@2650
    16
#include "TrustManipulationTests.h"
krista@2650
    17
krista@2461
    18
using namespace std;
krista@2461
    19
krista@2650
    20
TrustManipulationTests::TrustManipulationTests(string suitename, string test_home_dir) : 
krista@2653
    21
    EngineTestSessionSuite::EngineTestSessionSuite(suitename, test_home_dir) {            
krista@3121
    22
    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("TrustManipulationTests::check_trust_manipulation"),
krista@3121
    23
                                                                      static_cast<Func>(&TrustManipulationTests::check_trust_manipulation)));
krista@2650
    24
}
krista@2650
    25
krista@2650
    26
void TrustManipulationTests::check_trust_manipulation() {
krista@2461
    27
    cout << "\n*** trust manipulation test ***\n\n";
krista@2461
    28
krista@2461
    29
    char* user_id = get_new_uuid();
krista@2461
    30
    
krista@2650
    31
    PEP_STATUS status = PEP_STATUS_OK;
krista@2461
    32
krista@2461
    33
    cout << "creating id for : ";
krista@2461
    34
    char *uniqname = strdup("AAAAtestuser@testdomain.org");
krista@2461
    35
    srandom(time(NULL));
krista@2461
    36
    for(int i=0; i < 4;i++)
krista@2461
    37
        uniqname[i] += random() & 0xf;
krista@2461
    38
    
krista@2461
    39
    cout << uniqname << "\n";
krista@2461
    40
    pEp_identity * user = new_identity(uniqname, NULL, user_id, "Test User");
krista@2461
    41
    status = generate_keypair(session, user);
krista@2669
    42
    TEST_ASSERT_MSG((user->fpr), "user->fpr");
krista@2461
    43
krista@2461
    44
    char* keypair1 = strdup(user->fpr);
krista@2461
    45
    cout << "generated fingerprint \n";
krista@2461
    46
    cout << user->fpr << "\n";
krista@2461
    47
krista@2461
    48
    cout << "Setting key 1 (" << user->fpr << ") as the default for the identity." << endl;
krista@2461
    49
    // Put identity in the DB
krista@2461
    50
    status = set_identity(session, user);
krista@2461
    51
krista@2461
    52
    cout << "creating second keypair for : " << uniqname << endl;
krista@2461
    53
    
krista@2461
    54
    pEp_identity * user_again = new_identity(uniqname, NULL, user_id, "Test User");
krista@2461
    55
    status = generate_keypair(session, user_again);
krista@2669
    56
    TEST_ASSERT_MSG((user_again->fpr), "user_again->fpr");
krista@2461
    57
krista@2461
    58
    char* keypair2 = strdup(user_again->fpr);
krista@2461
    59
    cout << "generated fingerprint \n";
krista@2461
    60
    cout << user_again->fpr << "\n";
krista@2461
    61
krista@2669
    62
    TEST_ASSERT_MSG((strcmp(user->fpr, user_again->fpr) != 0), "strcmp(user->fpr, user_again->fpr) != 0");
krista@2461
    63
    update_identity(session, user);
krista@2669
    64
    TEST_ASSERT_MSG((strcmp(user->fpr, keypair1) == 0), "strcmp(user->fpr, keypair1) == 0");
krista@2461
    65
    cout << "Key 1 (" << user->fpr << ") is still the default for the identity after update_identity." << endl;
krista@2461
    66
krista@2461
    67
    // First, trust the SECOND key; make sure it replaces as the default
krista@2461
    68
    cout << "Set trust bit for key 2 (" << keypair2 << ") and ensure it replaces key 1 as the default." << endl;
krista@2461
    69
    status = trust_personal_key(session, user_again);
krista@2461
    70
    status = update_identity(session, user);
krista@2669
    71
    TEST_ASSERT_MSG((user->comm_type == PEP_ct_OpenPGP), "user->comm_type == PEP_ct_OpenPGP");
krista@2669
    72
    TEST_ASSERT_MSG((strcmp(user->fpr, keypair2) == 0), "strcmp(user->fpr, keypair2) == 0");
krista@2461
    73
    cout << "Key 2 (" << user->fpr << ") is now the default for the identity after update_identity, and its comm_type is PEP_ct_OpenPGP (trust bit set!)." << endl;
krista@2461
    74
krista@2461
    75
    cout << "Now make key 2 not trusted (which also removes it as a default everywhere)." << endl;
krista@2461
    76
    status = key_reset_trust(session, user);
krista@2461
    77
    status = get_trust(session, user);
krista@2669
    78
    TEST_ASSERT_MSG((strcmp(user->fpr, keypair2) == 0), "strcmp(user->fpr, keypair2) == 0");
krista@2669
    79
    TEST_ASSERT_MSG((user->comm_type == PEP_ct_OpenPGP_unconfirmed), "user->comm_type == PEP_ct_OpenPGP_unconfirmed");
krista@2461
    80
    cout << "Key 2 is untrusted in the DB." << endl;
krista@2461
    81
krista@2461
    82
    cout << "Now let's mistrust key 2 in the DB." << endl;
krista@2461
    83
    // Now let's mistrust the second key.
krista@2461
    84
    status = key_mistrusted(session, user);
krista@2461
    85
    status = get_trust(session, user);
krista@2669
    86
    TEST_ASSERT_MSG((strcmp(user->fpr, keypair2) == 0), "strcmp(user->fpr, keypair2) == 0");
krista@2669
    87
    TEST_ASSERT_MSG((user->comm_type == PEP_ct_mistrusted), "user->comm_type == PEP_ct_mistrusted");
krista@2461
    88
    cout << "Hoorah, we now do not trust key 2. (We never liked key 2 anyway.)" << endl;
krista@2461
    89
    cout << "Now we call update_identity to see what gifts it gives us (should be key 1 with key 1's initial trust.)" << endl;    
krista@2461
    90
    status = update_identity(session, user);
neal@3190
    91
    TEST_ASSERT_MSG((user->fpr), "user->fpr");
krista@2669
    92
    TEST_ASSERT_MSG((strcmp(user->fpr, keypair1) == 0), "strcmp(user->fpr, keypair1) == 0");
krista@2669
    93
    TEST_ASSERT_MSG((user->comm_type == PEP_ct_OpenPGP_unconfirmed), "user->comm_type == PEP_ct_OpenPGP_unconfirmed");
krista@2461
    94
    cout << "Yup, got key 1, and the trust status is PEP_ct_OpenPGP_unconfirmed." << endl;
krista@2461
    95
    
krista@2461
    96
    cout << "Let's mistrust key 1 too. It's been acting shifty lately." << endl;
krista@2461
    97
    status = key_mistrusted(session, user);
krista@2461
    98
    status = get_trust(session, user);
krista@2669
    99
    TEST_ASSERT_MSG((strcmp(user->fpr, keypair1) == 0), "strcmp(user->fpr, keypair1) == 0");
krista@2669
   100
    TEST_ASSERT_MSG((user->comm_type == PEP_ct_mistrusted), "user->comm_type == PEP_ct_mistrusted");
krista@2461
   101
    cout << "Hoorah, we now do not trust key 1. (TRUST NO ONE)" << endl;
krista@2461
   102
    cout << "Now we call update_identity to see what gifts it gives us (should be an empty key and a key not found comm_type.)" << endl;    
krista@2461
   103
    status = update_identity(session, user);
krista@2669
   104
    TEST_ASSERT_MSG((user->fpr == NULL), "user->fpr == NULL");
krista@2669
   105
    TEST_ASSERT_MSG((user->comm_type == PEP_ct_key_not_found), "user->comm_type == PEP_ct_key_not_found");
krista@2461
   106
    cout << "Yup, we trust no keys from " << uniqname << endl;
krista@2461
   107
    
krista@2461
   108
    cout << "TODO: Add cases where we have multiple user_ids addressing a single key, and multiple identities with that key + mistrust" << endl;
krista@2461
   109
    cout << "Passed all of our exciting messing with the trust DB. Moving on..." << endl;
krista@2461
   110
 
krista@2461
   111
    free(user_id);
krista@2461
   112
    free(keypair1);
krista@2461
   113
    free(uniqname);
krista@2461
   114
    free_identity(user);
krista@2461
   115
}