test/src/engine_tests/IntegrityTests.cc
author Krista Bennett <krista@pep-project.org>
Fri, 18 May 2018 10:20:08 +0200
branchENGINE-434
changeset 2678 610a608c410d
child 2679 567462e5b11a
permissions -rw-r--r--
ENGINE-434: PGP/MIME behaviour in and tested. Time to fix the rest.
     1 // This file is under GNU General Public License 3.0
     2 // see LICENSE.txt
     3 
     4 #include <stdlib.h>
     5 #include <string>
     6 #include <assert.h>
     7 
     8 #include "pEpEngine.h"
     9 #include "message_api.h"
    10 
    11 #include "test_util.h"
    12 
    13 #include "EngineTestIndividualSuite.h"
    14 #include "IntegrityTests.h"
    15 
    16 using namespace std;
    17 
    18 IntegrityTests::IntegrityTests(string suitename, string test_home_dir) :
    19     EngineTestIndividualSuite::EngineTestIndividualSuite(suitename, test_home_dir) {
    20     recip_fpr = "9D8047989841CF4207EA152A4ACAF735F390A40D";
    21     add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("IntegrityTests::check_unsigned_PGP_MIME"),
    22                                                                       static_cast<Func>(&IntegrityTests::check_unsigned_PGP_MIME)));
    23     add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("IntegrityTests::check_unsigned_PGP_MIME_attached_key"),
    24                                                                       static_cast<Func>(&IntegrityTests::check_unsigned_PGP_MIME_attached_key)));
    25     add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("IntegrityTests::check_unsigned_PGP_MIME_w_render_flag"),
    26                                                                       static_cast<Func>(&IntegrityTests::check_unsigned_PGP_MIME_w_render_flag)));
    27     add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("IntegrityTests::check_known_good_signed_PGP_MIME"),
    28                                                                       static_cast<Func>(&IntegrityTests::check_known_good_signed_PGP_MIME)));
    29     add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("IntegrityTests::check_known_good_signed_PGP_MIME_attached_key"),
    30                                                                       static_cast<Func>(&IntegrityTests::check_known_good_signed_PGP_MIME_attached_key)));
    31     add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("IntegrityTests::check_unknown_signed_PGP_MIME_no_key"),
    32                                                                       static_cast<Func>(&IntegrityTests::check_unknown_signed_PGP_MIME_no_key)));
    33     add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("IntegrityTests::check_unknown_signed_PGP_MIME_attached_key"),
    34                                                                       static_cast<Func>(&IntegrityTests::check_unknown_signed_PGP_MIME_attached_key)));
    35     add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("IntegrityTests::check_unsigned_PGP_MIME_corrupted"),
    36                                                                       static_cast<Func>(&IntegrityTests::check_unsigned_PGP_MIME_corrupted)));
    37     add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("IntegrityTests::check_signed_PGP_MIME_corrupted"),
    38                                                                       static_cast<Func>(&IntegrityTests::check_signed_PGP_MIME_corrupted)));
    39     add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("IntegrityTests::check_integrity"),
    40                                                                       static_cast<Func>(&IntegrityTests::check_integrity)));
    41 }
    42 
    43 void IntegrityTests::setup() {
    44     EngineTestIndividualSuite::setup();
    45     string recip_key = slurp("test_keys/pub/integrity_test_recip_0-0xF390A40D_pub.asc");
    46     PEP_STATUS status = import_key(session, recip_key.c_str(), recip_key.size(), NULL);
    47     assert(status == PEP_STATUS_OK);
    48     recip_key = slurp("test_keys/priv/integrity_test_recip_0-0xF390A40D_priv.asc");
    49     status = import_key(session, recip_key.c_str(), recip_key.size(), NULL);
    50     assert(status == PEP_STATUS_OK);
    51     pEp_identity* me = new_identity("integrity_test_recip@darthmama.org", recip_fpr, PEP_OWN_USERID, "Integrity Test Recipient");
    52     assert(me != NULL);
    53     status = set_own_key(session, me, recip_fpr);
    54     assert(status == PEP_STATUS_OK);
    55 
    56     message = "";
    57     decrypted_msg = NULL;
    58     decrypt_status = PEP_STATUS_OK;
    59     rating = PEP_rating_undefined;
    60     flags = 0;
    61     keylist = NULL;
    62     dummy_ignore = NULL;
    63 }
    64 
    65 void IntegrityTests::tear_down() {
    66     free_stringlist(keylist);
    67     free(decrypted_msg);
    68     EngineTestIndividualSuite::tear_down();
    69 }
    70 
    71 /*
    72 Type            Error State             Render              Status Code
    73 ---------------------------------------------------------------------------------------------------------------
    74 inline          ALL                     Yes, if present     Whatever GPG gives us
    75 PGP/MIME        Unsigned                No                  DECRYPTED_BUT_UNSIGNED (grey)
    76                 Signed, no key          Yes                 NO_KEY_FOR_SIGNER
    77                 Bad sig                 No                  SIGNATURE_DOES_NOT_MATCH
    78 Message 1.0     Unsigned                No                  MODIFICATION_DETECTED
    79                 Signed, no key          No                  MODIFICATION_DETECTED
    80                 Bad sig                 No                  SIGNATURE_DOES_NOT_MATCH
    81 Message 2.0     Unsigned                No                  MODIFICATION_DETECTED (red)
    82                 Signed, no key          No                  MODIFICATION_DETECTED  (red)
    83                 Bad sig                 No                  SIGNATURE_DOES_NOT_MATCH
    84 
    85 */
    86 
    87 void IntegrityTests::check_known_good_signed_PGP_MIME() {
    88     TEST_ASSERT(slurp_message_and_import_key(session, "test_mails/Signed no attach PGP_MIME.eml", message,
    89                                              "test_keys/pub/integrity_test_signer_0-0xFF26631A_pub.asc"));
    90 
    91     decrypt_status = MIME_decrypt_message(session, message.c_str(), message.size(), &decrypted_msg, &keylist,
    92                                           &rating, &flags, &dummy_ignore);
    93 
    94     snprintf(failed_msg_buf, TEST_FAILED_MESSAGE_BUFSIZE, "Decrypt status == %s, should be PEP_STATUS_OK", tl_status_string(decrypt_status));
    95     TEST_ASSERT_MSG(decrypt_status == PEP_STATUS_OK, failed_msg_buf);
    96     TEST_ASSERT(decrypted_msg != NULL);
    97     TEST_ASSERT(rating == PEP_rating_reliable);
    98 }
    99 
   100 void IntegrityTests::check_known_good_signed_PGP_MIME_attached_key() {
   101     TEST_ASSERT(slurp_message_and_import_key(session, "test_mails/Signed attached key PGP_MIME.eml", message,
   102                                              NULL));
   103 
   104     decrypt_status = MIME_decrypt_message(session, message.c_str(), message.size(), &decrypted_msg, &keylist,
   105                                           &rating, &flags, &dummy_ignore);
   106 
   107     snprintf(failed_msg_buf, TEST_FAILED_MESSAGE_BUFSIZE, "Decrypt status == %s, should be PEP_STATUS_OK", tl_status_string(decrypt_status));
   108     TEST_ASSERT_MSG(decrypt_status == PEP_STATUS_OK, failed_msg_buf);
   109     TEST_ASSERT(decrypted_msg != NULL);
   110     TEST_ASSERT(rating == PEP_rating_reliable);
   111 }
   112 
   113 void IntegrityTests::check_unsigned_PGP_MIME() {
   114     TEST_ASSERT(slurp_message_and_import_key(session, "test_mails/Unsigned from PGP_MIME_noattach.eml", message,
   115                                              "test_keys/pub/integrity_test_signer_0-0xFF26631A_pub.asc"));
   116 
   117     decrypt_status = MIME_decrypt_message(session, message.c_str(), message.size(), &decrypted_msg, &keylist,
   118                                           &rating, &flags, &dummy_ignore);
   119 
   120     snprintf(failed_msg_buf, TEST_FAILED_MESSAGE_BUFSIZE, "Decrypt status == %s, should be PEP_DECRYPTED_BUT_UNSIGNED", tl_status_string(decrypt_status));
   121     TEST_ASSERT_MSG(decrypt_status == PEP_DECRYPTED_BUT_UNSIGNED, failed_msg_buf);
   122     TEST_ASSERT(decrypted_msg == NULL);
   123     snprintf(failed_msg_buf, TEST_FAILED_MESSAGE_BUFSIZE, "Rating == %s, should be PEP_rating_unreliable", tl_rating_string(rating));
   124     TEST_ASSERT_MSG(rating == PEP_rating_unreliable, failed_msg_buf);
   125 }
   126 
   127 void IntegrityTests::check_unsigned_PGP_MIME_attached_key() {
   128     TEST_ASSERT(slurp_message_and_import_key(session, "test_mails/Unsigned from PGP_MIME_attach.eml", message,
   129                                              NULL));
   130 
   131     decrypt_status = MIME_decrypt_message(session, message.c_str(), message.size(), &decrypted_msg, &keylist,
   132                                           &rating, &flags, &dummy_ignore);
   133 
   134     snprintf(failed_msg_buf, TEST_FAILED_MESSAGE_BUFSIZE, "Decrypt status == %s, should be PEP_DECRYPTED_BUT_UNSIGNED", tl_status_string(decrypt_status));
   135     TEST_ASSERT_MSG(decrypt_status == PEP_DECRYPTED_BUT_UNSIGNED, failed_msg_buf);
   136     TEST_ASSERT(decrypted_msg == NULL);
   137     snprintf(failed_msg_buf, TEST_FAILED_MESSAGE_BUFSIZE, "Rating == %s, should be PEP_rating_unreliable", tl_rating_string(rating));
   138     TEST_ASSERT_MSG(rating == PEP_rating_unreliable, failed_msg_buf);
   139 }
   140 
   141 void IntegrityTests::check_unsigned_PGP_MIME_w_render_flag() {
   142     TEST_ASSERT(slurp_message_and_import_key(session, "test_mails/Unsigned from PGP_MIME_noattach.eml", message,
   143                                              "test_keys/pub/integrity_test_signer_0-0xFF26631A_pub.asc"));
   144     flags |= PEP_decrypt_deliver_pgpmime_badsigned;
   145     decrypt_status = MIME_decrypt_message(session, message.c_str(), message.size(), &decrypted_msg, &keylist,
   146                                           &rating, &flags, &dummy_ignore);
   147 
   148     snprintf(failed_msg_buf, TEST_FAILED_MESSAGE_BUFSIZE, "Decrypt status == %s, should be PEP_DECRYPTED_BUT_UNSIGNED", tl_status_string(decrypt_status));
   149     TEST_ASSERT_MSG(decrypt_status == PEP_DECRYPTED_BUT_UNSIGNED, failed_msg_buf);
   150     TEST_ASSERT(decrypted_msg != NULL);
   151     TEST_ASSERT(rating == PEP_rating_unreliable);
   152 }
   153 
   154 
   155 void IntegrityTests::check_unknown_signed_PGP_MIME_no_key() {
   156     TEST_ASSERT(slurp_message_and_import_key(session, "test_mails/Signed PGP_MIME by unknown signer no attach.eml", message,
   157                                              NULL));
   158     decrypt_status = MIME_decrypt_message(session, message.c_str(), message.size(), &decrypted_msg, &keylist,
   159                                           &rating, &flags, &dummy_ignore);
   160 
   161     snprintf(failed_msg_buf, TEST_FAILED_MESSAGE_BUFSIZE, "Decrypt status == %s, should be PEP_DECRYPT_NO_KEY_FOR_SIGNER", tl_status_string(decrypt_status));
   162     TEST_ASSERT_MSG(decrypt_status == PEP_DECRYPT_NO_KEY_FOR_SIGNER, failed_msg_buf);
   163     TEST_ASSERT(decrypted_msg != NULL);
   164     TEST_ASSERT(rating == PEP_rating_unreliable);
   165 }
   166 
   167 void IntegrityTests::check_unknown_signed_PGP_MIME_attached_key() {
   168     TEST_ASSERT(slurp_message_and_import_key(session, "test_mails/Signed PGP_MIME by unknown signer attach.eml", message,
   169                                              NULL));
   170 
   171     decrypt_status = MIME_decrypt_message(session, message.c_str(), message.size(), &decrypted_msg, &keylist,
   172                                           &rating, &flags, &dummy_ignore);
   173 
   174     snprintf(failed_msg_buf, TEST_FAILED_MESSAGE_BUFSIZE, "Decrypt status == %s, should be PEP_STATUS_OK", tl_status_string(decrypt_status));
   175     TEST_ASSERT_MSG(decrypt_status == PEP_STATUS_OK, failed_msg_buf);
   176     TEST_ASSERT(decrypted_msg != NULL);
   177     TEST_ASSERT(rating == PEP_rating_reliable);
   178 }
   179 
   180 // FIXME: we need cleverer attacked mails
   181 void IntegrityTests::check_unsigned_PGP_MIME_corrupted() {
   182     TEST_ASSERT(slurp_message_and_import_key(session, "test_mails/Unsigned from PGP_MIME_attach_corrupted.eml", message,
   183                                              NULL));
   184 
   185     decrypt_status = MIME_decrypt_message(session, message.c_str(), message.size(), &decrypted_msg, &keylist,
   186                                           &rating, &flags, &dummy_ignore);
   187 
   188 //    snprintf(failed_msg_buf, TEST_FAILED_MESSAGE_BUFSIZE, "Decrypt status == %s, should be PEP_STATUS_OK", tl_status_string(decrypt_status));
   189 //    TEST_ASSERT_MSG(decrypt_status == PEP_STATUS_OK, failed_msg_buf);
   190     TEST_ASSERT(decrypt_status != PEP_STATUS_OK && decrypt_status != PEP_DECRYPTED);
   191     TEST_ASSERT(decrypted_msg == NULL);
   192 }
   193 
   194 void IntegrityTests::check_signed_PGP_MIME_corrupted() {
   195     TEST_ASSERT(slurp_message_and_import_key(session, "test_mails/Signed attached key PGP_MIME_corrupted.eml", message,
   196                                              NULL));
   197 
   198     decrypt_status = MIME_decrypt_message(session, message.c_str(), message.size(), &decrypted_msg, &keylist,
   199                                           &rating, &flags, &dummy_ignore);
   200 
   201 //    snprintf(failed_msg_buf, TEST_FAILED_MESSAGE_BUFSIZE, "Decrypt status == %s, should be PEP_STATUS_OK", tl_status_string(decrypt_status));
   202 //    TEST_ASSERT_MSG(decrypt_status == PEP_STATUS_OK, failed_msg_buf);
   203     TEST_ASSERT(decrypt_status != PEP_STATUS_OK && decrypt_status != PEP_DECRYPTED);
   204     TEST_ASSERT(decrypted_msg == NULL);
   205 }
   206 
   207 void IntegrityTests::check_integrity() {
   208     TEST_ASSERT(true);
   209 }