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