moved SyncTests out to SyncTests.cc.b0rken so sync can still be tested. sync
authorKrista Bennett <krista@pep-project.org>
Mon, 26 Nov 2018 14:22:02 +0100
branchsync
changeset 31590489203c122d
parent 3157 0096242e8fc9
child 3161 da688705137e
moved SyncTests out to SyncTests.cc.b0rken so sync can still be tested.
test/src/SuiteMaker.cc
test/src/engine_tests/MessageTwoPointOhTests.cc
test/src/engine_tests/SyncTests.cc
test/src/engine_tests/SyncTests.cc.b0rken
     1.1 --- a/test/src/SuiteMaker.cc	Fri Nov 23 18:10:27 2018 +0100
     1.2 +++ b/test/src/SuiteMaker.cc	Mon Nov 26 14:22:02 2018 +0100
     1.3 @@ -53,6 +53,7 @@
     1.4  #include "CrashdumpTests.h"
     1.5  #include "CaseAndDotAddressTests.h"
     1.6  #include "ExpiredSubkeyTests.h"
     1.7 +#include "LeastCommonDenomColorTests.h"
     1.8  #include "ExternalRevokeTests.h"
     1.9  #include "UserIDAliasTests.h"
    1.10  
    1.11 @@ -99,12 +100,13 @@
    1.12      "CrashdumpTests",
    1.13      "CaseAndDotAddressTests",
    1.14      "ExpiredSubkeyTests",
    1.15 +    "LeastCommonDenomColorTests",
    1.16      "ExternalRevokeTests",
    1.17      "UserIDAliasTests",
    1.18  };
    1.19  
    1.20  // This file is generated, so magic constants are ok.
    1.21 -int SuiteMaker::num_suites = 43;
    1.22 +int SuiteMaker::num_suites = 44;
    1.23  
    1.24  void SuiteMaker::suitemaker_build(const char* test_class_name, const char* test_home, Test::Suite** test_suite) {
    1.25      if (strcmp(test_class_name, "DecorateTests") == 0)
    1.26 @@ -189,6 +191,8 @@
    1.27          *test_suite = new CaseAndDotAddressTests(test_class_name, test_home);
    1.28      else if (strcmp(test_class_name, "ExpiredSubkeyTests") == 0)
    1.29          *test_suite = new ExpiredSubkeyTests(test_class_name, test_home);
    1.30 +    else if (strcmp(test_class_name, "LeastCommonDenomColorTests") == 0)
    1.31 +        *test_suite = new LeastCommonDenomColorTests(test_class_name, test_home);
    1.32      else if (strcmp(test_class_name, "ExternalRevokeTests") == 0)
    1.33          *test_suite = new ExternalRevokeTests(test_class_name, test_home);
    1.34      else if (strcmp(test_class_name, "UserIDAliasTests") == 0)
     2.1 --- a/test/src/engine_tests/MessageTwoPointOhTests.cc	Fri Nov 23 18:10:27 2018 +0100
     2.2 +++ b/test/src/engine_tests/MessageTwoPointOhTests.cc	Mon Nov 26 14:22:02 2018 +0100
     2.3 @@ -28,7 +28,7 @@
     2.4  
     2.5  void MessageTwoPointOhTests::check_message_two_point_oh() {
     2.6  
     2.7 -    PEP_comm_type carol_comm_type = PEP_ct_OpenPGP_unconfirmed;
     2.8 +    PEP_comm_type carol_comm_type = PEP_ct_pEp;
     2.9  
    2.10      // message_api test code
    2.11  
     3.1 --- a/test/src/engine_tests/SyncTests.cc	Fri Nov 23 18:10:27 2018 +0100
     3.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.3 @@ -1,179 +0,0 @@
     3.4 -// This file is under GNU General Public License 3.0
     3.5 -// see LICENSE.txt
     3.6 -
     3.7 -#include <stdlib.h>
     3.8 -#include <string>
     3.9 -
    3.10 -#include "pEpEngine.h"
    3.11 -
    3.12 -#include "pEp_internal.h"
    3.13 -#include "KeySync_fsm.h"
    3.14 -#include "sync_codec.h"
    3.15 -
    3.16 -#include "EngineTestSessionSuite.h"
    3.17 -#include "SyncTests.h"
    3.18 -
    3.19 -using namespace std;
    3.20 -
    3.21 -void Sync_Adapter::processing()
    3.22 -{
    3.23 -    cout << "waiting for processing\n";
    3.24 -    while (!q.empty()) {
    3.25 -        nanosleep((const struct timespec[]){{0, 100000000L}}, NULL);
    3.26 -    }
    3.27 -}
    3.28 -
    3.29 -PEP_STATUS Sync_Adapter::notifyHandshake(
    3.30 -        pEp_identity *me,
    3.31 -        pEp_identity *partner,
    3.32 -        sync_handshake_signal signal
    3.33 -    )
    3.34 -{
    3.35 -    return PEP_STATUS_OK;
    3.36 -}
    3.37 -
    3.38 -int Sync_Adapter::inject_sync_event(SYNC_EVENT ev, void *management)
    3.39 -{
    3.40 -    Sync_event_t *_ev = ev;
    3.41 -    switch (_ev->fsm) {
    3.42 -        case Sync_PR_keysync:
    3.43 -            cout << "injecting event " << KeySync_event_name(_ev->event) << "\n";
    3.44 -            break;
    3.45 -        default:
    3.46 -            cout << "unknown state machine: " << _ev->fsm << "\n";
    3.47 -            assert(0);
    3.48 -    }
    3.49 -    auto adapter = static_cast< Sync_Adapter *>(management);
    3.50 -    adapter->q.push_front(ev);
    3.51 -    return 0;
    3.52 -}
    3.53 -
    3.54 -Sync_event_t *Sync_Adapter::retrieve_next_sync_event(void *management, time_t threshold)
    3.55 -{
    3.56 -    auto adapter = static_cast< Sync_Adapter *>(management);
    3.57 -    time_t started = time(nullptr);
    3.58 -    bool timeout = false;
    3.59 -
    3.60 -    while (adapter->q.empty()) {
    3.61 -        int i = 0;
    3.62 -        ++i;
    3.63 -        if (i > 10) {
    3.64 -            if (time(nullptr) > started + threshold) {
    3.65 -                timeout = true;
    3.66 -                break;
    3.67 -            }
    3.68 -            i = 0;
    3.69 -        }
    3.70 -        nanosleep((const struct timespec[]){{0, 100000000L}}, NULL);
    3.71 -    }
    3.72 -
    3.73 -    if (timeout)
    3.74 -        return SYNC_TIMEOUT_EVENT;
    3.75 -
    3.76 -    Sync_event_t *ev = adapter->q.pop_front();
    3.77 -    if (ev) {
    3.78 -        switch (ev->fsm) {
    3.79 -            case Sync_PR_keysync:
    3.80 -                cout << "sync thread: retrieving event " << KeySync_event_name(ev->event) << "\n";
    3.81 -                break;
    3.82 -            default:
    3.83 -                cout << "sync thread: unknown state machine: " << ev->fsm << "\n";
    3.84 -                assert(0);
    3.85 -        }
    3.86 -    }
    3.87 -    else {
    3.88 -        cout << "sync thread: retrieving shutdown\n";
    3.89 -    }
    3.90 -
    3.91 -    return ev;
    3.92 -}
    3.93 -
    3.94 -PEP_STATUS Sync_Adapter::messageToSend(struct _message *msg)
    3.95 -{
    3.96 -    assert(msg && msg->attachments);
    3.97 -    
    3.98 -    cout << "sending message:\n";
    3.99 -
   3.100 -    for (bloblist_t *b = msg->attachments; b && b->value; b = b->next) {
   3.101 -        if (b->mime_type && strcasecmp(b->mime_type, "application/pEp.sync") == 0) {
   3.102 -            assert(msg->from && msg->from->address && msg->from->username);
   3.103 -            cout << "<!-- " << msg->from->username << " <" << msg->from->address << "> -->\n";
   3.104 -            char *text = NULL;
   3.105 -            PEP_STATUS status = PER_to_XER_Sync_msg(msg->attachments->value, msg->attachments->size, &text);
   3.106 -            assert(status == PEP_STATUS_OK);
   3.107 -            cout << text << "\n";
   3.108 -            free(text);
   3.109 -        }
   3.110 -    }
   3.111 -
   3.112 -    free_message(msg);
   3.113 -    return PEP_STATUS_OK;
   3.114 -}
   3.115 -
   3.116 -void Sync_Adapter::sync_thread(PEP_SESSION session, Sync_Adapter *adapter)
   3.117 -{
   3.118 -    cout << "sync_thread: startup\n";
   3.119 -    do_sync_protocol(session, adapter);
   3.120 -    cout << "sync_thread: shutdown\n";
   3.121 -}
   3.122 -
   3.123 -SyncTests::SyncTests(string suitename, string test_home_dir) :
   3.124 -    EngineTestSessionSuite::EngineTestSessionSuite(suitename, test_home_dir) {
   3.125 -    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("SyncTests::check_sync"),
   3.126 -                                                                      static_cast<Func>(&SyncTests::check_sync)));
   3.127 -}
   3.128 -
   3.129 -void SyncTests::setup()
   3.130 -{
   3.131 -    EngineTestSessionSuite::setup();
   3.132 -
   3.133 -    pEp_identity *self = new_identity("alice@synctests.pEp", nullptr, "23", "Alice Miller");
   3.134 -    assert(self);
   3.135 -    cout << "setting own identity for " << self->address << "\n";
   3.136 -    PEP_STATUS status = myself(session, self);
   3.137 -    assert(self->me);
   3.138 -    assert(self->fpr);
   3.139 -    cout << "fpr: " << self->fpr << "\n";
   3.140 -    free_identity(self);
   3.141 -
   3.142 -    status = init(&sync, Sync_Adapter::messageToSend, Sync_Adapter::inject_sync_event);
   3.143 -    TEST_ASSERT(status == PEP_STATUS_OK);
   3.144 -
   3.145 -    cout << "initialize sync and start first state machine\n";
   3.146 -    status = register_sync_callbacks(
   3.147 -            sync,
   3.148 -            (void *) &adapter.q,
   3.149 -            Sync_Adapter::notifyHandshake,
   3.150 -            Sync_Adapter::retrieve_next_sync_event
   3.151 -        );
   3.152 -    TEST_ASSERT(status == PEP_STATUS_OK);
   3.153 -    TEST_ASSERT(sync->sync_state.keysync.state == Sole);
   3.154 -
   3.155 -    cout << "creating thread for sync\n";
   3.156 -    sync_thread = new thread(Sync_Adapter::sync_thread, sync, &adapter);
   3.157 -}
   3.158 -
   3.159 -void SyncTests::tear_down()
   3.160 -{
   3.161 -    adapter.processing();
   3.162 -
   3.163 -    cout << "sending shutdown to sync thread\n";
   3.164 -    adapter.q.push_front(nullptr);
   3.165 -    sync_thread->join();
   3.166 -
   3.167 -    unregister_sync_callbacks(sync);
   3.168 -    release(sync);
   3.169 -
   3.170 -    EngineTestSessionSuite::tear_down();
   3.171 -}
   3.172 -
   3.173 -void SyncTests::check_sync()
   3.174 -{
   3.175 -    cout << "check_sync(): trigger KeyGen event\n";
   3.176 -    signal_Sync_event(sync, Sync_PR_keysync, KeyGen);
   3.177 -    adapter.processing();
   3.178 -
   3.179 -    cout << "check_sync(): cry for unknown key\n";
   3.180 -    signal_Sync_event(sync, Sync_PR_keysync, CannotDecrypt);
   3.181 -}
   3.182 -
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/test/src/engine_tests/SyncTests.cc.b0rken	Mon Nov 26 14:22:02 2018 +0100
     4.3 @@ -0,0 +1,179 @@
     4.4 +// This file is under GNU General Public License 3.0
     4.5 +// see LICENSE.txt
     4.6 +
     4.7 +#include <stdlib.h>
     4.8 +#include <string>
     4.9 +
    4.10 +#include "pEpEngine.h"
    4.11 +
    4.12 +#include "pEp_internal.h"
    4.13 +#include "KeySync_fsm.h"
    4.14 +#include "sync_codec.h"
    4.15 +
    4.16 +#include "EngineTestSessionSuite.h"
    4.17 +#include "SyncTests.h"
    4.18 +
    4.19 +using namespace std;
    4.20 +
    4.21 +void Sync_Adapter::processing()
    4.22 +{
    4.23 +    cout << "waiting for processing\n";
    4.24 +    while (!q.empty()) {
    4.25 +        nanosleep((const struct timespec[]){{0, 100000000L}}, NULL);
    4.26 +    }
    4.27 +}
    4.28 +
    4.29 +PEP_STATUS Sync_Adapter::notifyHandshake(
    4.30 +        pEp_identity *me,
    4.31 +        pEp_identity *partner,
    4.32 +        sync_handshake_signal signal
    4.33 +    )
    4.34 +{
    4.35 +    return PEP_STATUS_OK;
    4.36 +}
    4.37 +
    4.38 +int Sync_Adapter::inject_sync_event(SYNC_EVENT ev, void *management)
    4.39 +{
    4.40 +    Sync_event_t *_ev = ev;
    4.41 +    switch (_ev->fsm) {
    4.42 +        case Sync_PR_keysync:
    4.43 +            cout << "injecting event " << KeySync_event_name(_ev->event) << "\n";
    4.44 +            break;
    4.45 +        default:
    4.46 +            cout << "unknown state machine: " << _ev->fsm << "\n";
    4.47 +            assert(0);
    4.48 +    }
    4.49 +    auto adapter = static_cast< Sync_Adapter *>(management);
    4.50 +    adapter->q.push_front(ev);
    4.51 +    return 0;
    4.52 +}
    4.53 +
    4.54 +Sync_event_t *Sync_Adapter::retrieve_next_sync_event(void *management, time_t threshold)
    4.55 +{
    4.56 +    auto adapter = static_cast< Sync_Adapter *>(management);
    4.57 +    time_t started = time(nullptr);
    4.58 +    bool timeout = false;
    4.59 +
    4.60 +    while (adapter->q.empty()) {
    4.61 +        int i = 0;
    4.62 +        ++i;
    4.63 +        if (i > 10) {
    4.64 +            if (time(nullptr) > started + threshold) {
    4.65 +                timeout = true;
    4.66 +                break;
    4.67 +            }
    4.68 +            i = 0;
    4.69 +        }
    4.70 +        nanosleep((const struct timespec[]){{0, 100000000L}}, NULL);
    4.71 +    }
    4.72 +
    4.73 +    if (timeout)
    4.74 +        return SYNC_TIMEOUT_EVENT;
    4.75 +
    4.76 +    Sync_event_t *ev = adapter->q.pop_front();
    4.77 +    if (ev) {
    4.78 +        switch (ev->fsm) {
    4.79 +            case Sync_PR_keysync:
    4.80 +                cout << "sync thread: retrieving event " << KeySync_event_name(ev->event) << "\n";
    4.81 +                break;
    4.82 +            default:
    4.83 +                cout << "sync thread: unknown state machine: " << ev->fsm << "\n";
    4.84 +                assert(0);
    4.85 +        }
    4.86 +    }
    4.87 +    else {
    4.88 +        cout << "sync thread: retrieving shutdown\n";
    4.89 +    }
    4.90 +
    4.91 +    return ev;
    4.92 +}
    4.93 +
    4.94 +PEP_STATUS Sync_Adapter::messageToSend(struct _message *msg)
    4.95 +{
    4.96 +    assert(msg && msg->attachments);
    4.97 +    
    4.98 +    cout << "sending message:\n";
    4.99 +
   4.100 +    for (bloblist_t *b = msg->attachments; b && b->value; b = b->next) {
   4.101 +        if (b->mime_type && strcasecmp(b->mime_type, "application/pEp.sync") == 0) {
   4.102 +            assert(msg->from && msg->from->address && msg->from->username);
   4.103 +            cout << "<!-- " << msg->from->username << " <" << msg->from->address << "> -->\n";
   4.104 +            char *text = NULL;
   4.105 +            PEP_STATUS status = PER_to_XER_Sync_msg(msg->attachments->value, msg->attachments->size, &text);
   4.106 +            assert(status == PEP_STATUS_OK);
   4.107 +            cout << text << "\n";
   4.108 +            free(text);
   4.109 +        }
   4.110 +    }
   4.111 +
   4.112 +    free_message(msg);
   4.113 +    return PEP_STATUS_OK;
   4.114 +}
   4.115 +
   4.116 +void Sync_Adapter::sync_thread(PEP_SESSION session, Sync_Adapter *adapter)
   4.117 +{
   4.118 +    cout << "sync_thread: startup\n";
   4.119 +    do_sync_protocol(session, adapter);
   4.120 +    cout << "sync_thread: shutdown\n";
   4.121 +}
   4.122 +
   4.123 +SyncTests::SyncTests(string suitename, string test_home_dir) :
   4.124 +    EngineTestSessionSuite::EngineTestSessionSuite(suitename, test_home_dir) {
   4.125 +    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("SyncTests::check_sync"),
   4.126 +                                                                      static_cast<Func>(&SyncTests::check_sync)));
   4.127 +}
   4.128 +
   4.129 +void SyncTests::setup()
   4.130 +{
   4.131 +    EngineTestSessionSuite::setup();
   4.132 +
   4.133 +    pEp_identity *self = new_identity("alice@synctests.pEp", nullptr, "23", "Alice Miller");
   4.134 +    assert(self);
   4.135 +    cout << "setting own identity for " << self->address << "\n";
   4.136 +    PEP_STATUS status = myself(session, self);
   4.137 +    assert(self->me);
   4.138 +    assert(self->fpr);
   4.139 +    cout << "fpr: " << self->fpr << "\n";
   4.140 +    free_identity(self);
   4.141 +
   4.142 +    status = init(&sync, Sync_Adapter::messageToSend, Sync_Adapter::inject_sync_event);
   4.143 +    TEST_ASSERT(status == PEP_STATUS_OK);
   4.144 +
   4.145 +    cout << "initialize sync and start first state machine\n";
   4.146 +    status = register_sync_callbacks(
   4.147 +            sync,
   4.148 +            (void *) &adapter.q,
   4.149 +            Sync_Adapter::notifyHandshake,
   4.150 +            Sync_Adapter::retrieve_next_sync_event
   4.151 +        );
   4.152 +    TEST_ASSERT(status == PEP_STATUS_OK);
   4.153 +    TEST_ASSERT(sync->sync_state.keysync.state == Sole);
   4.154 +
   4.155 +    cout << "creating thread for sync\n";
   4.156 +    sync_thread = new thread(Sync_Adapter::sync_thread, sync, &adapter);
   4.157 +}
   4.158 +
   4.159 +void SyncTests::tear_down()
   4.160 +{
   4.161 +    adapter.processing();
   4.162 +
   4.163 +    cout << "sending shutdown to sync thread\n";
   4.164 +    adapter.q.push_front(nullptr);
   4.165 +    sync_thread->join();
   4.166 +
   4.167 +    unregister_sync_callbacks(sync);
   4.168 +    release(sync);
   4.169 +
   4.170 +    EngineTestSessionSuite::tear_down();
   4.171 +}
   4.172 +
   4.173 +void SyncTests::check_sync()
   4.174 +{
   4.175 +    cout << "check_sync(): trigger KeyGen event\n";
   4.176 +    signal_Sync_event(sync, Sync_PR_keysync, KeyGen);
   4.177 +    adapter.processing();
   4.178 +
   4.179 +    cout << "check_sync(): cry for unknown key\n";
   4.180 +    signal_Sync_event(sync, Sync_PR_keysync, CannotDecrypt);
   4.181 +}
   4.182 +