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