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