test/test_util.cc
author Krista Bennett <krista@pep-project.org>
Mon, 05 Feb 2018 10:58:03 +0100
branchENGINE-250
changeset 2477 45e8ed722fec
parent 2444 a97dd32908e5
child 2533 722b45fd4e67
permissions -rw-r--r--
Intermittent commit
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@2477
     9
krista@2477
    10
void test_init() {
krista@2477
    11
    unlink ("../test_home/.pEp_management.db");
krista@2477
    12
    unlink ("../test_home/.pEp_management.db-shm");
krista@2477
    13
    unlink ("../test_home/.pEp_management.db-wal");
krista@2477
    14
}
krista@2477
    15
roker@1656
    16
std::string slurp(const std::string& filename)
roker@1656
    17
{
roker@1656
    18
	std::ifstream input(filename.c_str());
roker@1656
    19
	if(!input)
roker@1656
    20
	{
roker@1656
    21
		throw std::runtime_error("Cannot read file \"" + filename + "\"! ");
roker@1656
    22
	}
roker@1656
    23
	
roker@1656
    24
	std::stringstream sstr;
roker@1656
    25
	sstr << input.rdbuf();
roker@1656
    26
	return sstr.str();
roker@1656
    27
}
krista@1812
    28
krista@2282
    29
void dump_out(const char* filename, const char* outdata)
krista@2282
    30
{
krista@2282
    31
	std::ofstream outfile(filename);
krista@2282
    32
	if(!outfile)
krista@2282
    33
	{
krista@2282
    34
		throw std::runtime_error("Cannot open output file!");
krista@2282
    35
	}
krista@2282
    36
	
krista@2282
    37
	outfile << outdata;
krista@2282
    38
    outfile.close();
krista@2282
    39
}
krista@2282
    40
krista@2444
    41
char* get_new_uuid() {
krista@2444
    42
    char* new_uuid = (char*)calloc(37, 1);
krista@2444
    43
    pEpUUID uuid;
krista@2444
    44
    uuid_generate_random(uuid);
krista@2444
    45
    uuid_unparse_upper(uuid, new_uuid);
krista@2444
    46
    return new_uuid;
krista@2444
    47
}
krista@2282
    48
krista@1812
    49
const char* tl_status_string(PEP_STATUS status) {
krista@1812
    50
    switch (status) {
krista@1812
    51
        case PEP_STATUS_OK:
krista@1812
    52
            return "PEP_STATUS_OK";
krista@1812
    53
        case PEP_INIT_CANNOT_LOAD_GPGME:
krista@1812
    54
            return "PEP_INIT_CANNOT_LOAD_GPGME";
krista@1812
    55
        case PEP_INIT_GPGME_INIT_FAILED:
krista@1812
    56
            return "PEP_INIT_GPGME_INIT_FAILED";
krista@1812
    57
        case PEP_INIT_NO_GPG_HOME:
krista@1812
    58
            return "PEP_INIT_NO_GPG_HOME";
krista@1812
    59
        case PEP_INIT_NETPGP_INIT_FAILED:
krista@1812
    60
            return "PEP_INIT_NETPGP_INIT_FAILED";
krista@1812
    61
        case PEP_INIT_SQLITE3_WITHOUT_MUTEX:
krista@1812
    62
            return "PEP_INIT_SQLITE3_WITHOUT_MUTEX";
krista@1812
    63
        case PEP_INIT_CANNOT_OPEN_DB:
krista@1812
    64
            return "PEP_INIT_CANNOT_OPEN_DB";
krista@1812
    65
        case PEP_INIT_CANNOT_OPEN_SYSTEM_DB:
krista@1812
    66
            return "PEP_INIT_CANNOT_OPEN_SYSTEM_DB";
krista@1812
    67
        case PEP_KEY_NOT_FOUND:
krista@1812
    68
            return "PEP_KEY_NOT_FOUND";
krista@1812
    69
        case PEP_KEY_HAS_AMBIG_NAME:
krista@1812
    70
            return "PEP_KEY_HAS_AMBIG_NAME";
krista@1812
    71
        case PEP_GET_KEY_FAILED:
krista@1812
    72
            return "PEP_GET_KEY_FAILED";
krista@1812
    73
        case PEP_CANNOT_EXPORT_KEY:
krista@1812
    74
            return "PEP_CANNOT_EXPORT_KEY";
krista@1812
    75
        case PEP_CANNOT_EDIT_KEY:
krista@1812
    76
            return "PEP_CANNOT_EDIT_KEY";
krista@1812
    77
        case PEP_CANNOT_FIND_IDENTITY:
krista@1812
    78
            return "PEP_CANNOT_FIND_IDENTITY";
krista@1812
    79
        case PEP_CANNOT_SET_PERSON:
krista@1812
    80
            return "PEP_CANNOT_SET_PERSON";
krista@1812
    81
        case PEP_CANNOT_SET_PGP_KEYPAIR:
krista@1812
    82
            return "PEP_CANNOT_SET_PGP_KEYPAIR";
krista@1812
    83
        case PEP_CANNOT_SET_IDENTITY:
krista@1812
    84
            return "PEP_CANNOT_SET_IDENTITY";
krista@1812
    85
        case PEP_CANNOT_SET_TRUST:
krista@1812
    86
            return "PEP_CANNOT_SET_TRUST";
krista@1812
    87
        case PEP_KEY_BLACKLISTED:
krista@1812
    88
            return "PEP_KEY_BLACKLISTED";
krista@1812
    89
        case PEP_UNENCRYPTED:
krista@1812
    90
            return "PEP_UNENCRYPTED";
krista@1812
    91
        case PEP_VERIFIED:
krista@1812
    92
            return "PEP_VERIFIED";
krista@1812
    93
        case PEP_DECRYPTED:
krista@1812
    94
            return "PEP_DECRYPTED";
krista@1812
    95
        case PEP_DECRYPTED_AND_VERIFIED:
krista@1812
    96
            return "PEP_DECRYPTED_AND_VERIFIED";
krista@1812
    97
        case PEP_DECRYPT_WRONG_FORMAT:
krista@1812
    98
            return "PEP_DECRYPT_WRONG_FORMAT";
krista@1812
    99
        case PEP_DECRYPT_NO_KEY:
krista@1812
   100
            return "PEP_DECRYPT_NO_KEY";
krista@1812
   101
        case PEP_DECRYPT_SIGNATURE_DOES_NOT_MATCH:
krista@1812
   102
            return "PEP_DECRYPT_SIGNATURE_DOES_NOT_MATCH";
krista@1812
   103
        case PEP_VERIFY_NO_KEY:
krista@1812
   104
            return "PEP_VERIFY_NO_KEY";
krista@1812
   105
        case PEP_VERIFIED_AND_TRUSTED:
krista@1812
   106
            return "PEP_VERIFIED_AND_TRUSTED";
krista@1812
   107
        case PEP_CANNOT_DECRYPT_UNKNOWN:
krista@1812
   108
            return "PEP_CANNOT_DECRYPT_UNKNOWN";
krista@1812
   109
        case PEP_TRUSTWORD_NOT_FOUND:
krista@1812
   110
            return "PEP_TRUSTWORD_NOT_FOUND";
krista@1812
   111
        case PEP_TRUSTWORDS_FPR_WRONG_LENGTH:
krista@1812
   112
            return "PEP_TRUSTWORDS_FPR_WRONG_LENGTH";
krista@1812
   113
        case PEP_CANNOT_CREATE_KEY:
krista@1812
   114
            return "PEP_CANNOT_CREATE_KEY";
krista@1812
   115
        case PEP_CANNOT_SEND_KEY:
krista@1812
   116
            return "PEP_CANNOT_SEND_KEY";
krista@1812
   117
        case PEP_PHRASE_NOT_FOUND:
krista@1812
   118
            return "PEP_PHRASE_NOT_FOUND";
krista@1812
   119
        case PEP_SEND_FUNCTION_NOT_REGISTERED:
krista@1812
   120
            return "PEP_SEND_FUNCTION_NOT_REGISTERED";
krista@1812
   121
        case PEP_CONTRAINTS_VIOLATED:
krista@1812
   122
            return "PEP_CONTRAINTS_VIOLATED";
krista@1812
   123
        case PEP_CANNOT_ENCODE:
krista@1812
   124
            return "PEP_CANNOT_ENCODE";
krista@1812
   125
        case PEP_SYNC_NO_NOTIFY_CALLBACK:
krista@1812
   126
            return "PEP_SYNC_NO_NOTIFY_CALLBACK";
krista@1812
   127
        case PEP_SYNC_ILLEGAL_MESSAGE:
krista@1812
   128
            return "PEP_SYNC_ILLEGAL_MESSAGE";
krista@1812
   129
        case PEP_SYNC_NO_INJECT_CALLBACK:
krista@1812
   130
            return "PEP_SYNC_NO_INJECT_CALLBACK";
krista@1812
   131
        case PEP_SEQUENCE_VIOLATED:
krista@1812
   132
            return "PEP_SEQUENCE_VIOLATED";
krista@1812
   133
        case PEP_CANNOT_INCREASE_SEQUENCE:
krista@1812
   134
            return "PEP_CANNOT_INCREASE_SEQUENCE";
krista@1812
   135
        case PEP_CANNOT_SET_SEQUENCE_VALUE:
krista@1812
   136
            return "PEP_CANNOT_SET_SEQUENCE_VALUE";
krista@1812
   137
        case PEP_OWN_SEQUENCE:
krista@1812
   138
            return "PEP_OWN_SEQUENCE";
krista@1812
   139
        case PEP_SYNC_STATEMACHINE_ERROR:
krista@1812
   140
            return "PEP_SYNC_STATEMACHINE_ERROR";
krista@1812
   141
        case PEP_SYNC_NO_TRUST:
krista@1812
   142
            return "PEP_SYNC_NO_TRUST";
krista@1812
   143
        case PEP_STATEMACHINE_INVALID_STATE:
krista@1812
   144
            return "PEP_STATEMACHINE_INVALID_STATE";
krista@1812
   145
        case PEP_STATEMACHINE_INVALID_EVENT:
krista@1812
   146
            return "PEP_STATEMACHINE_INVALID_EVENT";
krista@1812
   147
        case PEP_STATEMACHINE_INVALID_CONDITION:
krista@1812
   148
            return "PEP_STATEMACHINE_INVALID_CONDITION";
krista@1812
   149
        case PEP_STATEMACHINE_INVALID_ACTION:
krista@1812
   150
            return "PEP_STATEMACHINE_INVALID_ACTION";
krista@1812
   151
        case PEP_STATEMACHINE_INHIBITED_EVENT:
krista@1812
   152
            return "PEP_STATEMACHINE_INHIBITED_EVENT";
krista@1812
   153
        case PEP_COMMIT_FAILED:
krista@1812
   154
            return "PEP_COMMIT_FAILED";
krista@1812
   155
        case PEP_MESSAGE_CONSUME:
krista@1812
   156
            return "PEP_MESSAGE_CONSUME";
krista@1812
   157
        case PEP_MESSAGE_IGNORE:
krista@1812
   158
            return "PEP_MESSAGE_IGNORE";
krista@1812
   159
        case PEP_RECORD_NOT_FOUND:
krista@1812
   160
            return "PEP_RECORD_NOT_FOUND";
krista@1812
   161
        case PEP_CANNOT_CREATE_TEMP_FILE:
krista@1812
   162
            return "PEP_CANNOT_CREATE_TEMP_FILE";
krista@1812
   163
        case PEP_ILLEGAL_VALUE:
krista@1812
   164
            return "PEP_ILLEGAL_VALUE";
krista@1812
   165
        case PEP_BUFFER_TOO_SMALL:
krista@1812
   166
            return "PEP_BUFFER_TOO_SMALL";
krista@1812
   167
        case PEP_OUT_OF_MEMORY:
krista@1812
   168
            return "PEP_OUT_OF_MEMORY";
krista@1812
   169
        case PEP_UNKNOWN_ERROR:
krista@1812
   170
            return "PEP_UNKNOWN_ERROR";
krista@1812
   171
        default:
krista@1812
   172
            return "PEP_STATUS_OMGWTFBBQ - This means you're using a status the test lib doesn't know about!";
krista@1812
   173
    }
krista@1812
   174
}
krista@1812
   175
const char* tl_rating_string(PEP_rating rating) {
krista@1812
   176
    switch (rating) {
krista@1812
   177
        case PEP_rating_undefined:
krista@1812
   178
            return "PEP_rating_undefined";
krista@1812
   179
        case PEP_rating_cannot_decrypt:
krista@1812
   180
            return "PEP_rating_cannot_decrypt";
krista@1812
   181
        case PEP_rating_have_no_key:
krista@1812
   182
            return "PEP_rating_have_no_key";
krista@1812
   183
        case PEP_rating_unencrypted:
krista@1812
   184
            return "PEP_rating_unencrypted";
krista@1812
   185
        case PEP_rating_unencrypted_for_some:
krista@1812
   186
            return "PEP_rating_unencrypted_for_some";
krista@1812
   187
        case PEP_rating_unreliable:
krista@1812
   188
            return "PEP_rating_unreliable";
krista@1812
   189
        case PEP_rating_reliable:
krista@1812
   190
            return "PEP_rating_reliable";
krista@1812
   191
        case PEP_rating_trusted:
krista@1812
   192
            return "PEP_rating_trusted";
krista@1812
   193
        case PEP_rating_trusted_and_anonymized:
krista@1812
   194
            return "PEP_rating_trusted_and_anonymized";
krista@1812
   195
        case PEP_rating_fully_anonymous:
krista@1812
   196
            return "PEP_rating_fully_anonymous";
krista@1812
   197
        case PEP_rating_mistrust:
krista@1812
   198
            return "PEP_rating_mistrust";
krista@1812
   199
        case PEP_rating_b0rken:
krista@1812
   200
            return "PEP_rating_b0rken";
krista@1812
   201
        case PEP_rating_under_attack:
krista@1812
   202
            return "PEP_rating_under_attack";
krista@1812
   203
        default:
krista@1812
   204
            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
   205
    }
krista@1812
   206
}
krista@1812
   207
krista@1812
   208
const char* tl_ct_string(PEP_comm_type ct) {
krista@1812
   209
    switch (ct) {
krista@1812
   210
        case PEP_ct_unknown:
krista@1812
   211
            return "PEP_ct_unknown";
krista@1812
   212
        case PEP_ct_no_encryption:
krista@1812
   213
            return "PEP_ct_no_encryption";
krista@1812
   214
        case PEP_ct_no_encrypted_channel:
krista@1812
   215
            return "PEP_ct_no_encrypted_channel";
krista@1812
   216
        case PEP_ct_key_not_found:
krista@1812
   217
            return "PEP_ct_key_not_found";
krista@1812
   218
        case PEP_ct_key_expired:
krista@1812
   219
            return "PEP_ct_key_expired";
krista@1812
   220
        case PEP_ct_key_revoked:
krista@1812
   221
            return "PEP_ct_key_revoked";
krista@1812
   222
        case PEP_ct_key_b0rken:
krista@1812
   223
            return "PEP_ct_key_b0rken";
krista@1812
   224
        case PEP_ct_my_key_not_included:
krista@1812
   225
            return "PEP_ct_my_key_not_included";
krista@1812
   226
        case PEP_ct_security_by_obscurity:
krista@1812
   227
            return "PEP_ct_security_by_obscurity";
krista@1812
   228
        case PEP_ct_b0rken_crypto:
krista@1812
   229
            return "PEP_ct_b0rken_crypto";
krista@1812
   230
        case PEP_ct_key_too_short:
krista@1812
   231
            return "PEP_ct_key_too_short";
krista@1812
   232
        case PEP_ct_compromized:
krista@1812
   233
            return "PEP_ct_compromized";
krista@1812
   234
        case PEP_ct_mistrusted:
krista@1812
   235
            return "PEP_ct_mistrusted";
krista@1812
   236
        case PEP_ct_unconfirmed_encryption:
krista@1812
   237
            return "PEP_ct_unconfirmed_encryption";
krista@1812
   238
        case PEP_ct_OpenPGP_weak_unconfirmed:
krista@1812
   239
            return "PEP_ct_OpenPGP_weak_unconfirmed";
krista@1812
   240
        case PEP_ct_to_be_checked:
krista@1812
   241
            return "PEP_ct_to_be_checked";
krista@1812
   242
        case PEP_ct_SMIME_unconfirmed:
krista@1812
   243
            return "PEP_ct_SMIME_unconfirmed";
krista@1812
   244
        case PEP_ct_CMS_unconfirmed:
krista@1812
   245
            return "PEP_ct_CMS_unconfirmed";
krista@1812
   246
        case PEP_ct_strong_but_unconfirmed:
krista@1812
   247
            return "PEP_ct_strong_but_unconfirmed";
krista@1812
   248
        case PEP_ct_OpenPGP_unconfirmed:
krista@1812
   249
            return "PEP_ct_OpenPGP_unconfirmed";
krista@1812
   250
        case PEP_ct_OTR_unconfirmed:
krista@1812
   251
            return "PEP_ct_OTR_unconfirmed";
krista@1812
   252
        case PEP_ct_unconfirmed_enc_anon:
krista@1812
   253
            return "PEP_ct_unconfirmed_enc_anon";
krista@1812
   254
        case PEP_ct_pEp_unconfirmed:
krista@1812
   255
            return "PEP_ct_pEp_unconfirmed";
krista@1812
   256
        case PEP_ct_confirmed:
krista@1812
   257
            return "PEP_ct_pEp_confirmed";
krista@1812
   258
        case PEP_ct_confirmed_encryption:
krista@1812
   259
            return "PEP_ct_confirmed_encryption";
krista@1812
   260
        case PEP_ct_OpenPGP_weak:
krista@1812
   261
            return "PEP_ct_OpenPGP_weak";
krista@1812
   262
        case PEP_ct_to_be_checked_confirmed:
krista@1812
   263
            return "PEP_ct_to_be_checked_confirmed";
krista@1812
   264
        case PEP_ct_SMIME:
krista@1812
   265
            return "PEP_ct_SMIME";
krista@1812
   266
        case PEP_ct_CMS:
krista@1812
   267
            return "PEP_ct_CMS";
krista@1812
   268
        case PEP_ct_strong_encryption:
krista@1812
   269
            return "PEP_ct_strong_encryption";
krista@1812
   270
        case PEP_ct_OpenPGP:
krista@1812
   271
            return "PEP_ct_OpenPGP";
krista@1812
   272
        case PEP_ct_OTR:
krista@1812
   273
            return "PEP_ct_OTR";
krista@1812
   274
        case PEP_ct_confirmed_enc_anon:
krista@1812
   275
            return "PEP_ct_confirmed_enc_anon";
krista@1812
   276
        case PEP_ct_pEp:
krista@1812
   277
            return "PEP_ct_pEp";
krista@1812
   278
        default:
krista@1812
   279
            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
   280
    }
krista@1812
   281
}