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.

467 lines
16 KiB

  1. #include <stdlib.h>
  2. #include <string>
  3. #include <cstring>
  4. #include "internal_format.h"
  5. #include "TestUtilities.h"
  6. #include "TestConstants.h"
  7. #include "Engine.h"
  8. #include <gtest/gtest.h>
  9. namespace {
  10. //The fixture for Engine619Test
  11. class Engine619Test : public ::testing::Test {
  12. public:
  13. Engine* engine;
  14. PEP_SESSION session;
  15. protected:
  16. // You can remove any or all of the following functions if its body
  17. // is empty.
  18. Engine619Test() {
  19. // You can do set-up work for each test here.
  20. test_suite_name = ::testing::UnitTest::GetInstance()->current_test_info()->GTEST_SUITE_SYM();
  21. test_name = ::testing::UnitTest::GetInstance()->current_test_info()->name();
  22. test_path = get_main_test_home_dir() + "/" + test_suite_name + "/" + test_name;
  23. std::cout << "QQQQ " << get_main_test_home_dir() << "," << test_suite_name << "," << test_name << "\n";
  24. }
  25. ~Engine619Test() override {
  26. // You can do clean-up work that doesn't throw exceptions here.
  27. }
  28. // If the constructor and destructor are not enough for setting up
  29. // and cleaning up each test, you can define the following methods:
  30. void SetUp() override {
  31. // Code here will be called immediately after the constructor (right
  32. // before each test).
  33. // Leave this empty if there are no files to copy to the home directory path
  34. std::vector<std::pair<std::string, std::string>> init_files = std::vector<std::pair<std::string, std::string>>();
  35. // Get a new test Engine.
  36. engine = new Engine(test_path);
  37. ASSERT_NOTNULL(engine);
  38. // Ok, let's initialize test directories etc.
  39. engine->prep(NULL, NULL, NULL, init_files);
  40. // Ok, try to start this bugger.
  41. engine->start();
  42. ASSERT_NOTNULL(engine->session);
  43. session = engine->session;
  44. // Engine is up. Keep on truckin'
  45. }
  46. void TearDown() override {
  47. // Code here will be called immediately after each test (right
  48. // before the destructor).
  49. engine->shut_down();
  50. delete engine;
  51. engine = NULL;
  52. session = NULL;
  53. }
  54. private:
  55. const char* test_suite_name;
  56. const char* test_name;
  57. string test_path;
  58. // Objects declared here can be used by all tests in the Engine619Test suite.
  59. };
  60. } // namespace
  61. // TEST_F(Engine619Test, check_internal_format) {
  62. // const char *data = "simulated data";
  63. // size_t data_size = strlen(data) + 1;
  64. // char *code;
  65. // size_t code_size;
  66. // // test PGP keys
  67. // PEP_STATUS status = encode_internal(data, data_size, "application/pgp-keys", &code, &code_size);
  68. // ASSERT_EQ(status, PEP_STATUS_OK);
  69. // ASSERT_EQ(code_size, data_size + 4);
  70. // ASSERT_EQ(code[0], 0);
  71. // ASSERT_EQ(code[1], 'K');
  72. // ASSERT_EQ(code[2], 2);
  73. // ASSERT_STREQ(code + 4, data);
  74. // // decode
  75. // char *value;
  76. // size_t size;
  77. // char *mime_type;
  78. // status = decode_internal(code, code_size, &value, &size, &mime_type);
  79. // ASSERT_EQ(status, PEP_STATUS_OK);
  80. // ASSERT_EQ(size, data_size);
  81. // ASSERT_STREQ(value, data);
  82. // ASSERT_STREQ(mime_type, "application/pgp-keys");
  83. // free(value);
  84. // free(code);
  85. // // test Sync
  86. // status = encode_internal(data, data_size, "application/pEp.sync", &code, &code_size);
  87. // ASSERT_EQ(status, PEP_STATUS_OK);
  88. // ASSERT_EQ(code_size, data_size + 4);
  89. // ASSERT_EQ(code[0], 0);
  90. // ASSERT_EQ(code[1], 'S');
  91. // ASSERT_EQ(code[2], 0);
  92. // ASSERT_STREQ(code + 4, data);
  93. // // decode
  94. // status = decode_internal(code, code_size, &value, &size, &mime_type);
  95. // ASSERT_EQ(status, PEP_STATUS_OK);
  96. // ASSERT_EQ(size, data_size);
  97. // ASSERT_STREQ(value, data);
  98. // ASSERT_STREQ(mime_type, "application/pEp.sync");
  99. // free(value);
  100. // free(code);
  101. // // test Distribution
  102. // status = encode_internal(data, data_size, "application/pEp.distribution", &code, &code_size);
  103. // ASSERT_EQ(status, PEP_STATUS_OK);
  104. // ASSERT_EQ(code_size, data_size + 4);
  105. // ASSERT_EQ(code[0], 0);
  106. // ASSERT_EQ(code[1], 'D');
  107. // ASSERT_EQ(code[2], 0);
  108. // ASSERT_STREQ(code + 4, data);
  109. // // decode
  110. // status = decode_internal(code, code_size, &value, &size, &mime_type);
  111. // ASSERT_EQ(status, PEP_STATUS_OK);
  112. // ASSERT_EQ(size, data_size);
  113. // ASSERT_STREQ(value, data);
  114. // ASSERT_STREQ(mime_type, "application/pEp.distribution");
  115. // free(value);
  116. // free(code);
  117. // // test PGP signature
  118. // status = encode_internal(data, data_size, "application/pgp-signature", &code, &code_size);
  119. // ASSERT_EQ(status, PEP_STATUS_OK);
  120. // ASSERT_EQ(code_size, data_size + 4);
  121. // ASSERT_EQ(code[0], 0);
  122. // ASSERT_EQ(code[1], 'A');
  123. // ASSERT_EQ(code[2], 2);
  124. // ASSERT_STREQ(code + 4, data);
  125. // // decode
  126. // status = decode_internal(code, code_size, &value, &size, &mime_type);
  127. // ASSERT_EQ(status, PEP_STATUS_OK);
  128. // ASSERT_EQ(size, data_size);
  129. // ASSERT_STREQ(value, data);
  130. // ASSERT_STREQ(mime_type, "application/pgp-signature");
  131. // free(value);
  132. // free(code);
  133. // }
  134. // TEST_F(Engine619Test, check_encrypt_decrypt_message) {
  135. // // a message from me, Alice, to Bob
  136. // const char* alice_fpr = "4ABE3AAF59AC32CFE4F86500A9411D176FF00E97";
  137. // const char* bob_fpr = "BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39";
  138. // PEP_STATUS status = read_file_and_import_key(session,
  139. // "test_keys/pub/pep-test-alice-0x6FF00E97_pub.asc");
  140. // ASSERT_EQ(status , PEP_KEY_IMPORTED);
  141. // status = set_up_ident_from_scratch(session,
  142. // "test_keys/priv/pep-test-alice-0x6FF00E97_priv.asc",
  143. // "pep.test.alice@pep-project.org", alice_fpr,
  144. // PEP_OWN_USERID, "Alice in Wonderland", NULL, true
  145. // );
  146. // ASSERT_OK;
  147. // ASSERT_TRUE(slurp_and_import_key(session, "test_keys/pub/pep-test-bob-0xC9C2EE39_pub.asc"));
  148. // message* msg = new_message(PEP_dir_outgoing);
  149. // pEp_identity* alice = new_identity("pep.test.alice@pep-project.org", NULL, PEP_OWN_USERID, NULL);
  150. // pEp_identity* bob = new_identity("pep.test.bob@pep-project.org", NULL, "Bob", NULL);
  151. // status = myself(session, alice);
  152. // ASSERT_OK;
  153. // status = update_identity(session, bob);
  154. // ASSERT_OK;
  155. // bob->fpr = strdup(bob_fpr);
  156. // status = set_identity(session, bob);
  157. // ASSERT_OK;
  158. // status = update_identity(session, bob);
  159. // ASSERT_OK;
  160. // status = set_as_pEp_user(session, bob);
  161. // ASSERT_OK;
  162. // msg->to = new_identity_list(bob);
  163. // msg->from = alice;
  164. // msg->shortmsg = strdup("Yo Bob!");
  165. // msg->longmsg = strdup("Look at my hot new sender fpr field!");
  166. // // Volker: This is a sloppy way to test - it got processed as a real distribution message because data has meaning
  167. // // and happily exposed a bug in your generation code, but... well, you know better :)
  168. // const char *distribution = "simulation of distribution data";
  169. // msg->attachments = new_bloblist(strdup(distribution), strlen(distribution)
  170. // + 1, "application/pEp.distribution", "distribution.pEp");
  171. // // encrypt this message inline
  172. // message* enc_msg = NULL;
  173. // status = encrypt_message(session, msg, NULL, &enc_msg, PEP_enc_inline, 0);
  174. // ASSERT_OK;
  175. // // .shortmsg will stay unencrypted
  176. // ASSERT_STREQ(msg->shortmsg, enc_msg->shortmsg);
  177. // // .longmsg will go encrypted
  178. // ASSERT_TRUE(is_PGP_message_text(enc_msg->longmsg));
  179. // ASSERT_TRUE(enc_msg->attachments);
  180. // ASSERT_TRUE(enc_msg->attachments->value);
  181. // bloblist_t *ad = enc_msg->attachments;
  182. // // distribution message is encrypted
  183. // ASSERT_TRUE(is_PGP_message_text(ad->value));
  184. // ASSERT_STREQ(ad->mime_type, "application/octet-stream");
  185. // ASSERT_STREQ(ad->filename, "distribution.pEp.pgp");
  186. // // next attachment
  187. // ASSERT_TRUE(ad->next);
  188. // ad = ad->next;
  189. // // attached key is encrypted
  190. // ASSERT_TRUE(is_PGP_message_text(ad->value));
  191. // ASSERT_STREQ(ad->mime_type, "application/octet-stream");
  192. // // ASSERT_STREQ(ad->filename, "file://pEpkey.asc.pgp");
  193. // // As of ENGINE-633:
  194. // ASSERT_STREQ(ad->filename, "file://sender_key.asc.pgp");
  195. // // decrypt this message
  196. // message *dec_msg = NULL;
  197. // stringlist_t *keylist = NULL;
  198. // PEP_rating rating;
  199. // PEP_decrypt_flags_t flags = 0;
  200. // status = decrypt_message(session, enc_msg, &dec_msg, &keylist, &rating, &flags);
  201. // ASSERT_EQ(status, PEP_STATUS_OK);
  202. // ASSERT_STREQ(dec_msg->shortmsg, enc_msg->shortmsg);
  203. // ASSERT_STREQ(msg->longmsg, dec_msg->longmsg);
  204. // // check attachments
  205. // ASSERT_TRUE(dec_msg->attachments);
  206. // ASSERT_TRUE(dec_msg->attachments->value);
  207. // bloblist_t *as = dec_msg->attachments;
  208. // bloblist_t *bl = msg->attachments;
  209. // ASSERT_STREQ(as->filename, "file://distribution.pEp");
  210. // // the MIME will be derived from filename
  211. // ASSERT_STREQ(as->mime_type, "application/pEp.distribution");
  212. // free_message(msg);
  213. // free_message(enc_msg);
  214. // free_message(dec_msg);
  215. // }
  216. // TEST_F(Engine619Test, check_encrypt_decrypt_message_elevated) {
  217. // // a message from me, Alice, to Bob
  218. // const char* alice_fpr = "4ABE3AAF59AC32CFE4F86500A9411D176FF00E97";
  219. // const char* bob_fpr = "BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39";
  220. // PEP_STATUS status = read_file_and_import_key(session,
  221. // "test_keys/pub/pep-test-alice-0x6FF00E97_pub.asc");
  222. // ASSERT_EQ(status , PEP_KEY_IMPORTED);
  223. // status = set_up_ident_from_scratch(session,
  224. // "test_keys/priv/pep-test-alice-0x6FF00E97_priv.asc",
  225. // "pep.test.alice@pep-project.org", alice_fpr,
  226. // PEP_OWN_USERID, "Alice in Wonderland", NULL, true
  227. // );
  228. // ASSERT_OK;
  229. // ASSERT_TRUE(slurp_and_import_key(session, "test_keys/pub/pep-test-bob-0xC9C2EE39_pub.asc"));
  230. // message* msg = new_message(PEP_dir_outgoing);
  231. // pEp_identity* alice = new_identity("pep.test.alice@pep-project.org", NULL, PEP_OWN_USERID, NULL);
  232. // pEp_identity* bob = new_identity("pep.test.bob@pep-project.org", NULL, "Bob", NULL);
  233. // status = myself(session, alice);
  234. // ASSERT_OK;
  235. // status = update_identity(session, bob);
  236. // ASSERT_OK;
  237. // bob->fpr = strdup(bob_fpr);
  238. // status = set_identity(session, bob);
  239. // ASSERT_OK;
  240. // status = update_identity(session, bob);
  241. // ASSERT_OK;
  242. // status = set_as_pEp_user(session, bob);
  243. // ASSERT_OK;
  244. // msg->to = new_identity_list(bob);
  245. // msg->from = alice;
  246. // msg->shortmsg = strdup("Yo Bob!");
  247. // msg->longmsg = strdup("Look at my hot new sender fpr field!");
  248. // const char *distribution = "simulation of distribution data";
  249. // msg->attachments = new_bloblist(strdup(distribution), strlen(distribution)
  250. // + 1, "application/pEp.distribution", "distribution.pEp");
  251. // // encrypt this message inline
  252. // message* enc_msg = NULL;
  253. // status = encrypt_message(session, msg, NULL, &enc_msg, PEP_enc_inline_EA, 0);
  254. // ASSERT_OK;
  255. // // .longmsg will go encrypted
  256. // ASSERT_TRUE(is_PGP_message_text(enc_msg->longmsg));
  257. // ASSERT_TRUE(enc_msg->attachments);
  258. // ASSERT_TRUE(enc_msg->attachments->value);
  259. // bloblist_t *ad = enc_msg->attachments;
  260. // // distribution message is encrypted
  261. // ASSERT_TRUE(is_PGP_message_text(ad->value));
  262. // ASSERT_STREQ(ad->mime_type, "application/octet-stream");
  263. // ASSERT_STREQ(ad->filename, "distribution.pEp.pgp");
  264. // // next attachment
  265. // ASSERT_TRUE(ad->next);
  266. // ad = ad->next;
  267. // // attached key is encrypted
  268. // ASSERT_TRUE(is_PGP_message_text(ad->value));
  269. // ASSERT_STREQ(ad->mime_type, "application/octet-stream");
  270. // // ASSERT_STREQ(ad->filename, "file://pEpkey.asc.pgp");
  271. // // As of ENGINE-633:
  272. // ASSERT_STREQ(ad->filename, "file://sender_key.asc.pgp");
  273. // // decrypt this message
  274. // char *ct = strdup(ad->value);
  275. // {
  276. // // test if this is an elevated attachment
  277. // char *pt;
  278. // size_t pt_size;
  279. // stringlist_t *keylist;
  280. // // decrypt this part
  281. // status = decrypt_and_verify(session, ct, strlen(ct) + 1, NULL, 0, &pt, &pt_size, &keylist, NULL);
  282. // ASSERT_EQ(status, PEP_DECRYPTED_AND_VERIFIED);
  283. // // decode internal message format
  284. // char *dt;
  285. // size_t dt_size;
  286. // char *mime_type;
  287. // status = decode_internal(pt, pt_size, &dt, &dt_size, &mime_type);
  288. // ASSERT_EQ(status, PEP_STATUS_OK);
  289. // ASSERT_TRUE(dt);
  290. // ASSERT_STREQ(mime_type, "application/pgp-keys");
  291. // free(pt);
  292. // free(dt);
  293. // free(mime_type);
  294. // free_stringlist(keylist);
  295. // }
  296. // // create artificial message for Key like a transport would do
  297. // message *art_msg = new_message(PEP_dir_incoming);
  298. // art_msg->enc_format = PEP_enc_inline_EA;
  299. // art_msg->from = identity_dup(enc_msg->to->ident);
  300. // art_msg->to = new_identity_list(identity_dup(enc_msg->from));
  301. // art_msg->longmsg = ct;
  302. // // decrypt this message
  303. // message *dec_msg = NULL;
  304. // stringlist_t *keylist = NULL;
  305. // PEP_rating rating;
  306. // PEP_decrypt_flags_t flags = 0;
  307. // status = decrypt_message(session, art_msg, &dec_msg, &keylist, &rating, &flags);
  308. // ASSERT_EQ(status, PEP_STATUS_OK);
  309. // ASSERT_TRUE(dec_msg);
  310. // // today the engine is sucking keys in
  311. // // ASSERT_STREQ(dec_msg->attachments->mime_type, "application/pgp-keys");
  312. // ASSERT_STREQ(dec_msg->shortmsg, "pEp");
  313. // stringpair_list_t *of;
  314. // bool pEp_auto_consume_found = false;
  315. // for (of = dec_msg->opt_fields; of && of->value; of = of->next) {
  316. // if (strcasecmp(of->value->key, "pEp-auto-consume") == 0) {
  317. // ASSERT_STREQ(of->value->value, "yes");
  318. // pEp_auto_consume_found = true;
  319. // break;
  320. // }
  321. // }
  322. // ASSERT_TRUE(pEp_auto_consume_found);
  323. // free_message(msg);
  324. // free_message(enc_msg);
  325. // free_message(dec_msg);
  326. // free_message(art_msg);
  327. // }
  328. TEST_F(Engine619Test, decrypt_message_with_private_key) {
  329. PEP_STATUS status = PEP_STATUS_OK;
  330. message *message_src
  331. = slurp_message_file_into_struct("test_mails/ExtraKeyPrivateKeyAttached.eml");
  332. ASSERT_NOTNULL (message_src);
  333. message *message_dst;
  334. stringlist_t* keys = NULL;
  335. PEP_rating rating;
  336. PEP_decrypt_flags_t flags = 0;
  337. status = decrypt_message(session, message_src, & message_dst,
  338. & keys, & rating, & flags);
  339. /* It is normal and expected that decryption fails here: this message is
  340. unencrypted. The point of this test is to arrive at a call to
  341. pgp_import_keydata , which used to corrut the heap (ENGINE-619). */
  342. ASSERT_EQ (status, PEP_UNENCRYPTED);
  343. /* Check that the message content matches what we expect. */
  344. ASSERT_NOTNULL (message_src->shortmsg);
  345. ASSERT_NOTNULL (message_src->longmsg);
  346. ASSERT_NULL (message_src->longmsg_formatted);
  347. /* The message has exactly one attachment, the private key. */
  348. bloblist_t *attachments = message_src->attachments;
  349. ASSERT_NOTNULL (attachments);
  350. ASSERT_NULL (attachments->next);
  351. /* We can even print it. */
  352. std::cerr << "THE FIRST ATTACHMENT HAS SIZE " << attachments->size << "\n";
  353. std::cerr << "THE FIRST ATTACHMENT IS:\n";
  354. std::cerr << attachments->value;
  355. std::cerr << "\n";
  356. }