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