test experiment local_cpptest
authorKrista Bennett <krista@pep-project.org>
Wed, 25 Apr 2018 08:39:15 +0200
branchlocal_cpptest
changeset 262828f07dbbafea
parent 2618 3cc1716b444e
child 2631 c99b50206039
test experiment
test/EngineTest.cc
test/EngineTest.h
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/EngineTest.cc	Wed Apr 25 08:39:15 2018 +0200
     1.3 @@ -0,0 +1,97 @@
     1.4 +#include <stdlib.h>
     1.5 +#include <unistd.h>
     1.6 +#include <ftw.h>
     1.7 +
     1.8 +#include "EngineTest.h"
     1.9 +using namespace std;
    1.10 +
    1.11 +// Constructors
    1.12 +EngineTest::EngineTest() {
    1.13 +    session = nullptr;
    1.14 +    test_home = nullptr;
    1.15 +    prev_gpg_home = nullptr;
    1.16 +}
    1.17 +
    1.18 +EngineTest::EngineTest(string suitename, string test_home_dir) {
    1.19 +    // FIXME: deal with base
    1.20 +    test_home = test_home_dir;
    1.21 +    
    1.22 +    // TODO: This is *nix specific, which the current testing env is anyway,
    1.23 +    // but it needn't remain so forever and always.
    1.24 +    prev_gpg_home = getenv("GNUPGHOME");
    1.25 +}
    1.26 +
    1.27 +virtual void EngineTest::setup() {
    1.28 +    set_full_env(); // This will be called by default before every test
    1.29 +}
    1.30 +
    1.31 +virtual void EngineTest::teardown() {
    1.32 +    cout << "calling release()\n";
    1.33 +    release(session);
    1.34 +    return 0;
    1.35 +}
    1.36 +
    1.37 +void EngineTest::set_full_env() {
    1.38 +
    1.39 +    if (!test_home)
    1.40 +        throw "SETUP: BAD INITIALISATION. No test home.";
    1.41 +    
    1.42 +    int success = system("gpgconf --kill all");
    1.43 +    if (success != 0)
    1.44 +        throw "SETUP: Error when executing 'gpgconf --kill all'."
    1.45 +    
    1.46 +    string home = getenv("HOME");
    1.47 +    if (test_home.compare(home) == 0 || test_home.compare(home + "/") == 0 ||
    1.48 +        test_home.compare(home + "/.gnupg") == 0 || test_home.compare(home + ".gnupg") == 0 ||
    1.49 +        test_home.compare(prev_gpg_home) == 0 || test_home.compare(prev_gpg_home + "/.gnupg") == 0 ||
    1.50 +        test_home.compare(prev_gpg_home + ".gnupg") == 0)
    1.51 +        throw "SETUP: new GNUPGHOME threatens to mess up user GNUPGHOME (and delete all their keys). NO DICE.";
    1.52 +    
    1.53 +    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;
    1.54 +    
    1.55 +    struct stat buf;
    1.56 +    if (stat(test_home.c_str(), buf) == 0) {
    1.57 +        cout << test_home << " exists. We'll recursively delete. We hope we're not horking your whole system..." << endl;
    1.58 +        success = nftw(test_home.c_str(), util_delete_filepath, 100, FTW_DEPTH);
    1.59 +        if (success != 0)
    1.60 +            throw "SETUP: can't delete the whole directory.";
    1.61 +    }
    1.62 +    
    1.63 +    success = setenv("GNUPGHOME", (test_home + "/.gnupg").c_str(), 1);
    1.64 +    if (success != 0)
    1.65 +        throw "SETUP: Error when setting GNUPGHOME.";
    1.66 +
    1.67 +    success = setenv("HOME", test_home)
    1.68 +    if (success != 0)
    1.69 +        throw "SETUP: Cannot set test_home for init.";
    1.70 +    
    1.71 +    cout << "calling init()\n";
    1.72 +    PEP_STATUS status = init(&session);
    1.73 +    assert(status == PEP_STATUS_OK);
    1.74 +    assert(session);
    1.75 +    cout << "init() completed.\n";
    1.76 +
    1.77 +    success = setenv("HOME", home);
    1.78 +    if (success != 0)
    1.79 +        throw "SETUP: Cannot reset home directory! Either set environment variable manually back to your home, or quit this session!";    
    1.80 +}
    1.81 +
    1.82 +int EngineTest::util_delete_filepath(const char *filepath, 
    1.83 +                                     const struct stat *file_stat, 
    1.84 +                                     int ftw_info, 
    1.85 +                                     struct FTW * ftw_struct) {
    1.86 +    int retval = 0;
    1.87 +    switch (ftw_info) {
    1.88 +        case FTW_DP:
    1.89 +            retval = rmdir(filepath);
    1.90 +            break;
    1.91 +        case FTW_F:
    1.92 +        case FTW_SLN:
    1.93 +            retval = unlink(filepath);
    1.94 +            break;    
    1.95 +        default:
    1.96 +            retval = -1;
    1.97 +    }
    1.98 +    
    1.99 +    return retval;
   1.100 +}
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/test/EngineTest.h	Wed Apr 25 08:39:15 2018 +0200
     2.3 @@ -0,0 +1,24 @@
     2.4 +#ifndef ENGINE_TEST_H
     2.5 +
     2.6 +#include <cpptest-suite.h>
     2.7 +#include <string>
     2.8 +#include "pEpEngine.h"
     2.9 +
    2.10 +using namespace std;
    2.11 +
    2.12 +class EngineTest : public Test::Suite {
    2.13 +    public:
    2.14 +        EngineTest();
    2.15 +        EngineTest(string suitename, string test_home_dir);
    2.16 +    protected:
    2.17 +        PEP_SESSION session;
    2.18 +        string test_home;
    2.19 +        string prev_gpg_home;
    2.20 +        string name;
    2.21 +        virtual void setup();
    2.22 +        virtual void tear_down();
    2.23 +        void set_full_env();
    2.24 +        void release_full_env();
    2.25 +        void initialise_test_home();    
    2.26 +};
    2.27 +#endif