test/src/EngineTestSuite.cc
author Krista 'DarthMama' Bennett <krista@pep.foundation>
Mon, 28 Jan 2019 14:04:12 +0100
changeset 3246 ec48ea509c8d
parent 3229 6d555032f84c
child 3270 d8aea79654c3
child 3277 fe1e94372261
permissions -rw-r--r--
Added dir creation and script for gpg socket dir to default
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@2828
     8
#include <fstream>
krista@2828
     9
#include <iostream>
krista@2828
    10
#include <sys/types.h>
krista@2828
    11
#include <sys/stat.h>
krista@2828
    12
krista@3170
    13
#include <string>
krista@3170
    14
#include <vector>
krista@2828
    15
#include <utility>
krista@2628
    16
krista@2703
    17
#include "platform_unix.h"
krista@2703
    18
krista@2703
    19
#include "test_util.h"
krista@2633
    20
#include "EngineTestSuite.h"
krista@3217
    21
#include "pEpTestStatic.h"
krista@3217
    22
#include <algorithm>
krista@2928
    23
krista@2628
    24
using namespace std;
krista@2628
    25
krista@2633
    26
// Constructor
krista@2633
    27
EngineTestSuite::EngineTestSuite(string suitename, string test_home_dir) {
krista@2628
    28
    // FIXME: deal with base
krista@2628
    29
    test_home = test_home_dir;
krista@2703
    30
            
krista@2652
    31
    number_of_tests = 0;
krista@2652
    32
    on_test_number = 0;
krista@2703
    33
    real_home = getenv("HOME");
krista@2628
    34
}
krista@2628
    35
krista@2645
    36
EngineTestSuite::~EngineTestSuite() {}
krista@2645
    37
krista@2653
    38
void EngineTestSuite::add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()> test_func) {
krista@2653
    39
    test_map.insert(test_func);
krista@2653
    40
    register_test(test_func.second, test_func.first);
krista@2652
    41
    number_of_tests++;
krista@2652
    42
}
krista@2652
    43
krista@2828
    44
void EngineTestSuite::copy_conf_file_to_test_dir(const char* dest_path, const char* conf_orig_path, const char* conf_dest_name) {
krista@2828
    45
    string conf_dest_path = dest_path;
krista@2828
    46
    
krista@2828
    47
    struct stat pathinfo;
krista@2828
    48
krista@2828
    49
    if(stat(conf_dest_path.c_str(), &pathinfo) != 0) {
krista@2828
    50
        int errchk = mkdir(conf_dest_path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
krista@2828
    51
        if (errchk != 0)
krista@2828
    52
            throw std::runtime_error("Error accessing conf file directory.");
krista@2828
    53
    }
krista@2828
    54
    
krista@2828
    55
    conf_dest_path += "/";
krista@2828
    56
    conf_dest_path += conf_dest_name;
krista@2828
    57
    
krista@2828
    58
    ifstream src(conf_orig_path);
krista@2828
    59
    ofstream dst(conf_dest_path.c_str(), ios::trunc);
krista@2828
    60
    
krista@2828
    61
    assert(src);
krista@2828
    62
    assert(dst);
krista@2828
    63
    
krista@2828
    64
    dst << src.rdbuf();
krista@2828
    65
     
krista@2828
    66
    src.close();
krista@2828
    67
    dst.close();
krista@2828
    68
}
krista@2828
    69
krista@2828
    70
void EngineTestSuite::add_file_to_gpg_dir_queue(string copy_from, string dst_fname) {    
krista@2828
    71
    gpgdir_fileadd_queue.push_back(make_pair(copy_from, dst_fname));
krista@2828
    72
}
krista@2828
    73
krista@2828
    74
void EngineTestSuite::add_file_to_home_dir_queue(string copy_from, string dst_fname) {
krista@2828
    75
    homedir_fileadd_queue.push_back(make_pair(copy_from, dst_fname));
krista@2828
    76
}
krista@2828
    77
krista@2828
    78
void EngineTestSuite::process_file_queue(string dirname, vector<pair<string, string>> file_queue) {
krista@2828
    79
    if (file_queue.empty())
krista@2828
    80
        return;
krista@2828
    81
        
krista@2828
    82
    vector<pair<string, string>>::iterator it;
krista@2828
    83
    
krista@2828
    84
    for (it = file_queue.begin(); it != file_queue.end(); it++) {
krista@2828
    85
        copy_conf_file_to_test_dir(dirname.c_str(), it->first.c_str(), it->second.c_str());
krista@2828
    86
    }
krista@2828
    87
    
krista@2828
    88
    file_queue.clear();
krista@2828
    89
}
krista@2828
    90
krista@2633
    91
void EngineTestSuite::set_full_env() {
krista@2828
    92
    set_full_env(NULL, NULL, NULL);
krista@2828
    93
}
krista@2828
    94
krista@2828
    95
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
    96
    int success = 0;
krista@2703
    97
    struct stat dirchk;
krista@2703
    98
    
krista@2703
    99
    set_my_name();
krista@2628
   100
krista@3217
   101
// FIXME
krista@3160
   102
#ifndef USE_NETPGP
krista@2703
   103
    success = system("gpgconf --kill all");
krista@2628
   104
    if (success != 0)
krista@3229
   105
        throw std::runtime_error("SETUP: Error when executing 'gpgconf --kill all'.");    
krista@3160
   106
#endif
krista@2703
   107
krista@2703
   108
    if (stat(test_home.c_str(), &dirchk) == 0) {
krista@2703
   109
        if (!S_ISDIR(dirchk.st_mode))
krista@2703
   110
            throw std::runtime_error(("The test directory, " + test_home + "exists, but is not a directory.").c_str()); 
krista@2703
   111
                    
krista@2703
   112
        struct stat buf;
krista@2703
   113
krista@2703
   114
        if (stat(test_home.c_str(), &buf) == 0) {
krista@2703
   115
            int success = nftw((test_home + "/.").c_str(), util_delete_filepath, 100, FTW_DEPTH);
krista@2703
   116
        }
krista@2703
   117
    }
krista@2703
   118
    else {
krista@2703
   119
        int errchk = mkdir(test_home.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
krista@2703
   120
        cout << errchk << endl;
krista@2703
   121
        if (errchk != 0)
krista@2703
   122
            throw std::runtime_error("Error creating a test directory.");
krista@2703
   123
    }
krista@2703
   124
krista@3217
   125
    if (my_name.size() > pEpTestStatic::classname_chars)
krista@3217
   126
        my_name.resize(pEpTestStatic::classname_chars);
krista@3217
   127
krista@3217
   128
    if (on_test_number > pEpTestStatic::max_test_num) {
krista@3217
   129
        cerr << "Warning - there are at least " << pEpTestStatic::max_test_num << " tests in this suite. While this probably won't cause "
krista@3217
   130
             << endl << "problems, there is an obscure possibility that if your test path is REALLY REALLY LONG, tests will fail because gpg-agent "
krista@3217
   131
             << endl << "won't start with huge paths. In general, however, we stop well before these limits, and pEpTestStatic::testnum_path_chars "
krista@3217
   132
             << endl << "is overly conservative, so you probably don't need to worry." << endl;
krista@3217
   133
    }    
krista@3217
   134
krista@2828
   135
    temp_test_home = test_home + "/" + my_name;
krista@2703
   136
    
krista@2703
   137
    int errchk = mkdir(temp_test_home.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
krista@2703
   138
    if (errchk != 0)
krista@2703
   139
        throw std::runtime_error("Error creating a test directory.");
krista@2703
   140
krista@2703
   141
    temp_test_home += "/" + to_string(on_test_number);
krista@2703
   142
krista@2703
   143
    errchk = mkdir(temp_test_home.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
krista@2703
   144
    if (errchk != 0)
krista@2703
   145
        throw std::runtime_error("Error creating a test directory.");
krista@2703
   146
krista@2703
   147
    // TODO: This is *nix specific, which the current testing env is anyway,
krista@2703
   148
    // but it needn't remain so forever and always
krista@3160
   149
krista@3160
   150
    string home = getenv("HOME");
krista@3160
   151
krista@3160
   152
    char* tmp = NULL;
krista@3160
   153
    
krista@3160
   154
    tmp = getenv("GNUPGHOME");
krista@3160
   155
krista@3160
   156
    prev_pgp_home.clear();
krista@3160
   157
    
krista@2703
   158
    if (tmp)
krista@3160
   159
        prev_pgp_home = tmp;
krista@2703
   160
        
krista@2703
   161
    if (temp_test_home.empty())
krista@2703
   162
        throw std::runtime_error("SETUP: BAD INITIALISATION. No test home.");
krista@2628
   163
    
krista@2703
   164
    assert(temp_test_home.compare(home) != 0);
krista@2703
   165
    assert(temp_test_home.compare(home + "/") != 0);
krista@2828
   166
    assert(temp_test_home.compare(home + "/.gnupg") != 0); // This is an EXCLUSION test, so we leave this.
krista@2828
   167
    assert(temp_test_home.compare(home + ".gnupg") != 0);
vb@2803
   168
    assert(temp_test_home.compare(home + "/gnupg") != 0);
vb@2803
   169
    assert(temp_test_home.compare(home + "gnupg") != 0);
krista@3160
   170
    assert(temp_test_home.compare(prev_pgp_home) != 0);
krista@3160
   171
    assert(temp_test_home.compare(prev_pgp_home + "/gnupg") != 0);
krista@3160
   172
    assert(temp_test_home.compare(prev_pgp_home + "gnupg") != 0);
krista@3160
   173
    assert(temp_test_home.compare(prev_pgp_home + "/.gnupg") != 0);
krista@3160
   174
    assert(temp_test_home.compare(prev_pgp_home + ".gnupg") != 0);
krista@2703
   175
krista@2703
   176
    if (temp_test_home.compare(home) == 0 || temp_test_home.compare(home + "/") == 0 ||
vb@2803
   177
        temp_test_home.compare(home + "/gnupg") == 0 || temp_test_home.compare(home + "gnupg") == 0 ||
krista@2828
   178
        temp_test_home.compare(home + "/.gnupg") == 0 || temp_test_home.compare(home + ".gnupg") == 0 ||
krista@3160
   179
        temp_test_home.compare(prev_pgp_home) == 0 || temp_test_home.compare(prev_pgp_home + "/gnupg") == 0 ||
krista@3160
   180
        temp_test_home.compare(prev_pgp_home + "gnupg") == 0 || temp_test_home.compare(prev_pgp_home + "/.gnupg") == 0 ||
krista@3160
   181
        temp_test_home.compare(prev_pgp_home + ".gnupg") == 0)
krista@3160
   182
        throw std::runtime_error("SETUP: new pgp homedir threatens to mess up user pgp homedir (and delete all their keys). NO DICE.");
krista@2628
   183
    
krista@2703
   184
//    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@2928
   185
    cout << "Test home directory is " << temp_test_home << endl;
krista@2628
   186
    
krista@2628
   187
    struct stat buf;
krista@2628
   188
    
vb@2803
   189
    success = setenv("GNUPGHOME", (temp_test_home + "/gnupg").c_str(), 1);
krista@2628
   190
    if (success != 0)
krista@2639
   191
        throw std::runtime_error("SETUP: Error when setting GNUPGHOME.");
krista@2628
   192
krista@2928
   193
    cout << "New GNUPGHOME is " << getenv("GNUPGHOME") << endl << endl;
krista@2703
   194
    
krista@2703
   195
    success = setenv("HOME", temp_test_home.c_str(), 1);
krista@2628
   196
    if (success != 0)
krista@2639
   197
        throw std::runtime_error("SETUP: Cannot set test_home for init.");
krista@2828
   198
krista@2828
   199
    string tmp_gpg_dir = temp_test_home + "/.gnupg";
krista@2828
   200
krista@2828
   201
    process_file_queue(tmp_gpg_dir, gpgdir_fileadd_queue);
krista@2828
   202
    process_file_queue(temp_test_home, homedir_fileadd_queue);
krista@2828
   203
krista@2828
   204
    if (gpg_conf_copy_path)
krista@2828
   205
        copy_conf_file_to_test_dir((temp_test_home + "/gnupg").c_str(), gpg_conf_copy_path, "gpg.conf");
krista@2828
   206
    if (gpg_agent_conf_file_copy_path)        
krista@2828
   207
        copy_conf_file_to_test_dir((temp_test_home + "/gnupg").c_str(), gpg_agent_conf_file_copy_path, "gpg-agent.conf");
krista@2828
   208
    if (db_conf_file_copy_path)
krista@2828
   209
        copy_conf_file_to_test_dir(temp_test_home.c_str(), db_conf_file_copy_path, ".pEp_management.db");
krista@2828
   210
        
krista@2703
   211
    unix_local_db(true);
krista@2703
   212
    gpg_conf(true);
krista@2703
   213
    gpg_agent_conf(true);
krista@2703
   214
    
krista@2703
   215
//    cout << "calling init()\n";
krista@2628
   216
    PEP_STATUS status = init(&session);
krista@3246
   217
#ifndef USE_NETPGP            
krista@3246
   218
    success = system("gpgconf --create-socketdir");
krista@3246
   219
    if (success != 0)
krista@3246
   220
        throw std::runtime_error("RESTORE: Error when executing 'gpgconf --create-socketdir'.");            
krista@3217
   221
    system("gpg-connect-agent /bye");   // Just in case - otherwise, we die on MacOS sometimes. Is this enough??
krista@3246
   222
#endif 
krista@2703
   223
    assert(status == PEP_STATUS_OK);
krista@2703
   224
    assert(session);
krista@2703
   225
//    cout << "init() completed.\n";
krista@2628
   226
krista@2628
   227
}
krista@2628
   228
krista@2633
   229
void EngineTestSuite::restore_full_env() {
krista@2703
   230
    release(session);
krista@2703
   231
    session = NULL;
krista@2703
   232
        
krista@3160
   233
    int success = 0;    
krista@3160
   234
krista@3160
   235
#ifndef USE_NETPGP        
krista@3160
   236
    success = system("gpgconf --kill all");
krista@2632
   237
    if (success != 0)
krista@2639
   238
        throw std::runtime_error("RESTORE: Error when executing 'gpgconf --kill all'.");
krista@3246
   239
    success = system("gpgconf --remove-socketdir");            
krista@3246
   240
    if (success != 0)
krista@3246
   241
        throw std::runtime_error("RESTORE: Error when executing 'gpgconf --remove-socketdir'.");
krista@3160
   242
#endif
krista@2632
   243
krista@3160
   244
    success = setenv("GNUPGHOME", prev_pgp_home.c_str(), 1);
krista@2632
   245
    if (success != 0)
krista@2639
   246
        throw std::runtime_error("RESTORE: Warning - cannot restore GNUPGHOME. Either set environment variable manually back to your home, or quit this session!");
krista@2703
   247
                
krista@2703
   248
    success = nftw((test_home + "/.").c_str(), util_delete_filepath, 100, FTW_DEPTH);
krista@2703
   249
    
krista@2703
   250
    success = setenv("HOME", real_home.c_str(), 1);
krista@2703
   251
    if (success != 0)
krista@2703
   252
        throw std::runtime_error("RESTORE: Cannot reset home directory! Either set environment variable manually back to your home, or quit this session!");
krista@2928
   253
    // else
krista@2928
   254
    //     cout << "RESTORE: HOME is now " << getenv("HOME") << endl;
krista@2703
   255
    unix_local_db(true);
krista@2703
   256
    gpg_conf(true);
krista@2703
   257
    gpg_agent_conf(true);
krista@2703
   258
krista@2632
   259
}
krista@2632
   260
krista@2653
   261
void EngineTestSuite::setup() {
krista@2653
   262
    on_test_number++;
krista@2653
   263
}
krista@2653
   264
krista@2634
   265
void EngineTestSuite::tear_down() {}
krista@2703
   266
krista@2703
   267
void EngineTestSuite::set_my_name() {
krista@2703
   268
    my_name = typeid(*this).name();
vb@2803
   269
}