test/pEpEngineTest.cc
author Edouard Tisserant
Fri, 17 Apr 2015 01:17:59 +0200
changeset 206 550328f5c103
parent 185 f3142e40d9e7
child 234 6904a5742e86
permissions -rw-r--r--
test : ensure \0 at the end of buffers read from files
vb@0
     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@137
    65
    char * cipher_buffer = NULL;
Edouard@137
    66
    size_t cipher_length = 0;
Edouard@137
    67
    ReadFileIntoMem("msg.asc", cipher_buffer, cipher_length);
vb@0
    68
Edouard@137
    69
	cout << "\n" << cipher_buffer;
vb@0
    70
Edouard@137
    71
	char *buf_text = NULL;
Edouard@137
    72
	size_t buf_size = 0;
vb@0
    73
	stringlist_t *keylist;
vb@0
    74
vb@0
    75
    cout << "calling decrypt_and_verify()\n";
Edouard@137
    76
    PEP_STATUS decrypt_result = decrypt_and_verify(session, cipher_buffer, cipher_length, &buf_text, &buf_size, &keylist);
vb@0
    77
vb@0
    78
    cout << "returning from decrypt_and_verify() with result == " << decrypt_result << "\n";
vb@0
    79
    assert(decrypt_result == PEP_DECRYPTED_AND_VERIFIED);
vb@0
    80
    assert(buf_text);
vb@0
    81
    assert(keylist);
vb@0
    82
vb@0
    83
    for (stringlist_t *_keylist=keylist; _keylist!=NULL; _keylist=_keylist->next) {
vb@0
    84
        assert(_keylist->value);
vb@0
    85
        cout << "signed with " << _keylist->value << "\n";
vb@0
    86
    }
vb@0
    87
vb@0
    88
    free_stringlist(keylist);
vb@0
    89
    buf_text[buf_size] = 0;
vb@0
    90
    string plain(buf_text);
vb@0
    91
    pEp_free(buf_text);
vb@0
    92
    cout << "\n" << plain;
vb@0
    93
Edouard@137
    94
    char * t1_buffer = NULL;
Edouard@137
    95
    size_t t1_length = 0;
Edouard@137
    96
    ReadFileIntoMem("t1.txt", t1_buffer, t1_length);
vb@0
    97
Edouard@137
    98
    char * sig_buffer = NULL;
Edouard@137
    99
    size_t sig_length = 0;
Edouard@137
   100
    ReadFileIntoMem("signature.asc", sig_buffer, sig_length);
vb@0
   101
Edouard@185
   102
    cout << "\ncalling verify_text()\n";
Edouard@137
   103
    PEP_STATUS verify_result = verify_text(session, t1_buffer, t1_length, sig_buffer, sig_length, &keylist);
Edouard@185
   104
    cout << "returning from verify_text() with result == " << verify_result << "\n";
vb@0
   105
    assert(verify_result == PEP_VERIFIED || verify_result == PEP_VERIFIED_AND_TRUSTED);
vb@0
   106
    assert(keylist->value);
vb@0
   107
    cout << "signed with " << keylist->value << "\n";
vb@0
   108
    free_stringlist(keylist);
vb@0
   109
Edouard@137
   110
    char * t2_buffer = NULL;
Edouard@137
   111
    size_t t2_length = 0;
Edouard@137
   112
    ReadFileIntoMem("t2.txt", t2_buffer, t2_length);
vb@0
   113
Edouard@206
   114
    cout << "\ncalling verify_text()\n";
Edouard@137
   115
    verify_result = verify_text(session, t2_buffer, t2_length, sig_buffer, sig_length, &keylist);
vb@0
   116
    assert(verify_result == PEP_DECRYPT_SIGNATURE_DOES_NOT_MATCH);
vb@0
   117
    free_stringlist(keylist);
vb@0
   118
Edouard@137
   119
    keylist = new_stringlist("DD55BF29DF9B1541");
Edouard@137
   120
    //keylist = new_stringlist("49422235FC99585B891C66530C7B109BFA7261F7");
vb@9
   121
    // stringlist_add(keylist, "C6FAA231A2B43252B9526D119550C6B6B8B0FCD6");
vb@46
   122
    // stringlist_add(keylist, "5DC8CAC595EDAD6598DD4732DD55BF29DF9B1541");
vb@0
   123
Edouard@137
   124
	buf_text = NULL;
Edouard@137
   125
	buf_size = 0;
Edouard@137
   126
vb@0
   127
    cout << "\ncalling encrypt_and_sign()\n";
vb@0
   128
    PEP_STATUS encrypt_result = encrypt_and_sign(session, keylist, plain.c_str(), plain.length(), &buf_text, &buf_size);
vb@0
   129
    cout << "returning from encrypt_and_sign() with result == " << encrypt_result << "\n";
vb@0
   130
    assert(encrypt_result == PEP_STATUS_OK);
vb@0
   131
    free_stringlist(keylist);
vb@0
   132
vb@0
   133
    buf_text[buf_size] = 0;
vb@0
   134
    string cipher2(buf_text);
vb@0
   135
    cout << "\n" << cipher2;
vb@0
   136
    pEp_free(buf_text);
vb@0
   137
Edouard@137
   138
    delete[] cipher_buffer;
Edouard@137
   139
    delete[] t1_buffer;
Edouard@137
   140
    delete[] sig_buffer;
Edouard@137
   141
    delete[] t2_buffer;
Edouard@137
   142
vb@0
   143
	cout << "\nfinding English safeword for 2342...\n";
vb@0
   144
	char * word;
vb@0
   145
	size_t wsize;
vb@0
   146
	safeword(session, 2342, "en", &word, &wsize);
vb@0
   147
	assert(word);
vb@0
   148
	cout << "the safeword for 2342 is " << word << "\n";
vb@0
   149
    pEp_free(word);
vb@0
   150
vb@0
   151
    string fingerprint = "4942 2235 FC99 585B 891C  6653 0C7B 109B FA72 61F7";
vb@0
   152
    char * words;
vb@0
   153
vb@0
   154
    cout << "\nfinding German safewords for " << fingerprint << "...\n";
vb@0
   155
    safewords(session, fingerprint.c_str(), "de", &words, &wsize, 5);
vb@0
   156
    assert(words);
vb@0
   157
    cout << words << "\n";
vb@0
   158
    pEp_free(words);
vb@0
   159
vb@0
   160
	pEp_identity *identity;
vb@0
   161
vb@0
   162
    identity = new_identity(
vb@8
   163
            "leon.schumacher@digitalekho.com",
vb@8
   164
            "8BD08954C74D830EEFFB5DEB2682A17F7C87F73D",
vb@8
   165
            "23",
vb@8
   166
            "Leon Schumacher"
vb@0
   167
        );
vb@0
   168
	identity->comm_type = PEP_ct_pEp;
vb@0
   169
vb@0
   170
	cout << "\nsetting identity...\n";
vb@0
   171
	PEP_STATUS pep_set_result = set_identity(session, identity);
vb@0
   172
	assert(pep_set_result == PEP_STATUS_OK);
vb@0
   173
    free_identity(identity);
vb@0
   174
	get_identity(session, "leon.schumacher@digitalekho.com", &identity);
vb@0
   175
	assert(identity);
vb@0
   176
	cout << "set: " << identity->address << ", " << identity->fpr << ", " << identity->user_id << ", " << identity->username << "\n";
vb@8
   177
vb@8
   178
    PEP_STATUS get_trust_result = get_trust(session, identity);
vb@8
   179
    assert(get_trust_result == PEP_STATUS_OK);
vb@8
   180
    cout << "trust of " << identity->user_id << " is " << identity->comm_type << "\n";
vb@8
   181
vb@0
   182
    free_identity(identity);
vb@0
   183
vb@0
   184
    cout << "\ngenerating key for testuser\n";
vb@0
   185
    identity = new_identity(
vb@8
   186
            "testuser@pibit.ch",
vb@0
   187
            NULL,
vb@8
   188
            "423",
vb@8
   189
            "Alfred E. Neuman"
vb@0
   190
        );
vb@0
   191
    assert(identity);
vb@0
   192
    PEP_STATUS generate_status = generate_keypair(session, identity);
vb@0
   193
    cout << "generate_keypair() exits with " << generate_status << "\n";
vb@0
   194
    assert(generate_status == PEP_STATUS_OK);
vb@0
   195
    cout << "generated key is " << identity->fpr << "\n";
vb@0
   196
vb@0
   197
    string key(identity->fpr);
vb@0
   198
    free_identity(identity);
vb@0
   199
vb@0
   200
    char *key_data;
vb@0
   201
    size_t size;
vb@0
   202
vb@0
   203
    cout << "export_key()\n\n";
vb@0
   204
    PEP_STATUS export_status = export_key(session, key.c_str(), &key_data, &size);
vb@0
   205
    assert(export_status == PEP_STATUS_OK);
vb@0
   206
    cout << key_data << "\n\n";
vb@0
   207
vb@0
   208
    cout << "deleting key pair " << key.c_str() << "\n";
vb@0
   209
    PEP_STATUS delete_status = delete_keypair(session, key.c_str());
vb@0
   210
    cout << "delete_keypair() exits with " << delete_status << "\n";
vb@0
   211
    assert(delete_status == PEP_STATUS_OK);
vb@0
   212
    
vb@0
   213
    cout << "import_key()\n";
vb@0
   214
    PEP_STATUS import_status = import_key(session, key_data, size);
vb@0
   215
    assert(import_status == PEP_STATUS_OK);
vb@0
   216
    cout << "successfully imported key\n";
vb@0
   217
vb@0
   218
    pEp_free(key_data);
vb@0
   219
vb@0
   220
    cout << "deleting key " << key.c_str() << " again\n";
vb@0
   221
    delete_status = delete_keypair(session, key.c_str());
vb@0
   222
    cout << "delete_keypair() exits with " << delete_status << "\n";
vb@0
   223
    assert(delete_status == PEP_STATUS_OK);
vb@0
   224
vb@0
   225
    cout << "finding key for outlooktest@dingens.org\n";
vb@0
   226
    PEP_STATUS find_keys_status = find_keys(session, "outlooktest@dingens.org", &keylist);
vb@0
   227
    assert(find_keys_status == PEP_STATUS_OK);
vb@0
   228
    assert(keylist);
vb@0
   229
    cout << "found: " << keylist->value << "\n";
vb@0
   230
    assert(keylist->next == NULL);
vb@0
   231
    free_stringlist(keylist);
vb@0
   232
vb@0
   233
    cout << "searching for vb@ulm.ccc.de on keyserver\n";
vb@0
   234
    PEP_STATUS recv_key_status = recv_key(session, "vb@ulm.ccc.de");
vb@0
   235
    assert(recv_key_status == PEP_STATUS_OK);
vb@0
   236
vb@0
   237
    cout << "sending vb@ulm.ccc.de to keyserver\n";
vb@0
   238
    PEP_STATUS send_key_status = send_key(session, "vb@ulm.ccc.de");
vb@0
   239
    assert(recv_key_status == PEP_STATUS_OK);
vb@0
   240
vb@9
   241
    PEP_comm_type tcomm_type;
Edouard@137
   242
    PEP_STATUS tstatus = get_key_rating(session, "DD55BF29DF9B1541", &tcomm_type);
vb@9
   243
    assert(tstatus == PEP_STATUS_OK);
vb@9
   244
    assert(tcomm_type == PEP_ct_OpenPGP_unconfirmed);
vb@9
   245
    
vb@0
   246
	cout << "\ncalling release()\n";
vb@0
   247
	release(session);
vb@0
   248
	return 0;
vb@0
   249
}