test/blacklist_test.cc
author Krista Bennett <krista@pep-project.org>
Fri, 19 Jan 2018 14:12:02 +0100
changeset 2418 05678e77bc6a
parent 2408 489d984ff68f
child 2462 48b526a0daac
permissions -rw-r--r--
fixed update_identity call that could be called on a myself value in outgoing_message_rating
     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     
    69     // 2797 65A2 FEB5 B7C7 31B8  61D9 3E4C EFD9 F7AF 4684 - this is the blacklisted key in blacklisted_pub.asc
    70 
    71     const string keytext = slurp("blacklisted_pub.asc");
    72     
    73     /* FIXME: put in automated test stuff (N.B. only gdb mem examination to this point to get
    74      *        fix in */
    75     /* import it into pep */
    76     PEP_STATUS status7 = import_key(session, keytext.c_str(), keytext.length(), NULL);
    77     
    78     const char* bl_fpr_1 = "279765A2FEB5B7C731B861D93E4CEFD9F7AF4684";
    79     const char* bl_fpr_2 = "634FAC4417E9B2A5DC2BD4AAC4AEEBBE7E62701B"; 
    80     bool is_blacklisted = false;
    81 
    82     // Clean up from previous runs
    83     PEP_STATUS status10 = blacklist_is_listed(session, bl_fpr_1, &is_blacklisted);
    84     if (is_blacklisted) {
    85         is_blacklisted = false;
    86         blacklist_delete(session, bl_fpr_1);
    87     }
    88     
    89     pEp_identity* blacklisted_identity = new_identity("blacklistedkeys@kgrothoff.org",
    90                                                       bl_fpr_1,
    91                                                       NULL,
    92                                                       "Blacklist Keypair");
    93 
    94     PEP_STATUS status8 = update_identity(session, blacklisted_identity);
    95     
    96     // THERE IS NO BLACKLISTING OF PEP KEYS
    97     //blacklisted_identity->comm_type = PEP_ct_pEp;
    98     blacklisted_identity->comm_type = PEP_ct_OpenPGP_unconfirmed;
    99 
   100     PEP_STATUS status99 = set_identity(session, blacklisted_identity);
   101     
   102     trust_personal_key(session, blacklisted_identity);
   103 
   104     PEP_STATUS status999 = update_identity(session, blacklisted_identity);
   105 
   106     assert(blacklisted_identity->comm_type == PEP_ct_OpenPGP);
   107 
   108     PEP_STATUS status9 = blacklist_add(session, bl_fpr_1);
   109     status10 = blacklist_is_listed(session, bl_fpr_1, &is_blacklisted);
   110     PEP_STATUS status11 = update_identity(session, blacklisted_identity);
   111     if (!(blacklisted_identity->fpr))
   112         cout << "OK! blacklisted_identity->fpr is empty. Yay!" << endl;
   113     else if (strcmp(blacklisted_identity->fpr, bl_fpr_2) == 0)
   114         cout << "OK! While this should be empty, you are probably running " << 
   115                 "this in your home directory instead of the test environment " << 
   116                 "and have leftover keys. This is an acceptable result here then. But you " <<
   117                 "should probably clean up after yourself :)" << endl;
   118     else
   119         cout << "Not OK. blacklisted_identity->fpr is " << blacklisted_identity->fpr << "." << endl
   120              << "Expected it to be empty or (possibly) " << bl_fpr_2 << endl;
   121     assert(!(blacklisted_identity->fpr) || blacklisted_identity->fpr[0] == '\0'|| (strcmp(blacklisted_identity->fpr, bl_fpr_2) == 0));
   122 
   123 
   124     const string keytext2 = slurp("blacklisted_pub2.asc");
   125     PEP_STATUS status14 = import_key(session, keytext2.c_str(), keytext2.length(), NULL);
   126     
   127     pEp_identity* blacklisted_identity2 = new_identity("blacklistedkeys@kgrothoff.org",
   128                                                        bl_fpr_2,
   129                                                         NULL,
   130                                                        "Blacklist Keypair");
   131     PEP_STATUS status15 = update_identity(session, blacklisted_identity2);
   132 
   133     assert(blacklisted_identity2->fpr && strcmp(blacklisted_identity2->fpr, bl_fpr_2) == 0);
   134     if (blacklisted_identity2->fpr && strcmp(blacklisted_identity2->fpr, bl_fpr_2) == 0)
   135         cout << "blacklisted identity's fpr successfully replaced by the unblacklisted one" << endl;
   136     // else
   137     //     cout << "blacklisted_identity->fpr should be " << bl_fpr_2 << " but is " << blacklisted_identity->fpr << endl;
   138     
   139     PEP_STATUS status12 = blacklist_delete(session, bl_fpr_1);
   140     PEP_STATUS status13 = update_identity(session, blacklisted_identity);
   141         
   142     pEp_identity* stored_identity = new_identity("blacklistedkeys@kgrothoff.org",
   143                                                   NULL,
   144                                                   blacklisted_identity->user_id,
   145                                                   "Blacklist Keypair");
   146      
   147     PEP_STATUS status00 = update_identity(session, stored_identity);
   148     
   149     // FIXME
   150     // assert(stored_identity->comm_type == PEP_ct_pEp);    
   151     
   152     PEP_STATUS status16 = delete_keypair(session, bl_fpr_1);
   153     update_identity(session, blacklisted_identity);
   154     PEP_STATUS status17 = delete_keypair(session, bl_fpr_2);
   155     update_identity(session, blacklisted_identity2);
   156         
   157     free_identity(blacklisted_identity);
   158     free_identity(blacklisted_identity2);
   159     
   160     cout << "calling release()\n";
   161     release(session);
   162     return 0;
   163 }