ENGINE-516: Yahoo, messages and context switches and mailboxes and all the things work\! sync_test_refactor
authorKrista 'DarthMama' Bennett <krista@pep.foundation>
Fri, 22 Feb 2019 12:58:03 +0100
branchsync_test_refactor
changeset 3307b3d26f9f0225
parent 3306 76e174c45bff
child 3313 b11d8d025466
ENGINE-516: Yahoo, messages and context switches and mailboxes and all the things work\!
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	Wed Feb 20 11:08:31 2019 +0100
     1.2 +++ b/test/include/DeviceModelTests.h	Fri Feb 22 12:58:03 2019 +0100
     1.3 @@ -15,6 +15,7 @@
     1.4          DeviceModelTests(string test_suite, string test_home_dir);
     1.5  
     1.6      protected:
     1.7 +        void setup();
     1.8          void tear_down();
     1.9          
    1.10      private:
    1.11 @@ -22,7 +23,8 @@
    1.12          void check_two_device_model();
    1.13          void check_two_device_functionality();
    1.14          void check_mbox();
    1.15 -        void check_shared_mbox();
    1.16 +        void check_three_device_mbox_with_send();
    1.17 +        void check_switch_context();
    1.18          
    1.19          void clear_and_delete_devices();
    1.20          
     2.1 --- a/test/include/pEpTestDevice.h	Wed Feb 20 11:08:31 2019 +0100
     2.2 +++ b/test/include/pEpTestDevice.h	Fri Feb 22 12:58:03 2019 +0100
     2.3 @@ -18,23 +18,48 @@
     2.4                        inject_sync_event_t inject_sync_ev_func = NULL);        
     2.5                        
     2.6          virtual ~pEpTestDevice();
     2.7 +        
     2.8 +        static pEpTestDevice* active;
     2.9 +        static void switch_context(pEpTestDevice* switch_to);
    2.10  
    2.11 +        // Write mail string to a timestamp-named file in the listed mailbox,
    2.12 +        // and return the name of the file.
    2.13          static string save_mail_to_mailbox(string mailbox_path, string mail);
    2.14  
    2.15          void set_mailbox_dir(string mbox_dirname);
    2.16 +        
    2.17 +        // set my variables as the environment vars (HOME, GNUPGHOME, etc)
    2.18          void set_device_environment();
    2.19 +        
    2.20 +        // clean up crypto agents and 
    2.21          void unset_device_environment();
    2.22 +        
    2.23 +        // Make all of this device's environment information the current
    2.24 +        // environment, and restart session for this device with this,
    2.25 +        // releasing the victim's device and session (and processing their 
    2.26 +        // send queue before doing this)
    2.27          void grab_context(pEpTestDevice* victim);
    2.28  
    2.29 -        string receive_mail(string mail); // save mail to local mailbox
    2.30 -                                          // kinda deprecated, but for 
    2.31 -                                          // manual use.
    2.32 -
    2.33 +        // take string and save to own mailbox as timestamped mail file and 
    2.34 +        // return filename 
    2.35 +        string receive_mail(string mail); 
    2.36 +        
    2.37 +        // Get the filenames of all unread mails
    2.38          void check_mail(vector<string> &unread);
    2.39 -        void read_mail(vector<string>mails, vector<string> &to_read);
    2.40 +        
    2.41 +        // Read all mails (by filename) into a vector of strings
    2.42 +        void read_mail(vector<string>mails, vector<message*> &to_read);
    2.43 +        
    2.44 +        // write everything into the correct mail by mailbox.
    2.45 +        // PRESUMES address_to_mbox_map HAS AN ENTRY FOR EVERY ADDRESS IN 
    2.46 +        // THE RECIP LIST
    2.47          PEP_STATUS process_send_queue();
    2.48 +        
    2.49 +        // write individual message struct to the correct mailbox
    2.50          PEP_STATUS send_mail(message* mail);
    2.51  
    2.52 +        void add_message_to_send_queue(message* msg);
    2.53 +        
    2.54          string device_name;
    2.55          PEP_SESSION session;
    2.56          string device_dir;        
    2.57 @@ -43,11 +68,11 @@
    2.58  
    2.59          messageToSend_t device_messageToSend;
    2.60          inject_sync_event_t device_inject_sync_event;
    2.61 +        map<string,string> address_to_mbox_map; // maybe string, vector<string>?
    2.62          
    2.63      protected:        
    2.64          string mbox_last_read;
    2.65          vector<string> mail_to_read;
    2.66 -        map<string,string> address_to_mbox_map; // maybe string, vector<string>?
    2.67          vector<message*> send_queue;
    2.68          
    2.69  //        string current_test_name;
     3.1 --- a/test/src/engine_tests/DeviceModelTests.cc	Wed Feb 20 11:08:31 2019 +0100
     3.2 +++ b/test/src/engine_tests/DeviceModelTests.cc	Fri Feb 22 12:58:03 2019 +0100
     3.3 @@ -28,8 +28,19 @@
     3.4                                                                        static_cast<Func>(&DeviceModelTests::check_device_model)));
     3.5      add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("DeviceModelTests::check_two_device_model"),
     3.6                                                                        static_cast<Func>(&DeviceModelTests::check_two_device_model)));
     3.7 +    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("DeviceModelTests::check_two_device_functionality"),
     3.8 +                                                                      static_cast<Func>(&DeviceModelTests::check_two_device_functionality)));
     3.9      add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("DeviceModelTests::check_mbox"),
    3.10                                                                        static_cast<Func>(&DeviceModelTests::check_mbox)));
    3.11 +    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("DeviceModelTests::check_three_device_mbox_with_send"),
    3.12 +                                                                      static_cast<Func>(&DeviceModelTests::check_three_device_mbox_with_send)));
    3.13 +    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("DeviceModelTests::check_switch_context"),
    3.14 +                                                                      static_cast<Func>(&DeviceModelTests::check_switch_context)));
    3.15 +}
    3.16 +
    3.17 +void DeviceModelTests::setup() {
    3.18 +    EngineTestIndividualSuite::setup();
    3.19 +    pEpTestDevice::active = NULL;
    3.20  }
    3.21  
    3.22  void DeviceModelTests::tear_down() {
    3.23 @@ -67,7 +78,7 @@
    3.24      TEST_ASSERT_MSG(strcmp(homedir.c_str(), first_device->device_dir.c_str()) == 0, "First device didn't set $HOME correctly.");
    3.25      string gpgdir = getenv("GNUPGHOME");
    3.26      TEST_ASSERT_MSG(strcmp(gpgdir.c_str(), (first_device->device_dir + "/gnupg").c_str()) == 0, "First device didn't set $GNUPGHOME correctly.");    
    3.27 -    first_device->unset_device_environment();
    3.28 +
    3.29      pEpTestDevice* second_device = new pEpTestDevice(temp_test_home, "Second");
    3.30      devices.push_back(second_device);
    3.31      homedir = getenv("HOME");
    3.32 @@ -92,10 +103,11 @@
    3.33      pEpTestDevice* first_device = new pEpTestDevice(temp_test_home, "First");
    3.34      devices.push_back(first_device);    
    3.35      first_device->set_mailbox_dir(first_device->device_dir + "/mbox");
    3.36 -    first_device->unset_device_environment();
    3.37 +    
    3.38      pEpTestDevice* second_device = new pEpTestDevice(temp_test_home, "Second");
    3.39      devices.push_back(second_device);    
    3.40      second_device->set_mailbox_dir(first_device->device_dir + "/mbox");
    3.41 +
    3.42      first_device->grab_context(second_device);
    3.43      TEST_ASSERT_MSG(first_device->mbox_dir.compare(second_device->mbox_dir) == 0,
    3.44                      "Shared mailbox is not really shared");
    3.45 @@ -142,7 +154,7 @@
    3.46      status = find_keys(first_device->session, alice_2_fpr, &keylist);
    3.47      
    3.48      TEST_ASSERT(!keylist);
    3.49 -    TEST_ASSERT(status == PEP_KEY_NOT_FOUND);
    3.50 +    TEST_ASSERT_MSG(status == PEP_STATUS_OK, tl_status_string(status));
    3.51      free_stringlist(keylist);
    3.52      keylist = NULL;
    3.53  
    3.54 @@ -151,7 +163,7 @@
    3.55      char* alice_2_keydata = NULL;
    3.56      size_t alice_2_keydata_size = 0;
    3.57      
    3.58 -    status = export_key(session, alice_2_fpr, &alice_2_keydata, &alice_2_keydata_size);
    3.59 +    status = export_key(second_device->session, alice_2_fpr, &alice_2_keydata, &alice_2_keydata_size);
    3.60  
    3.61      TEST_ASSERT_MSG(status == PEP_STATUS_OK, tl_status_string(status));
    3.62      TEST_ASSERT(alice_2_keydata);
    3.63 @@ -263,3 +275,400 @@
    3.64      TEST_ASSERT_MSG(curr_mail_received.size() == 2, 
    3.65                      (string("Received ") + to_string(curr_mail_received.size()) + " emails, should have received 2.").c_str());
    3.66  }
    3.67 +
    3.68 +void DeviceModelTests::check_three_device_mbox_with_send() {
    3.69 +    try {
    3.70 +        // Set up devices and shared mailbox
    3.71 +        pEpTestDevice* first_device = new pEpTestDevice(temp_test_home, "Alex");
    3.72 +        devices.push_back(first_device);    
    3.73 +        first_device->set_mailbox_dir(first_device->device_dir + "/mbox");
    3.74 +        string alex_email = "alex@darthmama.cool";
    3.75 +        pEp_identity* alex_identity = new_identity(alex_email.c_str(), NULL, "AlexID", "Alex Braithwaite");
    3.76 +        PEP_STATUS status = myself(first_device->session, alex_identity);
    3.77 +        
    3.78 +        pEpTestDevice* second_device = new pEpTestDevice(temp_test_home, "Bree");
    3.79 +        devices.push_back(second_device);    
    3.80 +        second_device->set_mailbox_dir(second_device->device_dir + "/mbox");
    3.81 +        string bree_email = "bree@cheese.melted";
    3.82 +        pEp_identity* bree_identity = new_identity(bree_email.c_str(), NULL, "BreeID", "Briana Cheeserton");
    3.83 +        status = myself(second_device->session, bree_identity);
    3.84 +        
    3.85 +        pEpTestDevice* third_device = new pEpTestDevice(temp_test_home, "Charmander");
    3.86 +        devices.push_back(third_device);    
    3.87 +        third_device->set_mailbox_dir(third_device->device_dir + "/mbox");
    3.88 +        string charm_email = "charmander@poke.mon";
    3.89 +        pEp_identity* charm_identity = new_identity(charm_email.c_str(), NULL, "CharmID", "Charmander T. Pokemon");
    3.90 +        status = myself(third_device->session, charm_identity);
    3.91 +        first_device->grab_context(third_device);
    3.92 +
    3.93 +        map<string,string> address_maps = {
    3.94 +            {alex_email,first_device->mbox_dir},
    3.95 +            {bree_email,second_device->mbox_dir},
    3.96 +            {charm_email,third_device->mbox_dir},
    3.97 +        };
    3.98 +
    3.99 +        // this just simulates the ability to address and deliver, so everyone has
   3.100 +        // the same maps.
   3.101 +        first_device->address_to_mbox_map = second_device->address_to_mbox_map =
   3.102 +            third_device->address_to_mbox_map = address_maps;
   3.103 +        // Note to self - I'll bet this is some C++ mem nightmare.
   3.104 +        
   3.105 +        message* msg = new_message(PEP_dir_outgoing);
   3.106 +        msg->from = identity_dup(alex_identity);
   3.107 +        msg->to = new_identity_list(new_identity(bree_email.c_str(), NULL, "ItsBree", "Bree Cheeserton"));
   3.108 +        msg->shortmsg = strdup("First test message!");
   3.109 +        msg->longmsg = strdup("Yo Bree! This is Alex! Hi!\nEr, hi!\n");
   3.110 +        msg->attachments = new_bloblist(NULL, 0, "application/octet-stream", NULL);
   3.111 +
   3.112 +        // engine_passthrough
   3.113 +        message* enc_msg = NULL;
   3.114 +        status = encrypt_message(first_device->session, msg, NULL, &enc_msg, PEP_enc_PGP_MIME, 0);
   3.115 +        TEST_ASSERT(status == PEP_UNENCRYPTED);
   3.116 +        TEST_ASSERT(enc_msg == NULL);
   3.117 +
   3.118 +        // when sent, msg is freed, so do NOT free it after this.
   3.119 +        first_device->add_message_to_send_queue(msg);
   3.120 +        second_device->grab_context(first_device);
   3.121 +        msg = NULL;
   3.122 +        
   3.123 +        vector<string> inbox_list;
   3.124 +        second_device->check_mail(inbox_list);
   3.125 +        TEST_ASSERT_MSG(inbox_list.size() == 1, 
   3.126 +                        (string("Second device received ") + to_string(inbox_list.size()) + " emails, should have received 1.").c_str());
   3.127 +
   3.128 +        vector<message*> inbox_mails;
   3.129 +        stringlist_t* keylist = NULL;
   3.130 +        PEP_rating rating;
   3.131 +        PEP_decrypt_flags_t flags;
   3.132 +        
   3.133 +        second_device->read_mail(inbox_list, inbox_mails);
   3.134 +        TEST_ASSERT(inbox_mails.size() == 1 && inbox_mails.at(0));
   3.135 +        
   3.136 +        // Get Alex's key
   3.137 +        status = decrypt_message(second_device->session,
   3.138 +                                 inbox_mails.at(0), &msg, &keylist,
   3.139 +                                 &rating, &flags);
   3.140 +        TEST_ASSERT(status == PEP_UNENCRYPTED);
   3.141 +        TEST_ASSERT(msg == NULL);
   3.142 +
   3.143 +        inbox_list.clear();
   3.144 +        free(inbox_mails.at(0));
   3.145 +        inbox_mails.clear();
   3.146 +        
   3.147 +        third_device->grab_context(second_device);
   3.148 +            
   3.149 +        msg = new_message(PEP_dir_outgoing);
   3.150 +        msg->from = identity_dup(charm_identity);
   3.151 +        msg->to = new_identity_list(new_identity(bree_email.c_str(), NULL, "SuperBree", "Bree Cheeserton"));
   3.152 +        msg->shortmsg = strdup("First test message!");
   3.153 +        msg->longmsg = strdup("Yo Bree! This is Charmander! I'm a cool Pokemon! Hi!\nEr, hi!\n");
   3.154 +        msg->attachments = new_bloblist(NULL, 0, "application/octet-stream", NULL);
   3.155 +
   3.156 +        // engine_passthrough
   3.157 +        enc_msg = NULL;
   3.158 +        status = encrypt_message(third_device->session, msg, NULL, &enc_msg, PEP_enc_PGP_MIME, 0);
   3.159 +        TEST_ASSERT(status == PEP_UNENCRYPTED);
   3.160 +        TEST_ASSERT(enc_msg == NULL);
   3.161 +
   3.162 +        // when sent, msg is freed, so do NOT free it after this.
   3.163 +        third_device->add_message_to_send_queue(msg);
   3.164 +        second_device->grab_context(third_device);
   3.165 +        msg = NULL;
   3.166 +            
   3.167 +        second_device->check_mail(inbox_list);
   3.168 +        TEST_ASSERT_MSG(inbox_list.size() == 1, 
   3.169 +                        (string("Second device received ") + to_string(inbox_list.size()) + " emails, should have received 1.").c_str());
   3.170 +    
   3.171 +        keylist = NULL;
   3.172 +        flags = 0;
   3.173 +        
   3.174 +        second_device->read_mail(inbox_list, inbox_mails);
   3.175 +        TEST_ASSERT(inbox_mails.size() == 1 && inbox_mails.at(0));
   3.176 +        
   3.177 +        // Get Charmander's key
   3.178 +        status = decrypt_message(second_device->session,
   3.179 +                                 inbox_mails.at(0), &msg, &keylist,
   3.180 +                                 &rating, &flags);
   3.181 +        TEST_ASSERT(status == PEP_UNENCRYPTED);
   3.182 +        TEST_ASSERT(msg == NULL);
   3.183 +
   3.184 +        inbox_list.clear();
   3.185 +        free(inbox_mails.at(0));
   3.186 +        inbox_mails.clear();    
   3.187 +
   3.188 +        // Ok, now, revenge of encrypting Bree
   3.189 +        msg = new_message(PEP_dir_outgoing);
   3.190 +        msg->from = identity_dup(bree_identity);
   3.191 +        msg->to = new_identity_list(new_identity(alex_email.c_str(), NULL, "Alexei", "Alex Braithwaite is a char in a bad novel"));
   3.192 +        identity_list_add(msg->to, new_identity(charm_email.c_str(), NULL, "Charming", "Charmanderpoke E. Mon is NOT a Pokemon"));
   3.193 +        msg->shortmsg = strdup("Last test message!");
   3.194 +        msg->longmsg = strdup("You guys are fools :)\n");
   3.195 +        msg->attachments = new_bloblist(NULL, 0, "application/octet-stream", NULL);
   3.196 +
   3.197 +        enc_msg = NULL;
   3.198 +        status = encrypt_message(second_device->session, msg, NULL, &enc_msg, PEP_enc_PGP_MIME, 0);
   3.199 +        TEST_ASSERT(status == PEP_STATUS_OK);
   3.200 +        TEST_ASSERT(enc_msg);
   3.201 +         
   3.202 +        free_message(msg);
   3.203 +        msg = NULL;
   3.204 +        free_stringlist(keylist);
   3.205 +        flags = 0;
   3.206 +        keylist = NULL;
   3.207 +        
   3.208 +        // when sent, enc_msg is freed, so do NOT free it after this.
   3.209 +        second_device->add_message_to_send_queue(enc_msg);
   3.210 +        first_device->grab_context(second_device);
   3.211 +        enc_msg = NULL;
   3.212 +
   3.213 +        first_device->check_mail(inbox_list);
   3.214 +        TEST_ASSERT_MSG(inbox_list.size() == 1, 
   3.215 +                        (string("First device received ") + to_string(inbox_list.size()) + " emails, should have received 1.").c_str());
   3.216 +            
   3.217 +        first_device->read_mail(inbox_list, inbox_mails);
   3.218 +        TEST_ASSERT(inbox_mails.size() == 1 && inbox_mails.at(0));
   3.219 +        
   3.220 +        status = decrypt_message(first_device->session,
   3.221 +                                 inbox_mails.at(0), &msg, &keylist,
   3.222 +                                 &rating, &flags);
   3.223 +        TEST_ASSERT_MSG(status == PEP_STATUS_OK, tl_status_string(status));
   3.224 +        TEST_ASSERT_MSG(rating == PEP_rating_reliable, tl_rating_string(rating));
   3.225 +        TEST_ASSERT(msg);
   3.226 +
   3.227 +        free_message(msg);
   3.228 +        inbox_list.clear();
   3.229 +        free(inbox_mails.at(0));
   3.230 +        inbox_mails.clear();    
   3.231 +
   3.232 +        msg = NULL;
   3.233 +        free_stringlist(keylist);
   3.234 +        flags = 0;
   3.235 +        keylist = NULL;
   3.236 +        
   3.237 +        third_device->grab_context(first_device);
   3.238 +
   3.239 +        third_device->check_mail(inbox_list);
   3.240 +        TEST_ASSERT_MSG(inbox_list.size() == 1, 
   3.241 +                        (string("Third device received ") + to_string(inbox_list.size()) + " emails, should have received 1.").c_str());
   3.242 +            
   3.243 +        third_device->read_mail(inbox_list, inbox_mails);
   3.244 +        TEST_ASSERT(inbox_mails.size() == 1 && inbox_mails.at(0));
   3.245 +        
   3.246 +        status = decrypt_message(third_device->session,
   3.247 +                                 inbox_mails.at(0), &msg, &keylist,
   3.248 +                                 &rating, &flags);
   3.249 +        TEST_ASSERT_MSG(status == PEP_STATUS_OK, tl_status_string(status));
   3.250 +        TEST_ASSERT_MSG(rating == PEP_rating_reliable, tl_rating_string(rating));
   3.251 +        TEST_ASSERT(msg);
   3.252 +
   3.253 +        free_message(msg);
   3.254 +        inbox_list.clear();
   3.255 +        free(inbox_mails.at(0));
   3.256 +        inbox_mails.clear();    
   3.257 +        free_stringlist(keylist);
   3.258 +    }
   3.259 +    catch (const runtime_error& error) {
   3.260 +        TEST_ASSERT_MSG(false, error.what());
   3.261 +    }
   3.262 +}
   3.263 +
   3.264 +void DeviceModelTests::check_switch_context() {
   3.265 +    try {
   3.266 +        // Set up devices and shared mailbox
   3.267 +        pEpTestDevice* first_device = new pEpTestDevice(temp_test_home, "Alex");
   3.268 +        devices.push_back(first_device);    
   3.269 +        first_device->set_mailbox_dir(first_device->device_dir + "/mbox");
   3.270 +        string alex_email = "alex@darthmama.cool";
   3.271 +        pEp_identity* alex_identity = new_identity(alex_email.c_str(), NULL, "AlexID", "Alex Braithwaite");
   3.272 +        PEP_STATUS status = myself(first_device->session, alex_identity);
   3.273 +        
   3.274 +        pEpTestDevice* second_device = new pEpTestDevice(temp_test_home, "Bree");
   3.275 +        devices.push_back(second_device);    
   3.276 +        second_device->set_mailbox_dir(second_device->device_dir + "/mbox");
   3.277 +        string bree_email = "bree@cheese.melted";
   3.278 +        pEp_identity* bree_identity = new_identity(bree_email.c_str(), NULL, "BreeID", "Briana Cheeserton");
   3.279 +        status = myself(second_device->session, bree_identity);
   3.280 +        
   3.281 +        pEpTestDevice* third_device = new pEpTestDevice(temp_test_home, "Charmander");
   3.282 +        devices.push_back(third_device);    
   3.283 +        third_device->set_mailbox_dir(third_device->device_dir + "/mbox");
   3.284 +        string charm_email = "charmander@poke.mon";
   3.285 +        pEp_identity* charm_identity = new_identity(charm_email.c_str(), NULL, "CharmID", "Charmander T. Pokemon");
   3.286 +        status = myself(third_device->session, charm_identity);
   3.287 +        
   3.288 +        pEpTestDevice::switch_context(first_device);
   3.289 +
   3.290 +        map<string,string> address_maps = {
   3.291 +            {alex_email,first_device->mbox_dir},
   3.292 +            {bree_email,second_device->mbox_dir},
   3.293 +            {charm_email,third_device->mbox_dir},
   3.294 +        };
   3.295 +
   3.296 +        // this just simulates the ability to address and deliver, so everyone has
   3.297 +        // the same maps.
   3.298 +        first_device->address_to_mbox_map = second_device->address_to_mbox_map =
   3.299 +            third_device->address_to_mbox_map = address_maps;
   3.300 +        // Note to self - I'll bet this is some C++ mem nightmare.
   3.301 +        
   3.302 +        message* msg = new_message(PEP_dir_outgoing);
   3.303 +        msg->from = identity_dup(alex_identity);
   3.304 +        msg->to = new_identity_list(new_identity(bree_email.c_str(), NULL, "ItsBree", "Bree Cheeserton"));
   3.305 +        msg->shortmsg = strdup("First test message!");
   3.306 +        msg->longmsg = strdup("Yo Bree! This is Alex! Hi!\nEr, hi!\n");
   3.307 +        msg->attachments = new_bloblist(NULL, 0, "application/octet-stream", NULL);
   3.308 +
   3.309 +        // engine_passthrough
   3.310 +        message* enc_msg = NULL;
   3.311 +        status = encrypt_message(pEpTestDevice::active->session, msg, NULL, &enc_msg, PEP_enc_PGP_MIME, 0);
   3.312 +        TEST_ASSERT(status == PEP_UNENCRYPTED);
   3.313 +        TEST_ASSERT(enc_msg == NULL);
   3.314 +
   3.315 +        // when sent, msg is freed, so do NOT free it after this.
   3.316 +        pEpTestDevice::active->add_message_to_send_queue(msg);
   3.317 +        pEpTestDevice::switch_context(second_device);
   3.318 +
   3.319 +        msg = NULL;
   3.320 +        
   3.321 +        vector<string> inbox_list;
   3.322 +        pEpTestDevice::active->check_mail(inbox_list);
   3.323 +        TEST_ASSERT_MSG(inbox_list.size() == 1, 
   3.324 +                        (string("Second device received ") + to_string(inbox_list.size()) + " emails, should have received 1.").c_str());
   3.325 +
   3.326 +        vector<message*> inbox_mails;
   3.327 +        stringlist_t* keylist = NULL;
   3.328 +        PEP_rating rating;
   3.329 +        PEP_decrypt_flags_t flags;
   3.330 +        
   3.331 +        pEpTestDevice::active->read_mail(inbox_list, inbox_mails);
   3.332 +        TEST_ASSERT(inbox_mails.size() == 1 && inbox_mails.at(0));
   3.333 +        
   3.334 +        // Get Alex's key
   3.335 +        status = decrypt_message(pEpTestDevice::active->session,
   3.336 +                                 inbox_mails.at(0), &msg, &keylist,
   3.337 +                                 &rating, &flags);
   3.338 +        TEST_ASSERT(status == PEP_UNENCRYPTED);
   3.339 +        TEST_ASSERT(msg == NULL);
   3.340 +
   3.341 +        inbox_list.clear();
   3.342 +        free(inbox_mails.at(0));
   3.343 +        inbox_mails.clear();
   3.344 +        
   3.345 +        pEpTestDevice::switch_context(third_device);
   3.346 +            
   3.347 +        msg = new_message(PEP_dir_outgoing);
   3.348 +        msg->from = identity_dup(charm_identity);
   3.349 +        msg->to = new_identity_list(new_identity(bree_email.c_str(), NULL, "SuperBree", "Bree Cheeserton"));
   3.350 +        msg->shortmsg = strdup("First test message!");
   3.351 +        msg->longmsg = strdup("Yo Bree! This is Charmander! I'm a cool Pokemon! Hi!\nEr, hi!\n");
   3.352 +        msg->attachments = new_bloblist(NULL, 0, "application/octet-stream", NULL);
   3.353 +
   3.354 +        // engine_passthrough
   3.355 +        enc_msg = NULL;
   3.356 +        status = encrypt_message(pEpTestDevice::active->session, msg, NULL, &enc_msg, PEP_enc_PGP_MIME, 0);
   3.357 +        TEST_ASSERT(status == PEP_UNENCRYPTED);
   3.358 +        TEST_ASSERT(enc_msg == NULL);
   3.359 +
   3.360 +        // when sent, msg is freed, so do NOT free it after this.
   3.361 +        pEpTestDevice::active->add_message_to_send_queue(msg);
   3.362 +
   3.363 +        pEpTestDevice::switch_context(second_device);
   3.364 +        msg = NULL;
   3.365 +            
   3.366 +        pEpTestDevice::active->check_mail(inbox_list);
   3.367 +        TEST_ASSERT_MSG(inbox_list.size() == 1, 
   3.368 +                        (string("Second device received ") + to_string(inbox_list.size()) + " emails, should have received 1.").c_str());
   3.369 +    
   3.370 +        keylist = NULL;
   3.371 +        flags = 0;
   3.372 +        
   3.373 +        pEpTestDevice::active->read_mail(inbox_list, inbox_mails);
   3.374 +        TEST_ASSERT(inbox_mails.size() == 1 && inbox_mails.at(0));
   3.375 +        
   3.376 +        // Get Charmander's key
   3.377 +        status = decrypt_message(pEpTestDevice::active->session,
   3.378 +                                 inbox_mails.at(0), &msg, &keylist,
   3.379 +                                 &rating, &flags);
   3.380 +        TEST_ASSERT(status == PEP_UNENCRYPTED);
   3.381 +        TEST_ASSERT(msg == NULL);
   3.382 +
   3.383 +        inbox_list.clear();
   3.384 +        free(inbox_mails.at(0));
   3.385 +        inbox_mails.clear();    
   3.386 +
   3.387 +        // Ok, now, revenge of encrypting Bree
   3.388 +        msg = new_message(PEP_dir_outgoing);
   3.389 +        msg->from = identity_dup(bree_identity);
   3.390 +        msg->to = new_identity_list(new_identity(alex_email.c_str(), NULL, "Alexei", "Alex Braithwaite is a char in a bad novel"));
   3.391 +        identity_list_add(msg->to, new_identity(charm_email.c_str(), NULL, "Charming", "Charmanderpoke E. Mon is NOT a Pokemon"));
   3.392 +        msg->shortmsg = strdup("Last test message!");
   3.393 +        msg->longmsg = strdup("You guys are fools :)\n");
   3.394 +        msg->attachments = new_bloblist(NULL, 0, "application/octet-stream", NULL);
   3.395 +
   3.396 +        enc_msg = NULL;
   3.397 +        status = encrypt_message(pEpTestDevice::active->session, msg, NULL, &enc_msg, PEP_enc_PGP_MIME, 0);
   3.398 +        TEST_ASSERT(status == PEP_STATUS_OK);
   3.399 +        TEST_ASSERT(enc_msg);
   3.400 +         
   3.401 +        free_message(msg);
   3.402 +        msg = NULL;
   3.403 +        free_stringlist(keylist);
   3.404 +        flags = 0;
   3.405 +        keylist = NULL;
   3.406 +        
   3.407 +        // when sent, enc_msg is freed, so do NOT free it after this.
   3.408 +        pEpTestDevice::active->add_message_to_send_queue(enc_msg);
   3.409 +
   3.410 +        pEpTestDevice::switch_context(first_device);
   3.411 +                
   3.412 +        enc_msg = NULL;
   3.413 +
   3.414 +        pEpTestDevice::active->check_mail(inbox_list);
   3.415 +        TEST_ASSERT_MSG(inbox_list.size() == 1, 
   3.416 +                        (string("First device received ") + to_string(inbox_list.size()) + " emails, should have received 1.").c_str());
   3.417 +            
   3.418 +        pEpTestDevice::active->read_mail(inbox_list, inbox_mails);
   3.419 +        TEST_ASSERT(inbox_mails.size() == 1 && inbox_mails.at(0));
   3.420 +        
   3.421 +        status = decrypt_message(pEpTestDevice::active->session,
   3.422 +                                 inbox_mails.at(0), &msg, &keylist,
   3.423 +                                 &rating, &flags);
   3.424 +        TEST_ASSERT_MSG(status == PEP_STATUS_OK, tl_status_string(status));
   3.425 +        TEST_ASSERT_MSG(rating == PEP_rating_reliable, tl_rating_string(rating));
   3.426 +        TEST_ASSERT(msg);
   3.427 +
   3.428 +        free_message(msg);
   3.429 +        inbox_list.clear();
   3.430 +        free(inbox_mails.at(0));
   3.431 +        inbox_mails.clear();    
   3.432 +
   3.433 +        msg = NULL;
   3.434 +        free_stringlist(keylist);
   3.435 +        flags = 0;
   3.436 +        keylist = NULL;
   3.437 +        
   3.438 +        pEpTestDevice::switch_context(third_device);
   3.439 +
   3.440 +        pEpTestDevice::active->check_mail(inbox_list);
   3.441 +        TEST_ASSERT_MSG(inbox_list.size() == 1, 
   3.442 +                        (string("Third device received ") + to_string(inbox_list.size()) + " emails, should have received 1.").c_str());
   3.443 +            
   3.444 +        pEpTestDevice::active->read_mail(inbox_list, inbox_mails);
   3.445 +        TEST_ASSERT(inbox_mails.size() == 1 && inbox_mails.at(0));
   3.446 +        
   3.447 +        status = decrypt_message(pEpTestDevice::active->session,
   3.448 +                                 inbox_mails.at(0), &msg, &keylist,
   3.449 +                                 &rating, &flags);
   3.450 +        TEST_ASSERT_MSG(status == PEP_STATUS_OK, tl_status_string(status));
   3.451 +        TEST_ASSERT_MSG(rating == PEP_rating_reliable, tl_rating_string(rating));
   3.452 +        TEST_ASSERT(msg);
   3.453 +
   3.454 +        free_message(msg);
   3.455 +        inbox_list.clear();
   3.456 +        free(inbox_mails.at(0));
   3.457 +        inbox_mails.clear();    
   3.458 +        free_stringlist(keylist);
   3.459 +    }
   3.460 +    catch (const runtime_error& error) {
   3.461 +        TEST_ASSERT_MSG(false, error.what());
   3.462 +    }
   3.463 +}
     4.1 --- a/test/src/pEpTestDevice.cc	Wed Feb 20 11:08:31 2019 +0100
     4.2 +++ b/test/src/pEpTestDevice.cc	Fri Feb 22 12:58:03 2019 +0100
     4.3 @@ -23,6 +23,8 @@
     4.4  #include "mime.h"
     4.5  #include <chrono>
     4.6  
     4.7 +pEpTestDevice* pEpTestDevice::active = NULL;
     4.8 +
     4.9  pEpTestDevice::pEpTestDevice(string test_dir, 
    4.10                               string my_name,
    4.11                               messageToSend_t mess_send_func,
    4.12 @@ -36,6 +38,8 @@
    4.13      device_messageToSend = mess_send_func;
    4.14      device_inject_sync_event = inject_sync_ev_func;
    4.15      
    4.16 +    if (active)
    4.17 +        active->unset_device_environment();
    4.18      set_device_environment();    
    4.19  }
    4.20  
    4.21 @@ -45,7 +49,12 @@
    4.22      nftw((device_dir).c_str(), util_delete_filepath, 100, FTW_DEPTH);
    4.23  }
    4.24  
    4.25 +void pEpTestDevice::switch_context(pEpTestDevice* switch_to) {
    4.26 +    switch_to->grab_context(active);
    4.27 +}
    4.28 +
    4.29  void pEpTestDevice::set_device_environment() {
    4.30 +    active = this;
    4.31      int success = 0;
    4.32      struct stat dirchk;
    4.33      
    4.34 @@ -62,11 +71,11 @@
    4.35          if (!S_ISDIR(dirchk.st_mode))
    4.36              throw std::runtime_error(("The test directory, " + device_dir + "exists, but is not a directory.").c_str()); 
    4.37                      
    4.38 -        struct stat buf;
    4.39 -
    4.40 -        if (stat(device_dir.c_str(), &buf) == 0) {
    4.41 -            int success = nftw((device_dir + "/.").c_str(), util_delete_filepath, 100, FTW_DEPTH);
    4.42 -        }
    4.43 +        // struct stat buf;
    4.44 +        // 
    4.45 +        // if (stat(device_dir.c_str(), &buf) == 0) {
    4.46 +        //     int success = nftw((device_dir + "/.").c_str(), util_delete_filepath, 100, FTW_DEPTH);
    4.47 +        // }
    4.48      }
    4.49      else {
    4.50          int errchk = mkdir(device_dir.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
    4.51 @@ -186,11 +195,13 @@
    4.52  
    4.53  PEP_STATUS pEpTestDevice::process_send_queue() {
    4.54      for (vector<message*>::iterator it = send_queue.begin(); it != send_queue.end(); it++) {
    4.55 -        if (!*it) {
    4.56 +        if (*it) {
    4.57              PEP_STATUS status = send_mail(*it);
    4.58              if (status != PEP_STATUS_OK)
    4.59                  return status;
    4.60          }
    4.61 +        free_message(*it);
    4.62 +        *it = NULL;
    4.63      }
    4.64      send_queue.clear();
    4.65      return PEP_STATUS_OK;
    4.66 @@ -202,8 +213,10 @@
    4.67  
    4.68      struct stat dirchk;
    4.69      
    4.70 -    if (mailbox_path.empty() || stat(mailbox_path.c_str(), &dirchk) != 0)
    4.71 -        throw std::runtime_error("pEpTestDevice: mailbox dir not initialised or removed.");                     
    4.72 +    if (mailbox_path.empty() || stat(mailbox_path.c_str(), &dirchk) != 0) {
    4.73 +        cerr << "ERROR: " << mailbox_path << endl;
    4.74 +        throw std::runtime_error("pEpTestDevice: mailbox dir not initialised or removed."); 
    4.75 +    }                    
    4.76  
    4.77      chrono::milliseconds timestamp = chrono::duration_cast< chrono::milliseconds >(
    4.78                                          chrono::system_clock::now().time_since_epoch());
    4.79 @@ -228,9 +241,10 @@
    4.80      mail_to_read.clear();
    4.81      struct stat dirchk;
    4.82  
    4.83 -    if (mbox_dir.empty() || stat(mbox_dir.c_str(), &dirchk) != 0)
    4.84 +    if (mbox_dir.empty() || stat(mbox_dir.c_str(), &dirchk) != 0) {
    4.85 +        cerr << "ERROR: " << mbox_dir << endl;        
    4.86          throw std::runtime_error("pEpTestDevice: mailbox dir not initialised or removed.");                     
    4.87 -
    4.88 +    }
    4.89      DIR* dir;   
    4.90      dirent* pdir;
    4.91   
    4.92 @@ -294,13 +308,25 @@
    4.93      mbox_last_read = string(unread.back());    
    4.94  }
    4.95  
    4.96 -void pEpTestDevice::read_mail(vector<string> mails, vector<string> &to_read) {
    4.97 +void pEpTestDevice::read_mail(vector<string> mails, vector<message*> &to_read) {
    4.98      to_read.clear();
    4.99      for (vector<string>::iterator it = mails.begin();
   4.100           it != mails.end(); it++) {
   4.101          string mail = slurp(mbox_dir + "/" + *it);
   4.102          if (mail.empty())
   4.103              continue;
   4.104 -        to_read.push_back(mail);
   4.105 +        message* msg = NULL;
   4.106 +        PEP_STATUS status = mime_decode_message(mail.c_str(), mail.size(), &msg);    
   4.107 +        if (status != PEP_STATUS_OK) {
   4.108 +            free(msg);
   4.109 +            continue;
   4.110 +        }
   4.111 +        to_read.push_back(msg);
   4.112      }
   4.113  }
   4.114 +
   4.115 +void pEpTestDevice::add_message_to_send_queue(message* msg) {
   4.116 +    if (!msg)
   4.117 +        return;
   4.118 +    send_queue.push_back(msg);
   4.119 +}