test/blacklist_test.cc
author Krista Grothoff <krista@pep-project.org>
Mon, 28 Nov 2016 17:48:05 +0100
branchENGINE-112
changeset 1449 c4874b0a2501
parent 1343 59f014327d4a
child 1503 0f86dbd30df1
permissions -rw-r--r--
ENGINE-112: updated get_trust in update_identity and stopped set_identity from setting empty fprs as keypairs and trust entries
fdik@494
     1
#include <iostream>
krista@1275
     2
#include <iostream>
krista@1275
     3
#include <fstream>
fdik@494
     4
#include <string>
roker@526
     5
#include <cstring> // for strcmp()
fdik@494
     6
#include <assert.h>
fdik@494
     7
#include "blacklist.h"
krista@1275
     8
#include "keymanagement.h"
fdik@494
     9
fdik@494
    10
using namespace std;
fdik@494
    11
fdik@494
    12
int main() {
fdik@494
    13
    cout << "\n*** blacklist_test ***\n\n";
fdik@494
    14
fdik@494
    15
    PEP_SESSION session;
fdik@494
    16
    
fdik@494
    17
    cout << "calling init()\n";
fdik@494
    18
    PEP_STATUS status1 = init(&session);   
fdik@494
    19
    assert(status1 == PEP_STATUS_OK);
fdik@494
    20
    assert(session);
fdik@494
    21
    cout << "init() completed.\n";
fdik@494
    22
fdik@494
    23
    // blacklist test code
fdik@494
    24
fdik@494
    25
    cout << "adding 23 to blacklist\n";
fdik@494
    26
    PEP_STATUS status2 = blacklist_add(session, "23");
fdik@494
    27
    assert(status2 == PEP_STATUS_OK);
fdik@494
    28
    cout << "added.\n";
fdik@494
    29
fdik@494
    30
    bool listed;
fdik@494
    31
    PEP_STATUS status3 = blacklist_is_listed(session, "23", &listed);
fdik@494
    32
    assert(status3 == PEP_STATUS_OK);
fdik@494
    33
    assert(listed);
fdik@494
    34
    cout << "23 is listed.\n";
fdik@494
    35
fdik@494
    36
    stringlist_t *blacklist;
fdik@494
    37
    PEP_STATUS status6 = blacklist_retrieve(session, &blacklist);
fdik@494
    38
    assert(status6 == PEP_STATUS_OK);
fdik@494
    39
    assert(blacklist);
fdik@494
    40
fdik@494
    41
    bool in23 = false;
fdik@494
    42
    cout << "the blacklist contains now: ";
fdik@494
    43
    for (stringlist_t *bl = blacklist; bl && bl->value; bl = bl->next) {
fdik@494
    44
        cout << bl->value << ", ";
roker@526
    45
        if (std::strcmp(bl->value, "23") == 0)
fdik@494
    46
            in23 = true;
fdik@494
    47
    }
fdik@494
    48
    cout << "END\n";
fdik@494
    49
    assert(in23);
fdik@494
    50
    free_stringlist(blacklist);
fdik@494
    51
fdik@494
    52
    cout << "deleting 23 from blacklist\n";
fdik@494
    53
    PEP_STATUS status4 = blacklist_delete(session, "23");
fdik@494
    54
    assert(status4 == PEP_STATUS_OK);
fdik@494
    55
    cout << "deleted.\n";
fdik@494
    56
    
fdik@494
    57
    PEP_STATUS status5 = blacklist_is_listed(session, "23", &listed);
fdik@494
    58
    assert(status5 == PEP_STATUS_OK);
fdik@494
    59
    assert(!listed);
fdik@494
    60
    cout << "23 is not listed any more.\n";
fdik@494
    61
krista@1275
    62
    cout << "blacklist only key for identity / unblacklist key / add key" << endl;
krista@1275
    63
    
krista@1275
    64
    // 2797 65A2 FEB5 B7C7 31B8  61D9 3E4C EFD9 F7AF 4684 - this is the blacklisted key in blacklisted_pub.asc
krista@1275
    65
krista@1275
    66
    /* read the key into memory */
krista@1275
    67
    ifstream infile("blacklisted_pub.asc");
krista@1275
    68
    string keytext;
krista@1275
    69
    while (!infile.eof()) {
krista@1275
    70
        static string line;
krista@1275
    71
        getline(infile, line);
krista@1275
    72
        keytext += line + "\n";
krista@1275
    73
    }
krista@1275
    74
    infile.close(); 
krista@1275
    75
    
krista@1275
    76
    /* FIXME: put in automated test stuff (N.B. only gdb mem examination to this point to get
krista@1275
    77
     *        fix in */
krista@1275
    78
    /* import it into pep */
krista@1275
    79
    PEP_STATUS status7 = import_key(session, keytext.c_str(), keytext.length(), NULL);
krista@1275
    80
    
krista@1275
    81
    const char* bl_fpr_1 = "279765A2FEB5B7C731B861D93E4CEFD9F7AF4684";
krista@1275
    82
    const char* bl_fpr_2 = "634FAC4417E9B2A5DC2BD4AAC4AEEBBE7E62701B";
krista@1275
    83
    bool is_blacklisted = false;
krista@1275
    84
    
krista@1275
    85
    pEp_identity* blacklisted_identity = new_identity("blacklistedkeys@kgrothoff.org",
krista@1275
    86
                                                      bl_fpr_1,
krista@1275
    87
                                                      NULL,
krista@1275
    88
                                                      "Blacklist Keypair");
krista@1449
    89
krista@1275
    90
    PEP_STATUS status8 = update_identity(session, blacklisted_identity);
krista@1449
    91
    
krista@1449
    92
    blacklisted_identity->comm_type = PEP_ct_pEp;
krista@1449
    93
krista@1449
    94
    PEP_STATUS status99 = set_identity(session, blacklisted_identity);
krista@1449
    95
        
krista@1449
    96
    trust_personal_key(session, blacklisted_identity);
krista@1449
    97
krista@1449
    98
    PEP_STATUS status999 = update_identity(session, blacklisted_identity);
krista@1449
    99
krista@1449
   100
    assert(blacklisted_identity->comm_type == PEP_ct_pEp);
krista@1275
   101
krista@1275
   102
    PEP_STATUS status9 = blacklist_add(session, bl_fpr_1);
krista@1275
   103
    PEP_STATUS status10 = blacklist_is_listed(session, bl_fpr_1, &is_blacklisted);
krista@1275
   104
    PEP_STATUS status11 = update_identity(session, blacklisted_identity);
krista@1275
   105
krista@1449
   106
    if (strcmp(blacklisted_identity->fpr, ""))
krista@1449
   107
        cout << "blacklisted_identity-> fpr should be empty, but is " << blacklisted_identity->fpr << endl;
krista@1449
   108
    else
krista@1449
   109
        cout << "blacklisted identity's fpr successfully wiped by update_identity" << endl;
krista@1449
   110
krista@1275
   111
    /* read the key into memory */
krista@1275
   112
    ifstream infile2("blacklisted_pub2.asc");
krista@1275
   113
    string keytext2;
krista@1275
   114
    while (!infile2.eof()) {
krista@1275
   115
        static string line2;
krista@1275
   116
        getline(infile2, line2);
krista@1275
   117
        keytext2 += line2 + "\n";
krista@1275
   118
    }
krista@1275
   119
    infile2.close(); 
krista@1275
   120
    
krista@1275
   121
    PEP_STATUS status14 = import_key(session, keytext.c_str(), keytext.length(), NULL);
krista@1275
   122
    
krista@1275
   123
    pEp_identity* blacklisted_identity2 = new_identity("blacklistedkeys@kgrothoff.org",
krista@1275
   124
                                                       bl_fpr_2,
krista@1275
   125
                                                        NULL,
krista@1275
   126
                                                       "Blacklist Keypair");
krista@1275
   127
    PEP_STATUS status15 = update_identity(session, blacklisted_identity2);
krista@1275
   128
    PEP_STATUS status12 = blacklist_delete(session, bl_fpr_1);
krista@1275
   129
    PEP_STATUS status13 = update_identity(session, blacklisted_identity);
krista@1449
   130
krista@1449
   131
    if (strcmp(blacklisted_identity->fpr, bl_fpr_2))
krista@1449
   132
        cout << "blacklisted identity's fpr successfully replaced by the unblacklisted one" << endl;
krista@1449
   133
    else
krista@1449
   134
        cout << "blacklisted_identity->fpr should be " << bl_fpr_2 << " but is " << blacklisted_identity->fpr << endl;
krista@1343
   135
            
krista@1449
   136
    pEp_identity* stored_identity = new_identity("blacklistedkeys@kgrothoff.org",
krista@1449
   137
                                                  NULL,
krista@1449
   138
                                                  blacklisted_identity->user_id,
krista@1449
   139
                                                  "Blacklist Keypair");
krista@1449
   140
     
krista@1449
   141
    PEP_STATUS status00 = update_identity(session, stored_identity);
krista@1449
   142
    
krista@1449
   143
    // Following should be true because bl_fpr_1's trust is set higher
krista@1449
   144
    assert(stored_identity->comm_type == PEP_ct_pEp && (strcmp(stored_identity->fpr, bl_fpr_1) == 0));    
krista@1449
   145
    
krista@1343
   146
    PEP_STATUS status16 = delete_keypair(session, bl_fpr_1);
krista@1343
   147
    update_identity(session, blacklisted_identity);
krista@1343
   148
    PEP_STATUS status17 = delete_keypair(session, bl_fpr_2);
krista@1343
   149
    update_identity(session, blacklisted_identity2);
krista@1343
   150
        
krista@1341
   151
    free_identity(blacklisted_identity);
krista@1341
   152
    free_identity(blacklisted_identity2);
krista@1341
   153
    
fdik@494
   154
    cout << "calling release()\n";
fdik@494
   155
    release(session);
fdik@494
   156
    return 0;
fdik@494
   157
}