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