test/pEpEngineTest.cc
author Krista Grothoff <krista@pep-project.org>
Mon, 05 Sep 2016 20:20:26 +0200
changeset 1141 e9e50ff70d23
parent 1011 e5873045d47a
child 1142 b0b208f85c88
permissions -rw-r--r--
Fixed key length issues in test which caused netpgp to fail.
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@753
    16
typedef std::string 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@753
    44
    buffer.resize(length);
roker@751
    45
roker@751
    46
    cout << "Reading " << length << " characters... ";
roker@753
    47
    txtFile.read (&buffer[0], length);
roker@751
    48
roker@751
    49
    if (!txtFile)
roker@751
    50
    {
roker@751
    51
        throw std::runtime_error( "error: only " + to_string(txtFile.gcount()) + " could be read from file" + fname );
roker@751
    52
    }
roker@751
    53
roker@751
    54
    cout << "all characters read successfully." << std::endl;
roker@751
    55
    return buffer;
roker@748
    56
}
roker@748
    57
roker@750
    58
roker@748
    59
int main(int argc, char* argv[])
roker@748
    60
{
roker@750
    61
    PEP_SESSION session;
roker@748
    62
roker@750
    63
    cout << "calling init()\n";
roker@750
    64
    PEP_STATUS init_result = init(&session);
roker@750
    65
    
roker@748
    66
    cout << "returning from init() with result == " << init_result << "\n";
roker@750
    67
    assert(init_result == PEP_STATUS_OK);
roker@748
    68
roker@748
    69
    PEP_SESSION second_session;
roker@748
    70
    cout << "second session test\n";
roker@748
    71
    PEP_STATUS second_init_result = init(&second_session);
roker@750
    72
    cout << "returning from second init() with result == " << second_init_result << "\n";
roker@748
    73
    assert(second_init_result == PEP_STATUS_OK);
roker@748
    74
    assert(second_session);
roker@748
    75
    cout << "dropping second session\n";
roker@750
    76
    release(second_session);
roker@748
    77
roker@750
    78
    cout << "logging test\n";
krista@1141
    79
    log_event(session, "log test", "pEp Engine Test", "This is a logging test sample.", "please ignore this line");
roker@748
    80
roker@748
    81
    // Our test user :
roker@748
    82
    // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
krista@1141
    83
    //                                 6FF00E97 -- won't work as search term with NetPGP
krista@1141
    84
    //                         A9411D176FF00E97 -- won't work as search term with NetPGP
krista@1141
    85
    // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97 -- full key fingerprint
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> 
krista@1141
    89
    //                                 C9C2EE39 -- won't work as search term with NetPGP
krista@1141
    90
    //                         59BFF488C9C2EE39 -- won't work as search term with NetPGP
krista@1141
    91
    // BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39 -- full key fingerprint
roker@748
    92
    // 
roker@748
    93
    // pEp Test John (test key, don't use) <pep.test.john@pep-project.org>
krista@1141
    94
    //                                 70DCF575 -- won't work as search term with NetPGP
krista@1141
    95
    //                         135CD6D170DCF575 -- won't work as search term with NetPGP
krista@1141
    96
    // AA2E4BEB93E5FE33DEFD8BE1135CD6D170DCF575 -- full key fingerprint
roker@748
    97
roker@748
    98
    const char *kflist[] = {
roker@748
    99
        "0x6FF00E97.asc",
roker@748
   100
        "0xC9C2EE39.asc",
roker@748
   101
        "0x70DCF575.asc",
roker@748
   102
        NULL
roker@748
   103
    };
roker@750
   104
roker@748
   105
    const char** kf = kflist;
roker@748
   106
    while(*kf){
roker@751
   107
        const Buffer k_user_buffer =  ReadFileIntoMem(*kf);
roker@748
   108
        cout << "import_key(" << *kf << ")\n";
roker@752
   109
        PEP_STATUS import_status = import_key(session, k_user_buffer.data(), k_user_buffer.size(), NULL);
roker@748
   110
        assert(import_status == PEP_STATUS_OK);
roker@748
   111
        cout << "successfully imported key\n";
roker@748
   112
        kf++;
roker@748
   113
    }
roker@748
   114
roker@752
   115
    const Buffer cipher_buffer = ReadFileIntoMem("msg.asc");
roker@751
   116
    cout << "\n" << cipher_buffer.data();
roker@748
   117
roker@750
   118
    char *buf_text = NULL;
roker@750
   119
    size_t buf_size = 0;
roker@750
   120
    stringlist_t *keylist;
roker@748
   121
roker@748
   122
    cout << "calling decrypt_and_verify()\n";
roker@752
   123
    PEP_STATUS decrypt_result = decrypt_and_verify(session, cipher_buffer.data(), cipher_buffer.size(), &buf_text, &buf_size, &keylist);
roker@748
   124
roker@748
   125
    cout << "returning from decrypt_and_verify() with result == 0x" << std::hex << decrypt_result << "\n";
roker@748
   126
    assert(decrypt_result == PEP_DECRYPTED_AND_VERIFIED);
roker@748
   127
    assert(buf_text);
roker@748
   128
    assert(keylist);
roker@748
   129
roker@748
   130
    for (stringlist_t *_keylist=keylist; _keylist!=NULL; _keylist=_keylist->next) {
roker@748
   131
        assert(_keylist->value);
roker@748
   132
        cout << "signed with " << _keylist->value << "\n";
roker@748
   133
    }
roker@748
   134
roker@748
   135
    free_stringlist(keylist);
roker@748
   136
    buf_text[buf_size] = 0;
roker@752
   137
    const string plain(buf_text);
roker@748
   138
    pEp_free(buf_text);
roker@748
   139
    cout << "\n" << plain;
roker@748
   140
roker@752
   141
    const Buffer t1_buffer = ReadFileIntoMem("t1.txt");
roker@752
   142
    const Buffer sig_buffer = ReadFileIntoMem("signature.asc");
roker@748
   143
roker@748
   144
    cout << "\ncalling verify_text()\n";
roker@752
   145
    PEP_STATUS verify_result = verify_text(session, t1_buffer.data(), t1_buffer.size(), sig_buffer.data(), sig_buffer.size(), &keylist);
roker@748
   146
    cout << "returning from verify_text() with result == " << verify_result << "\n";
roker@748
   147
    assert(verify_result == PEP_VERIFIED || verify_result == PEP_VERIFIED_AND_TRUSTED);
roker@748
   148
    assert(keylist->value);
roker@748
   149
    cout << "signed with " << keylist->value << "\n";
roker@748
   150
    free_stringlist(keylist);
roker@748
   151
roker@752
   152
    const Buffer t2_buffer = ReadFileIntoMem("t2.txt");
roker@748
   153
roker@748
   154
    cout << "\ncalling verify_text()\n";
roker@752
   155
    verify_result = verify_text(session, t2_buffer.data(), t2_buffer.size(), sig_buffer.data(), sig_buffer.size(), &keylist);
roker@748
   156
    assert(verify_result == PEP_DECRYPT_SIGNATURE_DOES_NOT_MATCH);
roker@748
   157
    free_stringlist(keylist);
roker@748
   158
krista@1141
   159
    keylist = new_stringlist("4ABE3AAF59AC32CFE4F86500A9411D176FF00E97");
krista@1141
   160
    stringlist_add(keylist, "BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39");
krista@1141
   161
    stringlist_add(keylist, "AA2E4BEB93E5FE33DEFD8BE1135CD6D170DCF575");
roker@748
   162
roker@750
   163
    buf_text = NULL;
roker@750
   164
    buf_size = 0;
roker@748
   165
roker@748
   166
    cout << "\ncalling encrypt_and_sign()\n";
roker@748
   167
    PEP_STATUS encrypt_result = encrypt_and_sign(session, keylist, plain.c_str(), plain.length(), &buf_text, &buf_size);
roker@748
   168
    cout << "returning from encrypt_and_sign() with result == " << encrypt_result << "\n";
roker@748
   169
    assert(encrypt_result == PEP_STATUS_OK);
roker@748
   170
    free_stringlist(keylist);
roker@748
   171
roker@752
   172
    buf_text[buf_size] = '\0';
roker@752
   173
    const string cipher2(buf_text);
roker@748
   174
    cout << "\n" << cipher2;
roker@748
   175
    pEp_free(buf_text);
roker@748
   176
roker@750
   177
    cout << "\nfinding English trustword for 2342...\n";
roker@752
   178
    char * word = NULL;
roker@750
   179
    size_t wsize;
roker@750
   180
    trustword(session, 2342, "en", &word, &wsize);
roker@750
   181
    assert(word);
Edouard@791
   182
    cout << "the English trustword for 2342 is " << word << "\n";
Edouard@791
   183
    pEp_free(word);
Edouard@791
   184
    cout << "\nfinding French trustword for 2342...\n";
Edouard@791
   185
    trustword(session, 2342, "fr", &word, &wsize);
Edouard@791
   186
    assert(word);
Edouard@791
   187
    cout << "the French trustword for 2342 is " << word << "\n";
roker@748
   188
    pEp_free(word);
roker@748
   189
roker@752
   190
    const string fingerprint = "4942 2235 FC99 585B 891C  6653 0C7B 109B FA72 61F7";
roker@752
   191
    char * words = NULL;
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@752
   235
    const string key(identity->fpr);
roker@748
   236
    free_identity(identity);
roker@748
   237
roker@752
   238
    char *key_data = NULL;
roker@752
   239
    size_t size = 0;
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@752
   258
    key_data=NULL;
roker@748
   259
roker@748
   260
    cout << "deleting key " << key.c_str() << " again\n";
roker@748
   261
    delete_status = delete_keypair(session, key.c_str());
roker@748
   262
    cout << "delete_keypair() exits with " << delete_status << "\n";
roker@748
   263
    assert(delete_status == PEP_STATUS_OK);
roker@748
   264
roker@748
   265
    cout << "finding key for pep.test.john@pep-project.org\n";
roker@748
   266
    PEP_STATUS find_keys_status = find_keys(session, "pep.test.john@pep-project.org", &keylist);
roker@748
   267
    cout << "find_keys() exits with " << find_keys_status << "\n";
roker@748
   268
    assert(find_keys_status == PEP_STATUS_OK);
roker@748
   269
    assert(keylist);
roker@748
   270
    cout << "found: " << keylist->value << "\n";
roker@748
   271
    assert(keylist->next == NULL);
roker@748
   272
    free_stringlist(keylist);
roker@748
   273
roker@748
   274
    cout << "searching for vb@ulm.ccc.de on keyserver\n";
roker@748
   275
    PEP_STATUS recv_key_status = recv_key(session, "vb@ulm.ccc.de");
roker@748
   276
    cout << "recv_key() exits with " << recv_key_status << "\n";
roker@748
   277
    assert(recv_key_status == PEP_STATUS_OK);
roker@748
   278
roker@748
   279
    cout << "sending vb@ulm.ccc.de to keyserver\n";
roker@748
   280
    PEP_STATUS send_key_status = send_key(session, "vb@ulm.ccc.de");
roker@748
   281
    cout << "send_key() exits with " << send_key_status << "\n";
roker@748
   282
    assert(send_key_status == PEP_STATUS_OK);
roker@748
   283
roker@748
   284
    PEP_comm_type tcomm_type;
roker@748
   285
    PEP_STATUS tstatus = get_key_rating(session, "59BFF488C9C2EE39", &tcomm_type);
roker@748
   286
    cout << "get_key_rating() exits with " << tstatus << "\n";
roker@748
   287
    assert(tstatus == PEP_STATUS_OK);
roker@748
   288
    assert(tcomm_type == PEP_ct_OpenPGP_unconfirmed);
roker@748
   289
    
roker@750
   290
    cout << "\ncalling release()\n";
krista@1011
   291
    cout << endl << "End of pEpEngineTest for engine version " << get_engine_version() << endl;
krista@1011
   292
roker@750
   293
    release(session);
roker@750
   294
    return 0;
roker@748
   295
}