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.

269 lines
8.8 KiB

  1. #include "test_utils.hh"
  2. #include <pEp/pEpEngine.h>
  3. #include <pEp/message_api.h>
  4. #include <pEp/keymanagement.h>
  5. #include <pEp/identity_list.h>
  6. #include <pEp/Adapter.hh>
  7. #include <pEp/status_to_string.hh>
  8. #include <pEp/mime.h>
  9. #include <tuple>
  10. namespace pEp {
  11. namespace Test {
  12. namespace Utils {
  13. //Ident
  14. pEpIdent wrap(::pEp_identity *const ident)
  15. {
  16. assert(ident);
  17. auto ret = pEpIdent(ident, [](::pEp_identity *) {});
  18. return ret;
  19. }
  20. pEpIdent appropriate(::pEp_identity *const ident)
  21. {
  22. assert(ident);
  23. auto ret = pEpIdent(ident, ::free_identity);
  24. return ret;
  25. }
  26. pEpIdent dup(const ::pEp_identity *const ident)
  27. {
  28. assert(ident);
  29. auto ret = pEpIdent(::identity_dup(ident), ::free_identity);
  30. return ret;
  31. }
  32. pEpIdent kill(::pEp_identity *const ident)
  33. {
  34. assert(ident);
  35. auto ret = pEpIdent(::identity_dup(ident), ::free_identity);
  36. ::free_identity(ident);
  37. return ret;
  38. }
  39. //IdentityList
  40. pEpIdentList wrap(::identity_list *const ident)
  41. {
  42. assert(ident);
  43. auto ret = pEpIdentList(ident, [](::identity_list *) {});
  44. return ret;
  45. }
  46. pEpIdentList appropriate(::identity_list *const ident)
  47. {
  48. assert(ident);
  49. auto ret = pEpIdentList(ident, ::free_identity_list);
  50. return ret;
  51. }
  52. pEpIdentList dup(const ::identity_list *const ident)
  53. {
  54. assert(ident);
  55. auto ret = pEpIdentList(::identity_list_dup(ident), ::free_identity_list);
  56. return ret;
  57. }
  58. pEpIdentList kill(::identity_list *const ident)
  59. {
  60. assert(ident);
  61. auto ret = pEpIdentList(::identity_list_dup(ident), ::free_identity_list);
  62. ::free_identity_list(ident);
  63. return ret;
  64. }
  65. //Message
  66. pEpMessage wrap(::message *const msg)
  67. {
  68. assert(msg);
  69. auto ret = pEpMessage(msg, [](::message *) {});
  70. return ret;
  71. }
  72. pEpMessage appropriate(::message *const msg)
  73. {
  74. assert(msg);
  75. auto ret = pEpMessage(msg, ::free_message);
  76. return ret;
  77. }
  78. pEpMessage dup(const ::message *const msg)
  79. {
  80. assert(msg);
  81. auto ret = pEpMessage(::message_dup(msg), ::free_message);
  82. return ret;
  83. }
  84. pEpMessage kill(::message *const msg)
  85. {
  86. assert(msg);
  87. auto ret = pEpMessage(::message_dup(msg), ::free_message);
  88. ::free_message(msg);
  89. return ret;
  90. }
  91. // helpers
  92. pEpIdent createOwnIdent(const std::string &address)
  93. {
  94. std::string name;
  95. ::pEp_identity *ident = nullptr;
  96. ident = ::new_identity(
  97. strdup(address.c_str()),
  98. "",
  99. PEP_OWN_USERID,
  100. ("myself " + address).c_str());
  101. ident->me = true;
  102. return appropriate(ident);
  103. }
  104. pEpIdent createCptIdent(const std::string &address)
  105. {
  106. std::string name;
  107. ::pEp_identity *ident = nullptr;
  108. ident = ::new_identity(
  109. strdup(address.c_str()),
  110. "",
  111. "",
  112. ("partner " + address).c_str());
  113. ident->me = false;
  114. return appropriate(ident);
  115. }
  116. pEpIdent createRawIdent(const std::string &address)
  117. {
  118. std::string name;
  119. ::pEp_identity *ident = nullptr;
  120. ident = ::new_identity(strdup(address.c_str()), "", "", "");
  121. ident->me = false;
  122. return appropriate(ident);
  123. }
  124. pEpIdentList createIdentityList(const std::vector<std::string> &addresses)
  125. {
  126. ::identity_list *list;
  127. list = ::new_identity_list(nullptr);
  128. for (std::string addr : addresses) {
  129. ::identity_list_add(list, ::identity_dup(createCptIdent(addr).get()));
  130. }
  131. return appropriate(list);
  132. }
  133. pEpMessage createMessage(pEpIdent from, pEpIdent to, const std::string &longmsg)
  134. {
  135. // create and fill in msg
  136. ::message *msg = ::new_message(PEP_dir_outgoing);
  137. msg->from = ::identity_dup(from.get());
  138. msg->to = ::new_identity_list(::identity_dup(to.get()));
  139. msg->longmsg = strdup(longmsg.c_str());
  140. pEpMessage ret = appropriate(msg);
  141. return ret;
  142. }
  143. pEpMessage createMessage(pEpIdent from, const std::string &to_addr, const std::string &longmsg)
  144. {
  145. pEpIdent to_ident = createCptIdent(to_addr);
  146. return createMessage(from, to_ident, longmsg);
  147. }
  148. std::string mimeEncode(const pEpMessage msg)
  149. {
  150. char *mimetext;
  151. PEP_STATUS status = ::mime_encode_message(msg.get(), false, &mimetext, false);
  152. throw_status(status);
  153. std::string text{ mimetext };
  154. free(mimetext);
  155. return text;
  156. }
  157. pEpMessage mimeDecode(const std::string &mime_text)
  158. {
  159. ::message *msg;
  160. bool has_possible_pEp_msg;
  161. ::PEP_STATUS status = ::mime_decode_message(
  162. mime_text.c_str(),
  163. mime_text.length(),
  164. &msg,
  165. &has_possible_pEp_msg);
  166. throw_status(status);
  167. return pEpMessage(msg, ::free_message);
  168. }
  169. EncryptResult encryptMessage(const pEpMessage msg)
  170. {
  171. pEpMessage msg_out;
  172. bool could_encrypt = false;
  173. ::message *msgenc = nullptr;
  174. PEP_STATUS status = ::encrypt_message(
  175. Adapter::session(),
  176. msg.get(),
  177. nullptr,
  178. &msgenc,
  179. PEP_enc_PEP,
  180. 0);
  181. throw_status(status);
  182. ::message *msg_out_p = nullptr;
  183. if (msgenc != nullptr) {
  184. could_encrypt = true;
  185. msg_out = appropriate(msgenc);
  186. } else {
  187. could_encrypt = false;
  188. msg_out = msg;
  189. }
  190. return EncryptResult(msg_out, "", could_encrypt);
  191. }
  192. DecryptResult decryptMessage(const pEpMessage msg, ::PEP_decrypt_flags_t *flags)
  193. {
  194. pEpMessage msg_out;
  195. bool was_encrypted = false;
  196. ::message *dec{ nullptr };
  197. ::stringlist_t *kl = ::new_stringlist("");
  198. ::PEP_rating rating;
  199. PEP_STATUS status = ::decrypt_message(
  200. Adapter::session(),
  201. msg.get(),
  202. &dec,
  203. &kl,
  204. &rating,
  205. flags);
  206. throw_status(status);
  207. if (dec != nullptr) {
  208. was_encrypted = true;
  209. msg_out = appropriate(dec);
  210. } else {
  211. was_encrypted = false;
  212. msg_out = msg;
  213. }
  214. return DecryptResult(msg_out, rating, kl, flags, was_encrypted);
  215. }
  216. DecryptResult decryptMessage(const pEpMessage msg)
  217. {
  218. ::PEP_decrypt_flags_t dummy{ 0 };
  219. return decryptMessage(msg, &dummy);
  220. }
  221. EncryptResult encryptAndEncode(const pEpMessage msg)
  222. {
  223. EncryptResult ret = encryptMessage(msg);
  224. std::string mime_text = mimeEncode(std::get<0>(ret));
  225. std::get<1>(ret) = mime_text;
  226. return ret;
  227. }
  228. DecryptResult decryptAndDecode(const std::string &mime_data)
  229. {
  230. DecryptResult ret;
  231. pEpMessage rx_msg = mimeDecode(mime_data);
  232. ret = decryptMessage(rx_msg);
  233. return ret;
  234. }
  235. } // namespace Utils
  236. } // namespace Test
  237. } // namespace pEp