1 // This file is under GNU General Public License 3.0
15 #include "../src/pEpEngine.h"
16 #include "../src/keymanagement.h"
20 typedef std::string Buffer;
22 std::string slurp(const std::string& filename)
24 std::ifstream input(filename.c_str());
27 throw std::runtime_error("Cannot read file \"" + filename + "\"! ");
30 std::stringstream sstr;
31 sstr << input.rdbuf();
35 // no C++11, yet? So do our own implementation:1G
37 std::string to_string(unsigned long u)
40 snprintf(buf,31, "%lu", u);
44 std::string status(PEP_STATUS status)
49 return "PEP_STATUS_OK";
53 snprintf(buf,31, "%u (0x%x)", status, status);
55 snprintf(buf,31, "%d", status);
61 } // end of anonymous namespace
64 Buffer ReadFileIntoMem(const char *fname){
65 cout << "opening " << fname << " for reading\n";
66 ifstream txtFile (fname, ifstream::binary);
67 assert(txtFile.is_open());
70 throw std::runtime_error( "error: cannot open file \"" + std::string(fname) + "\"" );
75 // get length of file:
76 txtFile.seekg (0, txtFile.end);
77 const size_t length = txtFile.tellg();
78 txtFile.seekg (0, txtFile.beg);
79 buffer.resize(length);
81 cout << "Reading " << length << " characters... ";
82 txtFile.read (&buffer[0], length);
86 throw std::runtime_error( "error: only " + to_string(txtFile.gcount()) + " could be read from file" + fname );
89 cout << "all characters read successfully." << std::endl;
94 int main(int argc, char* argv[])
98 cout << "calling init()\n";
99 PEP_STATUS init_result = init(&session, NULL, NULL);
101 cout << "returning from init() with result == " << status(init_result) << endl;
102 assert(init_result == PEP_STATUS_OK);
104 PEP_SESSION second_session;
105 cout << "second session test\n";
106 PEP_STATUS second_init_result = init(&second_session, NULL, NULL);
107 cout << "returning from second init() with result == " << status(second_init_result) << endl;
108 assert(second_init_result == PEP_STATUS_OK);
109 assert(second_session);
110 cout << "dropping second session\n";
111 release(second_session);
113 cout << "Importing keys...";
114 string pub_key = slurp("test_keys/pub/pep-test-alice-0x6FF00E97_pub.asc");
115 string priv_key = slurp("test_keys/priv/pep-test-alice-0x6FF00E97_priv.asc");
117 PEP_STATUS statuspub = import_key(session, pub_key.c_str(), pub_key.length(), NULL);
118 PEP_STATUS statuspriv = import_key(session, priv_key.c_str(), priv_key.length(), NULL);
119 assert(statuspub == PEP_STATUS_OK);
120 assert(statuspriv == PEP_STATUS_OK);
122 pub_key = slurp("test_keys/pub/pep-test-john-0x70DCF575_pub.asc");
123 priv_key = slurp("test_keys/priv/pep-test-john-0x70DCF575_priv.asc");
125 statuspub = import_key(session, pub_key.c_str(), pub_key.length(), NULL);
126 statuspriv = import_key(session, priv_key.c_str(), priv_key.length(), NULL);
127 assert(statuspub == PEP_STATUS_OK);
128 assert(statuspriv == PEP_STATUS_OK);
130 pub_key = slurp("test_keys/pub/pep-test-bob-0xC9C2EE39_pub.asc");
132 statuspub = import_key(session, pub_key.c_str(), pub_key.length(), NULL);
133 assert(statuspub == PEP_STATUS_OK);
134 assert(statuspriv == PEP_STATUS_OK);
137 cout << "creating message…\n";
138 pEp_identity* alice = new_identity("pep.test.alice@pep-project.org", "4ABE3AAF59AC32CFE4F86500A9411D176FF00E97", PEP_OWN_USERID, "Alice Test");
139 pEp_identity* bob = new_identity("pep.test.bob@pep-project.org", NULL, "42", "Bob Test");
142 cout << "logging test\n";
143 log_event(session, "log test", "pEp Engine Test", "This is a logging test sample.", "please ignore this line");
146 // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
147 // 6FF00E97 -- won't work as search term with NetPGP
148 // A9411D176FF00E97 -- won't work as search term with NetPGP
149 // 4ABE3AAF59AC32CFE4F86500A9411D176FF00E97 -- full key fingerprint
152 // pEp Test Bob (test key, don't use) <pep.test.bob@pep-project.org>
153 // C9C2EE39 -- won't work as search term with NetPGP
154 // 59BFF488C9C2EE39 -- won't work as search term with NetPGP
155 // BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39 -- full key fingerprint
157 // pEp Test John (test key, don't use) <pep.test.john@pep-project.org>
158 // 70DCF575 -- won't work as search term with NetPGP
159 // 135CD6D170DCF575 -- won't work as search term with NetPGP
160 // AA2E4BEB93E5FE33DEFD8BE1135CD6D170DCF575 -- full key fingerprint
162 const char *kflist[] = {
169 const char** kf = kflist;
171 const Buffer k_user_buffer = ReadFileIntoMem(*kf);
172 cout << "import_key(" << *kf << ")\n";
173 PEP_STATUS import_status = import_key(session, k_user_buffer.data(), k_user_buffer.size(), NULL);
174 assert(import_status == PEP_STATUS_OK);
175 cout << "successfully imported key\n";
179 const Buffer cipher_buffer = ReadFileIntoMem("msg.asc");
180 cout << "\n" << cipher_buffer.data();
182 char *buf_text = NULL;
184 stringlist_t *keylist;
186 cout << "calling decrypt_and_verify()\n";
187 PEP_STATUS decrypt_result = decrypt_and_verify(session, cipher_buffer.data(), cipher_buffer.size(), NULL, 0, &buf_text, &buf_size, &keylist);
189 cout << "returning from decrypt_and_verify() with result == " << status(decrypt_result) << endl;
190 assert(decrypt_result == PEP_DECRYPTED_AND_VERIFIED);
194 for (stringlist_t *_keylist=keylist; _keylist!=NULL; _keylist=_keylist->next) {
195 assert(_keylist->value);
196 cout << "signed with " << _keylist->value << endl;
199 free_stringlist(keylist);
200 buf_text[buf_size] = 0;
201 const string plain(buf_text);
203 cout << "\n" << plain;
205 const Buffer t1_buffer = ReadFileIntoMem("t1.txt");
206 const Buffer sig_buffer = ReadFileIntoMem("signature.asc");
208 cout << "\ncalling verify_text()\n";
209 PEP_STATUS verify_result = verify_text(session, t1_buffer.data(), t1_buffer.size(), sig_buffer.data(), sig_buffer.size(), &keylist);
210 cout << "returning from verify_text() with result == " << status(verify_result) << endl;
211 assert(verify_result == PEP_VERIFIED || verify_result == PEP_VERIFIED_AND_TRUSTED);
212 assert(keylist->value);
213 cout << "signed with " << keylist->value << endl;
214 free_stringlist(keylist);
216 const Buffer t2_buffer = ReadFileIntoMem("t2.txt");
218 cout << "\ncalling verify_text()\n";
219 verify_result = verify_text(session, t2_buffer.data(), t2_buffer.size(), sig_buffer.data(), sig_buffer.size(), &keylist);
220 assert(verify_result == PEP_DECRYPT_SIGNATURE_DOES_NOT_MATCH);
221 free_stringlist(keylist);
223 keylist = new_stringlist("4ABE3AAF59AC32CFE4F86500A9411D176FF00E97");
224 stringlist_add(keylist, "BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39");
225 stringlist_add(keylist, "AA2E4BEB93E5FE33DEFD8BE1135CD6D170DCF575");
230 cout << "\ncalling encrypt_and_sign()\n";
231 PEP_STATUS encrypt_result = encrypt_and_sign(session, keylist, plain.c_str(), plain.length(), &buf_text, &buf_size);
232 cout << "returning from encrypt_and_sign() with result == " << status(encrypt_result) << endl;
233 assert(encrypt_result == PEP_STATUS_OK);
234 free_stringlist(keylist);
236 buf_text[buf_size] = '\0';
237 const string cipher2(buf_text);
238 cout << "\n" << cipher2;
241 cout << "\nfinding English trustword for 2342...\n";
244 trustword(session, 2342, "en", &word, &wsize);
246 cout << "the English trustword for 2342 is " << word << endl;
248 cout << "\nfinding French trustword for 2342...\n";
249 trustword(session, 2342, "fr", &word, &wsize);
251 cout << "the French trustword for 2342 is " << word << endl;
254 const string fingerprint = "4942 2235 FC99 585B 891C 6653 0C7B 109B FA72 61F7";
257 cout << "\nfinding German trustwords for " << fingerprint << "...\n";
258 trustwords(session, fingerprint.c_str(), "de", &words, &wsize, 5);
260 cout << words << endl;
263 pEp_identity* identity = new_identity(
264 "leon.schumacher@digitalekho.com",
265 "8BD08954C74D830EEFFB5DEB2682A17F7C87F73D",
269 identity->comm_type = PEP_ct_pEp;
271 cout << "\nsetting identity...\n";
272 PEP_STATUS pep_set_result = set_identity(session, identity);
273 cout << pep_set_result << endl;
274 assert(pep_set_result == PEP_STATUS_OK);
275 free_identity(identity);
276 get_identity(session, "leon.schumacher@digitalekho.com", "23", &identity);
278 cout << "set: " << identity->address << ", " << identity->fpr << ", " << identity->user_id << ", " << identity->username << endl;
280 PEP_STATUS get_trust_result = get_trust(session, identity);
281 assert(get_trust_result == PEP_STATUS_OK);
282 cout << "trust of " << identity->user_id << " is " << identity->comm_type << endl;
284 free_identity(identity);
286 cout << "\ngenerating key for testuser\n";
287 identity = new_identity(
295 PEP_STATUS generate_status = generate_keypair(session, identity);
296 cout << "generate_keypair() exits with " << status(generate_status) << endl;
297 assert(generate_status == PEP_STATUS_OK);
298 cout << "generated key is " << identity->fpr << endl;
300 const string key(identity->fpr);
301 free_identity(identity);
303 char *key_data = NULL;
306 cout << "export_key()\n\n";
307 PEP_STATUS export_status = export_key(session, key.c_str(), &key_data, &size);
308 cout << "export_key() exits with " << status(export_status) << endl;
309 assert(export_status == PEP_STATUS_OK);
310 cout << key_data << "\n\n";
312 cout << "deleting key pair " << key.c_str() << endl;
313 PEP_STATUS delete_status = delete_keypair(session, key.c_str());
314 cout << "delete_keypair() exits with " << status(delete_status) << endl;
315 assert(delete_status == PEP_STATUS_OK);
317 cout << "import_key()\n";
318 PEP_STATUS import_status = import_key(session, key_data, size, NULL);
319 assert(import_status == PEP_STATUS_OK);
320 cout << "successfully imported key\n";
325 cout << "deleting key " << key.c_str() << " again\n";
326 delete_status = delete_keypair(session, key.c_str());
327 cout << "delete_keypair() exits with " << status(delete_status) << endl;
328 assert(delete_status == PEP_STATUS_OK);
330 cout << "finding key for pep.test.john@pep-project.org\n";
331 PEP_STATUS find_keys_status = find_keys(session, "pep.test.john@pep-project.org", &keylist);
332 cout << "find_keys() exits with " << status(find_keys_status) << endl;
333 assert(find_keys_status == PEP_STATUS_OK);
335 cout << "found: " << keylist->value << endl;
336 assert(keylist->next == NULL);
337 free_stringlist(keylist);
339 cout << "searching for vb@ulm.ccc.de on keyserver\n";
340 PEP_STATUS recv_key_status = recv_key(session, "vb@ulm.ccc.de");
341 cout << "recv_key() exits with " << status(recv_key_status) << endl;
342 assert(recv_key_status == PEP_STATUS_OK);
344 cout << "sending vb@ulm.ccc.de to keyserver\n";
345 PEP_STATUS send_key_status = send_key(session, "vb@ulm.ccc.de");
346 cout << "send_key() exits with " << status(send_key_status) << endl;
347 assert(send_key_status == PEP_STATUS_OK);
349 PEP_comm_type tcomm_type;
350 PEP_STATUS tstatus = get_key_rating(session, "BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39", &tcomm_type);
351 cout << "get_key_rating() exits with " << status(tstatus) << endl;
352 assert(tstatus == PEP_STATUS_OK);
353 assert(tcomm_type == PEP_ct_OpenPGP_unconfirmed);
355 cout << "\ncalling release()\n";
356 cout << endl << "End of pEpEngineTest for engine version " << get_engine_version() << endl;