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.

168 lines
6.0 KiB

  1. // This file is under GNU General Public License 3.0
  2. // see LICENSE.txt
  3. #include <stdlib.h>
  4. #include "TestConstants.h"
  5. #include <string>
  6. #include <iostream>
  7. #include <vector>
  8. #include <cstring> // for strcmp()
  9. #include "keymanagement.h"
  10. #include "message_api.h"
  11. #include "mime.h"
  12. #include "test_util.h"
  13. #include "pEpEngine.h"
  14. #include "Engine.h"
  15. #include <gtest/gtest.h>
  16. namespace {
  17. //The fixture for LeastColorGroupTest
  18. class LeastColorGroupTest : public ::testing::Test {
  19. public:
  20. Engine* engine;
  21. PEP_SESSION session;
  22. protected:
  23. // You can remove any or all of the following functions if its body
  24. // is empty.
  25. LeastColorGroupTest() {
  26. // You can do set-up work for each test here.
  27. test_suite_name = ::testing::UnitTest::GetInstance()->current_test_info()->GTEST_SUITE_SYM();
  28. test_name = ::testing::UnitTest::GetInstance()->current_test_info()->name();
  29. test_path = get_main_test_home_dir() + "/" + test_suite_name + "/" + test_name;
  30. }
  31. ~LeastColorGroupTest() override {
  32. // You can do clean-up work that doesn't throw exceptions here.
  33. }
  34. // If the constructor and destructor are not enough for setting up
  35. // and cleaning up each test, you can define the following methods:
  36. void SetUp() override {
  37. // Code here will be called immediately after the constructor (right
  38. // before each test).
  39. // Leave this empty if there are no files to copy to the home directory path
  40. std::vector<std::pair<std::string, std::string>> init_files = std::vector<std::pair<std::string, std::string>>();
  41. // Get a new test Engine.
  42. engine = new Engine(test_path);
  43. ASSERT_NOTNULL(engine);
  44. // Ok, let's initialize test directories etc.
  45. engine->prep(NULL, NULL, NULL, init_files);
  46. // Ok, try to start this bugger.
  47. engine->start();
  48. ASSERT_NOTNULL(engine->session);
  49. session = engine->session;
  50. // Engine is up. Keep on truckin'
  51. }
  52. void TearDown() override {
  53. // Code here will be called immediately after each test (right
  54. // before the destructor).
  55. engine->shut_down();
  56. delete engine;
  57. engine = NULL;
  58. session = NULL;
  59. }
  60. private:
  61. const char* test_suite_name;
  62. const char* test_name;
  63. string test_path;
  64. // Objects declared here can be used by all tests in the LeastColorGroupTest suite.
  65. };
  66. } // namespace
  67. TEST_F(LeastColorGroupTest, check_least_color_group) {
  68. const char* mailfile = "test_mails/color_test.eml";
  69. const std::vector<const char*> keynames = {
  70. "test_keys/priv/pep.color.test.P-0x3EBE215C_priv.asc",
  71. "test_keys/pub/pep.color.test.H-0xD17E598E_pub.asc",
  72. "test_keys/pub/pep.color.test.L-0xE9CDB4CE_pub.asc",
  73. "test_keys/pub/pep.color.test.P-0x3EBE215C_pub.asc",
  74. "test_keys/pub/pep.color.test.V-0x71FC6D28_pub.asc"
  75. };
  76. for (auto name : keynames) {
  77. output_stream << "\t read keyfile \"" << name << "\"..." << std::endl;
  78. const string keytextkey = slurp(name);
  79. PEP_STATUS statuskey = import_key(session, keytextkey.c_str(), keytextkey.length(), NULL);
  80. ASSERT_EQ(statuskey , PEP_TEST_KEY_IMPORT_SUCCESS);
  81. }
  82. output_stream << "\t read keyfile mailfile \"" << mailfile << "\"..." << std::endl;
  83. const string mailtext = slurp(mailfile);
  84. output_stream << "\t All files read successfully." << std::endl;
  85. pEp_identity * me1 = new_identity("pep.color.test.P@kgrothoff.org",
  86. "7EE6C60C68851954E1797F81EA59715E3EBE215C",
  87. PEP_OWN_USERID, "Pep Color Test P (recip)");
  88. me1->me = true;
  89. PEP_STATUS status = myself(session, me1);
  90. pEp_identity * sender1 = new_identity("pep.color.test.V@kgrothoff.org",
  91. NULL, "TOFU_pep.color.test.V@kgrothoff.org",
  92. "Pep Color Test V (sender)");
  93. status = set_fpr_preserve_ident(session, sender1, "AFC019B22E2CC61F13F285BF179B9DF271FC6D28", false);
  94. ASSERT_OK;
  95. status = update_identity(session, sender1);
  96. ASSERT_OK;
  97. status = trust_personal_key(session, sender1);
  98. ASSERT_OK;
  99. status = update_identity(session, sender1);
  100. ASSERT_OK;
  101. message* msg_ptr = nullptr;
  102. message* dest_msg = nullptr;
  103. message* final_ptr = nullptr;
  104. stringlist_t* keylist = nullptr;
  105. PEP_rating rating;
  106. PEP_decrypt_flags_t flags;
  107. status = mime_decode_message(mailtext.c_str(), mailtext.length(), &msg_ptr, NULL);
  108. ASSERT_OK;
  109. ASSERT_NOTNULL(msg_ptr);
  110. final_ptr = msg_ptr;
  111. flags = 0;
  112. status = decrypt_message(session, msg_ptr, &dest_msg, &keylist, &rating, &flags);
  113. final_ptr = dest_msg ? dest_msg : msg_ptr;
  114. output_stream << "shortmsg: " << final_ptr->shortmsg << endl << endl;
  115. output_stream << "longmsg: " << final_ptr->longmsg << endl << endl;
  116. output_stream << "longmsg_formatted: " << (final_ptr->longmsg_formatted ? final_ptr->longmsg_formatted : "(empty)") << endl << endl;
  117. output_stream << "rating: " << rating << endl << endl;
  118. output_stream << "keys used: " << endl;
  119. int i = 0;
  120. for (stringlist_t* k = keylist; k; k = k->next) {
  121. if (i == 0)
  122. output_stream << "\t Signer (key 0):\t" << k->value << endl;
  123. else
  124. output_stream << "\t #" << i << ":\t" << k->value << endl;
  125. i++;
  126. }
  127. // free_identity(me1);
  128. if (final_ptr == dest_msg)
  129. free_message(dest_msg);
  130. free_message(msg_ptr);
  131. free_stringlist(keylist);
  132. }