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