test/src/engine_tests/SyncTests.cc
author Volker Birk <vb@pep.foundation>
Sun, 21 Oct 2018 15:37:18 +0200
branchsync
changeset 3086 c35fd5131941
parent 2910 e1a7270acdcf
child 3100 1bd0c12ba841
permissions -rw-r--r--
adapt sync test
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@2899
    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@2888
    18
void Sync_Adapter::processing()
vb@2888
    19
{
vb@2888
    20
    cout << "waiting for processing\n";
vb@2888
    21
    while (!q.empty()) {
vb@2899
    22
        nanosleep((const struct timespec[]){{0, 100000000L}}, NULL);
vb@2888
    23
    }
vb@2888
    24
}
vb@2888
    25
vb@2886
    26
PEP_STATUS Sync_Adapter::notifyHandshake(
vb@2886
    27
        pEp_identity *me,
vb@2886
    28
        pEp_identity *partner,
vb@2886
    29
        sync_handshake_signal signal
vb@2886
    30
    )
vb@2886
    31
{
vb@2886
    32
    return PEP_STATUS_OK;
vb@2886
    33
}
vb@2852
    34
vb@2886
    35
int Sync_Adapter::inject_sync_event(SYNC_EVENT ev, void *management)
vb@2886
    36
{
vb@2886
    37
    Sync_event_t *_ev = ev;
vb@2886
    38
    switch (_ev->fsm) {
vb@2886
    39
        case Sync_PR_keysync:
vb@2886
    40
            cout << "injecting event " << KeySync_event_name(_ev->event) << "\n";
vb@2886
    41
            break;
vb@2886
    42
        default:
vb@2886
    43
            cout << "unknown state machine: " << _ev->fsm << "\n";
vb@2886
    44
            assert(0);
vb@2886
    45
    }
vb@2886
    46
    auto adapter = static_cast< Sync_Adapter *>(management);
vb@2886
    47
    adapter->q.push_front(ev);
vb@2886
    48
    return 0;
vb@2886
    49
}
vb@2886
    50
vb@2909
    51
Sync_event_t *Sync_Adapter::retrieve_next_sync_event(void *management, time_t threshold)
vb@2886
    52
{
vb@2886
    53
    auto adapter = static_cast< Sync_Adapter *>(management);
vb@2910
    54
    time_t started = time(nullptr);
vb@2910
    55
    bool timeout = false;
vb@2886
    56
vb@2886
    57
    while (adapter->q.empty()) {
vb@2910
    58
        int i = 0;
vb@2910
    59
        ++i;
vb@2910
    60
        if (i > 10) {
vb@2910
    61
            if (time(nullptr) > started + threshold) {
vb@2910
    62
                timeout = true;
vb@2910
    63
                break;
vb@2910
    64
            }
vb@2910
    65
            i = 0;
vb@2910
    66
        }
vb@2899
    67
        nanosleep((const struct timespec[]){{0, 100000000L}}, NULL);
vb@2852
    68
    }
vb@2852
    69
vb@2910
    70
    if (timeout)
vb@2910
    71
        return SYNC_TIMEOUT_EVENT;
vb@2910
    72
vb@2886
    73
    Sync_event_t *ev = adapter->q.pop_front();
vb@2886
    74
    if (ev) {
vb@2886
    75
        switch (ev->fsm) {
vb@2886
    76
            case Sync_PR_keysync:
vb@2886
    77
                cout << "sync thread: retrieving event " << KeySync_event_name(ev->event) << "\n";
vb@2886
    78
                break;
vb@2886
    79
            default:
vb@2886
    80
                cout << "sync thread: unknown state machine: " << ev->fsm << "\n";
vb@2886
    81
                assert(0);
vb@2886
    82
        }
vb@2886
    83
    }
vb@2886
    84
    else {
vb@2886
    85
        cout << "sync thread: retrieving shutdown\n";
vb@2852
    86
    }
vb@2864
    87
vb@2886
    88
    return ev;
vb@2886
    89
}
vb@2883
    90
vb@2899
    91
PEP_STATUS Sync_Adapter::messageToSend(struct _message *msg)
vb@2886
    92
{
vb@2886
    93
    assert(msg && msg->attachments);
vb@2886
    94
    
vb@2886
    95
    cout << "sending message:\n";
vb@2886
    96
vb@2886
    97
    for (bloblist_t *b = msg->attachments; b && b->value; b = b->next) {
vb@2886
    98
        if (b->mime_type && strcasecmp(b->mime_type, "application/pEp.sync") == 0) {
vb@2886
    99
            char *text = NULL;
vb@2886
   100
            PEP_STATUS status = PER_to_XER_Sync_msg(msg->attachments->value, msg->attachments->size, &text);
vb@2886
   101
            assert(status == PEP_STATUS_OK);
vb@2886
   102
            cout << text << "\n";
vb@2886
   103
            free(text);
vb@2873
   104
        }
vb@2852
   105
    }
vb@2871
   106
vb@2886
   107
    free_message(msg);
vb@2886
   108
    return PEP_STATUS_OK;
vb@2886
   109
}
vb@2874
   110
vb@2886
   111
void Sync_Adapter::sync_thread(PEP_SESSION session, Sync_Adapter *adapter)
vb@2886
   112
{
vb@2886
   113
    cout << "sync_thread: startup\n";
vb@2886
   114
    do_sync_protocol(session, adapter);
vb@2886
   115
    cout << "sync_thread: shutdown\n";
vb@2886
   116
}
vb@2852
   117
vb@2852
   118
SyncTests::SyncTests(string suitename, string test_home_dir) :
vb@2852
   119
    EngineTestSessionSuite::EngineTestSessionSuite(suitename, test_home_dir) {
vb@2852
   120
    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("SyncTests::check_sync"),
vb@2852
   121
                                                                      static_cast<Func>(&SyncTests::check_sync)));
vb@2852
   122
}
vb@2852
   123
vb@2886
   124
void SyncTests::setup()
vb@2871
   125
{
vb@2886
   126
    EngineTestSessionSuite::setup();
vb@2871
   127
vb@2876
   128
    pEp_identity *self = new_identity("alice@synctests.pEp", nullptr, "23", "Alice Miller");
vb@2874
   129
    assert(self);
vb@2874
   130
    cout << "setting own identity for " << self->address << "\n";
vb@2886
   131
    PEP_STATUS status = myself(session, self);
vb@2874
   132
    assert(self->me);
vb@2874
   133
    assert(self->fpr);
vb@2874
   134
    cout << "fpr: " << self->fpr << "\n";
vb@2874
   135
    free_identity(self);
vb@2874
   136
vb@2879
   137
    status = init(&sync, Sync_Adapter::messageToSend, Sync_Adapter::inject_sync_event);
vb@2875
   138
    TEST_ASSERT(status == PEP_STATUS_OK);
vb@2875
   139
vb@2876
   140
    cout << "initialize sync and start first state machine\n";
vb@2871
   141
    status = register_sync_callbacks(
vb@2871
   142
            sync,
vb@3086
   143
            (void *) &adapter.q,
vb@2852
   144
            Sync_Adapter::notifyHandshake,
vb@2852
   145
            Sync_Adapter::retrieve_next_sync_event
vb@2852
   146
        );
vb@2871
   147
    TEST_ASSERT(status == PEP_STATUS_OK);
vb@2873
   148
    TEST_ASSERT(sync->sync_state.keysync.state == Sole);
vb@2852
   149
vb@2873
   150
    cout << "creating thread for sync\n";
vb@2873
   151
    sync_thread = new thread(Sync_Adapter::sync_thread, sync, &adapter);
vb@2886
   152
}
vb@2882
   153
vb@2886
   154
void SyncTests::tear_down()
vb@2886
   155
{
vb@2888
   156
    adapter.processing();
vb@2887
   157
vb@2873
   158
    cout << "sending shutdown to sync thread\n";
vb@2873
   159
    adapter.q.push_front(nullptr);
vb@2873
   160
    sync_thread->join();
vb@2871
   161
vb@2871
   162
    unregister_sync_callbacks(sync);
vb@2871
   163
    release(sync);
vb@2886
   164
vb@2886
   165
    EngineTestSessionSuite::tear_down();
vb@2852
   166
}
vb@2852
   167
vb@2886
   168
void SyncTests::check_sync()
vb@2886
   169
{
vb@2887
   170
    cout << "check_sync(): trigger KeyGen event\n";
vb@2886
   171
    signal_Sync_event(sync, Sync_PR_keysync, KeyGen);
vb@2888
   172
    adapter.processing();
vb@2886
   173
vb@2887
   174
    cout << "check_sync(): cry for unknown key\n";
vb@2887
   175
    signal_Sync_event(sync, Sync_PR_keysync, CannotDecrypt);
vb@2886
   176
}
vb@2886
   177