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