PityTest11 is a very flexible C++11 peer-to-peer test framework supporting hierarchically structured test suites, multi-processing, logging, IPC, synchronization and more.
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.

131 lines
6.8 KiB

  1. // This file is under GNU General Public License 3.0
  2. // see LICENSE.txt
  3. #ifndef LIBPEPADAPTER_TEST_UTILS_HH
  4. #define LIBPEPADAPTER_TEST_UTILS_HH
  5. #include <pEp/pEpLog.hh>
  6. #include <string>
  7. #include <exception>
  8. #include <chrono>
  9. #include <thread>
  10. #include <cstring>
  11. #include <tuple>
  12. #include <pEp/pEpEngine.h>
  13. #include <pEp/identity_list.h>
  14. #include <pEp/message.h>
  15. #include <pEp/message_api.h>
  16. // ------------------------------------------------------------------------------------------------
  17. #ifndef ASSERT_EXCEPT
  18. #define ASSERT_EXCEPT(func) \
  19. do { \
  20. try { \
  21. (func); \
  22. assert(false); \
  23. } catch (const exception &e) { \
  24. pEp::Adapter::pEpLog::log(nested_exception_to_string(e)); \
  25. } \
  26. } while (0)
  27. #endif
  28. // ------------------------------------------------------------------------------------------------
  29. // Logging macros for testing
  30. // ------------------------------------------------------------------------------------------------
  31. // Use the macros if you need the message to be prefixed with "thread - __FILE__::__FUNTION__"
  32. // OTHERWISE, just use the logging functions from pEp::Adapter::pEpLog
  33. // TESTLOG - logformat "thread - __FILE__::__FUNTION__ - <message>"
  34. // To be used in a non-class/object context
  35. #ifndef TESTLOG
  36. #define TESTLOG(msg) \
  37. do { \
  38. std::stringstream msg_; \
  39. msg_ << "[" << getpid() << " " << std::this_thread::get_id() << "]"; \
  40. msg_ << " - " << __FILE__ << "::" << __FUNCTION__; \
  41. msg_ << " - " << msg; \
  42. pEp::Adapter::pEpLog::log(msg_.str()); \
  43. } while (0)
  44. #endif // TESTLOG
  45. // TESTLOGH1 - logformat "Thread - __FILE__::__FUNTION__ - <=============== message ==============>"
  46. #ifndef TESTLOGH1
  47. #define TESTLOGH1(msg) \
  48. do { \
  49. std::stringstream msg_; \
  50. msg_ << "[" << getpid() << " " << std::this_thread::get_id() << "]"; \
  51. msg_ << " - " << __FILE__ << "::" << __FUNCTION__; \
  52. msg_ << " - " << pEp::Adapter::pEpLog::decorateH1(msg); \
  53. pEp::Adapter::pEpLog::log(msg_.str()); \
  54. } while (0)
  55. #endif // TESTLOGH1
  56. // TESTLOGH2 - logformat "Thread - __FILE__::__FUNTION__ - <--------------- message -------------->"
  57. #ifndef TESTLOGH2
  58. #define TESTLOGH2(msg) \
  59. do { \
  60. std::stringstream msg_; \
  61. msg_ << "[" << getpid() << " " << std::this_thread::get_id() << "]"; \
  62. msg_ << " - " << __FILE__ << "::" << __FUNCTION__; \
  63. msg_ << " - " << pEp::Adapter::pEpLog::decorateH2(msg); \
  64. pEp::Adapter::pEpLog::log(msg_.str()); \
  65. } while (0)
  66. #endif // TESTLOGH2
  67. // ------------------------------------------------------------------------------------------------
  68. namespace pEp {
  69. namespace Test {
  70. namespace Utils {
  71. using pEpIdent = std::shared_ptr<::pEp_identity>;
  72. using pEpIdentList = std::shared_ptr<::identity_list>;
  73. using pEpMessage = std::shared_ptr<::message>;
  74. // [ DecryptedMessage, Rating, KeyList, Flags, WasEncrypted ]
  75. using DecryptResult = std::
  76. tuple<pEpMessage, ::PEP_rating, ::stringlist_t *, ::PEP_decrypt_flags_t *, bool>;
  77. // [ EncryptedMessage, MimeText, couldEncrypt ]
  78. using EncryptResult = std::tuple<pEpMessage, std::string, bool>;
  79. // Datatypes
  80. //Ident
  81. pEpIdent wrap(::pEp_identity *const ident);
  82. pEpIdent appropriate(::pEp_identity *const ident);
  83. pEpIdent dup(const ::pEp_identity *const ident);
  84. pEpIdent kill(::pEp_identity *const ident);
  85. //IdentityList
  86. pEpIdentList wrap(::identity_list *const ident);
  87. pEpIdentList appropriate(::identity_list *const ident);
  88. pEpIdentList dup(const ::identity_list *const ident);
  89. pEpIdentList kill(::identity_list *const ident);
  90. //Message
  91. pEpMessage wrap(::message *const msg);
  92. pEpMessage appropriate(::message *const msg);
  93. pEpMessage dup(const ::message *const msg);
  94. pEpMessage kill(::message *const msg);
  95. // helpers
  96. pEpIdent createOwnIdent(const std::string &address);
  97. pEpIdent createCptIdent(const std::string &address);
  98. pEpIdent createRawIdent(const std::string &address);
  99. pEpIdentList createIdentityList(const std::vector<std::string> &addresses);
  100. pEpMessage createMessage(pEpIdent from, pEpIdent to, const std::string &longmsg);
  101. pEpMessage createMessage(pEpIdent from, const std::string &to_addr, const std::string &longmsg);
  102. std::string mimeEncode(const pEpMessage msg);
  103. pEpMessage mimeDecode(const std::string &mime_text);
  104. EncryptResult encryptMessage(const pEpMessage msg);
  105. DecryptResult decryptMessage(const pEpMessage msg, ::PEP_decrypt_flags_t *flags);
  106. DecryptResult decryptMessage(const pEpMessage msg);
  107. EncryptResult encryptAndEncode(const pEpMessage msg);
  108. DecryptResult decryptAndDecode(const std::string &mime_data);
  109. } // namespace Utils
  110. } // namespace Test
  111. } // namespace pEp
  112. #endif // LIBPEPADAPTER_TEST_UTILS_HH