Er, ok, what? Reopening branch, I guess sync_test_refactor
authorKrista 'DarthMama' Bennett <krista@pep.foundation>
Mon, 04 Mar 2019 14:13:38 +0100
branchsync_test_refactor
changeset 3329112f50ef2979
parent 3323 28774889e2dd
child 3330 cfe45219cf84
Er, ok, what? Reopening branch, I guess
test/include/SyncDeviceTests.h
test/include/TestUtils.h
test/include/pEpTestDevice.h
test/src/TestUtils.cc
test/src/engine_tests/SyncDeviceTests.cc
test/src/pEpTestDevice.cc
     1.1 --- a/test/include/SyncDeviceTests.h	Tue Feb 26 08:53:50 2019 +0100
     1.2 +++ b/test/include/SyncDeviceTests.h	Mon Mar 04 14:13:38 2019 +0100
     1.3 @@ -5,16 +5,25 @@
     1.4  #define SYNC_DEVICE_H
     1.5  
     1.6  #include <string>
     1.7 +#include <vector>
     1.8  #include "EngineTestIndividualSuite.h"
     1.9 +#include "pEpTestDevice.h"
    1.10  
    1.11  using namespace std;
    1.12  
    1.13  class SyncDeviceTests : public EngineTestIndividualSuite {
    1.14      public:
    1.15          SyncDeviceTests(string test_suite, string test_home_dir);
    1.16 -                
    1.17 +        
    1.18 +        vector<pEpTestDevice*> device_queue;
    1.19 +
    1.20 +    protected:
    1.21 +        void setup();
    1.22 +        void tear_down();        
    1.23      private:
    1.24 -        void check_sync_device();
    1.25 +        void check_sync_two_devices();
    1.26 +        void check_sync_three_devices();
    1.27 +        void check_sync_two_grouped_devices();
    1.28  };
    1.29  
    1.30  #endif
     2.1 --- a/test/include/TestUtils.h	Tue Feb 26 08:53:50 2019 +0100
     2.2 +++ b/test/include/TestUtils.h	Mon Mar 04 14:13:38 2019 +0100
     2.3 @@ -5,6 +5,7 @@
     2.4  #include <stdlib.h>
     2.5  #include <sys/stat.h>
     2.6  #include <ftw.h>
     2.7 +#include <vector>
     2.8  
     2.9  #include "pEpEngine.h"
    2.10  #include "message_api.h"
    2.11 @@ -55,6 +56,7 @@
    2.12                           const struct stat *file_stat, 
    2.13                           int ftw_info, 
    2.14                           struct FTW * ftw_struct);
    2.15 -                         
    2.16 -        
    2.17 +
    2.18 +void clear_message_vector(std::vector<message*> &msgs);
    2.19 +                                 
    2.20  #endif
     3.1 --- a/test/include/pEpTestDevice.h	Tue Feb 26 08:53:50 2019 +0100
     3.2 +++ b/test/include/pEpTestDevice.h	Mon Mar 04 14:13:38 2019 +0100
     3.3 @@ -51,6 +51,8 @@
     3.4          // Read all mails (by filename) into a vector of strings
     3.5          void read_mail(vector<string>mails, vector<message*> &to_read);
     3.6          
     3.7 +        void delete_mail(string fname);
     3.8 +
     3.9          // write everything into the correct mail by mailbox.
    3.10          // PRESUMES address_to_mbox_map HAS AN ENTRY FOR EVERY ADDRESS IN 
    3.11          // THE RECIP LIST
     4.1 --- a/test/src/TestUtils.cc	Tue Feb 26 08:53:50 2019 +0100
     4.2 +++ b/test/src/TestUtils.cc	Mon Mar 04 14:13:38 2019 +0100
     4.3 @@ -14,6 +14,7 @@
     4.4  #include <stdlib.h>
     4.5  #include <unistd.h>
     4.6  #include <ftw.h>
     4.7 +#include <vector>
     4.8  
     4.9  PEP_STATUS read_file_and_import_key(PEP_SESSION session, const char* fname) {
    4.10      const std::string key = slurp(fname);
    4.11 @@ -454,3 +455,11 @@
    4.12      
    4.13      return retval;
    4.14  }
    4.15 +
    4.16 +void clear_message_vector(std::vector<message*> &msgs) {
    4.17 +    for (std::vector<message*>::iterator it = msgs.begin();
    4.18 +         it != msgs.end(); it++) {
    4.19 +        free_message(*it);        
    4.20 +    }
    4.21 +    msgs.clear();
    4.22 +}
     5.1 --- a/test/src/engine_tests/SyncDeviceTests.cc	Tue Feb 26 08:53:50 2019 +0100
     5.2 +++ b/test/src/engine_tests/SyncDeviceTests.cc	Mon Mar 04 14:13:38 2019 +0100
     5.3 @@ -9,9 +9,7 @@
     5.4  #include <cstring>
     5.5  
     5.6  #include "pEpEngine.h"
     5.7 -
     5.8  #include "TestUtils.h"
     5.9 -
    5.10  #include "pEpTestDevice.h"
    5.11  
    5.12  #include "EngineTestIndividualSuite.h"
    5.13 @@ -19,13 +17,224 @@
    5.14  
    5.15  using namespace std;
    5.16  
    5.17 -SyncDeviceTests::SyncDeviceTests(string suitename, string test_home_dir) :
    5.18 -    EngineTestIndividualSuite::EngineTestIndividualSuite(suitename, test_home_dir) {
    5.19 -    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("SyncDeviceTests::check_sync_device"),
    5.20 -                                                                      static_cast<Func>(&SyncDeviceTests::check_sync_device)));
    5.21 +SyncDeviceTests::SyncDeviceTests(string suitename, string test_home_dir)  :
    5.22 +    EngineTestIndividualSuite::EngineTestIndividualSuite(suitename, test_home_dir, false) {
    5.23 +    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("SyncDeviceTests::check_sync_two_devices"),
    5.24 +                                                                      static_cast<Func>(&SyncDeviceTests::check_sync_two_devices)));
    5.25 +    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("SyncDeviceTests::check_sync_three_devices"),
    5.26 +                                                                      static_cast<Func>(&SyncDeviceTests::check_sync_three_devices)));
    5.27 +    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("SyncDeviceTests::check_sync_two_grouped_devices"),
    5.28 +                                                                      static_cast<Func>(&SyncDeviceTests::check_sync_two_grouped_devices)));
    5.29  }
    5.30  
    5.31 -void SyncDeviceTests::check_sync_device() {
    5.32 +void SyncDeviceTests::setup() {
    5.33 +    EngineTestIndividualSuite::setup();
    5.34 +}
    5.35 +
    5.36 +void SyncDeviceTests::tear_down() {
    5.37 +    for (vector<pEpTestDevice*>::iterator it = device_queue.begin();
    5.38 +         it != device_queue.end(); it++) {
    5.39 +        delete(*it);
    5.40 +    }
    5.41 +    device_queue.clear();
    5.42 +    EngineTestIndividualSuite::tear_down();
    5.43 +}
    5.44 +
    5.45 +void SyncDeviceTests::check_sync_two_devices(){
    5.46 +    PEP_STATUS status = PEP_STATUS_OK;
    5.47 +
    5.48 +    pEpTestDevice* first;
    5.49 +    pEpTestDevice* second;
    5.50 +    
    5.51 +    device_queue.push_back(first = new pEpTestDevice(temp_test_home, "One"));
    5.52 +    string mbox = first->device_dir + "/mbox";
    5.53 +    first->set_mailbox_dir(mbox);
    5.54 +    
    5.55 +    device_queue.push_back(second = new pEpTestDevice(temp_test_home, "Two"));
    5.56 +
    5.57 +    second->set_mailbox_dir(mbox);
    5.58 +    first->address_to_mbox_map = second->address_to_mbox_map =
    5.59 +        {{"owen.corley@fort-tarsis.darthmama.org", mbox}};
    5.60 +    
    5.61 +    const char* addr = "owen.corley@fort-tarsis.darthmama.org";
    5.62 +    const char* uname_1 = "Owen Corley";
    5.63 +    const char* uname_2 = "Owen 'Sandwiches' Corley";
    5.64 +    const char* uid1 = "Owen_One";
    5.65 +    const char* uid2 = "Owen_Two";
    5.66 +    
    5.67 +    pEpTestDevice::switch_context(first);
    5.68 +    pEp_identity* owen_first = new_identity(addr, NULL, uid1, uname_1);
    5.69 +    status = myself(first->session, owen_first);
    5.70 +    TEST_ASSERT_MSG(status == PEP_STATUS_OK, tl_status_string(status));
    5.71 +    TEST_ASSERT(owen_first->fpr && *(owen_first->fpr) != '\0');
    5.72 +    string owen_1_fpr = owen_first->fpr;    
    5.73 +        
    5.74 +    pEpTestDevice::switch_context(second);
    5.75 +    pEp_identity* owen_second = new_identity(addr, NULL, uid2, uname_2);
    5.76 +    status = myself(second->session, owen_second);
    5.77 +    TEST_ASSERT_MSG(status == PEP_STATUS_OK, tl_status_string(status));
    5.78 +    TEST_ASSERT(owen_second->fpr && *(owen_second->fpr) != '\0');
    5.79 +    string owen_2_fpr = owen_second->fpr;    
    5.80 +
    5.81 +    TEST_ASSERT_MSG(owen_1_fpr.compare(owen_2_fpr) != 0,
    5.82 +                    "Only one fpr generated for two separate devices. This is a pEpTestDevice error.");
    5.83 +                    
    5.84 +    // Ok, so... let's do this? Send to myself. Why not.
    5.85 +    message* trigger_msg = new_message(PEP_dir_outgoing);
    5.86 +    trigger_msg->from = identity_dup(owen_second);
    5.87 +    trigger_msg->to = new_identity_list(new_identity(addr, NULL, uid2, uname_2));
    5.88 +    trigger_msg->shortmsg = strdup("Remember! Javelins look cool!");
    5.89 +    trigger_msg->longmsg = strdup("Make sure to tell the Freelancer her interceptor looks better with a gold butt.\n");
    5.90 +    trigger_msg->attachments = new_bloblist(NULL, 0, "application/octet-stream", NULL);
    5.91 +                
    5.92 +    message* enc_msg = NULL;
    5.93 +    status = encrypt_message(pEpTestDevice::active->session, trigger_msg, NULL,
    5.94 +                             &enc_msg, PEP_enc_PGP_MIME, 0);
    5.95 +    
    5.96 +    TEST_ASSERT_MSG(status == PEP_STATUS_OK, tl_status_string(status));                                 
    5.97 +    TEST_ASSERT(enc_msg);
    5.98 +    
    5.99 +    pEpTestDevice::active->add_message_to_send_queue(enc_msg);
   5.100 +    TEST_ASSERT_MSG(pEpTestDevice::active->send_queue.size() > 0,
   5.101 +                    "Empty send queue!!! :(");
   5.102 +    
   5.103 +    pEpTestDevice::switch_context(first);
   5.104 +
   5.105 +    vector<string> inbox_list;
   5.106 +    vector<message*> inbox_mails;
   5.107 +    
   5.108 +    pEpTestDevice::active->check_mail(inbox_list);
   5.109 +    TEST_ASSERT(inbox_list.size() > 0);    
   5.110 +    pEpTestDevice::active->read_mail(inbox_list, inbox_mails);
   5.111 +    TEST_ASSERT(inbox_mails.size() > 0);
   5.112 +    
   5.113 +    vector<message*> decrypted_mails;
   5.114 +    
   5.115 +    stringlist_t* keylist = NULL;
   5.116 +    PEP_rating rating;
   5.117 +    PEP_decrypt_flags_t flags = 0;     
   5.118 +    message* dec_msg = NULL;
   5.119 +
   5.120 +    status = decrypt_message(pEpTestDevice::active->session,
   5.121 +                             inbox_mails.at(0), &dec_msg, &keylist,
   5.122 +                             &rating, &flags);
   5.123 +
   5.124 +    TEST_ASSERT(status == PEP_DECRYPT_NO_KEY);
   5.125 +
   5.126 +    free_stringlist(keylist);
   5.127 +    inbox_list.clear();
   5.128 +    clear_message_vector(inbox_mails);
   5.129 +
   5.130 +    // Ok, we couldn't decrypt the message. So, uh, let's see what sync does
   5.131 +    pEpTestDevice::switch_context(second);
   5.132 +    
   5.133 +    pEpTestDevice::active->check_mail(inbox_list);
   5.134 +    TEST_ASSERT(inbox_list.size() > 0);    
   5.135 +    pEpTestDevice::active->read_mail(inbox_list, inbox_mails);
   5.136 +    TEST_ASSERT(inbox_mails.size() > 0);
   5.137 +    
   5.138 +//    vector<message*> decrypted_mails;
   5.139 +    
   5.140 +    keylist = NULL;
   5.141 +    flags = 0;     
   5.142 +    dec_msg = NULL;
   5.143 +
   5.144 +    status = decrypt_message(pEpTestDevice::active->session,
   5.145 +                             inbox_mails.at(0), &dec_msg, &keylist,
   5.146 +                             &rating, &flags);
   5.147 +
   5.148 +    TEST_ASSERT(status == PEP_STATUS_OK);
   5.149 +
   5.150 +    free_stringlist(keylist);
   5.151 +    keylist = NULL;
   5.152 +    flags = 0;
   5.153 +    free_message(dec_msg);
   5.154 +    dec_msg = NULL;
   5.155 +
   5.156 +    status = decrypt_message(pEpTestDevice::active->session,
   5.157 +                             inbox_mails.at(1), &dec_msg, &keylist,
   5.158 +                             &rating, &flags);
   5.159 +
   5.160 +    TEST_ASSERT_MSG(status == PEP_UNENCRYPTED, tl_status_string(status));
   5.161 +
   5.162 +    TEST_ASSERT(inbox_mails.size() == 2);
   5.163 +
   5.164 +    // Ok, let's let these bad boys talk back and forth 
   5.165 +    bool all_clear = false;
   5.166 +    do {
   5.167 +        for (int i = 0; i < 2; i++) {
   5.168 +            inbox_list.clear();
   5.169 +            clear_message_vector(inbox_mails);    
   5.170 +
   5.171 +            pEpTestDevice* switch_to =
   5.172 +                (i % 2 ? second : first);
   5.173 +            pEpTestDevice::switch_context(switch_to);
   5.174 +
   5.175 +            pEpTestDevice::active->check_mail(inbox_list);
   5.176 +            cout << "inbox_list size is " << inbox_list.size() << endl;
   5.177 +            cout << "i = " << i << endl;
   5.178 +            cout << "all_clear = " << all_clear << endl;
   5.179 +            if (inbox_list.empty() && (i == 0 || all_clear)) {
   5.180 +                cout << "Setting all_clear to true" << endl;
   5.181 +                all_clear = true;
   5.182 +                continue;
   5.183 +            }
   5.184 +            else {
   5.185 +                cout << "Setting all_clear to false" << endl;
   5.186 +                all_clear = false;
   5.187 +            }
   5.188 +                
   5.189 +            pEpTestDevice::active->read_mail(inbox_list, inbox_mails);
   5.190 +            cout << "Inbox mails size is " << inbox_mails.size() << endl;
   5.191 +            vector<string>::iterator fp = inbox_list.begin();
   5.192 +            cout << "WHAT" << endl;
   5.193 +            for (vector<message*>::iterator it = inbox_mails.begin();
   5.194 +                it != inbox_mails.end(); it++) {
   5.195 +                cout << "HELLO!" << endl;
   5.196 +                free_stringlist(keylist);                
   5.197 +                keylist = NULL;
   5.198 +                flags = 0;
   5.199 +                free_message(dec_msg);                      
   5.200 +                dec_msg = NULL;
   5.201 +                 
   5.202 +                status = decrypt_message(pEpTestDevice::active->session,
   5.203 +                                         *it, &dec_msg, &keylist,
   5.204 +                                         &rating, &flags);
   5.205 +                cout << "flags! : " << flags << endl;
   5.206 +                TEST_ASSERT(status == PEP_UNENCRYPTED);
   5.207 +                stringpair_list_t* opt_fields = (*it)->opt_fields;
   5.208 +                while (opt_fields && opt_fields->value && opt_fields->value->key && opt_fields->value->value) {
   5.209 +                    if (strcmp(opt_fields->value->key, "pEp-auto-consume") == 0 &&
   5.210 +                        strcmp(opt_fields->value->value, "yes") == 0) {
   5.211 +                        pEpTestDevice::active->delete_mail(*fp);    
   5.212 +                        break;
   5.213 +                    }
   5.214 +                    opt_fields = opt_fields->next;
   5.215 +                }
   5.216 +                fp++;
   5.217 +            }
   5.218 +            free_stringlist(keylist);                
   5.219 +            keylist = NULL;
   5.220 +            flags = 0;
   5.221 +            free_message(dec_msg);                      
   5.222 +            dec_msg = NULL;
   5.223 +        }
   5.224 +    } while (!all_clear);
   5.225 +    
   5.226 +    // inbox_list.clear();
   5.227 +    // free(inbox_mails.at(0));
   5.228 +    // inbox_mails.clear();    
   5.229 +    // 
   5.230 +    // switch_context(second);
   5.231 +
   5.232 +    
   5.233 +//    TEST_ASSERT(pEpTestDevice::active->send_queue.size() == 1);
   5.234 +}
   5.235 +
   5.236 +void SyncDeviceTests::check_sync_three_devices(){
   5.237      TEST_ASSERT(true);
   5.238  }
   5.239  
   5.240 +void SyncDeviceTests::check_sync_two_grouped_devices(){
   5.241 +    TEST_ASSERT(true);
   5.242 +}
     6.1 --- a/test/src/pEpTestDevice.cc	Tue Feb 26 08:53:50 2019 +0100
     6.2 +++ b/test/src/pEpTestDevice.cc	Mon Mar 04 14:13:38 2019 +0100
     6.3 @@ -59,6 +59,10 @@
     6.4      //     default:    
     6.5      //         return SYNC_HANDSHAKE_REJECTED;
     6.6      // }
     6.7 +    cout << "Notify Handshake on device '" << active->device_name << "' with: " << endl
     6.8 +         << "\tmy name and fpr as\t" << me->username << " : " << me->fpr << " and " << endl 
     6.9 +         << "\tother name/fpr as\t" << partner->username << " : " << partner->fpr << endl;
    6.10 +         
    6.11      return PEP_STATUS_OK;
    6.12  }
    6.13  
    6.14 @@ -405,6 +409,11 @@
    6.15      }
    6.16  }
    6.17  
    6.18 +void pEpTestDevice::delete_mail(string fname) {
    6.19 +    if (!fname.empty())
    6.20 +        remove((mbox_dir + "/" + fname).c_str());
    6.21 +}
    6.22 +
    6.23  void pEpTestDevice::add_message_to_send_queue(message* msg) {
    6.24      if (!msg)
    6.25          return;