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