test/src/EngineTestSuite.cc
author Krista Bennett <krista@pep-project.org>
Mon, 30 Jul 2018 19:28:52 +0200
branchENGINE-450
changeset 2787 c25afa6f22e3
parent 2782 5be6494a85ae
child 2789 038c70f9f5ce
child 2803 fadf1e8e22ff
permissions -rw-r--r--
Stripped ENGINE-427; agreed with Claudio on testing protocol.
     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 
     9 #include "platform_unix.h"
    10 
    11 #include "test_util.h"
    12 #include "EngineTestSuite.h"
    13 using namespace std;
    14 
    15 // Constructor
    16 EngineTestSuite::EngineTestSuite(string suitename, string test_home_dir) {
    17     // FIXME: deal with base
    18     test_home = test_home_dir;
    19             
    20     number_of_tests = 0;
    21     on_test_number = 0;
    22     real_home = getenv("HOME");
    23 }
    24 
    25 EngineTestSuite::~EngineTestSuite() {}
    26 
    27 void EngineTestSuite::add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()> test_func) {
    28     test_map.insert(test_func);
    29     register_test(test_func.second, test_func.first);
    30     number_of_tests++;
    31 }
    32 
    33 void EngineTestSuite::set_full_env() {
    34     int success = 0;
    35     struct stat dirchk;
    36     
    37     set_my_name();
    38 
    39     success = system("gpgconf --kill all");
    40     if (success != 0)
    41         throw std::runtime_error("SETUP: Error when executing 'gpgconf --kill all'.");
    42     sleep(1); // hopefully enough time for the system to recognise that it is dead. *sigh*    
    43 
    44     if (stat(test_home.c_str(), &dirchk) == 0) {
    45         if (!S_ISDIR(dirchk.st_mode))
    46             throw std::runtime_error(("The test directory, " + test_home + "exists, but is not a directory.").c_str()); 
    47                     
    48         struct stat buf;
    49 
    50         if (stat(test_home.c_str(), &buf) == 0) {
    51             cout << test_home << " exists. We'll recursively delete. We hope we're not horking your whole system..." << endl;
    52             int success = nftw((test_home + "/.").c_str(), util_delete_filepath, 100, FTW_DEPTH);
    53         }
    54     }
    55     else {
    56         int errchk = mkdir(test_home.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
    57         cout << errchk << endl;
    58         if (errchk != 0)
    59             throw std::runtime_error("Error creating a test directory.");
    60     }
    61 
    62     string temp_test_home = test_home + "/" + my_name;
    63     
    64     int errchk = mkdir(temp_test_home.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
    65     if (errchk != 0)
    66         throw std::runtime_error("Error creating a test directory.");
    67 
    68     temp_test_home += "/" + to_string(on_test_number);
    69 
    70     errchk = mkdir(temp_test_home.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
    71     if (errchk != 0)
    72         throw std::runtime_error("Error creating a test directory.");
    73 
    74     // TODO: This is *nix specific, which the current testing env is anyway,
    75     // but it needn't remain so forever and always
    76     char* tmp = getenv("GNUPGHOME");
    77     if (tmp)
    78         prev_gpg_home = tmp;
    79         
    80     if (temp_test_home.empty())
    81         throw std::runtime_error("SETUP: BAD INITIALISATION. No test home.");
    82 
    83     
    84     string home = getenv("HOME");
    85     cout << "home is " << home << endl;
    86     assert(temp_test_home.compare(home) != 0);
    87     assert(temp_test_home.compare(home + "/") != 0);
    88     assert(temp_test_home.compare(home + "/.gnupg") != 0);
    89     assert(temp_test_home.compare(home + ".gnupg") != 0);
    90     assert(temp_test_home.compare(prev_gpg_home) != 0);
    91     assert(temp_test_home.compare(prev_gpg_home + "/.gnupg") != 0);
    92     assert(temp_test_home.compare(prev_gpg_home + ".gnupg") != 0);
    93 
    94     if (temp_test_home.compare(home) == 0 || temp_test_home.compare(home + "/") == 0 ||
    95         temp_test_home.compare(home + "/.gnupg") == 0 || temp_test_home.compare(home + ".gnupg") == 0 ||
    96         temp_test_home.compare(prev_gpg_home) == 0 || temp_test_home.compare(prev_gpg_home + "/.gnupg") == 0 ||
    97         temp_test_home.compare(prev_gpg_home + ".gnupg") == 0)
    98         throw std::runtime_error("SETUP: new GNUPGHOME threatens to mess up user GNUPGHOME (and delete all their keys). NO DICE.");
    99     
   100 //    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;
   101     
   102     struct stat buf;
   103     
   104     success = setenv("GNUPGHOME", (temp_test_home + "/.gnupg").c_str(), 1);
   105     if (success != 0)
   106         throw std::runtime_error("SETUP: Error when setting GNUPGHOME.");
   107 
   108     cout << "New GNUPGHOME is " << getenv("GNUPGHOME") << endl;
   109     
   110     success = setenv("HOME", temp_test_home.c_str(), 1);
   111     if (success != 0)
   112         throw std::runtime_error("SETUP: Cannot set test_home for init.");
   113     
   114     unix_local_db(true);
   115     gpg_conf(true);
   116     gpg_agent_conf(true);
   117     
   118 //    cout << "calling init()\n";
   119     PEP_STATUS status = init(&session);
   120     assert(status == PEP_STATUS_OK);
   121     assert(session);
   122 //    cout << "init() completed.\n";
   123 
   124 }
   125 
   126 void EngineTestSuite::restore_full_env() {
   127     release(session);
   128     session = NULL;
   129         
   130     int success = system("gpgconf --kill all");
   131     if (success != 0)
   132         throw std::runtime_error("RESTORE: Error when executing 'gpgconf --kill all'.");
   133 
   134     success = setenv("GNUPGHOME", prev_gpg_home.c_str(), 1);
   135     if (success != 0)
   136         throw std::runtime_error("RESTORE: Warning - cannot restore GNUPGHOME. Either set environment variable manually back to your home, or quit this session!");
   137                 
   138     success = nftw((test_home + "/.").c_str(), util_delete_filepath, 100, FTW_DEPTH);
   139     
   140     success = setenv("HOME", real_home.c_str(), 1);
   141     if (success != 0)
   142         throw std::runtime_error("RESTORE: Cannot reset home directory! Either set environment variable manually back to your home, or quit this session!");
   143     else
   144         cout << "RESTORE: HOME is now " << getenv("HOME") << endl;
   145     unix_local_db(true);
   146     gpg_conf(true);
   147     gpg_agent_conf(true);
   148 
   149 }
   150 
   151 void EngineTestSuite::setup() {
   152     on_test_number++;
   153 }
   154 
   155 void EngineTestSuite::tear_down() {}
   156 
   157 void EngineTestSuite::set_my_name() {
   158     my_name = typeid(*this).name();
   159 }