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