test/src/engine_tests/SyncTests.cc
author Volker Birk <vb@pep.foundation>
Wed, 22 Aug 2018 20:51:23 +0200
branchsync
changeset 2874 45531079b073
parent 2873 ceaa29b309ef
child 2875 8e3169c2952a
permissions -rw-r--r--
create own identity
vb@2852
     1
// This file is under GNU General Public License 3.0
vb@2852
     2
// see LICENSE.txt
vb@2852
     3
vb@2852
     4
#include <stdlib.h>
vb@2852
     5
#include <string>
vb@2873
     6
#include <thread>
vb@2852
     7
vb@2852
     8
#include "pEpEngine.h"
vb@2852
     9
#include "sync_api.h"
vb@2871
    10
vb@2871
    11
#include "pEp_internal.h"
vb@2871
    12
#include "KeySync_fsm.h"
vb@2874
    13
#include "Sync_codec.h"
vb@2852
    14
vb@2852
    15
#include "EngineTestSessionSuite.h"
vb@2852
    16
#include "SyncTests.h"
vb@2852
    17
vb@2852
    18
#include "locked_queue.hh"
vb@2852
    19
vb@2852
    20
using namespace std;
vb@2852
    21
vb@2852
    22
class Sync_Adapter {
vb@2852
    23
public:
vb@2852
    24
    utility::locked_queue< Sync_event_t * > q;
vb@2852
    25
vb@2852
    26
    static PEP_STATUS notifyHandshake(
vb@2871
    27
            void *obj,
vb@2871
    28
            pEp_identity *me,
vb@2871
    29
            pEp_identity *partner,
vb@2871
    30
            sync_handshake_signal signal
vb@2871
    31
        )
vb@2852
    32
    {
vb@2852
    33
        return PEP_STATUS_OK;
vb@2852
    34
    }
vb@2852
    35
vb@2852
    36
    static int inject_sync_event(SYNC_EVENT ev, void *management)
vb@2852
    37
    {
vb@2853
    38
        auto adapter = static_cast< Sync_Adapter *>(management);
vb@2853
    39
        adapter->q.push_front(ev);
vb@2852
    40
        return 0;
vb@2852
    41
    }
vb@2864
    42
vb@2852
    43
    static Sync_event_t *retrieve_next_sync_event(void *management)
vb@2852
    44
    {
vb@2853
    45
        auto adapter = static_cast< Sync_Adapter *>(management);
vb@2873
    46
        while (adapter->q.empty()) {
vb@2873
    47
            sleep(1);
vb@2873
    48
        }
vb@2873
    49
        Sync_event_t *result = adapter->q.pop_front();
vb@2873
    50
        if (result)
vb@2873
    51
            cout << "retrieving sync event\n";
vb@2873
    52
        else
vb@2873
    53
            cout << "retrieving shutdown\n";
vb@2873
    54
        return result;
vb@2852
    55
    }
vb@2871
    56
vb@2871
    57
    static PEP_STATUS messageToSend(void *obj, struct _message *msg)
vb@2871
    58
    {
vb@2874
    59
        assert(msg && msg->attachments && msg->attachments->value && msg->attachments->size);
vb@2874
    60
        
vb@2874
    61
        cout << "sending message:\n";
vb@2874
    62
vb@2874
    63
        char *text = NULL;
vb@2874
    64
        PEP_STATUS status = PER_to_XER_Sync_msg(msg->attachments->value, msg->attachments->size, &text);
vb@2874
    65
        assert(status == PEP_STATUS_OK);
vb@2874
    66
        cout << text << "\n";
vb@2874
    67
vb@2874
    68
        free(text);
vb@2874
    69
        free_message(msg);
vb@2871
    70
        return PEP_STATUS_OK;
vb@2871
    71
    }
vb@2873
    72
vb@2873
    73
    static void sync_thread(PEP_SESSION session, Sync_Adapter *adapter)
vb@2873
    74
    {
vb@2873
    75
        cout << "sync_thread: startup\n";
vb@2873
    76
        do_sync_protocol(session, adapter);
vb@2873
    77
        cout << "sync_thread: shutdown\n";
vb@2873
    78
    }
vb@2852
    79
};
vb@2852
    80
vb@2852
    81
SyncTests::SyncTests(string suitename, string test_home_dir) :
vb@2852
    82
    EngineTestSessionSuite::EngineTestSessionSuite(suitename, test_home_dir) {
vb@2852
    83
    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("SyncTests::check_sync"),
vb@2852
    84
                                                                      static_cast<Func>(&SyncTests::check_sync)));
vb@2852
    85
}
vb@2852
    86
vb@2871
    87
void SyncTests::check_sync()
vb@2871
    88
{
vb@2852
    89
    Sync_Adapter adapter;
vb@2871
    90
    PEP_SESSION sync = NULL;
vb@2873
    91
    thread *sync_thread;
vb@2852
    92
vb@2871
    93
    PEP_STATUS status = init(&sync, Sync_Adapter::messageToSend);
vb@2871
    94
    TEST_ASSERT(status == PEP_STATUS_OK);
vb@2871
    95
vb@2874
    96
    pEp_identity *self = new_identity("alice@synctests.pep", nullptr, "23", "Alice Miller");
vb@2874
    97
    assert(self);
vb@2874
    98
    cout << "setting own identity for " << self->address << "\n";
vb@2874
    99
    status = myself(session, self);
vb@2874
   100
    assert(self->me);
vb@2874
   101
    assert(self->fpr);
vb@2874
   102
    cout << "fpr: " << self->fpr << "\n";
vb@2874
   103
    free_identity(self);
vb@2874
   104
vb@2871
   105
    status = register_sync_callbacks(
vb@2871
   106
            sync,
vb@2852
   107
            &adapter.q,
vb@2852
   108
            Sync_Adapter::notifyHandshake,
vb@2852
   109
            Sync_Adapter::inject_sync_event,
vb@2852
   110
            Sync_Adapter::retrieve_next_sync_event
vb@2852
   111
        );
vb@2871
   112
    TEST_ASSERT(status == PEP_STATUS_OK);
vb@2873
   113
    TEST_ASSERT(sync->sync_state.keysync.state == Sole);
vb@2852
   114
vb@2873
   115
    cout << "creating thread for sync\n";
vb@2873
   116
    sync_thread = new thread(Sync_Adapter::sync_thread, sync, &adapter);
vb@2873
   117
 
vb@2873
   118
    cout << "sending shutdown to sync thread\n";
vb@2873
   119
    adapter.q.push_front(nullptr);
vb@2873
   120
    sync_thread->join();
vb@2871
   121
vb@2871
   122
    unregister_sync_callbacks(sync);
vb@2871
   123
    release(sync);
vb@2852
   124
}
vb@2852
   125