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