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