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