test/pEpEngineTest.cc
author vb
Wed, 25 Jun 2014 18:44:58 +0200
changeset 0 16f27efbef98
child 8 26cc9f0228f4
permissions -rw-r--r--
initial commit
     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 #ifdef _WIN32
    12 #define strdup _strdup
    13 #endif
    14 
    15 using namespace std;
    16 
    17 int main(int argc, char* argv[])
    18 {
    19 	PEP_SESSION session;
    20 
    21 	cout << "calling init()\n";
    22 	PEP_STATUS init_result = init(&session);
    23 	
    24     cout << "returning from init() with result == " << init_result << "\n";
    25 	assert(init_result == PEP_STATUS_OK);
    26 
    27     PEP_SESSION second_session;
    28     cout << "second session test\n";
    29     PEP_STATUS second_init_result = init(&second_session);
    30 	cout << "returning from second init() with result == " << second_init_result << "\n";
    31     assert(second_init_result == PEP_STATUS_OK);
    32     assert(second_session);
    33     cout << "dropping second session\n";
    34 	release(second_session);
    35 
    36 	cout << "logging test\n";
    37 	log_event(session, "log test", "pEp Enginge Test", "This is a logging test sample.", "please ignore this line");
    38 
    39 	string cipher;
    40 
    41 	cout << "opening msc.asc for reading\n";
    42 	ifstream inFile ("msg.asc");
    43 	assert(inFile.is_open());
    44 
    45 	cout << "reading cipher text of msc.asc\n";
    46 	while (!inFile.eof()) {
    47 		static string line;
    48 		getline(inFile, line);
    49 		cipher += line + "\n";
    50 	}
    51 	inFile.close();
    52 
    53 	cout << "\n" << cipher;
    54 
    55 	char *buf_text;
    56 	size_t buf_size;
    57 	stringlist_t *keylist;
    58 
    59     cout << "calling decrypt_and_verify()\n";
    60     PEP_STATUS decrypt_result = decrypt_and_verify(session, cipher.c_str(), cipher.length(), &buf_text, &buf_size, &keylist);
    61 
    62     cout << "returning from decrypt_and_verify() with result == " << decrypt_result << "\n";
    63     assert(decrypt_result == PEP_DECRYPTED_AND_VERIFIED);
    64     assert(buf_text);
    65     assert(keylist);
    66 
    67     for (stringlist_t *_keylist=keylist; _keylist!=NULL; _keylist=_keylist->next) {
    68         assert(_keylist->value);
    69         cout << "signed with " << _keylist->value << "\n";
    70     }
    71 
    72     free_stringlist(keylist);
    73     buf_text[buf_size] = 0;
    74     string plain(buf_text);
    75     pEp_free(buf_text);
    76     cout << "\n" << plain;
    77 
    78     string t1, t2, sig;
    79 
    80 	cout << "\nopening t1.txt for reading\n";
    81 	ifstream txtFile ("t1.txt");
    82 	assert(txtFile.is_open());
    83 
    84 	cout << "reading t1 from t1.txt\n";
    85 	while (!txtFile.eof()) {
    86 		static string line;
    87 		getline(txtFile, line);
    88 		t1 += line + "\r\n";
    89 	}
    90 	txtFile.close();
    91     assert(t1.size());
    92     t1.erase(t1.size()-2, 2);
    93 
    94 	cout << "opening signature.asc for reading\n";
    95 	ifstream sigFile ("signature.asc");
    96 	assert(sigFile.is_open());
    97 
    98 	cout << "reading sig from signature.asc\n";
    99 	while (!sigFile.eof()) {
   100 		static string line;
   101 		getline(sigFile, line);
   102 		sig += line + "\n";
   103 	}
   104 	sigFile.close();
   105 
   106     cout << "\ncalling verify_test()\n";
   107     PEP_STATUS verify_result = verify_text(session, t1.c_str(), t1.size(), sig.c_str(), sig.size(), &keylist);
   108     cout << "result = " << verify_result << "\n";
   109     assert(verify_result == PEP_VERIFIED || verify_result == PEP_VERIFIED_AND_TRUSTED);
   110     assert(keylist->value);
   111     cout << "signed with " << keylist->value << "\n";
   112     free_stringlist(keylist);
   113 
   114 	cout << "\nopening t2.txt for reading\n";
   115 	ifstream txt2File ("t2.txt");
   116 	assert(txt2File.is_open());
   117 
   118 	cout << "reading t2 from t2.txt\n";
   119 	while (!txt2File.eof()) {
   120 		static string line;
   121 		getline(txt2File, line);
   122 		t2 += line + "\r\n";
   123 	}
   124 	txt2File.close();
   125     assert(t2.size());
   126     t1.erase(t2.size()-2, 2);
   127 
   128     cout << "\ncalling verify_test()\n";
   129     verify_result = verify_text(session, t2.c_str(), t2.size(), sig.c_str(), sig.size(), &keylist);
   130     cout << "result = " << verify_result << "\n";
   131     assert(verify_result == PEP_DECRYPT_SIGNATURE_DOES_NOT_MATCH);
   132     free_stringlist(keylist);
   133 
   134     keylist = new_stringlist("FA7261F7");
   135 
   136     cout << "\ncalling encrypt_and_sign()\n";
   137     PEP_STATUS encrypt_result = encrypt_and_sign(session, keylist, plain.c_str(), plain.length(), &buf_text, &buf_size);
   138     
   139     cout << "returning from encrypt_and_sign() with result == " << encrypt_result << "\n";
   140     assert(encrypt_result == PEP_STATUS_OK);
   141     free_stringlist(keylist);
   142 
   143     buf_text[buf_size] = 0;
   144     string cipher2(buf_text);
   145     cout << "\n" << cipher2;
   146     pEp_free(buf_text);
   147 
   148 	cout << "\nfinding English safeword for 2342...\n";
   149 	char * word;
   150 	size_t wsize;
   151 	safeword(session, 2342, "en", &word, &wsize);
   152 	assert(word);
   153 	cout << "the safeword for 2342 is " << word << "\n";
   154     pEp_free(word);
   155 
   156     string fingerprint = "4942 2235 FC99 585B 891C  6653 0C7B 109B FA72 61F7";
   157     char * words;
   158 
   159     cout << "\nfinding German safewords for " << fingerprint << "...\n";
   160     safewords(session, fingerprint.c_str(), "de", &words, &wsize, 5);
   161     assert(words);
   162     cout << words << "\n";
   163     pEp_free(words);
   164 
   165 	pEp_identity *identity;
   166 
   167     identity = new_identity(
   168             strdup("leon.schumacher@digitalekho.com"),
   169             strdup("8BD08954C74D830EEFFB5DEB2682A17F7C87F73D"),
   170             strdup("23"),
   171             strdup("Leon Schumacher")
   172         );
   173 	identity->comm_type = PEP_ct_pEp;
   174 
   175 	cout << "\nsetting identity...\n";
   176 	PEP_STATUS pep_set_result = set_identity(session, identity);
   177 	assert(pep_set_result == PEP_STATUS_OK);
   178     free_identity(identity);
   179 	get_identity(session, "leon.schumacher@digitalekho.com", &identity);
   180 	assert(identity);
   181 	cout << "set: " << identity->address << ", " << identity->fpr << ", " << identity->user_id << ", " << identity->username << "\n";
   182     free_identity(identity);
   183 
   184     stringlist_t *addresses = new_stringlist("leon.schumacher@digitalekho.com");
   185     PEP_comm_type comm_type;
   186     cout << "\nretrieving communication type for leon.schumacher@digitalekho.com\n";
   187     PEP_STATUS oct_result = outgoing_comm_type(session, addresses, &comm_type);
   188     cout << "communication type is " << comm_type << "\n";
   189     free_stringlist(addresses);
   190     assert(oct_result == PEP_STATUS_OK && comm_type == PEP_ct_pEp);
   191 
   192     stringlist_t *addresses2 = new_stringlist("leon.schumacher@digitalekho.com");
   193     stringlist_add(addresses2, "this.email@is.invalid");
   194     cout << "\nretrieving communication type for an unknown address\n";
   195     oct_result = outgoing_comm_type(session, addresses2, &comm_type);
   196     cout << "communication type is " << comm_type << "\n";
   197     cout << "status is " << oct_result << "\n";
   198     free_stringlist(addresses2);
   199     assert(oct_result == PEP_STATUS_OK && comm_type == PEP_ct_no_encryption);
   200 
   201     cout << "\ngenerating key for testuser\n";
   202     identity = new_identity(
   203             strdup("testuser@pibit.ch"),
   204             NULL,
   205             strdup("423"),
   206             strdup("Alfred E. Neuman")
   207         );
   208     assert(identity);
   209     PEP_STATUS generate_status = generate_keypair(session, identity);
   210     cout << "generate_keypair() exits with " << generate_status << "\n";
   211     assert(generate_status == PEP_STATUS_OK);
   212     cout << "generated key is " << identity->fpr << "\n";
   213 
   214     string key(identity->fpr);
   215     free_identity(identity);
   216 
   217     char *key_data;
   218     size_t size;
   219 
   220     cout << "export_key()\n\n";
   221     PEP_STATUS export_status = export_key(session, key.c_str(), &key_data, &size);
   222     assert(export_status == PEP_STATUS_OK);
   223     cout << key_data << "\n\n";
   224 
   225     cout << "deleting key pair " << key.c_str() << "\n";
   226     PEP_STATUS delete_status = delete_keypair(session, key.c_str());
   227     cout << "delete_keypair() exits with " << delete_status << "\n";
   228     assert(delete_status == PEP_STATUS_OK);
   229     
   230     cout << "import_key()\n";
   231     PEP_STATUS import_status = import_key(session, key_data, size);
   232     assert(import_status == PEP_STATUS_OK);
   233     cout << "successfully imported key\n";
   234 
   235     pEp_free(key_data);
   236 
   237     cout << "deleting key " << key.c_str() << " again\n";
   238     delete_status = delete_keypair(session, key.c_str());
   239     cout << "delete_keypair() exits with " << delete_status << "\n";
   240     assert(delete_status == PEP_STATUS_OK);
   241 
   242     cout << "finding key for outlooktest@dingens.org\n";
   243     PEP_STATUS find_keys_status = find_keys(session, "outlooktest@dingens.org", &keylist);
   244     assert(find_keys_status == PEP_STATUS_OK);
   245     assert(keylist);
   246     cout << "found: " << keylist->value << "\n";
   247     assert(keylist->next == NULL);
   248     free_stringlist(keylist);
   249 
   250     cout << "searching for vb@ulm.ccc.de on keyserver\n";
   251     PEP_STATUS recv_key_status = recv_key(session, "vb@ulm.ccc.de");
   252     assert(recv_key_status == PEP_STATUS_OK);
   253 
   254     cout << "sending vb@ulm.ccc.de to keyserver\n";
   255     PEP_STATUS send_key_status = send_key(session, "vb@ulm.ccc.de");
   256     assert(recv_key_status == PEP_STATUS_OK);
   257 
   258 	cout << "\ncalling release()\n";
   259 	release(session);
   260 	return 0;
   261 }