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