test/pEpEngineTest.cc
author Volker Birk <vb@pep-project.org>
Fri, 01 May 2015 11:37:38 +0200
changeset 234 6904a5742e86
parent 206 550328f5c103
child 243 c38813173fc7
permissions -rw-r--r--
safeword => trustword in test
     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     char * cipher_buffer = NULL;
    66     size_t cipher_length = 0;
    67     ReadFileIntoMem("msg.asc", cipher_buffer, cipher_length);
    68 
    69 	cout << "\n" << cipher_buffer;
    70 
    71 	char *buf_text = NULL;
    72 	size_t buf_size = 0;
    73 	stringlist_t *keylist;
    74 
    75     cout << "calling decrypt_and_verify()\n";
    76     PEP_STATUS decrypt_result = decrypt_and_verify(session, cipher_buffer, cipher_length, &buf_text, &buf_size, &keylist);
    77 
    78     cout << "returning from decrypt_and_verify() with result == " << decrypt_result << "\n";
    79     assert(decrypt_result == PEP_DECRYPTED_AND_VERIFIED);
    80     assert(buf_text);
    81     assert(keylist);
    82 
    83     for (stringlist_t *_keylist=keylist; _keylist!=NULL; _keylist=_keylist->next) {
    84         assert(_keylist->value);
    85         cout << "signed with " << _keylist->value << "\n";
    86     }
    87 
    88     free_stringlist(keylist);
    89     buf_text[buf_size] = 0;
    90     string plain(buf_text);
    91     pEp_free(buf_text);
    92     cout << "\n" << plain;
    93 
    94     char * t1_buffer = NULL;
    95     size_t t1_length = 0;
    96     ReadFileIntoMem("t1.txt", t1_buffer, t1_length);
    97 
    98     char * sig_buffer = NULL;
    99     size_t sig_length = 0;
   100     ReadFileIntoMem("signature.asc", sig_buffer, sig_length);
   101 
   102     cout << "\ncalling verify_text()\n";
   103     PEP_STATUS verify_result = verify_text(session, t1_buffer, t1_length, sig_buffer, sig_length, &keylist);
   104     cout << "returning from verify_text() with result == " << verify_result << "\n";
   105     assert(verify_result == PEP_VERIFIED || verify_result == PEP_VERIFIED_AND_TRUSTED);
   106     assert(keylist->value);
   107     cout << "signed with " << keylist->value << "\n";
   108     free_stringlist(keylist);
   109 
   110     char * t2_buffer = NULL;
   111     size_t t2_length = 0;
   112     ReadFileIntoMem("t2.txt", t2_buffer, t2_length);
   113 
   114     cout << "\ncalling verify_text()\n";
   115     verify_result = verify_text(session, t2_buffer, t2_length, sig_buffer, sig_length, &keylist);
   116     assert(verify_result == PEP_DECRYPT_SIGNATURE_DOES_NOT_MATCH);
   117     free_stringlist(keylist);
   118 
   119     keylist = new_stringlist("DD55BF29DF9B1541");
   120     //keylist = new_stringlist("49422235FC99585B891C66530C7B109BFA7261F7");
   121     // stringlist_add(keylist, "C6FAA231A2B43252B9526D119550C6B6B8B0FCD6");
   122     // stringlist_add(keylist, "5DC8CAC595EDAD6598DD4732DD55BF29DF9B1541");
   123 
   124 	buf_text = NULL;
   125 	buf_size = 0;
   126 
   127     cout << "\ncalling encrypt_and_sign()\n";
   128     PEP_STATUS encrypt_result = encrypt_and_sign(session, keylist, plain.c_str(), plain.length(), &buf_text, &buf_size);
   129     cout << "returning from encrypt_and_sign() with result == " << encrypt_result << "\n";
   130     assert(encrypt_result == PEP_STATUS_OK);
   131     free_stringlist(keylist);
   132 
   133     buf_text[buf_size] = 0;
   134     string cipher2(buf_text);
   135     cout << "\n" << cipher2;
   136     pEp_free(buf_text);
   137 
   138     delete[] cipher_buffer;
   139     delete[] t1_buffer;
   140     delete[] sig_buffer;
   141     delete[] t2_buffer;
   142 
   143 	cout << "\nfinding English trustword for 2342...\n";
   144 	char * word;
   145 	size_t wsize;
   146 	trustword(session, 2342, "en", &word, &wsize);
   147 	assert(word);
   148 	cout << "the trustword for 2342 is " << word << "\n";
   149     pEp_free(word);
   150 
   151     string fingerprint = "4942 2235 FC99 585B 891C  6653 0C7B 109B FA72 61F7";
   152     char * words;
   153 
   154     cout << "\nfinding German trustwords for " << fingerprint << "...\n";
   155     trustwords(session, fingerprint.c_str(), "de", &words, &wsize, 5);
   156     assert(words);
   157     cout << words << "\n";
   158     pEp_free(words);
   159 
   160 	pEp_identity *identity;
   161 
   162     identity = new_identity(
   163             "leon.schumacher@digitalekho.com",
   164             "8BD08954C74D830EEFFB5DEB2682A17F7C87F73D",
   165             "23",
   166             "Leon Schumacher"
   167         );
   168 	identity->comm_type = PEP_ct_pEp;
   169 
   170 	cout << "\nsetting identity...\n";
   171 	PEP_STATUS pep_set_result = set_identity(session, identity);
   172 	assert(pep_set_result == PEP_STATUS_OK);
   173     free_identity(identity);
   174 	get_identity(session, "leon.schumacher@digitalekho.com", &identity);
   175 	assert(identity);
   176 	cout << "set: " << identity->address << ", " << identity->fpr << ", " << identity->user_id << ", " << identity->username << "\n";
   177 
   178     PEP_STATUS get_trust_result = get_trust(session, identity);
   179     assert(get_trust_result == PEP_STATUS_OK);
   180     cout << "trust of " << identity->user_id << " is " << identity->comm_type << "\n";
   181 
   182     free_identity(identity);
   183 
   184     cout << "\ngenerating key for testuser\n";
   185     identity = new_identity(
   186             "testuser@pibit.ch",
   187             NULL,
   188             "423",
   189             "Alfred E. Neuman"
   190         );
   191     assert(identity);
   192     PEP_STATUS generate_status = generate_keypair(session, identity);
   193     cout << "generate_keypair() exits with " << generate_status << "\n";
   194     assert(generate_status == PEP_STATUS_OK);
   195     cout << "generated key is " << identity->fpr << "\n";
   196 
   197     string key(identity->fpr);
   198     free_identity(identity);
   199 
   200     char *key_data;
   201     size_t size;
   202 
   203     cout << "export_key()\n\n";
   204     PEP_STATUS export_status = export_key(session, key.c_str(), &key_data, &size);
   205     assert(export_status == PEP_STATUS_OK);
   206     cout << key_data << "\n\n";
   207 
   208     cout << "deleting key pair " << key.c_str() << "\n";
   209     PEP_STATUS delete_status = delete_keypair(session, key.c_str());
   210     cout << "delete_keypair() exits with " << delete_status << "\n";
   211     assert(delete_status == PEP_STATUS_OK);
   212     
   213     cout << "import_key()\n";
   214     PEP_STATUS import_status = import_key(session, key_data, size);
   215     assert(import_status == PEP_STATUS_OK);
   216     cout << "successfully imported key\n";
   217 
   218     pEp_free(key_data);
   219 
   220     cout << "deleting key " << key.c_str() << " again\n";
   221     delete_status = delete_keypair(session, key.c_str());
   222     cout << "delete_keypair() exits with " << delete_status << "\n";
   223     assert(delete_status == PEP_STATUS_OK);
   224 
   225     cout << "finding key for outlooktest@dingens.org\n";
   226     PEP_STATUS find_keys_status = find_keys(session, "outlooktest@dingens.org", &keylist);
   227     assert(find_keys_status == PEP_STATUS_OK);
   228     assert(keylist);
   229     cout << "found: " << keylist->value << "\n";
   230     assert(keylist->next == NULL);
   231     free_stringlist(keylist);
   232 
   233     cout << "searching for vb@ulm.ccc.de on keyserver\n";
   234     PEP_STATUS recv_key_status = recv_key(session, "vb@ulm.ccc.de");
   235     assert(recv_key_status == PEP_STATUS_OK);
   236 
   237     cout << "sending vb@ulm.ccc.de to keyserver\n";
   238     PEP_STATUS send_key_status = send_key(session, "vb@ulm.ccc.de");
   239     assert(recv_key_status == PEP_STATUS_OK);
   240 
   241     PEP_comm_type tcomm_type;
   242     PEP_STATUS tstatus = get_key_rating(session, "DD55BF29DF9B1541", &tcomm_type);
   243     assert(tstatus == PEP_STATUS_OK);
   244     assert(tcomm_type == PEP_ct_OpenPGP_unconfirmed);
   245     
   246 	cout << "\ncalling release()\n";
   247 	release(session);
   248 	return 0;
   249 }