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.
pEpEngine/test/src/Engine.cc

174 lines
5.5 KiB

4 years ago
#include <stdlib.h>
#include <sys/stat.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <ftw.h>
#include <assert.h>
#include <fstream>
#include <iostream>
#include <sys/types.h>
#include <sys/stat.h>
#include <string>
#include <vector>
#include <utility>
#include "platform_unix.h"
#include "TestUtilities.h"
4 years ago
#include "Engine.h"
#include "pEpTestStatic.h"
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.
2 years ago
#include "pEpEngine_internal.h"
4 years ago
#include <algorithm>
#include "TestConstants.h"
using namespace std;
// Constructor
Engine::Engine(string engine_home_dir) {
// FIXME: deal with base
engine_home = engine_home_dir;
real_home = getenv("HOME");
cached_messageToSend = NULL;
cached_inject_sync_event = NULL;
cached_ensure_passphrase = NULL;
4 years ago
}
Engine::~Engine() {}
void Engine::prep(messageToSend_t mts, inject_sync_event_t ise, ensure_passphrase_t ep,
4 years ago
std::vector<std::pair<std::string, std::string>> init_files) {
4 years ago
if (engine_home.empty())
4 years ago
throw std::runtime_error("Engine setup: BAD INITIALISATION. No test home.");
cached_messageToSend = mts;
cached_inject_sync_event = ise;
cached_ensure_passphrase = ep;
4 years ago
int success = 0;
struct stat dirchk;
if (stat(engine_home.c_str(), &dirchk) == 0) {
if (!S_ISDIR(dirchk.st_mode))
throw std::runtime_error(("ENGINE SETUP: The test directory, " + engine_home + " exists, but is not a directory.").c_str());
struct stat buf;
if (stat(engine_home.c_str(), &buf) == 0) {
int success = nftw((engine_home + "/.").c_str(), util_delete_filepath, 100, FTW_DEPTH);
}
}
else {
4 years ago
// Look, we're not creating all of these dirs...
const int errchk = system((string("mkdir -p ") + engine_home).c_str());
4 years ago
if (errchk != 0)
4 years ago
throw std::runtime_error("ENGINE SETUP: Error creating a test directory.");
4 years ago
}
process_file_queue(engine_home, init_files);
// We will set homedirs etc outside this function. Right now, we're just making sure we can.
// Let's make sure we're not trying to run it under the real current home, however.
4 years ago
if (engine_home.compare(real_home) == 0 || engine_home.compare(real_home + "/") == 0)
4 years ago
throw std::runtime_error("ENGINE SETUP: Cowardly refusing to set up for playing in what looks like the real home directory.");
}
void Engine::start() {
if (engine_home.empty())
throw std::runtime_error("Engine start: BAD INITIALISATION. No test home. Did you call Engine::prep() first?");
assert(engine_home.compare(real_home) != 0);
assert(engine_home.compare(real_home + "/") != 0);
output_stream << "Test home directory is " << engine_home << endl;
4 years ago
int success = 0;
4 years ago
success = setenv("HOME", engine_home.c_str(), 1);
if (success != 0)
throw std::runtime_error("SETUP: Cannot set engine_home for init.");
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.
2 years ago
PEP_STATUS status;
status = reset_path_cache();
assert(status == PEP_STATUS_OK);
FILE *f = fopen ("/home/luca/pep-src/pep-engine/PER_USER_DIRECTORY", "w");
fprintf (f, "* per_user_directory (): %s\n", per_user_directory ());
fclose (f);
f = fopen ("/home/luca/pep-src/pep-engine/PER_MACHINE_DIRECTORY", "w");
fprintf (f, "* per_machine_directory (): %s\n", per_machine_directory ());
fclose (f);
fprintf (stderr, "* per_user_directory (): %s\n", per_user_directory ());
fprintf (stderr, "* per_user_directory (): %s\n", per_user_directory ());
status = init(&session, cached_messageToSend, cached_inject_sync_event, cached_ensure_passphrase);
4 years ago
assert(status == PEP_STATUS_OK);
assert(session);
}
void Engine::copy_conf_file_to_test_dir(const char* dest_path, const char* conf_orig_path, const char* conf_dest_name) {
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.
2 years ago
string conf_dest_path
= (string(dest_path)
+ string("/")
+ string(per_user_relative_directory ())
+ string("/"));
fprintf (stderr, "COPYING %s from %s to %s\n",
dest_path,
conf_orig_path,
conf_dest_path.c_str ());
4 years ago
struct stat pathinfo;
if(stat(conf_dest_path.c_str(), &pathinfo) != 0) {
int errchk = mkdir(conf_dest_path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
if (errchk != 0)
throw std::runtime_error("Error accessing conf file directory.");
}
conf_dest_path += "/";
conf_dest_path += conf_dest_name;
ifstream src(conf_orig_path);
ofstream dst(conf_dest_path.c_str(), ios::trunc);
assert(src);
assert(dst);
dst << src.rdbuf();
src.close();
dst.close();
}
void Engine::process_file_queue(string dirname, vector<pair<string, string>> file_queue) {
if (file_queue.empty())
return;
vector<pair<string, string>>::iterator it;
for (it = file_queue.begin(); it != file_queue.end(); it++) {
copy_conf_file_to_test_dir(dirname.c_str(), it->first.c_str(), it->second.c_str());
}
file_queue.clear();
}
void Engine::shut_down() {
release(session);
session = NULL;
int success = 0;
success = nftw((engine_home + "/.").c_str(), util_delete_filepath, 100, FTW_DEPTH);
success = setenv("HOME", real_home.c_str(), 1);
if (success != 0)
throw std::runtime_error("RESTORE: Cannot reset home directory! Either set environment variable manually back to your home, or quit this session!");
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.
2 years ago
PEP_STATUS status = reset_path_cache();
assert(status == PEP_STATUS_OK);
4 years ago
}