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