test/src/util/test_util.cc
branchsync
changeset 3335 09af2cf0ac74
parent 3314 18a40b81649a
child 3422 09bec729847d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/src/util/test_util.cc	Fri Mar 15 12:43:56 2019 +0100
     1.3 @@ -0,0 +1,419 @@
     1.4 +#include "pEpEngine_test.h"
     1.5 +#include "pEpEngine.h"
     1.6 +#include "pEp_internal.h"
     1.7 +#include "message_api.h"
     1.8 +#include "test_util.h"
     1.9 +#include "TestConstants.h"
    1.10 +
    1.11 +#include <fstream>
    1.12 +#include <sstream>
    1.13 +#include <stdexcept>
    1.14 +#include <stdlib.h>
    1.15 +#include <sys/stat.h>
    1.16 +#include <errno.h>
    1.17 +#include <stdlib.h>
    1.18 +#include <unistd.h>
    1.19 +#include <ftw.h>
    1.20 +
    1.21 +PEP_STATUS read_file_and_import_key(PEP_SESSION session, const char* fname) {
    1.22 +    const std::string key = slurp(fname);
    1.23 +    PEP_STATUS status = (key.empty() ? PEP_KEY_NOT_FOUND : PEP_STATUS_OK);
    1.24 +    if (status == PEP_STATUS_OK)
    1.25 +        status = import_key(session, key.c_str(), key.size(), NULL);
    1.26 +    return status;    
    1.27 +}
    1.28 +
    1.29 +PEP_STATUS set_up_ident_from_scratch(PEP_SESSION session,
    1.30 +                                     const char* key_fname,
    1.31 +                                     const char* address,
    1.32 +                                     const char* fpr,
    1.33 +                                     const char* user_id,
    1.34 +                                     const char* username,
    1.35 +                                     pEp_identity** ret_ident,
    1.36 +                                     bool is_priv) {
    1.37 +    PEP_STATUS status = read_file_and_import_key(session,key_fname);
    1.38 +    if (status != PEP_KEY_IMPORTED)
    1.39 +        return status;
    1.40 +    else
    1.41 +        status = PEP_STATUS_OK;
    1.42 +    
    1.43 +    pEp_identity* ident = new_identity(address, fpr, user_id, username);
    1.44 +    if (is_priv && fpr) {
    1.45 +        status = set_own_key(session, ident, fpr);
    1.46 +        if (status == PEP_STATUS_OK)
    1.47 +            status = myself(session, ident);
    1.48 +    }
    1.49 +    else    
    1.50 +        status = update_identity(session, ident);
    1.51 +
    1.52 +    if (status != PEP_STATUS_OK)
    1.53 +        goto pep_free;
    1.54 +        
    1.55 +    if (!ident || !ident->fpr) {
    1.56 +        status = PEP_CANNOT_FIND_IDENTITY;
    1.57 +        goto pep_free;
    1.58 +    }
    1.59 +    
    1.60 +    if (ret_ident)
    1.61 +        *ret_ident = ident;
    1.62 +        
    1.63 +pep_free:
    1.64 +    if (!ret_ident)
    1.65 +        free_identity(ident);
    1.66 +    return status;    
    1.67 +}
    1.68 +
    1.69 +
    1.70 +bool file_exists(std::string filename) {
    1.71 +    struct stat buffer;
    1.72 +    return (stat(filename.c_str(), &buffer) == 0);
    1.73 +}
    1.74 +
    1.75 +char* str_to_lower(const char* str) {
    1.76 +    if (!str)
    1.77 +        return NULL;
    1.78 +    int str_len = strlen(str);
    1.79 +    if (str_len == 0)
    1.80 +        return strdup("");
    1.81 +    int i;
    1.82 +    
    1.83 +    char* retval = (char*) calloc(1, str_len + 1);
    1.84 +    for (i = 0; i < str_len; i++) {
    1.85 +        retval[i] = tolower(str[i]);
    1.86 +    }    
    1.87 +    return retval;
    1.88 +}
    1.89 +
    1.90 +// Because annoyed
    1.91 +bool _streq(const char* str1, const char* str2) {
    1.92 +    if (!str1) {
    1.93 +        if (str2)
    1.94 +            return false;
    1.95 +        return true;
    1.96 +    }
    1.97 +    if (!str2)
    1.98 +        return false;
    1.99 +        
   1.100 +    return (strcmp(str1, str2) == 0);
   1.101 +}
   1.102 +
   1.103 +bool _strceq(const char* str1, const char* str2) {
   1.104 +    char* str1_dup = str_to_lower(str1);
   1.105 +    char* str2_dup = str_to_lower(str2);
   1.106 +
   1.107 +    bool retval = _streq(str_to_lower(str1_dup), str_to_lower(str2_dup));
   1.108 +    free(str1_dup);
   1.109 +    free(str2_dup);
   1.110 +    return retval;
   1.111 +}
   1.112 +
   1.113 +void test_init() {
   1.114 +    unlink ("../test_home/.pEp_management.db");
   1.115 +    unlink ("../test_home/.pEp_management.db-shm");
   1.116 +    unlink ("../test_home/.pEp_management.db-wal");
   1.117 +}
   1.118 +
   1.119 +std::string slurp(const std::string& filename)
   1.120 +{
   1.121 +	std::ifstream input(filename.c_str());
   1.122 +	if(!input)
   1.123 +	{
   1.124 +		throw std::runtime_error("Cannot read file \"" + filename + "\"! ");
   1.125 +	}
   1.126 +	
   1.127 +	std::stringstream sstr;
   1.128 +	sstr << input.rdbuf();
   1.129 +	return sstr.str();
   1.130 +}
   1.131 +
   1.132 +void dump_out(const char* filename, const char* outdata)
   1.133 +{
   1.134 +	std::ofstream outfile(filename);
   1.135 +	if(!outfile)
   1.136 +	{
   1.137 +		throw std::runtime_error("Cannot open output file!");
   1.138 +	}
   1.139 +	
   1.140 +	outfile << outdata;
   1.141 +    outfile.close();
   1.142 +}
   1.143 +
   1.144 +char* get_new_uuid() {
   1.145 +    char* new_uuid = (char*)calloc(37, 1);
   1.146 +    pEpUUID uuid;
   1.147 +    uuid_generate_random(uuid);
   1.148 +    uuid_unparse_upper(uuid, new_uuid);
   1.149 +    return new_uuid;
   1.150 +}
   1.151 +
   1.152 +const char* tl_status_string(PEP_STATUS status) {
   1.153 +    switch (status) {
   1.154 +        case PEP_STATUS_OK:
   1.155 +            return "PEP_STATUS_OK";
   1.156 +        case PEP_INIT_CANNOT_LOAD_GPGME:
   1.157 +            return "PEP_INIT_CANNOT_LOAD_GPGME";
   1.158 +        case PEP_INIT_GPGME_INIT_FAILED:
   1.159 +            return "PEP_INIT_GPGME_INIT_FAILED";
   1.160 +        case PEP_INIT_NO_GPG_HOME:
   1.161 +            return "PEP_INIT_NO_GPG_HOME";
   1.162 +        case PEP_INIT_NETPGP_INIT_FAILED:
   1.163 +            return "PEP_INIT_NETPGP_INIT_FAILED";
   1.164 +        case PEP_INIT_SQLITE3_WITHOUT_MUTEX:
   1.165 +            return "PEP_INIT_SQLITE3_WITHOUT_MUTEX";
   1.166 +        case PEP_INIT_CANNOT_OPEN_DB:
   1.167 +            return "PEP_INIT_CANNOT_OPEN_DB";
   1.168 +        case PEP_INIT_CANNOT_OPEN_SYSTEM_DB:
   1.169 +            return "PEP_INIT_CANNOT_OPEN_SYSTEM_DB";
   1.170 +        case PEP_KEY_NOT_FOUND:
   1.171 +            return "PEP_KEY_NOT_FOUND";
   1.172 +        case PEP_KEY_HAS_AMBIG_NAME:
   1.173 +            return "PEP_KEY_HAS_AMBIG_NAME";
   1.174 +        case PEP_GET_KEY_FAILED:
   1.175 +            return "PEP_GET_KEY_FAILED";
   1.176 +        case PEP_CANNOT_EXPORT_KEY:
   1.177 +            return "PEP_CANNOT_EXPORT_KEY";
   1.178 +        case PEP_CANNOT_EDIT_KEY:
   1.179 +            return "PEP_CANNOT_EDIT_KEY";
   1.180 +        case PEP_CANNOT_FIND_IDENTITY:
   1.181 +            return "PEP_CANNOT_FIND_IDENTITY";
   1.182 +        case PEP_CANNOT_SET_PERSON:
   1.183 +            return "PEP_CANNOT_SET_PERSON";
   1.184 +        case PEP_CANNOT_SET_PGP_KEYPAIR:
   1.185 +            return "PEP_CANNOT_SET_PGP_KEYPAIR";
   1.186 +        case PEP_CANNOT_SET_IDENTITY:
   1.187 +            return "PEP_CANNOT_SET_IDENTITY";
   1.188 +        case PEP_CANNOT_SET_TRUST:
   1.189 +            return "PEP_CANNOT_SET_TRUST";
   1.190 +        case PEP_KEY_BLACKLISTED:
   1.191 +            return "PEP_KEY_BLACKLISTED";
   1.192 +        case PEP_UNENCRYPTED:
   1.193 +            return "PEP_UNENCRYPTED";
   1.194 +        case PEP_VERIFIED:
   1.195 +            return "PEP_VERIFIED";
   1.196 +        case PEP_DECRYPTED:
   1.197 +            return "PEP_DECRYPTED";
   1.198 +        case PEP_DECRYPTED_AND_VERIFIED:
   1.199 +            return "PEP_DECRYPTED_AND_VERIFIED";
   1.200 +        case PEP_DECRYPT_WRONG_FORMAT:
   1.201 +            return "PEP_DECRYPT_WRONG_FORMAT";
   1.202 +        case PEP_DECRYPT_NO_KEY:
   1.203 +            return "PEP_DECRYPT_NO_KEY";
   1.204 +        case PEP_DECRYPT_SIGNATURE_DOES_NOT_MATCH:
   1.205 +            return "PEP_DECRYPT_SIGNATURE_DOES_NOT_MATCH";
   1.206 +//        case PEP_DECRYPTED_BUT_UNSIGNED:
   1.207 +//            return "PEP_DECRYPTED_BUT_UNSIGNED";
   1.208 +//        case PEP_DECRYPT_MODIFICATION_DETECTED:
   1.209 +//            return "PEP_DECRYPT_MODIFICATION_DETECTED";
   1.210 +//        case PEP_DECRYPT_NO_KEY_FOR_SIGNER:
   1.211 +//            return "PEP_DECRYPT_NO_KEY_FOR_SIGNER";
   1.212 +        case PEP_VERIFY_NO_KEY:
   1.213 +            return "PEP_VERIFY_NO_KEY";
   1.214 +        case PEP_VERIFIED_AND_TRUSTED:
   1.215 +            return "PEP_VERIFIED_AND_TRUSTED";
   1.216 +        case PEP_CANNOT_DECRYPT_UNKNOWN:
   1.217 +            return "PEP_CANNOT_DECRYPT_UNKNOWN";
   1.218 +        case PEP_TRUSTWORD_NOT_FOUND:
   1.219 +            return "PEP_TRUSTWORD_NOT_FOUND";
   1.220 +        case PEP_TRUSTWORDS_FPR_WRONG_LENGTH:
   1.221 +            return "PEP_TRUSTWORDS_FPR_WRONG_LENGTH";
   1.222 +        case PEP_CANNOT_CREATE_KEY:
   1.223 +            return "PEP_CANNOT_CREATE_KEY";
   1.224 +        case PEP_CANNOT_SEND_KEY:
   1.225 +            return "PEP_CANNOT_SEND_KEY";
   1.226 +        case PEP_PHRASE_NOT_FOUND:
   1.227 +            return "PEP_PHRASE_NOT_FOUND";
   1.228 +        case PEP_SEND_FUNCTION_NOT_REGISTERED:
   1.229 +            return "PEP_SEND_FUNCTION_NOT_REGISTERED";
   1.230 +        case PEP_CONTRAINTS_VIOLATED:
   1.231 +            return "PEP_CONTRAINTS_VIOLATED";
   1.232 +        case PEP_CANNOT_ENCODE:
   1.233 +            return "PEP_CANNOT_ENCODE";
   1.234 +        case PEP_SYNC_NO_NOTIFY_CALLBACK:
   1.235 +            return "PEP_SYNC_NO_NOTIFY_CALLBACK";
   1.236 +        case PEP_SYNC_ILLEGAL_MESSAGE:
   1.237 +            return "PEP_SYNC_ILLEGAL_MESSAGE";
   1.238 +        case PEP_SYNC_NO_INJECT_CALLBACK:
   1.239 +            return "PEP_SYNC_NO_INJECT_CALLBACK";
   1.240 +        case PEP_CANNOT_INCREASE_SEQUENCE:
   1.241 +            return "PEP_CANNOT_INCREASE_SEQUENCE";
   1.242 +        case PEP_STATEMACHINE_ERROR:
   1.243 +            return "PEP_STATEMACHINE_ERROR";
   1.244 +        case PEP_NO_TRUST:
   1.245 +            return "PEP_NO_TRUST";
   1.246 +        case PEP_STATEMACHINE_INVALID_STATE:
   1.247 +            return "PEP_STATEMACHINE_INVALID_STATE";
   1.248 +        case PEP_STATEMACHINE_INVALID_EVENT:
   1.249 +            return "PEP_STATEMACHINE_INVALID_EVENT";
   1.250 +        case PEP_STATEMACHINE_INVALID_CONDITION:
   1.251 +            return "PEP_STATEMACHINE_INVALID_CONDITION";
   1.252 +        case PEP_STATEMACHINE_INVALID_ACTION:
   1.253 +            return "PEP_STATEMACHINE_INVALID_ACTION";
   1.254 +        case PEP_STATEMACHINE_INHIBITED_EVENT:
   1.255 +            return "PEP_STATEMACHINE_INHIBITED_EVENT";
   1.256 +        case PEP_COMMIT_FAILED:
   1.257 +            return "PEP_COMMIT_FAILED";
   1.258 +        case PEP_MESSAGE_CONSUME:
   1.259 +            return "PEP_MESSAGE_CONSUME";
   1.260 +        case PEP_MESSAGE_IGNORE:
   1.261 +            return "PEP_MESSAGE_IGNORE";
   1.262 +        case PEP_RECORD_NOT_FOUND:
   1.263 +            return "PEP_RECORD_NOT_FOUND";
   1.264 +        case PEP_CANNOT_CREATE_TEMP_FILE:
   1.265 +            return "PEP_CANNOT_CREATE_TEMP_FILE";
   1.266 +        case PEP_ILLEGAL_VALUE:
   1.267 +            return "PEP_ILLEGAL_VALUE";
   1.268 +        case PEP_BUFFER_TOO_SMALL:
   1.269 +            return "PEP_BUFFER_TOO_SMALL";
   1.270 +        case PEP_OUT_OF_MEMORY:
   1.271 +            return "PEP_OUT_OF_MEMORY";
   1.272 +        case PEP_UNKNOWN_ERROR:
   1.273 +            return "PEP_UNKNOWN_ERROR";    
   1.274 +        default:
   1.275 + 
   1.276 +            return "PEP_STATUS_OMGWTFBBQ - This means you're using a status the test lib doesn't know about!";
   1.277 +    }
   1.278 +}
   1.279 +const char* tl_rating_string(PEP_rating rating) {
   1.280 +    switch (rating) {
   1.281 +        case PEP_rating_undefined:
   1.282 +            return "PEP_rating_undefined";
   1.283 +        case PEP_rating_cannot_decrypt:
   1.284 +            return "PEP_rating_cannot_decrypt";
   1.285 +        case PEP_rating_have_no_key:
   1.286 +            return "PEP_rating_have_no_key";
   1.287 +        case PEP_rating_unencrypted:
   1.288 +            return "PEP_rating_unencrypted";
   1.289 +        case PEP_rating_unencrypted_for_some:
   1.290 +            return "PEP_rating_unencrypted_for_some";
   1.291 +        case PEP_rating_unreliable:
   1.292 +            return "PEP_rating_unreliable";
   1.293 +        case PEP_rating_reliable:
   1.294 +            return "PEP_rating_reliable";
   1.295 +        case PEP_rating_trusted:
   1.296 +            return "PEP_rating_trusted";
   1.297 +        case PEP_rating_trusted_and_anonymized:
   1.298 +            return "PEP_rating_trusted_and_anonymized";
   1.299 +        case PEP_rating_fully_anonymous:
   1.300 +            return "PEP_rating_fully_anonymous";
   1.301 +        case PEP_rating_mistrust:
   1.302 +            return "PEP_rating_mistrust";
   1.303 +        case PEP_rating_b0rken:
   1.304 +            return "PEP_rating_b0rken";
   1.305 +        case PEP_rating_under_attack:
   1.306 +            return "PEP_rating_under_attack";
   1.307 +        default:
   1.308 +            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.";
   1.309 +    }
   1.310 +}
   1.311 +
   1.312 +const char* tl_ct_string(PEP_comm_type ct) {
   1.313 +    switch (ct) {
   1.314 +        case PEP_ct_unknown:
   1.315 +            return "PEP_ct_unknown";
   1.316 +        case PEP_ct_no_encryption:
   1.317 +            return "PEP_ct_no_encryption";
   1.318 +        case PEP_ct_no_encrypted_channel:
   1.319 +            return "PEP_ct_no_encrypted_channel";
   1.320 +        case PEP_ct_key_not_found:
   1.321 +            return "PEP_ct_key_not_found";
   1.322 +        case PEP_ct_key_expired:
   1.323 +            return "PEP_ct_key_expired";
   1.324 +        case PEP_ct_key_revoked:
   1.325 +            return "PEP_ct_key_revoked";
   1.326 +        case PEP_ct_key_b0rken:
   1.327 +            return "PEP_ct_key_b0rken";
   1.328 +        case PEP_ct_my_key_not_included:
   1.329 +            return "PEP_ct_my_key_not_included";
   1.330 +        case PEP_ct_security_by_obscurity:
   1.331 +            return "PEP_ct_security_by_obscurity";
   1.332 +        case PEP_ct_b0rken_crypto:
   1.333 +            return "PEP_ct_b0rken_crypto";
   1.334 +        case PEP_ct_key_too_short:
   1.335 +            return "PEP_ct_key_too_short";
   1.336 +        case PEP_ct_compromised:
   1.337 +            return "PEP_ct_compromised";
   1.338 +        case PEP_ct_mistrusted:
   1.339 +            return "PEP_ct_mistrusted";
   1.340 +        case PEP_ct_unconfirmed_encryption:
   1.341 +            return "PEP_ct_unconfirmed_encryption";
   1.342 +        case PEP_ct_OpenPGP_weak_unconfirmed:
   1.343 +            return "PEP_ct_OpenPGP_weak_unconfirmed";
   1.344 +        case PEP_ct_to_be_checked:
   1.345 +            return "PEP_ct_to_be_checked";
   1.346 +        case PEP_ct_SMIME_unconfirmed:
   1.347 +            return "PEP_ct_SMIME_unconfirmed";
   1.348 +        case PEP_ct_CMS_unconfirmed:
   1.349 +            return "PEP_ct_CMS_unconfirmed";
   1.350 +        case PEP_ct_strong_but_unconfirmed:
   1.351 +            return "PEP_ct_strong_but_unconfirmed";
   1.352 +        case PEP_ct_OpenPGP_unconfirmed:
   1.353 +            return "PEP_ct_OpenPGP_unconfirmed";
   1.354 +        case PEP_ct_OTR_unconfirmed:
   1.355 +            return "PEP_ct_OTR_unconfirmed";
   1.356 +        case PEP_ct_unconfirmed_enc_anon:
   1.357 +            return "PEP_ct_unconfirmed_enc_anon";
   1.358 +        case PEP_ct_pEp_unconfirmed:
   1.359 +            return "PEP_ct_pEp_unconfirmed";
   1.360 +        case PEP_ct_confirmed:
   1.361 +            return "PEP_ct_pEp_confirmed";
   1.362 +        case PEP_ct_confirmed_encryption:
   1.363 +            return "PEP_ct_confirmed_encryption";
   1.364 +        case PEP_ct_OpenPGP_weak:
   1.365 +            return "PEP_ct_OpenPGP_weak";
   1.366 +        case PEP_ct_to_be_checked_confirmed:
   1.367 +            return "PEP_ct_to_be_checked_confirmed";
   1.368 +        case PEP_ct_SMIME:
   1.369 +            return "PEP_ct_SMIME";
   1.370 +        case PEP_ct_CMS:
   1.371 +            return "PEP_ct_CMS";
   1.372 +        case PEP_ct_strong_encryption:
   1.373 +            return "PEP_ct_strong_encryption";
   1.374 +        case PEP_ct_OpenPGP:
   1.375 +            return "PEP_ct_OpenPGP";
   1.376 +        case PEP_ct_OTR:
   1.377 +            return "PEP_ct_OTR";
   1.378 +        case PEP_ct_confirmed_enc_anon:
   1.379 +            return "PEP_ct_confirmed_enc_anon";
   1.380 +        case PEP_ct_pEp:
   1.381 +            return "PEP_ct_pEp";
   1.382 +        default:
   1.383 +            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.";
   1.384 +    }
   1.385 +}
   1.386 +
   1.387 +bool slurp_and_import_key(PEP_SESSION session, const char* key_filename) {
   1.388 +    std::string keyfile = slurp(key_filename);
   1.389 +    if (import_key(session, keyfile.c_str(), keyfile.size(), NULL) != PEP_TEST_KEY_IMPORT_SUCCESS)
   1.390 +        return false;
   1.391 +    return true;
   1.392 +}
   1.393 +
   1.394 +bool slurp_message_and_import_key(PEP_SESSION session, const char* message_fname, std::string& message, const char* key_filename) {
   1.395 +    bool ok = true;
   1.396 +    message = slurp(message_fname);
   1.397 +    if (key_filename)
   1.398 +        ok = slurp_and_import_key(session, key_filename);
   1.399 +    return ok;
   1.400 +}
   1.401 +
   1.402 +
   1.403 +
   1.404 +int util_delete_filepath(const char *filepath, 
   1.405 +                         const struct stat *file_stat, 
   1.406 +                         int ftw_info, 
   1.407 +                         struct FTW * ftw_struct) {
   1.408 +    int retval = 0;
   1.409 +    switch (ftw_info) {
   1.410 +        case FTW_DP:
   1.411 +            retval = rmdir(filepath);
   1.412 +            break;
   1.413 +        case FTW_F:
   1.414 +        case FTW_SLN:
   1.415 +            retval = unlink(filepath);
   1.416 +            break;    
   1.417 +        default:
   1.418 +            retval = -1;
   1.419 +    }
   1.420 +    
   1.421 +    return retval;
   1.422 +}