Intermittent commit. Argh. sync_test_refactor
authorKrista 'DarthMama' Bennett <krista@pep.foundation>
Sun, 24 Feb 2019 22:41:22 +0100
branchsync_test_refactor
changeset 3313b11d8d025466
parent 3307 b3d26f9f0225
child 3314 18a40b81649a
Intermittent commit. Argh.
test/gentestshell.py
test/include/pEpTestDevice.h
test/src/SuiteMaker.cc
test/src/pEpTestDevice.cc
     1.1 --- a/test/gentestshell.py	Fri Feb 22 12:58:03 2019 +0100
     1.2 +++ b/test/gentestshell.py	Sun Feb 24 22:41:22 2019 +0100
     1.3 @@ -87,7 +87,11 @@
     1.4  if not args.no_src:
     1.5      src_inc = ('#include <stdlib.h>\n'
     1.6                 '#include <string>\n\n'
     1.7 +               '#include <cpptest.h>\n\n'
     1.8 +               '#include <cstring>\n\n'                              
     1.9                 '#include "pEpEngine.h"\n\n'
    1.10 +               '#include "TestUtils.h"\n\n'
    1.11 +               '#include "pEpTestDevice.h"\n\n'
    1.12                 '#include "' + superclass +'.h"\n'
    1.13                 '#include "' + test_suite + '.h"\n\n')
    1.14  
     2.1 --- a/test/include/pEpTestDevice.h	Fri Feb 22 12:58:03 2019 +0100
     2.2 +++ b/test/include/pEpTestDevice.h	Sun Feb 24 22:41:22 2019 +0100
     2.3 @@ -8,14 +8,15 @@
     2.4  #include <utility>
     2.5  #include "pEpEngine.h"
     2.6  #include "message.h"
     2.7 +#include "sync_api.h"
     2.8 +#include "locked_queue.hh"
     2.9 +#include "Sync_event.h"
    2.10  
    2.11  using namespace std;
    2.12  
    2.13  class pEpTestDevice {
    2.14      public:
    2.15 -        pEpTestDevice(string test_dir, string my_name,
    2.16 -                      messageToSend_t mess_send_func = NULL,
    2.17 -                      inject_sync_event_t inject_sync_ev_func = NULL);        
    2.18 +        pEpTestDevice(string test_dir, string my_name);        
    2.19                        
    2.20          virtual ~pEpTestDevice();
    2.21          
    2.22 @@ -65,9 +66,20 @@
    2.23          string device_dir;        
    2.24          string root_test_dir;
    2.25          string mbox_dir;
    2.26 +        utility::locked_queue<Sync_event_t*> q;
    2.27  
    2.28 -        messageToSend_t device_messageToSend;
    2.29 -        inject_sync_event_t device_inject_sync_event;
    2.30 +        static Sync_event_t* retrieve_next_sync_event(void *management, time_t threshold);
    2.31 +        static int notify_handshake(pEp_identity *me,
    2.32 +                                    pEp_identity *partner,
    2.33 +                                    sync_handshake_signal signal);
    2.34 +        
    2.35 +        static int inject_sync_event(SYNC_EVENT ev, void *management);
    2.36 +//        Sync_event_t *retrieve_next_sync_event(void *management, unsigned threshold);
    2.37 +        static PEP_STATUS message_to_send(struct _message *msg);
    2.38 +
    2.39 +
    2.40 +//        messageToSend_t device_messageToSend;
    2.41 +//        inject_sync_event_t device_inject_sync_event;
    2.42          map<string,string> address_to_mbox_map; // maybe string, vector<string>?
    2.43          
    2.44      protected:        
     3.1 --- a/test/src/SuiteMaker.cc	Fri Feb 22 12:58:03 2019 +0100
     3.2 +++ b/test/src/SuiteMaker.cc	Sun Feb 24 22:41:22 2019 +0100
     3.3 @@ -18,6 +18,7 @@
     3.4  #include "Engine463Tests.h"
     3.5  #include "BloblistTests.h"
     3.6  #include "NewUpdateIdAndMyselfTests.h"
     3.7 +#include "SyncDeviceTests.h"
     3.8  #include "I18nTests.h"
     3.9  #include "IdentityListTests.h"
    3.10  #include "PgpBinaryTests.h"
    3.11 @@ -70,6 +71,7 @@
    3.12      "Engine463Tests",
    3.13      "BloblistTests",
    3.14      "NewUpdateIdAndMyselfTests",
    3.15 +    "SyncDeviceTests",
    3.16      "I18nTests",
    3.17      "IdentityListTests",
    3.18      "PgpBinaryTests",
    3.19 @@ -116,7 +118,7 @@
    3.20  };
    3.21  
    3.22  // This file is generated, so magic constants are ok.
    3.23 -int SuiteMaker::num_suites = 49;
    3.24 +int SuiteMaker::num_suites = 50;
    3.25  
    3.26  void SuiteMaker::suitemaker_build(const char* test_class_name, const char* test_home, Test::Suite** test_suite) {
    3.27      if (strcmp(test_class_name, "MimeTests") == 0)
    3.28 @@ -131,6 +133,8 @@
    3.29          *test_suite = new BloblistTests(test_class_name, test_home);
    3.30      else if (strcmp(test_class_name, "NewUpdateIdAndMyselfTests") == 0)
    3.31          *test_suite = new NewUpdateIdAndMyselfTests(test_class_name, test_home);
    3.32 +    else if (strcmp(test_class_name, "SyncDeviceTests") == 0)
    3.33 +        *test_suite = new SyncDeviceTests(test_class_name, test_home);
    3.34      else if (strcmp(test_class_name, "I18nTests") == 0)
    3.35          *test_suite = new I18nTests(test_class_name, test_home);
    3.36      else if (strcmp(test_class_name, "IdentityListTests") == 0)
     4.1 --- a/test/src/pEpTestDevice.cc	Fri Feb 22 12:58:03 2019 +0100
     4.2 +++ b/test/src/pEpTestDevice.cc	Sun Feb 24 22:41:22 2019 +0100
     4.3 @@ -21,25 +21,26 @@
     4.4  #include <algorithm>
     4.5  #include "TestConstants.h"
     4.6  #include "mime.h"
     4.7 +#include "sync_api.h"
     4.8 +#include "Sync_event.h"
     4.9 +#include "locked_queue.hh"
    4.10 +#include "KeySync_fsm.h"
    4.11  #include <chrono>
    4.12  
    4.13  pEpTestDevice* pEpTestDevice::active = NULL;
    4.14  
    4.15  pEpTestDevice::pEpTestDevice(string test_dir, 
    4.16 -                             string my_name,
    4.17 -                             messageToSend_t mess_send_func,
    4.18 -                             inject_sync_event_t inject_sync_ev_func
    4.19 -                             )                
    4.20 +                             string my_name)                
    4.21  {
    4.22      root_test_dir = test_dir;
    4.23      // FIXME: do we have to worry about dirlen now?
    4.24      device_dir = test_dir + "/" + my_name;
    4.25      device_name = my_name;
    4.26 -    device_messageToSend = mess_send_func;
    4.27 -    device_inject_sync_event = inject_sync_ev_func;
    4.28 +    // device_messageToSend = mess_send_func;
    4.29 +    // device_inject_sync_event = inject_sync_ev_func;
    4.30      
    4.31      if (active)
    4.32 -        active->unset_device_environment();
    4.33 +        pEpTestDevice::active->unset_device_environment();
    4.34      set_device_environment();    
    4.35  }
    4.36  
    4.37 @@ -54,7 +55,7 @@
    4.38  }
    4.39  
    4.40  void pEpTestDevice::set_device_environment() {
    4.41 -    active = this;
    4.42 +    pEpTestDevice::active = this;
    4.43      int success = 0;
    4.44      struct stat dirchk;
    4.45      
    4.46 @@ -101,7 +102,14 @@
    4.47      gpg_conf(true);
    4.48      gpg_agent_conf(true);
    4.49          
    4.50 -    PEP_STATUS status = init(&session, device_messageToSend, device_inject_sync_event);
    4.51 +    PEP_STATUS status = init(session, message_to_send, inject_sync_event);
    4.52 +    if (status != PEP_STATUS_OK)
    4.53 +        throw std::runtime_error("init() exploded! Bad!");
    4.54 +    status = register_sync_callbacks(session, (void*)(&(active->q)),
    4.55 +                                     notify_handshake, retrieve_next_sync_event);
    4.56 +    if (status != PEP_STATUS_OK)
    4.57 +        throw std::runtime_error("Couldn't register sync callbacks, possible because we dunno WTF we're doing.");
    4.58 +
    4.59  
    4.60  #ifndef USE_NETPGP            
    4.61      success = system("gpgconf --create-socketdir");
    4.62 @@ -330,3 +338,70 @@
    4.63          return;
    4.64      send_queue.push_back(msg);
    4.65  }
    4.66 +
    4.67 +PEP_STATUS pEpTestDevice::message_to_send(struct _message* msg) {
    4.68 +    pEpTestDevice::active->add_message_to_send_queue(msg);
    4.69 +}
    4.70 +
    4.71 +int pEpTestDevice::notify_handshake(pEp_identity* me,
    4.72 +                                    pEp_identity* partner,
    4.73 +                                    sync_handshake_signal signal) {
    4.74 +                                        
    4.75 +    switch(signal) {
    4.76 +        case SYNC_NOTIFY_UNDEFINED:
    4.77 +        case SYNC_NOTIFY_TIMEOUT:
    4.78 +        case SYNC_NOTIFY_OVERTAKEN:
    4.79 +        case SYNC_NOTIFY_ACCEPTED_DEVICE_ADDED:
    4.80 +        case SYNC_NOTIFY_ACCEPTED_GROUP_CREATED:
    4.81 +        case SYNC_NOTIFY_ACCEPTED_DEVICE_MOVED:   
    4.82 +            return SYNC_HANDSHAKE_CANCEL;
    4.83 +
    4.84 +        // We don't simulate the handshake process here.
    4.85 +        // If we need to, this is a TODO.
    4.86 +        case SYNC_NOTIFY_INIT_ADD_OUR_DEVICE:
    4.87 +        case SYNC_NOTIFY_INIT_ADD_OTHER_DEVICE:
    4.88 +        case SYNC_NOTIFY_INIT_FORM_GROUP:
    4.89 +        case SYNC_NOTIFY_INIT_MOVE_OUR_DEVICE:
    4.90 +            return SYNC_HANDSHAKE_ACCEPTED;
    4.91 +        
    4.92 +        default:    
    4.93 +            return SYNC_HANDSHAKE_REJECTED;
    4.94 +    }
    4.95 +}
    4.96 +
    4.97 +Sync_event_t* pEpTestDevice::retrieve_next_sync_event(void *management, time_t threshold)
    4.98 +{
    4.99 +    time_t started = time(nullptr);
   4.100 +    bool timeout = false;
   4.101 +    
   4.102 +    while (active->q.empty()) {
   4.103 +        int i = 0;
   4.104 +        ++i;
   4.105 +        if (i > 10) {
   4.106 +            if (time(nullptr) > started + threshold) {
   4.107 +                timeout = true;
   4.108 +                break;
   4.109 +            }   
   4.110 +            i = 0;
   4.111 +        }   
   4.112 +        nanosleep((const struct timespec[]){{0, 100000000L}}, NULL);
   4.113 +    }
   4.114 +    if (timeout)
   4.115 +        return new_sync_timeout_event();
   4.116 +
   4.117 +    return pEpTestDevice::active->q.pop_front();
   4.118 +}
   4.119 +
   4.120 +int inject_sync_event(SYNC_EVENT ev, void* management) {
   4.121 +    Sync_event_t *_ev = ev;
   4.122 +    switch (_ev->fsm) {
   4.123 +        case Sync_PR_keysync:
   4.124 +//            cout << "injecting event " << KeySync_event_name(_ev->event) << "\n";
   4.125 +            break;
   4.126 +        default:
   4.127 +            throw std::runtime_error("Unknown state machine.");
   4.128 +    }
   4.129 +    pEpTestDevice::active->q.push_front(ev);
   4.130 +    return 0;
   4.131 +}
   4.132 +