test/blacklist_test.cc
author Krista Grothoff <krista@pep-project.org>
Thu, 13 Oct 2016 16:22:09 +0200
branchENGINE-112
changeset 1275 39dfd55d21ee
parent 526 b8b302ec9330
child 1341 a54396ccb460
permissions -rw-r--r--
ENGINE-112: #comment Fixed empty fpr problems in stored identities which occurred due to blacklisting. When adding other keys, we now check to see if there is a matching identity entry which lacks an fpr and, if so, we update it instead of adding a new one. Also fixed elect_pubkey NOT to return blacklisted pubkeys.
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@1275
    89
    PEP_STATUS status8 = update_identity(session, blacklisted_identity);
krista@1275
    90
krista@1275
    91
    PEP_STATUS status9 = blacklist_add(session, bl_fpr_1);
krista@1275
    92
    PEP_STATUS status10 = blacklist_is_listed(session, bl_fpr_1, &is_blacklisted);
krista@1275
    93
    PEP_STATUS status11 = update_identity(session, blacklisted_identity);
krista@1275
    94
krista@1275
    95
    /* read the key into memory */
krista@1275
    96
    ifstream infile2("blacklisted_pub2.asc");
krista@1275
    97
    string keytext2;
krista@1275
    98
    while (!infile2.eof()) {
krista@1275
    99
        static string line2;
krista@1275
   100
        getline(infile2, line2);
krista@1275
   101
        keytext2 += line2 + "\n";
krista@1275
   102
    }
krista@1275
   103
    infile2.close(); 
krista@1275
   104
    
krista@1275
   105
    PEP_STATUS status14 = import_key(session, keytext.c_str(), keytext.length(), NULL);
krista@1275
   106
    
krista@1275
   107
    pEp_identity* blacklisted_identity2 = new_identity("blacklistedkeys@kgrothoff.org",
krista@1275
   108
                                                       bl_fpr_2,
krista@1275
   109
                                                        NULL,
krista@1275
   110
                                                       "Blacklist Keypair");
krista@1275
   111
    PEP_STATUS status15 = update_identity(session, blacklisted_identity2);
krista@1275
   112
    PEP_STATUS status12 = blacklist_delete(session, bl_fpr_1);
krista@1275
   113
    PEP_STATUS status13 = update_identity(session, blacklisted_identity);
krista@1275
   114
    
krista@1275
   115
    /* FIXME: remove both keys again from everywhere and clean up identities */
krista@1275
   116
    
fdik@494
   117
    cout << "calling release()\n";
fdik@494
   118
    release(session);
fdik@494
   119
    return 0;
fdik@494
   120
}
fdik@494
   121