test/src/engine_tests/SyncTests.cc
author Volker Birk <vb@pep.foundation>
Thu, 23 Aug 2018 11:43:22 +0200
branchsync
changeset 2883 a372b8f58aa1
parent 2882 76f789a19606
child 2886 ee600afcb920
permissions -rw-r--r--
make the baby cry
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@2880
    38
        Sync_event_t *_ev = ev;
vb@2881
    39
        cout << "injecting event " << KeySync_event_name(_ev->event) << "\n";
vb@2853
    40
        auto adapter = static_cast< Sync_Adapter *>(management);
vb@2853
    41
        adapter->q.push_front(ev);
vb@2852
    42
        return 0;
vb@2852
    43
    }
vb@2864
    44
vb@2852
    45
    static Sync_event_t *retrieve_next_sync_event(void *management)
vb@2852
    46
    {
vb@2853
    47
        auto adapter = static_cast< Sync_Adapter *>(management);
vb@2883
    48
vb@2873
    49
        while (adapter->q.empty()) {
vb@2873
    50
            sleep(1);
vb@2873
    51
        }
vb@2883
    52
vb@2883
    53
        Sync_event_t *ev = adapter->q.pop_front();
vb@2883
    54
        if (ev)
vb@2873
    55
            cout << "retrieving sync event\n";
vb@2873
    56
        else
vb@2873
    57
            cout << "retrieving shutdown\n";
vb@2883
    58
vb@2883
    59
        return ev;
vb@2852
    60
    }
vb@2871
    61
vb@2871
    62
    static PEP_STATUS messageToSend(void *obj, struct _message *msg)
vb@2871
    63
    {
vb@2876
    64
        assert(msg && msg->attachments);
vb@2874
    65
        
vb@2874
    66
        cout << "sending message:\n";
vb@2874
    67
vb@2876
    68
        for (bloblist_t *b = msg->attachments; b && b->value; b = b->next) {
vb@2876
    69
            if (b->mime_type && strcasecmp(b->mime_type, "application/pEp.sync") == 0) {
vb@2876
    70
                char *text = NULL;
vb@2876
    71
                PEP_STATUS status = PER_to_XER_Sync_msg(msg->attachments->value, msg->attachments->size, &text);
vb@2876
    72
                assert(status == PEP_STATUS_OK);
vb@2876
    73
                cout << text << "\n";
vb@2876
    74
                free(text);
vb@2876
    75
            }
vb@2876
    76
        }
vb@2874
    77
vb@2874
    78
        free_message(msg);
vb@2871
    79
        return PEP_STATUS_OK;
vb@2871
    80
    }
vb@2873
    81
vb@2873
    82
    static void sync_thread(PEP_SESSION session, Sync_Adapter *adapter)
vb@2873
    83
    {
vb@2873
    84
        cout << "sync_thread: startup\n";
vb@2873
    85
        do_sync_protocol(session, adapter);
vb@2873
    86
        cout << "sync_thread: shutdown\n";
vb@2873
    87
    }
vb@2852
    88
};
vb@2852
    89
vb@2852
    90
SyncTests::SyncTests(string suitename, string test_home_dir) :
vb@2852
    91
    EngineTestSessionSuite::EngineTestSessionSuite(suitename, test_home_dir) {
vb@2852
    92
    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("SyncTests::check_sync"),
vb@2852
    93
                                                                      static_cast<Func>(&SyncTests::check_sync)));
vb@2852
    94
}
vb@2852
    95
vb@2871
    96
void SyncTests::check_sync()
vb@2871
    97
{
vb@2852
    98
    Sync_Adapter adapter;
vb@2871
    99
    PEP_SESSION sync = NULL;
vb@2873
   100
    thread *sync_thread;
vb@2875
   101
    PEP_STATUS status = PEP_STATUS_OK;
vb@2871
   102
vb@2876
   103
    pEp_identity *self = new_identity("alice@synctests.pEp", nullptr, "23", "Alice Miller");
vb@2874
   104
    assert(self);
vb@2874
   105
    cout << "setting own identity for " << self->address << "\n";
vb@2874
   106
    status = myself(session, self);
vb@2874
   107
    assert(self->me);
vb@2874
   108
    assert(self->fpr);
vb@2874
   109
    cout << "fpr: " << self->fpr << "\n";
vb@2874
   110
    free_identity(self);
vb@2874
   111
vb@2879
   112
    status = init(&sync, Sync_Adapter::messageToSend, Sync_Adapter::inject_sync_event);
vb@2875
   113
    TEST_ASSERT(status == PEP_STATUS_OK);
vb@2875
   114
vb@2876
   115
    cout << "initialize sync and start first state machine\n";
vb@2871
   116
    status = register_sync_callbacks(
vb@2871
   117
            sync,
vb@2852
   118
            &adapter.q,
vb@2852
   119
            Sync_Adapter::notifyHandshake,
vb@2852
   120
            Sync_Adapter::retrieve_next_sync_event
vb@2852
   121
        );
vb@2871
   122
    TEST_ASSERT(status == PEP_STATUS_OK);
vb@2873
   123
    TEST_ASSERT(sync->sync_state.keysync.state == Sole);
vb@2852
   124
vb@2873
   125
    cout << "creating thread for sync\n";
vb@2873
   126
    sync_thread = new thread(Sync_Adapter::sync_thread, sync, &adapter);
vb@2873
   127
 
vb@2882
   128
    cout << "trigger KeyGen event\n";
vb@2882
   129
    signal_Sync_event(sync, Sync_PR_keysync, KeyGen);
vb@2882
   130
vb@2882
   131
    cout << "waiting for empty queue\n";
vb@2882
   132
    while (!adapter.q.empty()) {
vb@2882
   133
        sleep(1);
vb@2882
   134
    }
vb@2873
   135
    cout << "sending shutdown to sync thread\n";
vb@2873
   136
    adapter.q.push_front(nullptr);
vb@2873
   137
    sync_thread->join();
vb@2871
   138
vb@2871
   139
    unregister_sync_callbacks(sync);
vb@2871
   140
    release(sync);
vb@2852
   141
}
vb@2852
   142