test/pEpEngineTest.cc
author vb
Tue, 30 Dec 2014 13:04:17 +0100
changeset 39 66b5cc6cb987
parent 26 843e629627bf
child 46 7471e31bb278
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@26
    23
    /*
vb@0
    24
    PEP_SESSION second_session;
vb@0
    25
    cout << "second session test\n";
vb@0
    26
    PEP_STATUS second_init_result = init(&second_session);
vb@0
    27
	cout << "returning from second init() with result == " << second_init_result << "\n";
vb@0
    28
    assert(second_init_result == PEP_STATUS_OK);
vb@0
    29
    assert(second_session);
vb@0
    30
    cout << "dropping second session\n";
vb@0
    31
	release(second_session);
vb@26
    32
    */
vb@0
    33
vb@0
    34
	cout << "logging test\n";
vb@0
    35
	log_event(session, "log test", "pEp Enginge Test", "This is a logging test sample.", "please ignore this line");
vb@0
    36
vb@0
    37
	string cipher;
vb@0
    38
vb@0
    39
	cout << "opening msc.asc for reading\n";
vb@0
    40
	ifstream inFile ("msg.asc");
vb@0
    41
	assert(inFile.is_open());
vb@0
    42
vb@0
    43
	cout << "reading cipher text of msc.asc\n";
vb@0
    44
	while (!inFile.eof()) {
vb@0
    45
		static string line;
vb@0
    46
		getline(inFile, line);
vb@0
    47
		cipher += line + "\n";
vb@0
    48
	}
vb@0
    49
	inFile.close();
vb@0
    50
vb@0
    51
	cout << "\n" << cipher;
vb@0
    52
vb@0
    53
	char *buf_text;
vb@0
    54
	size_t buf_size;
vb@0
    55
	stringlist_t *keylist;
vb@0
    56
vb@0
    57
    cout << "calling decrypt_and_verify()\n";
vb@0
    58
    PEP_STATUS decrypt_result = decrypt_and_verify(session, cipher.c_str(), cipher.length(), &buf_text, &buf_size, &keylist);
vb@0
    59
vb@0
    60
    cout << "returning from decrypt_and_verify() with result == " << decrypt_result << "\n";
vb@0
    61
    assert(decrypt_result == PEP_DECRYPTED_AND_VERIFIED);
vb@0
    62
    assert(buf_text);
vb@0
    63
    assert(keylist);
vb@0
    64
vb@0
    65
    for (stringlist_t *_keylist=keylist; _keylist!=NULL; _keylist=_keylist->next) {
vb@0
    66
        assert(_keylist->value);
vb@0
    67
        cout << "signed with " << _keylist->value << "\n";
vb@0
    68
    }
vb@0
    69
vb@0
    70
    free_stringlist(keylist);
vb@0
    71
    buf_text[buf_size] = 0;
vb@0
    72
    string plain(buf_text);
vb@0
    73
    pEp_free(buf_text);
vb@0
    74
    cout << "\n" << plain;
vb@0
    75
vb@0
    76
    string t1, t2, sig;
vb@0
    77
vb@0
    78
	cout << "\nopening t1.txt for reading\n";
vb@0
    79
	ifstream txtFile ("t1.txt");
vb@0
    80
	assert(txtFile.is_open());
vb@0
    81
vb@0
    82
	cout << "reading t1 from t1.txt\n";
vb@0
    83
	while (!txtFile.eof()) {
vb@0
    84
		static string line;
vb@0
    85
		getline(txtFile, line);
vb@0
    86
		t1 += line + "\r\n";
vb@0
    87
	}
vb@0
    88
	txtFile.close();
vb@0
    89
    assert(t1.size());
vb@0
    90
    t1.erase(t1.size()-2, 2);
vb@0
    91
vb@0
    92
	cout << "opening signature.asc for reading\n";
vb@0
    93
	ifstream sigFile ("signature.asc");
vb@0
    94
	assert(sigFile.is_open());
vb@0
    95
vb@0
    96
	cout << "reading sig from signature.asc\n";
vb@0
    97
	while (!sigFile.eof()) {
vb@0
    98
		static string line;
vb@0
    99
		getline(sigFile, line);
vb@0
   100
		sig += line + "\n";
vb@0
   101
	}
vb@0
   102
	sigFile.close();
vb@0
   103
vb@0
   104
    cout << "\ncalling verify_test()\n";
vb@0
   105
    PEP_STATUS verify_result = verify_text(session, t1.c_str(), t1.size(), sig.c_str(), sig.size(), &keylist);
vb@0
   106
    cout << "result = " << verify_result << "\n";
vb@0
   107
    assert(verify_result == PEP_VERIFIED || verify_result == PEP_VERIFIED_AND_TRUSTED);
vb@0
   108
    assert(keylist->value);
vb@0
   109
    cout << "signed with " << keylist->value << "\n";
vb@0
   110
    free_stringlist(keylist);
vb@0
   111
vb@0
   112
	cout << "\nopening t2.txt for reading\n";
vb@0
   113
	ifstream txt2File ("t2.txt");
vb@0
   114
	assert(txt2File.is_open());
vb@0
   115
vb@0
   116
	cout << "reading t2 from t2.txt\n";
vb@0
   117
	while (!txt2File.eof()) {
vb@0
   118
		static string line;
vb@0
   119
		getline(txt2File, line);
vb@0
   120
		t2 += line + "\r\n";
vb@0
   121
	}
vb@0
   122
	txt2File.close();
vb@0
   123
    assert(t2.size());
vb@0
   124
    t1.erase(t2.size()-2, 2);
vb@0
   125
vb@0
   126
    cout << "\ncalling verify_test()\n";
vb@0
   127
    verify_result = verify_text(session, t2.c_str(), t2.size(), sig.c_str(), sig.size(), &keylist);
vb@0
   128
    cout << "result = " << verify_result << "\n";
vb@0
   129
    assert(verify_result == PEP_DECRYPT_SIGNATURE_DOES_NOT_MATCH);
vb@0
   130
    free_stringlist(keylist);
vb@0
   131
vb@9
   132
    keylist = new_stringlist("49422235FC99585B891C66530C7B109BFA7261F7");
vb@9
   133
    // stringlist_add(keylist, "C6FAA231A2B43252B9526D119550C6B6B8B0FCD6");
vb@9
   134
    stringlist_add(keylist, "5DC8CAC595EDAD6598DD4732DD55BF29DF9B1541");
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@8
   168
            "leon.schumacher@digitalekho.com",
vb@8
   169
            "8BD08954C74D830EEFFB5DEB2682A17F7C87F73D",
vb@8
   170
            "23",
vb@8
   171
            "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@8
   182
vb@8
   183
    PEP_STATUS get_trust_result = get_trust(session, identity);
vb@8
   184
    assert(get_trust_result == PEP_STATUS_OK);
vb@8
   185
    cout << "trust of " << identity->user_id << " is " << identity->comm_type << "\n";
vb@8
   186
vb@0
   187
    free_identity(identity);
vb@0
   188
vb@0
   189
    cout << "\ngenerating key for testuser\n";
vb@0
   190
    identity = new_identity(
vb@8
   191
            "testuser@pibit.ch",
vb@0
   192
            NULL,
vb@8
   193
            "423",
vb@8
   194
            "Alfred E. Neuman"
vb@0
   195
        );
vb@0
   196
    assert(identity);
vb@0
   197
    PEP_STATUS generate_status = generate_keypair(session, identity);
vb@0
   198
    cout << "generate_keypair() exits with " << generate_status << "\n";
vb@0
   199
    assert(generate_status == PEP_STATUS_OK);
vb@0
   200
    cout << "generated key is " << identity->fpr << "\n";
vb@0
   201
vb@0
   202
    string key(identity->fpr);
vb@0
   203
    free_identity(identity);
vb@0
   204
vb@0
   205
    char *key_data;
vb@0
   206
    size_t size;
vb@0
   207
vb@0
   208
    cout << "export_key()\n\n";
vb@0
   209
    PEP_STATUS export_status = export_key(session, key.c_str(), &key_data, &size);
vb@0
   210
    assert(export_status == PEP_STATUS_OK);
vb@0
   211
    cout << key_data << "\n\n";
vb@0
   212
vb@0
   213
    cout << "deleting key pair " << key.c_str() << "\n";
vb@0
   214
    PEP_STATUS delete_status = delete_keypair(session, key.c_str());
vb@0
   215
    cout << "delete_keypair() exits with " << delete_status << "\n";
vb@0
   216
    assert(delete_status == PEP_STATUS_OK);
vb@0
   217
    
vb@0
   218
    cout << "import_key()\n";
vb@0
   219
    PEP_STATUS import_status = import_key(session, key_data, size);
vb@0
   220
    assert(import_status == PEP_STATUS_OK);
vb@0
   221
    cout << "successfully imported key\n";
vb@0
   222
vb@0
   223
    pEp_free(key_data);
vb@0
   224
vb@0
   225
    cout << "deleting key " << key.c_str() << " again\n";
vb@0
   226
    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 << "finding key for outlooktest@dingens.org\n";
vb@0
   231
    PEP_STATUS find_keys_status = find_keys(session, "outlooktest@dingens.org", &keylist);
vb@0
   232
    assert(find_keys_status == PEP_STATUS_OK);
vb@0
   233
    assert(keylist);
vb@0
   234
    cout << "found: " << keylist->value << "\n";
vb@0
   235
    assert(keylist->next == NULL);
vb@0
   236
    free_stringlist(keylist);
vb@0
   237
vb@0
   238
    cout << "searching for vb@ulm.ccc.de on keyserver\n";
vb@0
   239
    PEP_STATUS recv_key_status = recv_key(session, "vb@ulm.ccc.de");
vb@0
   240
    assert(recv_key_status == PEP_STATUS_OK);
vb@0
   241
vb@0
   242
    cout << "sending vb@ulm.ccc.de to keyserver\n";
vb@0
   243
    PEP_STATUS send_key_status = send_key(session, "vb@ulm.ccc.de");
vb@0
   244
    assert(recv_key_status == PEP_STATUS_OK);
vb@0
   245
vb@9
   246
    PEP_comm_type tcomm_type;
vb@9
   247
    PEP_STATUS tstatus = get_key_rating(session, "49422235FC99585B891C66530C7B109BFA7261F7", &tcomm_type);
vb@9
   248
    assert(tstatus == PEP_STATUS_OK);
vb@9
   249
    assert(tcomm_type == PEP_ct_OpenPGP_unconfirmed);
vb@9
   250
    
vb@0
   251
	cout << "\ncalling release()\n";
vb@0
   252
	release(session);
vb@0
   253
	return 0;
vb@0
   254
}