test/pEpEngineTest.cc
author Krista Bennett <krista@pep-project.org>
Mon, 25 Sep 2017 15:56:10 +0200
changeset 2101 be4563366499
parent 1720 a4b6fe57af2d
child 2468 3a10b4f8ba41
permissions -rw-r--r--
pEpEngineTest now imports its own keys
vb@1513
     1
// This file is under GNU General Public License 3.0
vb@1513
     2
// see LICENSE.txt
vb@1513
     3
roker@748
     4
#include <iostream>
roker@748
     5
#include <fstream>
roker@751
     6
#include <stdexcept>
roker@748
     7
#include <string>
roker@751
     8
#include <vector>
roker@748
     9
roker@748
    10
#include <assert.h>
roker@748
    11
#include <string.h>
krista@2101
    12
#include <sstream>
krista@2101
    13
roker@748
    14
roker@748
    15
#include "../src/pEpEngine.h"
roker@748
    16
#include "../src/keymanagement.h"
roker@748
    17
roker@748
    18
using namespace std;
roker@748
    19
roker@753
    20
typedef std::string Buffer;
roker@751
    21
krista@2101
    22
std::string slurp(const std::string& filename)
krista@2101
    23
{
krista@2101
    24
        std::ifstream input(filename.c_str());
krista@2101
    25
        if(!input)
krista@2101
    26
        {
krista@2101
    27
                throw std::runtime_error("Cannot read file \"" + filename + "\"! ");
krista@2101
    28
        }
krista@2101
    29
krista@2101
    30
        std::stringstream sstr;
krista@2101
    31
        sstr << input.rdbuf();
krista@2101
    32
        return sstr.str();
krista@2101
    33
}
krista@2101
    34
roker@751
    35
// no C++11, yet? So do our own implementation:
roker@751
    36
namespace{
roker@751
    37
    std::string to_string(unsigned long u)
roker@751
    38
    {
roker@751
    39
        char buf[32];
roker@751
    40
        snprintf(buf,31, "%lu", u);
roker@751
    41
        return buf;
roker@751
    42
    }
roker@1710
    43
    
roker@1710
    44
    std::string status(PEP_STATUS status)
roker@1710
    45
    {
roker@1710
    46
        char buf[32] = {0};
roker@1710
    47
        if(status==0)
roker@1710
    48
        {
roker@1710
    49
            return "PEP_STATUS_OK";
roker@1710
    50
        }else{
roker@1710
    51
            if(status>0)
roker@1710
    52
            {
roker@1710
    53
                snprintf(buf,31, "%u (0x%x)", status, status);
roker@1710
    54
            }else{
roker@1710
    55
                snprintf(buf,31, "%d", status);
roker@1710
    56
            }
roker@1710
    57
        }
roker@1710
    58
        return buf;
roker@1710
    59
    }
roker@1710
    60
roker@1710
    61
} // end of anonymous namespace
roker@751
    62
roker@751
    63
roker@751
    64
Buffer ReadFileIntoMem(const char *fname){
roker@750
    65
    cout << "opening " << fname << " for reading\n";
roker@750
    66
    ifstream txtFile (fname, ifstream::binary);
roker@750
    67
    assert(txtFile.is_open());
roker@751
    68
    if (!txtFile)
roker@751
    69
    {
roker@751
    70
        throw std::runtime_error( "error: cannot open file \"" + std::string(fname) + "\"" );
roker@748
    71
    }
roker@748
    72
roker@751
    73
    Buffer buffer;
roker@751
    74
roker@751
    75
    // get length of file:
roker@751
    76
    txtFile.seekg (0, txtFile.end);
roker@751
    77
    const size_t length = txtFile.tellg();
roker@751
    78
    txtFile.seekg (0, txtFile.beg);
roker@753
    79
    buffer.resize(length);
roker@751
    80
roker@751
    81
    cout << "Reading " << length << " characters... ";
roker@753
    82
    txtFile.read (&buffer[0], length);
roker@751
    83
roker@751
    84
    if (!txtFile)
roker@751
    85
    {
roker@751
    86
        throw std::runtime_error( "error: only " + to_string(txtFile.gcount()) + " could be read from file" + fname );
roker@751
    87
    }
roker@751
    88
roker@751
    89
    cout << "all characters read successfully." << std::endl;
roker@751
    90
    return buffer;
roker@748
    91
}
roker@748
    92
roker@750
    93
roker@748
    94
int main(int argc, char* argv[])
roker@748
    95
{
roker@750
    96
    PEP_SESSION session;
roker@748
    97
roker@750
    98
    cout << "calling init()\n";
roker@750
    99
    PEP_STATUS init_result = init(&session);
roker@750
   100
    
roker@1710
   101
    cout << "returning from init() with result == " << status(init_result) << endl;
roker@750
   102
    assert(init_result == PEP_STATUS_OK);
roker@748
   103
roker@748
   104
    PEP_SESSION second_session;
roker@748
   105
    cout << "second session test\n";
roker@748
   106
    PEP_STATUS second_init_result = init(&second_session);
roker@1710
   107
    cout << "returning from second init() with result == " << status(second_init_result) << endl;
roker@748
   108
    assert(second_init_result == PEP_STATUS_OK);
roker@748
   109
    assert(second_session);
roker@748
   110
    cout << "dropping second session\n";
roker@750
   111
    release(second_session);
roker@748
   112
krista@2101
   113
    cout << "Importing keys...";
krista@2101
   114
    string pub_key = slurp("test_keys/pub/pep-test-alice-0x6FF00E97_pub.asc");
krista@2101
   115
    string priv_key = slurp("test_keys/priv/pep-test-alice-0x6FF00E97_priv.asc");
krista@2101
   116
krista@2101
   117
    PEP_STATUS statuspub = import_key(session, pub_key.c_str(), pub_key.length(), NULL);
krista@2101
   118
    PEP_STATUS statuspriv = import_key(session, priv_key.c_str(), priv_key.length(), NULL);
krista@2101
   119
    assert(statuspub == PEP_STATUS_OK);
krista@2101
   120
    assert(statuspriv == PEP_STATUS_OK);
krista@2101
   121
krista@2101
   122
    pub_key = slurp("test_keys/pub/pep-test-john-0x70DCF575_pub.asc");
krista@2101
   123
    priv_key = slurp("test_keys/priv/pep-test-john-0x70DCF575_priv.asc");
krista@2101
   124
krista@2101
   125
    statuspub = import_key(session, pub_key.c_str(), pub_key.length(), NULL);
krista@2101
   126
    statuspriv = import_key(session, priv_key.c_str(), priv_key.length(), NULL);
krista@2101
   127
    assert(statuspub == PEP_STATUS_OK);
krista@2101
   128
    assert(statuspriv == PEP_STATUS_OK);
krista@2101
   129
krista@2101
   130
    pub_key = slurp("test_keys/pub/pep-test-bob-0xC9C2EE39_pub.asc");
krista@2101
   131
krista@2101
   132
    statuspub = import_key(session, pub_key.c_str(), pub_key.length(), NULL);
krista@2101
   133
    assert(statuspub == PEP_STATUS_OK);
krista@2101
   134
    assert(statuspriv == PEP_STATUS_OK);
krista@2101
   135
krista@2101
   136
krista@2101
   137
    cout << "creating message…\n";
krista@2101
   138
    pEp_identity* alice = new_identity("pep.test.alice@pep-project.org", "4ABE3AAF59AC32CFE4F86500A9411D176FF00E97", PEP_OWN_USERID, "Alice Test");
krista@2101
   139
    pEp_identity* bob = new_identity("pep.test.bob@pep-project.org", NULL, "42", "Bob Test");
krista@2101
   140
krista@2101
   141
roker@750
   142
    cout << "logging test\n";
krista@1141
   143
    log_event(session, "log test", "pEp Engine Test", "This is a logging test sample.", "please ignore this line");
roker@748
   144
roker@748
   145
    // Our test user :
roker@748
   146
    // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
krista@1141
   147
    //                                 6FF00E97 -- won't work as search term with NetPGP
krista@1141
   148
    //                         A9411D176FF00E97 -- won't work as search term with NetPGP
krista@1141
   149
    // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97 -- full key fingerprint
roker@748
   150
    // 
roker@748
   151
    // Other peers :
roker@748
   152
    // pEp Test Bob (test key, don't use) <pep.test.bob@pep-project.org> 
krista@1141
   153
    //                                 C9C2EE39 -- won't work as search term with NetPGP
krista@1141
   154
    //                         59BFF488C9C2EE39 -- won't work as search term with NetPGP
krista@1141
   155
    // BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39 -- full key fingerprint
roker@748
   156
    // 
roker@748
   157
    // pEp Test John (test key, don't use) <pep.test.john@pep-project.org>
krista@1141
   158
    //                                 70DCF575 -- won't work as search term with NetPGP
krista@1141
   159
    //                         135CD6D170DCF575 -- won't work as search term with NetPGP
krista@1141
   160
    // AA2E4BEB93E5FE33DEFD8BE1135CD6D170DCF575 -- full key fingerprint
roker@748
   161
roker@748
   162
    const char *kflist[] = {
roker@748
   163
        "0x6FF00E97.asc",
roker@748
   164
        "0xC9C2EE39.asc",
roker@748
   165
        "0x70DCF575.asc",
roker@748
   166
        NULL
roker@748
   167
    };
roker@750
   168
roker@748
   169
    const char** kf = kflist;
roker@748
   170
    while(*kf){
roker@751
   171
        const Buffer k_user_buffer =  ReadFileIntoMem(*kf);
roker@748
   172
        cout << "import_key(" << *kf << ")\n";
roker@752
   173
        PEP_STATUS import_status = import_key(session, k_user_buffer.data(), k_user_buffer.size(), NULL);
roker@748
   174
        assert(import_status == PEP_STATUS_OK);
roker@748
   175
        cout << "successfully imported key\n";
roker@748
   176
        kf++;
roker@748
   177
    }
roker@748
   178
roker@752
   179
    const Buffer cipher_buffer = ReadFileIntoMem("msg.asc");
roker@751
   180
    cout << "\n" << cipher_buffer.data();
roker@748
   181
roker@750
   182
    char *buf_text = NULL;
roker@750
   183
    size_t buf_size = 0;
roker@750
   184
    stringlist_t *keylist;
roker@748
   185
roker@748
   186
    cout << "calling decrypt_and_verify()\n";
krista@1397
   187
    PEP_STATUS decrypt_result = decrypt_and_verify(session, cipher_buffer.data(), cipher_buffer.size(), NULL, 0, &buf_text, &buf_size, &keylist);
roker@748
   188
roker@1710
   189
    cout << "returning from decrypt_and_verify() with result == " << status(decrypt_result) << endl;
roker@748
   190
    assert(decrypt_result == PEP_DECRYPTED_AND_VERIFIED);
roker@748
   191
    assert(buf_text);
roker@748
   192
    assert(keylist);
roker@748
   193
roker@748
   194
    for (stringlist_t *_keylist=keylist; _keylist!=NULL; _keylist=_keylist->next) {
roker@748
   195
        assert(_keylist->value);
roker@1710
   196
        cout << "signed with " << _keylist->value << endl;
roker@748
   197
    }
roker@748
   198
roker@748
   199
    free_stringlist(keylist);
roker@748
   200
    buf_text[buf_size] = 0;
roker@752
   201
    const string plain(buf_text);
roker@748
   202
    pEp_free(buf_text);
roker@748
   203
    cout << "\n" << plain;
roker@748
   204
roker@752
   205
    const Buffer t1_buffer = ReadFileIntoMem("t1.txt");
roker@752
   206
    const Buffer sig_buffer = ReadFileIntoMem("signature.asc");
roker@748
   207
roker@748
   208
    cout << "\ncalling verify_text()\n";
roker@752
   209
    PEP_STATUS verify_result = verify_text(session, t1_buffer.data(), t1_buffer.size(), sig_buffer.data(), sig_buffer.size(), &keylist);
roker@1710
   210
    cout << "returning from verify_text() with result == " << status(verify_result) << endl;
roker@748
   211
    assert(verify_result == PEP_VERIFIED || verify_result == PEP_VERIFIED_AND_TRUSTED);
roker@748
   212
    assert(keylist->value);
roker@1710
   213
    cout << "signed with " << keylist->value << endl;
roker@748
   214
    free_stringlist(keylist);
roker@748
   215
roker@752
   216
    const Buffer t2_buffer = ReadFileIntoMem("t2.txt");
roker@748
   217
roker@748
   218
    cout << "\ncalling verify_text()\n";
roker@752
   219
    verify_result = verify_text(session, t2_buffer.data(), t2_buffer.size(), sig_buffer.data(), sig_buffer.size(), &keylist);
roker@748
   220
    assert(verify_result == PEP_DECRYPT_SIGNATURE_DOES_NOT_MATCH);
roker@748
   221
    free_stringlist(keylist);
roker@748
   222
krista@1141
   223
    keylist = new_stringlist("4ABE3AAF59AC32CFE4F86500A9411D176FF00E97");
krista@1141
   224
    stringlist_add(keylist, "BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39");
krista@1141
   225
    stringlist_add(keylist, "AA2E4BEB93E5FE33DEFD8BE1135CD6D170DCF575");
roker@748
   226
roker@750
   227
    buf_text = NULL;
roker@750
   228
    buf_size = 0;
roker@748
   229
roker@748
   230
    cout << "\ncalling encrypt_and_sign()\n";
roker@748
   231
    PEP_STATUS encrypt_result = encrypt_and_sign(session, keylist, plain.c_str(), plain.length(), &buf_text, &buf_size);
roker@1710
   232
    cout << "returning from encrypt_and_sign() with result == " << status(encrypt_result) << endl;
roker@748
   233
    assert(encrypt_result == PEP_STATUS_OK);
roker@748
   234
    free_stringlist(keylist);
roker@748
   235
roker@752
   236
    buf_text[buf_size] = '\0';
roker@752
   237
    const string cipher2(buf_text);
roker@748
   238
    cout << "\n" << cipher2;
roker@748
   239
    pEp_free(buf_text);
roker@748
   240
roker@750
   241
    cout << "\nfinding English trustword for 2342...\n";
roker@752
   242
    char * word = NULL;
roker@750
   243
    size_t wsize;
roker@750
   244
    trustword(session, 2342, "en", &word, &wsize);
roker@750
   245
    assert(word);
roker@1710
   246
    cout << "the English trustword for 2342 is " << word << endl;
Edouard@791
   247
    pEp_free(word);
Edouard@791
   248
    cout << "\nfinding French trustword for 2342...\n";
Edouard@791
   249
    trustword(session, 2342, "fr", &word, &wsize);
Edouard@791
   250
    assert(word);
roker@1710
   251
    cout << "the French trustword for 2342 is " << word << endl;
roker@748
   252
    pEp_free(word);
roker@748
   253
roker@752
   254
    const string fingerprint = "4942 2235 FC99 585B 891C  6653 0C7B 109B FA72 61F7";
roker@752
   255
    char * words = NULL;
roker@748
   256
roker@748
   257
    cout << "\nfinding German trustwords for " << fingerprint << "...\n";
roker@748
   258
    trustwords(session, fingerprint.c_str(), "de", &words, &wsize, 5);
roker@748
   259
    assert(words);
roker@1710
   260
    cout << words << endl;
roker@748
   261
    pEp_free(words);
roker@748
   262
roker@751
   263
    pEp_identity* identity  = new_identity(
roker@748
   264
            "leon.schumacher@digitalekho.com",
roker@748
   265
            "8BD08954C74D830EEFFB5DEB2682A17F7C87F73D",
roker@748
   266
            "23",
roker@748
   267
            "Leon Schumacher"
roker@748
   268
        );
roker@750
   269
    identity->comm_type = PEP_ct_pEp;
roker@748
   270
roker@750
   271
    cout << "\nsetting identity...\n";
roker@750
   272
    PEP_STATUS pep_set_result = set_identity(session, identity);
roker@750
   273
    assert(pep_set_result == PEP_STATUS_OK);
roker@748
   274
    free_identity(identity);
roker@750
   275
    get_identity(session, "leon.schumacher@digitalekho.com", "23", &identity);
roker@750
   276
    assert(identity);
roker@1710
   277
    cout << "set: " << identity->address << ", " << identity->fpr << ", " << identity->user_id << ", " << identity->username << endl;
roker@748
   278
roker@748
   279
    PEP_STATUS get_trust_result = get_trust(session, identity);
roker@748
   280
    assert(get_trust_result == PEP_STATUS_OK);
roker@1710
   281
    cout << "trust of " << identity->user_id << " is " << identity->comm_type << endl;
roker@748
   282
roker@748
   283
    free_identity(identity);
roker@748
   284
roker@748
   285
    cout << "\ngenerating key for testuser\n";
roker@748
   286
    identity = new_identity(
roker@748
   287
            "testuser@pibit.ch",
roker@748
   288
            NULL,
roker@748
   289
            "423",
roker@748
   290
            "Alfred E. Neuman"
roker@748
   291
        );
roker@750
   292
roker@748
   293
    assert(identity);
roker@748
   294
    PEP_STATUS generate_status = generate_keypair(session, identity);
roker@1710
   295
    cout << "generate_keypair() exits with " << status(generate_status) << endl;
roker@748
   296
    assert(generate_status == PEP_STATUS_OK);
roker@1710
   297
    cout << "generated key is " << identity->fpr << endl;
roker@748
   298
roker@752
   299
    const string key(identity->fpr);
roker@748
   300
    free_identity(identity);
roker@748
   301
roker@752
   302
    char *key_data = NULL;
roker@752
   303
    size_t size = 0;
roker@748
   304
roker@748
   305
    cout << "export_key()\n\n";
roker@748
   306
    PEP_STATUS export_status = export_key(session, key.c_str(), &key_data, &size);
roker@1710
   307
    cout << "export_key() exits with " << status(export_status) << endl;
roker@748
   308
    assert(export_status == PEP_STATUS_OK);
roker@748
   309
    cout << key_data << "\n\n";
roker@748
   310
roker@1710
   311
    cout << "deleting key pair " << key.c_str() << endl;
roker@748
   312
    PEP_STATUS delete_status = delete_keypair(session, key.c_str());
roker@1710
   313
    cout << "delete_keypair() exits with " << status(delete_status) << endl;
roker@748
   314
    assert(delete_status == PEP_STATUS_OK);
roker@748
   315
    
roker@748
   316
    cout << "import_key()\n";
roker@748
   317
    PEP_STATUS import_status = import_key(session, key_data, size, NULL);
roker@748
   318
    assert(import_status == PEP_STATUS_OK);
roker@748
   319
    cout << "successfully imported key\n";
roker@748
   320
roker@748
   321
    pEp_free(key_data);
roker@752
   322
    key_data=NULL;
roker@748
   323
roker@748
   324
    cout << "deleting key " << key.c_str() << " again\n";
roker@748
   325
    delete_status = delete_keypair(session, key.c_str());
roker@1710
   326
    cout << "delete_keypair() exits with " << status(delete_status) << endl;
roker@748
   327
    assert(delete_status == PEP_STATUS_OK);
roker@748
   328
roker@748
   329
    cout << "finding key for pep.test.john@pep-project.org\n";
roker@748
   330
    PEP_STATUS find_keys_status = find_keys(session, "pep.test.john@pep-project.org", &keylist);
roker@1710
   331
    cout << "find_keys() exits with " << status(find_keys_status) << endl;
roker@748
   332
    assert(find_keys_status == PEP_STATUS_OK);
roker@748
   333
    assert(keylist);
roker@1710
   334
    cout << "found: " << keylist->value << endl;
roker@748
   335
    assert(keylist->next == NULL);
roker@748
   336
    free_stringlist(keylist);
roker@748
   337
roker@748
   338
    cout << "searching for vb@ulm.ccc.de on keyserver\n";
roker@748
   339
    PEP_STATUS recv_key_status = recv_key(session, "vb@ulm.ccc.de");
roker@1710
   340
    cout << "recv_key() exits with " << status(recv_key_status) << endl;
roker@748
   341
    assert(recv_key_status == PEP_STATUS_OK);
roker@748
   342
roker@748
   343
    cout << "sending vb@ulm.ccc.de to keyserver\n";
roker@748
   344
    PEP_STATUS send_key_status = send_key(session, "vb@ulm.ccc.de");
roker@1710
   345
    cout << "send_key() exits with " << status(send_key_status) << endl;
roker@748
   346
    assert(send_key_status == PEP_STATUS_OK);
roker@748
   347
roker@748
   348
    PEP_comm_type tcomm_type;
krista@1142
   349
    PEP_STATUS tstatus = get_key_rating(session, "BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39", &tcomm_type);
roker@1720
   350
    cout << "get_key_rating() exits with " << status(tstatus) << endl;
roker@748
   351
    assert(tstatus == PEP_STATUS_OK);
roker@748
   352
    assert(tcomm_type == PEP_ct_OpenPGP_unconfirmed);
roker@748
   353
    
roker@750
   354
    cout << "\ncalling release()\n";
krista@1011
   355
    cout << endl << "End of pEpEngineTest for engine version " << get_engine_version() << endl;
krista@1011
   356
roker@750
   357
    release(session);
roker@750
   358
    return 0;
roker@748
   359
}