test/src/engine_tests/IntegrityTests.cc
branchENGINE-434
changeset 2678 610a608c410d
child 2679 567462e5b11a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/src/engine_tests/IntegrityTests.cc	Fri May 18 10:20:08 2018 +0200
     1.3 @@ -0,0 +1,209 @@
     1.4 +// This file is under GNU General Public License 3.0
     1.5 +// see LICENSE.txt
     1.6 +
     1.7 +#include <stdlib.h>
     1.8 +#include <string>
     1.9 +#include <assert.h>
    1.10 +
    1.11 +#include "pEpEngine.h"
    1.12 +#include "message_api.h"
    1.13 +
    1.14 +#include "test_util.h"
    1.15 +
    1.16 +#include "EngineTestIndividualSuite.h"
    1.17 +#include "IntegrityTests.h"
    1.18 +
    1.19 +using namespace std;
    1.20 +
    1.21 +IntegrityTests::IntegrityTests(string suitename, string test_home_dir) :
    1.22 +    EngineTestIndividualSuite::EngineTestIndividualSuite(suitename, test_home_dir) {
    1.23 +    recip_fpr = "9D8047989841CF4207EA152A4ACAF735F390A40D";
    1.24 +    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("IntegrityTests::check_unsigned_PGP_MIME"),
    1.25 +                                                                      static_cast<Func>(&IntegrityTests::check_unsigned_PGP_MIME)));
    1.26 +    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("IntegrityTests::check_unsigned_PGP_MIME_attached_key"),
    1.27 +                                                                      static_cast<Func>(&IntegrityTests::check_unsigned_PGP_MIME_attached_key)));
    1.28 +    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("IntegrityTests::check_unsigned_PGP_MIME_w_render_flag"),
    1.29 +                                                                      static_cast<Func>(&IntegrityTests::check_unsigned_PGP_MIME_w_render_flag)));
    1.30 +    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("IntegrityTests::check_known_good_signed_PGP_MIME"),
    1.31 +                                                                      static_cast<Func>(&IntegrityTests::check_known_good_signed_PGP_MIME)));
    1.32 +    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("IntegrityTests::check_known_good_signed_PGP_MIME_attached_key"),
    1.33 +                                                                      static_cast<Func>(&IntegrityTests::check_known_good_signed_PGP_MIME_attached_key)));
    1.34 +    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("IntegrityTests::check_unknown_signed_PGP_MIME_no_key"),
    1.35 +                                                                      static_cast<Func>(&IntegrityTests::check_unknown_signed_PGP_MIME_no_key)));
    1.36 +    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("IntegrityTests::check_unknown_signed_PGP_MIME_attached_key"),
    1.37 +                                                                      static_cast<Func>(&IntegrityTests::check_unknown_signed_PGP_MIME_attached_key)));
    1.38 +    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("IntegrityTests::check_unsigned_PGP_MIME_corrupted"),
    1.39 +                                                                      static_cast<Func>(&IntegrityTests::check_unsigned_PGP_MIME_corrupted)));
    1.40 +    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("IntegrityTests::check_signed_PGP_MIME_corrupted"),
    1.41 +                                                                      static_cast<Func>(&IntegrityTests::check_signed_PGP_MIME_corrupted)));
    1.42 +    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("IntegrityTests::check_integrity"),
    1.43 +                                                                      static_cast<Func>(&IntegrityTests::check_integrity)));
    1.44 +}
    1.45 +
    1.46 +void IntegrityTests::setup() {
    1.47 +    EngineTestIndividualSuite::setup();
    1.48 +    string recip_key = slurp("test_keys/pub/integrity_test_recip_0-0xF390A40D_pub.asc");
    1.49 +    PEP_STATUS status = import_key(session, recip_key.c_str(), recip_key.size(), NULL);
    1.50 +    assert(status == PEP_STATUS_OK);
    1.51 +    recip_key = slurp("test_keys/priv/integrity_test_recip_0-0xF390A40D_priv.asc");
    1.52 +    status = import_key(session, recip_key.c_str(), recip_key.size(), NULL);
    1.53 +    assert(status == PEP_STATUS_OK);
    1.54 +    pEp_identity* me = new_identity("integrity_test_recip@darthmama.org", recip_fpr, PEP_OWN_USERID, "Integrity Test Recipient");
    1.55 +    assert(me != NULL);
    1.56 +    status = set_own_key(session, me, recip_fpr);
    1.57 +    assert(status == PEP_STATUS_OK);
    1.58 +
    1.59 +    message = "";
    1.60 +    decrypted_msg = NULL;
    1.61 +    decrypt_status = PEP_STATUS_OK;
    1.62 +    rating = PEP_rating_undefined;
    1.63 +    flags = 0;
    1.64 +    keylist = NULL;
    1.65 +    dummy_ignore = NULL;
    1.66 +}
    1.67 +
    1.68 +void IntegrityTests::tear_down() {
    1.69 +    free_stringlist(keylist);
    1.70 +    free(decrypted_msg);
    1.71 +    EngineTestIndividualSuite::tear_down();
    1.72 +}
    1.73 +
    1.74 +/*
    1.75 +Type            Error State             Render              Status Code
    1.76 +---------------------------------------------------------------------------------------------------------------
    1.77 +inline          ALL                     Yes, if present     Whatever GPG gives us
    1.78 +PGP/MIME        Unsigned                No                  DECRYPTED_BUT_UNSIGNED (grey)
    1.79 +                Signed, no key          Yes                 NO_KEY_FOR_SIGNER
    1.80 +                Bad sig                 No                  SIGNATURE_DOES_NOT_MATCH
    1.81 +Message 1.0     Unsigned                No                  MODIFICATION_DETECTED
    1.82 +                Signed, no key          No                  MODIFICATION_DETECTED
    1.83 +                Bad sig                 No                  SIGNATURE_DOES_NOT_MATCH
    1.84 +Message 2.0     Unsigned                No                  MODIFICATION_DETECTED (red)
    1.85 +                Signed, no key          No                  MODIFICATION_DETECTED  (red)
    1.86 +                Bad sig                 No                  SIGNATURE_DOES_NOT_MATCH
    1.87 +
    1.88 +*/
    1.89 +
    1.90 +void IntegrityTests::check_known_good_signed_PGP_MIME() {
    1.91 +    TEST_ASSERT(slurp_message_and_import_key(session, "test_mails/Signed no attach PGP_MIME.eml", message,
    1.92 +                                             "test_keys/pub/integrity_test_signer_0-0xFF26631A_pub.asc"));
    1.93 +
    1.94 +    decrypt_status = MIME_decrypt_message(session, message.c_str(), message.size(), &decrypted_msg, &keylist,
    1.95 +                                          &rating, &flags, &dummy_ignore);
    1.96 +
    1.97 +    snprintf(failed_msg_buf, TEST_FAILED_MESSAGE_BUFSIZE, "Decrypt status == %s, should be PEP_STATUS_OK", tl_status_string(decrypt_status));
    1.98 +    TEST_ASSERT_MSG(decrypt_status == PEP_STATUS_OK, failed_msg_buf);
    1.99 +    TEST_ASSERT(decrypted_msg != NULL);
   1.100 +    TEST_ASSERT(rating == PEP_rating_reliable);
   1.101 +}
   1.102 +
   1.103 +void IntegrityTests::check_known_good_signed_PGP_MIME_attached_key() {
   1.104 +    TEST_ASSERT(slurp_message_and_import_key(session, "test_mails/Signed attached key PGP_MIME.eml", message,
   1.105 +                                             NULL));
   1.106 +
   1.107 +    decrypt_status = MIME_decrypt_message(session, message.c_str(), message.size(), &decrypted_msg, &keylist,
   1.108 +                                          &rating, &flags, &dummy_ignore);
   1.109 +
   1.110 +    snprintf(failed_msg_buf, TEST_FAILED_MESSAGE_BUFSIZE, "Decrypt status == %s, should be PEP_STATUS_OK", tl_status_string(decrypt_status));
   1.111 +    TEST_ASSERT_MSG(decrypt_status == PEP_STATUS_OK, failed_msg_buf);
   1.112 +    TEST_ASSERT(decrypted_msg != NULL);
   1.113 +    TEST_ASSERT(rating == PEP_rating_reliable);
   1.114 +}
   1.115 +
   1.116 +void IntegrityTests::check_unsigned_PGP_MIME() {
   1.117 +    TEST_ASSERT(slurp_message_and_import_key(session, "test_mails/Unsigned from PGP_MIME_noattach.eml", message,
   1.118 +                                             "test_keys/pub/integrity_test_signer_0-0xFF26631A_pub.asc"));
   1.119 +
   1.120 +    decrypt_status = MIME_decrypt_message(session, message.c_str(), message.size(), &decrypted_msg, &keylist,
   1.121 +                                          &rating, &flags, &dummy_ignore);
   1.122 +
   1.123 +    snprintf(failed_msg_buf, TEST_FAILED_MESSAGE_BUFSIZE, "Decrypt status == %s, should be PEP_DECRYPTED_BUT_UNSIGNED", tl_status_string(decrypt_status));
   1.124 +    TEST_ASSERT_MSG(decrypt_status == PEP_DECRYPTED_BUT_UNSIGNED, failed_msg_buf);
   1.125 +    TEST_ASSERT(decrypted_msg == NULL);
   1.126 +    snprintf(failed_msg_buf, TEST_FAILED_MESSAGE_BUFSIZE, "Rating == %s, should be PEP_rating_unreliable", tl_rating_string(rating));
   1.127 +    TEST_ASSERT_MSG(rating == PEP_rating_unreliable, failed_msg_buf);
   1.128 +}
   1.129 +
   1.130 +void IntegrityTests::check_unsigned_PGP_MIME_attached_key() {
   1.131 +    TEST_ASSERT(slurp_message_and_import_key(session, "test_mails/Unsigned from PGP_MIME_attach.eml", message,
   1.132 +                                             NULL));
   1.133 +
   1.134 +    decrypt_status = MIME_decrypt_message(session, message.c_str(), message.size(), &decrypted_msg, &keylist,
   1.135 +                                          &rating, &flags, &dummy_ignore);
   1.136 +
   1.137 +    snprintf(failed_msg_buf, TEST_FAILED_MESSAGE_BUFSIZE, "Decrypt status == %s, should be PEP_DECRYPTED_BUT_UNSIGNED", tl_status_string(decrypt_status));
   1.138 +    TEST_ASSERT_MSG(decrypt_status == PEP_DECRYPTED_BUT_UNSIGNED, failed_msg_buf);
   1.139 +    TEST_ASSERT(decrypted_msg == NULL);
   1.140 +    snprintf(failed_msg_buf, TEST_FAILED_MESSAGE_BUFSIZE, "Rating == %s, should be PEP_rating_unreliable", tl_rating_string(rating));
   1.141 +    TEST_ASSERT_MSG(rating == PEP_rating_unreliable, failed_msg_buf);
   1.142 +}
   1.143 +
   1.144 +void IntegrityTests::check_unsigned_PGP_MIME_w_render_flag() {
   1.145 +    TEST_ASSERT(slurp_message_and_import_key(session, "test_mails/Unsigned from PGP_MIME_noattach.eml", message,
   1.146 +                                             "test_keys/pub/integrity_test_signer_0-0xFF26631A_pub.asc"));
   1.147 +    flags |= PEP_decrypt_deliver_pgpmime_badsigned;
   1.148 +    decrypt_status = MIME_decrypt_message(session, message.c_str(), message.size(), &decrypted_msg, &keylist,
   1.149 +                                          &rating, &flags, &dummy_ignore);
   1.150 +
   1.151 +    snprintf(failed_msg_buf, TEST_FAILED_MESSAGE_BUFSIZE, "Decrypt status == %s, should be PEP_DECRYPTED_BUT_UNSIGNED", tl_status_string(decrypt_status));
   1.152 +    TEST_ASSERT_MSG(decrypt_status == PEP_DECRYPTED_BUT_UNSIGNED, failed_msg_buf);
   1.153 +    TEST_ASSERT(decrypted_msg != NULL);
   1.154 +    TEST_ASSERT(rating == PEP_rating_unreliable);
   1.155 +}
   1.156 +
   1.157 +
   1.158 +void IntegrityTests::check_unknown_signed_PGP_MIME_no_key() {
   1.159 +    TEST_ASSERT(slurp_message_and_import_key(session, "test_mails/Signed PGP_MIME by unknown signer no attach.eml", message,
   1.160 +                                             NULL));
   1.161 +    decrypt_status = MIME_decrypt_message(session, message.c_str(), message.size(), &decrypted_msg, &keylist,
   1.162 +                                          &rating, &flags, &dummy_ignore);
   1.163 +
   1.164 +    snprintf(failed_msg_buf, TEST_FAILED_MESSAGE_BUFSIZE, "Decrypt status == %s, should be PEP_DECRYPT_NO_KEY_FOR_SIGNER", tl_status_string(decrypt_status));
   1.165 +    TEST_ASSERT_MSG(decrypt_status == PEP_DECRYPT_NO_KEY_FOR_SIGNER, failed_msg_buf);
   1.166 +    TEST_ASSERT(decrypted_msg != NULL);
   1.167 +    TEST_ASSERT(rating == PEP_rating_unreliable);
   1.168 +}
   1.169 +
   1.170 +void IntegrityTests::check_unknown_signed_PGP_MIME_attached_key() {
   1.171 +    TEST_ASSERT(slurp_message_and_import_key(session, "test_mails/Signed PGP_MIME by unknown signer attach.eml", message,
   1.172 +                                             NULL));
   1.173 +
   1.174 +    decrypt_status = MIME_decrypt_message(session, message.c_str(), message.size(), &decrypted_msg, &keylist,
   1.175 +                                          &rating, &flags, &dummy_ignore);
   1.176 +
   1.177 +    snprintf(failed_msg_buf, TEST_FAILED_MESSAGE_BUFSIZE, "Decrypt status == %s, should be PEP_STATUS_OK", tl_status_string(decrypt_status));
   1.178 +    TEST_ASSERT_MSG(decrypt_status == PEP_STATUS_OK, failed_msg_buf);
   1.179 +    TEST_ASSERT(decrypted_msg != NULL);
   1.180 +    TEST_ASSERT(rating == PEP_rating_reliable);
   1.181 +}
   1.182 +
   1.183 +// FIXME: we need cleverer attacked mails
   1.184 +void IntegrityTests::check_unsigned_PGP_MIME_corrupted() {
   1.185 +    TEST_ASSERT(slurp_message_and_import_key(session, "test_mails/Unsigned from PGP_MIME_attach_corrupted.eml", message,
   1.186 +                                             NULL));
   1.187 +
   1.188 +    decrypt_status = MIME_decrypt_message(session, message.c_str(), message.size(), &decrypted_msg, &keylist,
   1.189 +                                          &rating, &flags, &dummy_ignore);
   1.190 +
   1.191 +//    snprintf(failed_msg_buf, TEST_FAILED_MESSAGE_BUFSIZE, "Decrypt status == %s, should be PEP_STATUS_OK", tl_status_string(decrypt_status));
   1.192 +//    TEST_ASSERT_MSG(decrypt_status == PEP_STATUS_OK, failed_msg_buf);
   1.193 +    TEST_ASSERT(decrypt_status != PEP_STATUS_OK && decrypt_status != PEP_DECRYPTED);
   1.194 +    TEST_ASSERT(decrypted_msg == NULL);
   1.195 +}
   1.196 +
   1.197 +void IntegrityTests::check_signed_PGP_MIME_corrupted() {
   1.198 +    TEST_ASSERT(slurp_message_and_import_key(session, "test_mails/Signed attached key PGP_MIME_corrupted.eml", message,
   1.199 +                                             NULL));
   1.200 +
   1.201 +    decrypt_status = MIME_decrypt_message(session, message.c_str(), message.size(), &decrypted_msg, &keylist,
   1.202 +                                          &rating, &flags, &dummy_ignore);
   1.203 +
   1.204 +//    snprintf(failed_msg_buf, TEST_FAILED_MESSAGE_BUFSIZE, "Decrypt status == %s, should be PEP_STATUS_OK", tl_status_string(decrypt_status));
   1.205 +//    TEST_ASSERT_MSG(decrypt_status == PEP_STATUS_OK, failed_msg_buf);
   1.206 +    TEST_ASSERT(decrypt_status != PEP_STATUS_OK && decrypt_status != PEP_DECRYPTED);
   1.207 +    TEST_ASSERT(decrypted_msg == NULL);
   1.208 +}
   1.209 +
   1.210 +void IntegrityTests::check_integrity() {
   1.211 +    TEST_ASSERT(true);
   1.212 +}