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