test/pEpEngineTest.cc
author Krista Bennett <krista@pep-project.org>
Fri, 19 Jan 2018 14:12:02 +0100
changeset 2418 05678e77bc6a
parent 2101 be4563366499
child 2468 3a10b4f8ba41
permissions -rw-r--r--
fixed update_identity call that could be called on a myself value in outgoing_message_rating
     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:
    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     assert(pep_set_result == PEP_STATUS_OK);
   274     free_identity(identity);
   275     get_identity(session, "leon.schumacher@digitalekho.com", "23", &identity);
   276     assert(identity);
   277     cout << "set: " << identity->address << ", " << identity->fpr << ", " << identity->user_id << ", " << identity->username << endl;
   278 
   279     PEP_STATUS get_trust_result = get_trust(session, identity);
   280     assert(get_trust_result == PEP_STATUS_OK);
   281     cout << "trust of " << identity->user_id << " is " << identity->comm_type << endl;
   282 
   283     free_identity(identity);
   284 
   285     cout << "\ngenerating key for testuser\n";
   286     identity = new_identity(
   287             "testuser@pibit.ch",
   288             NULL,
   289             "423",
   290             "Alfred E. Neuman"
   291         );
   292 
   293     assert(identity);
   294     PEP_STATUS generate_status = generate_keypair(session, identity);
   295     cout << "generate_keypair() exits with " << status(generate_status) << endl;
   296     assert(generate_status == PEP_STATUS_OK);
   297     cout << "generated key is " << identity->fpr << endl;
   298 
   299     const string key(identity->fpr);
   300     free_identity(identity);
   301 
   302     char *key_data = NULL;
   303     size_t size = 0;
   304 
   305     cout << "export_key()\n\n";
   306     PEP_STATUS export_status = export_key(session, key.c_str(), &key_data, &size);
   307     cout << "export_key() exits with " << status(export_status) << endl;
   308     assert(export_status == PEP_STATUS_OK);
   309     cout << key_data << "\n\n";
   310 
   311     cout << "deleting key pair " << key.c_str() << endl;
   312     PEP_STATUS delete_status = delete_keypair(session, key.c_str());
   313     cout << "delete_keypair() exits with " << status(delete_status) << endl;
   314     assert(delete_status == PEP_STATUS_OK);
   315     
   316     cout << "import_key()\n";
   317     PEP_STATUS import_status = import_key(session, key_data, size, NULL);
   318     assert(import_status == PEP_STATUS_OK);
   319     cout << "successfully imported key\n";
   320 
   321     pEp_free(key_data);
   322     key_data=NULL;
   323 
   324     cout << "deleting key " << key.c_str() << " again\n";
   325     delete_status = delete_keypair(session, key.c_str());
   326     cout << "delete_keypair() exits with " << status(delete_status) << endl;
   327     assert(delete_status == PEP_STATUS_OK);
   328 
   329     cout << "finding key for pep.test.john@pep-project.org\n";
   330     PEP_STATUS find_keys_status = find_keys(session, "pep.test.john@pep-project.org", &keylist);
   331     cout << "find_keys() exits with " << status(find_keys_status) << endl;
   332     assert(find_keys_status == PEP_STATUS_OK);
   333     assert(keylist);
   334     cout << "found: " << keylist->value << endl;
   335     assert(keylist->next == NULL);
   336     free_stringlist(keylist);
   337 
   338     cout << "searching for vb@ulm.ccc.de on keyserver\n";
   339     PEP_STATUS recv_key_status = recv_key(session, "vb@ulm.ccc.de");
   340     cout << "recv_key() exits with " << status(recv_key_status) << endl;
   341     assert(recv_key_status == PEP_STATUS_OK);
   342 
   343     cout << "sending vb@ulm.ccc.de to keyserver\n";
   344     PEP_STATUS send_key_status = send_key(session, "vb@ulm.ccc.de");
   345     cout << "send_key() exits with " << status(send_key_status) << endl;
   346     assert(send_key_status == PEP_STATUS_OK);
   347 
   348     PEP_comm_type tcomm_type;
   349     PEP_STATUS tstatus = get_key_rating(session, "BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39", &tcomm_type);
   350     cout << "get_key_rating() exits with " << status(tstatus) << endl;
   351     assert(tstatus == PEP_STATUS_OK);
   352     assert(tcomm_type == PEP_ct_OpenPGP_unconfirmed);
   353     
   354     cout << "\ncalling release()\n";
   355     cout << endl << "End of pEpEngineTest for engine version " << get_engine_version() << endl;
   356 
   357     release(session);
   358     return 0;
   359 }