test/pEpEngineTest.cc
changeset 751 78a762326f20
parent 750 56df4e0f3ab9
child 752 9c53a8bc555d
     1.1 --- a/test/pEpEngineTest.cc	Tue Jun 21 17:27:45 2016 +0200
     1.2 +++ b/test/pEpEngineTest.cc	Tue Jun 21 17:52:03 2016 +0200
     1.3 @@ -1,6 +1,8 @@
     1.4  #include <iostream>
     1.5  #include <fstream>
     1.6 +#include <stdexcept>
     1.7  #include <string>
     1.8 +#include <vector>
     1.9  
    1.10  #include <assert.h>
    1.11  #include <string.h>
    1.12 @@ -8,35 +10,50 @@
    1.13  #include "../src/pEpEngine.h"
    1.14  #include "../src/keymanagement.h"
    1.15  
    1.16 +
    1.17  using namespace std;
    1.18  
    1.19 -void ReadFileIntoMem(const char *fname, char* &buffer, size_t &length){
    1.20 -    buffer = NULL;
    1.21 -    length = 0;
    1.22 +typedef std::vector<char> Buffer;
    1.23 +
    1.24 +// no C++11, yet? So do our own implementation:
    1.25 +namespace{
    1.26 +    std::string to_string(unsigned long u)
    1.27 +    {
    1.28 +        char buf[32];
    1.29 +        snprintf(buf,31, "%lu", u);
    1.30 +        return buf;
    1.31 +    }
    1.32 +}
    1.33 +
    1.34 +
    1.35 +Buffer ReadFileIntoMem(const char *fname){
    1.36      cout << "opening " << fname << " for reading\n";
    1.37      ifstream txtFile (fname, ifstream::binary);
    1.38      assert(txtFile.is_open());
    1.39 -    if (txtFile) {
    1.40 -        // get length of file:
    1.41 -        txtFile.seekg (0, txtFile.end);
    1.42 -        length = txtFile.tellg();
    1.43 -        txtFile.seekg (0, txtFile.beg);
    1.44 -
    1.45 -        buffer = new char [length+1];
    1.46 -
    1.47 -        cout << "Reading " << length << " characters... ";
    1.48 -        txtFile.read (buffer,length);
    1.49 -        buffer[length]='\0';
    1.50 -
    1.51 -        if (txtFile)
    1.52 -          cout << "all characters read successfully.\n";
    1.53 -        else
    1.54 -          cout << "error: only " << txtFile.gcount() << " could be read\n";
    1.55 -        txtFile.close();
    1.56 +    if (!txtFile)
    1.57 +    {
    1.58 +        throw std::runtime_error( "error: cannot open file \"" + std::string(fname) + "\"" );
    1.59      }
    1.60  
    1.61 -    assert(buffer);
    1.62 -    assert(length);
    1.63 +    Buffer buffer;
    1.64 +
    1.65 +    // get length of file:
    1.66 +    txtFile.seekg (0, txtFile.end);
    1.67 +    const size_t length = txtFile.tellg();
    1.68 +    txtFile.seekg (0, txtFile.beg);
    1.69 +    buffer.resize(length+1);
    1.70 +
    1.71 +    cout << "Reading " << length << " characters... ";
    1.72 +    txtFile.read (buffer.data(), length);
    1.73 +    buffer.at(length)='\0';
    1.74 +
    1.75 +    if (!txtFile)
    1.76 +    {
    1.77 +        throw std::runtime_error( "error: only " + to_string(txtFile.gcount()) + " could be read from file" + fname );
    1.78 +    }
    1.79 +
    1.80 +    cout << "all characters read successfully." << std::endl;
    1.81 +    return buffer;
    1.82  }
    1.83  
    1.84  
    1.85 @@ -85,29 +102,26 @@
    1.86  
    1.87      const char** kf = kflist;
    1.88      while(*kf){
    1.89 -        char * k_user_buffer = NULL;
    1.90 -        size_t k_user_length = 0;
    1.91 -        ReadFileIntoMem(*kf, k_user_buffer, k_user_length);
    1.92 +        const Buffer k_user_buffer =  ReadFileIntoMem(*kf);
    1.93 +        const size_t k_user_length = k_user_buffer.size();
    1.94          cout << "import_key(" << *kf << ")\n";
    1.95 -        PEP_STATUS import_status = import_key(session, k_user_buffer, k_user_length, NULL);
    1.96 +        PEP_STATUS import_status = import_key(session, k_user_buffer.data(), k_user_length, NULL);
    1.97          assert(import_status == PEP_STATUS_OK);
    1.98          cout << "successfully imported key\n";
    1.99 -        delete[] k_user_buffer;
   1.100          kf++;
   1.101      }
   1.102  
   1.103 -    char * cipher_buffer = NULL;
   1.104 -    size_t cipher_length = 0;
   1.105 -    ReadFileIntoMem("msg.asc", cipher_buffer, cipher_length);
   1.106 +    Buffer cipher_buffer = ReadFileIntoMem("msg.asc");
   1.107 +    const size_t cipher_length = cipher_buffer.size();
   1.108  
   1.109 -    cout << "\n" << cipher_buffer;
   1.110 +    cout << "\n" << cipher_buffer.data();
   1.111  
   1.112      char *buf_text = NULL;
   1.113      size_t buf_size = 0;
   1.114      stringlist_t *keylist;
   1.115  
   1.116      cout << "calling decrypt_and_verify()\n";
   1.117 -    PEP_STATUS decrypt_result = decrypt_and_verify(session, cipher_buffer, cipher_length, &buf_text, &buf_size, &keylist);
   1.118 +    PEP_STATUS decrypt_result = decrypt_and_verify(session, cipher_buffer.data(), cipher_length, &buf_text, &buf_size, &keylist);
   1.119  
   1.120      cout << "returning from decrypt_and_verify() with result == 0x" << std::hex << decrypt_result << "\n";
   1.121      assert(decrypt_result == PEP_DECRYPTED_AND_VERIFIED);
   1.122 @@ -125,28 +139,25 @@
   1.123      pEp_free(buf_text);
   1.124      cout << "\n" << plain;
   1.125  
   1.126 -    char * t1_buffer = NULL;
   1.127 -    size_t t1_length = 0;
   1.128 -    ReadFileIntoMem("t1.txt", t1_buffer, t1_length);
   1.129 +    Buffer t1_buffer = ReadFileIntoMem("t1.txt");
   1.130 +    const size_t t1_length = t1_buffer.size();
   1.131  
   1.132 -    char * sig_buffer = NULL;
   1.133 -    size_t sig_length = 0;
   1.134 -    ReadFileIntoMem("signature.asc", sig_buffer, sig_length);
   1.135 +    Buffer sig_buffer = ReadFileIntoMem("signature.asc");
   1.136 +    const size_t sig_length = sig_buffer.size();
   1.137  
   1.138      cout << "\ncalling verify_text()\n";
   1.139 -    PEP_STATUS verify_result = verify_text(session, t1_buffer, t1_length, sig_buffer, sig_length, &keylist);
   1.140 +    PEP_STATUS verify_result = verify_text(session, t1_buffer.data(), t1_length, sig_buffer.data(), sig_length, &keylist);
   1.141      cout << "returning from verify_text() with result == " << verify_result << "\n";
   1.142      assert(verify_result == PEP_VERIFIED || verify_result == PEP_VERIFIED_AND_TRUSTED);
   1.143      assert(keylist->value);
   1.144      cout << "signed with " << keylist->value << "\n";
   1.145      free_stringlist(keylist);
   1.146  
   1.147 -    char * t2_buffer = NULL;
   1.148 -    size_t t2_length = 0;
   1.149 -    ReadFileIntoMem("t2.txt", t2_buffer, t2_length);
   1.150 +    Buffer t2_buffer = ReadFileIntoMem("t2.txt");
   1.151 +    size_t t2_length = t2_buffer.size();
   1.152  
   1.153      cout << "\ncalling verify_text()\n";
   1.154 -    verify_result = verify_text(session, t2_buffer, t2_length, sig_buffer, sig_length, &keylist);
   1.155 +    verify_result = verify_text(session, t2_buffer.data(), t2_length, sig_buffer.data(), sig_length, &keylist);
   1.156      assert(verify_result == PEP_DECRYPT_SIGNATURE_DOES_NOT_MATCH);
   1.157      free_stringlist(keylist);
   1.158  
   1.159 @@ -168,11 +179,6 @@
   1.160      cout << "\n" << cipher2;
   1.161      pEp_free(buf_text);
   1.162  
   1.163 -    delete[] cipher_buffer;
   1.164 -    delete[] t1_buffer;
   1.165 -    delete[] sig_buffer;
   1.166 -    delete[] t2_buffer;
   1.167 -
   1.168      cout << "\nfinding English trustword for 2342...\n";
   1.169      char * word;
   1.170      size_t wsize;
   1.171 @@ -190,9 +196,7 @@
   1.172      cout << words << "\n";
   1.173      pEp_free(words);
   1.174  
   1.175 -    pEp_identity *identity;
   1.176 -
   1.177 -    identity = new_identity(
   1.178 +    pEp_identity* identity  = new_identity(
   1.179              "leon.schumacher@digitalekho.com",
   1.180              "8BD08954C74D830EEFFB5DEB2682A17F7C87F73D",
   1.181              "23",