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