test/pEpEngineTest.cc
author vb
Wed, 25 Jun 2014 18:44:58 +0200
changeset 0 16f27efbef98
child 8 26cc9f0228f4
permissions -rw-r--r--
initial commit
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
#ifdef _WIN32
vb@0
    12
#define strdup _strdup
vb@0
    13
#endif
vb@0
    14
vb@0
    15
using namespace std;
vb@0
    16
vb@0
    17
int main(int argc, char* argv[])
vb@0
    18
{
vb@0
    19
	PEP_SESSION session;
vb@0
    20
vb@0
    21
	cout << "calling init()\n";
vb@0
    22
	PEP_STATUS init_result = init(&session);
vb@0
    23
	
vb@0
    24
    cout << "returning from init() with result == " << init_result << "\n";
vb@0
    25
	assert(init_result == PEP_STATUS_OK);
vb@0
    26
vb@0
    27
    PEP_SESSION second_session;
vb@0
    28
    cout << "second session test\n";
vb@0
    29
    PEP_STATUS second_init_result = init(&second_session);
vb@0
    30
	cout << "returning from second init() with result == " << second_init_result << "\n";
vb@0
    31
    assert(second_init_result == PEP_STATUS_OK);
vb@0
    32
    assert(second_session);
vb@0
    33
    cout << "dropping second session\n";
vb@0
    34
	release(second_session);
vb@0
    35
vb@0
    36
	cout << "logging test\n";
vb@0
    37
	log_event(session, "log test", "pEp Enginge Test", "This is a logging test sample.", "please ignore this line");
vb@0
    38
vb@0
    39
	string cipher;
vb@0
    40
vb@0
    41
	cout << "opening msc.asc for reading\n";
vb@0
    42
	ifstream inFile ("msg.asc");
vb@0
    43
	assert(inFile.is_open());
vb@0
    44
vb@0
    45
	cout << "reading cipher text of msc.asc\n";
vb@0
    46
	while (!inFile.eof()) {
vb@0
    47
		static string line;
vb@0
    48
		getline(inFile, line);
vb@0
    49
		cipher += line + "\n";
vb@0
    50
	}
vb@0
    51
	inFile.close();
vb@0
    52
vb@0
    53
	cout << "\n" << cipher;
vb@0
    54
vb@0
    55
	char *buf_text;
vb@0
    56
	size_t buf_size;
vb@0
    57
	stringlist_t *keylist;
vb@0
    58
vb@0
    59
    cout << "calling decrypt_and_verify()\n";
vb@0
    60
    PEP_STATUS decrypt_result = decrypt_and_verify(session, cipher.c_str(), cipher.length(), &buf_text, &buf_size, &keylist);
vb@0
    61
vb@0
    62
    cout << "returning from decrypt_and_verify() with result == " << decrypt_result << "\n";
vb@0
    63
    assert(decrypt_result == PEP_DECRYPTED_AND_VERIFIED);
vb@0
    64
    assert(buf_text);
vb@0
    65
    assert(keylist);
vb@0
    66
vb@0
    67
    for (stringlist_t *_keylist=keylist; _keylist!=NULL; _keylist=_keylist->next) {
vb@0
    68
        assert(_keylist->value);
vb@0
    69
        cout << "signed with " << _keylist->value << "\n";
vb@0
    70
    }
vb@0
    71
vb@0
    72
    free_stringlist(keylist);
vb@0
    73
    buf_text[buf_size] = 0;
vb@0
    74
    string plain(buf_text);
vb@0
    75
    pEp_free(buf_text);
vb@0
    76
    cout << "\n" << plain;
vb@0
    77
vb@0
    78
    string t1, t2, sig;
vb@0
    79
vb@0
    80
	cout << "\nopening t1.txt for reading\n";
vb@0
    81
	ifstream txtFile ("t1.txt");
vb@0
    82
	assert(txtFile.is_open());
vb@0
    83
vb@0
    84
	cout << "reading t1 from t1.txt\n";
vb@0
    85
	while (!txtFile.eof()) {
vb@0
    86
		static string line;
vb@0
    87
		getline(txtFile, line);
vb@0
    88
		t1 += line + "\r\n";
vb@0
    89
	}
vb@0
    90
	txtFile.close();
vb@0
    91
    assert(t1.size());
vb@0
    92
    t1.erase(t1.size()-2, 2);
vb@0
    93
vb@0
    94
	cout << "opening signature.asc for reading\n";
vb@0
    95
	ifstream sigFile ("signature.asc");
vb@0
    96
	assert(sigFile.is_open());
vb@0
    97
vb@0
    98
	cout << "reading sig from signature.asc\n";
vb@0
    99
	while (!sigFile.eof()) {
vb@0
   100
		static string line;
vb@0
   101
		getline(sigFile, line);
vb@0
   102
		sig += line + "\n";
vb@0
   103
	}
vb@0
   104
	sigFile.close();
vb@0
   105
vb@0
   106
    cout << "\ncalling verify_test()\n";
vb@0
   107
    PEP_STATUS verify_result = verify_text(session, t1.c_str(), t1.size(), sig.c_str(), sig.size(), &keylist);
vb@0
   108
    cout << "result = " << verify_result << "\n";
vb@0
   109
    assert(verify_result == PEP_VERIFIED || verify_result == PEP_VERIFIED_AND_TRUSTED);
vb@0
   110
    assert(keylist->value);
vb@0
   111
    cout << "signed with " << keylist->value << "\n";
vb@0
   112
    free_stringlist(keylist);
vb@0
   113
vb@0
   114
	cout << "\nopening t2.txt for reading\n";
vb@0
   115
	ifstream txt2File ("t2.txt");
vb@0
   116
	assert(txt2File.is_open());
vb@0
   117
vb@0
   118
	cout << "reading t2 from t2.txt\n";
vb@0
   119
	while (!txt2File.eof()) {
vb@0
   120
		static string line;
vb@0
   121
		getline(txt2File, line);
vb@0
   122
		t2 += line + "\r\n";
vb@0
   123
	}
vb@0
   124
	txt2File.close();
vb@0
   125
    assert(t2.size());
vb@0
   126
    t1.erase(t2.size()-2, 2);
vb@0
   127
vb@0
   128
    cout << "\ncalling verify_test()\n";
vb@0
   129
    verify_result = verify_text(session, t2.c_str(), t2.size(), sig.c_str(), sig.size(), &keylist);
vb@0
   130
    cout << "result = " << verify_result << "\n";
vb@0
   131
    assert(verify_result == PEP_DECRYPT_SIGNATURE_DOES_NOT_MATCH);
vb@0
   132
    free_stringlist(keylist);
vb@0
   133
vb@0
   134
    keylist = new_stringlist("FA7261F7");
vb@0
   135
vb@0
   136
    cout << "\ncalling encrypt_and_sign()\n";
vb@0
   137
    PEP_STATUS encrypt_result = encrypt_and_sign(session, keylist, plain.c_str(), plain.length(), &buf_text, &buf_size);
vb@0
   138
    
vb@0
   139
    cout << "returning from encrypt_and_sign() with result == " << encrypt_result << "\n";
vb@0
   140
    assert(encrypt_result == PEP_STATUS_OK);
vb@0
   141
    free_stringlist(keylist);
vb@0
   142
vb@0
   143
    buf_text[buf_size] = 0;
vb@0
   144
    string cipher2(buf_text);
vb@0
   145
    cout << "\n" << cipher2;
vb@0
   146
    pEp_free(buf_text);
vb@0
   147
vb@0
   148
	cout << "\nfinding English safeword for 2342...\n";
vb@0
   149
	char * word;
vb@0
   150
	size_t wsize;
vb@0
   151
	safeword(session, 2342, "en", &word, &wsize);
vb@0
   152
	assert(word);
vb@0
   153
	cout << "the safeword for 2342 is " << word << "\n";
vb@0
   154
    pEp_free(word);
vb@0
   155
vb@0
   156
    string fingerprint = "4942 2235 FC99 585B 891C  6653 0C7B 109B FA72 61F7";
vb@0
   157
    char * words;
vb@0
   158
vb@0
   159
    cout << "\nfinding German safewords for " << fingerprint << "...\n";
vb@0
   160
    safewords(session, fingerprint.c_str(), "de", &words, &wsize, 5);
vb@0
   161
    assert(words);
vb@0
   162
    cout << words << "\n";
vb@0
   163
    pEp_free(words);
vb@0
   164
vb@0
   165
	pEp_identity *identity;
vb@0
   166
vb@0
   167
    identity = new_identity(
vb@0
   168
            strdup("leon.schumacher@digitalekho.com"),
vb@0
   169
            strdup("8BD08954C74D830EEFFB5DEB2682A17F7C87F73D"),
vb@0
   170
            strdup("23"),
vb@0
   171
            strdup("Leon Schumacher")
vb@0
   172
        );
vb@0
   173
	identity->comm_type = PEP_ct_pEp;
vb@0
   174
vb@0
   175
	cout << "\nsetting identity...\n";
vb@0
   176
	PEP_STATUS pep_set_result = set_identity(session, identity);
vb@0
   177
	assert(pep_set_result == PEP_STATUS_OK);
vb@0
   178
    free_identity(identity);
vb@0
   179
	get_identity(session, "leon.schumacher@digitalekho.com", &identity);
vb@0
   180
	assert(identity);
vb@0
   181
	cout << "set: " << identity->address << ", " << identity->fpr << ", " << identity->user_id << ", " << identity->username << "\n";
vb@0
   182
    free_identity(identity);
vb@0
   183
vb@0
   184
    stringlist_t *addresses = new_stringlist("leon.schumacher@digitalekho.com");
vb@0
   185
    PEP_comm_type comm_type;
vb@0
   186
    cout << "\nretrieving communication type for leon.schumacher@digitalekho.com\n";
vb@0
   187
    PEP_STATUS oct_result = outgoing_comm_type(session, addresses, &comm_type);
vb@0
   188
    cout << "communication type is " << comm_type << "\n";
vb@0
   189
    free_stringlist(addresses);
vb@0
   190
    assert(oct_result == PEP_STATUS_OK && comm_type == PEP_ct_pEp);
vb@0
   191
vb@0
   192
    stringlist_t *addresses2 = new_stringlist("leon.schumacher@digitalekho.com");
vb@0
   193
    stringlist_add(addresses2, "this.email@is.invalid");
vb@0
   194
    cout << "\nretrieving communication type for an unknown address\n";
vb@0
   195
    oct_result = outgoing_comm_type(session, addresses2, &comm_type);
vb@0
   196
    cout << "communication type is " << comm_type << "\n";
vb@0
   197
    cout << "status is " << oct_result << "\n";
vb@0
   198
    free_stringlist(addresses2);
vb@0
   199
    assert(oct_result == PEP_STATUS_OK && comm_type == PEP_ct_no_encryption);
vb@0
   200
vb@0
   201
    cout << "\ngenerating key for testuser\n";
vb@0
   202
    identity = new_identity(
vb@0
   203
            strdup("testuser@pibit.ch"),
vb@0
   204
            NULL,
vb@0
   205
            strdup("423"),
vb@0
   206
            strdup("Alfred E. Neuman")
vb@0
   207
        );
vb@0
   208
    assert(identity);
vb@0
   209
    PEP_STATUS generate_status = generate_keypair(session, identity);
vb@0
   210
    cout << "generate_keypair() exits with " << generate_status << "\n";
vb@0
   211
    assert(generate_status == PEP_STATUS_OK);
vb@0
   212
    cout << "generated key is " << identity->fpr << "\n";
vb@0
   213
vb@0
   214
    string key(identity->fpr);
vb@0
   215
    free_identity(identity);
vb@0
   216
vb@0
   217
    char *key_data;
vb@0
   218
    size_t size;
vb@0
   219
vb@0
   220
    cout << "export_key()\n\n";
vb@0
   221
    PEP_STATUS export_status = export_key(session, key.c_str(), &key_data, &size);
vb@0
   222
    assert(export_status == PEP_STATUS_OK);
vb@0
   223
    cout << key_data << "\n\n";
vb@0
   224
vb@0
   225
    cout << "deleting key pair " << key.c_str() << "\n";
vb@0
   226
    PEP_STATUS delete_status = delete_keypair(session, key.c_str());
vb@0
   227
    cout << "delete_keypair() exits with " << delete_status << "\n";
vb@0
   228
    assert(delete_status == PEP_STATUS_OK);
vb@0
   229
    
vb@0
   230
    cout << "import_key()\n";
vb@0
   231
    PEP_STATUS import_status = import_key(session, key_data, size);
vb@0
   232
    assert(import_status == PEP_STATUS_OK);
vb@0
   233
    cout << "successfully imported key\n";
vb@0
   234
vb@0
   235
    pEp_free(key_data);
vb@0
   236
vb@0
   237
    cout << "deleting key " << key.c_str() << " again\n";
vb@0
   238
    delete_status = delete_keypair(session, key.c_str());
vb@0
   239
    cout << "delete_keypair() exits with " << delete_status << "\n";
vb@0
   240
    assert(delete_status == PEP_STATUS_OK);
vb@0
   241
vb@0
   242
    cout << "finding key for outlooktest@dingens.org\n";
vb@0
   243
    PEP_STATUS find_keys_status = find_keys(session, "outlooktest@dingens.org", &keylist);
vb@0
   244
    assert(find_keys_status == PEP_STATUS_OK);
vb@0
   245
    assert(keylist);
vb@0
   246
    cout << "found: " << keylist->value << "\n";
vb@0
   247
    assert(keylist->next == NULL);
vb@0
   248
    free_stringlist(keylist);
vb@0
   249
vb@0
   250
    cout << "searching for vb@ulm.ccc.de on keyserver\n";
vb@0
   251
    PEP_STATUS recv_key_status = recv_key(session, "vb@ulm.ccc.de");
vb@0
   252
    assert(recv_key_status == PEP_STATUS_OK);
vb@0
   253
vb@0
   254
    cout << "sending vb@ulm.ccc.de to keyserver\n";
vb@0
   255
    PEP_STATUS send_key_status = send_key(session, "vb@ulm.ccc.de");
vb@0
   256
    assert(recv_key_status == PEP_STATUS_OK);
vb@0
   257
vb@0
   258
	cout << "\ncalling release()\n";
vb@0
   259
	release(session);
vb@0
   260
	return 0;
vb@0
   261
}