Added mailbox tests, fixed stupid idea where I outclevered myself with mailbox construction and filenames, fixed device cleanup (for this test - will need to be added to EngineTestSuite, probably, or we make device tests a subclass of EngineIndividualSuite - probably the better idea) sync_test_refactor
authorKrista 'DarthMama' Bennett <krista@pep.foundation>
Mon, 18 Feb 2019 15:12:44 +0100
branchsync_test_refactor
changeset 330536c6d3446490
parent 3301 08494fd9288e
child 3306 76e174c45bff
Added mailbox tests, fixed stupid idea where I outclevered myself with mailbox construction and filenames, fixed device cleanup (for this test - will need to be added to EngineTestSuite, probably, or we make device tests a subclass of EngineIndividualSuite - probably the better idea)
test/include/DeviceModelTests.h
test/include/pEpTestDevice.h
test/src/engine_tests/DeviceModelTests.cc
test/src/pEpTestDevice.cc
     1.1 --- a/test/include/DeviceModelTests.h	Sun Feb 17 14:38:56 2019 +0100
     1.2 +++ b/test/include/DeviceModelTests.h	Mon Feb 18 15:12:44 2019 +0100
     1.3 @@ -5,6 +5,7 @@
     1.4  #define DEVICE_MODEL_H
     1.5  
     1.6  #include <string>
     1.7 +#include <vector>
     1.8  #include "EngineTestIndividualSuite.h"
     1.9  
    1.10  using namespace std;
    1.11 @@ -12,10 +13,20 @@
    1.12  class DeviceModelTests : public EngineTestIndividualSuite {
    1.13      public:
    1.14          DeviceModelTests(string test_suite, string test_home_dir);
    1.15 +
    1.16 +    protected:
    1.17 +        void tear_down();
    1.18 +        
    1.19      private:
    1.20          void check_device_model();
    1.21          void check_two_device_model();
    1.22          void check_two_device_functionality();
    1.23 +        void check_mbox();
    1.24 +        void check_shared_mbox();
    1.25 +        
    1.26 +        void clear_and_delete_devices();
    1.27 +        
    1.28 +        vector<pEpTestDevice*> devices;
    1.29  };
    1.30  
    1.31  #endif
     2.1 --- a/test/include/pEpTestDevice.h	Sun Feb 17 14:38:56 2019 +0100
     2.2 +++ b/test/include/pEpTestDevice.h	Mon Feb 18 15:12:44 2019 +0100
     2.3 @@ -23,7 +23,7 @@
     2.4          void unset_device_environment();
     2.5          void grab_context(pEpTestDevice* victim);
     2.6  
     2.7 -        vector<string> check_mail();
     2.8 +        void check_mail(vector<string> &unread);
     2.9          string save_mail_to_mailbox(string mail);
    2.10  
    2.11          string device_name;
    2.12 @@ -38,6 +38,7 @@
    2.13      protected:        
    2.14          string mbox_last_read;
    2.15          vector<string> mail_to_read;
    2.16 +        
    2.17  //        string current_test_name;
    2.18          
    2.19  //        void set_full_env();
    2.20 @@ -50,7 +51,6 @@
    2.21          // void add_file_to_gpg_dir_queue(std::string copy_from, std::string dst_fname);    
    2.22          // void add_file_to_home_dir_queue(std::string copy_from, std::string dst_fname);
    2.23          // void process_file_queue(std::string dirname, std::vector<std::pair<std::string, std::string>> file_queue);
    2.24 -
    2.25  };
    2.26 -
    2.27 +    
    2.28  #endif
     3.1 --- a/test/src/engine_tests/DeviceModelTests.cc	Sun Feb 17 14:38:56 2019 +0100
     3.2 +++ b/test/src/engine_tests/DeviceModelTests.cc	Mon Feb 18 15:12:44 2019 +0100
     3.3 @@ -12,6 +12,7 @@
     3.4  #include <sys/types.h>
     3.5  
     3.6  #include "pEpEngine.h"
     3.7 +#include "mime.h"
     3.8  
     3.9  #include "TestUtils.h"
    3.10  #include "EngineTestIndividualSuite.h"
    3.11 @@ -27,6 +28,18 @@
    3.12                                                                        static_cast<Func>(&DeviceModelTests::check_device_model)));
    3.13      add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("DeviceModelTests::check_two_device_model"),
    3.14                                                                        static_cast<Func>(&DeviceModelTests::check_two_device_model)));
    3.15 +    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("DeviceModelTests::check_mbox"),
    3.16 +                                                                      static_cast<Func>(&DeviceModelTests::check_mbox)));
    3.17 +    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("DeviceModelTests::check_shared_mbox"),
    3.18 +                                                                      static_cast<Func>(&DeviceModelTests::check_shared_mbox)));
    3.19 +}
    3.20 +
    3.21 +void DeviceModelTests::tear_down() {
    3.22 +    for (vector<pEpTestDevice*>::iterator it = devices.begin();
    3.23 +                                         it != devices.end(); it++)
    3.24 +        delete(*it);                                         
    3.25 +    devices.clear();
    3.26 +    EngineTestIndividualSuite::tear_down();
    3.27  }
    3.28  
    3.29  void DeviceModelTests::check_device_model() {
    3.30 @@ -44,13 +57,13 @@
    3.31  
    3.32      const string device_dir = string(single->device_dir);
    3.33      delete(single);
    3.34 -    
    3.35      TEST_ASSERT_MSG(stat(device_dir.c_str(), &dirchk) != 0,
    3.36                           "Device dir not removed.");
    3.37  }
    3.38  
    3.39  void DeviceModelTests::check_two_device_model() {
    3.40      pEpTestDevice* first_device = new pEpTestDevice(temp_test_home, "First");
    3.41 +    devices.push_back(first_device);
    3.42      first_device->set_mailbox_dir(first_device->device_dir + "/mbox");
    3.43      string homedir = getenv("HOME");
    3.44      TEST_ASSERT_MSG(strcmp(homedir.c_str(), first_device->device_dir.c_str()) == 0, "First device didn't set $HOME correctly.");
    3.45 @@ -58,6 +71,7 @@
    3.46      TEST_ASSERT_MSG(strcmp(gpgdir.c_str(), (first_device->device_dir + "/gnupg").c_str()) == 0, "First device didn't set $GNUPGHOME correctly.");    
    3.47      first_device->unset_device_environment();
    3.48      pEpTestDevice* second_device = new pEpTestDevice(temp_test_home, "Second");
    3.49 +    devices.push_back(second_device);
    3.50      homedir = getenv("HOME");
    3.51      TEST_ASSERT_MSG(strcmp(homedir.c_str(), second_device->device_dir.c_str()) == 0, "Second device didn't set $HOME correctly");
    3.52      gpgdir = getenv("GNUPGHOME");
    3.53 @@ -73,17 +87,16 @@
    3.54      TEST_ASSERT_MSG(strcmp(homedir.c_str(), second_device->device_dir.c_str()) == 0, "Second device failed to grab context.");
    3.55      gpgdir = getenv("GNUPGHOME");
    3.56      TEST_ASSERT_MSG(strcmp(gpgdir.c_str(), (second_device->device_dir + "/gnupg").c_str()) == 0, "Second device context switch didn't set $GNUPGHOME correctly.");        
    3.57 -    delete(first_device);
    3.58 -    delete(second_device);
    3.59 -    TEST_ASSERT(true);
    3.60  }
    3.61  
    3.62  void DeviceModelTests::check_two_device_functionality() {
    3.63      // Set up devices and shared mailbox
    3.64      pEpTestDevice* first_device = new pEpTestDevice(temp_test_home, "First");
    3.65 +    devices.push_back(first_device);    
    3.66      first_device->set_mailbox_dir(first_device->device_dir + "/mbox");
    3.67      first_device->unset_device_environment();
    3.68      pEpTestDevice* second_device = new pEpTestDevice(temp_test_home, "Second");
    3.69 +    devices.push_back(second_device);    
    3.70      second_device->set_mailbox_dir(first_device->device_dir + "/mbox");
    3.71      first_device->grab_context(second_device);
    3.72      TEST_ASSERT_MSG(first_device->mbox_dir.compare(second_device->mbox_dir) == 0,
    3.73 @@ -193,16 +206,76 @@
    3.74      free_identity(alice_dev_2_ident);
    3.75      free_identity(bob_id);
    3.76      free_identity(bob_id_2);              
    3.77 -    delete(first_device);
    3.78 -    delete(second_device);
    3.79 +}
    3.80 +
    3.81 +void DeviceModelTests::check_mbox() {
    3.82 +    // Set up devices and shared mailbox
    3.83 +    pEpTestDevice* first_device = new pEpTestDevice(temp_test_home, "Device");
    3.84 +    devices.push_back(first_device);
    3.85 +    
    3.86 +    first_device->set_mailbox_dir(first_device->device_dir + "/mbox");
    3.87 +
    3.88 +    string alice_email = "pep.test.alice@pep-project.org";
    3.89 +    string alice_fpr = "4ABE3AAF59AC32CFE4F86500A9411D176FF00E97";
    3.90 +    
    3.91 +    slurp_and_import_key(first_device->session, "test_keys/pub/pep-test-alice-0x6FF00E97_pub.asc");
    3.92 +    slurp_and_import_key(first_device->session, "test_keys/priv/pep-test-alice-0x6FF00E97_priv.asc");
    3.93 +    
    3.94 +    pEp_identity* alice_ident = new_identity(alice_email.c_str(), alice_fpr.c_str(), "ALICE", "Alice From Mel's Diner");    
    3.95 +    PEP_STATUS status = set_own_key(first_device->session, alice_ident, alice_fpr.c_str());    
    3.96 +    TEST_ASSERT_MSG(status == PEP_STATUS_OK, 
    3.97 +        (string("Unable to set own key. status is ") + tl_status_string(status)).c_str());
    3.98 +        
    3.99 +    message* new_msg = new_message(PEP_dir_outgoing);
   3.100 +    
   3.101 +    new_msg->from = alice_ident;
   3.102 +    new_msg->to = new_identity_list(identity_dup(alice_ident));
   3.103 +    new_msg->longmsg = strdup("Some dumb message.\nBlahblahblah.");
   3.104 +    new_msg->shortmsg = strdup("hello, world");
   3.105 +    new_msg->attachments = new_bloblist(NULL, 0, "application/octet-stream", NULL);
   3.106 +
   3.107 +    message* enc_msg = NULL;
   3.108 +    
   3.109 +    status = encrypt_message(first_device->session, new_msg, NULL, &enc_msg, PEP_enc_PGP_MIME, 0);
   3.110 +
   3.111 +    TEST_ASSERT(status == PEP_STATUS_OK);
   3.112 +    TEST_ASSERT(enc_msg);
   3.113 +    char* msg_text = NULL;
   3.114 +    mime_encode_message(enc_msg, false, &msg_text);
   3.115 +    TEST_ASSERT(msg_text);
   3.116 +    
   3.117 +    string filename = first_device->save_mail_to_mailbox(msg_text);
   3.118 +    TEST_ASSERT(!filename.empty());
   3.119 +
   3.120 +    vector<string> curr_mail_received;
   3.121 +    first_device->check_mail(curr_mail_received);
   3.122 +    TEST_ASSERT_MSG(curr_mail_received.size() == 1, 
   3.123 +                    (string("Received ") + to_string(curr_mail_received.size()) + " emails, should have received 1.").c_str());
   3.124 +
   3.125 +    first_device->save_mail_to_mailbox(msg_text);
   3.126 +    first_device->save_mail_to_mailbox(msg_text);
   3.127 +    first_device->save_mail_to_mailbox(msg_text);
   3.128 +    first_device->check_mail(curr_mail_received);
   3.129 +    TEST_ASSERT_MSG(curr_mail_received.size() == 3, 
   3.130 +                    (string("Received ") + to_string(curr_mail_received.size()) + " emails, should have received 3.").c_str());
   3.131 +    
   3.132 +    first_device->save_mail_to_mailbox(msg_text);
   3.133 +    first_device->save_mail_to_mailbox(msg_text);
   3.134 +    first_device->check_mail(curr_mail_received);
   3.135 +    TEST_ASSERT_MSG(curr_mail_received.size() == 2, 
   3.136 +                    (string("Received ") + to_string(curr_mail_received.size()) + " emails, should have received 2.").c_str());
   3.137  }
   3.138  
   3.139  void DeviceModelTests::check_shared_mbox() {
   3.140      // Set up devices and shared mailbox
   3.141 -    pEpTestDevice* first_device = new pEpTestDevice(temp_test_home, "First");
   3.142 +    pEpTestDevice* first_device = new pEpTestDevice(temp_test_home, "Muffins");
   3.143 +    devices.push_back(first_device);
   3.144 +
   3.145      first_device->set_mailbox_dir(first_device->device_dir + "/mbox");
   3.146      first_device->unset_device_environment();
   3.147 -    pEpTestDevice* second_device = new pEpTestDevice(temp_test_home, "Second");
   3.148 +    pEpTestDevice* second_device = new pEpTestDevice(temp_test_home, "MoMuffins");
   3.149 +    devices.push_back(second_device);
   3.150 +
   3.151      second_device->set_mailbox_dir(first_device->device_dir + "/mbox");
   3.152      first_device->grab_context(second_device);
   3.153      TEST_ASSERT_MSG(first_device->mbox_dir.compare(second_device->mbox_dir) == 0,
   3.154 @@ -238,7 +311,5 @@
   3.155      
   3.156      // Ok, everybody's set up. Let's play with mailboxes.
   3.157      
   3.158 -    first_device->grab_context(second_device);
   3.159 -    
   3.160 -    
   3.161 +    first_device->grab_context(second_device);    
   3.162  }
     4.1 --- a/test/src/pEpTestDevice.cc	Sun Feb 17 14:38:56 2019 +0100
     4.2 +++ b/test/src/pEpTestDevice.cc	Mon Feb 18 15:12:44 2019 +0100
     4.3 @@ -179,7 +179,7 @@
     4.4  
     4.5      struct stat dirchk;
     4.6      
     4.7 -    if (mbox_dir.empty() || stat(mbox_dir.c_str(), &dirchk) == 0)
     4.8 +    if (mbox_dir.empty() || stat(mbox_dir.c_str(), &dirchk) != 0)
     4.9          throw std::runtime_error("pEpTestDevice: mailbox dir not initialised or removed.");                     
    4.10  
    4.11      chrono::milliseconds timestamp = chrono::duration_cast< chrono::milliseconds >(
    4.12 @@ -191,65 +191,82 @@
    4.13      
    4.14      outfile.open(outfile_name);
    4.15      outfile << mail;
    4.16 -    outfile.close();    
    4.17 +    outfile.flush();
    4.18 +    outfile.close(); 
    4.19 +    cout << "Wrote " + outfile_name << endl;
    4.20 +    usleep(1000); // guarantees change in filename
    4.21      return outfile_name;
    4.22  }
    4.23  
    4.24  // Presumes everything in mbox dir is a .eml file
    4.25  // and was written in above ts form. We can change later if needed.
    4.26 -vector<string> pEpTestDevice::check_mail() {
    4.27 +void pEpTestDevice::check_mail(vector<string> &unread) {
    4.28 +    unread.clear();
    4.29      mail_to_read.clear();
    4.30      struct stat dirchk;
    4.31  
    4.32 -    if (mbox_dir.empty() || stat(mbox_dir.c_str(), &dirchk) == 0)
    4.33 +    if (mbox_dir.empty() || stat(mbox_dir.c_str(), &dirchk) != 0)
    4.34          throw std::runtime_error("pEpTestDevice: mailbox dir not initialised or removed.");                     
    4.35  
    4.36 -    vector<string> email_list;
    4.37      DIR* dir;   
    4.38      dirent* pdir;
    4.39   
    4.40      dir = opendir(mbox_dir.c_str()); 
    4.41 -    while ((pdir = readdir(dir)))
    4.42 -        email_list.push_back(pdir->d_name);  
    4.43 +    while ((pdir = readdir(dir))) {
    4.44 +        struct stat dirchk2;
    4.45 +        const char* fname = pdir->d_name;
    4.46 +        if (strcmp(fname, ".") && strcmp(fname, "..")) {
    4.47 +            stat((mbox_dir + fname).c_str(), &dirchk2);
    4.48 +            cout << "I see " << fname << endl;
    4.49 +            if (!S_ISDIR(dirchk2.st_mode)) {
    4.50 +                unread.push_back(fname); 
    4.51 +                cout << "I pushed " << fname << endl; 
    4.52 +            }    
    4.53 +        }        
    4.54 +    }    
    4.55  
    4.56 -    if (email_list.empty())
    4.57 -        return email_list;
    4.58 +    if (unread.empty())
    4.59 +        return;
    4.60      else    
    4.61 -        sort(email_list.begin(), email_list.end());
    4.62 +        sort(unread.begin(), unread.end());
    4.63      
    4.64 -    if (mbox_last_read.empty())
    4.65 -        return email_list;
    4.66 +    if (!mbox_last_read.empty()) {
    4.67      
    4.68 -    string last_read_time_str = 
    4.69 -        mbox_last_read.substr(0, mbox_last_read.find_last_of("."));
    4.70 -    unsigned long long last_read_ts = strtoull(last_read_time_str.c_str(), NULL, 10);
    4.71 -    
    4.72 -    int i = 0;
    4.73 -    
    4.74 -    for (vector<string>::iterator it = email_list.begin();
    4.75 -         it != email_list.end(); it++, i++) {
    4.76 -        string fname = *it;
    4.77 -        if (fname.empty())
    4.78 -            continue; // ??
    4.79 +        string last_read_time_str = 
    4.80 +            mbox_last_read.substr(0, mbox_last_read.find_last_of("."));
    4.81 +        unsigned long long last_read_ts = strtoull(last_read_time_str.c_str(), NULL, 10);
    4.82 +        
    4.83 +        int i = 0;
    4.84 +        
    4.85 +        for (vector<string>::iterator it = unread.begin();
    4.86 +             it != unread.end(); it++, i++) {
    4.87 +            string fname = *it;
    4.88 +            if (fname.empty())
    4.89 +                continue; // ??
    4.90  
    4.91 -        // I don't want to think about how to format to do a strcmp atm
    4.92 -        size_t dot_pos = fname.find_last_of(".");
    4.93 -        string ts_str = fname.substr(0, dot_pos);
    4.94 +            // I don't want to think about how to format to do a strcmp atm
    4.95 +            size_t dot_pos = fname.find_last_of(".");
    4.96 +            string ts_str = fname.substr(0, dot_pos);
    4.97 +            
    4.98 +            unsigned long long file_ts = strtoull(ts_str.c_str(), NULL, 10);
    4.99 +            
   4.100 +            if (file_ts > last_read_ts)
   4.101 +                break;
   4.102 +        }
   4.103          
   4.104 -        unsigned long long file_ts = strtoull(ts_str.c_str(), NULL, 10);
   4.105 -        
   4.106 -        if (file_ts > last_read_ts)
   4.107 -            break;
   4.108 +        if (i > unread.size())
   4.109 +            unread.clear();
   4.110 +        else {
   4.111 +            if (i != 0) {
   4.112 +                unread.erase(unread.begin(), unread.begin() + i);
   4.113 +                cout << "Unread contains: " << endl;
   4.114 +                for (vector<string>::iterator it = unread.begin();
   4.115 +                     it != unread.end(); it++) {
   4.116 +                    cout << *it << endl;
   4.117 +                }
   4.118 +            }
   4.119 +        }     
   4.120      }
   4.121      
   4.122 -    if (i > email_list.size())
   4.123 -        email_list.clear();
   4.124 -    else {
   4.125 -        if (i != 0) {
   4.126 -            vector<string> not_read(email_list.begin() + i, email_list.end());
   4.127 -            email_list = not_read;
   4.128 -        }
   4.129 -    }     
   4.130 -    
   4.131 -    return email_list;
   4.132 +    mbox_last_read = string(unread.back());    
   4.133  }