test/pEpEngineTest.cc
author Roker <roker@pep-project.org>
Thu, 14 Jul 2016 11:32:06 +0200
branchroker-linux
changeset 875 f89fe299d827
parent 800 6564b62a26e5
child 1014 bcae7f935d40
permissions -rw-r--r--
less elements in test
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@799
    12
#include "../src/stringpair.h"
roker@748
    13
roker@751
    14
roker@748
    15
using namespace std;
roker@748
    16
roker@753
    17
typedef std::string Buffer;
roker@751
    18
roker@751
    19
// no C++11, yet? So do our own implementation:
roker@751
    20
namespace{
roker@751
    21
    std::string to_string(unsigned long u)
roker@751
    22
    {
roker@751
    23
        char buf[32];
roker@751
    24
        snprintf(buf,31, "%lu", u);
roker@751
    25
        return buf;
roker@751
    26
    }
roker@751
    27
}
roker@751
    28
roker@751
    29
roker@751
    30
Buffer ReadFileIntoMem(const char *fname){
roker@750
    31
    cout << "opening " << fname << " for reading\n";
roker@750
    32
    ifstream txtFile (fname, ifstream::binary);
roker@750
    33
    assert(txtFile.is_open());
roker@751
    34
    if (!txtFile)
roker@751
    35
    {
roker@751
    36
        throw std::runtime_error( "error: cannot open file \"" + std::string(fname) + "\"" );
roker@748
    37
    }
roker@748
    38
roker@751
    39
    Buffer buffer;
roker@751
    40
roker@751
    41
    // get length of file:
roker@751
    42
    txtFile.seekg (0, txtFile.end);
roker@751
    43
    const size_t length = txtFile.tellg();
roker@751
    44
    txtFile.seekg (0, txtFile.beg);
roker@753
    45
    buffer.resize(length);
roker@751
    46
roker@751
    47
    cout << "Reading " << length << " characters... ";
roker@753
    48
    txtFile.read (&buffer[0], length);
roker@751
    49
roker@751
    50
    if (!txtFile)
roker@751
    51
    {
roker@751
    52
        throw std::runtime_error( "error: only " + to_string(txtFile.gcount()) + " could be read from file" + fname );
roker@751
    53
    }
roker@751
    54
roker@751
    55
    cout << "all characters read successfully." << std::endl;
roker@751
    56
    return buffer;
roker@748
    57
}
roker@748
    58
roker@750
    59
roker@799
    60
int stringlist_test()
roker@799
    61
{
roker@799
    62
	char stringkey[32] = {0};
roker@799
    63
	char stringvalue[32] = {0};
roker@799
    64
roker@875
    65
	for(double len=1.0; len<1E5; len=(len*3.8251)+1)
roker@799
    66
	{
roker@799
    67
		const unsigned ulen = unsigned(len);
roker@799
    68
		std::cout << "Stringpairlist length=" << ulen << ": " << std::endl;
roker@799
    69
		stringpair_list_t* sl = new_stringpair_list(nullptr);
roker@799
    70
		for(unsigned n=0; n<ulen; ++n)
roker@799
    71
		{
roker@799
    72
			snprintf(stringkey,63, "%x %x %a", n, ulen, len);
roker@799
    73
			snprintf(stringvalue,63, "%u %u %a", n, ulen, len);
roker@799
    74
			//std::cout << "\r\t\"" << stringvalue << "\" " << std::flush;
roker@799
    75
			auto sp = new_stringpair( stringkey, stringvalue);
roker@799
    76
			auto ret = stringpair_list_add(sl, sp);
roker@799
    77
			if(ret==NULL)
roker@799
    78
			{
roker@799
    79
				std::cout << " OUT OF MEMORY! n=" << n  << std::endl;
roker@799
    80
				return -1;
roker@799
    81
			}
roker@799
    82
		}
roker@799
    83
		std::cout << "\tfree_stringpairlist()..." << std::flush;
roker@799
    84
		free_stringpair_list(sl);
roker@799
    85
		std::cout << std::endl;
roker@799
    86
	}
roker@799
    87
	return 0;
roker@799
    88
}
roker@799
    89
roker@799
    90
roker@748
    91
int main(int argc, char* argv[])
roker@748
    92
{
roker@750
    93
    PEP_SESSION session;
roker@748
    94
roker@750
    95
    cout << "calling init()\n";
roker@750
    96
    PEP_STATUS init_result = init(&session);
roker@750
    97
    
roker@748
    98
    cout << "returning from init() with result == " << init_result << "\n";
roker@750
    99
    assert(init_result == PEP_STATUS_OK);
roker@748
   100
roker@799
   101
	stringlist_test();
roker@799
   102
roker@748
   103
    PEP_SESSION second_session;
roker@748
   104
    cout << "second session test\n";
roker@748
   105
    PEP_STATUS second_init_result = init(&second_session);
roker@750
   106
    cout << "returning from second init() with result == " << second_init_result << "\n";
roker@748
   107
    assert(second_init_result == PEP_STATUS_OK);
roker@748
   108
    assert(second_session);
roker@748
   109
    cout << "dropping second session\n";
roker@750
   110
    release(second_session);
roker@748
   111
roker@750
   112
    cout << "logging test\n";
roker@750
   113
    log_event(session, "log test", "pEp Enginge Test", "This is a logging test sample.", "please ignore this line");
roker@748
   114
roker@748
   115
    // Our test user :
roker@748
   116
    // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
roker@748
   117
    //         6FF00E97
roker@748
   118
    // A9411D176FF00E97
roker@748
   119
    // 
roker@748
   120
    // Other peers :
roker@748
   121
    // pEp Test Bob (test key, don't use) <pep.test.bob@pep-project.org> 
roker@748
   122
    //         C9C2EE39
roker@748
   123
    // 59BFF488C9C2EE39
roker@748
   124
    // 
roker@748
   125
    // pEp Test John (test key, don't use) <pep.test.john@pep-project.org>
roker@748
   126
    //         70DCF575
roker@748
   127
    // 135CD6D170DCF575
roker@748
   128
roker@748
   129
    const char *kflist[] = {
roker@748
   130
        "0x6FF00E97.asc",
roker@748
   131
        "0xC9C2EE39.asc",
roker@748
   132
        "0x70DCF575.asc",
roker@748
   133
        NULL
roker@748
   134
    };
roker@750
   135
roker@748
   136
    const char** kf = kflist;
roker@748
   137
    while(*kf){
roker@751
   138
        const Buffer k_user_buffer =  ReadFileIntoMem(*kf);
roker@748
   139
        cout << "import_key(" << *kf << ")\n";
roker@752
   140
        PEP_STATUS import_status = import_key(session, k_user_buffer.data(), k_user_buffer.size(), NULL);
roker@748
   141
        assert(import_status == PEP_STATUS_OK);
roker@748
   142
        cout << "successfully imported key\n";
roker@748
   143
        kf++;
roker@748
   144
    }
roker@748
   145
roker@752
   146
    const Buffer cipher_buffer = ReadFileIntoMem("msg.asc");
roker@751
   147
    cout << "\n" << cipher_buffer.data();
roker@748
   148
roker@750
   149
    char *buf_text = NULL;
roker@750
   150
    size_t buf_size = 0;
roker@750
   151
    stringlist_t *keylist;
roker@748
   152
roker@748
   153
    cout << "calling decrypt_and_verify()\n";
roker@752
   154
    PEP_STATUS decrypt_result = decrypt_and_verify(session, cipher_buffer.data(), cipher_buffer.size(), &buf_text, &buf_size, &keylist);
roker@748
   155
roker@748
   156
    cout << "returning from decrypt_and_verify() with result == 0x" << std::hex << decrypt_result << "\n";
roker@748
   157
    assert(decrypt_result == PEP_DECRYPTED_AND_VERIFIED);
roker@748
   158
    assert(buf_text);
roker@748
   159
    assert(keylist);
roker@748
   160
roker@748
   161
    for (stringlist_t *_keylist=keylist; _keylist!=NULL; _keylist=_keylist->next) {
roker@748
   162
        assert(_keylist->value);
roker@748
   163
        cout << "signed with " << _keylist->value << "\n";
roker@748
   164
    }
roker@748
   165
roker@748
   166
    free_stringlist(keylist);
roker@748
   167
    buf_text[buf_size] = 0;
roker@752
   168
    const string plain(buf_text);
roker@748
   169
    pEp_free(buf_text);
roker@748
   170
    cout << "\n" << plain;
roker@748
   171
roker@752
   172
    const Buffer t1_buffer = ReadFileIntoMem("t1.txt");
roker@752
   173
    const Buffer sig_buffer = ReadFileIntoMem("signature.asc");
roker@748
   174
roker@748
   175
    cout << "\ncalling verify_text()\n";
roker@752
   176
    PEP_STATUS verify_result = verify_text(session, t1_buffer.data(), t1_buffer.size(), sig_buffer.data(), sig_buffer.size(), &keylist);
roker@748
   177
    cout << "returning from verify_text() with result == " << verify_result << "\n";
roker@748
   178
    assert(verify_result == PEP_VERIFIED || verify_result == PEP_VERIFIED_AND_TRUSTED);
roker@748
   179
    assert(keylist->value);
roker@748
   180
    cout << "signed with " << keylist->value << "\n";
roker@748
   181
    free_stringlist(keylist);
roker@748
   182
roker@752
   183
    const Buffer t2_buffer = ReadFileIntoMem("t2.txt");
roker@748
   184
roker@748
   185
    cout << "\ncalling verify_text()\n";
roker@752
   186
    verify_result = verify_text(session, t2_buffer.data(), t2_buffer.size(), sig_buffer.data(), sig_buffer.size(), &keylist);
roker@748
   187
    assert(verify_result == PEP_DECRYPT_SIGNATURE_DOES_NOT_MATCH);
roker@748
   188
    free_stringlist(keylist);
roker@748
   189
roker@748
   190
    keylist = new_stringlist("A9411D176FF00E97");
roker@748
   191
    stringlist_add(keylist, "59BFF488C9C2EE39");
roker@748
   192
    stringlist_add(keylist, "135CD6D170DCF575");
roker@748
   193
roker@750
   194
    buf_text = NULL;
roker@750
   195
    buf_size = 0;
roker@748
   196
roker@748
   197
    cout << "\ncalling encrypt_and_sign()\n";
roker@748
   198
    PEP_STATUS encrypt_result = encrypt_and_sign(session, keylist, plain.c_str(), plain.length(), &buf_text, &buf_size);
roker@748
   199
    cout << "returning from encrypt_and_sign() with result == " << encrypt_result << "\n";
roker@748
   200
    assert(encrypt_result == PEP_STATUS_OK);
roker@748
   201
    free_stringlist(keylist);
roker@748
   202
roker@752
   203
    buf_text[buf_size] = '\0';
roker@752
   204
    const string cipher2(buf_text);
roker@748
   205
    cout << "\n" << cipher2;
roker@748
   206
    pEp_free(buf_text);
roker@748
   207
roker@750
   208
    cout << "\nfinding English trustword for 2342...\n";
roker@752
   209
    char * word = NULL;
roker@750
   210
    size_t wsize;
roker@750
   211
    trustword(session, 2342, "en", &word, &wsize);
roker@750
   212
    assert(word);
Edouard@791
   213
    cout << "the English trustword for 2342 is " << word << "\n";
Edouard@791
   214
    pEp_free(word);
Edouard@791
   215
    cout << "\nfinding French trustword for 2342...\n";
Edouard@791
   216
    trustword(session, 2342, "fr", &word, &wsize);
Edouard@791
   217
    assert(word);
Edouard@791
   218
    cout << "the French trustword for 2342 is " << word << "\n";
roker@748
   219
    pEp_free(word);
roker@748
   220
roker@752
   221
    const string fingerprint = "4942 2235 FC99 585B 891C  6653 0C7B 109B FA72 61F7";
roker@752
   222
    char * words = NULL;
roker@748
   223
roker@748
   224
    cout << "\nfinding German trustwords for " << fingerprint << "...\n";
roker@748
   225
    trustwords(session, fingerprint.c_str(), "de", &words, &wsize, 5);
roker@748
   226
    assert(words);
roker@748
   227
    cout << words << "\n";
roker@748
   228
    pEp_free(words);
roker@748
   229
roker@751
   230
    pEp_identity* identity  = new_identity(
roker@748
   231
            "leon.schumacher@digitalekho.com",
roker@748
   232
            "8BD08954C74D830EEFFB5DEB2682A17F7C87F73D",
roker@748
   233
            "23",
roker@748
   234
            "Leon Schumacher"
roker@748
   235
        );
roker@750
   236
    identity->comm_type = PEP_ct_pEp;
roker@748
   237
roker@750
   238
    cout << "\nsetting identity...\n";
roker@750
   239
    PEP_STATUS pep_set_result = set_identity(session, identity);
roker@750
   240
    assert(pep_set_result == PEP_STATUS_OK);
roker@748
   241
    free_identity(identity);
roker@750
   242
    get_identity(session, "leon.schumacher@digitalekho.com", "23", &identity);
roker@750
   243
    assert(identity);
roker@750
   244
    cout << "set: " << identity->address << ", " << identity->fpr << ", " << identity->user_id << ", " << identity->username << "\n";
roker@748
   245
roker@748
   246
    PEP_STATUS get_trust_result = get_trust(session, identity);
roker@748
   247
    assert(get_trust_result == PEP_STATUS_OK);
roker@748
   248
    cout << "trust of " << identity->user_id << " is " << identity->comm_type << "\n";
roker@748
   249
roker@748
   250
    free_identity(identity);
roker@748
   251
roker@748
   252
    cout << "\ngenerating key for testuser\n";
roker@748
   253
    identity = new_identity(
roker@748
   254
            "testuser@pibit.ch",
roker@748
   255
            NULL,
roker@748
   256
            "423",
roker@748
   257
            "Alfred E. Neuman"
roker@748
   258
        );
roker@750
   259
roker@748
   260
    assert(identity);
roker@748
   261
    PEP_STATUS generate_status = generate_keypair(session, identity);
roker@748
   262
    cout << "generate_keypair() exits with " << generate_status << "\n";
roker@748
   263
    assert(generate_status == PEP_STATUS_OK);
roker@748
   264
    cout << "generated key is " << identity->fpr << "\n";
roker@748
   265
roker@752
   266
    const string key(identity->fpr);
roker@748
   267
    free_identity(identity);
roker@748
   268
roker@752
   269
    char *key_data = NULL;
roker@752
   270
    size_t size = 0;
roker@748
   271
roker@748
   272
    cout << "export_key()\n\n";
roker@748
   273
    PEP_STATUS export_status = export_key(session, key.c_str(), &key_data, &size);
roker@748
   274
    cout << "export_key() exits with " << export_status << "\n";
roker@748
   275
    assert(export_status == PEP_STATUS_OK);
roker@748
   276
    cout << key_data << "\n\n";
roker@748
   277
roker@748
   278
    cout << "deleting key pair " << key.c_str() << "\n";
roker@748
   279
    PEP_STATUS delete_status = delete_keypair(session, key.c_str());
roker@748
   280
    cout << "delete_keypair() exits with " << delete_status << "\n";
roker@748
   281
    assert(delete_status == PEP_STATUS_OK);
roker@748
   282
    
roker@748
   283
    cout << "import_key()\n";
roker@748
   284
    PEP_STATUS import_status = import_key(session, key_data, size, NULL);
roker@748
   285
    assert(import_status == PEP_STATUS_OK);
roker@748
   286
    cout << "successfully imported key\n";
roker@748
   287
roker@748
   288
    pEp_free(key_data);
roker@752
   289
    key_data=NULL;
roker@748
   290
roker@748
   291
    cout << "deleting key " << key.c_str() << " again\n";
roker@748
   292
    delete_status = delete_keypair(session, key.c_str());
roker@748
   293
    cout << "delete_keypair() exits with " << delete_status << "\n";
roker@748
   294
    assert(delete_status == PEP_STATUS_OK);
roker@748
   295
roker@748
   296
    cout << "finding key for pep.test.john@pep-project.org\n";
roker@748
   297
    PEP_STATUS find_keys_status = find_keys(session, "pep.test.john@pep-project.org", &keylist);
roker@748
   298
    cout << "find_keys() exits with " << find_keys_status << "\n";
roker@748
   299
    assert(find_keys_status == PEP_STATUS_OK);
roker@748
   300
    assert(keylist);
roker@748
   301
    cout << "found: " << keylist->value << "\n";
roker@748
   302
    assert(keylist->next == NULL);
roker@748
   303
    free_stringlist(keylist);
roker@748
   304
roker@748
   305
    cout << "searching for vb@ulm.ccc.de on keyserver\n";
roker@748
   306
    PEP_STATUS recv_key_status = recv_key(session, "vb@ulm.ccc.de");
roker@748
   307
    cout << "recv_key() exits with " << recv_key_status << "\n";
roker@748
   308
    assert(recv_key_status == PEP_STATUS_OK);
roker@748
   309
roker@748
   310
    cout << "sending vb@ulm.ccc.de to keyserver\n";
roker@748
   311
    PEP_STATUS send_key_status = send_key(session, "vb@ulm.ccc.de");
roker@748
   312
    cout << "send_key() exits with " << send_key_status << "\n";
roker@748
   313
    assert(send_key_status == PEP_STATUS_OK);
roker@748
   314
roker@748
   315
    PEP_comm_type tcomm_type;
roker@748
   316
    PEP_STATUS tstatus = get_key_rating(session, "59BFF488C9C2EE39", &tcomm_type);
roker@748
   317
    cout << "get_key_rating() exits with " << tstatus << "\n";
roker@748
   318
    assert(tstatus == PEP_STATUS_OK);
roker@748
   319
    assert(tcomm_type == PEP_ct_OpenPGP_unconfirmed);
roker@748
   320
    
roker@750
   321
    cout << "\ncalling release()\n";
roker@750
   322
    release(session);
roker@750
   323
    return 0;
roker@748
   324
}