Added send queue and address->mbox mapping sync_test_refactor
authorKrista 'DarthMama' Bennett <krista@pep.foundation>
Wed, 20 Feb 2019 11:08:31 +0100
branchsync_test_refactor
changeset 330676e174c45bff
parent 3305 36c6d3446490
child 3307 b3d26f9f0225
Added send queue and address->mbox mapping
test/include/pEpTestDevice.h
test/src/engine_tests/DeviceModelTests.cc
test/src/pEpTestDevice.cc
     1.1 --- a/test/include/pEpTestDevice.h	Mon Feb 18 15:12:44 2019 +0100
     1.2 +++ b/test/include/pEpTestDevice.h	Wed Feb 20 11:08:31 2019 +0100
     1.3 @@ -7,6 +7,7 @@
     1.4  #include <vector>
     1.5  #include <utility>
     1.6  #include "pEpEngine.h"
     1.7 +#include "message.h"
     1.8  
     1.9  using namespace std;
    1.10  
    1.11 @@ -17,14 +18,22 @@
    1.12                        inject_sync_event_t inject_sync_ev_func = NULL);        
    1.13                        
    1.14          virtual ~pEpTestDevice();
    1.15 +
    1.16 +        static string save_mail_to_mailbox(string mailbox_path, string mail);
    1.17 +
    1.18          void set_mailbox_dir(string mbox_dirname);
    1.19 -
    1.20          void set_device_environment();
    1.21          void unset_device_environment();
    1.22          void grab_context(pEpTestDevice* victim);
    1.23  
    1.24 +        string receive_mail(string mail); // save mail to local mailbox
    1.25 +                                          // kinda deprecated, but for 
    1.26 +                                          // manual use.
    1.27 +
    1.28          void check_mail(vector<string> &unread);
    1.29 -        string save_mail_to_mailbox(string mail);
    1.30 +        void read_mail(vector<string>mails, vector<string> &to_read);
    1.31 +        PEP_STATUS process_send_queue();
    1.32 +        PEP_STATUS send_mail(message* mail);
    1.33  
    1.34          string device_name;
    1.35          PEP_SESSION session;
    1.36 @@ -38,6 +47,8 @@
    1.37      protected:        
    1.38          string mbox_last_read;
    1.39          vector<string> mail_to_read;
    1.40 +        map<string,string> address_to_mbox_map; // maybe string, vector<string>?
    1.41 +        vector<message*> send_queue;
    1.42          
    1.43  //        string current_test_name;
    1.44          
     2.1 --- a/test/src/engine_tests/DeviceModelTests.cc	Mon Feb 18 15:12:44 2019 +0100
     2.2 +++ b/test/src/engine_tests/DeviceModelTests.cc	Wed Feb 20 11:08:31 2019 +0100
     2.3 @@ -30,8 +30,6 @@
     2.4                                                                        static_cast<Func>(&DeviceModelTests::check_two_device_model)));
     2.5      add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("DeviceModelTests::check_mbox"),
     2.6                                                                        static_cast<Func>(&DeviceModelTests::check_mbox)));
     2.7 -    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("DeviceModelTests::check_shared_mbox"),
     2.8 -                                                                      static_cast<Func>(&DeviceModelTests::check_shared_mbox)));
     2.9  }
    2.10  
    2.11  void DeviceModelTests::tear_down() {
    2.12 @@ -244,7 +242,7 @@
    2.13      mime_encode_message(enc_msg, false, &msg_text);
    2.14      TEST_ASSERT(msg_text);
    2.15      
    2.16 -    string filename = first_device->save_mail_to_mailbox(msg_text);
    2.17 +    string filename = first_device->receive_mail(msg_text);
    2.18      TEST_ASSERT(!filename.empty());
    2.19  
    2.20      vector<string> curr_mail_received;
    2.21 @@ -252,64 +250,16 @@
    2.22      TEST_ASSERT_MSG(curr_mail_received.size() == 1, 
    2.23                      (string("Received ") + to_string(curr_mail_received.size()) + " emails, should have received 1.").c_str());
    2.24  
    2.25 -    first_device->save_mail_to_mailbox(msg_text);
    2.26 -    first_device->save_mail_to_mailbox(msg_text);
    2.27 -    first_device->save_mail_to_mailbox(msg_text);
    2.28 +    first_device->receive_mail(msg_text);
    2.29 +    first_device->receive_mail(msg_text);
    2.30 +    first_device->receive_mail(msg_text);
    2.31      first_device->check_mail(curr_mail_received);
    2.32      TEST_ASSERT_MSG(curr_mail_received.size() == 3, 
    2.33                      (string("Received ") + to_string(curr_mail_received.size()) + " emails, should have received 3.").c_str());
    2.34      
    2.35 -    first_device->save_mail_to_mailbox(msg_text);
    2.36 -    first_device->save_mail_to_mailbox(msg_text);
    2.37 +    first_device->receive_mail(msg_text);
    2.38 +    first_device->receive_mail(msg_text);
    2.39      first_device->check_mail(curr_mail_received);
    2.40      TEST_ASSERT_MSG(curr_mail_received.size() == 2, 
    2.41                      (string("Received ") + to_string(curr_mail_received.size()) + " emails, should have received 2.").c_str());
    2.42  }
    2.43 -
    2.44 -void DeviceModelTests::check_shared_mbox() {
    2.45 -    // Set up devices and shared mailbox
    2.46 -    pEpTestDevice* first_device = new pEpTestDevice(temp_test_home, "Muffins");
    2.47 -    devices.push_back(first_device);
    2.48 -
    2.49 -    first_device->set_mailbox_dir(first_device->device_dir + "/mbox");
    2.50 -    first_device->unset_device_environment();
    2.51 -    pEpTestDevice* second_device = new pEpTestDevice(temp_test_home, "MoMuffins");
    2.52 -    devices.push_back(second_device);
    2.53 -
    2.54 -    second_device->set_mailbox_dir(first_device->device_dir + "/mbox");
    2.55 -    first_device->grab_context(second_device);
    2.56 -    TEST_ASSERT_MSG(first_device->mbox_dir.compare(second_device->mbox_dir) == 0,
    2.57 -                    "Shared mailbox is not really shared");
    2.58 -
    2.59 -    string alice_email = "pep.test.alice@pep-project.org";
    2.60 -    string alice_fpr = "4ABE3AAF59AC32CFE4F86500A9411D176FF00E97";
    2.61 -    
    2.62 -    // First device is Alice's established one with the current key
    2.63 -    TEST_ASSERT_MSG(slurp_and_import_key(first_device->session, "test_keys/pub/pep-test-alice-0x6FF00E97_pub.asc"),
    2.64 -                    "Alice's pubkey not imported for first device.");
    2.65 -    TEST_ASSERT_MSG(slurp_and_import_key(first_device->session, "test_keys/priv/pep-test-alice-0x6FF00E97_priv.asc"),
    2.66 -                    "Alice's privkey not imported for first device.");
    2.67 -
    2.68 -    pEp_identity* alice_dev_1_ident = new_identity(alice_email.c_str(), alice_fpr.c_str(), "ALICE", "Alice From Mel's Diner");
    2.69 -    
    2.70 -    PEP_STATUS status = set_own_key(first_device->session, alice_dev_1_ident, alice_fpr.c_str());    
    2.71 -    TEST_ASSERT_MSG(status == PEP_STATUS_OK, 
    2.72 -        (string("Unable to set own key on first device. status is ") + tl_status_string(status)).c_str());
    2.73 -
    2.74 -    // Second device is one Alice is setting up (we'll use this model for keysync tests, so why not?)                
    2.75 -    second_device->grab_context(first_device);
    2.76 -
    2.77 -    pEp_identity* alice_dev_2_ident = new_identity(alice_email.c_str(), NULL, PEP_OWN_USERID, "Alice Miller");
    2.78 -
    2.79 -    status = myself(second_device->session, alice_dev_2_ident);
    2.80 -
    2.81 -    TEST_ASSERT_MSG(alice_dev_2_ident->fpr, "No fpr for alice on second device");
    2.82 -    TEST_ASSERT_MSG(alice_fpr.compare(alice_dev_2_ident->fpr) != 0,
    2.83 -                    "myself did not generate new key for alice on device 2; alice's old key was found.");
    2.84 -    
    2.85 -    const char* alice_2_fpr = alice_dev_2_ident->fpr;
    2.86 -    
    2.87 -    // Ok, everybody's set up. Let's play with mailboxes.
    2.88 -    
    2.89 -    first_device->grab_context(second_device);    
    2.90 -}
     3.1 --- a/test/src/pEpTestDevice.cc	Mon Feb 18 15:12:44 2019 +0100
     3.2 +++ b/test/src/pEpTestDevice.cc	Wed Feb 20 11:08:31 2019 +0100
     3.3 @@ -20,6 +20,7 @@
     3.4  #include "pEpTestStatic.h"
     3.5  #include <algorithm>
     3.6  #include "TestConstants.h"
     3.7 +#include "mime.h"
     3.8  #include <chrono>
     3.9  
    3.10  pEpTestDevice::pEpTestDevice(string test_dir, 
    3.11 @@ -105,35 +106,9 @@
    3.12  }
    3.13  
    3.14  void pEpTestDevice::grab_context(pEpTestDevice* victim) {
    3.15 +    victim->process_send_queue();
    3.16      victim->unset_device_environment();
    3.17      set_device_environment();
    3.18 -// 
    3.19 -//     int success = system("gpgconf --kill all");
    3.20 -//     if (success != 0)
    3.21 -//         throw std::runtime_error("SETUP: Error when executing 'gpgconf --kill all'.");    
    3.22 -//     struct stat buf;
    3.23 -// 
    3.24 -//     success = system("gpgconf --remove-socketdir");            
    3.25 -//     if (success != 0)
    3.26 -//         throw std::runtime_error("RESTORE: Error when executing 'gpgconf --remove-socketdir'.");    
    3.27 -// 
    3.28 -//     success = setenv("GNUPGHOME", (device_dir + "/gnupg").c_str(), 1);
    3.29 -// 
    3.30 -//     if (success != 0)
    3.31 -//         throw std::runtime_error("SETUP: Error when setting GNUPGHOME.");
    3.32 -// 
    3.33 -//     cout << "New GNUPGHOME is " << getenv("GNUPGHOME") << endl << endl;
    3.34 -// 
    3.35 -//     success = setenv("HOME", device_dir.c_str(), 1);
    3.36 -//     if (success != 0)
    3.37 -//         throw std::runtime_error("SETUP: Cannot set device_dir for init.");
    3.38 -// 
    3.39 -// w#ifndef USE_NETPGP            
    3.40 -//     success = system("gpgconf --create-socketdir");
    3.41 -//     if (success != 0)
    3.42 -//         throw std::runtime_error("RESTORE: Error when executing 'gpgconf --create-socketdir'.");        
    3.43 -//     system("gpg-connect-agent /bye");   // Just in case - otherwise, we die on MacOS sometimes. Is this enough??
    3.44 -// #endif
    3.45  }
    3.46  
    3.47  void pEpTestDevice::unset_device_environment() {
    3.48 @@ -173,19 +148,67 @@
    3.49      }    
    3.50  }
    3.51  
    3.52 -string pEpTestDevice::save_mail_to_mailbox(string mail) {
    3.53 +string pEpTestDevice::receive_mail(string mail) {
    3.54 +    return save_mail_to_mailbox(mbox_dir, mail);
    3.55 +}
    3.56 +
    3.57 +PEP_STATUS pEpTestDevice::send_mail(message* mail) {
    3.58 +    if (!mail->to)
    3.59 +        return PEP_ILLEGAL_VALUE;
    3.60 +        
    3.61 +    identity_list* to_list = mail->to;
    3.62 +    if (!to_list)
    3.63 +        return PEP_ILLEGAL_VALUE;
    3.64 +
    3.65 +    char* msg_str = NULL;
    3.66 +    PEP_STATUS status = mime_encode_message(mail, false, &msg_str);
    3.67 +    if (status != PEP_STATUS_OK)
    3.68 +        return status;
    3.69 +    if (!msg_str)
    3.70 +        return PEP_UNKNOWN_ERROR;
    3.71 +    
    3.72 +    for (identity_list* tl_curr = to_list; tl_curr; tl_curr = tl_curr->next) {
    3.73 +        if (!tl_curr->ident)
    3.74 +            return PEP_ILLEGAL_VALUE;
    3.75 +        const char* to = tl_curr->ident->address;
    3.76 +        if (!to || to[0] == '\0')
    3.77 +            return PEP_ILLEGAL_VALUE;
    3.78 +            
    3.79 +        std::map<string,string>::iterator it = address_to_mbox_map.find(to);
    3.80 +        if (it == address_to_mbox_map.end() || it->second.empty())
    3.81 +            return PEP_RECORD_NOT_FOUND;
    3.82 +        string mbox = it->second;
    3.83 +        if (save_mail_to_mailbox(mbox, msg_str).empty())
    3.84 +            return PEP_CANNOT_CREATE_TEMP_FILE;
    3.85 +    }    
    3.86 +    return PEP_STATUS_OK;
    3.87 +}
    3.88 +
    3.89 +PEP_STATUS pEpTestDevice::process_send_queue() {
    3.90 +    for (vector<message*>::iterator it = send_queue.begin(); it != send_queue.end(); it++) {
    3.91 +        if (!*it) {
    3.92 +            PEP_STATUS status = send_mail(*it);
    3.93 +            if (status != PEP_STATUS_OK)
    3.94 +                return status;
    3.95 +        }
    3.96 +    }
    3.97 +    send_queue.clear();
    3.98 +    return PEP_STATUS_OK;
    3.99 +}
   3.100 +
   3.101 +string pEpTestDevice::save_mail_to_mailbox(string mailbox_path, string mail) {
   3.102      if (mail.empty())
   3.103          throw std::runtime_error("Attempt to write empty mail to mailbox.");
   3.104  
   3.105      struct stat dirchk;
   3.106      
   3.107 -    if (mbox_dir.empty() || stat(mbox_dir.c_str(), &dirchk) != 0)
   3.108 +    if (mailbox_path.empty() || stat(mailbox_path.c_str(), &dirchk) != 0)
   3.109          throw std::runtime_error("pEpTestDevice: mailbox dir not initialised or removed.");                     
   3.110  
   3.111      chrono::milliseconds timestamp = chrono::duration_cast< chrono::milliseconds >(
   3.112                                          chrono::system_clock::now().time_since_epoch());
   3.113      
   3.114 -    string outfile_name = mbox_dir + "/" + to_string(timestamp.count()) + ".eml";
   3.115 +    string outfile_name = mailbox_path + "/" + to_string(timestamp.count()) + ".eml";
   3.116  
   3.117      ofstream outfile;
   3.118      
   3.119 @@ -270,3 +293,14 @@
   3.120      
   3.121      mbox_last_read = string(unread.back());    
   3.122  }
   3.123 +
   3.124 +void pEpTestDevice::read_mail(vector<string> mails, vector<string> &to_read) {
   3.125 +    to_read.clear();
   3.126 +    for (vector<string>::iterator it = mails.begin();
   3.127 +         it != mails.end(); it++) {
   3.128 +        string mail = slurp(mbox_dir + "/" + *it);
   3.129 +        if (mail.empty())
   3.130 +            continue;
   3.131 +        to_read.push_back(mail);
   3.132 +    }
   3.133 +}