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