test/pEpEngineTest.cc
author Roker <roker@pep-project.org>
Sun, 03 Jul 2016 01:11:23 +0200
branchroker-linux
changeset 799 f60a61e7d403
parent 753 dd21cdff7492
child 800 6564b62a26e5
permissions -rw-r--r--
add a recursion test that will crash
     1 #include <iostream>
     2 #include <fstream>
     3 #include <stdexcept>
     4 #include <string>
     5 #include <vector>
     6 
     7 #include <assert.h>
     8 #include <string.h>
     9 
    10 #include "../src/pEpEngine.h"
    11 #include "../src/keymanagement.h"
    12 #include "../src/stringpair.h"
    13 
    14 
    15 using namespace std;
    16 
    17 typedef std::string Buffer;
    18 
    19 // no C++11, yet? So do our own implementation:
    20 namespace{
    21     std::string to_string(unsigned long u)
    22     {
    23         char buf[32];
    24         snprintf(buf,31, "%lu", u);
    25         return buf;
    26     }
    27 }
    28 
    29 
    30 Buffer ReadFileIntoMem(const char *fname){
    31     cout << "opening " << fname << " for reading\n";
    32     ifstream txtFile (fname, ifstream::binary);
    33     assert(txtFile.is_open());
    34     if (!txtFile)
    35     {
    36         throw std::runtime_error( "error: cannot open file \"" + std::string(fname) + "\"" );
    37     }
    38 
    39     Buffer buffer;
    40 
    41     // get length of file:
    42     txtFile.seekg (0, txtFile.end);
    43     const size_t length = txtFile.tellg();
    44     txtFile.seekg (0, txtFile.beg);
    45     buffer.resize(length);
    46 
    47     cout << "Reading " << length << " characters... ";
    48     txtFile.read (&buffer[0], length);
    49 
    50     if (!txtFile)
    51     {
    52         throw std::runtime_error( "error: only " + to_string(txtFile.gcount()) + " could be read from file" + fname );
    53     }
    54 
    55     cout << "all characters read successfully." << std::endl;
    56     return buffer;
    57 }
    58 
    59 
    60 int stringlist_test()
    61 {
    62 	char stringkey[32] = {0};
    63 	char stringvalue[32] = {0};
    64 
    65 	for(double len=1.0; len<1E18; len=(len*3.8251)+1)
    66 	{
    67 		const unsigned ulen = unsigned(len);
    68 		std::cout << "Stringpairlist length=" << ulen << ": " << std::endl;
    69 		stringpair_list_t* sl = new_stringpair_list(nullptr);
    70 		for(unsigned n=0; n<ulen; ++n)
    71 		{
    72 			snprintf(stringkey,63, "%x %x %a", n, ulen, len);
    73 			snprintf(stringvalue,63, "%u %u %a", n, ulen, len);
    74 			//std::cout << "\r\t\"" << stringvalue << "\" " << std::flush;
    75 			auto sp = new_stringpair( stringkey, stringvalue);
    76 			auto ret = stringpair_list_add(sl, sp);
    77 			if(ret==NULL)
    78 			{
    79 				std::cout << " OUT OF MEMORY! n=" << n  << std::endl;
    80 				return -1;
    81 			}
    82 		}
    83 		std::cout << "\tfree_stringpairlist()..." << std::flush;
    84 		free_stringpair_list(sl);
    85 		std::cout << std::endl;
    86 	}
    87 	return 0;
    88 }
    89 
    90 
    91 int main(int argc, char* argv[])
    92 {
    93     PEP_SESSION session;
    94 
    95     cout << "calling init()\n";
    96     PEP_STATUS init_result = init(&session);
    97     
    98     cout << "returning from init() with result == " << init_result << "\n";
    99     assert(init_result == PEP_STATUS_OK);
   100 
   101 	stringlist_test();
   102 
   103     PEP_SESSION second_session;
   104     cout << "second session test\n";
   105     PEP_STATUS second_init_result = init(&second_session);
   106     cout << "returning from second init() with result == " << second_init_result << "\n";
   107     assert(second_init_result == PEP_STATUS_OK);
   108     assert(second_session);
   109     cout << "dropping second session\n";
   110     release(second_session);
   111 
   112     cout << "logging test\n";
   113     log_event(session, "log test", "pEp Enginge Test", "This is a logging test sample.", "please ignore this line");
   114 
   115     // Our test user :
   116     // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
   117     //         6FF00E97
   118     // A9411D176FF00E97
   119     // 
   120     // Other peers :
   121     // pEp Test Bob (test key, don't use) <pep.test.bob@pep-project.org> 
   122     //         C9C2EE39
   123     // 59BFF488C9C2EE39
   124     // 
   125     // pEp Test John (test key, don't use) <pep.test.john@pep-project.org>
   126     //         70DCF575
   127     // 135CD6D170DCF575
   128 
   129     const char *kflist[] = {
   130         "0x6FF00E97.asc",
   131         "0xC9C2EE39.asc",
   132         "0x70DCF575.asc",
   133         NULL
   134     };
   135 
   136     const char** kf = kflist;
   137     while(*kf){
   138         const Buffer k_user_buffer =  ReadFileIntoMem(*kf);
   139         cout << "import_key(" << *kf << ")\n";
   140         PEP_STATUS import_status = import_key(session, k_user_buffer.data(), k_user_buffer.size(), NULL);
   141         assert(import_status == PEP_STATUS_OK);
   142         cout << "successfully imported key\n";
   143         kf++;
   144     }
   145 
   146     const Buffer cipher_buffer = ReadFileIntoMem("msg.asc");
   147     cout << "\n" << cipher_buffer.data();
   148 
   149     char *buf_text = NULL;
   150     size_t buf_size = 0;
   151     stringlist_t *keylist;
   152 
   153     cout << "calling decrypt_and_verify()\n";
   154     PEP_STATUS decrypt_result = decrypt_and_verify(session, cipher_buffer.data(), cipher_buffer.size(), &buf_text, &buf_size, &keylist);
   155 
   156     cout << "returning from decrypt_and_verify() with result == 0x" << std::hex << decrypt_result << "\n";
   157     assert(decrypt_result == PEP_DECRYPTED_AND_VERIFIED);
   158     assert(buf_text);
   159     assert(keylist);
   160 
   161     for (stringlist_t *_keylist=keylist; _keylist!=NULL; _keylist=_keylist->next) {
   162         assert(_keylist->value);
   163         cout << "signed with " << _keylist->value << "\n";
   164     }
   165 
   166     free_stringlist(keylist);
   167     buf_text[buf_size] = 0;
   168     const string plain(buf_text);
   169     pEp_free(buf_text);
   170     cout << "\n" << plain;
   171 
   172     const Buffer t1_buffer = ReadFileIntoMem("t1.txt");
   173     const Buffer sig_buffer = ReadFileIntoMem("signature.asc");
   174 
   175     cout << "\ncalling verify_text()\n";
   176     PEP_STATUS verify_result = verify_text(session, t1_buffer.data(), t1_buffer.size(), sig_buffer.data(), sig_buffer.size(), &keylist);
   177     cout << "returning from verify_text() with result == " << verify_result << "\n";
   178     assert(verify_result == PEP_VERIFIED || verify_result == PEP_VERIFIED_AND_TRUSTED);
   179     assert(keylist->value);
   180     cout << "signed with " << keylist->value << "\n";
   181     free_stringlist(keylist);
   182 
   183     const Buffer t2_buffer = ReadFileIntoMem("t2.txt");
   184 
   185     cout << "\ncalling verify_text()\n";
   186     verify_result = verify_text(session, t2_buffer.data(), t2_buffer.size(), sig_buffer.data(), sig_buffer.size(), &keylist);
   187     assert(verify_result == PEP_DECRYPT_SIGNATURE_DOES_NOT_MATCH);
   188     free_stringlist(keylist);
   189 
   190     keylist = new_stringlist("A9411D176FF00E97");
   191     stringlist_add(keylist, "59BFF488C9C2EE39");
   192     stringlist_add(keylist, "135CD6D170DCF575");
   193 
   194     buf_text = NULL;
   195     buf_size = 0;
   196 
   197     cout << "\ncalling encrypt_and_sign()\n";
   198     PEP_STATUS encrypt_result = encrypt_and_sign(session, keylist, plain.c_str(), plain.length(), &buf_text, &buf_size);
   199     cout << "returning from encrypt_and_sign() with result == " << encrypt_result << "\n";
   200     assert(encrypt_result == PEP_STATUS_OK);
   201     free_stringlist(keylist);
   202 
   203     buf_text[buf_size] = '\0';
   204     const string cipher2(buf_text);
   205     cout << "\n" << cipher2;
   206     pEp_free(buf_text);
   207 
   208     cout << "\nfinding English trustword for 2342...\n";
   209     char * word = NULL;
   210     size_t wsize;
   211     trustword(session, 2342, "en", &word, &wsize);
   212     assert(word);
   213     cout << "the trustword for 2342 is " << word << "\n";
   214     pEp_free(word);
   215 
   216     const string fingerprint = "4942 2235 FC99 585B 891C  6653 0C7B 109B FA72 61F7";
   217     char * words = NULL;
   218 
   219     cout << "\nfinding German trustwords for " << fingerprint << "...\n";
   220     trustwords(session, fingerprint.c_str(), "de", &words, &wsize, 5);
   221     assert(words);
   222     cout << words << "\n";
   223     pEp_free(words);
   224 
   225     pEp_identity* identity  = new_identity(
   226             "leon.schumacher@digitalekho.com",
   227             "8BD08954C74D830EEFFB5DEB2682A17F7C87F73D",
   228             "23",
   229             "Leon Schumacher"
   230         );
   231     identity->comm_type = PEP_ct_pEp;
   232 
   233     cout << "\nsetting identity...\n";
   234     PEP_STATUS pep_set_result = set_identity(session, identity);
   235     assert(pep_set_result == PEP_STATUS_OK);
   236     free_identity(identity);
   237     get_identity(session, "leon.schumacher@digitalekho.com", "23", &identity);
   238     assert(identity);
   239     cout << "set: " << identity->address << ", " << identity->fpr << ", " << identity->user_id << ", " << identity->username << "\n";
   240 
   241     PEP_STATUS get_trust_result = get_trust(session, identity);
   242     assert(get_trust_result == PEP_STATUS_OK);
   243     cout << "trust of " << identity->user_id << " is " << identity->comm_type << "\n";
   244 
   245     free_identity(identity);
   246 
   247     cout << "\ngenerating key for testuser\n";
   248     identity = new_identity(
   249             "testuser@pibit.ch",
   250             NULL,
   251             "423",
   252             "Alfred E. Neuman"
   253         );
   254 
   255     assert(identity);
   256     PEP_STATUS generate_status = generate_keypair(session, identity);
   257     cout << "generate_keypair() exits with " << generate_status << "\n";
   258     assert(generate_status == PEP_STATUS_OK);
   259     cout << "generated key is " << identity->fpr << "\n";
   260 
   261     const string key(identity->fpr);
   262     free_identity(identity);
   263 
   264     char *key_data = NULL;
   265     size_t size = 0;
   266 
   267     cout << "export_key()\n\n";
   268     PEP_STATUS export_status = export_key(session, key.c_str(), &key_data, &size);
   269     cout << "export_key() exits with " << export_status << "\n";
   270     assert(export_status == PEP_STATUS_OK);
   271     cout << key_data << "\n\n";
   272 
   273     cout << "deleting key pair " << key.c_str() << "\n";
   274     PEP_STATUS delete_status = delete_keypair(session, key.c_str());
   275     cout << "delete_keypair() exits with " << delete_status << "\n";
   276     assert(delete_status == PEP_STATUS_OK);
   277     
   278     cout << "import_key()\n";
   279     PEP_STATUS import_status = import_key(session, key_data, size, NULL);
   280     assert(import_status == PEP_STATUS_OK);
   281     cout << "successfully imported key\n";
   282 
   283     pEp_free(key_data);
   284     key_data=NULL;
   285 
   286     cout << "deleting key " << key.c_str() << " again\n";
   287     delete_status = delete_keypair(session, key.c_str());
   288     cout << "delete_keypair() exits with " << delete_status << "\n";
   289     assert(delete_status == PEP_STATUS_OK);
   290 
   291     cout << "finding key for pep.test.john@pep-project.org\n";
   292     PEP_STATUS find_keys_status = find_keys(session, "pep.test.john@pep-project.org", &keylist);
   293     cout << "find_keys() exits with " << find_keys_status << "\n";
   294     assert(find_keys_status == PEP_STATUS_OK);
   295     assert(keylist);
   296     cout << "found: " << keylist->value << "\n";
   297     assert(keylist->next == NULL);
   298     free_stringlist(keylist);
   299 
   300     cout << "searching for vb@ulm.ccc.de on keyserver\n";
   301     PEP_STATUS recv_key_status = recv_key(session, "vb@ulm.ccc.de");
   302     cout << "recv_key() exits with " << recv_key_status << "\n";
   303     assert(recv_key_status == PEP_STATUS_OK);
   304 
   305     cout << "sending vb@ulm.ccc.de to keyserver\n";
   306     PEP_STATUS send_key_status = send_key(session, "vb@ulm.ccc.de");
   307     cout << "send_key() exits with " << send_key_status << "\n";
   308     assert(send_key_status == PEP_STATUS_OK);
   309 
   310     PEP_comm_type tcomm_type;
   311     PEP_STATUS tstatus = get_key_rating(session, "59BFF488C9C2EE39", &tcomm_type);
   312     cout << "get_key_rating() exits with " << tstatus << "\n";
   313     assert(tstatus == PEP_STATUS_OK);
   314     assert(tcomm_type == PEP_ct_OpenPGP_unconfirmed);
   315     
   316     cout << "\ncalling release()\n";
   317     release(session);
   318     return 0;
   319 }