test/pEpEngineTest.cc
author Roker <roker@pep-project.org>
Wed, 22 Jun 2016 10:03:35 +0200
changeset 753 dd21cdff7492
parent 752 9c53a8bc555d
child 791 0c0fa91db00e
child 799 f60a61e7d403
permissions -rw-r--r--
I fixed the test that I broke yesterday.
     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 Enginge 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
    84     // A9411D176FF00E97
    85     // 
    86     // Other peers :
    87     // pEp Test Bob (test key, don't use) <pep.test.bob@pep-project.org> 
    88     //         C9C2EE39
    89     // 59BFF488C9C2EE39
    90     // 
    91     // pEp Test John (test key, don't use) <pep.test.john@pep-project.org>
    92     //         70DCF575
    93     // 135CD6D170DCF575
    94 
    95     const char *kflist[] = {
    96         "0x6FF00E97.asc",
    97         "0xC9C2EE39.asc",
    98         "0x70DCF575.asc",
    99         NULL
   100     };
   101 
   102     const char** kf = kflist;
   103     while(*kf){
   104         const Buffer k_user_buffer =  ReadFileIntoMem(*kf);
   105         cout << "import_key(" << *kf << ")\n";
   106         PEP_STATUS import_status = import_key(session, k_user_buffer.data(), k_user_buffer.size(), NULL);
   107         assert(import_status == PEP_STATUS_OK);
   108         cout << "successfully imported key\n";
   109         kf++;
   110     }
   111 
   112     const Buffer cipher_buffer = ReadFileIntoMem("msg.asc");
   113     cout << "\n" << cipher_buffer.data();
   114 
   115     char *buf_text = NULL;
   116     size_t buf_size = 0;
   117     stringlist_t *keylist;
   118 
   119     cout << "calling decrypt_and_verify()\n";
   120     PEP_STATUS decrypt_result = decrypt_and_verify(session, cipher_buffer.data(), cipher_buffer.size(), &buf_text, &buf_size, &keylist);
   121 
   122     cout << "returning from decrypt_and_verify() with result == 0x" << std::hex << decrypt_result << "\n";
   123     assert(decrypt_result == PEP_DECRYPTED_AND_VERIFIED);
   124     assert(buf_text);
   125     assert(keylist);
   126 
   127     for (stringlist_t *_keylist=keylist; _keylist!=NULL; _keylist=_keylist->next) {
   128         assert(_keylist->value);
   129         cout << "signed with " << _keylist->value << "\n";
   130     }
   131 
   132     free_stringlist(keylist);
   133     buf_text[buf_size] = 0;
   134     const string plain(buf_text);
   135     pEp_free(buf_text);
   136     cout << "\n" << plain;
   137 
   138     const Buffer t1_buffer = ReadFileIntoMem("t1.txt");
   139     const Buffer sig_buffer = ReadFileIntoMem("signature.asc");
   140 
   141     cout << "\ncalling verify_text()\n";
   142     PEP_STATUS verify_result = verify_text(session, t1_buffer.data(), t1_buffer.size(), sig_buffer.data(), sig_buffer.size(), &keylist);
   143     cout << "returning from verify_text() with result == " << verify_result << "\n";
   144     assert(verify_result == PEP_VERIFIED || verify_result == PEP_VERIFIED_AND_TRUSTED);
   145     assert(keylist->value);
   146     cout << "signed with " << keylist->value << "\n";
   147     free_stringlist(keylist);
   148 
   149     const Buffer t2_buffer = ReadFileIntoMem("t2.txt");
   150 
   151     cout << "\ncalling verify_text()\n";
   152     verify_result = verify_text(session, t2_buffer.data(), t2_buffer.size(), sig_buffer.data(), sig_buffer.size(), &keylist);
   153     assert(verify_result == PEP_DECRYPT_SIGNATURE_DOES_NOT_MATCH);
   154     free_stringlist(keylist);
   155 
   156     keylist = new_stringlist("A9411D176FF00E97");
   157     stringlist_add(keylist, "59BFF488C9C2EE39");
   158     stringlist_add(keylist, "135CD6D170DCF575");
   159 
   160     buf_text = NULL;
   161     buf_size = 0;
   162 
   163     cout << "\ncalling encrypt_and_sign()\n";
   164     PEP_STATUS encrypt_result = encrypt_and_sign(session, keylist, plain.c_str(), plain.length(), &buf_text, &buf_size);
   165     cout << "returning from encrypt_and_sign() with result == " << encrypt_result << "\n";
   166     assert(encrypt_result == PEP_STATUS_OK);
   167     free_stringlist(keylist);
   168 
   169     buf_text[buf_size] = '\0';
   170     const string cipher2(buf_text);
   171     cout << "\n" << cipher2;
   172     pEp_free(buf_text);
   173 
   174     cout << "\nfinding English trustword for 2342...\n";
   175     char * word = NULL;
   176     size_t wsize;
   177     trustword(session, 2342, "en", &word, &wsize);
   178     assert(word);
   179     cout << "the trustword for 2342 is " << word << "\n";
   180     pEp_free(word);
   181 
   182     const string fingerprint = "4942 2235 FC99 585B 891C  6653 0C7B 109B FA72 61F7";
   183     char * words = NULL;
   184 
   185     cout << "\nfinding German trustwords for " << fingerprint << "...\n";
   186     trustwords(session, fingerprint.c_str(), "de", &words, &wsize, 5);
   187     assert(words);
   188     cout << words << "\n";
   189     pEp_free(words);
   190 
   191     pEp_identity* identity  = new_identity(
   192             "leon.schumacher@digitalekho.com",
   193             "8BD08954C74D830EEFFB5DEB2682A17F7C87F73D",
   194             "23",
   195             "Leon Schumacher"
   196         );
   197     identity->comm_type = PEP_ct_pEp;
   198 
   199     cout << "\nsetting identity...\n";
   200     PEP_STATUS pep_set_result = set_identity(session, identity);
   201     assert(pep_set_result == PEP_STATUS_OK);
   202     free_identity(identity);
   203     get_identity(session, "leon.schumacher@digitalekho.com", "23", &identity);
   204     assert(identity);
   205     cout << "set: " << identity->address << ", " << identity->fpr << ", " << identity->user_id << ", " << identity->username << "\n";
   206 
   207     PEP_STATUS get_trust_result = get_trust(session, identity);
   208     assert(get_trust_result == PEP_STATUS_OK);
   209     cout << "trust of " << identity->user_id << " is " << identity->comm_type << "\n";
   210 
   211     free_identity(identity);
   212 
   213     cout << "\ngenerating key for testuser\n";
   214     identity = new_identity(
   215             "testuser@pibit.ch",
   216             NULL,
   217             "423",
   218             "Alfred E. Neuman"
   219         );
   220 
   221     assert(identity);
   222     PEP_STATUS generate_status = generate_keypair(session, identity);
   223     cout << "generate_keypair() exits with " << generate_status << "\n";
   224     assert(generate_status == PEP_STATUS_OK);
   225     cout << "generated key is " << identity->fpr << "\n";
   226 
   227     const string key(identity->fpr);
   228     free_identity(identity);
   229 
   230     char *key_data = NULL;
   231     size_t size = 0;
   232 
   233     cout << "export_key()\n\n";
   234     PEP_STATUS export_status = export_key(session, key.c_str(), &key_data, &size);
   235     cout << "export_key() exits with " << export_status << "\n";
   236     assert(export_status == PEP_STATUS_OK);
   237     cout << key_data << "\n\n";
   238 
   239     cout << "deleting key pair " << key.c_str() << "\n";
   240     PEP_STATUS delete_status = delete_keypair(session, key.c_str());
   241     cout << "delete_keypair() exits with " << delete_status << "\n";
   242     assert(delete_status == PEP_STATUS_OK);
   243     
   244     cout << "import_key()\n";
   245     PEP_STATUS import_status = import_key(session, key_data, size, NULL);
   246     assert(import_status == PEP_STATUS_OK);
   247     cout << "successfully imported key\n";
   248 
   249     pEp_free(key_data);
   250     key_data=NULL;
   251 
   252     cout << "deleting key " << key.c_str() << " again\n";
   253     delete_status = delete_keypair(session, key.c_str());
   254     cout << "delete_keypair() exits with " << delete_status << "\n";
   255     assert(delete_status == PEP_STATUS_OK);
   256 
   257     cout << "finding key for pep.test.john@pep-project.org\n";
   258     PEP_STATUS find_keys_status = find_keys(session, "pep.test.john@pep-project.org", &keylist);
   259     cout << "find_keys() exits with " << find_keys_status << "\n";
   260     assert(find_keys_status == PEP_STATUS_OK);
   261     assert(keylist);
   262     cout << "found: " << keylist->value << "\n";
   263     assert(keylist->next == NULL);
   264     free_stringlist(keylist);
   265 
   266     cout << "searching for vb@ulm.ccc.de on keyserver\n";
   267     PEP_STATUS recv_key_status = recv_key(session, "vb@ulm.ccc.de");
   268     cout << "recv_key() exits with " << recv_key_status << "\n";
   269     assert(recv_key_status == PEP_STATUS_OK);
   270 
   271     cout << "sending vb@ulm.ccc.de to keyserver\n";
   272     PEP_STATUS send_key_status = send_key(session, "vb@ulm.ccc.de");
   273     cout << "send_key() exits with " << send_key_status << "\n";
   274     assert(send_key_status == PEP_STATUS_OK);
   275 
   276     PEP_comm_type tcomm_type;
   277     PEP_STATUS tstatus = get_key_rating(session, "59BFF488C9C2EE39", &tcomm_type);
   278     cout << "get_key_rating() exits with " << tstatus << "\n";
   279     assert(tstatus == PEP_STATUS_OK);
   280     assert(tcomm_type == PEP_ct_OpenPGP_unconfirmed);
   281     
   282     cout << "\ncalling release()\n";
   283     release(session);
   284     return 0;
   285 }