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