test/pEpEngineTest.cc
author Roker <roker@pep-project.org>
Tue, 21 Jun 2016 17:52:03 +0200
changeset 751 78a762326f20
parent 750 56df4e0f3ab9
child 752 9c53a8bc555d
permissions -rw-r--r--
use C++, not C in pEpEngineTest, and avoid manual memory management
roker@748
     1
#include <iostream>
roker@748
     2
#include <fstream>
roker@751
     3
#include <stdexcept>
roker@748
     4
#include <string>
roker@751
     5
#include <vector>
roker@748
     6
roker@748
     7
#include <assert.h>
roker@748
     8
#include <string.h>
roker@748
     9
roker@748
    10
#include "../src/pEpEngine.h"
roker@748
    11
#include "../src/keymanagement.h"
roker@748
    12
roker@751
    13
roker@748
    14
using namespace std;
roker@748
    15
roker@751
    16
typedef std::vector<char> Buffer;
roker@751
    17
roker@751
    18
// no C++11, yet? So do our own implementation:
roker@751
    19
namespace{
roker@751
    20
    std::string to_string(unsigned long u)
roker@751
    21
    {
roker@751
    22
        char buf[32];
roker@751
    23
        snprintf(buf,31, "%lu", u);
roker@751
    24
        return buf;
roker@751
    25
    }
roker@751
    26
}
roker@751
    27
roker@751
    28
roker@751
    29
Buffer ReadFileIntoMem(const char *fname){
roker@750
    30
    cout << "opening " << fname << " for reading\n";
roker@750
    31
    ifstream txtFile (fname, ifstream::binary);
roker@750
    32
    assert(txtFile.is_open());
roker@751
    33
    if (!txtFile)
roker@751
    34
    {
roker@751
    35
        throw std::runtime_error( "error: cannot open file \"" + std::string(fname) + "\"" );
roker@748
    36
    }
roker@748
    37
roker@751
    38
    Buffer buffer;
roker@751
    39
roker@751
    40
    // get length of file:
roker@751
    41
    txtFile.seekg (0, txtFile.end);
roker@751
    42
    const size_t length = txtFile.tellg();
roker@751
    43
    txtFile.seekg (0, txtFile.beg);
roker@751
    44
    buffer.resize(length+1);
roker@751
    45
roker@751
    46
    cout << "Reading " << length << " characters... ";
roker@751
    47
    txtFile.read (buffer.data(), length);
roker@751
    48
    buffer.at(length)='\0';
roker@751
    49
roker@751
    50
    if (!txtFile)
roker@751
    51
    {
roker@751
    52
        throw std::runtime_error( "error: only " + to_string(txtFile.gcount()) + " could be read from file" + fname );
roker@751
    53
    }
roker@751
    54
roker@751
    55
    cout << "all characters read successfully." << std::endl;
roker@751
    56
    return buffer;
roker@748
    57
}
roker@748
    58
roker@750
    59
roker@748
    60
int main(int argc, char* argv[])
roker@748
    61
{
roker@750
    62
    PEP_SESSION session;
roker@748
    63
roker@750
    64
    cout << "calling init()\n";
roker@750
    65
    PEP_STATUS init_result = init(&session);
roker@750
    66
    
roker@748
    67
    cout << "returning from init() with result == " << init_result << "\n";
roker@750
    68
    assert(init_result == PEP_STATUS_OK);
roker@748
    69
roker@748
    70
    PEP_SESSION second_session;
roker@748
    71
    cout << "second session test\n";
roker@748
    72
    PEP_STATUS second_init_result = init(&second_session);
roker@750
    73
    cout << "returning from second init() with result == " << second_init_result << "\n";
roker@748
    74
    assert(second_init_result == PEP_STATUS_OK);
roker@748
    75
    assert(second_session);
roker@748
    76
    cout << "dropping second session\n";
roker@750
    77
    release(second_session);
roker@748
    78
roker@750
    79
    cout << "logging test\n";
roker@750
    80
    log_event(session, "log test", "pEp Enginge Test", "This is a logging test sample.", "please ignore this line");
roker@748
    81
roker@748
    82
    // Our test user :
roker@748
    83
    // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
roker@748
    84
    //         6FF00E97
roker@748
    85
    // A9411D176FF00E97
roker@748
    86
    // 
roker@748
    87
    // Other peers :
roker@748
    88
    // pEp Test Bob (test key, don't use) <pep.test.bob@pep-project.org> 
roker@748
    89
    //         C9C2EE39
roker@748
    90
    // 59BFF488C9C2EE39
roker@748
    91
    // 
roker@748
    92
    // pEp Test John (test key, don't use) <pep.test.john@pep-project.org>
roker@748
    93
    //         70DCF575
roker@748
    94
    // 135CD6D170DCF575
roker@748
    95
roker@748
    96
    const char *kflist[] = {
roker@748
    97
        "0x6FF00E97.asc",
roker@748
    98
        "0xC9C2EE39.asc",
roker@748
    99
        "0x70DCF575.asc",
roker@748
   100
        NULL
roker@748
   101
    };
roker@750
   102
roker@748
   103
    const char** kf = kflist;
roker@748
   104
    while(*kf){
roker@751
   105
        const Buffer k_user_buffer =  ReadFileIntoMem(*kf);
roker@751
   106
        const size_t k_user_length = k_user_buffer.size();
roker@748
   107
        cout << "import_key(" << *kf << ")\n";
roker@751
   108
        PEP_STATUS import_status = import_key(session, k_user_buffer.data(), k_user_length, NULL);
roker@748
   109
        assert(import_status == PEP_STATUS_OK);
roker@748
   110
        cout << "successfully imported key\n";
roker@748
   111
        kf++;
roker@748
   112
    }
roker@748
   113
roker@751
   114
    Buffer cipher_buffer = ReadFileIntoMem("msg.asc");
roker@751
   115
    const size_t cipher_length = cipher_buffer.size();
roker@748
   116
roker@751
   117
    cout << "\n" << cipher_buffer.data();
roker@748
   118
roker@750
   119
    char *buf_text = NULL;
roker@750
   120
    size_t buf_size = 0;
roker@750
   121
    stringlist_t *keylist;
roker@748
   122
roker@748
   123
    cout << "calling decrypt_and_verify()\n";
roker@751
   124
    PEP_STATUS decrypt_result = decrypt_and_verify(session, cipher_buffer.data(), cipher_length, &buf_text, &buf_size, &keylist);
roker@748
   125
roker@748
   126
    cout << "returning from decrypt_and_verify() with result == 0x" << std::hex << decrypt_result << "\n";
roker@748
   127
    assert(decrypt_result == PEP_DECRYPTED_AND_VERIFIED);
roker@748
   128
    assert(buf_text);
roker@748
   129
    assert(keylist);
roker@748
   130
roker@748
   131
    for (stringlist_t *_keylist=keylist; _keylist!=NULL; _keylist=_keylist->next) {
roker@748
   132
        assert(_keylist->value);
roker@748
   133
        cout << "signed with " << _keylist->value << "\n";
roker@748
   134
    }
roker@748
   135
roker@748
   136
    free_stringlist(keylist);
roker@748
   137
    buf_text[buf_size] = 0;
roker@748
   138
    string plain(buf_text);
roker@748
   139
    pEp_free(buf_text);
roker@748
   140
    cout << "\n" << plain;
roker@748
   141
roker@751
   142
    Buffer t1_buffer = ReadFileIntoMem("t1.txt");
roker@751
   143
    const size_t t1_length = t1_buffer.size();
roker@748
   144
roker@751
   145
    Buffer sig_buffer = ReadFileIntoMem("signature.asc");
roker@751
   146
    const size_t sig_length = sig_buffer.size();
roker@748
   147
roker@748
   148
    cout << "\ncalling verify_text()\n";
roker@751
   149
    PEP_STATUS verify_result = verify_text(session, t1_buffer.data(), t1_length, sig_buffer.data(), sig_length, &keylist);
roker@748
   150
    cout << "returning from verify_text() with result == " << verify_result << "\n";
roker@748
   151
    assert(verify_result == PEP_VERIFIED || verify_result == PEP_VERIFIED_AND_TRUSTED);
roker@748
   152
    assert(keylist->value);
roker@748
   153
    cout << "signed with " << keylist->value << "\n";
roker@748
   154
    free_stringlist(keylist);
roker@748
   155
roker@751
   156
    Buffer t2_buffer = ReadFileIntoMem("t2.txt");
roker@751
   157
    size_t t2_length = t2_buffer.size();
roker@748
   158
roker@748
   159
    cout << "\ncalling verify_text()\n";
roker@751
   160
    verify_result = verify_text(session, t2_buffer.data(), t2_length, sig_buffer.data(), sig_length, &keylist);
roker@748
   161
    assert(verify_result == PEP_DECRYPT_SIGNATURE_DOES_NOT_MATCH);
roker@748
   162
    free_stringlist(keylist);
roker@748
   163
roker@748
   164
    keylist = new_stringlist("A9411D176FF00E97");
roker@748
   165
    stringlist_add(keylist, "59BFF488C9C2EE39");
roker@748
   166
    stringlist_add(keylist, "135CD6D170DCF575");
roker@748
   167
roker@750
   168
    buf_text = NULL;
roker@750
   169
    buf_size = 0;
roker@748
   170
roker@748
   171
    cout << "\ncalling encrypt_and_sign()\n";
roker@748
   172
    PEP_STATUS encrypt_result = encrypt_and_sign(session, keylist, plain.c_str(), plain.length(), &buf_text, &buf_size);
roker@748
   173
    cout << "returning from encrypt_and_sign() with result == " << encrypt_result << "\n";
roker@748
   174
    assert(encrypt_result == PEP_STATUS_OK);
roker@748
   175
    free_stringlist(keylist);
roker@748
   176
roker@748
   177
    buf_text[buf_size] = 0;
roker@748
   178
    string cipher2(buf_text);
roker@748
   179
    cout << "\n" << cipher2;
roker@748
   180
    pEp_free(buf_text);
roker@748
   181
roker@750
   182
    cout << "\nfinding English trustword for 2342...\n";
roker@750
   183
    char * word;
roker@750
   184
    size_t wsize;
roker@750
   185
    trustword(session, 2342, "en", &word, &wsize);
roker@750
   186
    assert(word);
roker@750
   187
    cout << "the trustword for 2342 is " << word << "\n";
roker@748
   188
    pEp_free(word);
roker@748
   189
roker@748
   190
    string fingerprint = "4942 2235 FC99 585B 891C  6653 0C7B 109B FA72 61F7";
roker@748
   191
    char * words;
roker@748
   192
roker@748
   193
    cout << "\nfinding German trustwords for " << fingerprint << "...\n";
roker@748
   194
    trustwords(session, fingerprint.c_str(), "de", &words, &wsize, 5);
roker@748
   195
    assert(words);
roker@748
   196
    cout << words << "\n";
roker@748
   197
    pEp_free(words);
roker@748
   198
roker@751
   199
    pEp_identity* identity  = new_identity(
roker@748
   200
            "leon.schumacher@digitalekho.com",
roker@748
   201
            "8BD08954C74D830EEFFB5DEB2682A17F7C87F73D",
roker@748
   202
            "23",
roker@748
   203
            "Leon Schumacher"
roker@748
   204
        );
roker@750
   205
    identity->comm_type = PEP_ct_pEp;
roker@748
   206
roker@750
   207
    cout << "\nsetting identity...\n";
roker@750
   208
    PEP_STATUS pep_set_result = set_identity(session, identity);
roker@750
   209
    assert(pep_set_result == PEP_STATUS_OK);
roker@748
   210
    free_identity(identity);
roker@750
   211
    get_identity(session, "leon.schumacher@digitalekho.com", "23", &identity);
roker@750
   212
    assert(identity);
roker@750
   213
    cout << "set: " << identity->address << ", " << identity->fpr << ", " << identity->user_id << ", " << identity->username << "\n";
roker@748
   214
roker@748
   215
    PEP_STATUS get_trust_result = get_trust(session, identity);
roker@748
   216
    assert(get_trust_result == PEP_STATUS_OK);
roker@748
   217
    cout << "trust of " << identity->user_id << " is " << identity->comm_type << "\n";
roker@748
   218
roker@748
   219
    free_identity(identity);
roker@748
   220
roker@748
   221
    cout << "\ngenerating key for testuser\n";
roker@748
   222
    identity = new_identity(
roker@748
   223
            "testuser@pibit.ch",
roker@748
   224
            NULL,
roker@748
   225
            "423",
roker@748
   226
            "Alfred E. Neuman"
roker@748
   227
        );
roker@750
   228
roker@748
   229
    assert(identity);
roker@748
   230
    PEP_STATUS generate_status = generate_keypair(session, identity);
roker@748
   231
    cout << "generate_keypair() exits with " << generate_status << "\n";
roker@748
   232
    assert(generate_status == PEP_STATUS_OK);
roker@748
   233
    cout << "generated key is " << identity->fpr << "\n";
roker@748
   234
roker@748
   235
    string key(identity->fpr);
roker@748
   236
    free_identity(identity);
roker@748
   237
roker@748
   238
    char *key_data;
roker@748
   239
    size_t size;
roker@748
   240
roker@748
   241
    cout << "export_key()\n\n";
roker@748
   242
    PEP_STATUS export_status = export_key(session, key.c_str(), &key_data, &size);
roker@748
   243
    cout << "export_key() exits with " << export_status << "\n";
roker@748
   244
    assert(export_status == PEP_STATUS_OK);
roker@748
   245
    cout << key_data << "\n\n";
roker@748
   246
roker@748
   247
    cout << "deleting key pair " << key.c_str() << "\n";
roker@748
   248
    PEP_STATUS delete_status = delete_keypair(session, key.c_str());
roker@748
   249
    cout << "delete_keypair() exits with " << delete_status << "\n";
roker@748
   250
    assert(delete_status == PEP_STATUS_OK);
roker@748
   251
    
roker@748
   252
    cout << "import_key()\n";
roker@748
   253
    PEP_STATUS import_status = import_key(session, key_data, size, NULL);
roker@748
   254
    assert(import_status == PEP_STATUS_OK);
roker@748
   255
    cout << "successfully imported key\n";
roker@748
   256
roker@748
   257
    pEp_free(key_data);
roker@748
   258
roker@748
   259
    cout << "deleting key " << key.c_str() << " again\n";
roker@748
   260
    delete_status = delete_keypair(session, key.c_str());
roker@748
   261
    cout << "delete_keypair() exits with " << delete_status << "\n";
roker@748
   262
    assert(delete_status == PEP_STATUS_OK);
roker@748
   263
roker@748
   264
    cout << "finding key for pep.test.john@pep-project.org\n";
roker@748
   265
    PEP_STATUS find_keys_status = find_keys(session, "pep.test.john@pep-project.org", &keylist);
roker@748
   266
    cout << "find_keys() exits with " << find_keys_status << "\n";
roker@748
   267
    assert(find_keys_status == PEP_STATUS_OK);
roker@748
   268
    assert(keylist);
roker@748
   269
    cout << "found: " << keylist->value << "\n";
roker@748
   270
    assert(keylist->next == NULL);
roker@748
   271
    free_stringlist(keylist);
roker@748
   272
roker@748
   273
    cout << "searching for vb@ulm.ccc.de on keyserver\n";
roker@748
   274
    PEP_STATUS recv_key_status = recv_key(session, "vb@ulm.ccc.de");
roker@748
   275
    cout << "recv_key() exits with " << recv_key_status << "\n";
roker@748
   276
    assert(recv_key_status == PEP_STATUS_OK);
roker@748
   277
roker@748
   278
    cout << "sending vb@ulm.ccc.de to keyserver\n";
roker@748
   279
    PEP_STATUS send_key_status = send_key(session, "vb@ulm.ccc.de");
roker@748
   280
    cout << "send_key() exits with " << send_key_status << "\n";
roker@748
   281
    assert(send_key_status == PEP_STATUS_OK);
roker@748
   282
roker@748
   283
    PEP_comm_type tcomm_type;
roker@748
   284
    PEP_STATUS tstatus = get_key_rating(session, "59BFF488C9C2EE39", &tcomm_type);
roker@748
   285
    cout << "get_key_rating() exits with " << tstatus << "\n";
roker@748
   286
    assert(tstatus == PEP_STATUS_OK);
roker@748
   287
    assert(tcomm_type == PEP_ct_OpenPGP_unconfirmed);
roker@748
   288
    
roker@750
   289
    cout << "\ncalling release()\n";
roker@750
   290
    release(session);
roker@750
   291
    return 0;
roker@748
   292
}