test/src/util/test_util.cc
branchsync_and_key_reset
changeset 2955 3b2500a15709
parent 2848 641d523b9c0f
parent 2917 51f4afe41dcb
child 2956 e82181973f21
     1.1 --- a/test/src/util/test_util.cc	Sat Sep 15 17:40:17 2018 +0200
     1.2 +++ b/test/src/util/test_util.cc	Thu Sep 27 17:34:45 2018 +0200
     1.3 @@ -2,6 +2,7 @@
     1.4  #include "pEpEngine.h"
     1.5  #include "pEp_internal.h"
     1.6  #include "message_api.h"
     1.7 +#include "test_util.h"
     1.8  
     1.9  #include <fstream>
    1.10  #include <sstream>
    1.11 @@ -13,6 +14,57 @@
    1.12  #include <unistd.h>
    1.13  #include <ftw.h>
    1.14  
    1.15 +PEP_STATUS read_file_and_import_key(PEP_SESSION session, const char* fname) {
    1.16 +    const std::string key = slurp(fname);
    1.17 +    PEP_STATUS status = (key.empty() ? PEP_KEY_NOT_FOUND : PEP_STATUS_OK);
    1.18 +    if (status == PEP_STATUS_OK)
    1.19 +        status = import_key(session, key.c_str(), key.size(), NULL);
    1.20 +    return status;    
    1.21 +}
    1.22 +
    1.23 +PEP_STATUS set_up_ident_from_scratch(PEP_SESSION session,
    1.24 +                                     const char* key_fname,
    1.25 +                                     const char* address,
    1.26 +                                     const char* fpr,
    1.27 +                                     const char* user_id,
    1.28 +                                     const char* username,
    1.29 +                                     pEp_identity** ret_ident,
    1.30 +                                     bool is_priv) {
    1.31 +    PEP_STATUS status = read_file_and_import_key(session,key_fname);
    1.32 +    if (status != PEP_STATUS_OK)
    1.33 +        return status;
    1.34 +    
    1.35 +    pEp_identity* ident = new_identity(address, fpr, user_id, username);
    1.36 +    if (is_priv && fpr) {
    1.37 +        status = set_own_key(session, ident, fpr);
    1.38 +        if (status == PEP_STATUS_OK)
    1.39 +            status = myself(session, ident);
    1.40 +    }
    1.41 +    else    
    1.42 +        status = update_identity(session, ident);
    1.43 +
    1.44 +    if (status != PEP_STATUS_OK)
    1.45 +        goto pep_free;
    1.46 +        
    1.47 +    if (!ident || !ident->fpr) {
    1.48 +        status = PEP_CANNOT_FIND_IDENTITY;
    1.49 +        goto pep_free;
    1.50 +    }
    1.51 +    
    1.52 +    if (ret_ident)
    1.53 +        *ret_ident = ident;
    1.54 +        
    1.55 +pep_free:
    1.56 +    if (!ret_ident)
    1.57 +        free_identity(ident);
    1.58 +}
    1.59 +
    1.60 +
    1.61 +bool file_exists(std::string filename) {
    1.62 +    struct stat buffer;
    1.63 +    return (stat(filename.c_str(), &buffer) == 0);
    1.64 +}
    1.65 +
    1.66  char* str_to_lower(const char* str) {
    1.67      if (!str)
    1.68          return NULL;