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