diff --git a/test/framework.cc b/test/framework.cc index 33dfe3e..22731da 100644 --- a/test/framework.cc +++ b/test/framework.cc @@ -14,6 +14,9 @@ #include #include +#include +#include +#include pEp::Test::Transport pEp::Test::transport; std::string pEp::Test::path; @@ -87,7 +90,12 @@ namespace pEp { return shared_ptr<::message>(msg, ::free_message); } - Message make_message(string text) + Identity make_identity(::pEp_identity *ident) + { + return shared_ptr<::pEp_identity>(ident , ::free_identity); + } + + Message mime_parse(string text) { ::message *msg; bool has_possible_pEp_msg; @@ -96,7 +104,7 @@ namespace pEp { return make_message(msg); } - string make_string(Message msg) + string mime_compose(Message msg) { char *mimetext; PEP_STATUS status = ::mime_encode_message(msg.get(), false, &mimetext, false); @@ -106,6 +114,40 @@ namespace pEp { return text; } + string make_pEp_msg(Message msg) + { + string text; + + ::message *_dst; + stringlist_t *keylist; + PEP_rating rating; + PEP_decrypt_flags_t flags = 0; + PEP_STATUS status = ::decrypt_message(session(), msg.get(), &_dst, &keylist, &rating, &flags); + throw_status(status); + Message dst = make_message(_dst); + + for (auto a = _dst->attachments; a && a->value; a = a->next) { + if (string("application/pEp.sync") == a->mime_type) { + char *_text; + status = PER_to_XER_Sync_msg(a->value, a->size, &_text); + throw_status(status); + text += _text; + pEp_free(_text); + return text; + } + else if (string("application/pEp.distribution") == a->mime_type) { + char *_text; + status = PER_to_XER_Distribution_msg(a->value, a->size, &_text); + throw_status(status); + text += _text; + pEp_free(_text); + return text; + } + } + + return text; + } + Message Transport::recv() { mkdir(inbox_path.c_str(), 0770); diff --git a/test/framework.hh b/test/framework.hh index ddb8fb7..a7e2a0a 100644 --- a/test/framework.hh +++ b/test/framework.hh @@ -19,16 +19,24 @@ namespace pEp { void import_key_from_file(string filename); using Message = shared_ptr<::message>; + using Identity= shared_ptr<::pEp_identity>; // use this instead of constructor to auto assign ::free_message as // deleter Message make_message(::message *msg); + // use this instead of constructor to auto assign ::free_identity as + // deleter + Identity make_identity(::pEp_identity *ident); + // MIME parser - Message make_message(string text); + Message mime_parse(string text); // MIME composer - string make_string(Message msg); + string mime_compose(Message msg); + + // Sync and Distribution decoder + string make_pEp_msg(Message msg); struct Transport { string inbox_path = "inbox"; diff --git a/test/test_leave_device_group.cc b/test/test_leave_device_group.cc new file mode 100644 index 0000000..9f11f97 --- /dev/null +++ b/test/test_leave_device_group.cc @@ -0,0 +1,84 @@ +#include + +#include "framework.hh" +#include "passphrase_cache.hh" +#include "callback_dispatcher.hh" + +#include + +using namespace pEp; +using namespace pEp::Adapter; +using namespace std; + +PEP_STATUS test_messageToSend(::message *_msg) +{ + Test::Message msg = Test::make_message(_msg); + cerr << Test::make_pEp_msg(msg); + return PEP_STATUS_OK; +} + + +PEP_STATUS test_notifyHandshake(pEp_identity *_me, pEp_identity *_partner, sync_handshake_signal signal) +{ + Test::Identity me = Test::make_identity(_me); + Test::Identity partner = Test::make_identity(_partner); + + return PEP_STATUS_OK; +} + +int main(int argc, char **argv) +{ + Test::setup(argc, argv); + + // set up two own identites for sync + + passphrase_cache.add("erwin"); + passphrase_cache.add("bob"); + + const char* bob_filename = ENGINE_TEST "/test_keys/bob-primary-with-password-bob-subkey-without.pgp"; + const char* bob_fpr = "5C76378A62B04CF3F41BEC8D4940FC9FA1878736"; + + const char* erwin_filename = ENGINE_TEST "/test_keys/erwin_normal_encrypted.pgp"; + const char* erwin_fpr = "CBA968BC01FCEB89F04CCF155C5E9E3F0420A570"; + + Test::import_key_from_file(bob_filename); + Test::import_key_from_file(erwin_filename); + + pEp_identity* bob = ::new_identity("bob@example.org", bob_fpr, "BOB", "Bob Dog"); + PEP_STATUS status = ::set_own_key(session(), bob, bob_fpr); + assert(status == PEP_STATUS_OK); + + status = ::enable_identity_for_sync(session(), bob); + assert(status == PEP_STATUS_OK); + + pEp_identity* erwin = ::new_identity("erwin@example.org", erwin_fpr, "BOB", "Bob is Erwin"); + status = ::set_own_key(session(), erwin, erwin_fpr); + assert(status == PEP_STATUS_OK); + + status = ::enable_identity_for_sync(session(), erwin); + assert(status == PEP_STATUS_OK); + + // simulate a device group by setting the identities to in sync + + status = set_identity_flags(session(), bob, PEP_idf_devicegroup); + status = set_identity_flags(session(), erwin, PEP_idf_devicegroup); + + // register at callback_dispatcher and start sync + + callback_dispatcher.add(test_messageToSend, test_notifyHandshake); + CallbackDispatcher::start_sync(); + + + // stop sync + CallbackDispatcher::stop_sync(); + + // free own identities and release session and release session + + ::free_identity(bob); + ::free_identity(erwin); + + session(Adapter::release); + + return 0; +} +