ENGINE-398: key reset tests and fixes ENGINE-398
authorKrista Bennett <krista@pep-project.org>
Thu, 13 Sep 2018 13:59:19 +0200
branchENGINE-398
changeset 2942c929f7eba32a
parent 2940 5c60780cf963
child 2943 dd40810a7490
ENGINE-398: key reset tests and fixes
src/message_api.c
test/include/KeyResetMessageTests.h
test/src/engine_tests/KeyResetMessageTests.cc
     1.1 --- a/src/message_api.c	Tue Sep 11 13:06:56 2018 +0200
     1.2 +++ b/src/message_api.c	Thu Sep 13 13:59:19 2018 +0200
     1.3 @@ -1664,6 +1664,11 @@
     1.4                                                       old_fpr,
     1.5                                                       new_fpr);
     1.6  
     1.7 +        if (status == PEP_CANNOT_FIND_IDENTITY) { // this is ok, just means we never mailed them 
     1.8 +            status = PEP_STATUS_OK;
     1.9 +            continue; 
    1.10 +        }
    1.11 +            
    1.12          if (status != PEP_STATUS_OK) {
    1.13              free(reset_msg);
    1.14              goto pep_free;
    1.15 @@ -1812,7 +1817,7 @@
    1.16      
    1.17      PEP_comm_type max_comm_type = PEP_ct_pEp;
    1.18  
    1.19 -    identity_list * _il;
    1.20 +    identity_list * _il = NULL;
    1.21  
    1.22      if (enc_format != PEP_enc_none && (_il = src->bcc) && _il->ident)
    1.23      // BCC limited support:
    1.24 @@ -1875,7 +1880,7 @@
    1.25              status = PEP_KEY_NOT_FOUND;
    1.26          }
    1.27      }
    1.28 -    else
    1.29 +    else // Non BCC
    1.30      {
    1.31          for (_il = src->to; _il && _il->ident; _il = _il->next) {
    1.32              PEP_STATUS _status = PEP_STATUS_OK;
    1.33 @@ -1917,6 +1922,7 @@
    1.34              }
    1.35              else
    1.36                  _status = myself(session, _il->ident);
    1.37 +                
    1.38              if (_status != PEP_STATUS_OK) {
    1.39                  status = PEP_UNENCRYPTED;
    1.40                  goto pep_error;
    1.41 @@ -3267,7 +3273,7 @@
    1.42  
    1.43                  free(replace_fpr);
    1.44                  replace_fpr = NULL;
    1.45 -                
    1.46 +                break;
    1.47              default:    
    1.48                  goto pep_free;    
    1.49          }
    1.50 @@ -3803,36 +3809,40 @@
    1.51                                                           curr_pair->key,
    1.52                                                           curr_pair->value);
    1.53  
    1.54 -            if (status != PEP_STATUS_OK)
    1.55 -                goto pep_error;
    1.56 -                
    1.57 -            if (!reset_msg) {
    1.58 -                status = PEP_OUT_OF_MEMORY;
    1.59 -                goto pep_error;
    1.60 -            }
    1.61 -            // insert into queue
    1.62 -            if (session->messageToSend) {
    1.63 -                status = session->messageToSend(session->sync_obj, reset_msg);
    1.64 -            } 
    1.65 -            else if (session->sync_session->messageToSend) {
    1.66 -                status = session->sync_session->messageToSend(session->sync_session->sync_obj, 
    1.67 -                                                              reset_msg); 
    1.68 -            }
    1.69 -            else {
    1.70 -                status = PEP_SYNC_NO_MESSAGE_SEND_CALLBACK;
    1.71 -            }
    1.72 -
    1.73 -            if (status == PEP_STATUS_OK) {    
    1.74 -                // Put into notified DB
    1.75 -                status = set_reset_contact_notified(session, curr_pair->key, msg->from->user_id);
    1.76 -                if (status != PEP_STATUS_OK) // It's ok to barf because it's a DB problem??
    1.77 +            // If we can't find the identity, this is someone we've never mailed, so we just
    1.78 +            // go on letting them use the wrong key until we mail them ourselves. (Spammers, etc)
    1.79 +            if (status != PEP_CANNOT_FIND_IDENTITY) {
    1.80 +                if (status != PEP_STATUS_OK)
    1.81                      goto pep_error;
    1.82 -            }
    1.83 -            else {
    1.84 -                // According to Volker, this would only be a fatal error, so...
    1.85 -                free_message(reset_msg); // ??
    1.86 -                reset_msg = NULL; // ??
    1.87 -                goto pep_error;
    1.88 +                    
    1.89 +                if (!reset_msg) {
    1.90 +                    status = PEP_OUT_OF_MEMORY;
    1.91 +                    goto pep_error;
    1.92 +                }
    1.93 +                // insert into queue
    1.94 +                if (session->messageToSend) {
    1.95 +                    status = session->messageToSend(session->sync_obj, reset_msg);
    1.96 +                } 
    1.97 +                else if (session->sync_session->messageToSend) {
    1.98 +                    status = session->sync_session->messageToSend(session->sync_session->sync_obj, 
    1.99 +                                                                  reset_msg); 
   1.100 +                }
   1.101 +                else {
   1.102 +                    status = PEP_SYNC_NO_MESSAGE_SEND_CALLBACK;
   1.103 +                }
   1.104 +
   1.105 +                if (status == PEP_STATUS_OK) {    
   1.106 +                    // Put into notified DB
   1.107 +                    status = set_reset_contact_notified(session, curr_pair->key, msg->from->user_id);
   1.108 +                    if (status != PEP_STATUS_OK) // It's ok to barf because it's a DB problem??
   1.109 +                        goto pep_error;
   1.110 +                }
   1.111 +                else {
   1.112 +                    // According to Volker, this would only be a fatal error, so...
   1.113 +                    free_message(reset_msg); // ??
   1.114 +                    reset_msg = NULL; // ??
   1.115 +                    goto pep_error;
   1.116 +                }
   1.117              }
   1.118          }
   1.119      }
     2.1 --- a/test/include/KeyResetMessageTests.h	Tue Sep 11 13:06:56 2018 +0200
     2.2 +++ b/test/include/KeyResetMessageTests.h	Thu Sep 13 13:59:19 2018 +0200
     2.3 @@ -39,13 +39,12 @@
     2.4          void check_reset_key_and_notify();
     2.5          void check_non_reset_receive_revoked();
     2.6          void check_reset_receive_revoked();
     2.7 -        void check_receive_key_reset_private();
     2.8 -        void check_receive_key_reset_wrong_signer();
     2.9 -        void check_receive_key_reset_unsigned();
    2.10 -        void check_receive_message_to_revoked_key();   
    2.11 +        void check_receive_message_to_revoked_key_from_unknown();   
    2.12 +        void check_receive_message_to_revoked_key_from_contact();   
    2.13          
    2.14          void send_setup();
    2.15          void receive_setup();     
    2.16 +        void create_msg_for_revoked_key();
    2.17          
    2.18  };
    2.19  
     3.1 --- a/test/src/engine_tests/KeyResetMessageTests.cc	Tue Sep 11 13:06:56 2018 +0200
     3.2 +++ b/test/src/engine_tests/KeyResetMessageTests.cc	Thu Sep 13 13:59:19 2018 +0200
     3.3 @@ -36,14 +36,10 @@
     3.4                                                                        static_cast<Func>(&KeyResetMessageTests::check_non_reset_receive_revoked)));
     3.5      add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("KeyResetMessageTests::check_reset_receive_revoked"),
     3.6                                                                        static_cast<Func>(&KeyResetMessageTests::check_reset_receive_revoked)));
     3.7 -    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("KeyResetMessageTests::check_receive_key_reset_private"),
     3.8 -                                                                      static_cast<Func>(&KeyResetMessageTests::check_receive_key_reset_private)));
     3.9 -    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("KeyResetMessageTests::check_receive_key_reset_wrong_signer"),
    3.10 -                                                                      static_cast<Func>(&KeyResetMessageTests::check_receive_key_reset_wrong_signer)));
    3.11 -    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("KeyResetMessageTests::check_receive_key_reset_unsigned"),
    3.12 -                                                                      static_cast<Func>(&KeyResetMessageTests::check_receive_key_reset_unsigned)));
    3.13 -    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("KeyResetMessageTests::check_receive_message_to_revoked_key"),
    3.14 -                                                                      static_cast<Func>(&KeyResetMessageTests::check_receive_message_to_revoked_key)));
    3.15 +    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("KeyResetMessageTests::check_receive_message_to_revoked_key_from_unknown"),
    3.16 +                                                                      static_cast<Func>(&KeyResetMessageTests::check_receive_message_to_revoked_key_from_unknown)));
    3.17 +    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("KeyResetMessageTests::check_receive_message_to_revoked_key_from_contact"),
    3.18 +                                                                      static_cast<Func>(&KeyResetMessageTests::check_receive_message_to_revoked_key_from_contact)));                                                                      
    3.19  }
    3.20  
    3.21  PEP_STATUS KeyResetMessageTests::message_send_callback(void* obj, message* msg) {
    3.22 @@ -55,6 +51,7 @@
    3.23      EngineTestIndividualSuite::setup();
    3.24      session->sync_obj = this;
    3.25      session->messageToSend = &KeyResetMessageTests::message_send_callback;
    3.26 +    m_queue.clear();
    3.27  }
    3.28  
    3.29  void KeyResetMessageTests::send_setup() {
    3.30 @@ -344,19 +341,167 @@
    3.31      free(keylist);    
    3.32  }
    3.33  
    3.34 +void KeyResetMessageTests::create_msg_for_revoked_key() {
    3.35 +    PEP_STATUS status = set_up_ident_from_scratch(session,
    3.36 +                "test_keys/pub/pep-test-gabrielle-0xE203586C_pub.asc",
    3.37 +                "pep-test-gabrielle@pep-project.org", NULL, PEP_OWN_USERID, 
    3.38 +                "Gabi", NULL, false
    3.39 +            );
    3.40 +    assert(status == PEP_STATUS_OK);
    3.41 +    status = set_up_ident_from_scratch(session,
    3.42 +                "test_keys/priv/pep-test-gabrielle-0xE203586C_priv.asc",
    3.43 +                "pep-test-gabrielle@pep-project.org", NULL, PEP_OWN_USERID, 
    3.44 +                "Gabi", NULL, false
    3.45 +            );
    3.46 +    assert(status == PEP_STATUS_OK);
    3.47 +    
    3.48 +    status = set_up_ident_from_scratch(session,
    3.49 +                "test_keys/pub/pep-test-alice-0x6FF00E97_pub.asc",
    3.50 +                "pep.test.alice@pep-project.org", NULL, "AliceOther", "Alice is tired of Bob",
    3.51 +                NULL, false
    3.52 +            );
    3.53 +    
    3.54 +    pEp_identity* from_ident = new_identity("pep-test-gabrielle@pep-project.org", NULL, PEP_OWN_USERID, NULL);
    3.55 +    status = myself(session, from_ident); 
    3.56 +    TEST_ASSERT_MSG(status == PEP_STATUS_OK, tl_status_string(status));
    3.57 +    TEST_ASSERT_MSG(from_ident->fpr && strcasecmp(from_ident->fpr, "906C9B8349954E82C5623C3C8C541BD4E203586C") == 0,
    3.58 +                    from_ident->fpr);
    3.59 +    TEST_ASSERT(from_ident->me);
    3.60 +    
    3.61 +    // "send" some messages to update the social graph entries
    3.62 +    identity_list* send_idents = 
    3.63 +        new_identity_list(
    3.64 +            new_identity("pep.test.alice@pep-project.org", NULL, "AliceOther", NULL));
    3.65 +    status = update_identity(session, send_idents->ident);
    3.66 +    TEST_ASSERT_MSG(status == PEP_STATUS_OK, tl_status_string(status));    
    3.67 +    status = set_as_pep_user(session, send_idents->ident);
    3.68 +                             
    3.69 +    message* outgoing_msg = new_message(PEP_dir_outgoing);
    3.70 +    TEST_ASSERT(outgoing_msg);
    3.71 +    outgoing_msg->from = from_ident;
    3.72 +    outgoing_msg->to = send_idents;
    3.73 +    outgoing_msg->shortmsg = strdup("Well isn't THIS a useless message...");
    3.74 +    outgoing_msg->longmsg = strdup("Hi Mom...\n");
    3.75 +    outgoing_msg->attachments = new_bloblist(NULL, 0, "application/octet-stream", NULL);
    3.76 +    cout << "Message created.\n\n";
    3.77 +    cout << "Encrypting message as MIME multipart…\n";
    3.78 +    message* enc_outgoing_msg = nullptr;
    3.79 +    cout << "Calling encrypt_message()\n";
    3.80 +    status = encrypt_message(session, outgoing_msg, NULL, &enc_outgoing_msg, PEP_enc_PGP_MIME, 0);
    3.81 +    TEST_ASSERT_MSG((status == PEP_STATUS_OK), tl_status_string(status));
    3.82 +    TEST_ASSERT(enc_outgoing_msg);
    3.83 +    cout << "Message encrypted.\n";    
    3.84 +    char* outstring = NULL;
    3.85 +    mime_encode_message(enc_outgoing_msg, false, &outstring);
    3.86 +    cout << outstring << endl;
    3.87 +    free_message(enc_outgoing_msg);
    3.88 +    free(outstring);
    3.89 +}
    3.90  
    3.91 -void KeyResetMessageTests::check_receive_key_reset_private() {
    3.92 +void KeyResetMessageTests::check_receive_message_to_revoked_key_from_unknown() {
    3.93 +    // create_msg_for_revoked_key(); // call to recreate msg
    3.94 +    send_setup();
    3.95 +    pEp_identity* from_ident = new_identity("pep.test.alice@pep-project.org", NULL, PEP_OWN_USERID, NULL);
    3.96 +    PEP_STATUS status = myself(session, from_ident); 
    3.97 +    TEST_ASSERT_MSG(status == PEP_STATUS_OK, tl_status_string(status));
    3.98 +    TEST_ASSERT_MSG(from_ident->fpr && strcasecmp(from_ident->fpr, alice_fpr) == 0,
    3.99 +                    from_ident->fpr);
   3.100 +    TEST_ASSERT(from_ident->me);
   3.101 +
   3.102 +    status = key_reset(session, alice_fpr, from_ident);
   3.103 +    TEST_ASSERT_MSG((status == PEP_STATUS_OK), tl_status_string(status));
   3.104 +    m_queue.clear();
   3.105 +    
   3.106 +    string received_mail = slurp("test_files/398_gabrielle_to_alice.eml");
   3.107 +    char* decrypted_msg = NULL;
   3.108 +    char* modified_src = NULL;
   3.109 +    stringlist_t* keylist = NULL;
   3.110 +    PEP_rating rating;
   3.111 +    PEP_decrypt_flags_t flags;
   3.112 +    status = MIME_decrypt_message(session, received_mail.c_str(), received_mail.size(),
   3.113 +                                  &decrypted_msg, &keylist, &rating, &flags, &modified_src);
   3.114 +    TEST_ASSERT(m_queue.size() == 0);
   3.115 +    free(decrypted_msg);
   3.116 +    free(modified_src);
   3.117 +    free_stringlist(keylist);
   3.118 +    free_identity(from_ident);
   3.119 +}
   3.120 +
   3.121 +void KeyResetMessageTests::check_receive_message_to_revoked_key_from_contact() {
   3.122 +    // create_msg_for_revoked_key(); // call to recreate msg
   3.123 +    send_setup();
   3.124 +    pEp_identity* from_ident = new_identity("pep.test.alice@pep-project.org", NULL, PEP_OWN_USERID, NULL);
   3.125 +    PEP_STATUS status = myself(session, from_ident); 
   3.126 +    TEST_ASSERT_MSG(status == PEP_STATUS_OK, tl_status_string(status));
   3.127 +    TEST_ASSERT_MSG(from_ident->fpr && strcasecmp(from_ident->fpr, alice_fpr) == 0,
   3.128 +                    from_ident->fpr);
   3.129 +    TEST_ASSERT(from_ident->me);
   3.130 +
   3.131 +    // Send Gabrielle a message
   3.132 +    identity_list* send_idents = new_identity_list(new_identity("pep-test-gabrielle@pep-project.org", NULL, "Gabi", "Gabi"));
   3.133 +    cout << "Creating outgoing message to update DB" << endl;
   3.134 +    message* outgoing_msg = new_message(PEP_dir_outgoing);
   3.135 +    TEST_ASSERT(outgoing_msg);
   3.136 +    outgoing_msg->from = from_ident;
   3.137 +    outgoing_msg->to = send_idents;
   3.138 +    outgoing_msg->shortmsg = strdup("Well isn't THIS a useless message...");
   3.139 +    outgoing_msg->longmsg = strdup("Hi Mom...\n");
   3.140 +    outgoing_msg->attachments = new_bloblist(NULL, 0, "application/octet-stream", NULL);
   3.141 +    cout << "Message created.\n\n";
   3.142 +    cout << "Encrypting message as MIME multipart…\n";
   3.143 +    message* enc_outgoing_msg = nullptr;
   3.144 +    cout << "Calling encrypt_message()\n";
   3.145 +    status = encrypt_message(session, outgoing_msg, NULL, &enc_outgoing_msg, PEP_enc_PGP_MIME, 0);
   3.146 +    TEST_ASSERT_MSG((status == PEP_UNENCRYPTED), tl_status_string(status));
   3.147 +    //
   3.148 +    cout << "Message created." << endl;
   3.149 +    
   3.150 +    // Make the update have occurred earlier, so we don't notify her
   3.151 +    // (We have no key for her yet anyway!)
   3.152 +    int int_result = sqlite3_exec(
   3.153 +        session->db,
   3.154 +        "update identity "
   3.155 +        "   set timestamp = 661008730 "
   3.156 +        "   where address = 'pep-test-gabrielle@pep-project.org' ;",
   3.157 +        NULL,
   3.158 +        NULL,
   3.159 +        NULL
   3.160 +    );
   3.161 +    TEST_ASSERT(int_result == SQLITE_OK);
   3.162 +
   3.163 +    status = key_reset(session, alice_fpr, from_ident);
   3.164 +    TEST_ASSERT_MSG((status == PEP_STATUS_OK), tl_status_string(status));
   3.165 +    TEST_ASSERT(m_queue.size() == 0);
   3.166 +    m_queue.clear();
   3.167 +
   3.168 +    // Now we get mail from Gabi, who only has our old key AND has become
   3.169 +    // a pEp user in the meantime...
   3.170 +    string received_mail = slurp("test_files/398_gabrielle_to_alice.eml");
   3.171 +    char* decrypted_msg = NULL;
   3.172 +    char* modified_src = NULL;
   3.173 +    stringlist_t* keylist = NULL;
   3.174 +    PEP_rating rating;
   3.175 +    PEP_decrypt_flags_t flags;
   3.176 +    status = MIME_decrypt_message(session, received_mail.c_str(), received_mail.size(),
   3.177 +                                  &decrypted_msg, &keylist, &rating, &flags, &modified_src);
   3.178 +    
   3.179 +    TEST_ASSERT(m_queue.size() == 1);
   3.180 +    vector<message*>::iterator it = m_queue.begin();
   3.181 +    message* reset_msg = *it;
   3.182 +    TEST_ASSERT(reset_msg);    
   3.183 +    TEST_ASSERT(reset_msg->from);    
   3.184 +    TEST_ASSERT(reset_msg->to);    
   3.185 +    TEST_ASSERT(reset_msg->to->ident);    
   3.186 +    TEST_ASSERT(strcmp(reset_msg->to->ident->address, "pep-test-gabrielle@pep-project.org") == 0);
   3.187 +    TEST_ASSERT(strcmp(reset_msg->to->ident->fpr, "906C9B8349954E82C5623C3C8C541BD4E203586C") == 0);    
   3.188 +    TEST_ASSERT(strcmp(reset_msg->from->fpr, alice_fpr) != 0);
   3.189 +    TEST_ASSERT(keylist);
   3.190 +    TEST_ASSERT(keylist->value);
   3.191 +    TEST_ASSERT(strcmp(keylist->value, alice_fpr) != 0);
   3.192 +    TEST_ASSERT(keylist->next);
   3.193 +    if (strcmp(keylist->next->value, "906C9B8349954E82C5623C3C8C541BD4E203586C") != 0)
   3.194 +        TEST_ASSERT(keylist->next->next && 
   3.195 +                    strcmp(keylist->next->value, 
   3.196 +                           "906C9B8349954E82C5623C3C8C541BD4E203586C") == 0);
   3.197      TEST_ASSERT(true);
   3.198  }
   3.199 -
   3.200 -void KeyResetMessageTests::check_receive_key_reset_wrong_signer() {
   3.201 -    TEST_ASSERT(true);
   3.202 -}
   3.203 -
   3.204 -void KeyResetMessageTests::check_receive_key_reset_unsigned() {
   3.205 -    TEST_ASSERT(true);
   3.206 -}
   3.207 -
   3.208 -void KeyResetMessageTests::check_receive_message_to_revoked_key() {
   3.209 -    TEST_ASSERT(true);
   3.210 -}