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