test/src/engine_tests/SyncTests.cc
branchsync
changeset 2886 ee600afcb920
parent 2883 a372b8f58aa1
child 2887 c5ab1f6ad3ee
     1.1 --- a/test/src/engine_tests/SyncTests.cc	Thu Aug 23 12:26:49 2018 +0200
     1.2 +++ b/test/src/engine_tests/SyncTests.cc	Thu Aug 23 15:23:31 2018 +0200
     1.3 @@ -3,10 +3,8 @@
     1.4  
     1.5  #include <stdlib.h>
     1.6  #include <string>
     1.7 -#include <thread>
     1.8  
     1.9  #include "pEpEngine.h"
    1.10 -#include "sync_api.h"
    1.11  
    1.12  #include "pEp_internal.h"
    1.13  #include "KeySync_fsm.h"
    1.14 @@ -15,77 +13,86 @@
    1.15  #include "EngineTestSessionSuite.h"
    1.16  #include "SyncTests.h"
    1.17  
    1.18 -#include "locked_queue.hh"
    1.19 -
    1.20  using namespace std;
    1.21  
    1.22 -class Sync_Adapter {
    1.23 -public:
    1.24 -    utility::locked_queue< Sync_event_t * > q;
    1.25 +PEP_STATUS Sync_Adapter::notifyHandshake(
    1.26 +        void *obj,
    1.27 +        pEp_identity *me,
    1.28 +        pEp_identity *partner,
    1.29 +        sync_handshake_signal signal
    1.30 +    )
    1.31 +{
    1.32 +    return PEP_STATUS_OK;
    1.33 +}
    1.34  
    1.35 -    static PEP_STATUS notifyHandshake(
    1.36 -            void *obj,
    1.37 -            pEp_identity *me,
    1.38 -            pEp_identity *partner,
    1.39 -            sync_handshake_signal signal
    1.40 -        )
    1.41 -    {
    1.42 -        return PEP_STATUS_OK;
    1.43 +int Sync_Adapter::inject_sync_event(SYNC_EVENT ev, void *management)
    1.44 +{
    1.45 +    Sync_event_t *_ev = ev;
    1.46 +    switch (_ev->fsm) {
    1.47 +        case Sync_PR_keysync:
    1.48 +            cout << "injecting event " << KeySync_event_name(_ev->event) << "\n";
    1.49 +            break;
    1.50 +        default:
    1.51 +            cout << "unknown state machine: " << _ev->fsm << "\n";
    1.52 +            assert(0);
    1.53      }
    1.54 +    auto adapter = static_cast< Sync_Adapter *>(management);
    1.55 +    adapter->q.push_front(ev);
    1.56 +    return 0;
    1.57 +}
    1.58  
    1.59 -    static int inject_sync_event(SYNC_EVENT ev, void *management)
    1.60 -    {
    1.61 -        Sync_event_t *_ev = ev;
    1.62 -        cout << "injecting event " << KeySync_event_name(_ev->event) << "\n";
    1.63 -        auto adapter = static_cast< Sync_Adapter *>(management);
    1.64 -        adapter->q.push_front(ev);
    1.65 -        return 0;
    1.66 +Sync_event_t *Sync_Adapter::retrieve_next_sync_event(void *management)
    1.67 +{
    1.68 +    auto adapter = static_cast< Sync_Adapter *>(management);
    1.69 +
    1.70 +    while (adapter->q.empty()) {
    1.71 +        sleep(1);
    1.72      }
    1.73  
    1.74 -    static Sync_event_t *retrieve_next_sync_event(void *management)
    1.75 -    {
    1.76 -        auto adapter = static_cast< Sync_Adapter *>(management);
    1.77 -
    1.78 -        while (adapter->q.empty()) {
    1.79 -            sleep(1);
    1.80 +    Sync_event_t *ev = adapter->q.pop_front();
    1.81 +    if (ev) {
    1.82 +        switch (ev->fsm) {
    1.83 +            case Sync_PR_keysync:
    1.84 +                cout << "sync thread: retrieving event " << KeySync_event_name(ev->event) << "\n";
    1.85 +                break;
    1.86 +            default:
    1.87 +                cout << "sync thread: unknown state machine: " << ev->fsm << "\n";
    1.88 +                assert(0);
    1.89          }
    1.90 -
    1.91 -        Sync_event_t *ev = adapter->q.pop_front();
    1.92 -        if (ev)
    1.93 -            cout << "retrieving sync event\n";
    1.94 -        else
    1.95 -            cout << "retrieving shutdown\n";
    1.96 -
    1.97 -        return ev;
    1.98 +    }
    1.99 +    else {
   1.100 +        cout << "sync thread: retrieving shutdown\n";
   1.101      }
   1.102  
   1.103 -    static PEP_STATUS messageToSend(void *obj, struct _message *msg)
   1.104 -    {
   1.105 -        assert(msg && msg->attachments);
   1.106 -        
   1.107 -        cout << "sending message:\n";
   1.108 +    return ev;
   1.109 +}
   1.110 +
   1.111 +PEP_STATUS Sync_Adapter::messageToSend(void *obj, struct _message *msg)
   1.112 +{
   1.113 +    assert(msg && msg->attachments);
   1.114 +    
   1.115 +    cout << "sending message:\n";
   1.116  
   1.117 -        for (bloblist_t *b = msg->attachments; b && b->value; b = b->next) {
   1.118 -            if (b->mime_type && strcasecmp(b->mime_type, "application/pEp.sync") == 0) {
   1.119 -                char *text = NULL;
   1.120 -                PEP_STATUS status = PER_to_XER_Sync_msg(msg->attachments->value, msg->attachments->size, &text);
   1.121 -                assert(status == PEP_STATUS_OK);
   1.122 -                cout << text << "\n";
   1.123 -                free(text);
   1.124 -            }
   1.125 +    for (bloblist_t *b = msg->attachments; b && b->value; b = b->next) {
   1.126 +        if (b->mime_type && strcasecmp(b->mime_type, "application/pEp.sync") == 0) {
   1.127 +            char *text = NULL;
   1.128 +            PEP_STATUS status = PER_to_XER_Sync_msg(msg->attachments->value, msg->attachments->size, &text);
   1.129 +            assert(status == PEP_STATUS_OK);
   1.130 +            cout << text << "\n";
   1.131 +            free(text);
   1.132          }
   1.133 -
   1.134 -        free_message(msg);
   1.135 -        return PEP_STATUS_OK;
   1.136      }
   1.137  
   1.138 -    static void sync_thread(PEP_SESSION session, Sync_Adapter *adapter)
   1.139 -    {
   1.140 -        cout << "sync_thread: startup\n";
   1.141 -        do_sync_protocol(session, adapter);
   1.142 -        cout << "sync_thread: shutdown\n";
   1.143 -    }
   1.144 -};
   1.145 +    free_message(msg);
   1.146 +    return PEP_STATUS_OK;
   1.147 +}
   1.148 +
   1.149 +void Sync_Adapter::sync_thread(PEP_SESSION session, Sync_Adapter *adapter)
   1.150 +{
   1.151 +    cout << "sync_thread: startup\n";
   1.152 +    do_sync_protocol(session, adapter);
   1.153 +    cout << "sync_thread: shutdown\n";
   1.154 +}
   1.155  
   1.156  SyncTests::SyncTests(string suitename, string test_home_dir) :
   1.157      EngineTestSessionSuite::EngineTestSessionSuite(suitename, test_home_dir) {
   1.158 @@ -93,17 +100,14 @@
   1.159                                                                        static_cast<Func>(&SyncTests::check_sync)));
   1.160  }
   1.161  
   1.162 -void SyncTests::check_sync()
   1.163 +void SyncTests::setup()
   1.164  {
   1.165 -    Sync_Adapter adapter;
   1.166 -    PEP_SESSION sync = NULL;
   1.167 -    thread *sync_thread;
   1.168 -    PEP_STATUS status = PEP_STATUS_OK;
   1.169 +    EngineTestSessionSuite::setup();
   1.170  
   1.171      pEp_identity *self = new_identity("alice@synctests.pEp", nullptr, "23", "Alice Miller");
   1.172      assert(self);
   1.173      cout << "setting own identity for " << self->address << "\n";
   1.174 -    status = myself(session, self);
   1.175 +    PEP_STATUS status = myself(session, self);
   1.176      assert(self->me);
   1.177      assert(self->fpr);
   1.178      cout << "fpr: " << self->fpr << "\n";
   1.179 @@ -124,19 +128,28 @@
   1.180  
   1.181      cout << "creating thread for sync\n";
   1.182      sync_thread = new thread(Sync_Adapter::sync_thread, sync, &adapter);
   1.183 - 
   1.184 -    cout << "trigger KeyGen event\n";
   1.185 -    signal_Sync_event(sync, Sync_PR_keysync, KeyGen);
   1.186 +}
   1.187  
   1.188 -    cout << "waiting for empty queue\n";
   1.189 -    while (!adapter.q.empty()) {
   1.190 -        sleep(1);
   1.191 -    }
   1.192 +void SyncTests::tear_down()
   1.193 +{
   1.194      cout << "sending shutdown to sync thread\n";
   1.195      adapter.q.push_front(nullptr);
   1.196      sync_thread->join();
   1.197  
   1.198      unregister_sync_callbacks(sync);
   1.199      release(sync);
   1.200 +
   1.201 +    EngineTestSessionSuite::tear_down();
   1.202  }
   1.203  
   1.204 +void SyncTests::check_sync()
   1.205 +{
   1.206 +    cout << "trigger KeyGen event\n";
   1.207 +    signal_Sync_event(sync, Sync_PR_keysync, KeyGen);
   1.208 +
   1.209 +    cout << "waiting for processing\n";
   1.210 +    while (!adapter.q.empty()) {
   1.211 +        sleep(1);
   1.212 +    }
   1.213 +}
   1.214 +