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