p≡p engine fork for my own dirty testing of stuff
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.

195 lines
7.5 KiB

  1. #include <stdlib.h>
  2. #include <string>
  3. #include <cstring>
  4. #include "pEpEngine.h"
  5. #include "pEpEngine_internal.h"
  6. #include "test_util.h"
  7. #include "TestConstants.h"
  8. #include "Engine.h"
  9. #include <gtest/gtest.h>
  10. namespace {
  11. //The fixture for TestSetDefaultFPRTest
  12. class TestSetDefaultFPRTest : public ::testing::Test {
  13. public:
  14. Engine* engine;
  15. PEP_SESSION session;
  16. const char* carol_fpr = "8DD4F5827B45839E9ACCA94687BDDFFB42A85A42";
  17. const char* bob_fpr = "BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39";
  18. const char* alice_fpr = "4ABE3AAF59AC32CFE4F86500A9411D176FF00E97";
  19. protected:
  20. // You can remove any or all of the following functions if its body
  21. // is empty.
  22. TestSetDefaultFPRTest() {
  23. // You can do set-up work for each test here.
  24. test_suite_name = ::testing::UnitTest::GetInstance()->current_test_info()->GTEST_SUITE_SYM();
  25. test_name = ::testing::UnitTest::GetInstance()->current_test_info()->name();
  26. test_path = get_main_test_home_dir() + "/" + test_suite_name + "/" + test_name;
  27. }
  28. ~TestSetDefaultFPRTest() override {
  29. // You can do clean-up work that doesn't throw exceptions here.
  30. }
  31. // If the constructor and destructor are not enough for setting up
  32. // and cleaning up each test, you can define the following methods:
  33. void SetUp() override {
  34. // Code here will be called immediately after the constructor (right
  35. // before each test).
  36. // Leave this empty if there are no files to copy to the home directory path
  37. std::vector<std::pair<std::string, std::string>> init_files = std::vector<std::pair<std::string, std::string>>();
  38. // Get a new test Engine.
  39. engine = new Engine(test_path);
  40. ASSERT_NE(engine, nullptr);
  41. // Ok, let's initialize test directories etc.
  42. engine->prep(NULL, NULL, NULL, init_files);
  43. // Ok, try to start this bugger.
  44. engine->start();
  45. ASSERT_NE(engine->session, nullptr);
  46. session = engine->session;
  47. // Engine is up. Keep on truckin'
  48. slurp_and_import_key(session, "test_keys/pub/pep-test-carol-0x42A85A42_pub.asc");
  49. slurp_and_import_key(session, "test_keys/pub/pep-test-bob-0xC9C2EE39_pub.asc");
  50. }
  51. void TearDown() override {
  52. // Code here will be called immediately after each test (right
  53. // before the destructor).
  54. engine->shut_down();
  55. delete engine;
  56. engine = NULL;
  57. session = NULL;
  58. }
  59. private:
  60. const char* test_suite_name;
  61. const char* test_name;
  62. string test_path;
  63. };
  64. } // namespace
  65. TEST_F(TestSetDefaultFPRTest, check_test_set_default_fpr) {
  66. pEp_identity* carol = NULL;
  67. PEP_STATUS status = set_up_preset(session, CAROL,
  68. true, true, false, false, false, &carol);
  69. status = update_identity(session, carol);
  70. ASSERT_OK;
  71. ASSERT_EQ(carol->fpr, nullptr);
  72. status = set_default_identity_fpr(session, carol->user_id, carol->address, carol_fpr);
  73. ASSERT_OK;
  74. status = update_identity(session, carol);
  75. ASSERT_OK;
  76. ASSERT_STREQ(carol->fpr, carol_fpr);
  77. status = set_default_identity_fpr(session, carol->user_id, carol->address, bob_fpr);
  78. ASSERT_OK;
  79. status = update_identity(session, carol);
  80. ASSERT_OK;
  81. ASSERT_STREQ(carol->fpr, bob_fpr);
  82. // Now let's set one that we don't have.
  83. status = set_default_identity_fpr(session, carol->user_id, carol->address, alice_fpr);
  84. ASSERT_OK;
  85. status = update_identity(session, carol);
  86. ASSERT_OK;
  87. ASSERT_STREQ(carol->fpr, carol_fpr); // Should be the user default, we don't have alice_fpr's key
  88. ASSERT_EQ(carol->comm_type, PEP_ct_pEp_unconfirmed);
  89. free_identity(carol);
  90. }
  91. TEST_F(TestSetDefaultFPRTest, check_test_set_comm_partner_key) {
  92. pEp_identity* carol = NULL;
  93. PEP_STATUS status = set_up_preset(session, CAROL,
  94. true, true, false, false, false, &carol);
  95. status = set_comm_partner_key(session, carol, carol_fpr);
  96. ASSERT_OK;
  97. status = update_identity(session, carol);
  98. ASSERT_OK;
  99. ASSERT_STREQ(carol->fpr, carol_fpr);
  100. status = set_comm_partner_key(session, carol, bob_fpr);
  101. ASSERT_OK;
  102. status = update_identity(session, carol);
  103. ASSERT_OK;
  104. ASSERT_STREQ(carol->fpr, bob_fpr);
  105. // Now let's set one that we don't have.
  106. status = set_comm_partner_key(session, carol, alice_fpr);
  107. ASSERT_OK;
  108. status = update_identity(session, carol);
  109. ASSERT_OK;
  110. ASSERT_STREQ(carol->fpr, carol_fpr); // Should be the user default, we don't have alice_fpr's key
  111. ASSERT_EQ(carol->comm_type, PEP_ct_pEp_unconfirmed);
  112. free_identity(carol);
  113. }
  114. TEST_F(TestSetDefaultFPRTest, check_test_set_default_no_identity) {
  115. pEp_identity* carol = NULL;
  116. PEP_STATUS status = set_up_preset(session, CAROL,
  117. true, true, false, false, false, &carol);
  118. status = update_identity(session, carol);
  119. ASSERT_OK;
  120. ASSERT_EQ(carol->fpr, nullptr);
  121. status = set_default_identity_fpr(session, carol->user_id, carol->address, carol_fpr);
  122. ASSERT_OK;
  123. status = update_identity(session, carol);
  124. ASSERT_OK;
  125. ASSERT_STREQ(carol->fpr, carol_fpr);
  126. pEp_identity* carol_bob = new_identity(carol->address, NULL, "BOB", "Carol is Bob, but not really");
  127. status = set_default_identity_fpr(session, carol_bob->user_id, carol_bob->address, carol_fpr);
  128. ASSERT_OK;
  129. status = update_identity(session, carol_bob);
  130. ASSERT_OK;
  131. ASSERT_EQ(carol_bob->fpr, nullptr);
  132. status = set_default_identity_fpr(session, carol_bob->user_id, carol_bob->address, carol_fpr);
  133. ASSERT_OK;
  134. status = update_identity(session, carol_bob);
  135. ASSERT_OK;
  136. ASSERT_STREQ(carol_bob->fpr, carol_fpr);
  137. free_identity(carol);
  138. free_identity(carol_bob);
  139. }
  140. TEST_F(TestSetDefaultFPRTest, check_test_set_comm_partner_key_no_set_identity) {
  141. pEp_identity* carol = NULL;
  142. PEP_STATUS status = set_up_preset(session, CAROL,
  143. false, false, false, false, false, &carol);
  144. string user_id_cache = carol->user_id;
  145. status = set_comm_partner_key(session, carol, carol_fpr);
  146. ASSERT_OK;
  147. status = update_identity(session, carol);
  148. ASSERT_OK;
  149. ASSERT_STREQ(carol->fpr, carol_fpr);
  150. ASSERT_STREQ(user_id_cache.c_str(), carol->user_id);
  151. status = set_comm_partner_key(session, carol, bob_fpr);
  152. ASSERT_OK;
  153. status = update_identity(session, carol);
  154. ASSERT_OK;
  155. ASSERT_STREQ(carol->fpr, bob_fpr);
  156. // Now let's set one that we don't have.
  157. status = set_comm_partner_key(session, carol, alice_fpr);
  158. ASSERT_OK;
  159. status = update_identity(session, carol);
  160. ASSERT_OK;
  161. ASSERT_STREQ(carol->fpr, carol_fpr); // Should be the user default, we don't have alice_fpr's key
  162. ASSERT_EQ(carol->comm_type, PEP_ct_OpenPGP_unconfirmed);
  163. pEp_identity* carol_bob = new_identity(carol->address, NULL, "BOB", "Carol is Bob, but not really");
  164. status = set_comm_partner_key(session, carol_bob, carol_fpr);
  165. ASSERT_OK;
  166. status = update_identity(session, carol_bob);
  167. ASSERT_OK;
  168. ASSERT_STREQ(carol_bob->fpr, carol_fpr); // differs from above case because of internal update_identity call
  169. free_identity(carol);
  170. free_identity(carol_bob);
  171. }