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