test/src/EngineTestSuite.cc
author Krista Bennett <krista@pep-project.org>
Tue, 31 Jul 2018 21:39:01 +0200
branchENGINE-450-MARK-II
changeset 2795 4bd160717c54
parent 2789 038c70f9f5ce
child 2798 e0abb44b8098
child 2821 7fe5b66e570d
permissions -rw-r--r--
ENGINE-450: Fixes for 450 reintegrated, and fixed a mem bug that doesn't happen when you have a ton of keys, so it got missed before.
     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 
    13 #include "platform_unix.h"
    14 
    15 #include "test_util.h"
    16 #include "EngineTestSuite.h"
    17 using namespace std;
    18 
    19 // Constructor
    20 EngineTestSuite::EngineTestSuite(string suitename, string test_home_dir) {
    21     // FIXME: deal with base
    22     test_home = test_home_dir;
    23             
    24     number_of_tests = 0;
    25     on_test_number = 0;
    26     real_home = getenv("HOME");
    27 }
    28 
    29 EngineTestSuite::~EngineTestSuite() {}
    30 
    31 void EngineTestSuite::add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()> test_func) {
    32     test_map.insert(test_func);
    33     register_test(test_func.second, test_func.first);
    34     number_of_tests++;
    35 }
    36 
    37 void EngineTestSuite::copy_conf_file_to_test_dir(const char* dest_path, const char* conf_orig_path, const char* conf_dest_name) {
    38     string conf_dest_path = dest_path;
    39     
    40     struct stat pathinfo;
    41 
    42     if(stat(conf_dest_path.c_str(), &pathinfo) != 0) {
    43         int errchk = mkdir(conf_dest_path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
    44         if (errchk != 0)
    45             throw std::runtime_error("Error accessing conf file directory.");
    46     }
    47     
    48     conf_dest_path += "/";
    49     conf_dest_path += conf_dest_name;
    50     
    51     ifstream src(conf_orig_path);
    52     ofstream dst(conf_dest_path.c_str(), ios::trunc);
    53     
    54     assert(src);
    55     assert(dst);
    56     
    57     dst << src.rdbuf();
    58      
    59     src.close();
    60     dst.close();
    61 }
    62 
    63 void EngineTestSuite::set_full_env() {
    64     set_full_env(NULL, NULL, NULL);
    65 }
    66 
    67 void EngineTestSuite::set_full_env(const char* gpg_conf_copy_path, const char* gpg_agent_conf_file_copy_path, const char* db_conf_file_copy_path) {
    68     int success = 0;
    69     struct stat dirchk;
    70     
    71     set_my_name();
    72 
    73     success = system("gpgconf --kill all");
    74     if (success != 0)
    75         throw std::runtime_error("SETUP: Error when executing 'gpgconf --kill all'.");
    76  //   sleep(1); // hopefully enough time for the system to recognise that it is dead. *sigh*    
    77 
    78     if (stat(test_home.c_str(), &dirchk) == 0) {
    79         if (!S_ISDIR(dirchk.st_mode))
    80             throw std::runtime_error(("The test directory, " + test_home + "exists, but is not a directory.").c_str()); 
    81                     
    82         struct stat buf;
    83 
    84         if (stat(test_home.c_str(), &buf) == 0) {
    85             cout << test_home << " exists. We'll recursively delete. We hope we're not horking your whole system..." << endl;
    86             int success = nftw((test_home + "/.").c_str(), util_delete_filepath, 100, FTW_DEPTH);
    87         }
    88     }
    89     else {
    90         int errchk = mkdir(test_home.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
    91         cout << errchk << endl;
    92         if (errchk != 0)
    93             throw std::runtime_error("Error creating a test directory.");
    94     }
    95 
    96     temp_test_home = test_home + "/" + my_name;
    97     
    98     int errchk = mkdir(temp_test_home.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
    99     if (errchk != 0)
   100         throw std::runtime_error("Error creating a test directory.");
   101 
   102     temp_test_home += "/" + to_string(on_test_number);
   103 
   104     errchk = mkdir(temp_test_home.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
   105     if (errchk != 0)
   106         throw std::runtime_error("Error creating a test directory.");
   107 
   108     // TODO: This is *nix specific, which the current testing env is anyway,
   109     // but it needn't remain so forever and always
   110     char* tmp = getenv("GNUPGHOME");
   111     if (tmp)
   112         prev_gpg_home = tmp;
   113         
   114     if (temp_test_home.empty())
   115         throw std::runtime_error("SETUP: BAD INITIALISATION. No test home.");
   116 
   117     
   118     string home = getenv("HOME");
   119     cout << "home is " << home << endl;
   120     assert(temp_test_home.compare(home) != 0);
   121     assert(temp_test_home.compare(home + "/") != 0);
   122     assert(temp_test_home.compare(home + "") != 0);
   123     assert(temp_test_home.compare(home + ".gnupg") != 0);
   124     assert(temp_test_home.compare(prev_gpg_home) != 0);
   125     assert(temp_test_home.compare(prev_gpg_home + "/.gnupg") != 0);
   126     assert(temp_test_home.compare(prev_gpg_home + ".gnupg") != 0);
   127 
   128     if (temp_test_home.compare(home) == 0 || temp_test_home.compare(home + "/") == 0 ||
   129         temp_test_home.compare(home + "/.gnupg") == 0 || temp_test_home.compare(home + ".gnupg") == 0 ||
   130         temp_test_home.compare(prev_gpg_home) == 0 || temp_test_home.compare(prev_gpg_home + "/.gnupg") == 0 ||
   131         temp_test_home.compare(prev_gpg_home + ".gnupg") == 0)
   132         throw std::runtime_error("SETUP: new GNUPGHOME threatens to mess up user GNUPGHOME (and delete all their keys). NO DICE.");
   133     
   134 //    cout << "Ok - checked if new test home will be safe. We'll try and make the directory, deleting it if it has already exists." << endl;
   135     
   136     struct stat buf;
   137     
   138     success = setenv("GNUPGHOME", (temp_test_home + "/.gnupg").c_str(), 1);
   139     if (success != 0)
   140         throw std::runtime_error("SETUP: Error when setting GNUPGHOME.");
   141 
   142     cout << "New GNUPGHOME is " << getenv("GNUPGHOME") << endl;
   143     
   144     success = setenv("HOME", temp_test_home.c_str(), 1);
   145     if (success != 0)
   146         throw std::runtime_error("SETUP: Cannot set test_home for init.");
   147 
   148     if (gpg_conf_copy_path)
   149         copy_conf_file_to_test_dir((temp_test_home + "/.gnupg").c_str(), gpg_conf_copy_path, "gpg.conf");
   150     if (gpg_agent_conf_file_copy_path)        
   151         copy_conf_file_to_test_dir((temp_test_home + "/.gnupg").c_str(), gpg_agent_conf_file_copy_path, "gpg-agent.conf");
   152     if (db_conf_file_copy_path)
   153         copy_conf_file_to_test_dir(temp_test_home.c_str(), db_conf_file_copy_path, ".pEp_management.db");
   154         
   155     unix_local_db(true);
   156     gpg_conf(true);
   157     gpg_agent_conf(true);
   158     
   159 //    cout << "calling init()\n";
   160     PEP_STATUS status = init(&session);
   161     assert(status == PEP_STATUS_OK);
   162     assert(session);
   163 //    cout << "init() completed.\n";
   164 
   165 }
   166 
   167 void EngineTestSuite::restore_full_env() {
   168     release(session);
   169     session = NULL;
   170         
   171     int success = system("gpgconf --kill all");
   172     if (success != 0)
   173         throw std::runtime_error("RESTORE: Error when executing 'gpgconf --kill all'.");
   174 
   175     success = setenv("GNUPGHOME", prev_gpg_home.c_str(), 1);
   176     if (success != 0)
   177         throw std::runtime_error("RESTORE: Warning - cannot restore GNUPGHOME. Either set environment variable manually back to your home, or quit this session!");
   178                 
   179     success = nftw((test_home + "/.").c_str(), util_delete_filepath, 100, FTW_DEPTH);
   180     
   181     success = setenv("HOME", real_home.c_str(), 1);
   182     if (success != 0)
   183         throw std::runtime_error("RESTORE: Cannot reset home directory! Either set environment variable manually back to your home, or quit this session!");
   184     else
   185         cout << "RESTORE: HOME is now " << getenv("HOME") << endl;
   186     unix_local_db(true);
   187     gpg_conf(true);
   188     gpg_agent_conf(true);
   189 
   190 }
   191 
   192 void EngineTestSuite::setup() {
   193     on_test_number++;
   194 }
   195 
   196 void EngineTestSuite::tear_down() {}
   197 
   198 void EngineTestSuite::set_my_name() {
   199     my_name = typeid(*this).name();
   200 }