test/pEpEngineTest.cc
author Roker <roker@pep-project.org>
Tue, 21 Jun 2016 17:52:03 +0200
changeset 751 78a762326f20
parent 750 56df4e0f3ab9
child 752 9c53a8bc555d
permissions -rw-r--r--
use C++, not C in pEpEngineTest, and avoid manual memory management
     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::vector<char> 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+1);
    45 
    46     cout << "Reading " << length << " characters... ";
    47     txtFile.read (buffer.data(), length);
    48     buffer.at(length)='\0';
    49 
    50     if (!txtFile)
    51     {
    52         throw std::runtime_error( "error: only " + to_string(txtFile.gcount()) + " could be read from file" + fname );
    53     }
    54 
    55     cout << "all characters read successfully." << std::endl;
    56     return buffer;
    57 }
    58 
    59 
    60 int main(int argc, char* argv[])
    61 {
    62     PEP_SESSION session;
    63 
    64     cout << "calling init()\n";
    65     PEP_STATUS init_result = init(&session);
    66     
    67     cout << "returning from init() with result == " << init_result << "\n";
    68     assert(init_result == PEP_STATUS_OK);
    69 
    70     PEP_SESSION second_session;
    71     cout << "second session test\n";
    72     PEP_STATUS second_init_result = init(&second_session);
    73     cout << "returning from second init() with result == " << second_init_result << "\n";
    74     assert(second_init_result == PEP_STATUS_OK);
    75     assert(second_session);
    76     cout << "dropping second session\n";
    77     release(second_session);
    78 
    79     cout << "logging test\n";
    80     log_event(session, "log test", "pEp Enginge Test", "This is a logging test sample.", "please ignore this line");
    81 
    82     // Our test user :
    83     // pEp Test Alice (test key don't use) <pep.test.alice@pep-project.org>
    84     //         6FF00E97
    85     // A9411D176FF00E97
    86     // 
    87     // Other peers :
    88     // pEp Test Bob (test key, don't use) <pep.test.bob@pep-project.org> 
    89     //         C9C2EE39
    90     // 59BFF488C9C2EE39
    91     // 
    92     // pEp Test John (test key, don't use) <pep.test.john@pep-project.org>
    93     //         70DCF575
    94     // 135CD6D170DCF575
    95 
    96     const char *kflist[] = {
    97         "0x6FF00E97.asc",
    98         "0xC9C2EE39.asc",
    99         "0x70DCF575.asc",
   100         NULL
   101     };
   102 
   103     const char** kf = kflist;
   104     while(*kf){
   105         const Buffer k_user_buffer =  ReadFileIntoMem(*kf);
   106         const size_t k_user_length = k_user_buffer.size();
   107         cout << "import_key(" << *kf << ")\n";
   108         PEP_STATUS import_status = import_key(session, k_user_buffer.data(), k_user_length, NULL);
   109         assert(import_status == PEP_STATUS_OK);
   110         cout << "successfully imported key\n";
   111         kf++;
   112     }
   113 
   114     Buffer cipher_buffer = ReadFileIntoMem("msg.asc");
   115     const size_t cipher_length = cipher_buffer.size();
   116 
   117     cout << "\n" << cipher_buffer.data();
   118 
   119     char *buf_text = NULL;
   120     size_t buf_size = 0;
   121     stringlist_t *keylist;
   122 
   123     cout << "calling decrypt_and_verify()\n";
   124     PEP_STATUS decrypt_result = decrypt_and_verify(session, cipher_buffer.data(), cipher_length, &buf_text, &buf_size, &keylist);
   125 
   126     cout << "returning from decrypt_and_verify() with result == 0x" << std::hex << decrypt_result << "\n";
   127     assert(decrypt_result == PEP_DECRYPTED_AND_VERIFIED);
   128     assert(buf_text);
   129     assert(keylist);
   130 
   131     for (stringlist_t *_keylist=keylist; _keylist!=NULL; _keylist=_keylist->next) {
   132         assert(_keylist->value);
   133         cout << "signed with " << _keylist->value << "\n";
   134     }
   135 
   136     free_stringlist(keylist);
   137     buf_text[buf_size] = 0;
   138     string plain(buf_text);
   139     pEp_free(buf_text);
   140     cout << "\n" << plain;
   141 
   142     Buffer t1_buffer = ReadFileIntoMem("t1.txt");
   143     const size_t t1_length = t1_buffer.size();
   144 
   145     Buffer sig_buffer = ReadFileIntoMem("signature.asc");
   146     const size_t sig_length = sig_buffer.size();
   147 
   148     cout << "\ncalling verify_text()\n";
   149     PEP_STATUS verify_result = verify_text(session, t1_buffer.data(), t1_length, sig_buffer.data(), sig_length, &keylist);
   150     cout << "returning from verify_text() with result == " << verify_result << "\n";
   151     assert(verify_result == PEP_VERIFIED || verify_result == PEP_VERIFIED_AND_TRUSTED);
   152     assert(keylist->value);
   153     cout << "signed with " << keylist->value << "\n";
   154     free_stringlist(keylist);
   155 
   156     Buffer t2_buffer = ReadFileIntoMem("t2.txt");
   157     size_t t2_length = t2_buffer.size();
   158 
   159     cout << "\ncalling verify_text()\n";
   160     verify_result = verify_text(session, t2_buffer.data(), t2_length, sig_buffer.data(), sig_length, &keylist);
   161     assert(verify_result == PEP_DECRYPT_SIGNATURE_DOES_NOT_MATCH);
   162     free_stringlist(keylist);
   163 
   164     keylist = new_stringlist("A9411D176FF00E97");
   165     stringlist_add(keylist, "59BFF488C9C2EE39");
   166     stringlist_add(keylist, "135CD6D170DCF575");
   167 
   168     buf_text = NULL;
   169     buf_size = 0;
   170 
   171     cout << "\ncalling encrypt_and_sign()\n";
   172     PEP_STATUS encrypt_result = encrypt_and_sign(session, keylist, plain.c_str(), plain.length(), &buf_text, &buf_size);
   173     cout << "returning from encrypt_and_sign() with result == " << encrypt_result << "\n";
   174     assert(encrypt_result == PEP_STATUS_OK);
   175     free_stringlist(keylist);
   176 
   177     buf_text[buf_size] = 0;
   178     string cipher2(buf_text);
   179     cout << "\n" << cipher2;
   180     pEp_free(buf_text);
   181 
   182     cout << "\nfinding English trustword for 2342...\n";
   183     char * word;
   184     size_t wsize;
   185     trustword(session, 2342, "en", &word, &wsize);
   186     assert(word);
   187     cout << "the trustword for 2342 is " << word << "\n";
   188     pEp_free(word);
   189 
   190     string fingerprint = "4942 2235 FC99 585B 891C  6653 0C7B 109B FA72 61F7";
   191     char * words;
   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     string key(identity->fpr);
   236     free_identity(identity);
   237 
   238     char *key_data;
   239     size_t size;
   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 
   259     cout << "deleting key " << key.c_str() << " again\n";
   260     delete_status = delete_keypair(session, key.c_str());
   261     cout << "delete_keypair() exits with " << delete_status << "\n";
   262     assert(delete_status == PEP_STATUS_OK);
   263 
   264     cout << "finding key for pep.test.john@pep-project.org\n";
   265     PEP_STATUS find_keys_status = find_keys(session, "pep.test.john@pep-project.org", &keylist);
   266     cout << "find_keys() exits with " << find_keys_status << "\n";
   267     assert(find_keys_status == PEP_STATUS_OK);
   268     assert(keylist);
   269     cout << "found: " << keylist->value << "\n";
   270     assert(keylist->next == NULL);
   271     free_stringlist(keylist);
   272 
   273     cout << "searching for vb@ulm.ccc.de on keyserver\n";
   274     PEP_STATUS recv_key_status = recv_key(session, "vb@ulm.ccc.de");
   275     cout << "recv_key() exits with " << recv_key_status << "\n";
   276     assert(recv_key_status == PEP_STATUS_OK);
   277 
   278     cout << "sending vb@ulm.ccc.de to keyserver\n";
   279     PEP_STATUS send_key_status = send_key(session, "vb@ulm.ccc.de");
   280     cout << "send_key() exits with " << send_key_status << "\n";
   281     assert(send_key_status == PEP_STATUS_OK);
   282 
   283     PEP_comm_type tcomm_type;
   284     PEP_STATUS tstatus = get_key_rating(session, "59BFF488C9C2EE39", &tcomm_type);
   285     cout << "get_key_rating() exits with " << tstatus << "\n";
   286     assert(tstatus == PEP_STATUS_OK);
   287     assert(tcomm_type == PEP_ct_OpenPGP_unconfirmed);
   288     
   289     cout << "\ncalling release()\n";
   290     release(session);
   291     return 0;
   292 }