test/src/engine_tests/SyncTests.cc
author Volker Birk <vb@pep.foundation>
Thu, 23 Aug 2018 15:23:31 +0200
branchsync
changeset 2886 ee600afcb920
parent 2883 a372b8f58aa1
child 2887 c5ab1f6ad3ee
permissions -rw-r--r--
reworking sructure for more tests
     1 // This file is under GNU General Public License 3.0
     2 // see LICENSE.txt
     3 
     4 #include <stdlib.h>
     5 #include <string>
     6 
     7 #include "pEpEngine.h"
     8 
     9 #include "pEp_internal.h"
    10 #include "KeySync_fsm.h"
    11 #include "Sync_codec.h"
    12 
    13 #include "EngineTestSessionSuite.h"
    14 #include "SyncTests.h"
    15 
    16 using namespace std;
    17 
    18 PEP_STATUS Sync_Adapter::notifyHandshake(
    19         void *obj,
    20         pEp_identity *me,
    21         pEp_identity *partner,
    22         sync_handshake_signal signal
    23     )
    24 {
    25     return PEP_STATUS_OK;
    26 }
    27 
    28 int Sync_Adapter::inject_sync_event(SYNC_EVENT ev, void *management)
    29 {
    30     Sync_event_t *_ev = ev;
    31     switch (_ev->fsm) {
    32         case Sync_PR_keysync:
    33             cout << "injecting event " << KeySync_event_name(_ev->event) << "\n";
    34             break;
    35         default:
    36             cout << "unknown state machine: " << _ev->fsm << "\n";
    37             assert(0);
    38     }
    39     auto adapter = static_cast< Sync_Adapter *>(management);
    40     adapter->q.push_front(ev);
    41     return 0;
    42 }
    43 
    44 Sync_event_t *Sync_Adapter::retrieve_next_sync_event(void *management)
    45 {
    46     auto adapter = static_cast< Sync_Adapter *>(management);
    47 
    48     while (adapter->q.empty()) {
    49         sleep(1);
    50     }
    51 
    52     Sync_event_t *ev = adapter->q.pop_front();
    53     if (ev) {
    54         switch (ev->fsm) {
    55             case Sync_PR_keysync:
    56                 cout << "sync thread: retrieving event " << KeySync_event_name(ev->event) << "\n";
    57                 break;
    58             default:
    59                 cout << "sync thread: unknown state machine: " << ev->fsm << "\n";
    60                 assert(0);
    61         }
    62     }
    63     else {
    64         cout << "sync thread: retrieving shutdown\n";
    65     }
    66 
    67     return ev;
    68 }
    69 
    70 PEP_STATUS Sync_Adapter::messageToSend(void *obj, struct _message *msg)
    71 {
    72     assert(msg && msg->attachments);
    73     
    74     cout << "sending message:\n";
    75 
    76     for (bloblist_t *b = msg->attachments; b && b->value; b = b->next) {
    77         if (b->mime_type && strcasecmp(b->mime_type, "application/pEp.sync") == 0) {
    78             char *text = NULL;
    79             PEP_STATUS status = PER_to_XER_Sync_msg(msg->attachments->value, msg->attachments->size, &text);
    80             assert(status == PEP_STATUS_OK);
    81             cout << text << "\n";
    82             free(text);
    83         }
    84     }
    85 
    86     free_message(msg);
    87     return PEP_STATUS_OK;
    88 }
    89 
    90 void Sync_Adapter::sync_thread(PEP_SESSION session, Sync_Adapter *adapter)
    91 {
    92     cout << "sync_thread: startup\n";
    93     do_sync_protocol(session, adapter);
    94     cout << "sync_thread: shutdown\n";
    95 }
    96 
    97 SyncTests::SyncTests(string suitename, string test_home_dir) :
    98     EngineTestSessionSuite::EngineTestSessionSuite(suitename, test_home_dir) {
    99     add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("SyncTests::check_sync"),
   100                                                                       static_cast<Func>(&SyncTests::check_sync)));
   101 }
   102 
   103 void SyncTests::setup()
   104 {
   105     EngineTestSessionSuite::setup();
   106 
   107     pEp_identity *self = new_identity("alice@synctests.pEp", nullptr, "23", "Alice Miller");
   108     assert(self);
   109     cout << "setting own identity for " << self->address << "\n";
   110     PEP_STATUS status = myself(session, self);
   111     assert(self->me);
   112     assert(self->fpr);
   113     cout << "fpr: " << self->fpr << "\n";
   114     free_identity(self);
   115 
   116     status = init(&sync, Sync_Adapter::messageToSend, Sync_Adapter::inject_sync_event);
   117     TEST_ASSERT(status == PEP_STATUS_OK);
   118 
   119     cout << "initialize sync and start first state machine\n";
   120     status = register_sync_callbacks(
   121             sync,
   122             &adapter.q,
   123             Sync_Adapter::notifyHandshake,
   124             Sync_Adapter::retrieve_next_sync_event
   125         );
   126     TEST_ASSERT(status == PEP_STATUS_OK);
   127     TEST_ASSERT(sync->sync_state.keysync.state == Sole);
   128 
   129     cout << "creating thread for sync\n";
   130     sync_thread = new thread(Sync_Adapter::sync_thread, sync, &adapter);
   131 }
   132 
   133 void SyncTests::tear_down()
   134 {
   135     cout << "sending shutdown to sync thread\n";
   136     adapter.q.push_front(nullptr);
   137     sync_thread->join();
   138 
   139     unregister_sync_callbacks(sync);
   140     release(sync);
   141 
   142     EngineTestSessionSuite::tear_down();
   143 }
   144 
   145 void SyncTests::check_sync()
   146 {
   147     cout << "trigger KeyGen event\n";
   148     signal_Sync_event(sync, Sync_PR_keysync, KeyGen);
   149 
   150     cout << "waiting for processing\n";
   151     while (!adapter.q.empty()) {
   152         sleep(1);
   153     }
   154 }
   155