Abandoning this for python tests. sync-test-dev
authorKrista 'DarthMama' Bennett <krista@pep.foundation>
Tue, 08 Oct 2019 13:45:23 +0200
branchsync-test-dev
changeset 41162a81e6f6c53c
parent 4108 36046fdf8106
Abandoning this for python tests.
src/pEpEngine.c
test/src/SyncTwoPartyTest.cc
     1.1 --- a/src/pEpEngine.c	Fri Oct 04 16:18:36 2019 +0200
     1.2 +++ b/src/pEpEngine.c	Tue Oct 08 13:45:23 2019 +0200
     1.3 @@ -11,7 +11,7 @@
     1.4  #include <time.h>
     1.5  #include <stdlib.h>
     1.6  
     1.7 -#define _PEP_SQLITE_DEBUG 1
     1.8 +#define _PEP_SQLITE_DEBUG 0
     1.9  #if _PEP_SQLITE_DEBUG
    1.10  #include <sqlite3.h>
    1.11  #endif
     2.1 --- a/test/src/SyncTwoPartyTest.cc	Fri Oct 04 16:18:36 2019 +0200
     2.2 +++ b/test/src/SyncTwoPartyTest.cc	Tue Oct 08 13:45:23 2019 +0200
     2.3 @@ -10,7 +10,9 @@
     2.4  #include <boost/interprocess/containers/vector.hpp>
     2.5  #include <boost/interprocess/allocators/allocator.hpp>
     2.6  #include <boost/date_time/posix_time/posix_time.hpp>
     2.7 -
     2.8 +#include <boost/interprocess/sync/scoped_lock.hpp>
     2.9 +#include <boost/interprocess/sync/named_mutex.hpp>
    2.10 +    
    2.11  #include "pEpEngine.h"
    2.12  #include "pEp_internal.h"
    2.13  #include "sync_api.h"
    2.14 @@ -34,18 +36,6 @@
    2.15  //its values from the segment
    2.16  typedef boost::interprocess::vector<std::string, ShmemAllocator> MailList;
    2.17  
    2.18 -class LockedMailList
    2.19 -{
    2.20 -public:
    2.21 -    MailList vec;
    2.22 -    boost::interprocess::interprocess_mutex mutex;
    2.23 -
    2.24 -    LockedMailList(ShmemAllocator sm) 
    2.25 -       : vec(sm) 
    2.26 -    {}    
    2.27 -};
    2.28 -
    2.29 -
    2.30  PEP_STATUS SyncTwoParty_message_send_callback(message* msg);
    2.31  int SyncTwoParty_inject_sync_event(SYNC_EVENT ev, void *management);
    2.32  SYNC_EVENT SyncTwoParty_retrieve_next_sync_event(void *management, unsigned threshold);
    2.33 @@ -55,44 +45,56 @@
    2.34  
    2.35  #define SyncTwoParty_segment_name "MessageQueueMem"
    2.36  #define SyncTwoParty_vector_name "MessageQueue"
    2.37 -#define SyncTwoParty_locked_vector_name "LockedMessageQueue"
    2.38 +#define SyncTwoParty_mutex_name "MessageQueueMutex"
    2.39          
    2.40  //The fixture for SyncTwoPartyTest
    2.41  class SyncTwoPartyTest : public ::testing::Test {
    2.42      public:
    2.43          std::deque<SYNC_EVENT> ev_q;
    2.44 -        LockedMailList* mail_queue = NULL;
    2.45 +        MailList* mail_queue = NULL;
    2.46          const char* test_suite_name;
    2.47          std::string test_name;
    2.48          std::string test_path;
    2.49 +        Engine* engine;
    2.50 +        PEP_SESSION session;        
    2.51          // Objects declared here can be used by all tests in the SyncTwoPartyTest suite.
    2.52  };
    2.53  
    2.54  
    2.55  int SyncTwoParty_inject_sync_event(SYNC_EVENT ev, void *management)
    2.56  {
    2.57 -    try {
    2.58 -        ((SyncTwoPartyTest*)SyncTwoParty_fake_this)->ev_q.push_front(ev);
    2.59 -    }
    2.60 -    catch (exception&) {
    2.61 -        return 1;
    2.62 -    }
    2.63 -    return 0;
    2.64 +    PEP_STATUS status = do_sync_protocol_step(((SyncTwoPartyTest*)SyncTwoParty_fake_this)->session, NULL, ev);
    2.65 +    return status == PEP_STATUS_OK ? 0 : 1;
    2.66 +    // try {
    2.67 +    //     ((SyncTwoPartyTest*)SyncTwoParty_fake_this)->ev_q.push_front(ev);
    2.68 +    // }
    2.69 +    // catch (exception&) {
    2.70 +    //     return 1;
    2.71 +    // }
    2.72  }
    2.73  
    2.74  PEP_STATUS SyncTwoParty_message_send_callback(message* msg) {
    2.75      PEP_STATUS status = PEP_STATUS_OK;
    2.76      char* msg_str = NULL;
    2.77      mime_encode_message(msg, false, &msg_str);
    2.78 -    LockedMailList* lml = ((SyncTwoPartyTest*)SyncTwoParty_fake_this)->mail_queue;
    2.79 -    boost::posix_time::ptime end_time = boost::posix_time::second_clock::universal_time() + boost::posix_time::seconds(5);
    2.80 -    bool locked = lml->mutex.timed_lock(end_time);
    2.81 -    if (locked) {
    2.82 -        lml->vec.push_back(string(msg_str));
    2.83 -        lml->mutex.unlock();
    2.84 +    MailList* lml = ((SyncTwoPartyTest*)SyncTwoParty_fake_this)->mail_queue;
    2.85 +    try {
    2.86 +        boost::posix_time::ptime end_time = boost::posix_time::second_clock::universal_time() + boost::posix_time::seconds(5);
    2.87 +
    2.88 +        named_mutex the_mutex = 
    2.89 +        //This will timed_lock the mutex
    2.90 +        boost::interprocess::scoped_lock<named_mutex> lock(), end_time);
    2.91 +
    2.92 +        if (!lock) {
    2.93 +            cerr << "Um, what? An exception should have been thrown." << endl;
    2.94 +        }
    2.95 +        lml->push_back(string(msg_str));
    2.96 +        cerr << ((SyncTwoPartyTest*)SyncTwoParty_fake_this)->test_name << " sent message." << endl << *(lml->end()) << endl;        
    2.97 +    }   
    2.98 +    catch (interprocess_exception e) {
    2.99 +        cerr << ((SyncTwoPartyTest*)SyncTwoParty_fake_this)->test_name << " timed out sending message." << endl;
   2.100 +        status = PEP_UNKNOWN_ERROR;
   2.101      }    
   2.102 -    else 
   2.103 -        status = PEP_UNKNOWN_ERROR;
   2.104      free(msg_str);
   2.105      return status;
   2.106  }
   2.107 @@ -118,9 +120,6 @@
   2.108          break;
   2.109      }
   2.110      
   2.111 -    if (!syncEvent)
   2.112 -        return new_sync_timeout_event();
   2.113 -
   2.114      return syncEvent;
   2.115  }
   2.116  
   2.117 @@ -143,12 +142,10 @@
   2.118      pid_t pid = fork();
   2.119      
   2.120      managed_shared_memory segment;
   2.121 +    managed_shared_memory mutex_segment;
   2.122      
   2.123      PEP_STATUS status = PEP_STATUS_OK;
   2.124 -    
   2.125 -    Engine* engine;
   2.126 -    PEP_SESSION session;
   2.127 -    
   2.128 +        
   2.129      // Create process specific variables and shared mail queue
   2.130      if (pid == 0) { // child
   2.131          sleep(1);
   2.132 @@ -160,46 +157,90 @@
   2.133          
   2.134          // Give the other process a while to set up the queue
   2.135          while (curr - start < 10) {
   2.136 +            cout << curr - start << endl;
   2.137              // Try stuff here
   2.138              try {
   2.139                  //Open the managed segment
   2.140 -                segment = managed_shared_memory(open_only, SyncTwoParty_segment_name);  
   2.141 +                segment = managed_shared_memory(open_or_create, SyncTwoParty_segment_name, 1048576);  
   2.142 +                mutex_segment = managed_shared_memory(open_or_create, "MutexMemory", 65536);                
   2.143              }
   2.144 -            catch (interprocess_exception e) {    
   2.145 +            catch (interprocess_exception e) {
   2.146 +                cout << e.what() << endl;
   2.147 +                sleep(1);   
   2.148                  curr = time(NULL);
   2.149                  continue;    
   2.150              }
   2.151              break;
   2.152          }   
   2.153  
   2.154 -        if ((mail_queue = (segment.find<LockedMailList>(SyncTwoParty_locked_vector_name).first)) == NULL) {
   2.155 -            cerr << "CHILD UNABLE TO OPEN SHARED MEMORY SEGMENT" << endl;
   2.156 +        if ((mail_queue = (segment.find<MailList>(SyncTwoParty_vector_name).first)) == NULL) {        
   2.157 +            cerr << "CHILD UNABLE TO OPEN SHARED MEMORY SEGMENT: " << SyncTwoParty_segment_name << " - " << SyncTwoParty_vector_name << endl;
   2.158              exit(-1);
   2.159          }
   2.160 +        else {
   2.161 +            cout << "OPENED IT!" << endl;
   2.162 +        }
   2.163      }
   2.164      else if (pid > 0) { // parent
   2.165          test_name = (test_name + "_0");
   2.166 -        
   2.167 -        //Remove shared memory on construction and destruction        
   2.168 -        struct shm_remove {
   2.169 -            shm_remove() { shared_memory_object::remove(SyncTwoParty_segment_name); }
   2.170 -            ~shm_remove(){ shared_memory_object::remove(SyncTwoParty_segment_name); }
   2.171 -        } remover;
   2.172 +        cout << "Got to " << test_name << endl;
   2.173  
   2.174 -        //Create a new segment with given name and size
   2.175 -        segment = managed_shared_memory(create_only, SyncTwoParty_segment_name, 1048576);
   2.176 +        // Apparently, this is unnecessary?
   2.177 +        // //Remove shared memory on construction and destruction        
   2.178 +        // struct shm_remove {
   2.179 +        //     shm_remove() { shared_memory_object::remove(SyncTwoParty_segment_name); }
   2.180 +        //     ~shm_remove(){ shared_memory_object::remove(SyncTwoParty_segment_name); }
   2.181 +        // } remover;
   2.182 +        // 
   2.183 +        // struct mutex_remove
   2.184 +        // {
   2.185 +        //     mutex_remove() { named_mutex::remove("fstream_named_mutex"); }
   2.186 +        //     ~mutex_remove(){ named_mutex::remove("fstream_named_mutex"); }
   2.187 +        // } mutex_remover;
   2.188  
   2.189 +        try {
   2.190 +            //Create a new segment with given name and size
   2.191 +            segment = managed_shared_memory(open_or_create, SyncTwoParty_segment_name, 1048576);
   2.192 +            
   2.193 +            //Create a new segment with given name and size
   2.194 +            mutex_segment = managed_shared_memory(open_or_create, "MutexMemory", 65536);
   2.195 +            
   2.196 +        }    
   2.197 +        catch (interprocess_exception e) {
   2.198 +            cerr << "PARENT UNABLE TO OPEN SHARED MEMORY SEGMENT" << endl;            
   2.199 +            int status;    
   2.200 +            wait(&status);
   2.201 +            exit(-1);
   2.202 +        }
   2.203 +        sleep(1);
   2.204          //Initialize shared memory STL-compatible allocator
   2.205          const ShmemAllocator alloc_inst(segment.get_segment_manager());
   2.206  
   2.207          //Construct a vector named "MailList" in shared memory with argument alloc_inst
   2.208 -        mail_queue = segment.construct<LockedMailList>(SyncTwoParty_locked_vector_name)(alloc_inst);
   2.209 +        mail_queue = segment.construct<MailList>(SyncTwoParty_vector_name)(alloc_inst);
   2.210 +                
   2.211 +        cerr << SyncTwoParty_segment_name << " - " << SyncTwoParty_vector_name << endl;
   2.212 +        
   2.213      }
   2.214      else {
   2.215          // OMGWTFBBQ
   2.216          exit(-1);
   2.217      }
   2.218 +
   2.219 +
   2.220 +    named_mutex mutex(open_or_create, SyncTwoParty_mutex_name);        
   2.221 +
   2.222 +    //Open or create the named mutex
   2.223 +    try {
   2.224 +        mutex_ptr = &mutex;
   2.225 +    }
   2.226 +    catch (interprocess_exception e) {
   2.227 +        cerr << "WTF? " << test_name << ":" << e.what();
   2.228 +        exit(-1);
   2.229 +    }    
   2.230      
   2.231 +    cout << test_name << endl;
   2.232 +
   2.233      // After this, this all happens in separate address spaces, so fake_this is safe
   2.234  
   2.235      SyncTwoParty_fake_this = this;
   2.236 @@ -228,6 +269,7 @@
   2.237      // Generate new identity for this device
   2.238      pEp_identity* me = new_identity("pickles@boofy.org", NULL, PEP_OWN_USERID, "Pickley Boofboof");
   2.239      status = myself(session, me);
   2.240 +    cout << test_name << ": " << me->fpr << endl;
   2.241      assert(status == PEP_STATUS_OK && me->fpr != NULL && me->fpr[0] != '\0');
   2.242  
   2.243      status = register_sync_callbacks(session, NULL, &SyncTwoParty_notify_handshake, &SyncTwoParty_retrieve_next_sync_event);
   2.244 @@ -245,16 +287,22 @@
   2.245          bool msg_received = false;
   2.246          bool event_processed = false;
   2.247          
   2.248 -        boost::posix_time::ptime end_time = boost::posix_time::second_clock::universal_time() + boost::posix_time::seconds(5);
   2.249 -        bool locked = mail_queue->mutex.timed_lock(end_time);
   2.250 +        if (mail_queue->size() > last_message_index) {
   2.251 +            try {
   2.252 +                boost::posix_time::ptime end_time = boost::posix_time::second_clock::universal_time() + boost::posix_time::seconds(5);
   2.253  
   2.254 -        if (locked) {
   2.255 -            if (mail_queue->vec.size() > last_message_index) {
   2.256 +                //This will timed_lock the mutex
   2.257 +                boost::interprocess::scoped_lock<named_mutex> the_lock(*mutex_ptr, end_time);
   2.258 +
   2.259 +                if (!the_lock) {
   2.260 +                    cerr << "Um, what? An exception should have been thrown." << endl;
   2.261 +                }
   2.262 +                
   2.263                  message* next_msg = NULL;
   2.264                  PEP_decrypt_flags_t flags = 0;
   2.265                  PEP_rating rating;
   2.266                  stringlist_t* keylist = NULL;
   2.267 -                std::string msg_str = mail_queue->vec.at(last_message_index++);
   2.268 +                std::string msg_str = mail_queue->at(last_message_index++);
   2.269                  message* actual_msg = NULL;
   2.270                  mime_decode_message(msg_str.c_str(), msg_str.length(), &actual_msg);
   2.271                  status = decrypt_message(session, actual_msg, &next_msg, &keylist, &rating, &flags);
   2.272 @@ -267,18 +315,23 @@
   2.273                  free_message(next_msg);
   2.274                  msg_received = true;
   2.275              }
   2.276 -            mail_queue->mutex.unlock();
   2.277 +            catch (interprocess_exception e) {
   2.278 +                cerr << test_name << " trying to lock queue. " << e.what() << endl;
   2.279 +            }
   2.280          }    
   2.281              
   2.282 +        // This doesn't mean anything anymore I think...
   2.283 +        /*    
   2.284          event = session->retrieve_next_sync_event(session->sync_management,
   2.285                  SYNC_THRESHOLD);
   2.286                  
   2.287          if (event) {
   2.288              event_processed = true;
   2.289              do_sync_protocol_step(session, NULL, event);
   2.290 -        }    
   2.291 +        } 
   2.292 +        */   
   2.293          now = time(NULL);
   2.294 -        if (msg_received || event_processed)
   2.295 +        if (msg_received)
   2.296              prev_change = now;
   2.297      } 
   2.298      
   2.299 @@ -302,7 +355,7 @@
   2.300          }   
   2.301          
   2.302          // When done, destroy the vector from the segment
   2.303 -        segment.destroy<LockedMailList>(SyncTwoParty_locked_vector_name);    
   2.304 +        segment.destroy<MailList>(SyncTwoParty_vector_name);    
   2.305      }
   2.306      else {
   2.307          engine->shut_down();