test/pEpEngineTest.cc
author Krista Bennett <krista@pep-project.org>
Wed, 06 Jun 2018 20:21:24 +0200
branchENGINE-422
changeset 2742 cd06139cb7fc
parent 2468 3a10b4f8ba41
child 2847 930626fcac04
permissions -rw-r--r--
close branch
     1 // This file is under GNU General Public License 3.0
     2 // see LICENSE.txt
     3 
     4 #include <iostream>
     5 #include <fstream>
     6 #include <stdexcept>
     7 #include <string>
     8 #include <vector>
     9 
    10 #include <assert.h>
    11 #include <string.h>
    12 #include <sstream>
    13 
    14 
    15 #include "../src/pEpEngine.h"
    16 #include "../src/keymanagement.h"
    17 
    18 using namespace std;
    19 
    20 typedef std::string Buffer;
    21 
    22 std::string slurp(const std::string& filename)
    23 {
    24         std::ifstream input(filename.c_str());
    25         if(!input)
    26         {
    27                 throw std::runtime_error("Cannot read file \"" + filename + "\"! ");
    28         }
    29 
    30         std::stringstream sstr;
    31         sstr << input.rdbuf();
    32         return sstr.str();
    33 }
    34 
    35 // no C++11, yet? So do our own implementation:1G
    36 namespace{
    37     std::string to_string(unsigned long u)
    38     {
    39         char buf[32];
    40         snprintf(buf,31, "%lu", u);
    41         return buf;
    42     }
    43     
    44     std::string status(PEP_STATUS status)
    45     {
    46         char buf[32] = {0};
    47         if(status==0)
    48         {
    49             return "PEP_STATUS_OK";
    50         }else{
    51             if(status>0)
    52             {
    53                 snprintf(buf,31, "%u (0x%x)", status, status);
    54             }else{
    55                 snprintf(buf,31, "%d", status);
    56             }
    57         }
    58         return buf;
    59     }
    60 
    61 } // end of anonymous namespace
    62 
    63 
    64 Buffer ReadFileIntoMem(const char *fname){
    65     cout << "opening " << fname << " for reading\n";
    66     ifstream txtFile (fname, ifstream::binary);
    67     assert(txtFile.is_open());
    68     if (!txtFile)
    69     {
    70         throw std::runtime_error( "error: cannot open file \"" + std::string(fname) + "\"" );
    71     }
    72 
    73     Buffer buffer;
    74 
    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);
    80 
    81     cout << "Reading " << length << " characters... ";
    82     txtFile.read (&buffer[0], length);
    83 
    84     if (!txtFile)
    85     {
    86         throw std::runtime_error( "error: only " + to_string(txtFile.gcount()) + " could be read from file" + fname );
    87     }
    88 
    89     cout << "all characters read successfully." << std::endl;
    90     return buffer;
    91 }
    92 
    93 
    94 int main(int argc, char* argv[])
    95 {
    96     PEP_SESSION session;
    97 
    98     cout << "calling init()\n";
    99     PEP_STATUS init_result = init(&session);
   100     
   101     cout << "returning from init() with result == " << status(init_result) << endl;
   102     assert(init_result == PEP_STATUS_OK);
   103 
   104     PEP_SESSION second_session;
   105     cout << "second session test\n";
   106     PEP_STATUS second_init_result = init(&second_session);
   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);
   112 
   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");
   116 
   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);
   121 
   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");
   124 
   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);
   129 
   130     pub_key = slurp("test_keys/pub/pep-test-bob-0xC9C2EE39_pub.asc");
   131 
   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);
   135 
   136 
   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");
   140 
   141 
   142     cout << "logging test\n";
   143     log_event(session, "log test", "pEp Engine Test", "This is a logging test sample.", "please ignore this line");
   144 
   145     // Our test user :
   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
   150     // 
   151     // Other peers :
   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
   156     // 
   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
   161 
   162     const char *kflist[] = {
   163         "0x6FF00E97.asc",
   164         "0xC9C2EE39.asc",
   165         "0x70DCF575.asc",
   166         NULL
   167     };
   168 
   169     const char** kf = kflist;
   170     while(*kf){
   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";
   176         kf++;
   177     }
   178 
   179     const Buffer cipher_buffer = ReadFileIntoMem("msg.asc");
   180     cout << "\n" << cipher_buffer.data();
   181 
   182     char *buf_text = NULL;
   183     size_t buf_size = 0;
   184     stringlist_t *keylist;
   185 
   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);
   188 
   189     cout << "returning from decrypt_and_verify() with result == " << status(decrypt_result) << endl;
   190     assert(decrypt_result == PEP_DECRYPTED_AND_VERIFIED);
   191     assert(buf_text);
   192     assert(keylist);
   193 
   194     for (stringlist_t *_keylist=keylist; _keylist!=NULL; _keylist=_keylist->next) {
   195         assert(_keylist->value);
   196         cout << "signed with " << _keylist->value << endl;
   197     }
   198 
   199     free_stringlist(keylist);
   200     buf_text[buf_size] = 0;
   201     const string plain(buf_text);
   202     pEp_free(buf_text);
   203     cout << "\n" << plain;
   204 
   205     const Buffer t1_buffer = ReadFileIntoMem("t1.txt");
   206     const Buffer sig_buffer = ReadFileIntoMem("signature.asc");
   207 
   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);
   215 
   216     const Buffer t2_buffer = ReadFileIntoMem("t2.txt");
   217 
   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);
   222 
   223     keylist = new_stringlist("4ABE3AAF59AC32CFE4F86500A9411D176FF00E97");
   224     stringlist_add(keylist, "BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39");
   225     stringlist_add(keylist, "AA2E4BEB93E5FE33DEFD8BE1135CD6D170DCF575");
   226 
   227     buf_text = NULL;
   228     buf_size = 0;
   229 
   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);
   235 
   236     buf_text[buf_size] = '\0';
   237     const string cipher2(buf_text);
   238     cout << "\n" << cipher2;
   239     pEp_free(buf_text);
   240 
   241     cout << "\nfinding English trustword for 2342...\n";
   242     char * word = NULL;
   243     size_t wsize;
   244     trustword(session, 2342, "en", &word, &wsize);
   245     assert(word);
   246     cout << "the English trustword for 2342 is " << word << endl;
   247     pEp_free(word);
   248     cout << "\nfinding French trustword for 2342...\n";
   249     trustword(session, 2342, "fr", &word, &wsize);
   250     assert(word);
   251     cout << "the French trustword for 2342 is " << word << endl;
   252     pEp_free(word);
   253 
   254     const string fingerprint = "4942 2235 FC99 585B 891C  6653 0C7B 109B FA72 61F7";
   255     char * words = NULL;
   256 
   257     cout << "\nfinding German trustwords for " << fingerprint << "...\n";
   258     trustwords(session, fingerprint.c_str(), "de", &words, &wsize, 5);
   259     assert(words);
   260     cout << words << endl;
   261     pEp_free(words);
   262 
   263     pEp_identity* identity  = new_identity(
   264             "leon.schumacher@digitalekho.com",
   265             "8BD08954C74D830EEFFB5DEB2682A17F7C87F73D",
   266             "23",
   267             "Leon Schumacher"
   268         );
   269     identity->comm_type = PEP_ct_pEp;
   270 
   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);
   277     assert(identity);
   278     cout << "set: " << identity->address << ", " << identity->fpr << ", " << identity->user_id << ", " << identity->username << endl;
   279 
   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;
   283 
   284     free_identity(identity);
   285 
   286     cout << "\ngenerating key for testuser\n";
   287     identity = new_identity(
   288             "testuser@pibit.ch",
   289             NULL,
   290             "423",
   291             "Alfred E. Neuman"
   292         );
   293 
   294     assert(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;
   299 
   300     const string key(identity->fpr);
   301     free_identity(identity);
   302 
   303     char *key_data = NULL;
   304     size_t size = 0;
   305 
   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";
   311 
   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);
   316     
   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";
   321 
   322     pEp_free(key_data);
   323     key_data=NULL;
   324 
   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);
   329 
   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);
   334     assert(keylist);
   335     cout << "found: " << keylist->value << endl;
   336     assert(keylist->next == NULL);
   337     free_stringlist(keylist);
   338 
   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);
   343 
   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);
   348 
   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);
   354     
   355     cout << "\ncalling release()\n";
   356     cout << endl << "End of pEpEngineTest for engine version " << get_engine_version() << endl;
   357 
   358     release(session);
   359     return 0;
   360 }