test/src/KeyResetMessageTest.cc
author Krista 'DarthMama' Bennett <krista@pep.foundation>
Sat, 18 Jan 2020 23:15:45 +0100
branchsync
changeset 4352 69ac22b40ac7
parent 4350 fe4f0b28aa48
child 4411 67187599f606
permissions -rw-r--r--
now ignoring own key resets in non-grouped keys
     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 <vector>
     7 #include <unordered_map>
     8 #include <assert.h>
     9 #include <iostream>
    10 #include <fstream>
    11 
    12 #include "pEpEngine.h"
    13 #include "pEp_internal.h"
    14 #include "mime.h"
    15 #include "keymanagement.h"
    16 #include "key_reset.h"
    17 
    18 #include "test_util.h"
    19 #include "TestConstants.h"
    20 
    21 
    22 #include "Engine.h"
    23 
    24 #include <gtest/gtest.h>
    25 
    26 PEP_STATUS KRMT_message_send_callback(message* msg);
    27 
    28 static void* KRMT_fake_this;
    29 
    30 //The fixture for KeyResetMessageTest
    31 class KeyResetMessageTest : public ::testing::Test {
    32     public:
    33         Engine* engine;
    34         PEP_SESSION session;
    35 
    36         vector<message*> m_queue;
    37 
    38     protected:
    39 
    40         const char* alice_fpr = "4ABE3AAF59AC32CFE4F86500A9411D176FF00E97";
    41         const char* bob_fpr = "BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39";
    42 
    43         const char* alice_receive_reset_fpr = "3340B36010854C3F52E97A30FA7B63E0202523DA";
    44 
    45         const string alice_user_id = PEP_OWN_USERID;
    46         const string bob_user_id = "BobId";
    47         const string carol_user_id = "carolId";
    48         const string dave_user_id = "DaveId";
    49         const string erin_user_id = "ErinErinErin";
    50         const string fenris_user_id = "BadWolf";
    51 
    52         // You can remove any or all of the following functions if its body
    53         // is empty.
    54         KeyResetMessageTest() {
    55             // You can do set-up work for each test here.
    56             test_suite_name = ::testing::UnitTest::GetInstance()->current_test_info()->GTEST_SUITE_SYM();
    57             test_name = ::testing::UnitTest::GetInstance()->current_test_info()->name();
    58             test_path = get_main_test_home_dir() + "/" + test_suite_name + "/" + test_name;
    59         }
    60 
    61         ~KeyResetMessageTest() override {
    62             // You can do clean-up work that doesn't throw exceptions here.
    63         }
    64 
    65         // If the constructor and destructor are not enough for setting up
    66         // and cleaning up each test, you can define the following methods:
    67 
    68         void SetUp() override {
    69             // Code here will be called immediately after the constructor (right
    70             // before each test).
    71 
    72             KRMT_fake_this = (void*)this;
    73             // Leave this empty if there are no files to copy to the home directory path
    74             std::vector<std::pair<std::string, std::string>> init_files = std::vector<std::pair<std::string, std::string>>();
    75 
    76             // Get a new test Engine.
    77             engine = new Engine(test_path);
    78             ASSERT_NE(engine, nullptr);
    79 
    80             // Ok, let's initialize test directories etc.
    81             engine->prep(&KRMT_message_send_callback, NULL, init_files);
    82 
    83             // Ok, try to start this bugger.
    84             engine->start();
    85             ASSERT_NE(engine->session, nullptr);
    86             session = engine->session;
    87 
    88             // Engine is up. Keep on truckin'
    89             m_queue.clear();
    90         }
    91 
    92         void TearDown() override {
    93             // Code here will be called immediately after each test (right
    94             // before the destructor).
    95             KRMT_fake_this = NULL;
    96             engine->shut_down();
    97             delete engine;
    98             engine = NULL;
    99             session = NULL;
   100         }
   101 
   102         void send_setup() {
   103             // Setup own identity
   104             PEP_STATUS status = read_file_and_import_key(session,
   105                         "test_keys/pub/pep-test-alice-0x6FF00E97_pub.asc");
   106             assert(status == PEP_KEY_IMPORTED);
   107             status = set_up_ident_from_scratch(session,
   108                         "test_keys/priv/pep-test-alice-0x6FF00E97_priv.asc",
   109                         "pep.test.alice@pep-project.org", alice_fpr,
   110                         alice_user_id.c_str(), "Alice in Wonderland", NULL, true
   111                     );
   112             ASSERT_EQ(status, PEP_STATUS_OK);
   113 
   114             status = set_up_ident_from_scratch(session,
   115                         "test_keys/pub/pep-test-bob-0xC9C2EE39_pub.asc",
   116                         "pep.test.bob@pep-project.org", NULL, bob_user_id.c_str(), "Bob's Burgers",
   117                         NULL, false
   118                     );
   119             ASSERT_EQ(status, PEP_STATUS_OK);
   120 
   121             status = set_up_ident_from_scratch(session,
   122                         "test_keys/pub/pep-test-carol-0x42A85A42_pub.asc",
   123                         "pep-test-carol@pep-project.org", NULL, carol_user_id.c_str(), "Carol Burnett",
   124                         NULL, false
   125                     );
   126             ASSERT_EQ(status, PEP_STATUS_OK);
   127 
   128             status = set_up_ident_from_scratch(session,
   129                         "test_keys/pub/pep-test-dave-0xBB5BCCF6_pub.asc",
   130                         "pep-test-dave@pep-project.org", NULL, dave_user_id.c_str(),
   131                         "David Hasselhoff (Germans Love Me)", NULL, false
   132                     );
   133             ASSERT_EQ(status, PEP_STATUS_OK);
   134 
   135             status = set_up_ident_from_scratch(session,
   136                         "test_keys/pub/pep-test-erin-0x9F8D7CBA_pub.asc",
   137                         "pep-test-erin@pep-project.org", NULL, erin_user_id.c_str(),
   138                         "Éirinn go Brách", NULL, false
   139                     );
   140             ASSERT_EQ(status, PEP_STATUS_OK);
   141 
   142             status = set_up_ident_from_scratch(session,
   143                         "test_keys/pub/pep.test.fenris-0x4F3D2900_pub.asc",
   144                         "pep.test.fenris@thisstilldoesntwork.lu", NULL, fenris_user_id.c_str(),
   145                         "Fenris Leto Hawke", NULL, false
   146                     );
   147             ASSERT_EQ(status, PEP_STATUS_OK);
   148         }
   149 
   150         void receive_setup() {
   151             PEP_STATUS status = read_file_and_import_key(session,
   152                         "test_keys/pub/pep-test-bob-0xC9C2EE39_pub.asc");
   153             assert(status == PEP_KEY_IMPORTED);
   154             status = set_up_ident_from_scratch(session,
   155                         "test_keys/priv/pep-test-bob-0xC9C2EE39_priv.asc",
   156                         "pep.test.bob@pep-project.org", bob_fpr,
   157                         bob_user_id.c_str(), "Robert Redford", NULL, true
   158                     );
   159             ASSERT_EQ(status, PEP_STATUS_OK);
   160 
   161             status = set_up_ident_from_scratch(session,
   162                         "test_keys/pub/pep-test-alice-0x6FF00E97_pub.asc",
   163                         "pep.test.alice@pep-project.org", NULL, alice_user_id.c_str(), "Alice in Wonderland",
   164                         NULL, false
   165                     );
   166             ASSERT_EQ(status, PEP_STATUS_OK);
   167         }
   168 
   169         void create_msg_for_revoked_key() {
   170             PEP_STATUS status = set_up_ident_from_scratch(session,
   171                         "test_keys/pub/pep-test-gabrielle-0xE203586C_pub.asc",
   172                         "pep-test-gabrielle@pep-project.org", NULL, PEP_OWN_USERID,
   173                         "Gabi", NULL, false
   174                     );
   175             ASSERT_EQ(status, PEP_STATUS_OK);
   176             status = set_up_ident_from_scratch(session,
   177                         "test_keys/priv/pep-test-gabrielle-0xE203586C_priv.asc",
   178                         "pep-test-gabrielle@pep-project.org", NULL, PEP_OWN_USERID,
   179                         "Gabi", NULL, false
   180                     );
   181             ASSERT_EQ(status, PEP_STATUS_OK);
   182 
   183             status = set_up_ident_from_scratch(session,
   184                         "test_keys/pub/pep-test-alice-0x6FF00E97_pub.asc",
   185                         "pep.test.alice@pep-project.org", NULL, "AliceOther", "Alice in Wonderland",
   186                         NULL, false
   187                     );
   188 
   189             pEp_identity* from_ident = new_identity("pep-test-gabrielle@pep-project.org", NULL, PEP_OWN_USERID, NULL);
   190             status = myself(session, from_ident);
   191             ASSERT_EQ(status , PEP_STATUS_OK);
   192             ASSERT_NE(from_ident->fpr, nullptr);
   193             ASSERT_STRCASEEQ(from_ident->fpr, "906C9B8349954E82C5623C3C8C541BD4E203586C");
   194             ASSERT_TRUE(from_ident->me);
   195 
   196             // "send" some messages to update the social graph entries
   197             identity_list* send_idents =
   198                 new_identity_list(
   199                     new_identity("pep.test.alice@pep-project.org", NULL, "AliceOther", NULL));
   200             status = update_identity(session, send_idents->ident);
   201             ASSERT_EQ(status , PEP_STATUS_OK);
   202             status = set_as_pEp_user(session, send_idents->ident);
   203 
   204             message* outgoing_msg = new_message(PEP_dir_outgoing);
   205             ASSERT_NE(outgoing_msg, nullptr);
   206             outgoing_msg->from = from_ident;
   207             outgoing_msg->to = send_idents;
   208             outgoing_msg->shortmsg = strdup("Well isn't THIS a useless message...");
   209             outgoing_msg->longmsg = strdup("Hi Mom...\n");
   210             outgoing_msg->attachments = new_bloblist(NULL, 0, "application/octet-stream", NULL);
   211             output_stream << "Message created.\n\n";
   212             output_stream << "Encrypting message as MIME multipart…\n";
   213             message* enc_outgoing_msg = nullptr;
   214             output_stream << "Calling encrypt_message()\n";
   215             status = encrypt_message(session, outgoing_msg, NULL, &enc_outgoing_msg, PEP_enc_PGP_MIME, 0);
   216             ASSERT_EQ(status , PEP_STATUS_OK);
   217             ASSERT_NE(enc_outgoing_msg, nullptr);
   218             output_stream << "Message encrypted.\n";
   219             char* outstring = NULL;
   220             mime_encode_message(enc_outgoing_msg, false, &outstring);
   221             output_stream << outstring << endl;
   222             free_message(enc_outgoing_msg);
   223             free(outstring);
   224         }
   225 
   226     private:
   227         const char* test_suite_name;
   228         const char* test_name;
   229         string test_path;
   230         // Objects declared here can be used by all tests in the KeyResetMessageTest suite.
   231 
   232 };
   233 
   234 PEP_STATUS KRMT_message_send_callback(message* msg) {
   235     ((KeyResetMessageTest*)KRMT_fake_this)->m_queue.push_back(msg);
   236     return PEP_STATUS_OK;
   237 }
   238 
   239 
   240 TEST_F(KeyResetMessageTest, check_reset_key_and_notify) {
   241     send_setup();
   242 
   243     pEp_identity* from_ident = new_identity("pep.test.alice@pep-project.org", NULL, PEP_OWN_USERID, NULL);
   244     PEP_STATUS status = myself(session, from_ident);
   245     ASSERT_EQ(status , PEP_STATUS_OK);
   246     ASSERT_NE(from_ident->fpr, nullptr);
   247     ASSERT_STRCASEEQ(from_ident->fpr, alice_fpr);
   248     ASSERT_TRUE(from_ident->me);
   249 
   250     // "send" some messages to update the social graph entries
   251     identity_list* send_idents =
   252         new_identity_list(
   253             new_identity("pep.test.bob@pep-project.org",
   254                          NULL, bob_user_id.c_str(), "Bob's Burgers"));
   255 
   256     identity_list_add(send_idents, new_identity("pep-test-carol@pep-project.org", NULL, NULL, NULL));
   257     identity_list_add(send_idents, new_identity("pep-test-dave@pep-project.org", NULL, NULL, NULL));
   258     identity_list_add(send_idents, new_identity("pep-test-erin@pep-project.org", NULL, NULL, NULL));
   259     identity_list_add(send_idents, new_identity("pep.test.fenris@thisstilldoesntwork.lu", NULL, NULL, NULL));
   260 
   261     identity_list* curr_ident;
   262 
   263     for (curr_ident = send_idents; curr_ident && curr_ident->ident; curr_ident = curr_ident->next) {
   264         status = update_identity(session, curr_ident->ident);
   265         if (strcmp(curr_ident->ident->user_id, bob_user_id.c_str()) == 0)
   266             continue;
   267 
   268         status = set_as_pEp_user(session, curr_ident->ident);
   269         ASSERT_EQ(status , PEP_STATUS_OK);
   270     }
   271 
   272     output_stream << "Creating outgoing message to update DB" << endl;
   273     message* outgoing_msg = new_message(PEP_dir_outgoing);
   274     ASSERT_NE(outgoing_msg, nullptr);
   275     outgoing_msg->from = from_ident;
   276     outgoing_msg->to = send_idents;
   277     outgoing_msg->shortmsg = strdup("Well isn't THIS a useless message...");
   278     outgoing_msg->longmsg = strdup("Hi Mom...\n");
   279     // outgoing_msg->attachments = new_bloblist(NULL, 0, "application/octet-stream", NULL);
   280     // that's illegal - VB.
   281     output_stream << "Message created.\n\n";
   282     output_stream << "Encrypting message as MIME multipart…\n";
   283     message* enc_outgoing_msg = nullptr;
   284     output_stream << "Calling encrypt_message()\n";
   285     status = encrypt_message(session, outgoing_msg, NULL, &enc_outgoing_msg, PEP_enc_PGP_MIME, 0);
   286     ASSERT_EQ(status , PEP_STATUS_OK);
   287     ASSERT_NE(enc_outgoing_msg, nullptr);
   288     output_stream << "Message encrypted.\n";
   289 
   290     // If this all worked, we should have a list of recent guys in our DB which, when we reset Alice's
   291     // key, will get sent some nice key reset messages.
   292     // But... we need to have one look like an older message. So. Time to mess with the DB.
   293     // Dave is our victim. Because I have a friend called Dave, who is actually a nice dude, but it amuses me.
   294     // (Note: said friend is NOT David Hasselhoff. To my knowledge. Hi Dave! (Addendum: Dave confirms he is
   295     // not Hasselhoff. But he wishes he were, sort of.))
   296     //
   297     // update identity
   298     //      set timestamp = 661008730
   299     //      where address = "pep-test-dave@pep-project.org"
   300     int int_result = sqlite3_exec(
   301         session->db,
   302         "update identity "
   303         "   set timestamp = 661008730 "
   304         "   where address = 'pep-test-dave@pep-project.org' ;",
   305         NULL,
   306         NULL,
   307         NULL
   308     );
   309     ASSERT_EQ(int_result , SQLITE_OK);
   310 
   311     status = key_reset(session, alice_fpr, from_ident);
   312     ASSERT_EQ(status , PEP_STATUS_OK);
   313     ASSERT_GT(m_queue.size(), 0);
   314     status = myself(session, from_ident);
   315     string new_fpr = from_ident->fpr;
   316     ASSERT_STRNE(alice_fpr, new_fpr.c_str());
   317 
   318     unordered_map<string, bool> hashmap;
   319     hashmap[alice_user_id] = false;
   320     hashmap[bob_user_id] = false;
   321     hashmap[carol_user_id] = false;
   322     hashmap[dave_user_id] = false;
   323     hashmap[erin_user_id] = false;
   324     hashmap[fenris_user_id] = false;
   325 
   326     // Number of messages we SHOULD be sending.
   327     ASSERT_EQ(m_queue.size(), 4);
   328 
   329     for (vector<message*>::iterator it = m_queue.begin(); it != m_queue.end(); it++) {
   330         message* curr_sent_msg = *it;
   331         ASSERT_NE(curr_sent_msg, nullptr);
   332         ASSERT_NE(curr_sent_msg->to, nullptr);
   333         ASSERT_NE(curr_sent_msg->to->ident, nullptr);
   334         ASSERT_EQ(curr_sent_msg->to->next, nullptr);
   335         pEp_identity* to = curr_sent_msg->to->ident;
   336         ASSERT_NE(to, nullptr);
   337         ASSERT_NE(to->user_id, nullptr);
   338 
   339         unordered_map<string, bool>::iterator jt = hashmap.find(to->user_id);
   340 
   341         ASSERT_NE(jt, hashmap.end());
   342         hashmap[jt->first] = true;
   343 
   344         // Uncomment to regenerate received message - remember to update
   345         // alice_receive_reset_fpr
   346         if (false) {
   347             if (strcmp(curr_sent_msg->to->ident->user_id, bob_user_id.c_str()) == 0) {
   348                 ofstream outfile;
   349                 outfile.open("test_files/398_reset_from_alice_to_bob.eml");
   350                 char* bob_msg = NULL;
   351                 mime_encode_message(curr_sent_msg, false, &bob_msg);
   352                 outfile << bob_msg;
   353                 outfile.close();
   354             }
   355             else if (strcmp(curr_sent_msg->to->ident->user_id, fenris_user_id.c_str()) == 0) {
   356                 ofstream outfile;
   357                 outfile.open("test_files/398_reset_from_alice_to_fenris.eml");
   358                 char* fenris_msg = NULL;
   359                 mime_encode_message(curr_sent_msg, false, &fenris_msg);
   360                 outfile << fenris_msg;
   361                 outfile.close();
   362             }
   363         }
   364     }
   365 
   366     // MESSAGE LIST NOW INVALID.
   367     m_queue.clear();
   368 
   369     // Make sure we have messages only to desired recips
   370     ASSERT_FALSE(hashmap[alice_user_id]);
   371     ASSERT_TRUE(hashmap[bob_user_id]);
   372     ASSERT_TRUE(hashmap[carol_user_id]);
   373     ASSERT_FALSE(hashmap[dave_user_id]);
   374     ASSERT_TRUE(hashmap[erin_user_id]);
   375     ASSERT_TRUE(hashmap[fenris_user_id]);
   376     cout << "HEY! reset_fpr is " << new_fpr << endl;
   377 }
   378 
   379 
   380 TEST_F(KeyResetMessageTest, check_non_reset_receive_revoked) {
   381     receive_setup();
   382     pEp_identity* alice_ident = new_identity("pep.test.alice@pep-project.org", NULL,
   383                                             alice_user_id.c_str(), NULL);
   384 
   385     PEP_STATUS status = update_identity(session, alice_ident);
   386     ASSERT_EQ(status , PEP_STATUS_OK);
   387     ASSERT_STREQ(alice_fpr, alice_ident->fpr);
   388 
   389     string received_mail = slurp("test_files/398_reset_from_alice_to_bob.eml");
   390     char* decrypted_msg = NULL;
   391     char* modified_src = NULL;
   392     stringlist_t* keylist = NULL;
   393     PEP_rating rating;
   394     PEP_decrypt_flags_t flags = 0;
   395     status = MIME_decrypt_message(session, received_mail.c_str(), received_mail.size(),
   396                                   &decrypted_msg, &keylist, &rating, &flags, &modified_src);
   397 
   398     ASSERT_EQ(status , PEP_STATUS_OK);
   399     ASSERT_NE(keylist, nullptr);
   400     if (keylist) // there's a test option to continue when asserts fail, so...
   401         ASSERT_STREQ(keylist->value,alice_receive_reset_fpr);
   402 
   403     status = update_identity(session, alice_ident);
   404     ASSERT_NE(alice_ident->fpr, nullptr);
   405     ASSERT_STREQ(alice_receive_reset_fpr,alice_ident->fpr);
   406 
   407     keylist = NULL;
   408 
   409     free(keylist);
   410 }
   411 
   412 TEST_F(KeyResetMessageTest, check_reset_receive_revoked) {
   413     PEP_STATUS status = set_up_ident_from_scratch(session,
   414                 "test_keys/pub/pep.test.fenris-0x4F3D2900_pub.asc",
   415                 "pep.test.fenris@thisstilldoesntwork.lu", NULL, fenris_user_id.c_str(),
   416                 "Fenris Leto Hawke", NULL, false
   417             );
   418     ASSERT_EQ(status, PEP_STATUS_OK);
   419     status = set_up_ident_from_scratch(session,
   420                 "test_keys/priv/pep.test.fenris-0x4F3D2900_priv.asc",
   421                 "pep.test.fenris@thisstilldoesntwork.lu", NULL, fenris_user_id.c_str(),
   422                 "Fenris Leto Hawke", NULL, true
   423             );
   424     ASSERT_EQ(status, PEP_STATUS_OK);
   425 
   426     status = set_up_ident_from_scratch(session,
   427                 "test_keys/pub/pep-test-alice-0x6FF00E97_pub.asc",
   428                 "pep.test.alice@pep-project.org", NULL, "ALICE_IS_NOT_OWN_ID", "Alice in Wonderland",
   429                 NULL, false
   430             );
   431     ASSERT_EQ(status, PEP_STATUS_OK);
   432 
   433     pEp_identity* alice_ident = new_identity("pep.test.alice@pep-project.org", NULL,
   434                                             "ALICE_IS_NOT_OWN_ID", "Alice in Wonderland");
   435 
   436     status = update_identity(session, alice_ident);
   437     ASSERT_EQ(status , PEP_STATUS_OK);
   438     ASSERT_STREQ(alice_fpr, alice_ident->fpr);
   439 
   440     string received_mail = slurp("test_files/398_reset_from_alice_to_fenris.eml");
   441     char* decrypted_msg = NULL;
   442     char* modified_src = NULL;
   443     stringlist_t* keylist = NULL;
   444     PEP_rating rating;
   445     PEP_decrypt_flags_t flags = 0;
   446     status = MIME_decrypt_message(session, received_mail.c_str(), received_mail.size(),
   447                                   &decrypted_msg, &keylist, &rating, &flags, &modified_src);
   448 
   449     ASSERT_EQ(status , PEP_STATUS_OK);
   450     ASSERT_NE(keylist, nullptr);
   451     if (keylist) // there's a test option to continue when asserts fail, so...
   452         ASSERT_STREQ(keylist->value, alice_receive_reset_fpr);
   453 
   454     status = update_identity(session, alice_ident);
   455     ASSERT_NE(alice_ident->fpr, nullptr);
   456     ASSERT_STREQ(alice_receive_reset_fpr, alice_ident->fpr);
   457 
   458     keylist = NULL;
   459 
   460     free(keylist);
   461 }
   462 
   463 
   464 TEST_F(KeyResetMessageTest, check_receive_message_to_revoked_key_from_unknown) {
   465     // create_msg_for_revoked_key(); // call to recreate msg
   466     send_setup();
   467     pEp_identity* from_ident = new_identity("pep.test.alice@pep-project.org", NULL, PEP_OWN_USERID, NULL);
   468     PEP_STATUS status = myself(session, from_ident);
   469     ASSERT_EQ(status , PEP_STATUS_OK);
   470     ASSERT_NE(from_ident->fpr, nullptr);
   471     ASSERT_STRCASEEQ(from_ident->fpr, alice_fpr);
   472     ASSERT_TRUE(from_ident->me);
   473 
   474     status = key_reset(session, alice_fpr, from_ident);
   475     ASSERT_EQ(status , PEP_STATUS_OK);
   476     m_queue.clear();
   477 
   478     string received_mail = slurp("test_files/398_gabrielle_to_alice.eml");
   479     char* decrypted_msg = NULL;
   480     char* modified_src = NULL;
   481     stringlist_t* keylist = NULL;
   482     PEP_rating rating;
   483     PEP_decrypt_flags_t flags = 0;
   484     status = MIME_decrypt_message(session, received_mail.c_str(), received_mail.size(),
   485                                   &decrypted_msg, &keylist, &rating, &flags, &modified_src);
   486     ASSERT_EQ(m_queue.size() , 0);
   487     free(decrypted_msg);
   488     free(modified_src);
   489     free_stringlist(keylist);
   490     free_identity(from_ident);
   491 }
   492 
   493 
   494 TEST_F(KeyResetMessageTest, check_receive_message_to_revoked_key_from_contact) {
   495     // create_msg_for_revoked_key(); // call to recreate msg
   496     send_setup();
   497     pEp_identity* from_ident = new_identity("pep.test.alice@pep-project.org", NULL, PEP_OWN_USERID, NULL);
   498     PEP_STATUS status = myself(session, from_ident);
   499     ASSERT_EQ(status , PEP_STATUS_OK);
   500     ASSERT_NE(from_ident->fpr, nullptr);
   501     ASSERT_STRCASEEQ(from_ident->fpr, alice_fpr);
   502     ASSERT_TRUE(from_ident->me);
   503 
   504     // Send Gabrielle a message
   505     identity_list* send_idents = new_identity_list(new_identity("pep-test-gabrielle@pep-project.org", NULL, "Gabi", "Gabi"));
   506     output_stream << "Creating outgoing message to update DB" << endl;
   507     message* outgoing_msg = new_message(PEP_dir_outgoing);
   508     ASSERT_NE(outgoing_msg, nullptr);
   509     outgoing_msg->from = from_ident;
   510     outgoing_msg->to = send_idents;
   511     outgoing_msg->shortmsg = strdup("Well isn't THIS a useless message...");
   512     outgoing_msg->longmsg = strdup("Hi Mom...\n");
   513     outgoing_msg->attachments = new_bloblist(NULL, 0, "application/octet-stream", NULL);
   514     output_stream << "Message created.\n\n";
   515     output_stream << "Encrypting message as MIME multipart…\n";
   516     message* enc_outgoing_msg = nullptr;
   517     output_stream << "Calling encrypt_message()\n";
   518     status = encrypt_message(session, outgoing_msg, NULL, &enc_outgoing_msg, PEP_enc_PGP_MIME, 0);
   519     ASSERT_EQ(status , PEP_UNENCRYPTED);
   520     //
   521     output_stream << "Message created." << endl;
   522 
   523     // Make the update have occurred earlier, so we don't notify her
   524     // (We have no key for her yet anyway!)
   525     int int_result = sqlite3_exec(
   526         session->db,
   527         "update identity "
   528         "   set timestamp = '2018-04-10 16:48:33' "
   529         "   where address = 'pep-test-gabrielle@pep-project.org' ;",
   530         NULL,
   531         NULL,
   532         NULL
   533     );
   534     ASSERT_EQ(int_result , SQLITE_OK);
   535 
   536     // FIXME: longer term we need to fix the test, but the key attached to the message below has expired, so for now, we give her a new key
   537     slurp_and_import_key(session, "test_keys/pub/pep-test-gabrielle-0xE203586C_pub.asc");
   538 
   539     status = key_reset(session, alice_fpr, from_ident);
   540     ASSERT_EQ(status , PEP_STATUS_OK);
   541     ASSERT_EQ(m_queue.size() , 0);
   542     m_queue.clear();
   543 
   544     // Now we get mail from Gabi, who only has our old key AND has become
   545     // a pEp user in the meantime...
   546     string received_mail = slurp("test_files/398_gabrielle_to_alice.eml");
   547     char* decrypted_msg = NULL;
   548     char* modified_src = NULL;
   549     stringlist_t* keylist = NULL;
   550     PEP_rating rating;
   551     PEP_decrypt_flags_t flags = 0;
   552     status = MIME_decrypt_message(session, received_mail.c_str(), received_mail.size(),
   553                                   &decrypted_msg, &keylist, &rating, &flags, &modified_src);
   554 
   555     ASSERT_EQ(m_queue.size() , 1);
   556     vector<message*>::iterator it = m_queue.begin();
   557     message* reset_msg = *it;
   558     ASSERT_NE(reset_msg, nullptr);
   559     ASSERT_NE(reset_msg->from, nullptr);
   560     ASSERT_NE(reset_msg->to, nullptr);
   561     ASSERT_NE(reset_msg->to->ident, nullptr);
   562     ASSERT_STREQ(reset_msg->to->ident->address, "pep-test-gabrielle@pep-project.org");
   563     ASSERT_STREQ(reset_msg->to->ident->fpr, "906C9B8349954E82C5623C3C8C541BD4E203586C");
   564     ASSERT_STRNE(reset_msg->from->fpr, alice_fpr);
   565     ASSERT_NE(keylist, nullptr);
   566     ASSERT_NE(keylist->value, nullptr);
   567     ASSERT_STRNE(keylist->value, alice_fpr);
   568     ASSERT_NE(keylist->next, nullptr);
   569     if (strcmp(keylist->next->value, "906C9B8349954E82C5623C3C8C541BD4E203586C") != 0) {
   570         ASSERT_NE(keylist->next->next, nullptr);
   571         ASSERT_STREQ(keylist->next->value, "906C9B8349954E82C5623C3C8C541BD4E203586C");
   572     }
   573 }
   574 
   575 
   576 TEST_F(KeyResetMessageTest, check_multiple_resets_single_key) {
   577     send_setup();
   578 
   579     pEp_identity* from_ident = new_identity("pep.test.alice@pep-project.org", NULL, PEP_OWN_USERID, NULL);
   580     PEP_STATUS status = myself(session, from_ident);
   581     ASSERT_EQ(status , PEP_STATUS_OK);
   582     ASSERT_NE(from_ident->fpr, nullptr);
   583     ASSERT_STRCASEEQ(from_ident->fpr, alice_fpr);
   584     ASSERT_TRUE(from_ident->me);
   585 
   586     status = key_reset(session, NULL, NULL);
   587     ASSERT_EQ(status , PEP_STATUS_OK);
   588 
   589     status = key_reset(session, NULL, NULL);
   590     ASSERT_EQ(status , PEP_STATUS_OK);
   591 
   592     status = myself(session, from_ident);
   593     ASSERT_EQ(status , PEP_STATUS_OK);
   594     ASSERT_TRUE(from_ident->fpr != NULL && from_ident->fpr[0] != 0);
   595 }
   596 
   597 
   598 TEST_F(KeyResetMessageTest, check_reset_ident_uid_only) {
   599     send_setup(); // lazy
   600     pEp_identity* bob = new_identity(NULL, NULL, bob_user_id.c_str(), NULL);
   601 
   602     // Ok, let's reset it
   603     PEP_STATUS status = key_reset_identity(session, bob, NULL);
   604     ASSERT_EQ(status , PEP_ILLEGAL_VALUE);
   605 }
   606 
   607 
   608 TEST_F(KeyResetMessageTest, check_reset_ident_address_only) {
   609     send_setup(); // lazy
   610     pEp_identity* bob = new_identity("pep.test.bob@pep-project.org", NULL, NULL, NULL);
   611 
   612     PEP_STATUS status = key_reset_identity(session, bob, NULL);
   613     ASSERT_EQ(status , PEP_ILLEGAL_VALUE);
   614 }
   615 
   616 
   617 TEST_F(KeyResetMessageTest, check_reset_ident_null_ident) {
   618     // Ok, let's reset it
   619     PEP_STATUS status = key_reset_identity(session, NULL, NULL);
   620     ASSERT_EQ(status , PEP_ILLEGAL_VALUE);
   621 }
   622 
   623 TEST_F(KeyResetMessageTest, check_reset_grouped_own) {
   624     send_setup(); // lazy
   625     pEp_identity* alice = new_identity("pep.test.alice@pep-project.org", NULL, alice_user_id.c_str(), NULL);
   626     PEP_STATUS status = myself(session, alice);
   627 
   628     ASSERT_EQ(status , PEP_STATUS_OK);
   629     ASSERT_TRUE(alice->fpr && alice->fpr[0]);
   630     ASSERT_TRUE(alice->me);
   631     ASSERT_STREQ(alice->fpr, alice_fpr);
   632 
   633     status = set_identity_flags(session, alice, alice->flags | PEP_idf_devicegroup);
   634     status = key_reset_identity(session, alice, alice_fpr);
   635     status = myself(session, alice);
   636     ASSERT_EQ(status , PEP_STATUS_OK);
   637     char* alice_new_fpr = alice->fpr;
   638     ASSERT_TRUE(alice_new_fpr && alice_new_fpr[0]);
   639     ASSERT_STRNE(alice_fpr, alice_new_fpr);
   640 
   641     ASSERT_EQ(m_queue.size(), 1);
   642 
   643     if (false) {
   644         ofstream outfile;
   645         outfile.open("test_mails/check_reset_grouped_own_recv.eml");
   646         message* curr_sent_msg = m_queue.at(0);
   647         char* msg_txt = NULL;
   648         mime_encode_message(curr_sent_msg, false, &msg_txt);
   649         outfile << msg_txt;
   650         outfile.close();
   651         cout << "    ASSERT_STREQ(alice->fpr, \"" << alice_new_fpr << "\");" << endl;
   652         
   653         // Check what we have here, because it looks wrong
   654         char* ptext = NULL;
   655         stringlist_t* _keylist;
   656         size_t psize = 0;
   657         status = decrypt_and_verify(session, curr_sent_msg->attachments->next->value,
   658                                                        strlen(curr_sent_msg->attachments->next->value), NULL, 0,
   659                                                        &ptext, &psize, &_keylist,
   660                                                        NULL);
   661         message* inner_msg = NULL;
   662         status = mime_decode_message(ptext, psize, &inner_msg);
   663         
   664         bloblist_t* key_reset_payload = inner_msg->attachments;  
   665         message* keyreset_msg = NULL;
   666         status = mime_decode_message(key_reset_payload->value, key_reset_payload->size, &keyreset_msg);
   667         keyreset_command_list* cl = NULL;
   668         status = PER_to_key_reset_commands(keyreset_msg->attachments->value, keyreset_msg->attachments->size, &cl);                                             
   669         ASSERT_NE(cl, nullptr);
   670         ASSERT_STREQ(cl->command->ident->address, "pep.test.alice@pep-project.org");
   671         ASSERT_STREQ(cl->command->ident->fpr, alice_fpr);        
   672         ASSERT_STREQ(cl->command->new_key, alice_new_fpr);                
   673         ASSERT_EQ(cl->next, nullptr);
   674     }
   675 }
   676 
   677 TEST_F(KeyResetMessageTest, check_reset_grouped_own_recv) {
   678     send_setup(); // lazy
   679     pEp_identity* alice = new_identity("pep.test.alice@pep-project.org", NULL, alice_user_id.c_str(), NULL);
   680     PEP_STATUS status = myself(session, alice);
   681 
   682     ASSERT_EQ(status , PEP_STATUS_OK);
   683     ASSERT_TRUE(alice->fpr && alice->fpr[0]);
   684     ASSERT_TRUE(alice->me);
   685     ASSERT_STREQ(alice->fpr, alice_fpr);
   686 
   687     status = set_identity_flags(session, alice, alice->flags | PEP_idf_devicegroup);
   688     ASSERT_EQ(status , PEP_STATUS_OK);
   689     status = myself(session, alice);
   690     ASSERT_EQ(status , PEP_STATUS_OK);
   691 
   692     string received_mail = slurp("test_mails/check_reset_grouped_own_recv.eml");
   693     char* decrypted_msg = NULL;
   694     char* modified_src = NULL;
   695     stringlist_t* keylist = NULL;
   696     PEP_rating rating;
   697     PEP_decrypt_flags_t flags = 0;
   698     status = MIME_decrypt_message(session, received_mail.c_str(), received_mail.size(),
   699                                   &decrypted_msg, &keylist, &rating, &flags, &modified_src);
   700 
   701     status = myself(session, alice);
   702     ASSERT_EQ(status , PEP_STATUS_OK);
   703     ASSERT_STRNE(alice->fpr, alice_fpr);
   704     ASSERT_STREQ(alice->fpr, "0D9374B9573548600272BF1D84A892F08ED7BBBF");
   705     bool revoked = false;
   706     status = key_revoked(session, alice_fpr, &revoked);
   707     ASSERT_EQ(status, PEP_STATUS_OK);
   708     ASSERT_TRUE(revoked);
   709 
   710 }
   711 
   712 TEST_F(KeyResetMessageTest, check_reset_grouped_own_multi_ident_one_fpr) {
   713     char* pubkey1 = strdup("74D79B4496E289BD8A71B70BA8E2C4530019697D");
   714 
   715     pEp_identity* alex_id = new_identity("pep.test.alexander@darthmama.org",
   716                                         NULL,
   717                                         "AlexID",
   718                                         "Alexander Braithwaite");
   719 
   720     pEp_identity* alex_id2 = new_identity("pep.test.alexander6@darthmama.org",
   721                                           NULL,
   722                                           "AlexID",
   723                                           "Alexander Braithwaite");
   724 
   725     pEp_identity* alex_id3 = new_identity("pep.test.alexander6a@darthmama.org",
   726                                           NULL,
   727                                           "AlexID",
   728                                           "Alexander Braithwaite");
   729 
   730     PEP_STATUS status = read_file_and_import_key(session, "test_keys/pub/pep.test.alexander6-0x0019697D_pub.asc");
   731     status = read_file_and_import_key(session, "test_keys/priv/pep.test.alexander6-0x0019697D_priv.asc");
   732 
   733     alex_id->me = true;
   734     status = set_own_key(session, alex_id, pubkey1);
   735     ASSERT_EQ(status, PEP_STATUS_OK);
   736     status = set_identity_flags(session, alex_id, alex_id->flags | PEP_idf_devicegroup);
   737     ASSERT_EQ(status , PEP_STATUS_OK);
   738 
   739     alex_id2->me = true;
   740     status = set_own_key(session, alex_id2, pubkey1);
   741     ASSERT_EQ(status, PEP_STATUS_OK);
   742     status = set_identity_flags(session, alex_id2, alex_id2->flags | PEP_idf_devicegroup);
   743     ASSERT_EQ(status , PEP_STATUS_OK);
   744 
   745     alex_id3->me = true;
   746     status = set_own_key(session, alex_id3, pubkey1);
   747     ASSERT_EQ(status, PEP_STATUS_OK);
   748     status = set_identity_flags(session, alex_id3, alex_id3->flags | PEP_idf_devicegroup);
   749     ASSERT_EQ(status , PEP_STATUS_OK);
   750 
   751     status = myself(session, alex_id);
   752     ASSERT_EQ(status, PEP_STATUS_OK);
   753     ASSERT_STREQ(pubkey1, alex_id->fpr);
   754 
   755     status = myself(session, alex_id2);
   756     ASSERT_EQ(status, PEP_STATUS_OK);
   757     ASSERT_STREQ(pubkey1, alex_id2->fpr);
   758 
   759     status = myself(session, alex_id3);
   760     ASSERT_EQ(status, PEP_STATUS_OK);
   761     ASSERT_STREQ(pubkey1, alex_id3->fpr);
   762 
   763     status = key_reset_identity(session, alex_id, pubkey1);
   764 
   765     free(alex_id->fpr);
   766     alex_id->fpr = strdup(pubkey1);
   767     status = get_trust(session, alex_id);
   768     ASSERT_EQ(alex_id->comm_type , PEP_ct_mistrusted);
   769 
   770     bool revoked = false;
   771     status = key_revoked(session, pubkey1, &revoked);
   772     ASSERT_EQ(status, PEP_STATUS_OK);
   773     ASSERT_TRUE(revoked);
   774 
   775     status = myself(session, alex_id);
   776     ASSERT_EQ(status, PEP_STATUS_OK);
   777     ASSERT_STRNE(pubkey1, alex_id->fpr);
   778 
   779     status = myself(session, alex_id2);
   780     ASSERT_EQ(status, PEP_STATUS_OK);
   781     ASSERT_STRNE(pubkey1, alex_id2->fpr);
   782 
   783     status = myself(session, alex_id3);
   784     ASSERT_EQ(status, PEP_STATUS_OK);
   785     ASSERT_STRNE(pubkey1, alex_id3->fpr);
   786 
   787     ASSERT_STRNE(alex_id->fpr, alex_id2->fpr);
   788     ASSERT_STRNE(alex_id->fpr, alex_id3->fpr);
   789     ASSERT_STRNE(alex_id2->fpr, alex_id3->fpr);
   790 
   791     ASSERT_EQ(m_queue.size(),1);
   792     if (false) {
   793         ofstream outfile;
   794         message* curr_sent_msg = m_queue.at(0);        
   795         string fname = "test_mails/check_reset_grouped_own_multi_ident_one_fpr.eml";
   796         outfile.open(fname);
   797         char* msg_txt = NULL;
   798         mime_encode_message(curr_sent_msg, false, &msg_txt);
   799         outfile << msg_txt;
   800         outfile.close();        
   801         cout <<  "    const char* replkey1 = \"" << alex_id->fpr << "\";" << endl;    
   802         cout <<  "    const char* replkey2 = \"" << alex_id2->fpr << "\";" << endl;    
   803         cout <<  "    const char* replkey3 = \"" << alex_id3->fpr << "\";" << endl;        
   804     }    
   805 
   806     free_identity(alex_id);
   807     free_identity(alex_id2);
   808     free_identity(alex_id3);
   809 }
   810 
   811 TEST_F(KeyResetMessageTest, check_reset_grouped_own_multi_ident_one_fpr_recv) {
   812     PEP_STATUS status = PEP_STATUS_OK;
   813     const char* replkey1 = "369BD11FB63BAE28105085BFE399BE76DA9EB063";
   814     const char* replkey2 = "6F61E78E8250F1CA7573F01F53F6467C533334F7";
   815     const char* replkey3 = "0ECDE4A782182BD5B59BF7D25738C0F2C6D2D837";
   816         
   817     // set up device own state
   818     char* pubkey1 = strdup("74D79B4496E289BD8A71B70BA8E2C4530019697D");
   819 
   820     pEp_identity* alex_id = new_identity("pep.test.alexander@darthmama.org",
   821                                         NULL,
   822                                         "AlexID",
   823                                         "Alexander Braithwaite");
   824 
   825     pEp_identity* alex_id2 = new_identity("pep.test.alexander6@darthmama.org",
   826                                           NULL,
   827                                           "AlexID",
   828                                           "Alexander Braithwaite");
   829 
   830     pEp_identity* alex_id3 = new_identity("pep.test.alexander6a@darthmama.org",
   831                                           NULL,
   832                                           "AlexID",
   833                                           "Alexander Braithwaite");
   834 
   835     status = read_file_and_import_key(session, "test_keys/pub/pep.test.alexander6-0x0019697D_pub.asc");
   836     status = read_file_and_import_key(session, "test_keys/priv/pep.test.alexander6-0x0019697D_priv.asc");
   837 
   838     alex_id->me = true;
   839     status = set_own_key(session, alex_id, pubkey1);
   840     ASSERT_EQ(status, PEP_STATUS_OK);
   841     status = set_identity_flags(session, alex_id, alex_id->flags | PEP_idf_devicegroup);
   842     ASSERT_EQ(status , PEP_STATUS_OK);
   843 
   844     alex_id2->me = true;
   845     status = set_own_key(session, alex_id2, pubkey1);
   846     ASSERT_EQ(status, PEP_STATUS_OK);
   847     status = set_identity_flags(session, alex_id2, alex_id2->flags | PEP_idf_devicegroup);
   848     ASSERT_EQ(status , PEP_STATUS_OK);
   849 
   850     alex_id3->me = true;
   851     status = set_own_key(session, alex_id3, pubkey1);
   852     ASSERT_EQ(status, PEP_STATUS_OK);
   853     status = set_identity_flags(session, alex_id3, alex_id3->flags | PEP_idf_devicegroup);
   854     ASSERT_EQ(status , PEP_STATUS_OK);
   855 
   856     status = myself(session, alex_id);
   857     ASSERT_EQ(status, PEP_STATUS_OK);
   858     ASSERT_STREQ(pubkey1, alex_id->fpr);
   859 
   860     status = myself(session, alex_id2);
   861     ASSERT_EQ(status, PEP_STATUS_OK);
   862     ASSERT_STREQ(pubkey1, alex_id2->fpr);
   863 
   864     status = myself(session, alex_id3);
   865     ASSERT_EQ(status, PEP_STATUS_OK);
   866     ASSERT_STREQ(pubkey1, alex_id3->fpr);
   867 
   868     // receive reset messages
   869     message* dec_msg = NULL;
   870     stringlist_t* keylist = NULL;
   871     PEP_rating rating;
   872     PEP_decrypt_flags_t flags = 0;
   873 
   874     string fname = "test_mails/check_reset_grouped_own_multi_ident_one_fpr.eml";
   875     string mailstr = slurp(fname.c_str());
   876     message* new_msg = NULL;
   877     status = mime_decode_message(mailstr.c_str(), mailstr.size(), &new_msg);
   878     ASSERT_NE(new_msg, nullptr);
   879     ASSERT_EQ(status, PEP_STATUS_OK);
   880 
   881     status = decrypt_message(session, new_msg, &dec_msg, &keylist, &rating, &flags);
   882     ASSERT_EQ(status, PEP_STATUS_OK);        
   883 
   884     status = myself(session, alex_id);
   885     ASSERT_EQ(status, PEP_STATUS_OK);
   886     ASSERT_STREQ(replkey1, alex_id->fpr);
   887 
   888     status = myself(session, alex_id2);
   889     ASSERT_EQ(status, PEP_STATUS_OK);
   890     ASSERT_STREQ(replkey2, alex_id2->fpr);
   891 
   892     status = myself(session, alex_id3);
   893     ASSERT_EQ(status, PEP_STATUS_OK);
   894     ASSERT_STREQ(replkey3, alex_id3->fpr);
   895 }
   896 
   897 TEST_F(KeyResetMessageTest, check_reset_grouped_own_multiple_keys_multiple_idents_reset_all) {
   898     char* pubkey1 = strdup("74D79B4496E289BD8A71B70BA8E2C4530019697D");
   899     char* pubkey2 = strdup("2E21325D202A44BFD9C607FCF095B202503B14D8");
   900     char* pubkey3 = strdup("3C1E713D8519D7F907E3142D179EAA24A216E95A");
   901 
   902     pEp_identity* alex_id = new_identity("pep.test.alexander@darthmama.org",
   903                                         NULL,
   904                                         "AlexID",
   905                                         "Alexander Braithwaite");
   906 
   907     pEp_identity* alex_id2 = new_identity("pep.test.alexander6@darthmama.org",
   908                                           NULL,
   909                                           "AlexID",
   910                                           "Alexander Braithwaite");
   911 
   912     pEp_identity* alex_id3 = new_identity("pep.test.alexander6a@darthmama.org",
   913                                           NULL,
   914                                           "AlexID",
   915                                           "Alexander Braithwaite");
   916 
   917 
   918     PEP_STATUS status = read_file_and_import_key(session, "test_keys/pub/pep.test.alexander6-0x0019697D_pub.asc");
   919     status = read_file_and_import_key(session, "test_keys/pub/pep.test.alexander6-0x503B14D8_pub.asc");
   920     status = read_file_and_import_key(session, "test_keys/pub/pep.test.alexander6-0xA216E95A_pub.asc");
   921     status = read_file_and_import_key(session, "test_keys/priv/pep.test.alexander6-0x0019697D_priv.asc");
   922     status = read_file_and_import_key(session, "test_keys/priv/pep.test.alexander6-0x503B14D8_priv.asc");
   923     status = read_file_and_import_key(session, "test_keys/priv/pep.test.alexander6-0xA216E95A_priv.asc");
   924 
   925     alex_id->me = true;
   926     status = set_own_key(session, alex_id, pubkey1);
   927     ASSERT_EQ(status, PEP_STATUS_OK);
   928     status = set_identity_flags(session, alex_id, alex_id->flags | PEP_idf_devicegroup);
   929     ASSERT_EQ(status , PEP_STATUS_OK);
   930 
   931     alex_id2->me = true;
   932     status = set_own_key(session, alex_id2, pubkey2);
   933     ASSERT_EQ(status, PEP_STATUS_OK);
   934     status = set_identity_flags(session, alex_id2, alex_id2->flags | PEP_idf_devicegroup);
   935     ASSERT_EQ(status , PEP_STATUS_OK);
   936 
   937     alex_id3->me = true;
   938     status = set_own_key(session, alex_id3, pubkey3);
   939     ASSERT_EQ(status, PEP_STATUS_OK);
   940     status = set_identity_flags(session, alex_id3, alex_id3->flags | PEP_idf_devicegroup);
   941     ASSERT_EQ(status , PEP_STATUS_OK);
   942 
   943     status = myself(session, alex_id);
   944     ASSERT_EQ(status, PEP_STATUS_OK);
   945     ASSERT_STREQ(pubkey1, alex_id->fpr);
   946 
   947     status = myself(session, alex_id2);
   948     ASSERT_EQ(status, PEP_STATUS_OK);
   949     ASSERT_STREQ(pubkey2, alex_id2->fpr);
   950 
   951     status = myself(session, alex_id3);
   952     ASSERT_EQ(status, PEP_STATUS_OK);
   953     ASSERT_STREQ(pubkey3, alex_id3->fpr);
   954 
   955     status = key_reset_all_own_keys(session);
   956 
   957     free(alex_id->fpr);
   958     alex_id->fpr = strdup(pubkey1);
   959     status = get_trust(session, alex_id);
   960     ASSERT_EQ(alex_id->comm_type , PEP_ct_mistrusted);
   961 
   962     free(alex_id2->fpr);
   963     alex_id2->fpr = strdup(pubkey2);
   964     status = get_trust(session, alex_id2);
   965     ASSERT_EQ(alex_id2->comm_type , PEP_ct_mistrusted);
   966 
   967     free(alex_id3->fpr);
   968     alex_id3->fpr = strdup(pubkey3);
   969     status = get_trust(session, alex_id3);
   970     ASSERT_EQ(alex_id3->comm_type , PEP_ct_mistrusted);
   971 
   972     bool revoked = false;
   973     status = key_revoked(session, pubkey1, &revoked);
   974     ASSERT_EQ(status, PEP_STATUS_OK);
   975     ASSERT_TRUE(revoked);
   976 
   977     revoked = false;
   978     status = key_revoked(session, pubkey2, &revoked);
   979     ASSERT_EQ(status, PEP_STATUS_OK);
   980     ASSERT_TRUE(revoked);
   981 
   982     revoked = false;
   983     status = key_revoked(session, pubkey3, &revoked);
   984     ASSERT_EQ(status, PEP_STATUS_OK);
   985     ASSERT_TRUE(revoked);
   986 
   987     status = myself(session, alex_id);
   988     ASSERT_EQ(status, PEP_STATUS_OK);
   989     ASSERT_STRNE(pubkey1, alex_id->fpr);
   990 
   991     status = myself(session, alex_id2);
   992     ASSERT_EQ(status, PEP_STATUS_OK);
   993     ASSERT_STRNE(pubkey2, alex_id2->fpr);
   994 
   995     status = myself(session, alex_id3);
   996     ASSERT_EQ(status, PEP_STATUS_OK);
   997     ASSERT_STRNE(pubkey3, alex_id3->fpr);
   998 
   999     // Not reaaaally necessary, but...
  1000     ASSERT_STRNE(alex_id->fpr, alex_id2->fpr);
  1001     ASSERT_STRNE(alex_id->fpr, alex_id3->fpr);
  1002     ASSERT_STRNE(alex_id2->fpr, alex_id3->fpr);
  1003 
  1004     ASSERT_EQ(m_queue.size(),3);
  1005     if (false) {
  1006         ofstream outfile;
  1007         int i = 0;
  1008         for (vector<message*>::iterator it = m_queue.begin(); it != m_queue.end(); it++, i++) {
  1009             message* curr_sent_msg = *it;        
  1010             string fname = string("test_mails/check_reset_grouped_own_multiple_keys_multiple_idents_reset_all_") + to_string(i) + ".eml";
  1011             outfile.open(fname);
  1012             char* msg_txt = NULL;
  1013             mime_encode_message(curr_sent_msg, false, &msg_txt);
  1014             outfile << msg_txt;
  1015             outfile.close();        
  1016         }
  1017         cout <<  "    // For " << alex_id->address << endl;
  1018         cout <<  "    const char* replkey1 = \"" << alex_id->fpr << "\";" << endl;    
  1019         cout <<  "    // For " << alex_id2->address << endl;        
  1020         cout <<  "    const char* replkey2 = \"" << alex_id2->fpr << "\";" << endl;    
  1021         cout <<  "    // For " << alex_id3->address << endl;        
  1022         cout <<  "    const char* replkey3 = \"" << alex_id3->fpr << "\";" << endl;        
  1023     }    
  1024 
  1025     free_identity(alex_id);
  1026     free_identity(alex_id2);
  1027     free_identity(alex_id3);
  1028 }
  1029 
  1030 TEST_F(KeyResetMessageTest, check_reset_all_own_grouped) {
  1031     char* pubkey1 = strdup("74D79B4496E289BD8A71B70BA8E2C4530019697D");
  1032     char* pubkey2 = strdup("2E21325D202A44BFD9C607FCF095B202503B14D8");
  1033     char* pubkey3 = strdup("3C1E713D8519D7F907E3142D179EAA24A216E95A");
  1034 
  1035     pEp_identity* alex_id = new_identity("pep.test.alexander@darthmama.org",
  1036                                         NULL,
  1037                                         "AlexID",
  1038                                         "Alexander Braithwaite");
  1039 
  1040     pEp_identity* alex_id2 = new_identity("pep.test.alexander6@darthmama.org",
  1041                                           NULL,
  1042                                           "AlexID",
  1043                                           "Alexander Braithwaite");
  1044 
  1045     pEp_identity* alex_id3 = new_identity("pep.test.alexander6a@darthmama.org",
  1046                                           NULL,
  1047                                           "AlexID",
  1048                                           "Alexander Braithwaite");
  1049 
  1050 
  1051     PEP_STATUS status = read_file_and_import_key(session, "test_keys/pub/pep.test.alexander6-0x0019697D_pub.asc");
  1052     status = read_file_and_import_key(session, "test_keys/pub/pep.test.alexander6-0x503B14D8_pub.asc");
  1053     status = read_file_and_import_key(session, "test_keys/pub/pep.test.alexander6-0xA216E95A_pub.asc");
  1054     status = read_file_and_import_key(session, "test_keys/priv/pep.test.alexander6-0x0019697D_priv.asc");
  1055     status = read_file_and_import_key(session, "test_keys/priv/pep.test.alexander6-0x503B14D8_priv.asc");
  1056     status = read_file_and_import_key(session, "test_keys/priv/pep.test.alexander6-0xA216E95A_priv.asc");
  1057 
  1058     alex_id->me = true;
  1059     status = set_own_key(session, alex_id, pubkey1);
  1060     ASSERT_EQ(status, PEP_STATUS_OK);
  1061     status = set_identity_flags(session, alex_id, alex_id->flags | PEP_idf_devicegroup);
  1062     ASSERT_EQ(status , PEP_STATUS_OK);
  1063 
  1064     alex_id2->me = true;
  1065     status = set_own_key(session, alex_id2, pubkey2);
  1066     ASSERT_EQ(status, PEP_STATUS_OK);
  1067     status = set_identity_flags(session, alex_id2, alex_id2->flags | PEP_idf_not_for_sync);
  1068     ASSERT_EQ(status , PEP_STATUS_OK);
  1069 
  1070     alex_id3->me = true;
  1071     status = set_own_key(session, alex_id3, pubkey3);
  1072     ASSERT_EQ(status, PEP_STATUS_OK);
  1073     status = set_identity_flags(session, alex_id3, alex_id3->flags | PEP_idf_devicegroup);
  1074     ASSERT_EQ(status , PEP_STATUS_OK);
  1075 
  1076     status = myself(session, alex_id);
  1077     ASSERT_EQ(status, PEP_STATUS_OK);
  1078     ASSERT_STREQ(pubkey1, alex_id->fpr);
  1079 
  1080     status = myself(session, alex_id2);
  1081     ASSERT_EQ(status, PEP_STATUS_OK);
  1082     ASSERT_STREQ(pubkey2, alex_id2->fpr);
  1083 
  1084     status = myself(session, alex_id3);
  1085     ASSERT_EQ(status, PEP_STATUS_OK);
  1086     ASSERT_STREQ(pubkey3, alex_id3->fpr);
  1087 
  1088     status = key_reset_own_grouped_keys(session);
  1089 
  1090     free(alex_id->fpr);
  1091     alex_id->fpr = strdup(pubkey1);
  1092     status = get_trust(session, alex_id);
  1093     ASSERT_EQ(alex_id->comm_type , PEP_ct_mistrusted);
  1094 
  1095     free(alex_id2->fpr);
  1096     alex_id2->fpr = strdup(pubkey2);
  1097     status = get_trust(session, alex_id2);
  1098     ASSERT_EQ(alex_id2->comm_type , PEP_ct_pEp);
  1099 
  1100     free(alex_id3->fpr);
  1101     alex_id3->fpr = strdup(pubkey3);
  1102     status = get_trust(session, alex_id3);
  1103     ASSERT_EQ(alex_id3->comm_type , PEP_ct_mistrusted);
  1104 
  1105     bool revoked = false;
  1106     status = key_revoked(session, pubkey1, &revoked);
  1107     ASSERT_EQ(status, PEP_STATUS_OK);
  1108     ASSERT_TRUE(revoked);
  1109 
  1110     revoked = false;
  1111     status = key_revoked(session, pubkey2, &revoked);
  1112     ASSERT_EQ(status, PEP_STATUS_OK);
  1113     ASSERT_FALSE(revoked);
  1114 
  1115     revoked = false;
  1116     status = key_revoked(session, pubkey3, &revoked);
  1117     ASSERT_EQ(status, PEP_STATUS_OK);
  1118     ASSERT_TRUE(revoked);
  1119 
  1120     status = myself(session, alex_id);
  1121     ASSERT_EQ(status, PEP_STATUS_OK);
  1122     ASSERT_STRNE(pubkey1, alex_id->fpr);
  1123 
  1124     status = myself(session, alex_id2);
  1125     ASSERT_EQ(status, PEP_STATUS_OK);
  1126     ASSERT_STREQ(pubkey2, alex_id2->fpr);
  1127 
  1128     status = myself(session, alex_id3);
  1129     ASSERT_EQ(status, PEP_STATUS_OK);
  1130     ASSERT_STRNE(pubkey3, alex_id3->fpr);
  1131 
  1132     ASSERT_EQ(m_queue.size(),2);
  1133     if (false) {
  1134         ofstream outfile;
  1135         int i = 0;
  1136         for (vector<message*>::iterator it = m_queue.begin(); it != m_queue.end(); it++, i++) {
  1137             message* curr_sent_msg = *it;        
  1138             string fname = string("test_mails/check_reset_all_own_grouped") + to_string(i) + ".eml";
  1139             outfile.open(fname);
  1140             char* msg_txt = NULL;
  1141             mime_encode_message(curr_sent_msg, false, &msg_txt);
  1142             outfile << msg_txt;
  1143             outfile.close();        
  1144         }
  1145         cout <<  "    // For " << alex_id->address << endl;
  1146         cout <<  "    const char* replkey1 = \"" << alex_id->fpr << "\";" << endl;    
  1147         cout <<  "    // For " << alex_id3->address << endl;        
  1148         cout <<  "    const char* replkey3 = \"" << alex_id3->fpr << "\";" << endl;        
  1149     }    
  1150 
  1151     free_identity(alex_id);
  1152     free_identity(alex_id2);
  1153     free_identity(alex_id3);
  1154 }
  1155 
  1156 TEST_F(KeyResetMessageTest, check_reset_all_own_grouped_recv) {
  1157     PEP_STATUS status = PEP_STATUS_OK;
  1158     char* pubkey1 = strdup("74D79B4496E289BD8A71B70BA8E2C4530019697D");
  1159     char* pubkey2 = strdup("2E21325D202A44BFD9C607FCF095B202503B14D8");
  1160     char* pubkey3 = strdup("3C1E713D8519D7F907E3142D179EAA24A216E95A");
  1161 
  1162     // For pep.test.alexander@darthmama.org
  1163     const char* replkey1 = "80C297DEA68620511456DEE3AA0383505C87139D";
  1164     // For pep.test.alexander6a@darthmama.org
  1165     const char* replkey3 = "1F541C9A0EB643403035AC820A13A27FBC1A77EC";
  1166     
  1167     pEp_identity* alex_id = new_identity("pep.test.alexander@darthmama.org",
  1168                                         NULL,
  1169                                         "AlexID",
  1170                                         "Alexander Braithwaite");
  1171 
  1172     pEp_identity* alex_id2 = new_identity("pep.test.alexander6@darthmama.org",
  1173                                           NULL,
  1174                                           "AlexID",
  1175                                           "Alexander Braithwaite");
  1176 
  1177     pEp_identity* alex_id3 = new_identity("pep.test.alexander6a@darthmama.org",
  1178                                           NULL,
  1179                                           "AlexID",
  1180                                           "Alexander Braithwaite");
  1181 
  1182 
  1183     status = read_file_and_import_key(session, "test_keys/pub/pep.test.alexander6-0x0019697D_pub.asc");
  1184     status = read_file_and_import_key(session, "test_keys/pub/pep.test.alexander6-0x503B14D8_pub.asc");
  1185     status = read_file_and_import_key(session, "test_keys/pub/pep.test.alexander6-0xA216E95A_pub.asc");
  1186     status = read_file_and_import_key(session, "test_keys/priv/pep.test.alexander6-0x0019697D_priv.asc");
  1187     status = read_file_and_import_key(session, "test_keys/priv/pep.test.alexander6-0x503B14D8_priv.asc");
  1188     status = read_file_and_import_key(session, "test_keys/priv/pep.test.alexander6-0xA216E95A_priv.asc");
  1189 
  1190     alex_id->me = true;
  1191     status = set_own_key(session, alex_id, pubkey1);
  1192     ASSERT_EQ(status, PEP_STATUS_OK);
  1193     status = set_identity_flags(session, alex_id, alex_id->flags | PEP_idf_devicegroup);
  1194     ASSERT_EQ(status , PEP_STATUS_OK);
  1195 
  1196     alex_id2->me = true;
  1197     status = set_own_key(session, alex_id2, pubkey2);
  1198     ASSERT_EQ(status, PEP_STATUS_OK);
  1199     status = set_identity_flags(session, alex_id2, alex_id2->flags | PEP_idf_devicegroup);
  1200     ASSERT_EQ(status , PEP_STATUS_OK);
  1201 
  1202     alex_id3->me = true;
  1203     status = set_own_key(session, alex_id3, pubkey3);
  1204     ASSERT_EQ(status, PEP_STATUS_OK);
  1205     status = set_identity_flags(session, alex_id3, alex_id3->flags | PEP_idf_devicegroup);
  1206     ASSERT_EQ(status , PEP_STATUS_OK);
  1207 
  1208     status = myself(session, alex_id);
  1209     ASSERT_EQ(status, PEP_STATUS_OK);
  1210     ASSERT_STREQ(pubkey1, alex_id->fpr);
  1211 
  1212     status = myself(session, alex_id2);
  1213     ASSERT_EQ(status, PEP_STATUS_OK);
  1214     ASSERT_STREQ(pubkey2, alex_id2->fpr);
  1215 
  1216     status = myself(session, alex_id3);
  1217     ASSERT_EQ(status, PEP_STATUS_OK);
  1218     ASSERT_STREQ(pubkey3, alex_id3->fpr);
  1219 
  1220     const int num_msgs = 2;
  1221     for (int i = 0; i < num_msgs; i++) {
  1222         // receive reset messages
  1223         message* dec_msg = NULL;
  1224         stringlist_t* keylist = NULL;
  1225         PEP_rating rating;
  1226         PEP_decrypt_flags_t flags = 0;
  1227 
  1228         string fname = string("test_mails/check_reset_all_own_grouped") + to_string(i) + ".eml";
  1229         string mailstr = slurp(fname.c_str());
  1230         message* new_msg = NULL;
  1231         status = mime_decode_message(mailstr.c_str(), mailstr.size(), &new_msg);
  1232         ASSERT_NE(new_msg, nullptr);
  1233         ASSERT_EQ(status, PEP_STATUS_OK);
  1234 
  1235         status = decrypt_message(session, new_msg, &dec_msg, &keylist, &rating, &flags);
  1236         ASSERT_EQ(status, PEP_STATUS_OK);        
  1237     }
  1238 
  1239     status = myself(session, alex_id);
  1240     ASSERT_EQ(status, PEP_STATUS_OK);
  1241     ASSERT_STREQ(replkey1, alex_id->fpr);
  1242 
  1243     status = myself(session, alex_id2);
  1244     ASSERT_EQ(status, PEP_STATUS_OK);
  1245     ASSERT_STREQ(pubkey2, alex_id2->fpr);
  1246 
  1247     status = myself(session, alex_id3);
  1248     ASSERT_EQ(status, PEP_STATUS_OK);
  1249     ASSERT_STREQ(replkey3, alex_id3->fpr);
  1250 }
  1251 
  1252 TEST_F(KeyResetMessageTest, check_reset_grouped_own_multiple_keys_multiple_idents_reset_all_recv) {
  1253     PEP_STATUS status = PEP_STATUS_OK;
  1254     char* pubkey1 = strdup("74D79B4496E289BD8A71B70BA8E2C4530019697D");
  1255     char* pubkey2 = strdup("2E21325D202A44BFD9C607FCF095B202503B14D8");
  1256     char* pubkey3 = strdup("3C1E713D8519D7F907E3142D179EAA24A216E95A");
  1257 
  1258     // For pep.test.alexander@darthmama.org
  1259     const char* replkey1 = "D3F3F1A71B90AB972965983A425AF48C11059347";
  1260     // For pep.test.alexander6@darthmama.org
  1261     const char* replkey2 = "C3752B57BDFD8969AD77DA73B00EC49BF2B71A66";
  1262     // For pep.test.alexander6a@darthmama.org
  1263     const char* replkey3 = "52E8C995B74990EC258BD7B1C27D8251B345F01F";
  1264     
  1265     pEp_identity* alex_id = new_identity("pep.test.alexander@darthmama.org",
  1266                                         NULL,
  1267                                         "AlexID",
  1268                                         "Alexander Braithwaite");
  1269 
  1270     pEp_identity* alex_id2 = new_identity("pep.test.alexander6@darthmama.org",
  1271                                           NULL,
  1272                                           "AlexID",
  1273                                           "Alexander Braithwaite");
  1274 
  1275     pEp_identity* alex_id3 = new_identity("pep.test.alexander6a@darthmama.org",
  1276                                           NULL,
  1277                                           "AlexID",
  1278                                           "Alexander Braithwaite");
  1279 
  1280 
  1281     status = read_file_and_import_key(session, "test_keys/pub/pep.test.alexander6-0x0019697D_pub.asc");
  1282     status = read_file_and_import_key(session, "test_keys/pub/pep.test.alexander6-0x503B14D8_pub.asc");
  1283     status = read_file_and_import_key(session, "test_keys/pub/pep.test.alexander6-0xA216E95A_pub.asc");
  1284     status = read_file_and_import_key(session, "test_keys/priv/pep.test.alexander6-0x0019697D_priv.asc");
  1285     status = read_file_and_import_key(session, "test_keys/priv/pep.test.alexander6-0x503B14D8_priv.asc");
  1286     status = read_file_and_import_key(session, "test_keys/priv/pep.test.alexander6-0xA216E95A_priv.asc");
  1287 
  1288     alex_id->me = true;
  1289     status = set_own_key(session, alex_id, pubkey1);
  1290     ASSERT_EQ(status, PEP_STATUS_OK);
  1291     status = set_identity_flags(session, alex_id, alex_id->flags | PEP_idf_devicegroup);
  1292     ASSERT_EQ(status , PEP_STATUS_OK);
  1293 
  1294     alex_id2->me = true;
  1295     status = set_own_key(session, alex_id2, pubkey2);
  1296     ASSERT_EQ(status, PEP_STATUS_OK);
  1297     status = set_identity_flags(session, alex_id2, alex_id2->flags | PEP_idf_devicegroup);
  1298     ASSERT_EQ(status , PEP_STATUS_OK);
  1299 
  1300     alex_id3->me = true;
  1301     status = set_own_key(session, alex_id3, pubkey3);
  1302     ASSERT_EQ(status, PEP_STATUS_OK);
  1303     status = set_identity_flags(session, alex_id3, alex_id3->flags | PEP_idf_devicegroup);
  1304     ASSERT_EQ(status , PEP_STATUS_OK);
  1305 
  1306     status = myself(session, alex_id);
  1307     ASSERT_EQ(status, PEP_STATUS_OK);
  1308     ASSERT_STREQ(pubkey1, alex_id->fpr);
  1309 
  1310     status = myself(session, alex_id2);
  1311     ASSERT_EQ(status, PEP_STATUS_OK);
  1312     ASSERT_STREQ(pubkey2, alex_id2->fpr);
  1313 
  1314     status = myself(session, alex_id3);
  1315     ASSERT_EQ(status, PEP_STATUS_OK);
  1316     ASSERT_STREQ(pubkey3, alex_id3->fpr);
  1317 
  1318     const int num_msgs = 3;
  1319     for (int i = 0; i < num_msgs; i++) {
  1320         // receive reset messages
  1321         message* dec_msg = NULL;
  1322         stringlist_t* keylist = NULL;
  1323         PEP_rating rating;
  1324         PEP_decrypt_flags_t flags = 0;
  1325 
  1326         string fname = string("test_mails/check_reset_grouped_own_multiple_keys_multiple_idents_reset_all_") + to_string(i) + ".eml";
  1327         string mailstr = slurp(fname.c_str());
  1328         message* new_msg = NULL;
  1329         status = mime_decode_message(mailstr.c_str(), mailstr.size(), &new_msg);
  1330         ASSERT_NE(new_msg, nullptr);
  1331         ASSERT_EQ(status, PEP_STATUS_OK);
  1332 
  1333         status = decrypt_message(session, new_msg, &dec_msg, &keylist, &rating, &flags);
  1334         ASSERT_EQ(status, PEP_STATUS_OK);        
  1335     }
  1336 
  1337     status = myself(session, alex_id);
  1338     ASSERT_EQ(status, PEP_STATUS_OK);
  1339     ASSERT_STREQ(replkey1, alex_id->fpr);
  1340 
  1341     status = myself(session, alex_id2);
  1342     ASSERT_EQ(status, PEP_STATUS_OK);
  1343     ASSERT_STREQ(replkey2, alex_id2->fpr);
  1344 
  1345     status = myself(session, alex_id3);
  1346     ASSERT_EQ(status, PEP_STATUS_OK);
  1347     ASSERT_STREQ(replkey3, alex_id3->fpr);
  1348 }
  1349 
  1350 
  1351 TEST_F(KeyResetMessageTest, check_reset_grouped_own_multiple_keys_multiple_idents_reset_one) {
  1352     char* pubkey1 = strdup("74D79B4496E289BD8A71B70BA8E2C4530019697D");
  1353     char* pubkey2 = strdup("2E21325D202A44BFD9C607FCF095B202503B14D8");
  1354     char* pubkey3 = strdup("3C1E713D8519D7F907E3142D179EAA24A216E95A");
  1355 
  1356     pEp_identity* alex_id = new_identity("pep.test.alexander@darthmama.org",
  1357                                         NULL,
  1358                                         "AlexID",
  1359                                         "Alexander Braithwaite");
  1360 
  1361     pEp_identity* alex_id2 = new_identity("pep.test.alexander6@darthmama.org",
  1362                                           NULL,
  1363                                           "AlexID",
  1364                                           "Alexander Braithwaite");
  1365 
  1366     pEp_identity* alex_id3 = new_identity("pep.test.alexander6a@darthmama.org",
  1367                                           NULL,
  1368                                           "AlexID",
  1369                                           "Alexander Braithwaite");
  1370 
  1371 
  1372     PEP_STATUS status = read_file_and_import_key(session, "test_keys/pub/pep.test.alexander6-0x0019697D_pub.asc");
  1373     status = read_file_and_import_key(session, "test_keys/pub/pep.test.alexander6-0x503B14D8_pub.asc");
  1374     status = read_file_and_import_key(session, "test_keys/pub/pep.test.alexander6-0xA216E95A_pub.asc");
  1375     status = read_file_and_import_key(session, "test_keys/priv/pep.test.alexander6-0x0019697D_priv.asc");
  1376     status = read_file_and_import_key(session, "test_keys/priv/pep.test.alexander6-0x503B14D8_priv.asc");
  1377     status = read_file_and_import_key(session, "test_keys/priv/pep.test.alexander6-0xA216E95A_priv.asc");
  1378 
  1379     alex_id->me = true;
  1380     status = set_own_key(session, alex_id, pubkey1);
  1381     ASSERT_EQ(status, PEP_STATUS_OK);
  1382     status = set_identity_flags(session, alex_id2, alex_id2->flags | PEP_idf_devicegroup);
  1383     ASSERT_EQ(status , PEP_STATUS_OK);
  1384 
  1385     alex_id2->me = true;
  1386     status = set_own_key(session, alex_id2, pubkey2);
  1387     ASSERT_EQ(status, PEP_STATUS_OK);
  1388     status = set_identity_flags(session, alex_id2, alex_id2->flags | PEP_idf_devicegroup);
  1389     ASSERT_EQ(status , PEP_STATUS_OK);
  1390 
  1391     alex_id3->me = true;
  1392     status = set_own_key(session, alex_id3, pubkey3);
  1393     ASSERT_EQ(status, PEP_STATUS_OK);
  1394     status = set_identity_flags(session, alex_id2, alex_id2->flags | PEP_idf_devicegroup);
  1395     ASSERT_EQ(status , PEP_STATUS_OK);
  1396 
  1397     status = myself(session, alex_id);
  1398     ASSERT_EQ(status, PEP_STATUS_OK);
  1399     ASSERT_STREQ(pubkey1, alex_id->fpr);
  1400 
  1401     status = myself(session, alex_id2);
  1402     ASSERT_EQ(status, PEP_STATUS_OK);
  1403     ASSERT_STREQ(pubkey2, alex_id2->fpr);
  1404 
  1405     status = myself(session, alex_id3);
  1406     ASSERT_EQ(status, PEP_STATUS_OK);
  1407     ASSERT_STREQ(pubkey3, alex_id3->fpr);
  1408 
  1409     status = key_reset_identity(session, alex_id2, alex_id2->fpr);
  1410 
  1411     free(alex_id->fpr);
  1412     alex_id->fpr = strdup(pubkey1);
  1413     status = get_trust(session, alex_id);
  1414     ASSERT_EQ(alex_id->comm_type , PEP_ct_pEp);
  1415 
  1416     free(alex_id2->fpr);
  1417     alex_id2->fpr = strdup(pubkey2);
  1418     status = get_trust(session, alex_id2);
  1419     ASSERT_EQ(alex_id2->comm_type , PEP_ct_mistrusted);
  1420 
  1421     free(alex_id3->fpr);
  1422     alex_id3->fpr = strdup(pubkey3);
  1423     status = get_trust(session, alex_id3);
  1424     ASSERT_EQ(alex_id3->comm_type , PEP_ct_pEp);
  1425 
  1426     bool revoked = false;
  1427     status = key_revoked(session, pubkey1, &revoked);
  1428     ASSERT_EQ(status, PEP_STATUS_OK);
  1429     ASSERT_FALSE(revoked);
  1430 
  1431     revoked = false;
  1432     status = key_revoked(session, pubkey2, &revoked);
  1433     ASSERT_EQ(status, PEP_STATUS_OK);
  1434     ASSERT_TRUE(revoked);
  1435 
  1436     revoked = false;
  1437     status = key_revoked(session, pubkey3, &revoked);
  1438     ASSERT_EQ(status, PEP_STATUS_OK);
  1439     ASSERT_FALSE(revoked);
  1440 
  1441     status = myself(session, alex_id);
  1442     ASSERT_EQ(status, PEP_STATUS_OK);
  1443     ASSERT_STREQ(pubkey1, alex_id->fpr);
  1444 
  1445     status = myself(session, alex_id2);
  1446     ASSERT_EQ(status, PEP_STATUS_OK);
  1447     ASSERT_STRNE(pubkey2, alex_id2->fpr);
  1448 
  1449     status = myself(session, alex_id3);
  1450     ASSERT_EQ(status, PEP_STATUS_OK);
  1451     ASSERT_STREQ(pubkey3, alex_id3->fpr);
  1452 
  1453     // Not reaaaally necessary, but...
  1454     ASSERT_STRNE(alex_id->fpr, alex_id2->fpr);
  1455     ASSERT_STRNE(alex_id->fpr, alex_id3->fpr);
  1456     ASSERT_STRNE(alex_id2->fpr, alex_id3->fpr);
  1457 
  1458     ASSERT_EQ(m_queue.size(),1);
  1459     if (false) {
  1460         ofstream outfile;
  1461         message* curr_sent_msg = m_queue.at(0);        
  1462         string fname = "test_mails/check_reset_grouped_own_multiple_keys_multiple_idents_reset_one.eml";
  1463         outfile.open(fname);
  1464         char* msg_txt = NULL;
  1465         mime_encode_message(curr_sent_msg, false, &msg_txt);
  1466         outfile << msg_txt;
  1467         outfile.close();        
  1468         cout <<  "    const char* replkey2 = \"" << alex_id2->fpr << "\";" << endl;    
  1469     }    
  1470     
  1471 
  1472     free_identity(alex_id);
  1473     free_identity(alex_id2);
  1474     free_identity(alex_id3);
  1475 }
  1476 
  1477 TEST_F(KeyResetMessageTest, check_reset_grouped_own_multiple_keys_multiple_idents_reset_one_recv) {
  1478     char* pubkey1 = strdup("74D79B4496E289BD8A71B70BA8E2C4530019697D");
  1479     char* pubkey2 = strdup("2E21325D202A44BFD9C607FCF095B202503B14D8");
  1480     char* pubkey3 = strdup("3C1E713D8519D7F907E3142D179EAA24A216E95A");
  1481     const char* replkey2 = "E08F5E0AB145AFF1DBC4D0FB49F3D5CAC8A47991";
  1482           
  1483     pEp_identity* alex_id = new_identity("pep.test.alexander@darthmama.org",
  1484                                         NULL,
  1485                                         "AlexID",
  1486                                         "Alexander Braithwaite");
  1487 
  1488     pEp_identity* alex_id2 = new_identity("pep.test.alexander6@darthmama.org",
  1489                                           NULL,
  1490                                           "AlexID",
  1491                                           "Alexander Braithwaite");
  1492 
  1493     pEp_identity* alex_id3 = new_identity("pep.test.alexander6a@darthmama.org",
  1494                                           NULL,
  1495                                           "AlexID",
  1496                                           "Alexander Braithwaite");
  1497 
  1498 
  1499     PEP_STATUS status = read_file_and_import_key(session, "test_keys/pub/pep.test.alexander6-0x0019697D_pub.asc");
  1500     status = read_file_and_import_key(session, "test_keys/pub/pep.test.alexander6-0x503B14D8_pub.asc");
  1501     status = read_file_and_import_key(session, "test_keys/pub/pep.test.alexander6-0xA216E95A_pub.asc");
  1502     status = read_file_and_import_key(session, "test_keys/priv/pep.test.alexander6-0x0019697D_priv.asc");
  1503     status = read_file_and_import_key(session, "test_keys/priv/pep.test.alexander6-0x503B14D8_priv.asc");
  1504     status = read_file_and_import_key(session, "test_keys/priv/pep.test.alexander6-0xA216E95A_priv.asc");
  1505 
  1506     alex_id->me = true;
  1507     status = set_own_key(session, alex_id, pubkey1);
  1508     ASSERT_EQ(status, PEP_STATUS_OK);
  1509     status = set_identity_flags(session, alex_id2, alex_id2->flags | PEP_idf_devicegroup);
  1510     ASSERT_EQ(status , PEP_STATUS_OK);
  1511 
  1512     alex_id2->me = true;
  1513     status = set_own_key(session, alex_id2, pubkey2);
  1514     ASSERT_EQ(status, PEP_STATUS_OK);
  1515     status = set_identity_flags(session, alex_id2, alex_id2->flags | PEP_idf_devicegroup);
  1516     ASSERT_EQ(status , PEP_STATUS_OK);
  1517 
  1518     alex_id3->me = true;
  1519     status = set_own_key(session, alex_id3, pubkey3);
  1520     ASSERT_EQ(status, PEP_STATUS_OK);
  1521     status = set_identity_flags(session, alex_id2, alex_id2->flags | PEP_idf_devicegroup);
  1522     ASSERT_EQ(status , PEP_STATUS_OK);
  1523 
  1524     status = myself(session, alex_id);
  1525     ASSERT_EQ(status, PEP_STATUS_OK);
  1526     ASSERT_STREQ(pubkey1, alex_id->fpr);
  1527 
  1528     status = myself(session, alex_id2);
  1529     ASSERT_EQ(status, PEP_STATUS_OK);
  1530     ASSERT_STREQ(pubkey2, alex_id2->fpr);
  1531 
  1532     status = myself(session, alex_id3);
  1533     ASSERT_EQ(status, PEP_STATUS_OK);
  1534     ASSERT_STREQ(pubkey3, alex_id3->fpr);
  1535 
  1536     message* dec_msg = NULL;
  1537     stringlist_t* keylist = NULL;
  1538     PEP_rating rating;
  1539     PEP_decrypt_flags_t flags = 0;
  1540 
  1541     string fname = "test_mails/check_reset_grouped_own_multiple_keys_multiple_idents_reset_one.eml";
  1542     string mailstr = slurp(fname.c_str());
  1543     message* new_msg = NULL;
  1544     status = mime_decode_message(mailstr.c_str(), mailstr.size(), &new_msg);
  1545     ASSERT_NE(new_msg, nullptr);
  1546     ASSERT_EQ(status, PEP_STATUS_OK);
  1547 
  1548     status = decrypt_message(session, new_msg, &dec_msg, &keylist, &rating, &flags);
  1549     ASSERT_EQ(status, PEP_STATUS_OK);        
  1550 
  1551     status = myself(session, alex_id);
  1552     ASSERT_EQ(status, PEP_STATUS_OK);
  1553     ASSERT_STREQ(pubkey1, alex_id->fpr);
  1554 
  1555     status = myself(session, alex_id2);
  1556     ASSERT_EQ(status, PEP_STATUS_OK);
  1557     ASSERT_STREQ(replkey2, alex_id2->fpr);
  1558 
  1559     status = myself(session, alex_id3);
  1560     ASSERT_EQ(status, PEP_STATUS_OK);
  1561     ASSERT_STREQ(pubkey3, alex_id3->fpr);
  1562 
  1563     free_identity(alex_id);
  1564     free_identity(alex_id2);
  1565     free_identity(alex_id3);
  1566 }
  1567 
  1568 
  1569 TEST_F(KeyResetMessageTest, check_reset_ident_other_pub_fpr) {
  1570     send_setup(); // lazy
  1571     pEp_identity* bob = new_identity("pep.test.bob@pep-project.org", NULL, bob_user_id.c_str(), NULL);
  1572     PEP_STATUS status = update_identity(session, bob);
  1573 
  1574     ASSERT_EQ(status , PEP_STATUS_OK);
  1575     ASSERT_TRUE(bob->fpr && bob->fpr[0]);
  1576     status = set_as_pEp_user(session, bob);
  1577     status = trust_personal_key(session, bob);
  1578 
  1579     status = update_identity(session, bob);
  1580     ASSERT_EQ(status , PEP_STATUS_OK);
  1581     ASSERT_EQ(bob->comm_type , PEP_ct_pEp);
  1582 
  1583     // Ok, let's reset it
  1584     status = key_reset_identity(session, bob, bob->fpr);
  1585     status = update_identity(session, bob);
  1586     ASSERT_EQ(status , PEP_STATUS_OK);
  1587     ASSERT_EQ(bob->comm_type , PEP_ct_key_not_found);
  1588     ASSERT_TRUE(!(bob->fpr) || !(bob->fpr[0]));
  1589     // TODO: import key, verify PEP_ct_OpenPGP_unconfirmed
  1590 }
  1591 
  1592 
  1593 // Corner case?
  1594 TEST_F(KeyResetMessageTest, check_reset_ident_other_priv_fpr) {
  1595     send_setup(); // lazy
  1596     // Also import Bob's private key, because that dude is a fool.
  1597     PEP_STATUS status = read_file_and_import_key(session, "test_keys/priv/pep-test-bob-0xC9C2EE39_priv.asc");
  1598     pEp_identity* bob = new_identity("pep.test.bob@pep-project.org", NULL, bob_user_id.c_str(), NULL);
  1599     status = update_identity(session, bob);
  1600 
  1601     ASSERT_EQ(status , PEP_STATUS_OK);
  1602     ASSERT_TRUE(bob->fpr && bob->fpr[0]);
  1603     ASSERT_FALSE(bob->me);
  1604 
  1605     status = set_as_pEp_user(session, bob);
  1606     status = trust_personal_key(session, bob);
  1607 
  1608     status = update_identity(session, bob);
  1609     ASSERT_EQ(status , PEP_STATUS_OK);
  1610     ASSERT_EQ(bob->comm_type , PEP_ct_pEp);
  1611     ASSERT_FALSE(bob->me);
  1612 
  1613     // Ok, let's reset it
  1614     status = key_reset_identity(session, bob, bob->fpr);
  1615     status = update_identity(session, bob);
  1616     ASSERT_EQ(status , PEP_STATUS_OK);
  1617     ASSERT_EQ(bob->comm_type , PEP_ct_key_not_found);
  1618     ASSERT_TRUE(!(bob->fpr) || !(bob->fpr[0]));
  1619 
  1620     // TODO: import key, verify PEP_ct_OpenPGP_unconfirmed
  1621 }
  1622 
  1623 
  1624 TEST_F(KeyResetMessageTest, check_reset_ident_other_pub_no_fpr) {
  1625     send_setup(); // lazy
  1626     pEp_identity* bob = new_identity("pep.test.bob@pep-project.org", NULL, bob_user_id.c_str(), NULL);
  1627     PEP_STATUS status = update_identity(session, bob);
  1628 
  1629     ASSERT_EQ(status , PEP_STATUS_OK);
  1630     ASSERT_TRUE(bob->fpr && bob->fpr[0]);
  1631     status = set_as_pEp_user(session, bob);
  1632     status = trust_personal_key(session, bob);
  1633 
  1634     status = update_identity(session, bob);
  1635     ASSERT_EQ(status , PEP_STATUS_OK);
  1636     ASSERT_EQ(bob->comm_type , PEP_ct_pEp);
  1637     free(bob->fpr);
  1638     bob->fpr = NULL;
  1639 
  1640     // Ok, let's reset it
  1641     status = key_reset_identity(session, bob, NULL);
  1642     status = update_identity(session, bob);
  1643     ASSERT_EQ(status , PEP_STATUS_OK);
  1644     ASSERT_EQ(bob->comm_type , PEP_ct_key_not_found);
  1645     ASSERT_TRUE(!(bob->fpr) || !(bob->fpr[0]));
  1646 
  1647     // TODO: import key, verify PEP_ct_OpenPGP_unconfirmed
  1648 }
  1649 //    const char* bob_fpr = "BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39";
  1650 // TODO: multiplr keys above
  1651 
  1652 
  1653 TEST_F(KeyResetMessageTest, check_reset_ident_other_priv_no_fpr) {
  1654     send_setup(); // lazy
  1655     // Also import Bob's private key, because that dude is a fool.
  1656     PEP_STATUS status = read_file_and_import_key(session, "test_keys/priv/pep-test-bob-0xC9C2EE39_priv.asc");
  1657     pEp_identity* bob = new_identity("pep.test.bob@pep-project.org", NULL, bob_user_id.c_str(), NULL);
  1658     status = update_identity(session, bob);
  1659 
  1660     ASSERT_EQ(status , PEP_STATUS_OK);
  1661     ASSERT_TRUE(bob->fpr && bob->fpr[0]);
  1662     status = set_as_pEp_user(session, bob);
  1663     status = trust_personal_key(session, bob);
  1664 
  1665     status = update_identity(session, bob);
  1666     ASSERT_EQ(status , PEP_STATUS_OK);
  1667     ASSERT_EQ(bob->comm_type , PEP_ct_pEp);
  1668     ASSERT_FALSE(bob->me);
  1669     free(bob->fpr);
  1670     bob->fpr = NULL;
  1671 
  1672     // Ok, let's reset it
  1673     status = key_reset_identity(session, bob, NULL);
  1674     status = update_identity(session, bob);
  1675     ASSERT_EQ(status , PEP_STATUS_OK);
  1676     ASSERT_EQ(bob->comm_type , PEP_ct_key_not_found);
  1677     ASSERT_TRUE(!(bob->fpr) || !(bob->fpr[0]));
  1678     ASSERT_FALSE(bob->me);
  1679 
  1680     // TODO: import key, verify PEP_ct_OpenPGP_unconfirmed
  1681 }
  1682 
  1683 
  1684 TEST_F(KeyResetMessageTest, check_reset_ident_own_pub_fpr) {
  1685     send_setup(); // lazy
  1686     pEp_identity* alice = new_identity("pep.test.alice@pep-project.org", NULL, alice_user_id.c_str(), NULL);
  1687     PEP_STATUS status = read_file_and_import_key(session, "test_keys/pub/pep.test.alexander-0x26B54E4E_pub.asc");
  1688 
  1689     // hacky
  1690     alice->fpr = strdup("3AD9F60FAEB22675DB873A1362D6981326B54E4E");
  1691     status = set_pgp_keypair(session, alice->fpr);
  1692     ASSERT_EQ(status , PEP_STATUS_OK);
  1693     alice->comm_type = PEP_ct_OpenPGP;
  1694     status = set_trust(session, alice);
  1695     ASSERT_EQ(status , PEP_STATUS_OK);
  1696 
  1697     // Ok, let's reset it
  1698     status = key_reset_identity(session, alice, alice->fpr);
  1699     status = myself(session, alice);
  1700     ASSERT_EQ(status , PEP_STATUS_OK);
  1701 
  1702     ASSERT_TRUE(alice->me);
  1703     ASSERT_NE(alice->fpr, nullptr);
  1704     ASSERT_STREQ(alice->fpr, alice_fpr);
  1705     ASSERT_EQ(alice->comm_type , PEP_ct_pEp);
  1706 
  1707     free(alice->fpr);
  1708     alice->fpr = strdup("3AD9F60FAEB22675DB873A1362D6981326B54E4E");
  1709     status = get_trust(session, alice);
  1710     ASSERT_EQ(status , PEP_CANNOT_FIND_IDENTITY);
  1711 }
  1712 
  1713 
  1714 TEST_F(KeyResetMessageTest, check_reset_ident_own_priv_fpr) {
  1715     send_setup(); // lazy
  1716     pEp_identity* alice = new_identity("pep.test.alice@pep-project.org", NULL, alice_user_id.c_str(), NULL);
  1717     PEP_STATUS status = myself(session, alice);
  1718 
  1719     ASSERT_EQ(status , PEP_STATUS_OK);
  1720     ASSERT_TRUE(alice->fpr && alice->fpr[0]);
  1721     ASSERT_TRUE(alice->me);
  1722     ASSERT_STREQ(alice->fpr, alice_fpr);
  1723 
  1724     status = key_reset_identity(session, alice, alice_fpr);
  1725     status = myself(session, alice);
  1726     ASSERT_EQ(status , PEP_STATUS_OK);
  1727     char* alice_new_fpr = alice->fpr;
  1728     ASSERT_TRUE(alice_new_fpr && alice_new_fpr[0]);
  1729     ASSERT_STRNE(alice_fpr, alice_new_fpr);
  1730 }
  1731 
  1732 
  1733 TEST_F(KeyResetMessageTest, check_reset_ident_own_priv_no_fpr) {
  1734     send_setup(); // lazy
  1735     pEp_identity* alice = new_identity("pep.test.alice@pep-project.org", NULL, alice_user_id.c_str(), NULL);
  1736     PEP_STATUS status = myself(session, alice);
  1737 
  1738     ASSERT_EQ(status , PEP_STATUS_OK);
  1739     ASSERT_TRUE(alice->fpr && alice->fpr[0]);
  1740     ASSERT_TRUE(alice->me);
  1741     ASSERT_STREQ(alice->fpr, alice_fpr);
  1742     free(alice->fpr);
  1743     alice->fpr = NULL;
  1744     status = key_reset_identity(session, alice, NULL);
  1745     status = myself(session, alice);
  1746     ASSERT_EQ(status , PEP_STATUS_OK);
  1747     char* alice_new_fpr = alice->fpr;
  1748     ASSERT_TRUE(alice_new_fpr && alice_new_fpr[0]);
  1749     ASSERT_STRNE(alice_fpr, alice_new_fpr);
  1750 }
  1751 
  1752 
  1753 TEST_F(KeyResetMessageTest, check_reset_user_other_no_fpr) {
  1754       char* pubkey1 = strdup("74D79B4496E289BD8A71B70BA8E2C4530019697D");
  1755       char* pubkey2 = strdup("2E21325D202A44BFD9C607FCF095B202503B14D8");
  1756       char* pubkey3 = strdup("3C1E713D8519D7F907E3142D179EAA24A216E95A");
  1757       char* pubkey4 = strdup("B4CE2F6947B6947C500F0687AEFDE530BDA17020");
  1758 
  1759       pEp_identity* alex_id = new_identity("pep.test.alexander@darthmama.org",
  1760                                             NULL,
  1761                                             "AlexID",
  1762                                             "Alexander Braithwaite");
  1763 
  1764 /*
  1765 test_keys/pub/pep.test.alexander6-0x0019697D_pub.asc
  1766 test_keys/pub/pep.test.alexander6-0x503B14D8_pub.asc
  1767 test_keys/pub/pep.test.alexander6-0xA216E95A_pub.asc
  1768 test_keys/pub/pep.test.alexander6-0xBDA17020_pub.asc
  1769 */
  1770     PEP_STATUS status = read_file_and_import_key(session, "test_keys/pub/pep.test.alexander6-0x0019697D_pub.asc");
  1771     status = read_file_and_import_key(session, "test_keys/pub/pep.test.alexander6-0x503B14D8_pub.asc");
  1772     status = read_file_and_import_key(session, "test_keys/pub/pep.test.alexander6-0xA216E95A_pub.asc");
  1773     status = read_file_and_import_key(session, "test_keys/pub/pep.test.alexander6-0xBDA17020_pub.asc");
  1774 
  1775     free(alex_id->fpr);
  1776     alex_id->fpr = strdup(pubkey1);
  1777     status = trust_personal_key(session, alex_id);
  1778     free(alex_id->fpr);
  1779     alex_id->fpr = strdup(pubkey3);
  1780     status = trust_personal_key(session, alex_id);
  1781     status = set_as_pEp_user(session, alex_id);
  1782     free(alex_id->fpr);
  1783     alex_id->fpr = strdup(pubkey4);
  1784     status = trust_personal_key(session, alex_id);
  1785 
  1786     status = key_reset_user(session, alex_id->user_id, NULL);
  1787 
  1788     stringlist_t* keylist = NULL;
  1789 
  1790     free(alex_id->fpr);
  1791     alex_id->fpr = strdup(pubkey1);
  1792     status = get_trust(session, alex_id);
  1793     ASSERT_EQ(alex_id->comm_type , PEP_ct_unknown);
  1794     status = find_keys(session, pubkey1, &keylist);
  1795     ASSERT_TRUE(status == PEP_GET_KEY_FAILED || !keylist || EMPTYSTR(keylist->value));
  1796 
  1797     free(alex_id->fpr);
  1798     alex_id->fpr = strdup(pubkey2);
  1799     status = get_trust(session, alex_id);
  1800     ASSERT_EQ(alex_id->comm_type , PEP_ct_unknown);
  1801     status = find_keys(session, pubkey2, &keylist);
  1802     ASSERT_TRUE(status == PEP_STATUS_OK && keylist && !EMPTYSTR(keylist->value));
  1803 
  1804     free(alex_id->fpr);
  1805     alex_id->fpr = strdup(pubkey3);
  1806     status = get_trust(session, alex_id);
  1807     ASSERT_EQ(alex_id->comm_type , PEP_ct_unknown);
  1808     status = find_keys(session, pubkey3, &keylist);
  1809     ASSERT_TRUE(status == PEP_GET_KEY_FAILED || !keylist || EMPTYSTR(keylist->value));
  1810 
  1811     free(alex_id->fpr);
  1812     alex_id->fpr = strdup(pubkey4);
  1813     status = get_trust(session, alex_id);
  1814     ASSERT_EQ(alex_id->comm_type , PEP_ct_unknown);
  1815     status = find_keys(session, pubkey4, &keylist);
  1816     ASSERT_TRUE(status == PEP_GET_KEY_FAILED || !keylist || EMPTYSTR(keylist->value));
  1817 
  1818 }
  1819 
  1820 
  1821 TEST_F(KeyResetMessageTest, check_reset_user_other_fpr) {
  1822       char* pubkey1 = strdup("74D79B4496E289BD8A71B70BA8E2C4530019697D");
  1823       char* pubkey2 = strdup("2E21325D202A44BFD9C607FCF095B202503B14D8");
  1824       char* pubkey3 = strdup("3C1E713D8519D7F907E3142D179EAA24A216E95A");
  1825       char* pubkey4 = strdup("B4CE2F6947B6947C500F0687AEFDE530BDA17020");
  1826 
  1827       pEp_identity* alex_id = new_identity("pep.test.alexander@darthmama.org",
  1828                                             NULL,
  1829                                             "AlexID",
  1830                                             "Alexander Braithwaite");
  1831 
  1832 /*
  1833 test_keys/pub/pep.test.alexander6-0x0019697D_pub.asc
  1834 test_keys/pub/pep.test.alexander6-0x503B14D8_pub.asc
  1835 test_keys/pub/pep.test.alexander6-0xA216E95A_pub.asc
  1836 test_keys/pub/pep.test.alexander6-0xBDA17020_pub.asc
  1837 */
  1838     PEP_STATUS status = read_file_and_import_key(session, "test_keys/pub/pep.test.alexander6-0x0019697D_pub.asc");
  1839     status = read_file_and_import_key(session, "test_keys/pub/pep.test.alexander6-0x503B14D8_pub.asc");
  1840     status = read_file_and_import_key(session, "test_keys/pub/pep.test.alexander6-0xA216E95A_pub.asc");
  1841     status = read_file_and_import_key(session, "test_keys/pub/pep.test.alexander6-0xBDA17020_pub.asc");
  1842 
  1843     free(alex_id->fpr);
  1844     alex_id->fpr = strdup(pubkey1);
  1845     status = trust_personal_key(session, alex_id);
  1846     free(alex_id->fpr);
  1847     alex_id->fpr = strdup(pubkey3);
  1848     status = trust_personal_key(session, alex_id);
  1849     status = set_as_pEp_user(session, alex_id);
  1850     free(alex_id->fpr);
  1851     alex_id->fpr = strdup(pubkey4);
  1852     status = trust_personal_key(session, alex_id);
  1853 
  1854     status = key_reset_user(session, alex_id->user_id, pubkey3);
  1855 
  1856     stringlist_t* keylist = NULL;
  1857 
  1858     free(alex_id->fpr);
  1859     alex_id->fpr = strdup(pubkey1);
  1860     status = get_trust(session, alex_id);
  1861     ASSERT_EQ(alex_id->comm_type , PEP_ct_pEp);
  1862     status = find_keys(session, pubkey1, &keylist);
  1863     ASSERT_TRUE(status == PEP_STATUS_OK && keylist && !EMPTYSTR(keylist->value));
  1864 
  1865     free_stringlist(keylist);
  1866     keylist = NULL;
  1867 
  1868     free(alex_id->fpr);
  1869     alex_id->fpr = strdup(pubkey2);
  1870     status = get_trust(session, alex_id);
  1871     ASSERT_EQ(alex_id->comm_type , PEP_ct_unknown);
  1872     status = find_keys(session, pubkey2, &keylist);
  1873     ASSERT_TRUE(status == PEP_STATUS_OK && keylist && !EMPTYSTR(keylist->value));
  1874 
  1875     free(alex_id->fpr);
  1876     alex_id->fpr = strdup(pubkey3);
  1877     status = get_trust(session, alex_id);
  1878     ASSERT_EQ(alex_id->comm_type , PEP_ct_unknown);
  1879     status = find_keys(session, pubkey3, &keylist);
  1880     ASSERT_TRUE(status == PEP_GET_KEY_FAILED || !keylist || EMPTYSTR(keylist->value));
  1881 
  1882     free(alex_id->fpr);
  1883     alex_id->fpr = strdup(pubkey4);
  1884     status = get_trust(session, alex_id);
  1885     ASSERT_EQ(alex_id->comm_type , PEP_ct_pEp);
  1886     status = find_keys(session, pubkey4, &keylist);
  1887     ASSERT_TRUE(status == PEP_STATUS_OK && keylist && !EMPTYSTR(keylist->value));
  1888 
  1889     // next line is for readability.
  1890     alex_id->fpr = NULL;
  1891     free_stringlist(keylist);
  1892     free(pubkey1);
  1893     free(pubkey2);
  1894     free(pubkey3);
  1895     free(pubkey4);
  1896     free_identity(alex_id);
  1897 }
  1898 
  1899 
  1900 TEST_F(KeyResetMessageTest, check_reset_user_own_fpr) {
  1901       char* pubkey1 = strdup("74D79B4496E289BD8A71B70BA8E2C4530019697D");
  1902       char* pubkey2 = strdup("2E21325D202A44BFD9C607FCF095B202503B14D8");
  1903       char* pubkey3 = strdup("3C1E713D8519D7F907E3142D179EAA24A216E95A");
  1904       char* pubkey4 = strdup("B4CE2F6947B6947C500F0687AEFDE530BDA17020");
  1905 
  1906       pEp_identity* alex_id = new_identity("pep.test.alexander@darthmama.org",
  1907                                             NULL,
  1908                                             "AlexID",
  1909                                             "Alexander Braithwaite");
  1910 
  1911 /*
  1912 test_keys/pub/pep.test.alexander6-0x0019697D_pub.asc
  1913 test_keys/pub/pep.test.alexander6-0x503B14D8_pub.asc
  1914 test_keys/pub/pep.test.alexander6-0xA216E95A_pub.asc
  1915 test_keys/pub/pep.test.alexander6-0xBDA17020_pub.asc
  1916 */
  1917     PEP_STATUS status = read_file_and_import_key(session, "test_keys/pub/pep.test.alexander6-0x0019697D_pub.asc");
  1918     status = read_file_and_import_key(session, "test_keys/pub/pep.test.alexander6-0x503B14D8_pub.asc");
  1919     status = read_file_and_import_key(session, "test_keys/pub/pep.test.alexander6-0xA216E95A_pub.asc");
  1920     status = read_file_and_import_key(session, "test_keys/pub/pep.test.alexander6-0xBDA17020_pub.asc");
  1921     status = read_file_and_import_key(session, "test_keys/priv/pep.test.alexander6-0x0019697D_priv.asc");
  1922     status = read_file_and_import_key(session, "test_keys/priv/pep.test.alexander6-0x503B14D8_priv.asc");
  1923     status = read_file_and_import_key(session, "test_keys/priv/pep.test.alexander6-0xA216E95A_priv.asc");
  1924     status = read_file_and_import_key(session, "test_keys/priv/pep.test.alexander6-0xBDA17020_priv.asc");
  1925 
  1926     alex_id->me = true;
  1927     status = set_own_key(session, alex_id, pubkey1);
  1928     status = set_own_key(session, alex_id, pubkey4);
  1929     status = set_own_key(session, alex_id, pubkey3);
  1930 
  1931     status = myself(session, alex_id);
  1932 
  1933     status = key_reset_user(session, alex_id->user_id, pubkey3);
  1934 
  1935     free(alex_id->fpr);
  1936     alex_id->fpr = strdup(pubkey1);
  1937     status = get_trust(session, alex_id);
  1938     ASSERT_EQ(alex_id->comm_type , PEP_ct_pEp);
  1939 
  1940     free(alex_id->fpr);
  1941     alex_id->fpr = strdup(pubkey2);
  1942     status = get_trust(session, alex_id);
  1943     ASSERT_EQ(alex_id->comm_type , PEP_ct_unknown);
  1944 
  1945     stringlist_t* keylist = NULL;
  1946 
  1947     free(alex_id->fpr);
  1948     alex_id->fpr = strdup(pubkey3);
  1949     status = get_trust(session, alex_id);
  1950     ASSERT_EQ(alex_id->comm_type , PEP_ct_mistrusted);
  1951     status = find_keys(session, pubkey4, &keylist);
  1952     ASSERT_TRUE(status == PEP_STATUS_OK && keylist && !EMPTYSTR(keylist->value));
  1953 
  1954     free_stringlist(keylist);
  1955     keylist = NULL;
  1956 
  1957     free(alex_id->fpr);
  1958     alex_id->fpr = strdup(pubkey4);
  1959     status = get_trust(session, alex_id);
  1960     ASSERT_EQ(alex_id->comm_type , PEP_ct_pEp);
  1961 
  1962     // next line is for readability.
  1963     alex_id->fpr = NULL;
  1964     free_stringlist(keylist);
  1965     free(pubkey1);
  1966     free(pubkey2);
  1967     free(pubkey3);
  1968     free(pubkey4);
  1969     free_identity(alex_id);
  1970 }
  1971 
  1972 
  1973 TEST_F(KeyResetMessageTest, check_reset_user_no_fpr) {
  1974       char* pubkey1 = strdup("74D79B4496E289BD8A71B70BA8E2C4530019697D");
  1975       char* pubkey2 = strdup("2E21325D202A44BFD9C607FCF095B202503B14D8");
  1976       char* pubkey3 = strdup("3C1E713D8519D7F907E3142D179EAA24A216E95A");
  1977       char* pubkey4 = strdup("B4CE2F6947B6947C500F0687AEFDE530BDA17020");
  1978 
  1979       pEp_identity* alex_id = new_identity("pep.test.alexander@darthmama.org",
  1980                                             NULL,
  1981                                             "AlexID",
  1982                                             "Alexander Braithwaite");
  1983 
  1984 /*
  1985 test_keys/pub/pep.test.alexander6-0x0019697D_pub.asc
  1986 test_keys/pub/pep.test.alexander6-0x503B14D8_pub.asc
  1987 test_keys/pub/pep.test.alexander6-0xA216E95A_pub.asc
  1988 test_keys/pub/pep.test.alexander6-0xBDA17020_pub.asc
  1989 */
  1990     PEP_STATUS status = read_file_and_import_key(session, "test_keys/pub/pep.test.alexander6-0x0019697D_pub.asc");
  1991     status = read_file_and_import_key(session, "test_keys/pub/pep.test.alexander6-0x503B14D8_pub.asc");
  1992     status = read_file_and_import_key(session, "test_keys/pub/pep.test.alexander6-0xA216E95A_pub.asc");
  1993     status = read_file_and_import_key(session, "test_keys/pub/pep.test.alexander6-0xBDA17020_pub.asc");
  1994     status = read_file_and_import_key(session, "test_keys/priv/pep.test.alexander6-0x0019697D_priv.asc");
  1995     status = read_file_and_import_key(session, "test_keys/priv/pep.test.alexander6-0x503B14D8_priv.asc");
  1996     status = read_file_and_import_key(session, "test_keys/priv/pep.test.alexander6-0xA216E95A_priv.asc");
  1997     status = read_file_and_import_key(session, "test_keys/priv/pep.test.alexander6-0xBDA17020_priv.asc");
  1998 
  1999     alex_id->me = true;
  2000     status = set_own_key(session, alex_id, pubkey1);
  2001     status = set_own_key(session, alex_id, pubkey3);
  2002     status = set_own_key(session, alex_id, pubkey4);
  2003 
  2004     status = key_reset_user(session, alex_id->user_id, NULL);
  2005 
  2006     ASSERT_EQ(status , PEP_ILLEGAL_VALUE);
  2007 
  2008     free(pubkey1);
  2009     free(pubkey2);
  2010     free(pubkey3);
  2011     free(pubkey4);
  2012     free_identity(alex_id);
  2013 }
  2014 
  2015 
  2016 TEST_F(KeyResetMessageTest, check_reset_all_own_keys) {
  2017     char* pubkey1 = strdup("74D79B4496E289BD8A71B70BA8E2C4530019697D");
  2018     char* pubkey2 = strdup("2E21325D202A44BFD9C607FCF095B202503B14D8");
  2019     char* pubkey3 = strdup("3C1E713D8519D7F907E3142D179EAA24A216E95A");
  2020     char* pubkey4 = strdup("B4CE2F6947B6947C500F0687AEFDE530BDA17020");
  2021 
  2022     pEp_identity* alex_id = new_identity("pep.test.alexander@darthmama.org",
  2023                                          NULL,
  2024                                          "AlexID",
  2025                                          "Alexander Braithwaite");
  2026 
  2027     pEp_identity* alex_id2 = new_identity("pep.test.alexander6@darthmama.org",
  2028                                           NULL,
  2029                                           "AlexID",
  2030                                           "Alexander Braithwaite");
  2031 
  2032 
  2033     PEP_STATUS status = read_file_and_import_key(session, "test_keys/pub/pep.test.alexander6-0x0019697D_pub.asc");
  2034     status = read_file_and_import_key(session, "test_keys/pub/pep.test.alexander6-0x503B14D8_pub.asc");
  2035     status = read_file_and_import_key(session, "test_keys/pub/pep.test.alexander6-0xA216E95A_pub.asc");
  2036     status = read_file_and_import_key(session, "test_keys/pub/pep.test.alexander6-0xBDA17020_pub.asc");
  2037     status = read_file_and_import_key(session, "test_keys/priv/pep.test.alexander6-0x0019697D_priv.asc");
  2038     status = read_file_and_import_key(session, "test_keys/priv/pep.test.alexander6-0x503B14D8_priv.asc");
  2039     status = read_file_and_import_key(session, "test_keys/priv/pep.test.alexander6-0xA216E95A_priv.asc");
  2040     status = read_file_and_import_key(session, "test_keys/priv/pep.test.alexander6-0xBDA17020_priv.asc");
  2041 
  2042     alex_id->me = true;
  2043     status = set_own_key(session, alex_id, pubkey3);
  2044     status = myself(session, alex_id);
  2045     status = set_own_key(session, alex_id, pubkey1);
  2046     status = myself(session, alex_id);
  2047 
  2048     alex_id2->me = true;
  2049     status = set_own_key(session, alex_id2, pubkey4);
  2050     status = myself(session, alex_id2);
  2051 
  2052     status = key_reset_all_own_keys(session);
  2053 
  2054     stringlist_t* keylist = NULL;
  2055 
  2056     free(alex_id->fpr);
  2057     alex_id->fpr = strdup(pubkey1);
  2058     status = get_trust(session, alex_id);
  2059     ASSERT_EQ(alex_id->comm_type , PEP_ct_mistrusted);
  2060     status = find_keys(session, pubkey1, &keylist);
  2061     ASSERT_TRUE(status == PEP_STATUS_OK && keylist && !EMPTYSTR(keylist->value));
  2062 
  2063     free_stringlist(keylist);
  2064     keylist = NULL;
  2065 
  2066     free(alex_id->fpr);
  2067     alex_id->fpr = strdup(pubkey2);
  2068     status = get_trust(session, alex_id);
  2069     ASSERT_EQ(alex_id->comm_type , PEP_ct_unknown);
  2070     status = find_keys(session, pubkey2, &keylist);
  2071     ASSERT_TRUE(status == PEP_STATUS_OK && keylist && !EMPTYSTR(keylist->value));
  2072 
  2073     free_stringlist(keylist);
  2074     keylist = NULL;
  2075 
  2076     free(alex_id->fpr);
  2077     alex_id->fpr = strdup(pubkey3);
  2078     status = get_trust(session, alex_id);
  2079     ASSERT_EQ(alex_id->comm_type , PEP_ct_pEp);
  2080     status = find_keys(session, pubkey3, &keylist);
  2081     ASSERT_TRUE(status == PEP_STATUS_OK && keylist && !EMPTYSTR(keylist->value));
  2082 
  2083     free_stringlist(keylist);
  2084     keylist = NULL;
  2085 
  2086     free(alex_id2->fpr);
  2087     alex_id2->fpr = strdup(pubkey4);
  2088     status = get_trust(session, alex_id2);
  2089     ASSERT_EQ(alex_id2->comm_type , PEP_ct_mistrusted);
  2090     status = find_keys(session, pubkey4, &keylist);
  2091     ASSERT_TRUE(status == PEP_STATUS_OK && keylist && !EMPTYSTR(keylist->value));
  2092 
  2093     free_stringlist(keylist);
  2094     keylist = NULL;
  2095 
  2096     alex_id->fpr = NULL;
  2097     status = myself(session, alex_id);
  2098     ASSERT_EQ(status , PEP_STATUS_OK);
  2099 
  2100     ASSERT_NE(alex_id->fpr, nullptr);
  2101     output_stream << "alex_id->fpr is " << alex_id->fpr << endl;
  2102     ASSERT_STRNE(alex_id->fpr, pubkey1);
  2103     ASSERT_STRNE(alex_id->fpr, pubkey2);
  2104     ASSERT_STRNE(alex_id->fpr, pubkey3);
  2105     ASSERT_STRNE(alex_id->fpr, pubkey4);
  2106     ASSERT_EQ(alex_id->comm_type , PEP_ct_pEp);
  2107 
  2108     alex_id2->fpr = NULL;
  2109     status = myself(session, alex_id2);
  2110     ASSERT_EQ(status , PEP_STATUS_OK);
  2111     ASSERT_STRNE(alex_id2->fpr, pubkey1);
  2112     ASSERT_STRNE(alex_id2->fpr, pubkey2);
  2113     ASSERT_STRNE(alex_id2->fpr, pubkey3);
  2114     ASSERT_STRNE(alex_id2->fpr, pubkey4);
  2115 
  2116     free(pubkey1);
  2117     free(pubkey2);
  2118     free(pubkey3);
  2119     free(pubkey4);
  2120     free_identity(alex_id);
  2121 }
  2122 
  2123 
  2124 TEST_F(KeyResetMessageTest, check_reset_all_own_no_own) {
  2125       char* pubkey1 = strdup("74D79B4496E289BD8A71B70BA8E2C4530019697D");
  2126       char* pubkey2 = strdup("2E21325D202A44BFD9C607FCF095B202503B14D8");
  2127       char* pubkey3 = strdup("3C1E713D8519D7F907E3142D179EAA24A216E95A");
  2128       char* pubkey4 = strdup("B4CE2F6947B6947C500F0687AEFDE530BDA17020");
  2129 
  2130       pEp_identity* alex_id = new_identity("pep.test.alexander@darthmama.org",
  2131                                             NULL,
  2132                                             "AlexID",
  2133                                             "Alexander Braithwaite");
  2134 
  2135 /*
  2136 test_keys/pub/pep.test.alexander6-0x0019697D_pub.asc
  2137 test_keys/pub/pep.test.alexander6-0x503B14D8_pub.asc
  2138 test_keys/pub/pep.test.alexander6-0xA216E95A_pub.asc
  2139 test_keys/pub/pep.test.alexander6-0xBDA17020_pub.asc
  2140 */
  2141     PEP_STATUS status = read_file_and_import_key(session, "test_keys/pub/pep.test.alexander6-0x0019697D_pub.asc");
  2142     status = read_file_and_import_key(session, "test_keys/pub/pep.test.alexander6-0x503B14D8_pub.asc");
  2143     status = read_file_and_import_key(session, "test_keys/pub/pep.test.alexander6-0xA216E95A_pub.asc");
  2144     status = read_file_and_import_key(session, "test_keys/pub/pep.test.alexander6-0xBDA17020_pub.asc");
  2145 
  2146     free(alex_id->fpr);
  2147     alex_id->fpr = strdup(pubkey1);
  2148     status = trust_personal_key(session, alex_id);
  2149     free(alex_id->fpr);
  2150     alex_id->fpr = strdup(pubkey3);
  2151     status = trust_personal_key(session, alex_id);
  2152     free(alex_id->fpr);
  2153     alex_id->fpr = strdup(pubkey4);
  2154     status = trust_personal_key(session, alex_id);
  2155 
  2156     status = key_reset_all_own_keys(session);
  2157     ASSERT_EQ(status , PEP_CANNOT_FIND_IDENTITY);
  2158 
  2159     stringlist_t* keylist = NULL;
  2160 
  2161     free(alex_id->fpr);
  2162     alex_id->fpr = strdup(pubkey1);
  2163     status = get_trust(session, alex_id);
  2164     ASSERT_EQ(alex_id->comm_type , PEP_ct_OpenPGP);
  2165     status = find_keys(session, pubkey1, &keylist);
  2166     ASSERT_TRUE(status == PEP_STATUS_OK && keylist && !EMPTYSTR(keylist->value));
  2167 
  2168     free_stringlist(keylist);
  2169         keylist = NULL;
  2170 
  2171     free(alex_id->fpr);
  2172     alex_id->fpr = strdup(pubkey2);
  2173     status = get_trust(session, alex_id);
  2174     ASSERT_EQ(alex_id->comm_type , PEP_ct_unknown);
  2175     status = find_keys(session, pubkey2, &keylist);
  2176     ASSERT_TRUE(status == PEP_STATUS_OK && keylist && !EMPTYSTR(keylist->value));
  2177 
  2178     free(alex_id->fpr);
  2179     alex_id->fpr = strdup(pubkey3);
  2180     status = get_trust(session, alex_id);
  2181     ASSERT_EQ(alex_id->comm_type , PEP_ct_OpenPGP);
  2182     status = find_keys(session, pubkey3, &keylist);
  2183     ASSERT_TRUE(status == PEP_STATUS_OK && keylist && !EMPTYSTR(keylist->value));
  2184 
  2185     free(alex_id->fpr);
  2186     alex_id->fpr = strdup(pubkey4);
  2187     status = get_trust(session, alex_id);
  2188     ASSERT_EQ(alex_id->comm_type , PEP_ct_OpenPGP);
  2189     status = find_keys(session, pubkey4, &keylist);
  2190     ASSERT_TRUE(status == PEP_STATUS_OK && keylist && !EMPTYSTR(keylist->value));
  2191 
  2192     // next line is for readability.
  2193     alex_id->fpr = NULL;
  2194     free_stringlist(keylist);
  2195     free(pubkey1);
  2196     free(pubkey2);
  2197     free(pubkey3);
  2198     free(pubkey4);
  2199     free_identity(alex_id);
  2200 
  2201 }
  2202 
  2203 // TEST_F(KeyResetMessageTest, check_reset_mistrust_next_msg_have_mailed) {
  2204 //
  2205 // }
  2206 
  2207 TEST_F(KeyResetMessageTest, not_a_test) {
  2208     pEp_identity* bob = NULL;
  2209     PEP_STATUS status = set_up_preset(session, BOB,
  2210                                       true, true, true, true, true, &bob);
  2211 
  2212     const char* carol_fpr = "8DD4F5827B45839E9ACCA94687BDDFFB42A85A42";
  2213     slurp_and_import_key(session, "test_keys/pub/pep-test-bob-0xC9C2EE39_pub.asc");
  2214     slurp_and_import_key(session, "test_keys/pub/pep-test-carol-0x42A85A42_pub.asc");
  2215 
  2216     pEp_identity* carol = new_identity("pep-test-carol@pep-project.org", carol_fpr, carol_user_id.c_str(), "Christmas Carol");
  2217     status = update_identity(session, carol);
  2218 
  2219     message* bob_msg = new_message(PEP_dir_outgoing);
  2220     bob_msg->from = identity_dup(bob);
  2221     bob_msg->to = new_identity_list(carol);
  2222     bob_msg->shortmsg = strdup("Engine bugs suck\n");
  2223     bob_msg->longmsg = strdup("Everything is the engine's fault.\n");
  2224 
  2225     char* enc_msg_str = NULL;
  2226     message* enc_msg = NULL;
  2227 
  2228     status = encrypt_message(session, bob_msg, NULL, &enc_msg, PEP_enc_PGP_MIME, 0);
  2229     ASSERT_EQ(status, PEP_STATUS_OK);
  2230     status = mime_encode_message(enc_msg, false, &enc_msg_str);
  2231 
  2232     ofstream myfile;
  2233     myfile.open("test_mails/ENGINE-654_bob_mail.eml");
  2234     myfile << enc_msg_str;
  2235     myfile.close();
  2236 }
  2237 
  2238 
  2239 TEST_F(KeyResetMessageTest, check_no_reset_message_to_self) {
  2240     pEp_identity* bob = NULL;
  2241     PEP_STATUS status = set_up_preset(session, BOB,
  2242                                       true, true, true, true, true, &bob);
  2243 
  2244     slurp_and_import_key(session, "test_keys/pub/pep-test-bob-0xC9C2EE39_pub.asc");
  2245 
  2246     message* bob_msg = new_message(PEP_dir_outgoing);
  2247     bob_msg->from = identity_dup(bob);
  2248     bob_msg->to = new_identity_list(identity_dup(bob));
  2249     bob_msg->shortmsg = strdup("Engine bugs suck\n");
  2250     bob_msg->longmsg = strdup("Everything is the engine's fault.\n");
  2251 
  2252     message* enc_msg = NULL;
  2253 
  2254     status = encrypt_message(session, bob_msg, NULL, &enc_msg, PEP_enc_PGP_MIME, 0);
  2255     ASSERT_EQ(status, PEP_STATUS_OK);
  2256 
  2257     key_reset_all_own_keys(session);
  2258 
  2259     message* dec_msg = NULL;
  2260     stringlist_t* keylist = NULL;
  2261     PEP_rating rating;
  2262     PEP_decrypt_flags_t flags = 0;
  2263 
  2264     status = decrypt_message(session, enc_msg, &dec_msg, &keylist, &rating, &flags);
  2265     ASSERT_EQ(m_queue.size(), 0);
  2266     ASSERT_EQ(status, PEP_VERIFY_SIGNER_KEY_REVOKED);
  2267 }
  2268 
  2269 
  2270 TEST_F(KeyResetMessageTest, check_reset_mistrust_next_msg_have_not_mailed) {
  2271     pEp_identity* carol = NULL;
  2272     PEP_STATUS status = set_up_preset(session, CAROL,
  2273                                       true, true, true, true, true, &carol);
  2274 
  2275     status = myself(session, carol);
  2276     ASSERT_STREQ(carol->fpr, "8DD4F5827B45839E9ACCA94687BDDFFB42A85A42");
  2277 
  2278     slurp_and_import_key(session, "test_keys/pub/pep-test-bob-0xC9C2EE39_pub.asc");
  2279     pEp_identity* bob = new_identity("pep.test.bob@pep-project.org", bob_fpr, NULL, "Bob's Burgers");
  2280     status = update_identity(session, bob);
  2281 
  2282     status = key_mistrusted(session, bob);
  2283     ASSERT_EQ(status, PEP_STATUS_OK);
  2284 //    ASSERT_EQ(bob->fpr, nullptr);
  2285 
  2286     string mail_from_bob = slurp("test_mails/ENGINE-654_bob_mail.eml");
  2287     //
  2288     // // Ok, so let's see if the thing is mistrusted
  2289     message* bob_enc_msg = NULL;
  2290     //
  2291     // mime_decode_message(mail_from_bob.c_str(), mail_from_bob.size(), &bob_enc_msg);
  2292     //
  2293     message* bob_dec_msg = NULL;
  2294     stringlist_t* keylist = NULL;
  2295     PEP_rating rating;
  2296     PEP_decrypt_flags_t flags = 0;
  2297     //
  2298     // status = decrypt_message(session, bob_enc_msg, &bob_dec_msg, &keylist, &rating, &flags);
  2299     // ASSERT_EQ(status, PEP_STATUS_OK);
  2300     // ASSERT_EQ(rating, PEP_rating_mistrust);
  2301     //
  2302     // free_message(bob_enc_msg);
  2303     // free_message(bob_dec_msg);
  2304 
  2305     free(bob->fpr);
  2306     bob->fpr = NULL;
  2307 
  2308     status = key_reset_identity(session, bob, NULL);
  2309     ASSERT_EQ(status, PEP_STATUS_OK);
  2310 
  2311     // status = identity_rating(session, bob, &rating);
  2312     // status = update_identity(session, bob);
  2313     status = identity_rating(session, bob, &rating);
  2314     ASSERT_EQ(rating, PEP_rating_have_no_key);
  2315     //update_identity(session, bob);
  2316             //    ASSERT_EQ(bob->fpr, nullptr);
  2317 
  2318     mime_decode_message(mail_from_bob.c_str(), mail_from_bob.size(), &bob_enc_msg);
  2319 
  2320     bob_dec_msg = NULL;
  2321     free_stringlist(keylist);
  2322     keylist = NULL;
  2323     flags = 0;
  2324 
  2325     status = decrypt_message(session, bob_enc_msg, &bob_dec_msg, &keylist, &rating, &flags);
  2326     ASSERT_EQ(status, PEP_STATUS_OK);
  2327     ASSERT_EQ(rating, PEP_rating_reliable);
  2328 
  2329 }
  2330 /*
  2331 TEST_F(KeyResetMessageTest, check_reset_own_with_revocations) {
  2332     pEp_identity* id1 = new_identity("krista-not-real@darthmama.org", NULL, PEP_OWN_USERID, "Krista at Home");
  2333     PEP_STATUS status = myself(session, id1);
  2334     pEp_identity* id2 = NULL;
  2335     status = set_up_preset(session, ALICE, true, true, false, false, false, &id2);
  2336     pEp_identity* id3 = new_identity("krista-not-real@angryshark.eu", NULL, PEP_OWN_USERID, "Krista at Shark");
  2337     status = myself(session, id3);
  2338     pEp_identity* id4 = NULL;
  2339     status = set_up_preset(session, BOB, true, false, false, false, false, &id4);
  2340     pEp_identity* id5 = new_identity("krista-not-real@pep.foundation", NULL, PEP_OWN_USERID, "Krista at Work");
  2341     status = myself(session, id5);
  2342     pEp_identity* id6 = new_identity("grrrr-not-real@angryshark.eu", NULL, PEP_OWN_USERID, "GRRRR is a Shark");
  2343     status = myself(session, id6);
  2344     pEp_identity* id7 = NULL;
  2345     status = set_up_preset(session, CAROL, true, false, true, false, false, &id7);
  2346     pEp_identity* id8 = NULL;
  2347     status = set_up_preset(session, DAVE, true, true, true, false, false, &id8);
  2348 
  2349     identity_list* own_identities = NULL;
  2350     stringlist_t* revocations = NULL;
  2351     stringlist_t* keys = NULL;
  2352 
  2353     stringlist_t* first_keylist = new_stringlist(NULL);
  2354     stringlist_add(first_keylist, strdup(id1->fpr));
  2355     stringlist_add(first_keylist, strdup(id3->fpr));
  2356     stringlist_add(first_keylist, strdup(id5->fpr));
  2357     stringlist_add(first_keylist, strdup(id6->fpr));
  2358 
  2359     status = key_reset_own_and_deliver_revocations(session,
  2360                                                    &own_identities,
  2361                                                    &revocations,
  2362                                                    &keys);
  2363 
  2364     ASSERT_EQ(status, PEP_STATUS_OK);
  2365     ASSERT_NE(own_identities, nullptr);
  2366     ASSERT_NE(revocations, nullptr);
  2367     ASSERT_NE(keys, nullptr);
  2368 
  2369     int i = 0;
  2370     identity_list* curr_ident = own_identities;
  2371     stringlist_t* second_keylist = new_stringlist(NULL);
  2372 
  2373     for (i = 0; i < 4 && curr_ident; i++, curr_ident = curr_ident->next) {
  2374         ASSERT_NE(curr_ident->ident, nullptr);
  2375         ASSERT_NE(curr_ident->ident->fpr, nullptr);
  2376         stringlist_t* found = stringlist_search(first_keylist, curr_ident->ident->fpr);
  2377         ASSERT_EQ(found, nullptr);
  2378         PEP_comm_type ct = PEP_ct_unknown;
  2379         status = get_key_rating(session, curr_ident->ident->fpr, &ct);
  2380         ASSERT_EQ(ct, PEP_ct_OpenPGP_unconfirmed);
  2381         stringlist_add(second_keylist, strdup(curr_ident->ident->fpr));
  2382     }
  2383     ASSERT_EQ(i, 4);
  2384     ASSERT_EQ(curr_ident, nullptr);
  2385 
  2386     stringlist_t* curr_key = first_keylist;
  2387     for (i = 0; i < 4; i++, curr_key = curr_key->next) {
  2388         PEP_comm_type ct = PEP_ct_unknown;
  2389         status = get_key_rating(session, curr_key->value, &ct);
  2390         ASSERT_EQ(ct, PEP_ct_key_revoked);
  2391     }
  2392 
  2393     // Ok, now we're going to delete all the keys, and then try to reimport.
  2394     curr_key = first_keylist;
  2395     for (i = 0; i < 4; curr_key = curr_key->next, i++) {
  2396         status = delete_keypair(session, curr_key->value);
  2397         ASSERT_EQ(status, PEP_STATUS_OK);
  2398     }
  2399     ASSERT_EQ(i, 4);
  2400     ASSERT_EQ(curr_key, nullptr);
  2401 
  2402     curr_key = second_keylist;
  2403     for (i = 0; i < 4; curr_key = curr_key->next, i++) {
  2404         status = delete_keypair(session, curr_key->value);
  2405         ASSERT_EQ(status, PEP_STATUS_OK);
  2406     }
  2407     ASSERT_EQ(i, 4);
  2408     ASSERT_EQ(curr_key, nullptr);
  2409 
  2410     // Make sure we can't find them
  2411     curr_key = first_keylist;
  2412     for (i = 0; i < 4; curr_key = curr_key->next, i++) {
  2413         PEP_comm_type ct = PEP_ct_unknown;
  2414         status = get_key_rating(session, curr_key->value, &ct);
  2415         ASSERT_EQ(status, PEP_KEY_NOT_FOUND);
  2416     }
  2417     curr_key = second_keylist;
  2418     for (i = 0; i < 4; curr_key = curr_key->next, i++) {
  2419         PEP_comm_type ct = PEP_ct_unknown;
  2420         status = get_key_rating(session, curr_key->value, &ct);
  2421         ASSERT_EQ(status, PEP_KEY_NOT_FOUND);
  2422     }
  2423 
  2424 
  2425     // Reimport
  2426     curr_key = revocations;
  2427     for (i = 0; i < 4; curr_key = curr_key->next, i++) {
  2428         status = import_key(session, curr_key->value, strlen(curr_key->value), NULL);
  2429         ASSERT_EQ(status, PEP_KEY_IMPORTED);
  2430     }
  2431     ASSERT_EQ(i, 4);
  2432     ASSERT_EQ(curr_key, nullptr);
  2433 
  2434     curr_key = keys;
  2435     for (i = 0; i < 4; curr_key = curr_key->next, i++) {
  2436         status = import_key(session, curr_key->value, strlen(curr_key->value), NULL);
  2437         ASSERT_EQ(status, PEP_KEY_IMPORTED);
  2438     }
  2439     ASSERT_EQ(i, 4);
  2440     ASSERT_EQ(curr_key, nullptr);
  2441 
  2442     // Check the revoked keys to be sure they are revoked
  2443     curr_key = first_keylist;
  2444     for (i = 0; i < 4; curr_key = curr_key->next, i++) {
  2445         PEP_comm_type ct = PEP_ct_unknown;
  2446         status = get_key_rating(session, curr_key->value, &ct);
  2447         ASSERT_EQ(ct, PEP_ct_key_revoked);
  2448         ASSERT_EQ(status, PEP_STATUS_OK);
  2449     }
  2450     // Check the imported keys to be sure they are OK
  2451     curr_key = second_keylist;
  2452     for (i = 0; i < 4; curr_key = curr_key->next, i++) {
  2453         PEP_comm_type ct = PEP_ct_unknown;
  2454         status = get_key_rating(session, curr_key->value, &ct);
  2455         ASSERT_EQ(ct, PEP_ct_OpenPGP_unconfirmed);
  2456         ASSERT_EQ(status, PEP_STATUS_OK);
  2457     }
  2458 }
  2459 */
  2460 
  2461 
  2462 TEST_F(KeyResetMessageTest, codec_test) {
  2463     // create input values
  2464 
  2465     pEp_identity *ident1 = new_identity("alice@pep-project.org", "FEDCBA9876543210", "42", "Alice Miller");
  2466     ASSERT_NE(ident1, nullptr);
  2467     const char *key1 = "0123456789ABCDEF";
  2468     keyreset_command *cmd1 = new_keyreset_command(ident1, key1);
  2469     ASSERT_NE(cmd1, nullptr);
  2470 
  2471     keyreset_command_list *il = new_keyreset_command_list(cmd1);
  2472     ASSERT_NE(il, nullptr);
  2473 
  2474     pEp_identity *ident2 = new_identity("alice@peptest.ch", "0123456789abcdef", "42", "Alice Miller");
  2475     ASSERT_NE(ident2, nullptr);
  2476     const char *key2 = "fedcba9876543210";
  2477     keyreset_command *cmd2 = new_keyreset_command(ident2, key2);
  2478     ASSERT_NE(cmd2, nullptr);
  2479 
  2480     keyreset_command_list *_il = keyreset_command_list_add(il, cmd2);
  2481     ASSERT_NE(_il, nullptr);
  2482 
  2483     // check created struct
  2484 
  2485     ASSERT_NE(il->command, nullptr);
  2486     ASSERT_NE(il->command->ident, nullptr);
  2487     ASSERT_NE(il->command->new_key, nullptr);
  2488 
  2489     ASSERT_STREQ(il->command->ident->address, ident1->address);
  2490     ASSERT_STREQ(il->command->ident->fpr, ident1->fpr);
  2491     ASSERT_STREQ(il->command->ident->user_id, ident1->user_id);
  2492     ASSERT_STREQ(il->command->ident->username, ident1->username);
  2493     ASSERT_STREQ(il->command->new_key, key1);
  2494 
  2495     ASSERT_NE(il->next, nullptr);
  2496     ASSERT_NE(il->next->command, nullptr);
  2497     ASSERT_NE(il->next->command->ident, nullptr);
  2498     ASSERT_NE(il->next->command->new_key, nullptr);
  2499 
  2500     ASSERT_STREQ(il->next->command->ident->address, ident2->address);
  2501     ASSERT_STRCASEEQ(il->next->command->ident->fpr, ident2->fpr);
  2502     ASSERT_STREQ(il->next->command->ident->user_id, ident2->user_id);
  2503     ASSERT_STREQ(il->next->command->ident->username, ident2->username);
  2504     ASSERT_STRCASEEQ(il->next->command->new_key, key2);
  2505 
  2506     ASSERT_EQ(il->next->next, nullptr);
  2507 
  2508     // encode
  2509 
  2510     char *cmds = nullptr;
  2511     size_t size = 0;
  2512     PEP_STATUS status = key_reset_commands_to_PER(il, &cmds, &size);
  2513     ASSERT_EQ(status, PEP_STATUS_OK);
  2514     ASSERT_NE(cmds, nullptr);
  2515     ASSERT_NE(size, 0);
  2516 
  2517     // decode
  2518 
  2519     keyreset_command_list *ol = nullptr;
  2520     status = PER_to_key_reset_commands(cmds, size, &ol);
  2521     ASSERT_EQ(status, PEP_STATUS_OK);
  2522     ASSERT_NE(ol, nullptr);
  2523 
  2524     // compare
  2525 
  2526     ASSERT_NE(ol->command, nullptr);
  2527     ASSERT_NE(ol->command->ident, nullptr);
  2528     ASSERT_NE(ol->command->new_key, nullptr);
  2529 
  2530     ASSERT_STREQ(ol->command->ident->address, ident1->address);
  2531     ASSERT_STREQ(ol->command->ident->fpr, ident1->fpr);
  2532     ASSERT_STREQ(ol->command->ident->user_id, ident1->user_id);
  2533     ASSERT_STREQ(ol->command->ident->username, ident1->username);
  2534     ASSERT_STREQ(ol->command->new_key, key1);
  2535 
  2536     ASSERT_NE(ol->next, nullptr);
  2537     ASSERT_NE(ol->next->command, nullptr);
  2538     ASSERT_NE(ol->next->command->ident, nullptr);
  2539     ASSERT_NE(ol->next->command->new_key, nullptr);
  2540 
  2541     ASSERT_STREQ(ol->next->command->ident->address, ident2->address);
  2542     ASSERT_STRCASEEQ(ol->next->command->ident->fpr, ident2->fpr);
  2543     ASSERT_STREQ(ol->next->command->ident->user_id, ident2->user_id);
  2544     ASSERT_STREQ(ol->next->command->ident->username, ident2->username);
  2545     ASSERT_STRCASEEQ(ol->next->command->new_key, key2);
  2546 
  2547     ASSERT_EQ(ol->next->next, nullptr);
  2548 
  2549     // free
  2550 
  2551     free_keyreset_command_list(ol);
  2552     free(cmds);
  2553 
  2554     free_identity(ident1);
  2555     free_identity(ident2);
  2556     free_keyreset_command_list(il);
  2557 }