Fixed pEpEngineTest
authorEdouard Tisserant
Thu, 26 Mar 2015 16:03:34 +0100
changeset 1371a61b5fab081
parent 136 c9c6abd39b06
child 138 0764a1d12ea7
Fixed pEpEngineTest
test/pEpEngineTest.cc
     1.1 --- a/test/pEpEngineTest.cc	Thu Mar 26 16:01:54 2015 +0100
     1.2 +++ b/test/pEpEngineTest.cc	Thu Mar 26 16:03:34 2015 +0100
     1.3 @@ -10,6 +10,35 @@
     1.4  
     1.5  using namespace std;
     1.6  
     1.7 +void ReadFileIntoMem(const char *fname, char* &buffer, size_t &length){
     1.8 +    buffer = NULL;
     1.9 +    length = 0;
    1.10 +	cout << "opening " << fname << " for reading\n";
    1.11 +	ifstream txtFile (fname, ifstream::binary);
    1.12 +	assert(txtFile.is_open());
    1.13 +    if (txtFile) {
    1.14 +        // get length of file:
    1.15 +        txtFile.seekg (0, txtFile.end);
    1.16 +        length = txtFile.tellg();
    1.17 +        txtFile.seekg (0, txtFile.beg);
    1.18 +
    1.19 +        buffer = new char [length];
    1.20 +
    1.21 +        cout << "Reading " << length << " characters... ";
    1.22 +        txtFile.read (buffer,length);
    1.23 +
    1.24 +        if (txtFile)
    1.25 +          cout << "all characters read successfully.\n";
    1.26 +        else
    1.27 +          cout << "error: only " << txtFile.gcount() << " could be read\n";
    1.28 +        txtFile.close();
    1.29 +    }
    1.30 +
    1.31 +	txtFile.close();
    1.32 +    assert(buffer);
    1.33 +    assert(length);
    1.34 +}
    1.35 +
    1.36  int main(int argc, char* argv[])
    1.37  {
    1.38  	PEP_SESSION session;
    1.39 @@ -32,28 +61,18 @@
    1.40  	cout << "logging test\n";
    1.41  	log_event(session, "log test", "pEp Enginge Test", "This is a logging test sample.", "please ignore this line");
    1.42  
    1.43 -	string cipher;
    1.44 +    char * cipher_buffer = NULL;
    1.45 +    size_t cipher_length = 0;
    1.46 +    ReadFileIntoMem("msg.asc", cipher_buffer, cipher_length);
    1.47  
    1.48 -	cout << "opening msc.asc for reading\n";
    1.49 -	ifstream inFile ("msg.asc");
    1.50 -	assert(inFile.is_open());
    1.51 +	cout << "\n" << cipher_buffer;
    1.52  
    1.53 -	cout << "reading cipher text of msc.asc\n";
    1.54 -	while (!inFile.eof()) {
    1.55 -		static string line;
    1.56 -		getline(inFile, line);
    1.57 -		cipher += line + "\n";
    1.58 -	}
    1.59 -	inFile.close();
    1.60 -
    1.61 -	cout << "\n" << cipher;
    1.62 -
    1.63 -	char *buf_text;
    1.64 -	size_t buf_size;
    1.65 +	char *buf_text = NULL;
    1.66 +	size_t buf_size = 0;
    1.67  	stringlist_t *keylist;
    1.68  
    1.69      cout << "calling decrypt_and_verify()\n";
    1.70 -    PEP_STATUS decrypt_result = decrypt_and_verify(session, cipher.c_str(), cipher.length(), &buf_text, &buf_size, &keylist);
    1.71 +    PEP_STATUS decrypt_result = decrypt_and_verify(session, cipher_buffer, cipher_length, &buf_text, &buf_size, &keylist);
    1.72  
    1.73      cout << "returning from decrypt_and_verify() with result == " << decrypt_result << "\n";
    1.74      assert(decrypt_result == PEP_DECRYPTED_AND_VERIFIED);
    1.75 @@ -71,69 +90,40 @@
    1.76      pEp_free(buf_text);
    1.77      cout << "\n" << plain;
    1.78  
    1.79 -    string t1, t2, sig;
    1.80 +    char * t1_buffer = NULL;
    1.81 +    size_t t1_length = 0;
    1.82 +    ReadFileIntoMem("t1.txt", t1_buffer, t1_length);
    1.83  
    1.84 -	cout << "\nopening t1.txt for reading\n";
    1.85 -	ifstream txtFile ("t1.txt");
    1.86 -	assert(txtFile.is_open());
    1.87 -
    1.88 -	cout << "reading t1 from t1.txt\n";
    1.89 -	while (!txtFile.eof()) {
    1.90 -		static string line;
    1.91 -		getline(txtFile, line);
    1.92 -		t1 += line + "\r\n";
    1.93 -	}
    1.94 -	txtFile.close();
    1.95 -    assert(t1.size());
    1.96 -    t1.erase(t1.size()-2, 2);
    1.97 -
    1.98 -	cout << "opening signature.asc for reading\n";
    1.99 -	ifstream sigFile ("signature.asc");
   1.100 -	assert(sigFile.is_open());
   1.101 -
   1.102 -	cout << "reading sig from signature.asc\n";
   1.103 -	while (!sigFile.eof()) {
   1.104 -		static string line;
   1.105 -		getline(sigFile, line);
   1.106 -		sig += line + "\n";
   1.107 -	}
   1.108 -	sigFile.close();
   1.109 +    char * sig_buffer = NULL;
   1.110 +    size_t sig_length = 0;
   1.111 +    ReadFileIntoMem("signature.asc", sig_buffer, sig_length);
   1.112  
   1.113      cout << "\ncalling verify_test()\n";
   1.114 -    PEP_STATUS verify_result = verify_text(session, t1.c_str(), t1.size(), sig.c_str(), sig.size(), &keylist);
   1.115 -    cout << "result = " << verify_result << "\n";
   1.116 +    PEP_STATUS verify_result = verify_text(session, t1_buffer, t1_length, sig_buffer, sig_length, &keylist);
   1.117      assert(verify_result == PEP_VERIFIED || verify_result == PEP_VERIFIED_AND_TRUSTED);
   1.118      assert(keylist->value);
   1.119      cout << "signed with " << keylist->value << "\n";
   1.120      free_stringlist(keylist);
   1.121  
   1.122 -	cout << "\nopening t2.txt for reading\n";
   1.123 -	ifstream txt2File ("t2.txt");
   1.124 -	assert(txt2File.is_open());
   1.125 -
   1.126 -	cout << "reading t2 from t2.txt\n";
   1.127 -	while (!txt2File.eof()) {
   1.128 -		static string line;
   1.129 -		getline(txt2File, line);
   1.130 -		t2 += line + "\r\n";
   1.131 -	}
   1.132 -	txt2File.close();
   1.133 -    assert(t2.size());
   1.134 -    t1.erase(t2.size()-2, 2);
   1.135 +    char * t2_buffer = NULL;
   1.136 +    size_t t2_length = 0;
   1.137 +    ReadFileIntoMem("t2.txt", t2_buffer, t2_length);
   1.138  
   1.139      cout << "\ncalling verify_test()\n";
   1.140 -    verify_result = verify_text(session, t2.c_str(), t2.size(), sig.c_str(), sig.size(), &keylist);
   1.141 -    cout << "result = " << verify_result << "\n";
   1.142 +    verify_result = verify_text(session, t2_buffer, t2_length, sig_buffer, sig_length, &keylist);
   1.143      assert(verify_result == PEP_DECRYPT_SIGNATURE_DOES_NOT_MATCH);
   1.144      free_stringlist(keylist);
   1.145  
   1.146 -    keylist = new_stringlist("49422235FC99585B891C66530C7B109BFA7261F7");
   1.147 +    keylist = new_stringlist("DD55BF29DF9B1541");
   1.148 +    //keylist = new_stringlist("49422235FC99585B891C66530C7B109BFA7261F7");
   1.149      // stringlist_add(keylist, "C6FAA231A2B43252B9526D119550C6B6B8B0FCD6");
   1.150      // stringlist_add(keylist, "5DC8CAC595EDAD6598DD4732DD55BF29DF9B1541");
   1.151  
   1.152 +	buf_text = NULL;
   1.153 +	buf_size = 0;
   1.154 +
   1.155      cout << "\ncalling encrypt_and_sign()\n";
   1.156      PEP_STATUS encrypt_result = encrypt_and_sign(session, keylist, plain.c_str(), plain.length(), &buf_text, &buf_size);
   1.157 -    
   1.158      cout << "returning from encrypt_and_sign() with result == " << encrypt_result << "\n";
   1.159      assert(encrypt_result == PEP_STATUS_OK);
   1.160      free_stringlist(keylist);
   1.161 @@ -143,6 +133,11 @@
   1.162      cout << "\n" << cipher2;
   1.163      pEp_free(buf_text);
   1.164  
   1.165 +    delete[] cipher_buffer;
   1.166 +    delete[] t1_buffer;
   1.167 +    delete[] sig_buffer;
   1.168 +    delete[] t2_buffer;
   1.169 +
   1.170  	cout << "\nfinding English safeword for 2342...\n";
   1.171  	char * word;
   1.172  	size_t wsize;
   1.173 @@ -242,7 +237,7 @@
   1.174      assert(recv_key_status == PEP_STATUS_OK);
   1.175  
   1.176      PEP_comm_type tcomm_type;
   1.177 -    PEP_STATUS tstatus = get_key_rating(session, "49422235FC99585B891C66530C7B109BFA7261F7", &tcomm_type);
   1.178 +    PEP_STATUS tstatus = get_key_rating(session, "DD55BF29DF9B1541", &tcomm_type);
   1.179      assert(tstatus == PEP_STATUS_OK);
   1.180      assert(tcomm_type == PEP_ct_OpenPGP_unconfirmed);
   1.181