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