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.

173 lines
5.5 KiB

fix ENGINE-956 It is now possible to use shell environment variables in PER_USER_DIRECTORY and PER_MACHINE_DIRECTORY, expanded at *run* time. This is meant to allow more freedom to the deployment engineer, without affecting development. Notice that, when PER_USER_DIRECTORY and PER_MACHINE_DIRECTORY are defined in the Makefile, dollar signs must be escaped (a dollar becomes a double dollar) because of *make* syntax: this has nothing to do with pEp engine code. Expansion follows the Unix shell $VARIABLE syntax: ${VARIABLE} is not supported. See _expand_variables. src/platform_unix.c contained some duplicated logic about caching path results into static variables; this change set would have added to the complexity by calling the new path expansion function, in many different places. Seize the occasion for factoring. Do not change the meaning of PER_USER_DIRECTORY yet; I would like to do it, since it is confusing and inconsistent with per_user_directory (PER_USER_DIRECTORY is a relative path on Unix, while per_user_directory is absolute), but the actual semantics with respect to $HOME and $PEP_HOME is complicated and I do not want to break it. New API function per_user_relative_directory. Remove the ugly "reset" argument from unix_local_db (which was conditionally defined according to NDEBUG!), used to force path re-computation in the test suite after changing an environment variable so as to work in a new "home" directory. In the place of this reset argument add clear API functions to the engine for handling the cache. My quick grapping and IRC interaction confirm that nobody was using the functionality out of the engine test suite. Adapt the test suite to use the new API. Tentatively add caching and expansion functionality to android_system_db as well.
9 months ago
3 years ago
3 years ago
3 years ago
fix ENGINE-956 It is now possible to use shell environment variables in PER_USER_DIRECTORY and PER_MACHINE_DIRECTORY, expanded at *run* time. This is meant to allow more freedom to the deployment engineer, without affecting development. Notice that, when PER_USER_DIRECTORY and PER_MACHINE_DIRECTORY are defined in the Makefile, dollar signs must be escaped (a dollar becomes a double dollar) because of *make* syntax: this has nothing to do with pEp engine code. Expansion follows the Unix shell $VARIABLE syntax: ${VARIABLE} is not supported. See _expand_variables. src/platform_unix.c contained some duplicated logic about caching path results into static variables; this change set would have added to the complexity by calling the new path expansion function, in many different places. Seize the occasion for factoring. Do not change the meaning of PER_USER_DIRECTORY yet; I would like to do it, since it is confusing and inconsistent with per_user_directory (PER_USER_DIRECTORY is a relative path on Unix, while per_user_directory is absolute), but the actual semantics with respect to $HOME and $PEP_HOME is complicated and I do not want to break it. New API function per_user_relative_directory. Remove the ugly "reset" argument from unix_local_db (which was conditionally defined according to NDEBUG!), used to force path re-computation in the test suite after changing an environment variable so as to work in a new "home" directory. In the place of this reset argument add clear API functions to the engine for handling the cache. My quick grapping and IRC interaction confirm that nobody was using the functionality out of the engine test suite. Adapt the test suite to use the new API. Tentatively add caching and expansion functionality to android_system_db as well.
9 months ago
fix ENGINE-956 It is now possible to use shell environment variables in PER_USER_DIRECTORY and PER_MACHINE_DIRECTORY, expanded at *run* time. This is meant to allow more freedom to the deployment engineer, without affecting development. Notice that, when PER_USER_DIRECTORY and PER_MACHINE_DIRECTORY are defined in the Makefile, dollar signs must be escaped (a dollar becomes a double dollar) because of *make* syntax: this has nothing to do with pEp engine code. Expansion follows the Unix shell $VARIABLE syntax: ${VARIABLE} is not supported. See _expand_variables. src/platform_unix.c contained some duplicated logic about caching path results into static variables; this change set would have added to the complexity by calling the new path expansion function, in many different places. Seize the occasion for factoring. Do not change the meaning of PER_USER_DIRECTORY yet; I would like to do it, since it is confusing and inconsistent with per_user_directory (PER_USER_DIRECTORY is a relative path on Unix, while per_user_directory is absolute), but the actual semantics with respect to $HOME and $PEP_HOME is complicated and I do not want to break it. New API function per_user_relative_directory. Remove the ugly "reset" argument from unix_local_db (which was conditionally defined according to NDEBUG!), used to force path re-computation in the test suite after changing an environment variable so as to work in a new "home" directory. In the place of this reset argument add clear API functions to the engine for handling the cache. My quick grapping and IRC interaction confirm that nobody was using the functionality out of the engine test suite. Adapt the test suite to use the new API. Tentatively add caching and expansion functionality to android_system_db as well.
9 months ago
fix ENGINE-956 It is now possible to use shell environment variables in PER_USER_DIRECTORY and PER_MACHINE_DIRECTORY, expanded at *run* time. This is meant to allow more freedom to the deployment engineer, without affecting development. Notice that, when PER_USER_DIRECTORY and PER_MACHINE_DIRECTORY are defined in the Makefile, dollar signs must be escaped (a dollar becomes a double dollar) because of *make* syntax: this has nothing to do with pEp engine code. Expansion follows the Unix shell $VARIABLE syntax: ${VARIABLE} is not supported. See _expand_variables. src/platform_unix.c contained some duplicated logic about caching path results into static variables; this change set would have added to the complexity by calling the new path expansion function, in many different places. Seize the occasion for factoring. Do not change the meaning of PER_USER_DIRECTORY yet; I would like to do it, since it is confusing and inconsistent with per_user_directory (PER_USER_DIRECTORY is a relative path on Unix, while per_user_directory is absolute), but the actual semantics with respect to $HOME and $PEP_HOME is complicated and I do not want to break it. New API function per_user_relative_directory. Remove the ugly "reset" argument from unix_local_db (which was conditionally defined according to NDEBUG!), used to force path re-computation in the test suite after changing an environment variable so as to work in a new "home" directory. In the place of this reset argument add clear API functions to the engine for handling the cache. My quick grapping and IRC interaction confirm that nobody was using the functionality out of the engine test suite. Adapt the test suite to use the new API. Tentatively add caching and expansion functionality to android_system_db as well.
9 months ago
  1. #include <stdlib.h>
  2. #include <sys/stat.h>
  3. #include <errno.h>
  4. #include <stdlib.h>
  5. #include <unistd.h>
  6. #include <ftw.h>
  7. #include <assert.h>
  8. #include <fstream>
  9. #include <iostream>
  10. #include <sys/types.h>
  11. #include <sys/stat.h>
  12. #include <string>
  13. #include <vector>
  14. #include <utility>
  15. #include "platform_unix.h"
  16. #include "TestUtilities.h"
  17. #include "Engine.h"
  18. #include "pEpTestStatic.h"
  19. #include "pEpEngine_internal.h"
  20. #include <algorithm>
  21. #include "TestConstants.h"
  22. using namespace std;
  23. // Constructor
  24. Engine::Engine(string engine_home_dir) {
  25. // FIXME: deal with base
  26. engine_home = engine_home_dir;
  27. real_home = getenv("HOME");
  28. cached_messageToSend = NULL;
  29. cached_inject_sync_event = NULL;
  30. cached_ensure_passphrase = NULL;
  31. }
  32. Engine::~Engine() {}
  33. void Engine::prep(messageToSend_t mts, inject_sync_event_t ise, ensure_passphrase_t ep,
  34. std::vector<std::pair<std::string, std::string>> init_files) {
  35. if (engine_home.empty())
  36. throw std::runtime_error("Engine setup: BAD INITIALISATION. No test home.");
  37. cached_messageToSend = mts;
  38. cached_inject_sync_event = ise;
  39. cached_ensure_passphrase = ep;
  40. int success = 0;
  41. struct stat dirchk;
  42. if (stat(engine_home.c_str(), &dirchk) == 0) {
  43. if (!S_ISDIR(dirchk.st_mode))
  44. throw std::runtime_error(("ENGINE SETUP: The test directory, " + engine_home + " exists, but is not a directory.").c_str());
  45. struct stat buf;
  46. if (stat(engine_home.c_str(), &buf) == 0) {
  47. int success = nftw((engine_home + "/.").c_str(), util_delete_filepath, 100, FTW_DEPTH);
  48. }
  49. }
  50. else {
  51. // Look, we're not creating all of these dirs...
  52. const int errchk = system((string("mkdir -p ") + engine_home).c_str());
  53. if (errchk != 0)
  54. throw std::runtime_error("ENGINE SETUP: Error creating a test directory.");
  55. }
  56. process_file_queue(engine_home, init_files);
  57. // We will set homedirs etc outside this function. Right now, we're just making sure we can.
  58. // Let's make sure we're not trying to run it under the real current home, however.
  59. if (engine_home.compare(real_home) == 0 || engine_home.compare(real_home + "/") == 0)
  60. throw std::runtime_error("ENGINE SETUP: Cowardly refusing to set up for playing in what looks like the real home directory.");
  61. }
  62. void Engine::start() {
  63. if (engine_home.empty())
  64. throw std::runtime_error("Engine start: BAD INITIALISATION. No test home. Did you call Engine::prep() first?");
  65. assert(engine_home.compare(real_home) != 0);
  66. assert(engine_home.compare(real_home + "/") != 0);
  67. output_stream << "Test home directory is " << engine_home << endl;
  68. int success = 0;
  69. success = setenv("HOME", engine_home.c_str(), 1);
  70. if (success != 0)
  71. throw std::runtime_error("SETUP: Cannot set engine_home for init.");
  72. PEP_STATUS status;
  73. status = reset_path_cache();
  74. assert(status == PEP_STATUS_OK);
  75. FILE *f = fopen ("/home/luca/pep-src/pep-engine/PER_USER_DIRECTORY", "w");
  76. fprintf (f, "* per_user_directory (): %s\n", per_user_directory ());
  77. fclose (f);
  78. f = fopen ("/home/luca/pep-src/pep-engine/PER_MACHINE_DIRECTORY", "w");
  79. fprintf (f, "* per_machine_directory (): %s\n", per_machine_directory ());
  80. fclose (f);
  81. fprintf (stderr, "* per_user_directory (): %s\n", per_user_directory ());
  82. fprintf (stderr, "* per_user_directory (): %s\n", per_user_directory ());
  83. status = init(&session, cached_messageToSend, cached_inject_sync_event, cached_ensure_passphrase);
  84. assert(status == PEP_STATUS_OK);
  85. assert(session);
  86. }
  87. void Engine::copy_conf_file_to_test_dir(const char* dest_path, const char* conf_orig_path, const char* conf_dest_name) {
  88. string conf_dest_path
  89. = (string(dest_path)
  90. + string("/")
  91. + string(per_user_relative_directory ())
  92. + string("/"));
  93. fprintf (stderr, "COPYING %s from %s to %s\n",
  94. dest_path,
  95. conf_orig_path,
  96. conf_dest_path.c_str ());
  97. struct stat pathinfo;
  98. if(stat(conf_dest_path.c_str(), &pathinfo) != 0) {
  99. int errchk = mkdir(conf_dest_path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
  100. if (errchk != 0)
  101. throw std::runtime_error("Error accessing conf file directory.");
  102. }
  103. conf_dest_path += "/";
  104. conf_dest_path += conf_dest_name;
  105. ifstream src(conf_orig_path);
  106. ofstream dst(conf_dest_path.c_str(), ios::trunc);
  107. assert(src);
  108. assert(dst);
  109. dst << src.rdbuf();
  110. src.close();
  111. dst.close();
  112. }
  113. void Engine::process_file_queue(string dirname, vector<pair<string, string>> file_queue) {
  114. if (file_queue.empty())
  115. return;
  116. vector<pair<string, string>>::iterator it;
  117. for (it = file_queue.begin(); it != file_queue.end(); it++) {
  118. copy_conf_file_to_test_dir(dirname.c_str(), it->first.c_str(), it->second.c_str());
  119. }
  120. file_queue.clear();
  121. }
  122. void Engine::shut_down() {
  123. release(session);
  124. session = NULL;
  125. int success = 0;
  126. success = nftw((engine_home + "/.").c_str(), util_delete_filepath, 100, FTW_DEPTH);
  127. success = setenv("HOME", real_home.c_str(), 1);
  128. if (success != 0)
  129. throw std::runtime_error("RESTORE: Cannot reset home directory! Either set environment variable manually back to your home, or quit this session!");
  130. PEP_STATUS status = reset_path_cache();
  131. assert(status == PEP_STATUS_OK);
  132. }