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