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