test/src/engine_tests/SyncTests.cc.b0rken
author Volker Birk <vb@pep-project.org>
Thu, 06 Jun 2019 22:20:55 +0200
branchsync
changeset 3817 c2cb0ab6040a
parent 3159 0489203c122d
child 3818 0b34a967ab39
permissions -rw-r--r--
subtype "pEp.sync" invalid, switching to "pEp-sync"
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@3817
    98
        if (b->mime_type && strcasecmp(b->mime_type, "application/pEp-sync") == 0) {
vb@3100
    99
            assert(msg->from && msg->from->address && msg->from->username);
vb@3102
   100
            cout << "<!-- " << msg->from->username << " <" << msg->from->address << "> -->\n";
vb@2886
   101
            char *text = NULL;
vb@2886
   102
            PEP_STATUS status = PER_to_XER_Sync_msg(msg->attachments->value, msg->attachments->size, &text);
vb@2886
   103
            assert(status == PEP_STATUS_OK);
vb@2886
   104
            cout << text << "\n";
vb@2886
   105
            free(text);
vb@2873
   106
        }
vb@2852
   107
    }
vb@2871
   108
vb@2886
   109
    free_message(msg);
vb@2886
   110
    return PEP_STATUS_OK;
vb@2886
   111
}
vb@2874
   112
vb@2886
   113
void Sync_Adapter::sync_thread(PEP_SESSION session, Sync_Adapter *adapter)
vb@2886
   114
{
vb@2886
   115
    cout << "sync_thread: startup\n";
vb@2886
   116
    do_sync_protocol(session, adapter);
vb@2886
   117
    cout << "sync_thread: shutdown\n";
vb@2886
   118
}
vb@2852
   119
vb@2852
   120
SyncTests::SyncTests(string suitename, string test_home_dir) :
vb@2852
   121
    EngineTestSessionSuite::EngineTestSessionSuite(suitename, test_home_dir) {
vb@2852
   122
    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("SyncTests::check_sync"),
vb@2852
   123
                                                                      static_cast<Func>(&SyncTests::check_sync)));
vb@2852
   124
}
vb@2852
   125
vb@2886
   126
void SyncTests::setup()
vb@2871
   127
{
vb@2886
   128
    EngineTestSessionSuite::setup();
vb@2871
   129
vb@2876
   130
    pEp_identity *self = new_identity("alice@synctests.pEp", nullptr, "23", "Alice Miller");
vb@2874
   131
    assert(self);
vb@2874
   132
    cout << "setting own identity for " << self->address << "\n";
vb@2886
   133
    PEP_STATUS status = myself(session, self);
vb@2874
   134
    assert(self->me);
vb@2874
   135
    assert(self->fpr);
vb@2874
   136
    cout << "fpr: " << self->fpr << "\n";
vb@2874
   137
    free_identity(self);
vb@2874
   138
vb@2879
   139
    status = init(&sync, Sync_Adapter::messageToSend, Sync_Adapter::inject_sync_event);
vb@2875
   140
    TEST_ASSERT(status == PEP_STATUS_OK);
vb@2875
   141
vb@2876
   142
    cout << "initialize sync and start first state machine\n";
vb@2871
   143
    status = register_sync_callbacks(
vb@2871
   144
            sync,
vb@3086
   145
            (void *) &adapter.q,
vb@2852
   146
            Sync_Adapter::notifyHandshake,
vb@2852
   147
            Sync_Adapter::retrieve_next_sync_event
vb@2852
   148
        );
vb@2871
   149
    TEST_ASSERT(status == PEP_STATUS_OK);
vb@2873
   150
    TEST_ASSERT(sync->sync_state.keysync.state == Sole);
vb@2852
   151
vb@2873
   152
    cout << "creating thread for sync\n";
vb@2873
   153
    sync_thread = new thread(Sync_Adapter::sync_thread, sync, &adapter);
vb@2886
   154
}
vb@2882
   155
vb@2886
   156
void SyncTests::tear_down()
vb@2886
   157
{
vb@2888
   158
    adapter.processing();
vb@2887
   159
vb@2873
   160
    cout << "sending shutdown to sync thread\n";
vb@2873
   161
    adapter.q.push_front(nullptr);
vb@2873
   162
    sync_thread->join();
vb@2871
   163
vb@2871
   164
    unregister_sync_callbacks(sync);
vb@2871
   165
    release(sync);
vb@2886
   166
vb@2886
   167
    EngineTestSessionSuite::tear_down();
vb@2852
   168
}
vb@2852
   169
vb@2886
   170
void SyncTests::check_sync()
vb@2886
   171
{
vb@2887
   172
    cout << "check_sync(): trigger KeyGen event\n";
vb@2886
   173
    signal_Sync_event(sync, Sync_PR_keysync, KeyGen);
vb@2888
   174
    adapter.processing();
vb@2886
   175
vb@2887
   176
    cout << "check_sync(): cry for unknown key\n";
vb@2887
   177
    signal_Sync_event(sync, Sync_PR_keysync, CannotDecrypt);
vb@2886
   178
}
vb@2886
   179