test/src/util/test_util.cc
author Krista Bennett <krista@pep-project.org>
Fri, 31 Aug 2018 14:50:54 +0200
branchENGINE-398
changeset 2917 51f4afe41dcb
parent 2798 e0abb44b8098
child 2955 3b2500a15709
permissions -rw-r--r--
ENGINE-398: bug fixes
roker@1656
     1
#include "pEpEngine_test.h"
krista@1812
     2
#include "pEpEngine.h"
krista@2444
     3
#include "pEp_internal.h"
krista@1812
     4
#include "message_api.h"
krista@2917
     5
#include "test_util.h"
krista@2703
     6
roker@1656
     7
#include <fstream>
roker@1656
     8
#include <sstream>
roker@1656
     9
#include <stdexcept>
krista@2703
    10
#include <stdlib.h>
krista@2703
    11
#include <sys/stat.h>
krista@2703
    12
#include <errno.h>
krista@2703
    13
#include <stdlib.h>
krista@2703
    14
#include <unistd.h>
krista@2703
    15
#include <ftw.h>
roker@1656
    16
krista@2917
    17
PEP_STATUS read_file_and_import_key(PEP_SESSION session, const char* fname) {
krista@2917
    18
    const std::string key = slurp(fname);
krista@2917
    19
    PEP_STATUS status = (key.empty() ? PEP_KEY_NOT_FOUND : PEP_STATUS_OK);
krista@2917
    20
    if (status == PEP_STATUS_OK)
krista@2917
    21
        status = import_key(session, key.c_str(), key.size(), NULL);
krista@2917
    22
    return status;    
krista@2917
    23
}
krista@2917
    24
krista@2917
    25
PEP_STATUS set_up_ident_from_scratch(PEP_SESSION session,
krista@2917
    26
                                     const char* key_fname,
krista@2917
    27
                                     const char* address,
krista@2917
    28
                                     const char* fpr,
krista@2917
    29
                                     const char* user_id,
krista@2917
    30
                                     const char* username,
krista@2917
    31
                                     pEp_identity** ret_ident,
krista@2917
    32
                                     bool is_priv) {
krista@2917
    33
    PEP_STATUS status = read_file_and_import_key(session,key_fname);
krista@2917
    34
    if (status != PEP_STATUS_OK)
krista@2917
    35
        return status;
krista@2917
    36
    
krista@2917
    37
    pEp_identity* ident = new_identity(address, fpr, user_id, username);
krista@2917
    38
    if (is_priv && fpr) {
krista@2917
    39
        status = set_own_key(session, ident, fpr);
krista@2917
    40
        if (status == PEP_STATUS_OK)
krista@2917
    41
            status = myself(session, ident);
krista@2917
    42
    }
krista@2917
    43
    else    
krista@2917
    44
        status = update_identity(session, ident);
krista@2917
    45
krista@2917
    46
    if (status != PEP_STATUS_OK)
krista@2917
    47
        goto pep_free;
krista@2917
    48
        
krista@2917
    49
    if (!ident || !ident->fpr) {
krista@2917
    50
        status = PEP_CANNOT_FIND_IDENTITY;
krista@2917
    51
        goto pep_free;
krista@2917
    52
    }
krista@2917
    53
    
krista@2917
    54
    if (ret_ident)
krista@2917
    55
        *ret_ident = ident;
krista@2917
    56
        
krista@2917
    57
pep_free:
krista@2917
    58
    if (!ret_ident)
krista@2917
    59
        free_identity(ident);
krista@2917
    60
}
krista@2917
    61
krista@2917
    62
krista@2798
    63
bool file_exists(std::string filename) {
krista@2798
    64
    struct stat buffer;
krista@2798
    65
    return (stat(filename.c_str(), &buffer) == 0);
krista@2798
    66
}
krista@2798
    67
krista@2533
    68
char* str_to_lower(const char* str) {
krista@2533
    69
    if (!str)
krista@2533
    70
        return NULL;
krista@2533
    71
    int str_len = strlen(str);
krista@2533
    72
    if (str_len == 0)
krista@2533
    73
        return strdup("");
krista@2533
    74
    int i;
krista@2533
    75
    
krista@2533
    76
    char* retval = (char*) calloc(1, str_len + 1);
krista@2533
    77
    for (i = 0; i < str_len; i++) {
krista@2533
    78
        retval[i] = tolower(str[i]);
krista@2533
    79
    }    
krista@2533
    80
    return retval;
krista@2533
    81
}
krista@2533
    82
krista@2533
    83
// Because annoyed
krista@2533
    84
bool _streq(const char* str1, const char* str2) {
krista@2533
    85
    if (!str1) {
krista@2533
    86
        if (str2)
krista@2533
    87
            return false;
krista@2533
    88
        return true;
krista@2533
    89
    }
krista@2533
    90
    if (!str2)
krista@2533
    91
        return false;
krista@2533
    92
        
krista@2533
    93
    return (strcmp(str1, str2) == 0);
krista@2533
    94
}
krista@2533
    95
krista@2533
    96
bool _strceq(const char* str1, const char* str2) {
krista@2533
    97
    char* str1_dup = str_to_lower(str1);
krista@2533
    98
    char* str2_dup = str_to_lower(str2);
krista@2533
    99
krista@2533
   100
    bool retval = _streq(str_to_lower(str1_dup), str_to_lower(str2_dup));
krista@2533
   101
    free(str1_dup);
krista@2533
   102
    free(str2_dup);
krista@2533
   103
    return retval;
krista@2533
   104
}
krista@2477
   105
krista@2477
   106
void test_init() {
krista@2477
   107
    unlink ("../test_home/.pEp_management.db");
krista@2477
   108
    unlink ("../test_home/.pEp_management.db-shm");
krista@2477
   109
    unlink ("../test_home/.pEp_management.db-wal");
krista@2477
   110
}
krista@2477
   111
roker@1656
   112
std::string slurp(const std::string& filename)
roker@1656
   113
{
roker@1656
   114
	std::ifstream input(filename.c_str());
roker@1656
   115
	if(!input)
roker@1656
   116
	{
roker@1656
   117
		throw std::runtime_error("Cannot read file \"" + filename + "\"! ");
roker@1656
   118
	}
roker@1656
   119
	
roker@1656
   120
	std::stringstream sstr;
roker@1656
   121
	sstr << input.rdbuf();
roker@1656
   122
	return sstr.str();
roker@1656
   123
}
krista@1812
   124
krista@2282
   125
void dump_out(const char* filename, const char* outdata)
krista@2282
   126
{
krista@2282
   127
	std::ofstream outfile(filename);
krista@2282
   128
	if(!outfile)
krista@2282
   129
	{
krista@2282
   130
		throw std::runtime_error("Cannot open output file!");
krista@2282
   131
	}
krista@2282
   132
	
krista@2282
   133
	outfile << outdata;
krista@2282
   134
    outfile.close();
krista@2282
   135
}
krista@2282
   136
krista@2444
   137
char* get_new_uuid() {
krista@2444
   138
    char* new_uuid = (char*)calloc(37, 1);
krista@2444
   139
    pEpUUID uuid;
krista@2444
   140
    uuid_generate_random(uuid);
krista@2444
   141
    uuid_unparse_upper(uuid, new_uuid);
krista@2444
   142
    return new_uuid;
krista@2444
   143
}
krista@2282
   144
krista@1812
   145
const char* tl_status_string(PEP_STATUS status) {
krista@1812
   146
    switch (status) {
krista@1812
   147
        case PEP_STATUS_OK:
krista@1812
   148
            return "PEP_STATUS_OK";
krista@1812
   149
        case PEP_INIT_CANNOT_LOAD_GPGME:
krista@1812
   150
            return "PEP_INIT_CANNOT_LOAD_GPGME";
krista@1812
   151
        case PEP_INIT_GPGME_INIT_FAILED:
krista@1812
   152
            return "PEP_INIT_GPGME_INIT_FAILED";
krista@1812
   153
        case PEP_INIT_NO_GPG_HOME:
krista@1812
   154
            return "PEP_INIT_NO_GPG_HOME";
krista@1812
   155
        case PEP_INIT_NETPGP_INIT_FAILED:
krista@1812
   156
            return "PEP_INIT_NETPGP_INIT_FAILED";
krista@1812
   157
        case PEP_INIT_SQLITE3_WITHOUT_MUTEX:
krista@1812
   158
            return "PEP_INIT_SQLITE3_WITHOUT_MUTEX";
krista@1812
   159
        case PEP_INIT_CANNOT_OPEN_DB:
krista@1812
   160
            return "PEP_INIT_CANNOT_OPEN_DB";
krista@1812
   161
        case PEP_INIT_CANNOT_OPEN_SYSTEM_DB:
krista@1812
   162
            return "PEP_INIT_CANNOT_OPEN_SYSTEM_DB";
krista@1812
   163
        case PEP_KEY_NOT_FOUND:
krista@1812
   164
            return "PEP_KEY_NOT_FOUND";
krista@1812
   165
        case PEP_KEY_HAS_AMBIG_NAME:
krista@1812
   166
            return "PEP_KEY_HAS_AMBIG_NAME";
krista@1812
   167
        case PEP_GET_KEY_FAILED:
krista@1812
   168
            return "PEP_GET_KEY_FAILED";
krista@1812
   169
        case PEP_CANNOT_EXPORT_KEY:
krista@1812
   170
            return "PEP_CANNOT_EXPORT_KEY";
krista@1812
   171
        case PEP_CANNOT_EDIT_KEY:
krista@1812
   172
            return "PEP_CANNOT_EDIT_KEY";
krista@1812
   173
        case PEP_CANNOT_FIND_IDENTITY:
krista@1812
   174
            return "PEP_CANNOT_FIND_IDENTITY";
krista@1812
   175
        case PEP_CANNOT_SET_PERSON:
krista@1812
   176
            return "PEP_CANNOT_SET_PERSON";
krista@1812
   177
        case PEP_CANNOT_SET_PGP_KEYPAIR:
krista@1812
   178
            return "PEP_CANNOT_SET_PGP_KEYPAIR";
krista@1812
   179
        case PEP_CANNOT_SET_IDENTITY:
krista@1812
   180
            return "PEP_CANNOT_SET_IDENTITY";
krista@1812
   181
        case PEP_CANNOT_SET_TRUST:
krista@1812
   182
            return "PEP_CANNOT_SET_TRUST";
krista@1812
   183
        case PEP_KEY_BLACKLISTED:
krista@1812
   184
            return "PEP_KEY_BLACKLISTED";
krista@1812
   185
        case PEP_UNENCRYPTED:
krista@1812
   186
            return "PEP_UNENCRYPTED";
krista@1812
   187
        case PEP_VERIFIED:
krista@1812
   188
            return "PEP_VERIFIED";
krista@1812
   189
        case PEP_DECRYPTED:
krista@1812
   190
            return "PEP_DECRYPTED";
krista@1812
   191
        case PEP_DECRYPTED_AND_VERIFIED:
krista@1812
   192
            return "PEP_DECRYPTED_AND_VERIFIED";
krista@1812
   193
        case PEP_DECRYPT_WRONG_FORMAT:
krista@1812
   194
            return "PEP_DECRYPT_WRONG_FORMAT";
krista@1812
   195
        case PEP_DECRYPT_NO_KEY:
krista@1812
   196
            return "PEP_DECRYPT_NO_KEY";
krista@1812
   197
        case PEP_DECRYPT_SIGNATURE_DOES_NOT_MATCH:
krista@1812
   198
            return "PEP_DECRYPT_SIGNATURE_DOES_NOT_MATCH";
krista@2703
   199
//        case PEP_DECRYPTED_BUT_UNSIGNED:
krista@2703
   200
//            return "PEP_DECRYPTED_BUT_UNSIGNED";
krista@2703
   201
//        case PEP_DECRYPT_MODIFICATION_DETECTED:
krista@2703
   202
//            return "PEP_DECRYPT_MODIFICATION_DETECTED";
krista@2703
   203
//        case PEP_DECRYPT_NO_KEY_FOR_SIGNER:
krista@2703
   204
//            return "PEP_DECRYPT_NO_KEY_FOR_SIGNER";
krista@1812
   205
        case PEP_VERIFY_NO_KEY:
krista@1812
   206
            return "PEP_VERIFY_NO_KEY";
krista@1812
   207
        case PEP_VERIFIED_AND_TRUSTED:
krista@1812
   208
            return "PEP_VERIFIED_AND_TRUSTED";
krista@1812
   209
        case PEP_CANNOT_DECRYPT_UNKNOWN:
krista@1812
   210
            return "PEP_CANNOT_DECRYPT_UNKNOWN";
krista@1812
   211
        case PEP_TRUSTWORD_NOT_FOUND:
krista@1812
   212
            return "PEP_TRUSTWORD_NOT_FOUND";
krista@1812
   213
        case PEP_TRUSTWORDS_FPR_WRONG_LENGTH:
krista@1812
   214
            return "PEP_TRUSTWORDS_FPR_WRONG_LENGTH";
krista@1812
   215
        case PEP_CANNOT_CREATE_KEY:
krista@1812
   216
            return "PEP_CANNOT_CREATE_KEY";
krista@1812
   217
        case PEP_CANNOT_SEND_KEY:
krista@1812
   218
            return "PEP_CANNOT_SEND_KEY";
krista@1812
   219
        case PEP_PHRASE_NOT_FOUND:
krista@1812
   220
            return "PEP_PHRASE_NOT_FOUND";
krista@1812
   221
        case PEP_SEND_FUNCTION_NOT_REGISTERED:
krista@1812
   222
            return "PEP_SEND_FUNCTION_NOT_REGISTERED";
krista@1812
   223
        case PEP_CONTRAINTS_VIOLATED:
krista@1812
   224
            return "PEP_CONTRAINTS_VIOLATED";
krista@1812
   225
        case PEP_CANNOT_ENCODE:
krista@1812
   226
            return "PEP_CANNOT_ENCODE";
krista@1812
   227
        case PEP_SYNC_NO_NOTIFY_CALLBACK:
krista@1812
   228
            return "PEP_SYNC_NO_NOTIFY_CALLBACK";
krista@1812
   229
        case PEP_SYNC_ILLEGAL_MESSAGE:
krista@1812
   230
            return "PEP_SYNC_ILLEGAL_MESSAGE";
krista@1812
   231
        case PEP_SYNC_NO_INJECT_CALLBACK:
krista@1812
   232
            return "PEP_SYNC_NO_INJECT_CALLBACK";
krista@1812
   233
        case PEP_SEQUENCE_VIOLATED:
krista@1812
   234
            return "PEP_SEQUENCE_VIOLATED";
krista@1812
   235
        case PEP_CANNOT_INCREASE_SEQUENCE:
krista@1812
   236
            return "PEP_CANNOT_INCREASE_SEQUENCE";
krista@1812
   237
        case PEP_CANNOT_SET_SEQUENCE_VALUE:
krista@1812
   238
            return "PEP_CANNOT_SET_SEQUENCE_VALUE";
krista@1812
   239
        case PEP_OWN_SEQUENCE:
krista@1812
   240
            return "PEP_OWN_SEQUENCE";
krista@1812
   241
        case PEP_SYNC_STATEMACHINE_ERROR:
krista@1812
   242
            return "PEP_SYNC_STATEMACHINE_ERROR";
krista@1812
   243
        case PEP_SYNC_NO_TRUST:
krista@1812
   244
            return "PEP_SYNC_NO_TRUST";
krista@1812
   245
        case PEP_STATEMACHINE_INVALID_STATE:
krista@1812
   246
            return "PEP_STATEMACHINE_INVALID_STATE";
krista@1812
   247
        case PEP_STATEMACHINE_INVALID_EVENT:
krista@1812
   248
            return "PEP_STATEMACHINE_INVALID_EVENT";
krista@1812
   249
        case PEP_STATEMACHINE_INVALID_CONDITION:
krista@1812
   250
            return "PEP_STATEMACHINE_INVALID_CONDITION";
krista@1812
   251
        case PEP_STATEMACHINE_INVALID_ACTION:
krista@1812
   252
            return "PEP_STATEMACHINE_INVALID_ACTION";
krista@1812
   253
        case PEP_STATEMACHINE_INHIBITED_EVENT:
krista@1812
   254
            return "PEP_STATEMACHINE_INHIBITED_EVENT";
krista@1812
   255
        case PEP_COMMIT_FAILED:
krista@1812
   256
            return "PEP_COMMIT_FAILED";
krista@1812
   257
        case PEP_MESSAGE_CONSUME:
krista@1812
   258
            return "PEP_MESSAGE_CONSUME";
krista@1812
   259
        case PEP_MESSAGE_IGNORE:
krista@1812
   260
            return "PEP_MESSAGE_IGNORE";
krista@1812
   261
        case PEP_RECORD_NOT_FOUND:
krista@1812
   262
            return "PEP_RECORD_NOT_FOUND";
krista@1812
   263
        case PEP_CANNOT_CREATE_TEMP_FILE:
krista@1812
   264
            return "PEP_CANNOT_CREATE_TEMP_FILE";
krista@1812
   265
        case PEP_ILLEGAL_VALUE:
krista@1812
   266
            return "PEP_ILLEGAL_VALUE";
krista@1812
   267
        case PEP_BUFFER_TOO_SMALL:
krista@1812
   268
            return "PEP_BUFFER_TOO_SMALL";
krista@1812
   269
        case PEP_OUT_OF_MEMORY:
krista@1812
   270
            return "PEP_OUT_OF_MEMORY";
krista@1812
   271
        case PEP_UNKNOWN_ERROR:
krista@2703
   272
            return "PEP_UNKNOWN_ERROR";    
krista@1812
   273
        default:
krista@2630
   274
 
krista@1812
   275
            return "PEP_STATUS_OMGWTFBBQ - This means you're using a status the test lib doesn't know about!";
krista@1812
   276
    }
krista@1812
   277
}
krista@1812
   278
const char* tl_rating_string(PEP_rating rating) {
krista@1812
   279
    switch (rating) {
krista@1812
   280
        case PEP_rating_undefined:
krista@1812
   281
            return "PEP_rating_undefined";
krista@1812
   282
        case PEP_rating_cannot_decrypt:
krista@1812
   283
            return "PEP_rating_cannot_decrypt";
krista@1812
   284
        case PEP_rating_have_no_key:
krista@1812
   285
            return "PEP_rating_have_no_key";
krista@1812
   286
        case PEP_rating_unencrypted:
krista@1812
   287
            return "PEP_rating_unencrypted";
krista@1812
   288
        case PEP_rating_unencrypted_for_some:
krista@1812
   289
            return "PEP_rating_unencrypted_for_some";
krista@1812
   290
        case PEP_rating_unreliable:
krista@1812
   291
            return "PEP_rating_unreliable";
krista@1812
   292
        case PEP_rating_reliable:
krista@1812
   293
            return "PEP_rating_reliable";
krista@1812
   294
        case PEP_rating_trusted:
krista@1812
   295
            return "PEP_rating_trusted";
krista@1812
   296
        case PEP_rating_trusted_and_anonymized:
krista@1812
   297
            return "PEP_rating_trusted_and_anonymized";
krista@1812
   298
        case PEP_rating_fully_anonymous:
krista@1812
   299
            return "PEP_rating_fully_anonymous";
krista@1812
   300
        case PEP_rating_mistrust:
krista@1812
   301
            return "PEP_rating_mistrust";
krista@1812
   302
        case PEP_rating_b0rken:
krista@1812
   303
            return "PEP_rating_b0rken";
krista@1812
   304
        case PEP_rating_under_attack:
krista@1812
   305
            return "PEP_rating_under_attack";
krista@1812
   306
        default:
krista@1812
   307
            return "PEP_rating_OMGWTFBBQ - in other words, INVALID RATING VALUE!!!\n\nSomething bad is going on here, or a new rating value has been added to the enum and not the test function.";
krista@1812
   308
    }
krista@1812
   309
}
krista@1812
   310
krista@1812
   311
const char* tl_ct_string(PEP_comm_type ct) {
krista@1812
   312
    switch (ct) {
krista@1812
   313
        case PEP_ct_unknown:
krista@1812
   314
            return "PEP_ct_unknown";
krista@1812
   315
        case PEP_ct_no_encryption:
krista@1812
   316
            return "PEP_ct_no_encryption";
krista@1812
   317
        case PEP_ct_no_encrypted_channel:
krista@1812
   318
            return "PEP_ct_no_encrypted_channel";
krista@1812
   319
        case PEP_ct_key_not_found:
krista@1812
   320
            return "PEP_ct_key_not_found";
krista@1812
   321
        case PEP_ct_key_expired:
krista@1812
   322
            return "PEP_ct_key_expired";
krista@1812
   323
        case PEP_ct_key_revoked:
krista@1812
   324
            return "PEP_ct_key_revoked";
krista@1812
   325
        case PEP_ct_key_b0rken:
krista@1812
   326
            return "PEP_ct_key_b0rken";
krista@1812
   327
        case PEP_ct_my_key_not_included:
krista@1812
   328
            return "PEP_ct_my_key_not_included";
krista@1812
   329
        case PEP_ct_security_by_obscurity:
krista@1812
   330
            return "PEP_ct_security_by_obscurity";
krista@1812
   331
        case PEP_ct_b0rken_crypto:
krista@1812
   332
            return "PEP_ct_b0rken_crypto";
krista@1812
   333
        case PEP_ct_key_too_short:
krista@1812
   334
            return "PEP_ct_key_too_short";
krista@2593
   335
        case PEP_ct_compromised:
krista@2593
   336
            return "PEP_ct_compromised";
krista@1812
   337
        case PEP_ct_mistrusted:
krista@1812
   338
            return "PEP_ct_mistrusted";
krista@1812
   339
        case PEP_ct_unconfirmed_encryption:
krista@1812
   340
            return "PEP_ct_unconfirmed_encryption";
krista@1812
   341
        case PEP_ct_OpenPGP_weak_unconfirmed:
krista@1812
   342
            return "PEP_ct_OpenPGP_weak_unconfirmed";
krista@1812
   343
        case PEP_ct_to_be_checked:
krista@1812
   344
            return "PEP_ct_to_be_checked";
krista@1812
   345
        case PEP_ct_SMIME_unconfirmed:
krista@1812
   346
            return "PEP_ct_SMIME_unconfirmed";
krista@1812
   347
        case PEP_ct_CMS_unconfirmed:
krista@1812
   348
            return "PEP_ct_CMS_unconfirmed";
krista@1812
   349
        case PEP_ct_strong_but_unconfirmed:
krista@1812
   350
            return "PEP_ct_strong_but_unconfirmed";
krista@1812
   351
        case PEP_ct_OpenPGP_unconfirmed:
krista@1812
   352
            return "PEP_ct_OpenPGP_unconfirmed";
krista@1812
   353
        case PEP_ct_OTR_unconfirmed:
krista@1812
   354
            return "PEP_ct_OTR_unconfirmed";
krista@1812
   355
        case PEP_ct_unconfirmed_enc_anon:
krista@1812
   356
            return "PEP_ct_unconfirmed_enc_anon";
krista@1812
   357
        case PEP_ct_pEp_unconfirmed:
krista@1812
   358
            return "PEP_ct_pEp_unconfirmed";
krista@1812
   359
        case PEP_ct_confirmed:
krista@1812
   360
            return "PEP_ct_pEp_confirmed";
krista@1812
   361
        case PEP_ct_confirmed_encryption:
krista@1812
   362
            return "PEP_ct_confirmed_encryption";
krista@1812
   363
        case PEP_ct_OpenPGP_weak:
krista@1812
   364
            return "PEP_ct_OpenPGP_weak";
krista@1812
   365
        case PEP_ct_to_be_checked_confirmed:
krista@1812
   366
            return "PEP_ct_to_be_checked_confirmed";
krista@1812
   367
        case PEP_ct_SMIME:
krista@1812
   368
            return "PEP_ct_SMIME";
krista@1812
   369
        case PEP_ct_CMS:
krista@1812
   370
            return "PEP_ct_CMS";
krista@1812
   371
        case PEP_ct_strong_encryption:
krista@1812
   372
            return "PEP_ct_strong_encryption";
krista@1812
   373
        case PEP_ct_OpenPGP:
krista@1812
   374
            return "PEP_ct_OpenPGP";
krista@1812
   375
        case PEP_ct_OTR:
krista@1812
   376
            return "PEP_ct_OTR";
krista@1812
   377
        case PEP_ct_confirmed_enc_anon:
krista@1812
   378
            return "PEP_ct_confirmed_enc_anon";
krista@1812
   379
        case PEP_ct_pEp:
krista@1812
   380
            return "PEP_ct_pEp";
krista@1812
   381
        default:
krista@1812
   382
            return "PEP_ct_OMGWTFBBQ\n\nIn other words, comm type is invalid. Either something's corrupt or a new ct value has been added to the enum but not to the test function.";
krista@1812
   383
    }
krista@1812
   384
}
krista@2703
   385
krista@2703
   386
bool slurp_message_and_import_key(PEP_SESSION session, const char* message_fname, std::string& message, const char* key_filename) {
krista@2703
   387
    message = slurp(message_fname);
krista@2703
   388
    if (key_filename) {
krista@2703
   389
        std::string keyfile = slurp(key_filename);
krista@2703
   390
        if (import_key(session, keyfile.c_str(), keyfile.size(), NULL) != PEP_STATUS_OK)
krista@2703
   391
            return false;
krista@2703
   392
    }
krista@2703
   393
    return true;
krista@2703
   394
}
krista@2703
   395
krista@2703
   396
krista@2703
   397
krista@2703
   398
int util_delete_filepath(const char *filepath, 
krista@2703
   399
                         const struct stat *file_stat, 
krista@2703
   400
                         int ftw_info, 
krista@2703
   401
                         struct FTW * ftw_struct) {
krista@2703
   402
    int retval = 0;
krista@2703
   403
    switch (ftw_info) {
krista@2703
   404
        case FTW_DP:
krista@2703
   405
            retval = rmdir(filepath);
krista@2703
   406
            break;
krista@2703
   407
        case FTW_F:
krista@2703
   408
        case FTW_SLN:
krista@2703
   409
            retval = unlink(filepath);
krista@2703
   410
            break;    
krista@2703
   411
        default:
krista@2703
   412
            retval = -1;
krista@2703
   413
    }
krista@2703
   414
    
krista@2703
   415
    return retval;
krista@2703
   416
}
krista@2703
   417