adding SyncTests.cc again sync
authorVolker Birk <vb@pep-project.org>
Mon, 28 Jan 2019 17:11:33 +0100
branchsync
changeset 32511d8a5f106a64
parent 3250 32211c0ba931
child 3252 6ae6df9f498d
child 3253 891e6ca8aced
adding SyncTests.cc again
test/src/engine_tests/SyncTests.cc
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/src/engine_tests/SyncTests.cc	Mon Jan 28 17:11:33 2019 +0100
     1.3 @@ -0,0 +1,179 @@
     1.4 +// This file is under GNU General Public License 3.0
     1.5 +// see LICENSE.txt
     1.6 +
     1.7 +#include <stdlib.h>
     1.8 +#include <string>
     1.9 +
    1.10 +#include "pEpEngine.h"
    1.11 +
    1.12 +#include "pEp_internal.h"
    1.13 +#include "KeySync_fsm.h"
    1.14 +#include "sync_codec.h"
    1.15 +
    1.16 +#include "EngineTestSessionSuite.h"
    1.17 +#include "SyncTests.h"
    1.18 +
    1.19 +using namespace std;
    1.20 +
    1.21 +void Sync_Adapter::processing()
    1.22 +{
    1.23 +    cout << "waiting for processing\n";
    1.24 +    while (!q.empty()) {
    1.25 +        nanosleep((const struct timespec[]){{0, 100000000L}}, NULL);
    1.26 +    }
    1.27 +}
    1.28 +
    1.29 +PEP_STATUS Sync_Adapter::notifyHandshake(
    1.30 +        pEp_identity *me,
    1.31 +        pEp_identity *partner,
    1.32 +        sync_handshake_signal signal
    1.33 +    )
    1.34 +{
    1.35 +    return PEP_STATUS_OK;
    1.36 +}
    1.37 +
    1.38 +int Sync_Adapter::inject_sync_event(SYNC_EVENT ev, void *management)
    1.39 +{
    1.40 +    Sync_event_t *_ev = ev;
    1.41 +    switch (_ev->fsm) {
    1.42 +        case Sync_PR_keysync:
    1.43 +            cout << "injecting event " << KeySync_event_name(_ev->event) << "\n";
    1.44 +            break;
    1.45 +        default:
    1.46 +            cout << "unknown state machine: " << _ev->fsm << "\n";
    1.47 +            assert(0);
    1.48 +    }
    1.49 +    auto adapter = static_cast< Sync_Adapter *>(management);
    1.50 +    adapter->q.push_front(ev);
    1.51 +    return 0;
    1.52 +}
    1.53 +
    1.54 +Sync_event_t *Sync_Adapter::retrieve_next_sync_event(void *management, unsigned threshold)
    1.55 +{
    1.56 +    auto adapter = static_cast< Sync_Adapter *>(management);
    1.57 +    time_t started = time(nullptr);
    1.58 +    bool timeout = false;
    1.59 +
    1.60 +    while (adapter->q.empty()) {
    1.61 +        int i = 0;
    1.62 +        ++i;
    1.63 +        if (i > 10) {
    1.64 +            if (time(nullptr) > started + threshold) {
    1.65 +                timeout = true;
    1.66 +                break;
    1.67 +            }
    1.68 +            i = 0;
    1.69 +        }
    1.70 +        nanosleep((const struct timespec[]){{0, 100000000L}}, NULL);
    1.71 +    }
    1.72 +
    1.73 +    if (timeout)
    1.74 +        return SYNC_TIMEOUT_EVENT;
    1.75 +
    1.76 +    Sync_event_t *ev = adapter->q.pop_front();
    1.77 +    if (ev) {
    1.78 +        switch (ev->fsm) {
    1.79 +            case Sync_PR_keysync:
    1.80 +                cout << "sync thread: retrieving event " << KeySync_event_name(ev->event) << "\n";
    1.81 +                break;
    1.82 +            default:
    1.83 +                cout << "sync thread: unknown state machine: " << ev->fsm << "\n";
    1.84 +                assert(0);
    1.85 +        }
    1.86 +    }
    1.87 +    else {
    1.88 +        cout << "sync thread: retrieving shutdown\n";
    1.89 +    }
    1.90 +
    1.91 +    return ev;
    1.92 +}
    1.93 +
    1.94 +PEP_STATUS Sync_Adapter::messageToSend(struct _message *msg)
    1.95 +{
    1.96 +    assert(msg && msg->attachments);
    1.97 +    
    1.98 +    cout << "sending message:\n";
    1.99 +
   1.100 +    for (bloblist_t *b = msg->attachments; b && b->value; b = b->next) {
   1.101 +        if (b->mime_type && strcasecmp(b->mime_type, "application/pEp.sync") == 0) {
   1.102 +            assert(msg->from && msg->from->address && msg->from->username);
   1.103 +            cout << "<!-- " << msg->from->username << " <" << msg->from->address << "> -->\n";
   1.104 +            char *text = NULL;
   1.105 +            PEP_STATUS status = PER_to_XER_Sync_msg(msg->attachments->value, msg->attachments->size, &text);
   1.106 +            assert(status == PEP_STATUS_OK);
   1.107 +            cout << text << "\n";
   1.108 +            free(text);
   1.109 +        }
   1.110 +    }
   1.111 +
   1.112 +    free_message(msg);
   1.113 +    return PEP_STATUS_OK;
   1.114 +}
   1.115 +
   1.116 +void Sync_Adapter::sync_thread(PEP_SESSION session, Sync_Adapter *adapter)
   1.117 +{
   1.118 +    cout << "sync_thread: startup\n";
   1.119 +    do_sync_protocol(session, adapter);
   1.120 +    cout << "sync_thread: shutdown\n";
   1.121 +}
   1.122 +
   1.123 +SyncTests::SyncTests(string suitename, string test_home_dir) :
   1.124 +    EngineTestSessionSuite::EngineTestSessionSuite(suitename, test_home_dir) {
   1.125 +    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("SyncTests::check_sync"),
   1.126 +                                                                      static_cast<Func>(&SyncTests::check_sync)));
   1.127 +}
   1.128 +
   1.129 +void SyncTests::setup()
   1.130 +{
   1.131 +    EngineTestSessionSuite::setup();
   1.132 +
   1.133 +    pEp_identity *self = new_identity("alice@synctests.pEp", nullptr, "23", "Alice Miller");
   1.134 +    assert(self);
   1.135 +    cout << "setting own identity for " << self->address << "\n";
   1.136 +    PEP_STATUS status = myself(session, self);
   1.137 +    assert(self->me);
   1.138 +    assert(self->fpr);
   1.139 +    cout << "fpr: " << self->fpr << "\n";
   1.140 +    free_identity(self);
   1.141 +
   1.142 +    status = init(&sync, Sync_Adapter::messageToSend, Sync_Adapter::inject_sync_event);
   1.143 +    TEST_ASSERT(status == PEP_STATUS_OK);
   1.144 +
   1.145 +    cout << "initialize sync and start first state machine\n";
   1.146 +    status = register_sync_callbacks(
   1.147 +            sync,
   1.148 +            (void *) &adapter.q,
   1.149 +            Sync_Adapter::notifyHandshake,
   1.150 +            Sync_Adapter::retrieve_next_sync_event
   1.151 +        );
   1.152 +    TEST_ASSERT(status == PEP_STATUS_OK);
   1.153 +    TEST_ASSERT(sync->sync_state.keysync.state == Sole);
   1.154 +
   1.155 +    cout << "creating thread for sync\n";
   1.156 +    sync_thread = new thread(Sync_Adapter::sync_thread, sync, &adapter);
   1.157 +}
   1.158 +
   1.159 +void SyncTests::tear_down()
   1.160 +{
   1.161 +    adapter.processing();
   1.162 +
   1.163 +    cout << "sending shutdown to sync thread\n";
   1.164 +    adapter.q.push_front(nullptr);
   1.165 +    sync_thread->join();
   1.166 +
   1.167 +    unregister_sync_callbacks(sync);
   1.168 +    release(sync);
   1.169 +
   1.170 +    EngineTestSessionSuite::tear_down();
   1.171 +}
   1.172 +
   1.173 +void SyncTests::check_sync()
   1.174 +{
   1.175 +    cout << "check_sync(): trigger KeyGen event\n";
   1.176 +    signal_Sync_event(sync, Sync_PR_keysync, KeyGen);
   1.177 +    adapter.processing();
   1.178 +
   1.179 +    cout << "check_sync(): cry for unknown key\n";
   1.180 +    signal_Sync_event(sync, Sync_PR_keysync, CannotDecrypt);
   1.181 +}
   1.182 +