test/pEpEngineTest.cc
author Edouard Tisserant
Thu, 11 Jun 2015 16:04:35 +0200
changeset 317 1946e0517a20
parent 315 fb80b70e844d
child 322 978c746d7d2c
permissions -rw-r--r--
pEpEngineTest : uses address from new test dumies for find_keys() as well
     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 	txtFile.close();
    39     assert(buffer);
    40     assert(length);
    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         "6FF00E97_sec.asc",
    81         "C9C2EE39_pub.asc",
    82         "70DCF575_pub.asc",
    83         NULL
    84     };
    85     const char** kf = kflist;
    86     while(*kf){
    87         char * k_user_buffer = NULL;
    88         size_t k_user_length = 0;
    89         ReadFileIntoMem(*kf, k_user_buffer, k_user_length);
    90         cout << "import_key(" << *kf << ")\n";
    91         PEP_STATUS import_status = import_key(session, k_user_buffer, k_user_length);
    92         assert(import_status == PEP_STATUS_OK);
    93         cout << "successfully imported key\n";
    94         delete[] k_user_buffer;
    95         kf++;
    96     }
    97 
    98     char * cipher_buffer = NULL;
    99     size_t cipher_length = 0;
   100     ReadFileIntoMem("msg.asc", cipher_buffer, cipher_length);
   101 
   102 	cout << "\n" << cipher_buffer;
   103 
   104 	char *buf_text = NULL;
   105 	size_t buf_size = 0;
   106 	stringlist_t *keylist;
   107 
   108     cout << "calling decrypt_and_verify()\n";
   109     PEP_STATUS decrypt_result = decrypt_and_verify(session, cipher_buffer, cipher_length, &buf_text, &buf_size, &keylist);
   110 
   111     cout << "returning from decrypt_and_verify() with result == " << decrypt_result << "\n";
   112     assert(decrypt_result == PEP_DECRYPTED_AND_VERIFIED);
   113     assert(buf_text);
   114     assert(keylist);
   115 
   116     for (stringlist_t *_keylist=keylist; _keylist!=NULL; _keylist=_keylist->next) {
   117         assert(_keylist->value);
   118         cout << "signed with " << _keylist->value << "\n";
   119     }
   120 
   121     free_stringlist(keylist);
   122     buf_text[buf_size] = 0;
   123     string plain(buf_text);
   124     pEp_free(buf_text);
   125     cout << "\n" << plain;
   126 
   127     char * t1_buffer = NULL;
   128     size_t t1_length = 0;
   129     ReadFileIntoMem("t1.txt", t1_buffer, t1_length);
   130 
   131     char * sig_buffer = NULL;
   132     size_t sig_length = 0;
   133     ReadFileIntoMem("signature.asc", sig_buffer, sig_length);
   134 
   135     cout << "\ncalling verify_text()\n";
   136     PEP_STATUS verify_result = verify_text(session, t1_buffer, t1_length, sig_buffer, sig_length, &keylist);
   137     cout << "returning from verify_text() with result == " << verify_result << "\n";
   138     assert(verify_result == PEP_VERIFIED || verify_result == PEP_VERIFIED_AND_TRUSTED);
   139     assert(keylist->value);
   140     cout << "signed with " << keylist->value << "\n";
   141     free_stringlist(keylist);
   142 
   143     char * t2_buffer = NULL;
   144     size_t t2_length = 0;
   145     ReadFileIntoMem("t2.txt", t2_buffer, t2_length);
   146 
   147     cout << "\ncalling verify_text()\n";
   148     verify_result = verify_text(session, t2_buffer, t2_length, sig_buffer, sig_length, &keylist);
   149     assert(verify_result == PEP_DECRYPT_SIGNATURE_DOES_NOT_MATCH);
   150     free_stringlist(keylist);
   151 
   152     keylist = new_stringlist("A9411D176FF00E97");
   153     stringlist_add(keylist, "59BFF488C9C2EE39");
   154     stringlist_add(keylist, "135CD6D170DCF575");
   155 
   156 	buf_text = NULL;
   157 	buf_size = 0;
   158 
   159     cout << "\ncalling encrypt_and_sign()\n";
   160     PEP_STATUS encrypt_result = encrypt_and_sign(session, keylist, plain.c_str(), plain.length(), &buf_text, &buf_size);
   161     cout << "returning from encrypt_and_sign() with result == " << encrypt_result << "\n";
   162     assert(encrypt_result == PEP_STATUS_OK);
   163     free_stringlist(keylist);
   164 
   165     buf_text[buf_size] = 0;
   166     string cipher2(buf_text);
   167     cout << "\n" << cipher2;
   168     pEp_free(buf_text);
   169 
   170     delete[] cipher_buffer;
   171     delete[] t1_buffer;
   172     delete[] sig_buffer;
   173     delete[] t2_buffer;
   174 
   175 	cout << "\nfinding English trustword for 2342...\n";
   176 	char * word;
   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     string fingerprint = "4942 2235 FC99 585B 891C  6653 0C7B 109B FA72 61F7";
   184     char * words;
   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;
   193 
   194     identity = new_identity(
   195             "leon.schumacher@digitalekho.com",
   196             "8BD08954C74D830EEFFB5DEB2682A17F7C87F73D",
   197             "23",
   198             "Leon Schumacher"
   199         );
   200 	identity->comm_type = PEP_ct_pEp;
   201 
   202 	cout << "\nsetting identity...\n";
   203 	PEP_STATUS pep_set_result = set_identity(session, identity);
   204 	assert(pep_set_result == PEP_STATUS_OK);
   205     free_identity(identity);
   206 	get_identity(session, "leon.schumacher@digitalekho.com", &identity);
   207 	assert(identity);
   208 	cout << "set: " << identity->address << ", " << identity->fpr << ", " << identity->user_id << ", " << identity->username << "\n";
   209 
   210     PEP_STATUS get_trust_result = get_trust(session, identity);
   211     assert(get_trust_result == PEP_STATUS_OK);
   212     cout << "trust of " << identity->user_id << " is " << identity->comm_type << "\n";
   213 
   214     free_identity(identity);
   215 
   216     cout << "\ngenerating key for testuser\n";
   217     identity = new_identity(
   218             "testuser@pibit.ch",
   219             NULL,
   220             "423",
   221             "Alfred E. Neuman"
   222         );
   223     assert(identity);
   224     PEP_STATUS generate_status = generate_keypair(session, identity);
   225     cout << "generate_keypair() exits with " << generate_status << "\n";
   226     assert(generate_status == PEP_STATUS_OK);
   227     cout << "generated key is " << identity->fpr << "\n";
   228 
   229     string key(identity->fpr);
   230     free_identity(identity);
   231 
   232     char *key_data;
   233     size_t size;
   234 
   235     cout << "export_key()\n\n";
   236     PEP_STATUS export_status = export_key(session, key.c_str(), &key_data, &size);
   237     cout << "export_key() exits with " << export_status << "\n";
   238     assert(export_status == PEP_STATUS_OK);
   239     cout << key_data << "\n\n";
   240 
   241     cout << "deleting key pair " << key.c_str() << "\n";
   242     PEP_STATUS delete_status = delete_keypair(session, key.c_str());
   243     cout << "delete_keypair() exits with " << delete_status << "\n";
   244     assert(delete_status == PEP_STATUS_OK);
   245     
   246     cout << "import_key()\n";
   247     PEP_STATUS import_status = import_key(session, key_data, size);
   248     assert(import_status == PEP_STATUS_OK);
   249     cout << "successfully imported key\n";
   250 
   251     pEp_free(key_data);
   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     assert(send_key_status == PEP_STATUS_OK);
   275 
   276     PEP_comm_type tcomm_type;
   277     PEP_STATUS tstatus = get_key_rating(session, "DD55BF29DF9B1541", &tcomm_type);
   278     assert(tstatus == PEP_STATUS_OK);
   279     assert(tcomm_type == PEP_ct_OpenPGP_unconfirmed);
   280     
   281 	cout << "\ncalling release()\n";
   282 	release(session);
   283 	return 0;
   284 }