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