test/src/EncryptForIdentityTest.cc
author Krista 'DarthMama' Bennett <krista@pep.foundation>
Fri, 29 May 2020 14:39:48 +0200
changeset 4719 b4d7c37ac808
parent 4716 f8a5313261e6
child 4726 c3a9837c15e6
child 4736 94beaee71f5c
permissions -rw-r--r--
ENGINE-755: now works and is tested, though if we want to do this right we should modify the rest of the test as well... !!!!!!!!REQUIRES LIBETPAN UPDATE!!!!!!!!
     1 // This file is under GNU General Public License 3.0
     2 // see LICENSE.txt
     3 
     4 #include <stdlib.h>
     5 #include "TestConstants.h"
     6 #include <unistd.h>
     7 #include <string>
     8 #include <cstring>
     9 #include <iostream>
    10 #include <fstream>
    11 
    12 #include "pEpEngine.h"
    13 #include "mime.h"
    14 #include "message_api.h"
    15 #include "keymanagement.h"
    16 #include "test_util.h"
    17 
    18 
    19 
    20 #include "Engine.h"
    21 
    22 #include <gtest/gtest.h>
    23 
    24 
    25 namespace {
    26 
    27 	//The fixture for EncryptForIdentityTest
    28     class EncryptForIdentityTest : public ::testing::Test {
    29         public:
    30             Engine* engine;
    31             PEP_SESSION session;
    32 
    33         protected:
    34             // You can remove any or all of the following functions if its body
    35             // is empty.
    36             EncryptForIdentityTest() {
    37                 // You can do set-up work for each test here.
    38                 test_suite_name = ::testing::UnitTest::GetInstance()->current_test_info()->GTEST_SUITE_SYM();
    39                 test_name = ::testing::UnitTest::GetInstance()->current_test_info()->name();
    40                 test_path = get_main_test_home_dir() + "/" + test_suite_name + "/" + test_name;
    41             }
    42 
    43             ~EncryptForIdentityTest() override {
    44                 // You can do clean-up work that doesn't throw exceptions here.
    45             }
    46 
    47             // If the constructor and destructor are not enough for setting up
    48             // and cleaning up each test, you can define the following methods:
    49 
    50             void SetUp() override {
    51                 // Code here will be called immediately after the constructor (right
    52                 // before each test).
    53 
    54                 // Leave this empty if there are no files to copy to the home directory path
    55                 std::vector<std::pair<std::string, std::string>> init_files = std::vector<std::pair<std::string, std::string>>();
    56 
    57                 // Get a new test Engine.
    58                 engine = new Engine(test_path);
    59                 ASSERT_NE(engine, nullptr);
    60 
    61                 // Ok, let's initialize test directories etc.
    62                 engine->prep(NULL, NULL, init_files);
    63 
    64                 // Ok, try to start this bugger.
    65                 engine->start();
    66                 ASSERT_NE(engine->session, nullptr);
    67                 session = engine->session;
    68 
    69                 // Engine is up. Keep on truckin'
    70             }
    71 
    72             void TearDown() override {
    73                 // Code here will be called immediately after each test (right
    74                 // before the destructor).
    75                 engine->shut_down();
    76                 delete engine;
    77                 engine = NULL;
    78                 session = NULL;
    79             }
    80 
    81         private:
    82             const char* test_suite_name;
    83             const char* test_name;
    84             string test_path;
    85             // Objects declared here can be used by all tests in the EncryptForIdentityTest suite.
    86 
    87     };
    88 
    89 }  // namespace
    90 
    91 
    92 TEST_F(EncryptForIdentityTest, check_encrypt_for_identity) {
    93 
    94     // message_api test code
    95 
    96     const string alice_pub_key = slurp("test_keys/pub/pep-test-alice-0x6FF00E97_pub.asc");
    97     const string alice_priv_key = slurp("test_keys/priv/pep-test-alice-0x6FF00E97_priv.asc");
    98     const string gabrielle_pub_key = slurp("test_keys/pub/pep-test-gabrielle-0xE203586C_pub.asc");
    99     const string bella_pub_key = slurp("test_keys/pub/pep.test.bella-0xAF516AAE_pub.asc");
   100 
   101     PEP_STATUS statuspub = import_key(session, alice_pub_key.c_str(), alice_pub_key.length(), NULL);
   102     PEP_STATUS statuspriv = import_key(session, alice_priv_key.c_str(), alice_priv_key.length(), NULL);
   103     ASSERT_EQ(statuspub, PEP_TEST_KEY_IMPORT_SUCCESS);
   104     ASSERT_EQ(statuspriv, PEP_TEST_KEY_IMPORT_SUCCESS);
   105 
   106     statuspub = import_key(session, gabrielle_pub_key.c_str(), gabrielle_pub_key.length(), NULL);
   107     ASSERT_EQ(statuspub, PEP_TEST_KEY_IMPORT_SUCCESS);
   108     statuspub = import_key(session, bella_pub_key.c_str(), bella_pub_key.length(), NULL);
   109     ASSERT_EQ(statuspub, PEP_TEST_KEY_IMPORT_SUCCESS);
   110 
   111     const char* alice_fpr = "4ABE3AAF59AC32CFE4F86500A9411D176FF00E97";
   112     const char* gabrielle_fpr = "906C9B8349954E82C5623C3C8C541BD4E203586C";
   113     const char* bella_fpr = "5631BF1357326A02AA470EEEB815EF7FA4516AAE";
   114     const char* nobody_fpr = "1111111111111111111111111111111111111111";
   115 
   116     output_stream << "creating message…\n";
   117     pEp_identity* alice = new_identity("pep.test.alice@pep-project.org", alice_fpr, PEP_OWN_USERID, "Alice Test");
   118     pEp_identity* bob = new_identity("pep.test.bob@pep-project.org", NULL, "42", "Bob Test");
   119 
   120     alice->me = true;
   121 
   122     PEP_STATUS mystatus = set_own_key(session, alice, alice_fpr);
   123     ASSERT_EQ(mystatus, PEP_STATUS_OK);
   124 
   125     identity_list* to_list = new_identity_list(bob); // to bob
   126     message* outgoing_message = new_message(PEP_dir_outgoing);
   127     ASSERT_NE(outgoing_message, nullptr);
   128     outgoing_message->from = alice;
   129     outgoing_message->to = to_list;
   130     outgoing_message->shortmsg = strdup("Greetings, humans!");
   131     outgoing_message->longmsg = strdup("This is a test of the emergency message system. This is only a test. BEEP.");
   132     outgoing_message->attachments = new_bloblist(NULL, 0, "application/octet-stream", NULL);
   133     output_stream << "message created.\n";
   134 
   135     char* encoded_text = nullptr;
   136     PEP_STATUS status = mime_encode_message(outgoing_message, false, &encoded_text, false);
   137     ASSERT_EQ(status, PEP_STATUS_OK);
   138     ASSERT_NE(encoded_text, nullptr);
   139 
   140     output_stream << "decrypted:\n\n";
   141     output_stream << encoded_text << "\n";
   142 
   143     free(encoded_text);
   144 
   145     message* encrypted_msg = nullptr;
   146     output_stream << "calling encrypt_message_for_identity()\n";
   147     status = encrypt_message_for_self(session, alice, outgoing_message, NULL, &encrypted_msg, PEP_enc_PGP_MIME, PEP_encrypt_flag_force_unsigned | PEP_encrypt_flag_force_no_attached_key);
   148     output_stream << "encrypt_message() returns " << tl_status_string(status) << '.' << endl;
   149     ASSERT_EQ(status, PEP_STATUS_OK);
   150     ASSERT_NE(encrypted_msg, nullptr);
   151     output_stream << "message encrypted.\n";
   152 
   153     status = mime_encode_message(encrypted_msg, false, &encoded_text, false);
   154     ASSERT_EQ(status, PEP_STATUS_OK);
   155     ASSERT_NE(encoded_text, nullptr);
   156 
   157     output_stream << "encrypted:\n\n";
   158     output_stream << encoded_text << "\n";
   159 
   160     message* decoded_msg = nullptr;
   161     status = mime_decode_message(encoded_text, strlen(encoded_text), &decoded_msg, NULL);
   162     ASSERT_EQ(status, PEP_STATUS_OK);
   163     const string string3 = encoded_text;
   164 
   165     unlink("tmp/msg_encrypt_for_self.asc");
   166     ofstream outFile3("tmp/msg_encrypt_for_self.asc");
   167     outFile3.write(string3.c_str(), string3.size());
   168     outFile3.close();
   169 
   170     message* decrypted_msg = nullptr;
   171     stringlist_t* keylist_used = nullptr;
   172 
   173     PEP_rating rating;
   174     PEP_decrypt_flags_t flags;
   175 
   176     flags = 0;
   177     status = decrypt_message(session, encrypted_msg, &decrypted_msg, &keylist_used, &rating, &flags);
   178     ASSERT_NE(decrypted_msg, nullptr);
   179     ASSERT_NE(keylist_used, nullptr);
   180     ASSERT_NE(rating, 0);
   181     ASSERT_TRUE(status == PEP_DECRYPTED && rating == PEP_rating_unreliable);
   182     PEP_comm_type ct = encrypted_msg->from->comm_type;
   183     ASSERT_TRUE(ct == PEP_ct_pEp || ct == PEP_ct_pEp_unconfirmed || ct == PEP_ct_OpenPGP || ct == PEP_ct_OpenPGP_unconfirmed);
   184 
   185     output_stream << "keys used:\n";
   186 
   187     int i = 0;
   188 
   189     for (stringlist_t* kl4 = keylist_used; kl4 && kl4->value; kl4 = kl4->next, i++)
   190     {
   191         if (i == 0) {
   192             ASSERT_STRCASEEQ("",kl4->value);
   193         }
   194         else {
   195             output_stream << "\t " << kl4->value << endl;
   196             ASSERT_STRCASEEQ("4ABE3AAF59AC32CFE4F86500A9411D176FF00E97", kl4->value);
   197             output_stream << "Encrypted for Alice! Yay! It worked!" << endl;
   198         }
   199         ASSERT_LT(i , 2);
   200     }
   201     output_stream << "Encrypted ONLY for Alice! Test passed. Move along. These are not the bugs you are looking for." << endl;
   202 
   203     output_stream << "freeing messages…\n";
   204     free_message(encrypted_msg);
   205     free_message(decrypted_msg);
   206     free_stringlist (keylist_used);
   207     output_stream << "done.\n";
   208 
   209     output_stream << "Now encrypt for self with extra keys." << endl;
   210     stringlist_t* extra_keys = new_stringlist(gabrielle_fpr);
   211     stringlist_add(extra_keys, bella_fpr);
   212     encrypted_msg = NULL;
   213     decrypted_msg = NULL;
   214     keylist_used = NULL;
   215 
   216     output_stream << "calling encrypt_message_for_identity()\n";
   217     status = encrypt_message_for_self(session, alice, outgoing_message, extra_keys, &encrypted_msg, PEP_enc_PGP_MIME, PEP_encrypt_flag_force_unsigned | PEP_encrypt_flag_force_no_attached_key);
   218     output_stream << "encrypt_message() returns " << tl_status_string(status) << '.' << endl;
   219     ASSERT_EQ(status, PEP_STATUS_OK);
   220     ASSERT_NE(encrypted_msg, nullptr);
   221     output_stream << "message encrypted.\n";
   222 
   223     flags = 0;
   224     status = decrypt_message(session, encrypted_msg, &decrypted_msg, &keylist_used, &rating, &flags);
   225     ASSERT_NE(decrypted_msg, nullptr);
   226     ASSERT_NE(keylist_used, nullptr);
   227     ASSERT_NE(rating, 0);
   228     ASSERT_TRUE(status == PEP_DECRYPTED && rating == PEP_rating_unreliable);
   229     ct = encrypted_msg->from->comm_type;
   230     ASSERT_TRUE(ct == PEP_ct_pEp || ct == PEP_ct_pEp_unconfirmed || ct == PEP_ct_OpenPGP || ct == PEP_ct_OpenPGP_unconfirmed);
   231 
   232     output_stream << "keys used:\n";
   233 
   234     for (stringlist_t* incoming_kl = extra_keys; incoming_kl && incoming_kl->value; incoming_kl = incoming_kl->next) {
   235         bool found = false;
   236         output_stream << "Encrypted for: ";
   237         for (stringlist_t* kl4 = keylist_used; kl4 && kl4->value; kl4 = kl4->next, i++) {
   238             if (strcasecmp(incoming_kl->value, kl4->value) == 0) {
   239                 output_stream << "\t " << kl4->value;
   240                 found = true;
   241                 break;
   242             }
   243         }
   244         output_stream << endl;
   245         ASSERT_TRUE(found);
   246     }
   247     output_stream << "Encrypted for all the extra keys!" << endl;
   248 
   249     bool found = false;
   250     for (stringlist_t* kl4 = keylist_used; kl4 && kl4->value; kl4 = kl4->next)
   251     {
   252         if (strcasecmp(alice_fpr, kl4->value) == 0) {
   253             found = true;
   254             output_stream << "Encrypted also for Alice! Yay!" << endl;
   255             break;
   256         }
   257     }
   258     ASSERT_TRUE(found);
   259 
   260     free_message(encrypted_msg);
   261     encrypted_msg = NULL;
   262     free_message(decrypted_msg);
   263     decrypted_msg = NULL;
   264     free_stringlist(keylist_used);
   265     keylist_used = NULL;
   266 
   267     output_stream << "Now add a bad fpr." << endl;
   268 
   269     stringlist_add(extra_keys, nobody_fpr);
   270 
   271     output_stream << "calling encrypt_message_for_identity()\n";
   272     status = encrypt_message_for_self(session, alice, outgoing_message, extra_keys, &encrypted_msg, PEP_enc_PGP_MIME, PEP_encrypt_flag_force_unsigned | PEP_encrypt_flag_force_no_attached_key);
   273     output_stream << "encrypt_message() returns " << tl_status_string(status) << '.' << endl;
   274     ASSERT_NE(status, PEP_STATUS_OK);
   275 
   276     free_message(outgoing_message);
   277     outgoing_message = NULL;
   278     free_message(encrypted_msg);
   279     encrypted_msg = NULL;
   280     free_message(decrypted_msg);
   281     decrypted_msg = NULL;
   282     free_stringlist(keylist_used);
   283     keylist_used = NULL;
   284 
   285 
   286     output_stream << "*** Now testing MIME_encrypt_for_self ***" << endl;
   287 
   288     alice = new_identity("pep.test.alice@pep-project.org", NULL, PEP_OWN_USERID, "Alice Test");
   289     bob = new_identity("pep.test.bob@pep-project.org", NULL, "42", "Bob Test");
   290 
   291     output_stream << "Reading in alice_bob_encrypt_test_plaintext_mime.eml..." << endl;
   292 
   293     const string mimetext = slurp("test_mails/alice_bob_encrypt_test_plaintext_mime.eml");
   294 
   295     output_stream << "Text read:" << endl;
   296     output_stream << mimetext.c_str() << endl;
   297     char* encrypted_mimetext = nullptr;
   298 
   299     output_stream << "Calling MIME_encrypt_message_for_self" << endl;
   300     status = MIME_encrypt_message_for_self(session, alice, mimetext.c_str(),
   301                                            mimetext.size(),
   302                                            NULL,
   303                                            &encrypted_mimetext,
   304                                            PEP_enc_PGP_MIME,
   305                                            PEP_encrypt_flag_force_unsigned | PEP_encrypt_flag_force_no_attached_key);
   306 
   307     output_stream << "Encrypted message:" << endl;
   308     output_stream << encrypted_mimetext << endl;
   309 
   310     output_stream << "Calling MIME_decrypt_message" << endl;
   311 
   312     char* decrypted_mimetext = nullptr;
   313     free_stringlist(keylist_used);
   314     keylist_used = nullptr;
   315     PEP_decrypt_flags_t mimeflags;
   316     PEP_rating mimerating;
   317     char* modified_src = NULL;
   318 
   319     mimeflags = 0;
   320     status = MIME_decrypt_message(session,
   321                                   encrypted_mimetext,
   322                                   strlen(encrypted_mimetext),
   323                                   &decrypted_mimetext,
   324                                   &keylist_used,
   325                                   &mimerating,
   326                                   &mimeflags,
   327 				  &modified_src);
   328 
   329     ASSERT_NE(decrypted_mimetext, nullptr);
   330     ASSERT_NE(keylist_used, nullptr);
   331     ASSERT_NE(mimerating, 0);
   332 
   333     ASSERT_TRUE(status == PEP_DECRYPTED && mimerating == PEP_rating_unreliable);
   334 
   335     output_stream << "Decrypted message:" << endl;
   336     output_stream << decrypted_mimetext << endl;
   337 
   338     output_stream << "keys used:\n";
   339 
   340     i = 0;
   341 
   342     for (stringlist_t* kl4 = keylist_used; kl4 && kl4->value; kl4 = kl4->next, i++)
   343     {
   344         if (i == 0) {
   345             ASSERT_STRCASEEQ("",kl4->value);
   346         }
   347         else {
   348             output_stream << "\t " << kl4->value << endl;
   349             ASSERT_STRCASEEQ("4ABE3AAF59AC32CFE4F86500A9411D176FF00E97", kl4->value);
   350             output_stream << "Encrypted for Alice! Yay! It worked!" << endl;
   351         }
   352         ASSERT_LT(i , 2);
   353     }
   354     output_stream << "Encrypted ONLY for Alice! Test passed. Move along. These are not the bugs you are looking for." << endl;
   355 }
   356 
   357 TEST_F(EncryptForIdentityTest, check_encrypt_for_identity_with_URI) {
   358 
   359     // message_api test code
   360 
   361     const string alice_pub_key = slurp("test_keys/pub/pep-test-alice-0x6FF00E97_pub.asc");
   362     const string alice_priv_key = slurp("test_keys/priv/pep-test-alice-0x6FF00E97_priv.asc");
   363     const string gabrielle_pub_key = slurp("test_keys/pub/pep-test-gabrielle-0xE203586C_pub.asc");
   364     const string bella_pub_key = slurp("test_keys/pub/pep.test.bella-0xAF516AAE_pub.asc");
   365 
   366     PEP_STATUS statuspub = import_key(session, alice_pub_key.c_str(), alice_pub_key.length(), NULL);
   367     PEP_STATUS statuspriv = import_key(session, alice_priv_key.c_str(), alice_priv_key.length(), NULL);
   368     ASSERT_EQ(statuspub, PEP_TEST_KEY_IMPORT_SUCCESS);
   369     ASSERT_EQ(statuspriv, PEP_TEST_KEY_IMPORT_SUCCESS);
   370 
   371     statuspub = import_key(session, gabrielle_pub_key.c_str(), gabrielle_pub_key.length(), NULL);
   372     ASSERT_EQ(statuspub, PEP_TEST_KEY_IMPORT_SUCCESS);
   373     statuspub = import_key(session, bella_pub_key.c_str(), bella_pub_key.length(), NULL);
   374     ASSERT_EQ(statuspub, PEP_TEST_KEY_IMPORT_SUCCESS);
   375 
   376     const char* alice_fpr = "4ABE3AAF59AC32CFE4F86500A9411D176FF00E97";
   377     const char* gabrielle_fpr = "906C9B8349954E82C5623C3C8C541BD4E203586C";
   378     const char* bella_fpr = "5631BF1357326A02AA470EEEB815EF7FA4516AAE";
   379     const char* nobody_fpr = "1111111111111111111111111111111111111111";
   380 
   381     output_stream << "creating message…\n";
   382     pEp_identity* alice = new_identity("payto://BIC/SYSTEMA", alice_fpr, PEP_OWN_USERID, "Alice Test");
   383     pEp_identity* bob = new_identity("payto://BIC/SYSTEMB", NULL, "42", "Bob Test");
   384 
   385     alice->me = true;
   386 
   387     PEP_STATUS mystatus = set_own_key(session, alice, alice_fpr);
   388     ASSERT_EQ(mystatus, PEP_STATUS_OK);
   389 
   390     identity_list* to_list = new_identity_list(bob); // to bob
   391     message* outgoing_message = new_message(PEP_dir_outgoing);
   392     ASSERT_NE(outgoing_message, nullptr);
   393     outgoing_message->from = alice;
   394     outgoing_message->to = to_list;
   395     outgoing_message->shortmsg = strdup("Greetings, humans!");
   396     outgoing_message->longmsg = strdup("This is a test of the emergency message system. This is only a test. BEEP.");
   397     outgoing_message->attachments = new_bloblist(NULL, 0, "application/octet-stream", NULL);
   398     output_stream << "message created.\n";
   399 
   400     char* encoded_text = nullptr;
   401     PEP_STATUS status = mime_encode_message(outgoing_message, false, &encoded_text, false);
   402     ASSERT_EQ(status, PEP_STATUS_OK);
   403     ASSERT_NE(encoded_text, nullptr);
   404 
   405     output_stream << "decrypted:\n\n";
   406     output_stream << encoded_text << "\n";
   407 
   408     free(encoded_text);
   409 
   410     message* encrypted_msg = nullptr;
   411     output_stream << "calling encrypt_message_for_identity()\n";
   412     status = encrypt_message_for_self(session, alice, outgoing_message, NULL, &encrypted_msg, PEP_enc_PGP_MIME, PEP_encrypt_flag_force_unsigned | PEP_encrypt_flag_force_no_attached_key);
   413     output_stream << "encrypt_message() returns " << tl_status_string(status) << '.' << endl;
   414     ASSERT_EQ(status, PEP_STATUS_OK);
   415     ASSERT_NE(encrypted_msg, nullptr);
   416     output_stream << "message encrypted.\n";
   417 
   418     status = mime_encode_message(encrypted_msg, false, &encoded_text, false);
   419     ASSERT_EQ(status, PEP_STATUS_OK);
   420     ASSERT_NE(encoded_text, nullptr);
   421 
   422     output_stream << "encrypted:\n\n";
   423     output_stream << encoded_text << "\n";
   424 
   425     message* decoded_msg = nullptr;
   426     status = mime_decode_message(encoded_text, strlen(encoded_text), &decoded_msg, NULL);
   427     ASSERT_EQ(status, PEP_STATUS_OK);
   428     const string string3 = encoded_text;
   429 
   430     unlink("tmp/msg_encrypt_for_self.asc");
   431     ofstream outFile3("tmp/msg_encrypt_for_self.asc");
   432     outFile3.write(string3.c_str(), string3.size());
   433     outFile3.close();
   434 
   435     message* decrypted_msg = nullptr;
   436     stringlist_t* keylist_used = nullptr;
   437 
   438     PEP_rating rating;
   439     PEP_decrypt_flags_t flags;
   440 
   441     flags = 0;
   442     status = decrypt_message(session, encrypted_msg, &decrypted_msg, &keylist_used, &rating, &flags);
   443     ASSERT_NE(decrypted_msg, nullptr);
   444     ASSERT_NE(keylist_used, nullptr);
   445     ASSERT_NE(rating, 0);
   446     ASSERT_TRUE(status == PEP_DECRYPTED && rating == PEP_rating_unreliable);
   447     PEP_comm_type ct = encrypted_msg->from->comm_type;
   448     ASSERT_TRUE(ct == PEP_ct_pEp || ct == PEP_ct_pEp_unconfirmed || ct == PEP_ct_OpenPGP || ct == PEP_ct_OpenPGP_unconfirmed);
   449 
   450     output_stream << "keys used:\n";
   451 
   452     int i = 0;
   453 
   454     for (stringlist_t* kl4 = keylist_used; kl4 && kl4->value; kl4 = kl4->next, i++)
   455     {
   456         if (i == 0) {
   457             ASSERT_STRCASEEQ("",kl4->value);
   458         }
   459         else {
   460             output_stream << "\t " << kl4->value << endl;
   461             ASSERT_STRCASEEQ("4ABE3AAF59AC32CFE4F86500A9411D176FF00E97", kl4->value);
   462             output_stream << "Encrypted for Alice! Yay! It worked!" << endl;
   463         }
   464         ASSERT_LT(i , 2);
   465     }
   466     output_stream << "Encrypted ONLY for Alice! Test passed. Move along. These are not the bugs you are looking for." << endl;
   467 
   468     output_stream << "freeing messages…\n";
   469     free_message(encrypted_msg);
   470     free_message(decrypted_msg);
   471     free_stringlist (keylist_used);
   472     output_stream << "done.\n";
   473 
   474     output_stream << "Now encrypt for self with extra keys." << endl;
   475     stringlist_t* extra_keys = new_stringlist(gabrielle_fpr);
   476     stringlist_add(extra_keys, bella_fpr);
   477     encrypted_msg = NULL;
   478     decrypted_msg = NULL;
   479     keylist_used = NULL;
   480 
   481     output_stream << "calling encrypt_message_for_identity()\n";
   482     status = encrypt_message_for_self(session, alice, outgoing_message, extra_keys, &encrypted_msg, PEP_enc_PGP_MIME, PEP_encrypt_flag_force_unsigned | PEP_encrypt_flag_force_no_attached_key);
   483     output_stream << "encrypt_message() returns " << tl_status_string(status) << '.' << endl;
   484     ASSERT_EQ(status, PEP_STATUS_OK);
   485     ASSERT_NE(encrypted_msg, nullptr);
   486     output_stream << "message encrypted.\n";
   487 
   488     flags = 0;
   489     status = decrypt_message(session, encrypted_msg, &decrypted_msg, &keylist_used, &rating, &flags);
   490     ASSERT_NE(decrypted_msg, nullptr);
   491     ASSERT_NE(keylist_used, nullptr);
   492     ASSERT_NE(rating, 0);
   493     ASSERT_TRUE(status == PEP_DECRYPTED && rating == PEP_rating_unreliable);
   494     ct = encrypted_msg->from->comm_type;
   495     ASSERT_TRUE(ct == PEP_ct_pEp || ct == PEP_ct_pEp_unconfirmed || ct == PEP_ct_OpenPGP || ct == PEP_ct_OpenPGP_unconfirmed);
   496 
   497     output_stream << "keys used:\n";
   498 
   499     for (stringlist_t* incoming_kl = extra_keys; incoming_kl && incoming_kl->value; incoming_kl = incoming_kl->next) {
   500         bool found = false;
   501         output_stream << "Encrypted for: ";
   502         for (stringlist_t* kl4 = keylist_used; kl4 && kl4->value; kl4 = kl4->next, i++) {
   503             if (strcasecmp(incoming_kl->value, kl4->value) == 0) {
   504                 output_stream << "\t " << kl4->value;
   505                 found = true;
   506                 break;
   507             }
   508         }
   509         output_stream << endl;
   510         ASSERT_TRUE(found);
   511     }
   512     output_stream << "Encrypted for all the extra keys!" << endl;
   513 
   514     bool found = false;
   515     for (stringlist_t* kl4 = keylist_used; kl4 && kl4->value; kl4 = kl4->next)
   516     {
   517         if (strcasecmp(alice_fpr, kl4->value) == 0) {
   518             found = true;
   519             output_stream << "Encrypted also for Alice! Yay!" << endl;
   520             break;
   521         }
   522     }
   523     ASSERT_TRUE(found);
   524 
   525     free_message(encrypted_msg);
   526     encrypted_msg = NULL;
   527     free_message(decrypted_msg);
   528     decrypted_msg = NULL;
   529     free_stringlist(keylist_used);
   530     keylist_used = NULL;
   531 
   532     output_stream << "Now add a bad fpr." << endl;
   533 
   534     stringlist_add(extra_keys, nobody_fpr);
   535 
   536     output_stream << "calling encrypt_message_for_identity()\n";
   537     status = encrypt_message_for_self(session, alice, outgoing_message, extra_keys, &encrypted_msg, PEP_enc_PGP_MIME, PEP_encrypt_flag_force_unsigned | PEP_encrypt_flag_force_no_attached_key);
   538     output_stream << "encrypt_message() returns " << tl_status_string(status) << '.' << endl;
   539     ASSERT_NE(status, PEP_STATUS_OK);
   540 
   541     free_message(outgoing_message);
   542     outgoing_message = NULL;
   543     free_message(encrypted_msg);
   544     encrypted_msg = NULL;
   545     free_message(decrypted_msg);
   546     decrypted_msg = NULL;
   547     free_stringlist(keylist_used);
   548     keylist_used = NULL;
   549 
   550 
   551     output_stream << "*** Now testing MIME_encrypt_for_self ***" << endl;
   552 
   553     alice = new_identity("payto://BIC/SYSTEMA", NULL, PEP_OWN_USERID, "Alice Test");
   554     bob = new_identity("payto://BIC/SYSTEMB", NULL, "42", "Bob Test");
   555 
   556     output_stream << "Reading in alice_bob_encrypt_test_plaintext_mime.eml..." << endl;
   557 
   558     const string mimetext = slurp("test_mails/alice_bob_encrypt_test_plaintext_mime.eml");
   559 
   560     output_stream << "Text read:" << endl;
   561     output_stream << mimetext.c_str() << endl;
   562     char* encrypted_mimetext = nullptr;
   563 
   564     output_stream << "Calling MIME_encrypt_message_for_self" << endl;
   565     status = MIME_encrypt_message_for_self(session, alice, mimetext.c_str(),
   566                                            mimetext.size(),
   567                                            NULL,
   568                                            &encrypted_mimetext,
   569                                            PEP_enc_PGP_MIME,
   570                                            PEP_encrypt_flag_force_unsigned | PEP_encrypt_flag_force_no_attached_key);
   571 
   572     output_stream << "Encrypted message:" << endl;
   573     output_stream << encrypted_mimetext << endl;
   574 
   575     output_stream << "Calling MIME_decrypt_message" << endl;
   576 
   577     char* decrypted_mimetext = nullptr;
   578     free_stringlist(keylist_used);
   579     keylist_used = nullptr;
   580     PEP_decrypt_flags_t mimeflags;
   581     PEP_rating mimerating;
   582     char* modified_src = NULL;
   583 
   584     mimeflags = 0;
   585     status = MIME_decrypt_message(session,
   586                                   encrypted_mimetext,
   587                                   strlen(encrypted_mimetext),
   588                                   &decrypted_mimetext,
   589                                   &keylist_used,
   590                                   &mimerating,
   591                                   &mimeflags,
   592 				  &modified_src);
   593 
   594     ASSERT_NE(decrypted_mimetext, nullptr);
   595     ASSERT_NE(keylist_used, nullptr);
   596     ASSERT_NE(mimerating, 0);
   597 
   598     ASSERT_TRUE(status == PEP_DECRYPTED && mimerating == PEP_rating_unreliable);
   599 
   600     output_stream << "Decrypted message:" << endl;
   601     output_stream << decrypted_mimetext << endl;
   602 
   603     output_stream << "keys used:\n";
   604 
   605     i = 0;
   606 
   607     for (stringlist_t* kl4 = keylist_used; kl4 && kl4->value; kl4 = kl4->next, i++)
   608     {
   609         if (i == 0) {
   610             ASSERT_STRCASEEQ("",kl4->value);
   611         }
   612         else {
   613             output_stream << "\t " << kl4->value << endl;
   614             ASSERT_STRCASEEQ("4ABE3AAF59AC32CFE4F86500A9411D176FF00E97", kl4->value);
   615             output_stream << "Encrypted for Alice! Yay! It worked!" << endl;
   616         }
   617         ASSERT_LT(i , 2);
   618     }
   619     output_stream << "Encrypted ONLY for Alice! Test passed. Move along. These are not the bugs you are looking for." << endl;
   620 }