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