test/src/util/test_util.cc
author Krista 'DarthMama' Bennett <krista@pep.foundation>
Wed, 06 Feb 2019 19:10:31 +0100
changeset 3279 011d5b2f231f
parent 3277 fe1e94372261
child 3281 676ee202da9b
permissions -rw-r--r--
ENGINE_511: removed weird extraneous line from test_util
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@3277
     5
#include "TestConstants.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@2798
    17
bool file_exists(std::string filename) {
krista@2798
    18
    struct stat buffer;
krista@2798
    19
    return (stat(filename.c_str(), &buffer) == 0);
krista@2798
    20
}
krista@2798
    21
krista@2533
    22
char* str_to_lower(const char* str) {
krista@2533
    23
    if (!str)
krista@2533
    24
        return NULL;
krista@2533
    25
    int str_len = strlen(str);
krista@2533
    26
    if (str_len == 0)
krista@2533
    27
        return strdup("");
krista@2533
    28
    int i;
krista@2533
    29
    
krista@2533
    30
    char* retval = (char*) calloc(1, str_len + 1);
krista@2533
    31
    for (i = 0; i < str_len; i++) {
krista@2533
    32
        retval[i] = tolower(str[i]);
krista@2533
    33
    }    
krista@2533
    34
    return retval;
krista@2533
    35
}
krista@2533
    36
krista@2533
    37
// Because annoyed
krista@2533
    38
bool _streq(const char* str1, const char* str2) {
krista@2533
    39
    if (!str1) {
krista@2533
    40
        if (str2)
krista@2533
    41
            return false;
krista@2533
    42
        return true;
krista@2533
    43
    }
krista@2533
    44
    if (!str2)
krista@2533
    45
        return false;
krista@2533
    46
        
krista@2533
    47
    return (strcmp(str1, str2) == 0);
krista@2533
    48
}
krista@2533
    49
krista@2533
    50
bool _strceq(const char* str1, const char* str2) {
krista@2533
    51
    char* str1_dup = str_to_lower(str1);
krista@2533
    52
    char* str2_dup = str_to_lower(str2);
krista@2533
    53
krista@2533
    54
    bool retval = _streq(str_to_lower(str1_dup), str_to_lower(str2_dup));
krista@2533
    55
    free(str1_dup);
krista@2533
    56
    free(str2_dup);
krista@2533
    57
    return retval;
krista@2533
    58
}
krista@2477
    59
krista@2477
    60
void test_init() {
krista@2477
    61
    unlink ("../test_home/.pEp_management.db");
krista@2477
    62
    unlink ("../test_home/.pEp_management.db-shm");
krista@2477
    63
    unlink ("../test_home/.pEp_management.db-wal");
krista@2477
    64
}
krista@2477
    65
roker@1656
    66
std::string slurp(const std::string& filename)
roker@1656
    67
{
roker@1656
    68
	std::ifstream input(filename.c_str());
roker@1656
    69
	if(!input)
roker@1656
    70
	{
roker@1656
    71
		throw std::runtime_error("Cannot read file \"" + filename + "\"! ");
roker@1656
    72
	}
roker@1656
    73
	
roker@1656
    74
	std::stringstream sstr;
roker@1656
    75
	sstr << input.rdbuf();
roker@1656
    76
	return sstr.str();
roker@1656
    77
}
krista@1812
    78
krista@2282
    79
void dump_out(const char* filename, const char* outdata)
krista@2282
    80
{
krista@2282
    81
	std::ofstream outfile(filename);
krista@2282
    82
	if(!outfile)
krista@2282
    83
	{
krista@2282
    84
		throw std::runtime_error("Cannot open output file!");
krista@2282
    85
	}
krista@2282
    86
	
krista@2282
    87
	outfile << outdata;
krista@2282
    88
    outfile.close();
krista@2282
    89
}
krista@2282
    90
krista@2444
    91
char* get_new_uuid() {
krista@2444
    92
    char* new_uuid = (char*)calloc(37, 1);
krista@2444
    93
    pEpUUID uuid;
krista@2444
    94
    uuid_generate_random(uuid);
krista@2444
    95
    uuid_unparse_upper(uuid, new_uuid);
krista@2444
    96
    return new_uuid;
krista@2444
    97
}
krista@2282
    98
krista@1812
    99
const char* tl_status_string(PEP_STATUS status) {
krista@1812
   100
    switch (status) {
krista@1812
   101
        case PEP_STATUS_OK:
krista@1812
   102
            return "PEP_STATUS_OK";
krista@1812
   103
        case PEP_INIT_CANNOT_LOAD_GPGME:
krista@1812
   104
            return "PEP_INIT_CANNOT_LOAD_GPGME";
krista@1812
   105
        case PEP_INIT_GPGME_INIT_FAILED:
krista@1812
   106
            return "PEP_INIT_GPGME_INIT_FAILED";
krista@1812
   107
        case PEP_INIT_NO_GPG_HOME:
krista@1812
   108
            return "PEP_INIT_NO_GPG_HOME";
krista@1812
   109
        case PEP_INIT_NETPGP_INIT_FAILED:
krista@1812
   110
            return "PEP_INIT_NETPGP_INIT_FAILED";
krista@1812
   111
        case PEP_INIT_SQLITE3_WITHOUT_MUTEX:
krista@1812
   112
            return "PEP_INIT_SQLITE3_WITHOUT_MUTEX";
krista@1812
   113
        case PEP_INIT_CANNOT_OPEN_DB:
krista@1812
   114
            return "PEP_INIT_CANNOT_OPEN_DB";
krista@1812
   115
        case PEP_INIT_CANNOT_OPEN_SYSTEM_DB:
krista@1812
   116
            return "PEP_INIT_CANNOT_OPEN_SYSTEM_DB";
krista@1812
   117
        case PEP_KEY_NOT_FOUND:
krista@1812
   118
            return "PEP_KEY_NOT_FOUND";
krista@1812
   119
        case PEP_KEY_HAS_AMBIG_NAME:
krista@1812
   120
            return "PEP_KEY_HAS_AMBIG_NAME";
krista@1812
   121
        case PEP_GET_KEY_FAILED:
krista@1812
   122
            return "PEP_GET_KEY_FAILED";
krista@1812
   123
        case PEP_CANNOT_EXPORT_KEY:
krista@1812
   124
            return "PEP_CANNOT_EXPORT_KEY";
krista@1812
   125
        case PEP_CANNOT_EDIT_KEY:
krista@1812
   126
            return "PEP_CANNOT_EDIT_KEY";
krista@1812
   127
        case PEP_CANNOT_FIND_IDENTITY:
krista@1812
   128
            return "PEP_CANNOT_FIND_IDENTITY";
krista@1812
   129
        case PEP_CANNOT_SET_PERSON:
krista@1812
   130
            return "PEP_CANNOT_SET_PERSON";
krista@1812
   131
        case PEP_CANNOT_SET_PGP_KEYPAIR:
krista@1812
   132
            return "PEP_CANNOT_SET_PGP_KEYPAIR";
krista@1812
   133
        case PEP_CANNOT_SET_IDENTITY:
krista@1812
   134
            return "PEP_CANNOT_SET_IDENTITY";
krista@1812
   135
        case PEP_CANNOT_SET_TRUST:
krista@1812
   136
            return "PEP_CANNOT_SET_TRUST";
krista@1812
   137
        case PEP_KEY_BLACKLISTED:
krista@1812
   138
            return "PEP_KEY_BLACKLISTED";
krista@1812
   139
        case PEP_UNENCRYPTED:
krista@1812
   140
            return "PEP_UNENCRYPTED";
krista@1812
   141
        case PEP_VERIFIED:
krista@1812
   142
            return "PEP_VERIFIED";
krista@1812
   143
        case PEP_DECRYPTED:
krista@1812
   144
            return "PEP_DECRYPTED";
krista@1812
   145
        case PEP_DECRYPTED_AND_VERIFIED:
krista@1812
   146
            return "PEP_DECRYPTED_AND_VERIFIED";
krista@1812
   147
        case PEP_DECRYPT_WRONG_FORMAT:
krista@1812
   148
            return "PEP_DECRYPT_WRONG_FORMAT";
krista@1812
   149
        case PEP_DECRYPT_NO_KEY:
krista@1812
   150
            return "PEP_DECRYPT_NO_KEY";
krista@1812
   151
        case PEP_DECRYPT_SIGNATURE_DOES_NOT_MATCH:
krista@1812
   152
            return "PEP_DECRYPT_SIGNATURE_DOES_NOT_MATCH";
krista@2703
   153
//        case PEP_DECRYPTED_BUT_UNSIGNED:
krista@2703
   154
//            return "PEP_DECRYPTED_BUT_UNSIGNED";
krista@2703
   155
//        case PEP_DECRYPT_MODIFICATION_DETECTED:
krista@2703
   156
//            return "PEP_DECRYPT_MODIFICATION_DETECTED";
krista@2703
   157
//        case PEP_DECRYPT_NO_KEY_FOR_SIGNER:
krista@2703
   158
//            return "PEP_DECRYPT_NO_KEY_FOR_SIGNER";
krista@1812
   159
        case PEP_VERIFY_NO_KEY:
krista@1812
   160
            return "PEP_VERIFY_NO_KEY";
krista@1812
   161
        case PEP_VERIFIED_AND_TRUSTED:
krista@1812
   162
            return "PEP_VERIFIED_AND_TRUSTED";
krista@1812
   163
        case PEP_CANNOT_DECRYPT_UNKNOWN:
krista@1812
   164
            return "PEP_CANNOT_DECRYPT_UNKNOWN";
krista@1812
   165
        case PEP_TRUSTWORD_NOT_FOUND:
krista@1812
   166
            return "PEP_TRUSTWORD_NOT_FOUND";
krista@1812
   167
        case PEP_TRUSTWORDS_FPR_WRONG_LENGTH:
krista@1812
   168
            return "PEP_TRUSTWORDS_FPR_WRONG_LENGTH";
krista@1812
   169
        case PEP_CANNOT_CREATE_KEY:
krista@1812
   170
            return "PEP_CANNOT_CREATE_KEY";
krista@1812
   171
        case PEP_CANNOT_SEND_KEY:
krista@1812
   172
            return "PEP_CANNOT_SEND_KEY";
krista@1812
   173
        case PEP_PHRASE_NOT_FOUND:
krista@1812
   174
            return "PEP_PHRASE_NOT_FOUND";
krista@1812
   175
        case PEP_SEND_FUNCTION_NOT_REGISTERED:
krista@1812
   176
            return "PEP_SEND_FUNCTION_NOT_REGISTERED";
krista@1812
   177
        case PEP_CONTRAINTS_VIOLATED:
krista@1812
   178
            return "PEP_CONTRAINTS_VIOLATED";
krista@1812
   179
        case PEP_CANNOT_ENCODE:
krista@1812
   180
            return "PEP_CANNOT_ENCODE";
krista@1812
   181
        case PEP_SYNC_NO_NOTIFY_CALLBACK:
krista@1812
   182
            return "PEP_SYNC_NO_NOTIFY_CALLBACK";
krista@1812
   183
        case PEP_SYNC_ILLEGAL_MESSAGE:
krista@1812
   184
            return "PEP_SYNC_ILLEGAL_MESSAGE";
krista@1812
   185
        case PEP_SYNC_NO_INJECT_CALLBACK:
krista@1812
   186
            return "PEP_SYNC_NO_INJECT_CALLBACK";
krista@1812
   187
        case PEP_SEQUENCE_VIOLATED:
krista@1812
   188
            return "PEP_SEQUENCE_VIOLATED";
krista@1812
   189
        case PEP_CANNOT_INCREASE_SEQUENCE:
krista@1812
   190
            return "PEP_CANNOT_INCREASE_SEQUENCE";
krista@1812
   191
        case PEP_CANNOT_SET_SEQUENCE_VALUE:
krista@1812
   192
            return "PEP_CANNOT_SET_SEQUENCE_VALUE";
krista@1812
   193
        case PEP_OWN_SEQUENCE:
krista@1812
   194
            return "PEP_OWN_SEQUENCE";
krista@1812
   195
        case PEP_SYNC_STATEMACHINE_ERROR:
krista@1812
   196
            return "PEP_SYNC_STATEMACHINE_ERROR";
krista@1812
   197
        case PEP_SYNC_NO_TRUST:
krista@1812
   198
            return "PEP_SYNC_NO_TRUST";
krista@1812
   199
        case PEP_STATEMACHINE_INVALID_STATE:
krista@1812
   200
            return "PEP_STATEMACHINE_INVALID_STATE";
krista@1812
   201
        case PEP_STATEMACHINE_INVALID_EVENT:
krista@1812
   202
            return "PEP_STATEMACHINE_INVALID_EVENT";
krista@1812
   203
        case PEP_STATEMACHINE_INVALID_CONDITION:
krista@1812
   204
            return "PEP_STATEMACHINE_INVALID_CONDITION";
krista@1812
   205
        case PEP_STATEMACHINE_INVALID_ACTION:
krista@1812
   206
            return "PEP_STATEMACHINE_INVALID_ACTION";
krista@1812
   207
        case PEP_STATEMACHINE_INHIBITED_EVENT:
krista@1812
   208
            return "PEP_STATEMACHINE_INHIBITED_EVENT";
krista@1812
   209
        case PEP_COMMIT_FAILED:
krista@1812
   210
            return "PEP_COMMIT_FAILED";
krista@1812
   211
        case PEP_MESSAGE_CONSUME:
krista@1812
   212
            return "PEP_MESSAGE_CONSUME";
krista@1812
   213
        case PEP_MESSAGE_IGNORE:
krista@1812
   214
            return "PEP_MESSAGE_IGNORE";
krista@1812
   215
        case PEP_RECORD_NOT_FOUND:
krista@1812
   216
            return "PEP_RECORD_NOT_FOUND";
krista@1812
   217
        case PEP_CANNOT_CREATE_TEMP_FILE:
krista@1812
   218
            return "PEP_CANNOT_CREATE_TEMP_FILE";
krista@1812
   219
        case PEP_ILLEGAL_VALUE:
krista@1812
   220
            return "PEP_ILLEGAL_VALUE";
krista@1812
   221
        case PEP_BUFFER_TOO_SMALL:
krista@1812
   222
            return "PEP_BUFFER_TOO_SMALL";
krista@1812
   223
        case PEP_OUT_OF_MEMORY:
krista@1812
   224
            return "PEP_OUT_OF_MEMORY";
krista@1812
   225
        case PEP_UNKNOWN_ERROR:
krista@2703
   226
            return "PEP_UNKNOWN_ERROR";    
krista@1812
   227
        default:
krista@2630
   228
 
krista@1812
   229
            return "PEP_STATUS_OMGWTFBBQ - This means you're using a status the test lib doesn't know about!";
krista@1812
   230
    }
krista@1812
   231
}
krista@1812
   232
const char* tl_rating_string(PEP_rating rating) {
krista@1812
   233
    switch (rating) {
krista@1812
   234
        case PEP_rating_undefined:
krista@1812
   235
            return "PEP_rating_undefined";
krista@1812
   236
        case PEP_rating_cannot_decrypt:
krista@1812
   237
            return "PEP_rating_cannot_decrypt";
krista@1812
   238
        case PEP_rating_have_no_key:
krista@1812
   239
            return "PEP_rating_have_no_key";
krista@1812
   240
        case PEP_rating_unencrypted:
krista@1812
   241
            return "PEP_rating_unencrypted";
krista@1812
   242
        case PEP_rating_unencrypted_for_some:
krista@1812
   243
            return "PEP_rating_unencrypted_for_some";
krista@1812
   244
        case PEP_rating_unreliable:
krista@1812
   245
            return "PEP_rating_unreliable";
krista@1812
   246
        case PEP_rating_reliable:
krista@1812
   247
            return "PEP_rating_reliable";
krista@1812
   248
        case PEP_rating_trusted:
krista@1812
   249
            return "PEP_rating_trusted";
krista@1812
   250
        case PEP_rating_trusted_and_anonymized:
krista@1812
   251
            return "PEP_rating_trusted_and_anonymized";
krista@1812
   252
        case PEP_rating_fully_anonymous:
krista@1812
   253
            return "PEP_rating_fully_anonymous";
krista@1812
   254
        case PEP_rating_mistrust:
krista@1812
   255
            return "PEP_rating_mistrust";
krista@1812
   256
        case PEP_rating_b0rken:
krista@1812
   257
            return "PEP_rating_b0rken";
krista@1812
   258
        case PEP_rating_under_attack:
krista@1812
   259
            return "PEP_rating_under_attack";
krista@1812
   260
        default:
krista@1812
   261
            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
   262
    }
krista@1812
   263
}
krista@1812
   264
krista@1812
   265
const char* tl_ct_string(PEP_comm_type ct) {
krista@1812
   266
    switch (ct) {
krista@1812
   267
        case PEP_ct_unknown:
krista@1812
   268
            return "PEP_ct_unknown";
krista@1812
   269
        case PEP_ct_no_encryption:
krista@1812
   270
            return "PEP_ct_no_encryption";
krista@1812
   271
        case PEP_ct_no_encrypted_channel:
krista@1812
   272
            return "PEP_ct_no_encrypted_channel";
krista@1812
   273
        case PEP_ct_key_not_found:
krista@1812
   274
            return "PEP_ct_key_not_found";
krista@1812
   275
        case PEP_ct_key_expired:
krista@1812
   276
            return "PEP_ct_key_expired";
krista@1812
   277
        case PEP_ct_key_revoked:
krista@1812
   278
            return "PEP_ct_key_revoked";
krista@1812
   279
        case PEP_ct_key_b0rken:
krista@1812
   280
            return "PEP_ct_key_b0rken";
krista@1812
   281
        case PEP_ct_my_key_not_included:
krista@1812
   282
            return "PEP_ct_my_key_not_included";
krista@1812
   283
        case PEP_ct_security_by_obscurity:
krista@1812
   284
            return "PEP_ct_security_by_obscurity";
krista@1812
   285
        case PEP_ct_b0rken_crypto:
krista@1812
   286
            return "PEP_ct_b0rken_crypto";
krista@1812
   287
        case PEP_ct_key_too_short:
krista@1812
   288
            return "PEP_ct_key_too_short";
krista@2593
   289
        case PEP_ct_compromised:
krista@2593
   290
            return "PEP_ct_compromised";
krista@1812
   291
        case PEP_ct_mistrusted:
krista@1812
   292
            return "PEP_ct_mistrusted";
krista@1812
   293
        case PEP_ct_unconfirmed_encryption:
krista@1812
   294
            return "PEP_ct_unconfirmed_encryption";
krista@1812
   295
        case PEP_ct_OpenPGP_weak_unconfirmed:
krista@1812
   296
            return "PEP_ct_OpenPGP_weak_unconfirmed";
krista@1812
   297
        case PEP_ct_to_be_checked:
krista@1812
   298
            return "PEP_ct_to_be_checked";
krista@1812
   299
        case PEP_ct_SMIME_unconfirmed:
krista@1812
   300
            return "PEP_ct_SMIME_unconfirmed";
krista@1812
   301
        case PEP_ct_CMS_unconfirmed:
krista@1812
   302
            return "PEP_ct_CMS_unconfirmed";
krista@1812
   303
        case PEP_ct_strong_but_unconfirmed:
krista@1812
   304
            return "PEP_ct_strong_but_unconfirmed";
krista@1812
   305
        case PEP_ct_OpenPGP_unconfirmed:
krista@1812
   306
            return "PEP_ct_OpenPGP_unconfirmed";
krista@1812
   307
        case PEP_ct_OTR_unconfirmed:
krista@1812
   308
            return "PEP_ct_OTR_unconfirmed";
krista@1812
   309
        case PEP_ct_unconfirmed_enc_anon:
krista@1812
   310
            return "PEP_ct_unconfirmed_enc_anon";
krista@1812
   311
        case PEP_ct_pEp_unconfirmed:
krista@1812
   312
            return "PEP_ct_pEp_unconfirmed";
krista@1812
   313
        case PEP_ct_confirmed:
krista@1812
   314
            return "PEP_ct_pEp_confirmed";
krista@1812
   315
        case PEP_ct_confirmed_encryption:
krista@1812
   316
            return "PEP_ct_confirmed_encryption";
krista@1812
   317
        case PEP_ct_OpenPGP_weak:
krista@1812
   318
            return "PEP_ct_OpenPGP_weak";
krista@1812
   319
        case PEP_ct_to_be_checked_confirmed:
krista@1812
   320
            return "PEP_ct_to_be_checked_confirmed";
krista@1812
   321
        case PEP_ct_SMIME:
krista@1812
   322
            return "PEP_ct_SMIME";
krista@1812
   323
        case PEP_ct_CMS:
krista@1812
   324
            return "PEP_ct_CMS";
krista@1812
   325
        case PEP_ct_strong_encryption:
krista@1812
   326
            return "PEP_ct_strong_encryption";
krista@1812
   327
        case PEP_ct_OpenPGP:
krista@1812
   328
            return "PEP_ct_OpenPGP";
krista@1812
   329
        case PEP_ct_OTR:
krista@1812
   330
            return "PEP_ct_OTR";
krista@1812
   331
        case PEP_ct_confirmed_enc_anon:
krista@1812
   332
            return "PEP_ct_confirmed_enc_anon";
krista@1812
   333
        case PEP_ct_pEp:
krista@1812
   334
            return "PEP_ct_pEp";
krista@1812
   335
        default:
krista@1812
   336
            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
   337
    }
krista@1812
   338
}
krista@2703
   339
krista@2995
   340
bool slurp_and_import_key(PEP_SESSION session, const char* key_filename) {
krista@2995
   341
    std::string keyfile = slurp(key_filename);
krista@3277
   342
    if (import_key(session, keyfile.c_str(), keyfile.size(), NULL) != PEP_TEST_KEY_IMPORT_SUCCESS)
krista@2995
   343
        return false;
krista@2995
   344
    return true;
krista@2995
   345
}
krista@2995
   346
krista@2703
   347
bool slurp_message_and_import_key(PEP_SESSION session, const char* message_fname, std::string& message, const char* key_filename) {
krista@2995
   348
    bool ok = true;
krista@2703
   349
    message = slurp(message_fname);
krista@2995
   350
    if (key_filename)
krista@2995
   351
        ok = slurp_and_import_key(session, key_filename);
krista@2995
   352
    return ok;
krista@2703
   353
}
krista@2703
   354
krista@2703
   355
krista@2703
   356
krista@2703
   357
int util_delete_filepath(const char *filepath, 
krista@2703
   358
                         const struct stat *file_stat, 
krista@2703
   359
                         int ftw_info, 
krista@2703
   360
                         struct FTW * ftw_struct) {
krista@2703
   361
    int retval = 0;
krista@2703
   362
    switch (ftw_info) {
krista@2703
   363
        case FTW_DP:
krista@2703
   364
            retval = rmdir(filepath);
krista@2703
   365
            break;
krista@2703
   366
        case FTW_F:
krista@2703
   367
        case FTW_SLN:
krista@2703
   368
            retval = unlink(filepath);
krista@2703
   369
            break;    
krista@2703
   370
        default:
krista@2703
   371
            retval = -1;
krista@2703
   372
    }
krista@2703
   373
    
krista@2703
   374
    return retval;
krista@2703
   375
}