test/pEpEngineTest.cc
author Roker <roker@pep-project.org>
Tue, 21 Jun 2016 16:33:31 +0200
changeset 748 967b0bae4f01
parent 728 80b966277620
child 750 56df4e0f3ab9
permissions -rw-r--r--
dos2unix pEpEngineTest.cc
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@748
    16
	cout << "opening " << fname << " for reading\n";
roker@748
    17
	ifstream txtFile (fname, ifstream::binary);
roker@748
    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
	txtFile.close();
roker@748
    39
    assert(buffer);
roker@748
    40
    assert(length);
roker@748
    41
}
roker@748
    42
roker@748
    43
int main(int argc, char* argv[])
roker@748
    44
{
roker@748
    45
	PEP_SESSION session;
roker@748
    46
roker@748
    47
	cout << "calling init()\n";
roker@748
    48
	PEP_STATUS init_result = init(&session);
roker@748
    49
	
roker@748
    50
    cout << "returning from init() with result == " << init_result << "\n";
roker@748
    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@748
    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@748
    60
	release(second_session);
roker@748
    61
roker@748
    62
	cout << "logging test\n";
roker@748
    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@748
    85
    const char** kf = kflist;
roker@748
    86
    while(*kf){
roker@748
    87
        char * k_user_buffer = NULL;
roker@748
    88
        size_t k_user_length = 0;
roker@748
    89
        ReadFileIntoMem(*kf, k_user_buffer, k_user_length);
roker@748
    90
        cout << "import_key(" << *kf << ")\n";
roker@748
    91
        PEP_STATUS import_status = import_key(session, k_user_buffer, k_user_length, NULL);
roker@748
    92
        assert(import_status == PEP_STATUS_OK);
roker@748
    93
        cout << "successfully imported key\n";
roker@748
    94
        delete[] k_user_buffer;
roker@748
    95
        kf++;
roker@748
    96
    }
roker@748
    97
roker@748
    98
    char * cipher_buffer = NULL;
roker@748
    99
    size_t cipher_length = 0;
roker@748
   100
    ReadFileIntoMem("msg.asc", cipher_buffer, cipher_length);
roker@748
   101
roker@748
   102
	cout << "\n" << cipher_buffer;
roker@748
   103
roker@748
   104
	char *buf_text = NULL;
roker@748
   105
	size_t buf_size = 0;
roker@748
   106
	stringlist_t *keylist;
roker@748
   107
roker@748
   108
    cout << "calling decrypt_and_verify()\n";
roker@748
   109
    PEP_STATUS decrypt_result = decrypt_and_verify(session, cipher_buffer, cipher_length, &buf_text, &buf_size, &keylist);
roker@748
   110
roker@748
   111
    cout << "returning from decrypt_and_verify() with result == 0x" << std::hex << decrypt_result << "\n";
roker@748
   112
    assert(decrypt_result == PEP_DECRYPTED_AND_VERIFIED);
roker@748
   113
    assert(buf_text);
roker@748
   114
    assert(keylist);
roker@748
   115
roker@748
   116
    for (stringlist_t *_keylist=keylist; _keylist!=NULL; _keylist=_keylist->next) {
roker@748
   117
        assert(_keylist->value);
roker@748
   118
        cout << "signed with " << _keylist->value << "\n";
roker@748
   119
    }
roker@748
   120
roker@748
   121
    free_stringlist(keylist);
roker@748
   122
    buf_text[buf_size] = 0;
roker@748
   123
    string plain(buf_text);
roker@748
   124
    pEp_free(buf_text);
roker@748
   125
    cout << "\n" << plain;
roker@748
   126
roker@748
   127
    char * t1_buffer = NULL;
roker@748
   128
    size_t t1_length = 0;
roker@748
   129
    ReadFileIntoMem("t1.txt", t1_buffer, t1_length);
roker@748
   130
roker@748
   131
    char * sig_buffer = NULL;
roker@748
   132
    size_t sig_length = 0;
roker@748
   133
    ReadFileIntoMem("signature.asc", sig_buffer, sig_length);
roker@748
   134
roker@748
   135
    cout << "\ncalling verify_text()\n";
roker@748
   136
    PEP_STATUS verify_result = verify_text(session, t1_buffer, t1_length, sig_buffer, sig_length, &keylist);
roker@748
   137
    cout << "returning from verify_text() with result == " << verify_result << "\n";
roker@748
   138
    assert(verify_result == PEP_VERIFIED || verify_result == PEP_VERIFIED_AND_TRUSTED);
roker@748
   139
    assert(keylist->value);
roker@748
   140
    cout << "signed with " << keylist->value << "\n";
roker@748
   141
    free_stringlist(keylist);
roker@748
   142
roker@748
   143
    char * t2_buffer = NULL;
roker@748
   144
    size_t t2_length = 0;
roker@748
   145
    ReadFileIntoMem("t2.txt", t2_buffer, t2_length);
roker@748
   146
roker@748
   147
    cout << "\ncalling verify_text()\n";
roker@748
   148
    verify_result = verify_text(session, t2_buffer, t2_length, sig_buffer, sig_length, &keylist);
roker@748
   149
    assert(verify_result == PEP_DECRYPT_SIGNATURE_DOES_NOT_MATCH);
roker@748
   150
    free_stringlist(keylist);
roker@748
   151
roker@748
   152
    keylist = new_stringlist("A9411D176FF00E97");
roker@748
   153
    stringlist_add(keylist, "59BFF488C9C2EE39");
roker@748
   154
    stringlist_add(keylist, "135CD6D170DCF575");
roker@748
   155
roker@748
   156
	buf_text = NULL;
roker@748
   157
	buf_size = 0;
roker@748
   158
roker@748
   159
    cout << "\ncalling encrypt_and_sign()\n";
roker@748
   160
    PEP_STATUS encrypt_result = encrypt_and_sign(session, keylist, plain.c_str(), plain.length(), &buf_text, &buf_size);
roker@748
   161
    cout << "returning from encrypt_and_sign() with result == " << encrypt_result << "\n";
roker@748
   162
    assert(encrypt_result == PEP_STATUS_OK);
roker@748
   163
    free_stringlist(keylist);
roker@748
   164
roker@748
   165
    buf_text[buf_size] = 0;
roker@748
   166
    string cipher2(buf_text);
roker@748
   167
    cout << "\n" << cipher2;
roker@748
   168
    pEp_free(buf_text);
roker@748
   169
roker@748
   170
    delete[] cipher_buffer;
roker@748
   171
    delete[] t1_buffer;
roker@748
   172
    delete[] sig_buffer;
roker@748
   173
    delete[] t2_buffer;
roker@748
   174
roker@748
   175
	cout << "\nfinding English trustword for 2342...\n";
roker@748
   176
	char * word;
roker@748
   177
	size_t wsize;
roker@748
   178
	trustword(session, 2342, "en", &word, &wsize);
roker@748
   179
	assert(word);
roker@748
   180
	cout << "the trustword for 2342 is " << word << "\n";
roker@748
   181
    pEp_free(word);
roker@748
   182
roker@748
   183
    string fingerprint = "4942 2235 FC99 585B 891C  6653 0C7B 109B FA72 61F7";
roker@748
   184
    char * words;
roker@748
   185
roker@748
   186
    cout << "\nfinding German trustwords for " << fingerprint << "...\n";
roker@748
   187
    trustwords(session, fingerprint.c_str(), "de", &words, &wsize, 5);
roker@748
   188
    assert(words);
roker@748
   189
    cout << words << "\n";
roker@748
   190
    pEp_free(words);
roker@748
   191
roker@748
   192
	pEp_identity *identity;
roker@748
   193
roker@748
   194
    identity = new_identity(
roker@748
   195
            "leon.schumacher@digitalekho.com",
roker@748
   196
            "8BD08954C74D830EEFFB5DEB2682A17F7C87F73D",
roker@748
   197
            "23",
roker@748
   198
            "Leon Schumacher"
roker@748
   199
        );
roker@748
   200
	identity->comm_type = PEP_ct_pEp;
roker@748
   201
roker@748
   202
	cout << "\nsetting identity...\n";
roker@748
   203
	PEP_STATUS pep_set_result = set_identity(session, identity);
roker@748
   204
	assert(pep_set_result == PEP_STATUS_OK);
roker@748
   205
    free_identity(identity);
roker@748
   206
	get_identity(session, "leon.schumacher@digitalekho.com", "23", &identity);
roker@748
   207
	assert(identity);
roker@748
   208
	cout << "set: " << identity->address << ", " << identity->fpr << ", " << identity->user_id << ", " << identity->username << "\n";
roker@748
   209
roker@748
   210
    PEP_STATUS get_trust_result = get_trust(session, identity);
roker@748
   211
    assert(get_trust_result == PEP_STATUS_OK);
roker@748
   212
    cout << "trust of " << identity->user_id << " is " << identity->comm_type << "\n";
roker@748
   213
roker@748
   214
    free_identity(identity);
roker@748
   215
roker@748
   216
    cout << "\ngenerating key for testuser\n";
roker@748
   217
    identity = new_identity(
roker@748
   218
            "testuser@pibit.ch",
roker@748
   219
            NULL,
roker@748
   220
            "423",
roker@748
   221
            "Alfred E. Neuman"
roker@748
   222
        );
roker@748
   223
    assert(identity);
roker@748
   224
    PEP_STATUS generate_status = generate_keypair(session, identity);
roker@748
   225
    cout << "generate_keypair() exits with " << generate_status << "\n";
roker@748
   226
    assert(generate_status == PEP_STATUS_OK);
roker@748
   227
    cout << "generated key is " << identity->fpr << "\n";
roker@748
   228
roker@748
   229
    string key(identity->fpr);
roker@748
   230
    free_identity(identity);
roker@748
   231
roker@748
   232
    char *key_data;
roker@748
   233
    size_t size;
roker@748
   234
roker@748
   235
    cout << "export_key()\n\n";
roker@748
   236
    PEP_STATUS export_status = export_key(session, key.c_str(), &key_data, &size);
roker@748
   237
    cout << "export_key() exits with " << export_status << "\n";
roker@748
   238
    assert(export_status == PEP_STATUS_OK);
roker@748
   239
    cout << key_data << "\n\n";
roker@748
   240
roker@748
   241
    cout << "deleting key pair " << key.c_str() << "\n";
roker@748
   242
    PEP_STATUS delete_status = delete_keypair(session, key.c_str());
roker@748
   243
    cout << "delete_keypair() exits with " << delete_status << "\n";
roker@748
   244
    assert(delete_status == PEP_STATUS_OK);
roker@748
   245
    
roker@748
   246
    cout << "import_key()\n";
roker@748
   247
    PEP_STATUS import_status = import_key(session, key_data, size, NULL);
roker@748
   248
    assert(import_status == PEP_STATUS_OK);
roker@748
   249
    cout << "successfully imported key\n";
roker@748
   250
roker@748
   251
    pEp_free(key_data);
roker@748
   252
roker@748
   253
    cout << "deleting key " << key.c_str() << " again\n";
roker@748
   254
    delete_status = delete_keypair(session, key.c_str());
roker@748
   255
    cout << "delete_keypair() exits with " << delete_status << "\n";
roker@748
   256
    assert(delete_status == PEP_STATUS_OK);
roker@748
   257
roker@748
   258
    cout << "finding key for pep.test.john@pep-project.org\n";
roker@748
   259
    PEP_STATUS find_keys_status = find_keys(session, "pep.test.john@pep-project.org", &keylist);
roker@748
   260
    cout << "find_keys() exits with " << find_keys_status << "\n";
roker@748
   261
    assert(find_keys_status == PEP_STATUS_OK);
roker@748
   262
    assert(keylist);
roker@748
   263
    cout << "found: " << keylist->value << "\n";
roker@748
   264
    assert(keylist->next == NULL);
roker@748
   265
    free_stringlist(keylist);
roker@748
   266
roker@748
   267
    cout << "searching for vb@ulm.ccc.de on keyserver\n";
roker@748
   268
    PEP_STATUS recv_key_status = recv_key(session, "vb@ulm.ccc.de");
roker@748
   269
    cout << "recv_key() exits with " << recv_key_status << "\n";
roker@748
   270
    assert(recv_key_status == PEP_STATUS_OK);
roker@748
   271
roker@748
   272
    cout << "sending vb@ulm.ccc.de to keyserver\n";
roker@748
   273
    PEP_STATUS send_key_status = send_key(session, "vb@ulm.ccc.de");
roker@748
   274
    cout << "send_key() exits with " << send_key_status << "\n";
roker@748
   275
    assert(send_key_status == PEP_STATUS_OK);
roker@748
   276
roker@748
   277
    PEP_comm_type tcomm_type;
roker@748
   278
    PEP_STATUS tstatus = get_key_rating(session, "59BFF488C9C2EE39", &tcomm_type);
roker@748
   279
    cout << "get_key_rating() exits with " << tstatus << "\n";
roker@748
   280
    assert(tstatus == PEP_STATUS_OK);
roker@748
   281
    assert(tcomm_type == PEP_ct_OpenPGP_unconfirmed);
roker@748
   282
    
roker@748
   283
	cout << "\ncalling release()\n";
roker@748
   284
	release(session);
roker@748
   285
	return 0;
roker@748
   286
}