reworking sructure for more tests sync
authorVolker Birk <vb@pep.foundation>
Thu, 23 Aug 2018 15:23:31 +0200
branchsync
changeset 2886ee600afcb920
parent 2885 9907d249919e
child 2887 c5ab1f6ad3ee
reworking sructure for more tests
test/include/SyncTests.h
test/src/engine_tests/SyncTests.cc
     1.1 --- a/test/include/SyncTests.h	Thu Aug 23 12:26:49 2018 +0200
     1.2 +++ b/test/include/SyncTests.h	Thu Aug 23 15:23:31 2018 +0200
     1.3 @@ -5,14 +5,42 @@
     1.4  #define SYNC_H
     1.5  
     1.6  #include <string>
     1.7 +#include <thread>
     1.8  #include "EngineTestSessionSuite.h"
     1.9 +#include "locked_queue.hh"
    1.10 +#include "sync_api.h"
    1.11 +#include "Sync_impl.h"
    1.12  
    1.13  using namespace std;
    1.14  
    1.15 +class Sync_Adapter {
    1.16 +public:
    1.17 +    utility::locked_queue< Sync_event_t * > q;
    1.18 +
    1.19 +    static PEP_STATUS notifyHandshake(
    1.20 +            void *obj,
    1.21 +            pEp_identity *me,
    1.22 +            pEp_identity *partner,
    1.23 +            sync_handshake_signal signal
    1.24 +        );
    1.25 +    static int inject_sync_event(SYNC_EVENT ev, void *management);
    1.26 +    static Sync_event_t *retrieve_next_sync_event(void *management);
    1.27 +    static PEP_STATUS messageToSend(void *obj, struct _message *msg);
    1.28 +
    1.29 +    static void sync_thread(PEP_SESSION session, Sync_Adapter *adapter);
    1.30 +};
    1.31 +
    1.32  class SyncTests : public EngineTestSessionSuite {
    1.33      public:
    1.34          SyncTests(string test_suite, string test_home_dir);
    1.35 +
    1.36 +        void setup();
    1.37 +        void tear_down();
    1.38 +
    1.39      private:
    1.40 +        Sync_Adapter adapter;
    1.41 +        PEP_SESSION sync = NULL;
    1.42 +        thread *sync_thread;
    1.43          void check_sync();
    1.44  };
    1.45  
     2.1 --- a/test/src/engine_tests/SyncTests.cc	Thu Aug 23 12:26:49 2018 +0200
     2.2 +++ b/test/src/engine_tests/SyncTests.cc	Thu Aug 23 15:23:31 2018 +0200
     2.3 @@ -3,10 +3,8 @@
     2.4  
     2.5  #include <stdlib.h>
     2.6  #include <string>
     2.7 -#include <thread>
     2.8  
     2.9  #include "pEpEngine.h"
    2.10 -#include "sync_api.h"
    2.11  
    2.12  #include "pEp_internal.h"
    2.13  #include "KeySync_fsm.h"
    2.14 @@ -15,77 +13,86 @@
    2.15  #include "EngineTestSessionSuite.h"
    2.16  #include "SyncTests.h"
    2.17  
    2.18 -#include "locked_queue.hh"
    2.19 -
    2.20  using namespace std;
    2.21  
    2.22 -class Sync_Adapter {
    2.23 -public:
    2.24 -    utility::locked_queue< Sync_event_t * > q;
    2.25 +PEP_STATUS Sync_Adapter::notifyHandshake(
    2.26 +        void *obj,
    2.27 +        pEp_identity *me,
    2.28 +        pEp_identity *partner,
    2.29 +        sync_handshake_signal signal
    2.30 +    )
    2.31 +{
    2.32 +    return PEP_STATUS_OK;
    2.33 +}
    2.34  
    2.35 -    static PEP_STATUS notifyHandshake(
    2.36 -            void *obj,
    2.37 -            pEp_identity *me,
    2.38 -            pEp_identity *partner,
    2.39 -            sync_handshake_signal signal
    2.40 -        )
    2.41 -    {
    2.42 -        return PEP_STATUS_OK;
    2.43 +int Sync_Adapter::inject_sync_event(SYNC_EVENT ev, void *management)
    2.44 +{
    2.45 +    Sync_event_t *_ev = ev;
    2.46 +    switch (_ev->fsm) {
    2.47 +        case Sync_PR_keysync:
    2.48 +            cout << "injecting event " << KeySync_event_name(_ev->event) << "\n";
    2.49 +            break;
    2.50 +        default:
    2.51 +            cout << "unknown state machine: " << _ev->fsm << "\n";
    2.52 +            assert(0);
    2.53 +    }
    2.54 +    auto adapter = static_cast< Sync_Adapter *>(management);
    2.55 +    adapter->q.push_front(ev);
    2.56 +    return 0;
    2.57 +}
    2.58 +
    2.59 +Sync_event_t *Sync_Adapter::retrieve_next_sync_event(void *management)
    2.60 +{
    2.61 +    auto adapter = static_cast< Sync_Adapter *>(management);
    2.62 +
    2.63 +    while (adapter->q.empty()) {
    2.64 +        sleep(1);
    2.65      }
    2.66  
    2.67 -    static int inject_sync_event(SYNC_EVENT ev, void *management)
    2.68 -    {
    2.69 -        Sync_event_t *_ev = ev;
    2.70 -        cout << "injecting event " << KeySync_event_name(_ev->event) << "\n";
    2.71 -        auto adapter = static_cast< Sync_Adapter *>(management);
    2.72 -        adapter->q.push_front(ev);
    2.73 -        return 0;
    2.74 +    Sync_event_t *ev = adapter->q.pop_front();
    2.75 +    if (ev) {
    2.76 +        switch (ev->fsm) {
    2.77 +            case Sync_PR_keysync:
    2.78 +                cout << "sync thread: retrieving event " << KeySync_event_name(ev->event) << "\n";
    2.79 +                break;
    2.80 +            default:
    2.81 +                cout << "sync thread: unknown state machine: " << ev->fsm << "\n";
    2.82 +                assert(0);
    2.83 +        }
    2.84 +    }
    2.85 +    else {
    2.86 +        cout << "sync thread: retrieving shutdown\n";
    2.87      }
    2.88  
    2.89 -    static Sync_event_t *retrieve_next_sync_event(void *management)
    2.90 -    {
    2.91 -        auto adapter = static_cast< Sync_Adapter *>(management);
    2.92 +    return ev;
    2.93 +}
    2.94  
    2.95 -        while (adapter->q.empty()) {
    2.96 -            sleep(1);
    2.97 +PEP_STATUS Sync_Adapter::messageToSend(void *obj, struct _message *msg)
    2.98 +{
    2.99 +    assert(msg && msg->attachments);
   2.100 +    
   2.101 +    cout << "sending message:\n";
   2.102 +
   2.103 +    for (bloblist_t *b = msg->attachments; b && b->value; b = b->next) {
   2.104 +        if (b->mime_type && strcasecmp(b->mime_type, "application/pEp.sync") == 0) {
   2.105 +            char *text = NULL;
   2.106 +            PEP_STATUS status = PER_to_XER_Sync_msg(msg->attachments->value, msg->attachments->size, &text);
   2.107 +            assert(status == PEP_STATUS_OK);
   2.108 +            cout << text << "\n";
   2.109 +            free(text);
   2.110          }
   2.111 -
   2.112 -        Sync_event_t *ev = adapter->q.pop_front();
   2.113 -        if (ev)
   2.114 -            cout << "retrieving sync event\n";
   2.115 -        else
   2.116 -            cout << "retrieving shutdown\n";
   2.117 -
   2.118 -        return ev;
   2.119      }
   2.120  
   2.121 -    static PEP_STATUS messageToSend(void *obj, struct _message *msg)
   2.122 -    {
   2.123 -        assert(msg && msg->attachments);
   2.124 -        
   2.125 -        cout << "sending message:\n";
   2.126 +    free_message(msg);
   2.127 +    return PEP_STATUS_OK;
   2.128 +}
   2.129  
   2.130 -        for (bloblist_t *b = msg->attachments; b && b->value; b = b->next) {
   2.131 -            if (b->mime_type && strcasecmp(b->mime_type, "application/pEp.sync") == 0) {
   2.132 -                char *text = NULL;
   2.133 -                PEP_STATUS status = PER_to_XER_Sync_msg(msg->attachments->value, msg->attachments->size, &text);
   2.134 -                assert(status == PEP_STATUS_OK);
   2.135 -                cout << text << "\n";
   2.136 -                free(text);
   2.137 -            }
   2.138 -        }
   2.139 -
   2.140 -        free_message(msg);
   2.141 -        return PEP_STATUS_OK;
   2.142 -    }
   2.143 -
   2.144 -    static void sync_thread(PEP_SESSION session, Sync_Adapter *adapter)
   2.145 -    {
   2.146 -        cout << "sync_thread: startup\n";
   2.147 -        do_sync_protocol(session, adapter);
   2.148 -        cout << "sync_thread: shutdown\n";
   2.149 -    }
   2.150 -};
   2.151 +void Sync_Adapter::sync_thread(PEP_SESSION session, Sync_Adapter *adapter)
   2.152 +{
   2.153 +    cout << "sync_thread: startup\n";
   2.154 +    do_sync_protocol(session, adapter);
   2.155 +    cout << "sync_thread: shutdown\n";
   2.156 +}
   2.157  
   2.158  SyncTests::SyncTests(string suitename, string test_home_dir) :
   2.159      EngineTestSessionSuite::EngineTestSessionSuite(suitename, test_home_dir) {
   2.160 @@ -93,17 +100,14 @@
   2.161                                                                        static_cast<Func>(&SyncTests::check_sync)));
   2.162  }
   2.163  
   2.164 -void SyncTests::check_sync()
   2.165 +void SyncTests::setup()
   2.166  {
   2.167 -    Sync_Adapter adapter;
   2.168 -    PEP_SESSION sync = NULL;
   2.169 -    thread *sync_thread;
   2.170 -    PEP_STATUS status = PEP_STATUS_OK;
   2.171 +    EngineTestSessionSuite::setup();
   2.172  
   2.173      pEp_identity *self = new_identity("alice@synctests.pEp", nullptr, "23", "Alice Miller");
   2.174      assert(self);
   2.175      cout << "setting own identity for " << self->address << "\n";
   2.176 -    status = myself(session, self);
   2.177 +    PEP_STATUS status = myself(session, self);
   2.178      assert(self->me);
   2.179      assert(self->fpr);
   2.180      cout << "fpr: " << self->fpr << "\n";
   2.181 @@ -124,19 +128,28 @@
   2.182  
   2.183      cout << "creating thread for sync\n";
   2.184      sync_thread = new thread(Sync_Adapter::sync_thread, sync, &adapter);
   2.185 - 
   2.186 -    cout << "trigger KeyGen event\n";
   2.187 -    signal_Sync_event(sync, Sync_PR_keysync, KeyGen);
   2.188 +}
   2.189  
   2.190 -    cout << "waiting for empty queue\n";
   2.191 -    while (!adapter.q.empty()) {
   2.192 -        sleep(1);
   2.193 -    }
   2.194 +void SyncTests::tear_down()
   2.195 +{
   2.196      cout << "sending shutdown to sync thread\n";
   2.197      adapter.q.push_front(nullptr);
   2.198      sync_thread->join();
   2.199  
   2.200      unregister_sync_callbacks(sync);
   2.201      release(sync);
   2.202 +
   2.203 +    EngineTestSessionSuite::tear_down();
   2.204  }
   2.205  
   2.206 +void SyncTests::check_sync()
   2.207 +{
   2.208 +    cout << "trigger KeyGen event\n";
   2.209 +    signal_Sync_event(sync, Sync_PR_keysync, KeyGen);
   2.210 +
   2.211 +    cout << "waiting for processing\n";
   2.212 +    while (!adapter.q.empty()) {
   2.213 +        sleep(1);
   2.214 +    }
   2.215 +}
   2.216 +