test/pEpEngineTest.cc
changeset 0 16f27efbef98
child 8 26cc9f0228f4
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/pEpEngineTest.cc	Wed Jun 25 18:44:58 2014 +0200
     1.3 @@ -0,0 +1,261 @@
     1.4 +#include <iostream>
     1.5 +#include <fstream>
     1.6 +#include <string>
     1.7 +
     1.8 +#include <assert.h>
     1.9 +#include <string.h>
    1.10 +
    1.11 +#include "../src/pEpEngine.h"
    1.12 +#include "../src/keymanagement.h"
    1.13 +
    1.14 +#ifdef _WIN32
    1.15 +#define strdup _strdup
    1.16 +#endif
    1.17 +
    1.18 +using namespace std;
    1.19 +
    1.20 +int main(int argc, char* argv[])
    1.21 +{
    1.22 +	PEP_SESSION session;
    1.23 +
    1.24 +	cout << "calling init()\n";
    1.25 +	PEP_STATUS init_result = init(&session);
    1.26 +	
    1.27 +    cout << "returning from init() with result == " << init_result << "\n";
    1.28 +	assert(init_result == PEP_STATUS_OK);
    1.29 +
    1.30 +    PEP_SESSION second_session;
    1.31 +    cout << "second session test\n";
    1.32 +    PEP_STATUS second_init_result = init(&second_session);
    1.33 +	cout << "returning from second init() with result == " << second_init_result << "\n";
    1.34 +    assert(second_init_result == PEP_STATUS_OK);
    1.35 +    assert(second_session);
    1.36 +    cout << "dropping second session\n";
    1.37 +	release(second_session);
    1.38 +
    1.39 +	cout << "logging test\n";
    1.40 +	log_event(session, "log test", "pEp Enginge Test", "This is a logging test sample.", "please ignore this line");
    1.41 +
    1.42 +	string cipher;
    1.43 +
    1.44 +	cout << "opening msc.asc for reading\n";
    1.45 +	ifstream inFile ("msg.asc");
    1.46 +	assert(inFile.is_open());
    1.47 +
    1.48 +	cout << "reading cipher text of msc.asc\n";
    1.49 +	while (!inFile.eof()) {
    1.50 +		static string line;
    1.51 +		getline(inFile, line);
    1.52 +		cipher += line + "\n";
    1.53 +	}
    1.54 +	inFile.close();
    1.55 +
    1.56 +	cout << "\n" << cipher;
    1.57 +
    1.58 +	char *buf_text;
    1.59 +	size_t buf_size;
    1.60 +	stringlist_t *keylist;
    1.61 +
    1.62 +    cout << "calling decrypt_and_verify()\n";
    1.63 +    PEP_STATUS decrypt_result = decrypt_and_verify(session, cipher.c_str(), cipher.length(), &buf_text, &buf_size, &keylist);
    1.64 +
    1.65 +    cout << "returning from decrypt_and_verify() with result == " << decrypt_result << "\n";
    1.66 +    assert(decrypt_result == PEP_DECRYPTED_AND_VERIFIED);
    1.67 +    assert(buf_text);
    1.68 +    assert(keylist);
    1.69 +
    1.70 +    for (stringlist_t *_keylist=keylist; _keylist!=NULL; _keylist=_keylist->next) {
    1.71 +        assert(_keylist->value);
    1.72 +        cout << "signed with " << _keylist->value << "\n";
    1.73 +    }
    1.74 +
    1.75 +    free_stringlist(keylist);
    1.76 +    buf_text[buf_size] = 0;
    1.77 +    string plain(buf_text);
    1.78 +    pEp_free(buf_text);
    1.79 +    cout << "\n" << plain;
    1.80 +
    1.81 +    string t1, t2, sig;
    1.82 +
    1.83 +	cout << "\nopening t1.txt for reading\n";
    1.84 +	ifstream txtFile ("t1.txt");
    1.85 +	assert(txtFile.is_open());
    1.86 +
    1.87 +	cout << "reading t1 from t1.txt\n";
    1.88 +	while (!txtFile.eof()) {
    1.89 +		static string line;
    1.90 +		getline(txtFile, line);
    1.91 +		t1 += line + "\r\n";
    1.92 +	}
    1.93 +	txtFile.close();
    1.94 +    assert(t1.size());
    1.95 +    t1.erase(t1.size()-2, 2);
    1.96 +
    1.97 +	cout << "opening signature.asc for reading\n";
    1.98 +	ifstream sigFile ("signature.asc");
    1.99 +	assert(sigFile.is_open());
   1.100 +
   1.101 +	cout << "reading sig from signature.asc\n";
   1.102 +	while (!sigFile.eof()) {
   1.103 +		static string line;
   1.104 +		getline(sigFile, line);
   1.105 +		sig += line + "\n";
   1.106 +	}
   1.107 +	sigFile.close();
   1.108 +
   1.109 +    cout << "\ncalling verify_test()\n";
   1.110 +    PEP_STATUS verify_result = verify_text(session, t1.c_str(), t1.size(), sig.c_str(), sig.size(), &keylist);
   1.111 +    cout << "result = " << verify_result << "\n";
   1.112 +    assert(verify_result == PEP_VERIFIED || verify_result == PEP_VERIFIED_AND_TRUSTED);
   1.113 +    assert(keylist->value);
   1.114 +    cout << "signed with " << keylist->value << "\n";
   1.115 +    free_stringlist(keylist);
   1.116 +
   1.117 +	cout << "\nopening t2.txt for reading\n";
   1.118 +	ifstream txt2File ("t2.txt");
   1.119 +	assert(txt2File.is_open());
   1.120 +
   1.121 +	cout << "reading t2 from t2.txt\n";
   1.122 +	while (!txt2File.eof()) {
   1.123 +		static string line;
   1.124 +		getline(txt2File, line);
   1.125 +		t2 += line + "\r\n";
   1.126 +	}
   1.127 +	txt2File.close();
   1.128 +    assert(t2.size());
   1.129 +    t1.erase(t2.size()-2, 2);
   1.130 +
   1.131 +    cout << "\ncalling verify_test()\n";
   1.132 +    verify_result = verify_text(session, t2.c_str(), t2.size(), sig.c_str(), sig.size(), &keylist);
   1.133 +    cout << "result = " << verify_result << "\n";
   1.134 +    assert(verify_result == PEP_DECRYPT_SIGNATURE_DOES_NOT_MATCH);
   1.135 +    free_stringlist(keylist);
   1.136 +
   1.137 +    keylist = new_stringlist("FA7261F7");
   1.138 +
   1.139 +    cout << "\ncalling encrypt_and_sign()\n";
   1.140 +    PEP_STATUS encrypt_result = encrypt_and_sign(session, keylist, plain.c_str(), plain.length(), &buf_text, &buf_size);
   1.141 +    
   1.142 +    cout << "returning from encrypt_and_sign() with result == " << encrypt_result << "\n";
   1.143 +    assert(encrypt_result == PEP_STATUS_OK);
   1.144 +    free_stringlist(keylist);
   1.145 +
   1.146 +    buf_text[buf_size] = 0;
   1.147 +    string cipher2(buf_text);
   1.148 +    cout << "\n" << cipher2;
   1.149 +    pEp_free(buf_text);
   1.150 +
   1.151 +	cout << "\nfinding English safeword for 2342...\n";
   1.152 +	char * word;
   1.153 +	size_t wsize;
   1.154 +	safeword(session, 2342, "en", &word, &wsize);
   1.155 +	assert(word);
   1.156 +	cout << "the safeword for 2342 is " << word << "\n";
   1.157 +    pEp_free(word);
   1.158 +
   1.159 +    string fingerprint = "4942 2235 FC99 585B 891C  6653 0C7B 109B FA72 61F7";
   1.160 +    char * words;
   1.161 +
   1.162 +    cout << "\nfinding German safewords for " << fingerprint << "...\n";
   1.163 +    safewords(session, fingerprint.c_str(), "de", &words, &wsize, 5);
   1.164 +    assert(words);
   1.165 +    cout << words << "\n";
   1.166 +    pEp_free(words);
   1.167 +
   1.168 +	pEp_identity *identity;
   1.169 +
   1.170 +    identity = new_identity(
   1.171 +            strdup("leon.schumacher@digitalekho.com"),
   1.172 +            strdup("8BD08954C74D830EEFFB5DEB2682A17F7C87F73D"),
   1.173 +            strdup("23"),
   1.174 +            strdup("Leon Schumacher")
   1.175 +        );
   1.176 +	identity->comm_type = PEP_ct_pEp;
   1.177 +
   1.178 +	cout << "\nsetting identity...\n";
   1.179 +	PEP_STATUS pep_set_result = set_identity(session, identity);
   1.180 +	assert(pep_set_result == PEP_STATUS_OK);
   1.181 +    free_identity(identity);
   1.182 +	get_identity(session, "leon.schumacher@digitalekho.com", &identity);
   1.183 +	assert(identity);
   1.184 +	cout << "set: " << identity->address << ", " << identity->fpr << ", " << identity->user_id << ", " << identity->username << "\n";
   1.185 +    free_identity(identity);
   1.186 +
   1.187 +    stringlist_t *addresses = new_stringlist("leon.schumacher@digitalekho.com");
   1.188 +    PEP_comm_type comm_type;
   1.189 +    cout << "\nretrieving communication type for leon.schumacher@digitalekho.com\n";
   1.190 +    PEP_STATUS oct_result = outgoing_comm_type(session, addresses, &comm_type);
   1.191 +    cout << "communication type is " << comm_type << "\n";
   1.192 +    free_stringlist(addresses);
   1.193 +    assert(oct_result == PEP_STATUS_OK && comm_type == PEP_ct_pEp);
   1.194 +
   1.195 +    stringlist_t *addresses2 = new_stringlist("leon.schumacher@digitalekho.com");
   1.196 +    stringlist_add(addresses2, "this.email@is.invalid");
   1.197 +    cout << "\nretrieving communication type for an unknown address\n";
   1.198 +    oct_result = outgoing_comm_type(session, addresses2, &comm_type);
   1.199 +    cout << "communication type is " << comm_type << "\n";
   1.200 +    cout << "status is " << oct_result << "\n";
   1.201 +    free_stringlist(addresses2);
   1.202 +    assert(oct_result == PEP_STATUS_OK && comm_type == PEP_ct_no_encryption);
   1.203 +
   1.204 +    cout << "\ngenerating key for testuser\n";
   1.205 +    identity = new_identity(
   1.206 +            strdup("testuser@pibit.ch"),
   1.207 +            NULL,
   1.208 +            strdup("423"),
   1.209 +            strdup("Alfred E. Neuman")
   1.210 +        );
   1.211 +    assert(identity);
   1.212 +    PEP_STATUS generate_status = generate_keypair(session, identity);
   1.213 +    cout << "generate_keypair() exits with " << generate_status << "\n";
   1.214 +    assert(generate_status == PEP_STATUS_OK);
   1.215 +    cout << "generated key is " << identity->fpr << "\n";
   1.216 +
   1.217 +    string key(identity->fpr);
   1.218 +    free_identity(identity);
   1.219 +
   1.220 +    char *key_data;
   1.221 +    size_t size;
   1.222 +
   1.223 +    cout << "export_key()\n\n";
   1.224 +    PEP_STATUS export_status = export_key(session, key.c_str(), &key_data, &size);
   1.225 +    assert(export_status == PEP_STATUS_OK);
   1.226 +    cout << key_data << "\n\n";
   1.227 +
   1.228 +    cout << "deleting key pair " << key.c_str() << "\n";
   1.229 +    PEP_STATUS delete_status = delete_keypair(session, key.c_str());
   1.230 +    cout << "delete_keypair() exits with " << delete_status << "\n";
   1.231 +    assert(delete_status == PEP_STATUS_OK);
   1.232 +    
   1.233 +    cout << "import_key()\n";
   1.234 +    PEP_STATUS import_status = import_key(session, key_data, size);
   1.235 +    assert(import_status == PEP_STATUS_OK);
   1.236 +    cout << "successfully imported key\n";
   1.237 +
   1.238 +    pEp_free(key_data);
   1.239 +
   1.240 +    cout << "deleting key " << key.c_str() << " again\n";
   1.241 +    delete_status = delete_keypair(session, key.c_str());
   1.242 +    cout << "delete_keypair() exits with " << delete_status << "\n";
   1.243 +    assert(delete_status == PEP_STATUS_OK);
   1.244 +
   1.245 +    cout << "finding key for outlooktest@dingens.org\n";
   1.246 +    PEP_STATUS find_keys_status = find_keys(session, "outlooktest@dingens.org", &keylist);
   1.247 +    assert(find_keys_status == PEP_STATUS_OK);
   1.248 +    assert(keylist);
   1.249 +    cout << "found: " << keylist->value << "\n";
   1.250 +    assert(keylist->next == NULL);
   1.251 +    free_stringlist(keylist);
   1.252 +
   1.253 +    cout << "searching for vb@ulm.ccc.de on keyserver\n";
   1.254 +    PEP_STATUS recv_key_status = recv_key(session, "vb@ulm.ccc.de");
   1.255 +    assert(recv_key_status == PEP_STATUS_OK);
   1.256 +
   1.257 +    cout << "sending vb@ulm.ccc.de to keyserver\n";
   1.258 +    PEP_STATUS send_key_status = send_key(session, "vb@ulm.ccc.de");
   1.259 +    assert(recv_key_status == PEP_STATUS_OK);
   1.260 +
   1.261 +	cout << "\ncalling release()\n";
   1.262 +	release(session);
   1.263 +	return 0;
   1.264 +}