p≡p engine
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.

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