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