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