ENGINE-434: PGP/MIME behaviour in and tested. Time to fix the rest. ENGINE-434
authorKrista Bennett <krista@pep-project.org>
Fri, 18 May 2018 10:20:08 +0200
branchENGINE-434
changeset 2678610a608c410d
parent 2677 a1ade141a1d1
child 2679 567462e5b11a
ENGINE-434: PGP/MIME behaviour in and tested. Time to fix the rest.
test/include/IntegrityTests.h
test/src/engine_tests/IntegrityTests.cc
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/include/IntegrityTests.h	Fri May 18 10:20:08 2018 +0200
     1.3 @@ -0,0 +1,49 @@
     1.4 +// This file is under GNU General Public License 3.0
     1.5 +// see LICENSE.txt
     1.6 +
     1.7 +#ifndef INTEGRITY_H
     1.8 +#define INTEGRITY_H
     1.9 +
    1.10 +#include <string>
    1.11 +#include "pEpEngine.h"
    1.12 +#include "message_api.h"
    1.13 +
    1.14 +#include "EngineTestIndividualSuite.h"
    1.15 +
    1.16 +using namespace std;
    1.17 +
    1.18 +class IntegrityTests : public EngineTestIndividualSuite {
    1.19 +    public:
    1.20 +        IntegrityTests(string test_suite, string test_home_dir);
    1.21 +        
    1.22 +    protected:
    1.23 +        void setup();
    1.24 +        void tear_down();
    1.25 +            
    1.26 +    private:
    1.27 +        const char* recip_fpr;
    1.28 +
    1.29 +        // used by each test
    1.30 +        string message;
    1.31 +        char* decrypted_msg;
    1.32 +        PEP_STATUS decrypt_status;
    1.33 +        PEP_rating rating;
    1.34 +        PEP_decrypt_flags_t flags;
    1.35 +        stringlist_t* keylist;
    1.36 +        char* dummy_ignore;
    1.37 +        
    1.38 +        void check_unsigned_PGP_MIME();
    1.39 +        void check_unsigned_PGP_MIME_attached_key();
    1.40 +        void check_unsigned_PGP_MIME_w_render_flag();
    1.41 +        void check_known_good_signed_PGP_MIME();
    1.42 +        void check_known_good_signed_PGP_MIME_attached_key();
    1.43 +        void check_unknown_signed_PGP_MIME_no_key();
    1.44 +        void check_unknown_signed_PGP_MIME_attached_key();
    1.45 +        void check_unsigned_PGP_MIME_corrupted();
    1.46 +        void check_signed_PGP_MIME_corrupted();
    1.47 +        void check_integrity();
    1.48 +        
    1.49 +
    1.50 +};
    1.51 +
    1.52 +#endif
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/test/src/engine_tests/IntegrityTests.cc	Fri May 18 10:20:08 2018 +0200
     2.3 @@ -0,0 +1,209 @@
     2.4 +// This file is under GNU General Public License 3.0
     2.5 +// see LICENSE.txt
     2.6 +
     2.7 +#include <stdlib.h>
     2.8 +#include <string>
     2.9 +#include <assert.h>
    2.10 +
    2.11 +#include "pEpEngine.h"
    2.12 +#include "message_api.h"
    2.13 +
    2.14 +#include "test_util.h"
    2.15 +
    2.16 +#include "EngineTestIndividualSuite.h"
    2.17 +#include "IntegrityTests.h"
    2.18 +
    2.19 +using namespace std;
    2.20 +
    2.21 +IntegrityTests::IntegrityTests(string suitename, string test_home_dir) :
    2.22 +    EngineTestIndividualSuite::EngineTestIndividualSuite(suitename, test_home_dir) {
    2.23 +    recip_fpr = "9D8047989841CF4207EA152A4ACAF735F390A40D";
    2.24 +    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("IntegrityTests::check_unsigned_PGP_MIME"),
    2.25 +                                                                      static_cast<Func>(&IntegrityTests::check_unsigned_PGP_MIME)));
    2.26 +    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("IntegrityTests::check_unsigned_PGP_MIME_attached_key"),
    2.27 +                                                                      static_cast<Func>(&IntegrityTests::check_unsigned_PGP_MIME_attached_key)));
    2.28 +    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("IntegrityTests::check_unsigned_PGP_MIME_w_render_flag"),
    2.29 +                                                                      static_cast<Func>(&IntegrityTests::check_unsigned_PGP_MIME_w_render_flag)));
    2.30 +    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("IntegrityTests::check_known_good_signed_PGP_MIME"),
    2.31 +                                                                      static_cast<Func>(&IntegrityTests::check_known_good_signed_PGP_MIME)));
    2.32 +    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("IntegrityTests::check_known_good_signed_PGP_MIME_attached_key"),
    2.33 +                                                                      static_cast<Func>(&IntegrityTests::check_known_good_signed_PGP_MIME_attached_key)));
    2.34 +    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("IntegrityTests::check_unknown_signed_PGP_MIME_no_key"),
    2.35 +                                                                      static_cast<Func>(&IntegrityTests::check_unknown_signed_PGP_MIME_no_key)));
    2.36 +    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("IntegrityTests::check_unknown_signed_PGP_MIME_attached_key"),
    2.37 +                                                                      static_cast<Func>(&IntegrityTests::check_unknown_signed_PGP_MIME_attached_key)));
    2.38 +    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("IntegrityTests::check_unsigned_PGP_MIME_corrupted"),
    2.39 +                                                                      static_cast<Func>(&IntegrityTests::check_unsigned_PGP_MIME_corrupted)));
    2.40 +    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("IntegrityTests::check_signed_PGP_MIME_corrupted"),
    2.41 +                                                                      static_cast<Func>(&IntegrityTests::check_signed_PGP_MIME_corrupted)));
    2.42 +    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("IntegrityTests::check_integrity"),
    2.43 +                                                                      static_cast<Func>(&IntegrityTests::check_integrity)));
    2.44 +}
    2.45 +
    2.46 +void IntegrityTests::setup() {
    2.47 +    EngineTestIndividualSuite::setup();
    2.48 +    string recip_key = slurp("test_keys/pub/integrity_test_recip_0-0xF390A40D_pub.asc");
    2.49 +    PEP_STATUS status = import_key(session, recip_key.c_str(), recip_key.size(), NULL);
    2.50 +    assert(status == PEP_STATUS_OK);
    2.51 +    recip_key = slurp("test_keys/priv/integrity_test_recip_0-0xF390A40D_priv.asc");
    2.52 +    status = import_key(session, recip_key.c_str(), recip_key.size(), NULL);
    2.53 +    assert(status == PEP_STATUS_OK);
    2.54 +    pEp_identity* me = new_identity("integrity_test_recip@darthmama.org", recip_fpr, PEP_OWN_USERID, "Integrity Test Recipient");
    2.55 +    assert(me != NULL);
    2.56 +    status = set_own_key(session, me, recip_fpr);
    2.57 +    assert(status == PEP_STATUS_OK);
    2.58 +
    2.59 +    message = "";
    2.60 +    decrypted_msg = NULL;
    2.61 +    decrypt_status = PEP_STATUS_OK;
    2.62 +    rating = PEP_rating_undefined;
    2.63 +    flags = 0;
    2.64 +    keylist = NULL;
    2.65 +    dummy_ignore = NULL;
    2.66 +}
    2.67 +
    2.68 +void IntegrityTests::tear_down() {
    2.69 +    free_stringlist(keylist);
    2.70 +    free(decrypted_msg);
    2.71 +    EngineTestIndividualSuite::tear_down();
    2.72 +}
    2.73 +
    2.74 +/*
    2.75 +Type            Error State             Render              Status Code
    2.76 +---------------------------------------------------------------------------------------------------------------
    2.77 +inline          ALL                     Yes, if present     Whatever GPG gives us
    2.78 +PGP/MIME        Unsigned                No                  DECRYPTED_BUT_UNSIGNED (grey)
    2.79 +                Signed, no key          Yes                 NO_KEY_FOR_SIGNER
    2.80 +                Bad sig                 No                  SIGNATURE_DOES_NOT_MATCH
    2.81 +Message 1.0     Unsigned                No                  MODIFICATION_DETECTED
    2.82 +                Signed, no key          No                  MODIFICATION_DETECTED
    2.83 +                Bad sig                 No                  SIGNATURE_DOES_NOT_MATCH
    2.84 +Message 2.0     Unsigned                No                  MODIFICATION_DETECTED (red)
    2.85 +                Signed, no key          No                  MODIFICATION_DETECTED  (red)
    2.86 +                Bad sig                 No                  SIGNATURE_DOES_NOT_MATCH
    2.87 +
    2.88 +*/
    2.89 +
    2.90 +void IntegrityTests::check_known_good_signed_PGP_MIME() {
    2.91 +    TEST_ASSERT(slurp_message_and_import_key(session, "test_mails/Signed no attach PGP_MIME.eml", message,
    2.92 +                                             "test_keys/pub/integrity_test_signer_0-0xFF26631A_pub.asc"));
    2.93 +
    2.94 +    decrypt_status = MIME_decrypt_message(session, message.c_str(), message.size(), &decrypted_msg, &keylist,
    2.95 +                                          &rating, &flags, &dummy_ignore);
    2.96 +
    2.97 +    snprintf(failed_msg_buf, TEST_FAILED_MESSAGE_BUFSIZE, "Decrypt status == %s, should be PEP_STATUS_OK", tl_status_string(decrypt_status));
    2.98 +    TEST_ASSERT_MSG(decrypt_status == PEP_STATUS_OK, failed_msg_buf);
    2.99 +    TEST_ASSERT(decrypted_msg != NULL);
   2.100 +    TEST_ASSERT(rating == PEP_rating_reliable);
   2.101 +}
   2.102 +
   2.103 +void IntegrityTests::check_known_good_signed_PGP_MIME_attached_key() {
   2.104 +    TEST_ASSERT(slurp_message_and_import_key(session, "test_mails/Signed attached key PGP_MIME.eml", message,
   2.105 +                                             NULL));
   2.106 +
   2.107 +    decrypt_status = MIME_decrypt_message(session, message.c_str(), message.size(), &decrypted_msg, &keylist,
   2.108 +                                          &rating, &flags, &dummy_ignore);
   2.109 +
   2.110 +    snprintf(failed_msg_buf, TEST_FAILED_MESSAGE_BUFSIZE, "Decrypt status == %s, should be PEP_STATUS_OK", tl_status_string(decrypt_status));
   2.111 +    TEST_ASSERT_MSG(decrypt_status == PEP_STATUS_OK, failed_msg_buf);
   2.112 +    TEST_ASSERT(decrypted_msg != NULL);
   2.113 +    TEST_ASSERT(rating == PEP_rating_reliable);
   2.114 +}
   2.115 +
   2.116 +void IntegrityTests::check_unsigned_PGP_MIME() {
   2.117 +    TEST_ASSERT(slurp_message_and_import_key(session, "test_mails/Unsigned from PGP_MIME_noattach.eml", message,
   2.118 +                                             "test_keys/pub/integrity_test_signer_0-0xFF26631A_pub.asc"));
   2.119 +
   2.120 +    decrypt_status = MIME_decrypt_message(session, message.c_str(), message.size(), &decrypted_msg, &keylist,
   2.121 +                                          &rating, &flags, &dummy_ignore);
   2.122 +
   2.123 +    snprintf(failed_msg_buf, TEST_FAILED_MESSAGE_BUFSIZE, "Decrypt status == %s, should be PEP_DECRYPTED_BUT_UNSIGNED", tl_status_string(decrypt_status));
   2.124 +    TEST_ASSERT_MSG(decrypt_status == PEP_DECRYPTED_BUT_UNSIGNED, failed_msg_buf);
   2.125 +    TEST_ASSERT(decrypted_msg == NULL);
   2.126 +    snprintf(failed_msg_buf, TEST_FAILED_MESSAGE_BUFSIZE, "Rating == %s, should be PEP_rating_unreliable", tl_rating_string(rating));
   2.127 +    TEST_ASSERT_MSG(rating == PEP_rating_unreliable, failed_msg_buf);
   2.128 +}
   2.129 +
   2.130 +void IntegrityTests::check_unsigned_PGP_MIME_attached_key() {
   2.131 +    TEST_ASSERT(slurp_message_and_import_key(session, "test_mails/Unsigned from PGP_MIME_attach.eml", message,
   2.132 +                                             NULL));
   2.133 +
   2.134 +    decrypt_status = MIME_decrypt_message(session, message.c_str(), message.size(), &decrypted_msg, &keylist,
   2.135 +                                          &rating, &flags, &dummy_ignore);
   2.136 +
   2.137 +    snprintf(failed_msg_buf, TEST_FAILED_MESSAGE_BUFSIZE, "Decrypt status == %s, should be PEP_DECRYPTED_BUT_UNSIGNED", tl_status_string(decrypt_status));
   2.138 +    TEST_ASSERT_MSG(decrypt_status == PEP_DECRYPTED_BUT_UNSIGNED, failed_msg_buf);
   2.139 +    TEST_ASSERT(decrypted_msg == NULL);
   2.140 +    snprintf(failed_msg_buf, TEST_FAILED_MESSAGE_BUFSIZE, "Rating == %s, should be PEP_rating_unreliable", tl_rating_string(rating));
   2.141 +    TEST_ASSERT_MSG(rating == PEP_rating_unreliable, failed_msg_buf);
   2.142 +}
   2.143 +
   2.144 +void IntegrityTests::check_unsigned_PGP_MIME_w_render_flag() {
   2.145 +    TEST_ASSERT(slurp_message_and_import_key(session, "test_mails/Unsigned from PGP_MIME_noattach.eml", message,
   2.146 +                                             "test_keys/pub/integrity_test_signer_0-0xFF26631A_pub.asc"));
   2.147 +    flags |= PEP_decrypt_deliver_pgpmime_badsigned;
   2.148 +    decrypt_status = MIME_decrypt_message(session, message.c_str(), message.size(), &decrypted_msg, &keylist,
   2.149 +                                          &rating, &flags, &dummy_ignore);
   2.150 +
   2.151 +    snprintf(failed_msg_buf, TEST_FAILED_MESSAGE_BUFSIZE, "Decrypt status == %s, should be PEP_DECRYPTED_BUT_UNSIGNED", tl_status_string(decrypt_status));
   2.152 +    TEST_ASSERT_MSG(decrypt_status == PEP_DECRYPTED_BUT_UNSIGNED, failed_msg_buf);
   2.153 +    TEST_ASSERT(decrypted_msg != NULL);
   2.154 +    TEST_ASSERT(rating == PEP_rating_unreliable);
   2.155 +}
   2.156 +
   2.157 +
   2.158 +void IntegrityTests::check_unknown_signed_PGP_MIME_no_key() {
   2.159 +    TEST_ASSERT(slurp_message_and_import_key(session, "test_mails/Signed PGP_MIME by unknown signer no attach.eml", message,
   2.160 +                                             NULL));
   2.161 +    decrypt_status = MIME_decrypt_message(session, message.c_str(), message.size(), &decrypted_msg, &keylist,
   2.162 +                                          &rating, &flags, &dummy_ignore);
   2.163 +
   2.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));
   2.165 +    TEST_ASSERT_MSG(decrypt_status == PEP_DECRYPT_NO_KEY_FOR_SIGNER, failed_msg_buf);
   2.166 +    TEST_ASSERT(decrypted_msg != NULL);
   2.167 +    TEST_ASSERT(rating == PEP_rating_unreliable);
   2.168 +}
   2.169 +
   2.170 +void IntegrityTests::check_unknown_signed_PGP_MIME_attached_key() {
   2.171 +    TEST_ASSERT(slurp_message_and_import_key(session, "test_mails/Signed PGP_MIME by unknown signer attach.eml", message,
   2.172 +                                             NULL));
   2.173 +
   2.174 +    decrypt_status = MIME_decrypt_message(session, message.c_str(), message.size(), &decrypted_msg, &keylist,
   2.175 +                                          &rating, &flags, &dummy_ignore);
   2.176 +
   2.177 +    snprintf(failed_msg_buf, TEST_FAILED_MESSAGE_BUFSIZE, "Decrypt status == %s, should be PEP_STATUS_OK", tl_status_string(decrypt_status));
   2.178 +    TEST_ASSERT_MSG(decrypt_status == PEP_STATUS_OK, failed_msg_buf);
   2.179 +    TEST_ASSERT(decrypted_msg != NULL);
   2.180 +    TEST_ASSERT(rating == PEP_rating_reliable);
   2.181 +}
   2.182 +
   2.183 +// FIXME: we need cleverer attacked mails
   2.184 +void IntegrityTests::check_unsigned_PGP_MIME_corrupted() {
   2.185 +    TEST_ASSERT(slurp_message_and_import_key(session, "test_mails/Unsigned from PGP_MIME_attach_corrupted.eml", message,
   2.186 +                                             NULL));
   2.187 +
   2.188 +    decrypt_status = MIME_decrypt_message(session, message.c_str(), message.size(), &decrypted_msg, &keylist,
   2.189 +                                          &rating, &flags, &dummy_ignore);
   2.190 +
   2.191 +//    snprintf(failed_msg_buf, TEST_FAILED_MESSAGE_BUFSIZE, "Decrypt status == %s, should be PEP_STATUS_OK", tl_status_string(decrypt_status));
   2.192 +//    TEST_ASSERT_MSG(decrypt_status == PEP_STATUS_OK, failed_msg_buf);
   2.193 +    TEST_ASSERT(decrypt_status != PEP_STATUS_OK && decrypt_status != PEP_DECRYPTED);
   2.194 +    TEST_ASSERT(decrypted_msg == NULL);
   2.195 +}
   2.196 +
   2.197 +void IntegrityTests::check_signed_PGP_MIME_corrupted() {
   2.198 +    TEST_ASSERT(slurp_message_and_import_key(session, "test_mails/Signed attached key PGP_MIME_corrupted.eml", message,
   2.199 +                                             NULL));
   2.200 +
   2.201 +    decrypt_status = MIME_decrypt_message(session, message.c_str(), message.size(), &decrypted_msg, &keylist,
   2.202 +                                          &rating, &flags, &dummy_ignore);
   2.203 +
   2.204 +//    snprintf(failed_msg_buf, TEST_FAILED_MESSAGE_BUFSIZE, "Decrypt status == %s, should be PEP_STATUS_OK", tl_status_string(decrypt_status));
   2.205 +//    TEST_ASSERT_MSG(decrypt_status == PEP_STATUS_OK, failed_msg_buf);
   2.206 +    TEST_ASSERT(decrypt_status != PEP_STATUS_OK && decrypt_status != PEP_DECRYPTED);
   2.207 +    TEST_ASSERT(decrypted_msg == NULL);
   2.208 +}
   2.209 +
   2.210 +void IntegrityTests::check_integrity() {
   2.211 +    TEST_ASSERT(true);
   2.212 +}