test/blacklist_accept_new_key_test.cc
author Volker Birk <vb@pep.foundation>
Tue, 27 Dec 2016 21:13:41 +0100
changeset 1513 e7f7e42385b5
parent 1506 fc7d96185247
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
krista@1343
     4
#include <iostream>
krista@1343
     5
#include <iostream>
krista@1343
     6
#include <fstream>
krista@1343
     7
#include <string>
krista@1343
     8
#include <cstring> // for strcmp()
krista@1343
     9
#include <assert.h>
krista@1343
    10
#include "blacklist.h"
krista@1343
    11
#include "keymanagement.h"
krista@1343
    12
#include "message_api.h"
krista@1343
    13
#include "mime.h"
krista@1343
    14
krista@1343
    15
using namespace std;
krista@1343
    16
krista@1343
    17
int main() {
krista@1343
    18
    cout << "\n*** blacklist_test ***\n\n";
krista@1343
    19
krista@1343
    20
    PEP_SESSION session;
krista@1343
    21
    
krista@1343
    22
    cout << "calling init()\n";
krista@1343
    23
    PEP_STATUS status1 = init(&session);   
krista@1343
    24
    assert(status1 == PEP_STATUS_OK);
krista@1343
    25
    assert(session);
krista@1343
    26
    cout << "init() completed.\n";
krista@1343
    27
krista@1343
    28
    // blacklist test code
krista@1343
    29
krista@1343
    30
    cout << "blacklist only key for identity / add key / check which key is used" << endl;
krista@1343
    31
    
krista@1343
    32
    // 2797 65A2 FEB5 B7C7 31B8  61D9 3E4C EFD9 F7AF 4684 - this is the blacklisted key in blacklisted_pub.asc
krista@1343
    33
krista@1343
    34
    /* read the key into memory */
krista@1343
    35
    ifstream infile("blacklisted_pub.asc");
krista@1343
    36
    string keytext;
krista@1343
    37
    while (!infile.eof()) {
krista@1343
    38
        static string line;
krista@1343
    39
        getline(infile, line);
krista@1343
    40
        keytext += line + "\n";
krista@1343
    41
    }
krista@1343
    42
    infile.close(); 
krista@1343
    43
    
krista@1343
    44
    /* import it into pep */
krista@1343
    45
    PEP_STATUS status7 = import_key(session, keytext.c_str(), keytext.length(), NULL);
krista@1343
    46
    
krista@1343
    47
    const char* bl_fpr_1 = "279765A2FEB5B7C731B861D93E4CEFD9F7AF4684";
krista@1343
    48
    bool is_blacklisted = false;
krista@1343
    49
    
krista@1343
    50
    pEp_identity* blacklisted_identity = new_identity("blacklistedkeys@kgrothoff.org",
krista@1343
    51
                                                      bl_fpr_1,
krista@1343
    52
                                                      NULL,
krista@1343
    53
                                                      "Blacklist Keypair");
krista@1343
    54
    PEP_STATUS status8 = update_identity(session, blacklisted_identity);
krista@1343
    55
    PEP_STATUS status9 = blacklist_add(session, bl_fpr_1);
krista@1343
    56
    PEP_STATUS status10 = blacklist_is_listed(session, bl_fpr_1, &is_blacklisted);
krista@1343
    57
    PEP_STATUS status11 = update_identity(session, blacklisted_identity);
krista@1343
    58
krista@1343
    59
    /* identity is blacklisted. Now let's read in a message which contains a new key for that ID. */
krista@1343
    60
    
krista@1343
    61
    const char* new_key = "634FAC4417E9B2A5DC2BD4AAC4AEEBBE7E62701B";    
krista@1343
    62
    
krista@1343
    63
    ifstream infile2("test_mails/blacklist_new_key_attached.eml");
krista@1343
    64
    string mailtext;
krista@1343
    65
    while (!infile2.eof()) {
krista@1343
    66
        static string line;
krista@1343
    67
        getline(infile2, line);
krista@1343
    68
        mailtext += line + "\n";
krista@1343
    69
    }
krista@1343
    70
    infile2.close(); 
krista@1343
    71
krista@1343
    72
    pEp_identity * me1 = new_identity("blacklist_test@kgrothoff.org", NULL, PEP_OWN_USERID, "Blacklisted Key Message Recipient");    
krista@1343
    73
krista@1343
    74
    PEP_STATUS status = update_identity(session, me1);
krista@1343
    75
    message* msg_ptr = nullptr;
krista@1343
    76
    message* dest_msg = nullptr;
krista@1343
    77
    stringlist_t* keylist = nullptr;
krista@1343
    78
    PEP_rating rating;
krista@1343
    79
    PEP_decrypt_flags_t flags;
krista@1343
    80
    
krista@1343
    81
    status = mime_decode_message(mailtext.c_str(), mailtext.length(), &msg_ptr);
krista@1343
    82
    assert(status == PEP_STATUS_OK);
krista@1343
    83
    status = decrypt_message(session, msg_ptr, &dest_msg, &keylist, &rating, &flags);
krista@1343
    84
krista@1343
    85
    PEP_STATUS status12 = update_identity(session, blacklisted_identity);
krista@1343
    86
krista@1343
    87
    assert(strcasecmp(blacklisted_identity->fpr, new_key) == 0);
krista@1343
    88
krista@1343
    89
    PEP_STATUS status13 = blacklist_delete(session, bl_fpr_1);
krista@1343
    90
    PEP_STATUS status14 = update_identity(session, blacklisted_identity);
krista@1343
    91
edouard@1506
    92
    status = delete_keypair(session, new_key);
edouard@1506
    93
    update_identity(session, blacklisted_identity);
krista@1343
    94
    status = delete_keypair(session, bl_fpr_1);
krista@1343
    95
    update_identity(session, blacklisted_identity);
krista@1343
    96
    
krista@1343
    97
    free_message(msg_ptr);
krista@1343
    98
    free_message(dest_msg);
krista@1343
    99
    free_stringlist(keylist);
krista@1343
   100
    
krista@1343
   101
    cout << "calling release()\n";
krista@1343
   102
    release(session);
krista@1343
   103
    return 0;
krista@1343
   104
}
krista@1343
   105