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.
     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_DECRYPTED_BUT_UNSIGNED:
   141             return "PEP_DECRYPTED_BUT_UNSIGNED";
   142         case PEP_DECRYPT_MODIFICATION_DETECTED:
   143             return "PEP_DECRYPT_MODIFICATION_DETECTED";
   144         case PEP_DECRYPT_NO_KEY_FOR_SIGNER:
   145             return "PEP_DECRYPT_NO_KEY_FOR_SIGNER";
   146         case PEP_VERIFY_NO_KEY:
   147             return "PEP_VERIFY_NO_KEY";
   148         case PEP_VERIFIED_AND_TRUSTED:
   149             return "PEP_VERIFIED_AND_TRUSTED";
   150         case PEP_CANNOT_DECRYPT_UNKNOWN:
   151             return "PEP_CANNOT_DECRYPT_UNKNOWN";
   152         case PEP_TRUSTWORD_NOT_FOUND:
   153             return "PEP_TRUSTWORD_NOT_FOUND";
   154         case PEP_TRUSTWORDS_FPR_WRONG_LENGTH:
   155             return "PEP_TRUSTWORDS_FPR_WRONG_LENGTH";
   156         case PEP_CANNOT_CREATE_KEY:
   157             return "PEP_CANNOT_CREATE_KEY";
   158         case PEP_CANNOT_SEND_KEY:
   159             return "PEP_CANNOT_SEND_KEY";
   160         case PEP_PHRASE_NOT_FOUND:
   161             return "PEP_PHRASE_NOT_FOUND";
   162         case PEP_SEND_FUNCTION_NOT_REGISTERED:
   163             return "PEP_SEND_FUNCTION_NOT_REGISTERED";
   164         case PEP_CONTRAINTS_VIOLATED:
   165             return "PEP_CONTRAINTS_VIOLATED";
   166         case PEP_CANNOT_ENCODE:
   167             return "PEP_CANNOT_ENCODE";
   168         case PEP_SYNC_NO_NOTIFY_CALLBACK:
   169             return "PEP_SYNC_NO_NOTIFY_CALLBACK";
   170         case PEP_SYNC_ILLEGAL_MESSAGE:
   171             return "PEP_SYNC_ILLEGAL_MESSAGE";
   172         case PEP_SYNC_NO_INJECT_CALLBACK:
   173             return "PEP_SYNC_NO_INJECT_CALLBACK";
   174         case PEP_SEQUENCE_VIOLATED:
   175             return "PEP_SEQUENCE_VIOLATED";
   176         case PEP_CANNOT_INCREASE_SEQUENCE:
   177             return "PEP_CANNOT_INCREASE_SEQUENCE";
   178         case PEP_CANNOT_SET_SEQUENCE_VALUE:
   179             return "PEP_CANNOT_SET_SEQUENCE_VALUE";
   180         case PEP_OWN_SEQUENCE:
   181             return "PEP_OWN_SEQUENCE";
   182         case PEP_SYNC_STATEMACHINE_ERROR:
   183             return "PEP_SYNC_STATEMACHINE_ERROR";
   184         case PEP_SYNC_NO_TRUST:
   185             return "PEP_SYNC_NO_TRUST";
   186         case PEP_STATEMACHINE_INVALID_STATE:
   187             return "PEP_STATEMACHINE_INVALID_STATE";
   188         case PEP_STATEMACHINE_INVALID_EVENT:
   189             return "PEP_STATEMACHINE_INVALID_EVENT";
   190         case PEP_STATEMACHINE_INVALID_CONDITION:
   191             return "PEP_STATEMACHINE_INVALID_CONDITION";
   192         case PEP_STATEMACHINE_INVALID_ACTION:
   193             return "PEP_STATEMACHINE_INVALID_ACTION";
   194         case PEP_STATEMACHINE_INHIBITED_EVENT:
   195             return "PEP_STATEMACHINE_INHIBITED_EVENT";
   196         case PEP_COMMIT_FAILED:
   197             return "PEP_COMMIT_FAILED";
   198         case PEP_MESSAGE_CONSUME:
   199             return "PEP_MESSAGE_CONSUME";
   200         case PEP_MESSAGE_IGNORE:
   201             return "PEP_MESSAGE_IGNORE";
   202         case PEP_RECORD_NOT_FOUND:
   203             return "PEP_RECORD_NOT_FOUND";
   204         case PEP_CANNOT_CREATE_TEMP_FILE:
   205             return "PEP_CANNOT_CREATE_TEMP_FILE";
   206         case PEP_ILLEGAL_VALUE:
   207             return "PEP_ILLEGAL_VALUE";
   208         case PEP_BUFFER_TOO_SMALL:
   209             return "PEP_BUFFER_TOO_SMALL";
   210         case PEP_OUT_OF_MEMORY:
   211             return "PEP_OUT_OF_MEMORY";
   212         case PEP_UNKNOWN_ERROR:
   213             return "PEP_UNKNOWN_ERROR";    
   214         default:
   215  
   216             return "PEP_STATUS_OMGWTFBBQ - This means you're using a status the test lib doesn't know about!";
   217     }
   218 }
   219 const char* tl_rating_string(PEP_rating rating) {
   220     switch (rating) {
   221         case PEP_rating_undefined:
   222             return "PEP_rating_undefined";
   223         case PEP_rating_cannot_decrypt:
   224             return "PEP_rating_cannot_decrypt";
   225         case PEP_rating_have_no_key:
   226             return "PEP_rating_have_no_key";
   227         case PEP_rating_unencrypted:
   228             return "PEP_rating_unencrypted";
   229         case PEP_rating_unencrypted_for_some:
   230             return "PEP_rating_unencrypted_for_some";
   231         case PEP_rating_unreliable:
   232             return "PEP_rating_unreliable";
   233         case PEP_rating_reliable:
   234             return "PEP_rating_reliable";
   235         case PEP_rating_trusted:
   236             return "PEP_rating_trusted";
   237         case PEP_rating_trusted_and_anonymized:
   238             return "PEP_rating_trusted_and_anonymized";
   239         case PEP_rating_fully_anonymous:
   240             return "PEP_rating_fully_anonymous";
   241         case PEP_rating_mistrust:
   242             return "PEP_rating_mistrust";
   243         case PEP_rating_b0rken:
   244             return "PEP_rating_b0rken";
   245         case PEP_rating_under_attack:
   246             return "PEP_rating_under_attack";
   247         default:
   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.";
   249     }
   250 }
   251 
   252 const char* tl_ct_string(PEP_comm_type ct) {
   253     switch (ct) {
   254         case PEP_ct_unknown:
   255             return "PEP_ct_unknown";
   256         case PEP_ct_no_encryption:
   257             return "PEP_ct_no_encryption";
   258         case PEP_ct_no_encrypted_channel:
   259             return "PEP_ct_no_encrypted_channel";
   260         case PEP_ct_key_not_found:
   261             return "PEP_ct_key_not_found";
   262         case PEP_ct_key_expired:
   263             return "PEP_ct_key_expired";
   264         case PEP_ct_key_revoked:
   265             return "PEP_ct_key_revoked";
   266         case PEP_ct_key_b0rken:
   267             return "PEP_ct_key_b0rken";
   268         case PEP_ct_my_key_not_included:
   269             return "PEP_ct_my_key_not_included";
   270         case PEP_ct_security_by_obscurity:
   271             return "PEP_ct_security_by_obscurity";
   272         case PEP_ct_b0rken_crypto:
   273             return "PEP_ct_b0rken_crypto";
   274         case PEP_ct_key_too_short:
   275             return "PEP_ct_key_too_short";
   276         case PEP_ct_compromised:
   277             return "PEP_ct_compromised";
   278         case PEP_ct_mistrusted:
   279             return "PEP_ct_mistrusted";
   280         case PEP_ct_unconfirmed_encryption:
   281             return "PEP_ct_unconfirmed_encryption";
   282         case PEP_ct_OpenPGP_weak_unconfirmed:
   283             return "PEP_ct_OpenPGP_weak_unconfirmed";
   284         case PEP_ct_to_be_checked:
   285             return "PEP_ct_to_be_checked";
   286         case PEP_ct_SMIME_unconfirmed:
   287             return "PEP_ct_SMIME_unconfirmed";
   288         case PEP_ct_CMS_unconfirmed:
   289             return "PEP_ct_CMS_unconfirmed";
   290         case PEP_ct_strong_but_unconfirmed:
   291             return "PEP_ct_strong_but_unconfirmed";
   292         case PEP_ct_OpenPGP_unconfirmed:
   293             return "PEP_ct_OpenPGP_unconfirmed";
   294         case PEP_ct_OTR_unconfirmed:
   295             return "PEP_ct_OTR_unconfirmed";
   296         case PEP_ct_unconfirmed_enc_anon:
   297             return "PEP_ct_unconfirmed_enc_anon";
   298         case PEP_ct_pEp_unconfirmed:
   299             return "PEP_ct_pEp_unconfirmed";
   300         case PEP_ct_confirmed:
   301             return "PEP_ct_pEp_confirmed";
   302         case PEP_ct_confirmed_encryption:
   303             return "PEP_ct_confirmed_encryption";
   304         case PEP_ct_OpenPGP_weak:
   305             return "PEP_ct_OpenPGP_weak";
   306         case PEP_ct_to_be_checked_confirmed:
   307             return "PEP_ct_to_be_checked_confirmed";
   308         case PEP_ct_SMIME:
   309             return "PEP_ct_SMIME";
   310         case PEP_ct_CMS:
   311             return "PEP_ct_CMS";
   312         case PEP_ct_strong_encryption:
   313             return "PEP_ct_strong_encryption";
   314         case PEP_ct_OpenPGP:
   315             return "PEP_ct_OpenPGP";
   316         case PEP_ct_OTR:
   317             return "PEP_ct_OTR";
   318         case PEP_ct_confirmed_enc_anon:
   319             return "PEP_ct_confirmed_enc_anon";
   320         case PEP_ct_pEp:
   321             return "PEP_ct_pEp";
   322         default:
   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.";
   324     }
   325 }
   326 
   327 bool slurp_message_and_import_key(PEP_SESSION session, const char* message_fname, std::string& message, const char* key_filename) {
   328     message = slurp(message_fname);
   329     if (key_filename) {
   330         std::string keyfile = slurp(key_filename);
   331         if (import_key(session, keyfile.c_str(), keyfile.size(), NULL) != PEP_STATUS_OK)
   332             return false;
   333     }
   334     return true;
   335 }