C++11 library providing functionality common to all adapters.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
3.9 KiB

2 years ago
2 years ago
2 years ago
  1. // This file is under GNU General Public License 3.0
  2. // see LICENSE.txt
  3. #include "framework/framework.hh"
  4. #include <iostream>
  5. #include <vector>
  6. #include <pEp/sync_api.h>
  7. #include "../src/callback_dispatcher.hh"
  8. #include "../src/passphrase_cache.hh"
  9. using namespace std;
  10. using namespace pEp;
  11. vector<string> expected_msg = { "synchronizeGroupKeys",
  12. "groupKeysUpdate",
  13. "initUnledGroupKeyReset",
  14. "beacon",
  15. "beacon" };
  16. vector<::sync_handshake_signal> expected_notification = { SYNC_NOTIFY_IN_GROUP,
  17. SYNC_NOTIFY_START,
  18. SYNC_NOTIFY_SOLE,
  19. SYNC_NOTIFY_START,
  20. SYNC_NOTIFY_STOP };
  21. ::PEP_STATUS test_messageToSend(::message* _msg)
  22. {
  23. static auto actual = expected_msg.begin();
  24. Test::Message msg = Test::make_message(_msg);
  25. string text = Test::make_pEp_msg(msg);
  26. cerr << "expecting: " << *actual << endl;
  27. cerr << text;
  28. assert(text.find(*actual++) != string::npos);
  29. return PEP_STATUS_OK;
  30. }
  31. ::PEP_STATUS test_notifyHandshake(::pEp_identity* _me, ::pEp_identity* _partner, sync_handshake_signal signal)
  32. {
  33. static auto actual = expected_notification.begin();
  34. Test::Identity me = Test::make_identity(_me);
  35. Test::Identity partner = Test::make_identity(_partner);
  36. cerr << "expecting: " << *actual << endl;
  37. cerr << "notifyHandshake: " << signal << endl;
  38. assert(signal == *actual++);
  39. return PEP_STATUS_OK;
  40. }
  41. int main(int argc, char** argv)
  42. {
  43. Test::setup(argc, argv);
  44. Adapter::session();
  45. // set up two own identites for sync
  46. passphrase_cache.add("erwin");
  47. passphrase_cache.add("bob");
  48. const char* bob_filename = ENGINE_TEST
  49. "/test_keys/bob-primary-with-password-bob-subkey-without.pgp";
  50. const char* bob_fpr = "5C76378A62B04CF3F41BEC8D4940FC9FA1878736";
  51. const char* erwin_filename = ENGINE_TEST "/test_keys/erwin_normal_encrypted.pgp";
  52. const char* erwin_fpr = "CBA968BC01FCEB89F04CCF155C5E9E3F0420A570";
  53. Test::import_key_from_file(bob_filename);
  54. Test::import_key_from_file(erwin_filename);
  55. Test::Identity bob = Test::make_identity(
  56. ::new_identity("bob@example.org", bob_fpr, "BOB", "Bob Dog"));
  57. PEP_STATUS status = ::set_own_key(Adapter::session(), bob.get(), bob_fpr);
  58. assert(status == PEP_STATUS_OK);
  59. status = ::enable_identity_for_sync(Adapter::session(), bob.get());
  60. assert(status == PEP_STATUS_OK);
  61. Test::Identity erwin = Test::make_identity(
  62. ::new_identity("erwin@example.org", erwin_fpr, "BOB", "Bob is Erwin"));
  63. status = ::set_own_key(Adapter::session(), erwin.get(), erwin_fpr);
  64. assert(status == PEP_STATUS_OK);
  65. status = ::enable_identity_for_sync(Adapter::session(), erwin.get());
  66. assert(status == PEP_STATUS_OK);
  67. // simulate a device group by setting the identities to in sync
  68. status = ::set_identity_flags(Adapter::session(), bob.get(), PEP_idf_devicegroup);
  69. status = ::set_identity_flags(Adapter::session(), erwin.get(), PEP_idf_devicegroup);
  70. // register at callback_dispatcher and start sync
  71. callback_dispatcher.add(test_messageToSend, test_notifyHandshake);
  72. CallbackDispatcher::start_sync();
  73. // leave device group
  74. status = ::leave_device_group(Adapter::session());
  75. // wait for sync shutdown and release first session
  76. Test::join_sync_thread();
  77. assert(!Adapter::is_sync_running());
  78. // switch off and on again
  79. CallbackDispatcher::start_sync();
  80. sleep(2);
  81. assert(Adapter::is_sync_running());
  82. CallbackDispatcher::stop_sync();
  83. Test::join_sync_thread();
  84. assert(!Adapter::is_sync_running());
  85. Adapter::session(Adapter::release);
  86. return 0;
  87. }