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