test/external_revoke_test.cc
author Krista Bennett <krista@pep-project.org>
Wed, 31 May 2017 17:47:42 +0200
branchENGINE-209
changeset 1811 04d2a7abcfa2
parent 1808 121361c60644
child 1812 925ad845e3ec
permissions -rw-r--r--
ENGINE-209: asserts put into tests. Still have question about PEP_ct_pEp
krista@1808
     1
// This file is under GNU General Public License 3.0
krista@1808
     2
// see LICENSE.txt
krista@1808
     3
krista@1808
     4
#include <stdlib.h>
krista@1808
     5
#include <string.h>
krista@1808
     6
#include <time.h>
krista@1808
     7
#include "platform.h"
krista@1808
     8
#include <iostream>
krista@1808
     9
#include <fstream>
krista@1808
    10
#include <assert.h>
krista@1808
    11
#include "mime.h"
krista@1808
    12
#include "message_api.h"
krista@1808
    13
krista@1808
    14
using namespace std;
krista@1808
    15
krista@1808
    16
int main() {
krista@1808
    17
    cout << "\n*** external_revoke_test.cc ***\n\n";
krista@1808
    18
krista@1808
    19
    PEP_SESSION session;
krista@1808
    20
    
krista@1808
    21
    cout << "calling init()\n";
krista@1808
    22
    PEP_STATUS status = init(&session);   
krista@1808
    23
    assert(status == PEP_STATUS_OK);
krista@1808
    24
    assert(session);
krista@1808
    25
    cout << "init() completed.\n";
krista@1808
    26
krista@1808
    27
#ifndef NETPGP
krista@1808
    28
krista@1808
    29
    // Create sender ID
krista@1808
    30
    
krista@1808
    31
    pEp_identity * me = new_identity("pep.test.apple@pep-project.org", NULL, PEP_OWN_USERID, "Alice Cooper");
krista@1808
    32
    status = update_identity(session, me);
krista@1808
    33
    status = trust_personal_key(session, me);
krista@1808
    34
    status = update_identity(session, me);
krista@1808
    35
    
krista@1808
    36
    // Create key
krista@1808
    37
krista@1808
    38
    cout << "creating new id for : ";
krista@1808
    39
    char *uniqname = strdup("AAAAtestuser@testdomain.org");
krista@1808
    40
    srandom(time(NULL));
krista@1808
    41
    for(int i=0; i < 4;i++)
krista@1808
    42
        uniqname[i] += random() & 0xf;
krista@1808
    43
    
krista@1808
    44
    cout << uniqname << "\n";
krista@1808
    45
    pEp_identity * recip1 = new_identity(uniqname, NULL, NULL, "Test User");
krista@1808
    46
krista@1808
    47
    status = generate_keypair(session, recip1);
krista@1808
    48
    
krista@1808
    49
    cout << "generated fingerprint \n";
krista@1808
    50
    cout << recip1->fpr << "\n";
krista@1808
    51
krista@1808
    52
    // Trust it
krista@1808
    53
    recip1->me = false;
krista@1808
    54
    status = update_identity(session, recip1);
krista@1808
    55
    status = trust_personal_key(session, recip1);
krista@1808
    56
    status = update_identity(session, recip1);
krista@1808
    57
krista@1808
    58
    const char* r1_userid = (recip1->user_id ? strdup(recip1->user_id) : NULL);
krista@1808
    59
krista@1808
    60
    // encrypt something to the key
krista@1808
    61
    cout << "creating message…\n";
krista@1808
    62
    identity_list* to_list = new_identity_list(identity_dup(recip1)); // to bob
krista@1808
    63
    message* outgoing_message = new_message(PEP_dir_outgoing);
krista@1808
    64
    assert(outgoing_message);
krista@1808
    65
    outgoing_message->from = identity_dup(me);
krista@1808
    66
    outgoing_message->to = to_list;
krista@1808
    67
    outgoing_message->shortmsg = strdup("Greetings, humans!");
krista@1808
    68
    outgoing_message->longmsg = strdup("This is a test of the emergency message system. This is only a test. BEEP.");
krista@1808
    69
    outgoing_message->attachments = new_bloblist(NULL, 0, "application/octet-stream", NULL);
krista@1808
    70
    cout << "message created.\n";
krista@1808
    71
krista@1808
    72
    message* encrypted_outgoing_msg = NULL;
krista@1808
    73
    
krista@1808
    74
    status = encrypt_message(session, outgoing_message, NULL, &encrypted_outgoing_msg, PEP_enc_PGP_MIME, 0);
krista@1808
    75
    
krista@1808
    76
    // check status
krista@1808
    77
    assert(status == PEP_STATUS_OK);
krista@1808
    78
    assert(encrypted_outgoing_msg);
krista@1808
    79
krista@1808
    80
    // check comm_type
krista@1808
    81
    cout << "comm_type: " << encrypted_outgoing_msg->to->ident->comm_type << endl;
krista@1808
    82
    assert(encrypted_outgoing_msg->to->ident->comm_type == PEP_ct_OpenPGP);
krista@1811
    83
    
krista@1808
    84
    status = get_trust(session, recip1);
krista@1808
    85
    
krista@1808
    86
    cout << "Recip's trust DB comm_type = " << hex << recip1->comm_type << endl;
krista@1811
    87
    assert(recip1->comm_type == PEP_ct_OpenPGP); // FIXME: PEP_ct_pEp???
krista@1808
    88
krista@1808
    89
    // decrypt message
krista@1808
    90
    free_message(outgoing_message);
krista@1808
    91
    outgoing_message = NULL;
krista@1808
    92
krista@1808
    93
    stringlist_t* keylist = nullptr;
krista@1808
    94
    PEP_rating rating;
krista@1808
    95
    PEP_decrypt_flags_t flags;
krista@1808
    96
krista@1808
    97
    status = decrypt_message(session, encrypted_outgoing_msg, &outgoing_message, &keylist, &rating, &flags);
krista@1808
    98
    assert(status == PEP_STATUS_OK);
krista@1811
    99
    assert(rating == PEP_rating_trusted);
krista@1808
   100
krista@1808
   101
    // check rating
krista@1808
   102
    cout << "Rating of decrypted message to trusted recip: " << rating << endl;
krista@1811
   103
    assert(rating == PEP_rating_trusted); // FIXME: trusted and anonymised?
krista@1811
   104
    
krista@1808
   105
    // check comm_type
krista@1808
   106
    status = get_trust(session, recip1);
krista@1811
   107
krista@1808
   108
    cout << "Recip's trust DB comm_type = " << recip1->comm_type << endl;
krista@1811
   109
    assert(recip1->comm_type == PEP_ct_OpenPGP); // FIXME: PEP_ct_pEp???
krista@1808
   110
krista@1808
   111
    // externally revoke key
krista@1808
   112
    // (note - as of 23.5.17, revoke_key() doesn't touch the trust db, just the keyring, so we can do this)
krista@1808
   113
krista@1808
   114
    status = get_identity(session, uniqname, r1_userid, &recip1);
krista@1808
   115
    
krista@1808
   116
    status = revoke_key(session, recip1->fpr, "encrypt_for_identity_test");
krista@1808
   117
    
krista@1808
   118
    // free messages
krista@1808
   119
    free_message(outgoing_message);
krista@1808
   120
    free_message(encrypted_outgoing_msg);
krista@1808
   121
    outgoing_message = NULL;
krista@1808
   122
    encrypted_outgoing_msg = NULL;
krista@1808
   123
    
krista@1808
   124
    // encrypt something to the key
krista@1808
   125
    cout << "creating message…\n";
krista@1808
   126
    to_list = new_identity_list(identity_dup(recip1)); // to bob
krista@1808
   127
    outgoing_message = new_message(PEP_dir_outgoing);
krista@1808
   128
    assert(outgoing_message);
krista@1808
   129
    outgoing_message->from = identity_dup(me);
krista@1808
   130
    outgoing_message->to = to_list;
krista@1808
   131
    outgoing_message->shortmsg = strdup("Greetings, humans!");
krista@1808
   132
    outgoing_message->longmsg = strdup("This is a test of the emergency message system. This is only a test. BEEP.");
krista@1808
   133
    outgoing_message->attachments = new_bloblist(NULL, 0, "application/octet-stream", NULL);
krista@1808
   134
    cout << "message created.\n";
krista@1808
   135
krista@1808
   136
    encrypted_outgoing_msg = NULL;
krista@1808
   137
    message* decrypted_msg = NULL;
krista@1808
   138
krista@1808
   139
    status = encrypt_message(session, outgoing_message, NULL, &encrypted_outgoing_msg, PEP_enc_PGP_MIME, 0);
krista@1808
   140
krista@1811
   141
    PEP_comm_type ct = (encrypted_outgoing_msg ? encrypted_outgoing_msg->to->ident->comm_type : outgoing_message->to->ident->comm_type);
krista@1811
   142
krista@1808
   143
    // check comm_type
krista@1811
   144
    cout << "comm_type: " << ct << endl;
krista@1811
   145
    assert(ct == PEP_ct_key_revoked);
krista@1811
   146
    
krista@1808
   147
    status = get_trust(session, recip1);
krista@1808
   148
krista@1808
   149
    cout << "Recip's trust DB comm_type = " << hex << recip1->comm_type << endl;
krista@1811
   150
    assert(recip1->comm_type == PEP_ct_key_revoked);
krista@1808
   151
krista@1808
   152
    // decrypt message
krista@1808
   153
//    free_message(outgoing_message);
krista@1808
   154
//    outgoing_message = NULL;
krista@1808
   155
krista@1808
   156
    status = decrypt_message(session, outgoing_message, &decrypted_msg, &keylist, &rating, &flags);
krista@1811
   157
    assert(status == PEP_UNENCRYPTED);
krista@1808
   158
krista@1808
   159
    // check rating
krista@1808
   160
    cout << "Rating of decrypted message to trusted recip: " << rating << endl;
krista@1811
   161
    assert(rating == PEP_rating_unencrypted);
krista@1808
   162
krista@1811
   163
    ct = (decrypted_msg ? decrypted_msg->to->ident->comm_type : outgoing_message->to->ident->comm_type);
krista@1811
   164
krista@1811
   165
    cout << "comm_type: " << ct << endl;
krista@1811
   166
    assert(ct == PEP_ct_key_revoked);
krista@1808
   167
    
krista@1808
   168
    status = get_trust(session, recip1);
krista@1808
   169
    
krista@1808
   170
    cout << "Recip's trust DB comm_type = " << hex << recip1->comm_type << endl;
krista@1811
   171
    assert(recip1->comm_type == PEP_ct_key_revoked);
krista@1808
   172
krista@1808
   173
    // generate new key
krista@1808
   174
    status = generate_keypair(session, recip1);
krista@1808
   175
    
krista@1808
   176
    cout << "generated fingerprint \n";
krista@1808
   177
    cout << recip1->fpr << "\n";
krista@1808
   178
krista@1808
   179
    // PART DEUX
krista@1808
   180
    
krista@1808
   181
    // Create key
krista@1808
   182
krista@1808
   183
    // DO NOT trust it
krista@1808
   184
krista@1808
   185
    // encrypt something to the key
krista@1808
   186
krista@1808
   187
    // check rating
krista@1808
   188
krista@1808
   189
    // check comm_type
krista@1808
   190
krista@1808
   191
    // externally revoke key
krista@1808
   192
    
krista@1808
   193
    // encrypt something to the key
krista@1808
   194
krista@1808
   195
    // check rating
krista@1808
   196
krista@1808
   197
    // check comm_type
krista@1808
   198
krista@1808
   199
krista@1808
   200
    // PART TROIS
krista@1808
   201
    
krista@1808
   202
    // Create key
krista@1808
   203
krista@1808
   204
    // DO NOT trust it
krista@1808
   205
krista@1808
   206
    // encrypt something to the key
krista@1808
   207
krista@1808
   208
    // check rating
krista@1808
   209
krista@1808
   210
    // check comm_type
krista@1808
   211
krista@1808
   212
    // externally revoke key
krista@1808
   213
    
krista@1808
   214
    // encrypt something to the key
krista@1808
   215
krista@1808
   216
    // check rating
krista@1808
   217
krista@1808
   218
    // check comm_type
krista@1808
   219
    
krista@1808
   220
    free(uniqname);
krista@1808
   221
#else
krista@1808
   222
    cout << "Sorry, test is not defined for NETPGP at this time." << endl;
krista@1808
   223
    
krista@1808
   224
#endif
krista@1808
   225
    
krista@1808
   226
    release(session);
krista@1808
   227
krista@1808
   228
    return 0;
krista@1808
   229
}