test/src/EngineTestSuite.cc
author Krista Bennett <krista@pep-project.org>
Tue, 14 Aug 2018 08:55:46 +0200
branchENGINE-427
changeset 2821 7fe5b66e570d
parent 2795 4bd160717c54
parent 2803 fadf1e8e22ff
child 2823 f56e57460c61
child 2822 a33bdd73952a
permissions -rw-r--r--
Fixed problems resulting from .gnupg pathname fix
     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 + "/.gnupg") != 0); // This is an EXCLUSION test, so we leave this.
   123     assert(temp_test_home.compare(home + ".gnupg") != 0);
   124     assert(temp_test_home.compare(home + "/gnupg") != 0);
   125     assert(temp_test_home.compare(home + "gnupg") != 0);
   126     assert(temp_test_home.compare(prev_gpg_home) != 0);
   127     assert(temp_test_home.compare(prev_gpg_home + "/gnupg") != 0);
   128     assert(temp_test_home.compare(prev_gpg_home + "gnupg") != 0);
   129     assert(temp_test_home.compare(prev_gpg_home + "/.gnupg") != 0);
   130     assert(temp_test_home.compare(prev_gpg_home + ".gnupg") != 0);
   131 
   132     if (temp_test_home.compare(home) == 0 || temp_test_home.compare(home + "/") == 0 ||
   133         temp_test_home.compare(home + "/gnupg") == 0 || temp_test_home.compare(home + "gnupg") == 0 ||
   134         temp_test_home.compare(home + "/.gnupg") == 0 || temp_test_home.compare(home + ".gnupg") == 0 ||
   135         temp_test_home.compare(prev_gpg_home) == 0 || temp_test_home.compare(prev_gpg_home + "/gnupg") == 0 ||
   136         temp_test_home.compare(prev_gpg_home + "gnupg") == 0 || temp_test_home.compare(prev_gpg_home + "/.gnupg") == 0 ||
   137         temp_test_home.compare(prev_gpg_home + ".gnupg") == 0)
   138         throw std::runtime_error("SETUP: new GNUPGHOME threatens to mess up user GNUPGHOME (and delete all their keys). NO DICE.");
   139     
   140 //    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;
   141     
   142     struct stat buf;
   143     
   144     success = setenv("GNUPGHOME", (temp_test_home + "/gnupg").c_str(), 1);
   145     if (success != 0)
   146         throw std::runtime_error("SETUP: Error when setting GNUPGHOME.");
   147 
   148     cout << "New GNUPGHOME is " << getenv("GNUPGHOME") << endl;
   149     
   150     success = setenv("HOME", temp_test_home.c_str(), 1);
   151     if (success != 0)
   152         throw std::runtime_error("SETUP: Cannot set test_home for init.");
   153 
   154     if (gpg_conf_copy_path)
   155         copy_conf_file_to_test_dir((temp_test_home + "/gnupg").c_str(), gpg_conf_copy_path, "gpg.conf");
   156     if (gpg_agent_conf_file_copy_path)        
   157         copy_conf_file_to_test_dir((temp_test_home + "/gnupg").c_str(), gpg_agent_conf_file_copy_path, "gpg-agent.conf");
   158     if (db_conf_file_copy_path)
   159         copy_conf_file_to_test_dir(temp_test_home.c_str(), db_conf_file_copy_path, ".pEp_management.db");
   160         
   161     unix_local_db(true);
   162     gpg_conf(true);
   163     gpg_agent_conf(true);
   164     
   165 //    cout << "calling init()\n";
   166     PEP_STATUS status = init(&session);
   167     assert(status == PEP_STATUS_OK);
   168     assert(session);
   169 //    cout << "init() completed.\n";
   170 
   171 }
   172 
   173 void EngineTestSuite::restore_full_env() {
   174     release(session);
   175     session = NULL;
   176         
   177     int success = system("gpgconf --kill all");
   178     if (success != 0)
   179         throw std::runtime_error("RESTORE: Error when executing 'gpgconf --kill all'.");
   180 
   181     success = setenv("GNUPGHOME", prev_gpg_home.c_str(), 1);
   182     if (success != 0)
   183         throw std::runtime_error("RESTORE: Warning - cannot restore GNUPGHOME. Either set environment variable manually back to your home, or quit this session!");
   184                 
   185     success = nftw((test_home + "/.").c_str(), util_delete_filepath, 100, FTW_DEPTH);
   186     
   187     success = setenv("HOME", real_home.c_str(), 1);
   188     if (success != 0)
   189         throw std::runtime_error("RESTORE: Cannot reset home directory! Either set environment variable manually back to your home, or quit this session!");
   190     else
   191         cout << "RESTORE: HOME is now " << getenv("HOME") << endl;
   192     unix_local_db(true);
   193     gpg_conf(true);
   194     gpg_agent_conf(true);
   195 
   196 }
   197 
   198 void EngineTestSuite::setup() {
   199     on_test_number++;
   200 }
   201 
   202 void EngineTestSuite::tear_down() {}
   203 
   204 void EngineTestSuite::set_my_name() {
   205     my_name = typeid(*this).name();
   206 }
   207