Now split into tests which init before every function and those which do for the whole session. While this will be used transitionally in some cases (all current tests run on this model within the same file), there is sense to being able to perform multiple tests within the same engine session anyway. local_cpptest
authorKrista Bennett <krista@pep-project.org>
Thu, 26 Apr 2018 12:01:33 +0200
branchlocal_cpptest
changeset 2633d7f52765febf
parent 2632 250b86a3a25c
child 2634 b754efa45304
Now split into tests which init before every function and those which do for the whole session. While this will be used transitionally in some cases (all current tests run on this model within the same file), there is sense to being able to perform multiple tests within the same engine session anyway.
test/EngineTest.cc
test/EngineTest.h
test/EngineTestIndividualSuite.cc
test/EngineTestIndividualSuite.h
test/EngineTestSessionSuite.cc
test/EngineTestSessionSuite.h
test/EngineTestSuite.cc
test/EngineTestSuite.h
     1.1 --- a/test/EngineTest.cc	Wed Apr 25 16:40:27 2018 +0200
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,105 +0,0 @@
     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 -}
    1.15 -
    1.16 -EngineTest::EngineTest(string suitename, string test_home_dir) {
    1.17 -    // FIXME: deal with base
    1.18 -    test_home = test_home_dir;
    1.19 -    
    1.20 -    // TODO: This is *nix specific, which the current testing env is anyway,
    1.21 -    // but it needn't remain so forever and always.
    1.22 -    prev_gpg_home = getenv("GNUPGHOME");
    1.23 -}
    1.24 -
    1.25 -void EngineTest::setup() {
    1.26 -    set_full_env(); // This will be called by default before every test
    1.27 -}
    1.28 -
    1.29 -void EngineTest::tear_down() {
    1.30 -    cout << "calling release()\n";
    1.31 -    release(session);
    1.32 -    restore_full_env();
    1.33 -}
    1.34 -
    1.35 -void EngineTest::set_full_env() {
    1.36 -
    1.37 -    if (test_home.empty())
    1.38 -        throw "SETUP: BAD INITIALISATION. No test home.";
    1.39 -    
    1.40 -    int success = system("gpgconf --kill all");
    1.41 -    if (success != 0)
    1.42 -        throw "SETUP: Error when executing 'gpgconf --kill all'.";
    1.43 -    
    1.44 -    string home = getenv("HOME");
    1.45 -    if (test_home.compare(home) == 0 || test_home.compare(home + "/") == 0 ||
    1.46 -        test_home.compare(home + "/.gnupg") == 0 || test_home.compare(home + ".gnupg") == 0 ||
    1.47 -        test_home.compare(prev_gpg_home) == 0 || test_home.compare(prev_gpg_home + "/.gnupg") == 0 ||
    1.48 -        test_home.compare(prev_gpg_home + ".gnupg") == 0)
    1.49 -        throw "SETUP: new GNUPGHOME threatens to mess up user GNUPGHOME (and delete all their keys). NO DICE.";
    1.50 -    
    1.51 -    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.52 -    
    1.53 -    struct stat buf;
    1.54 -    if (stat(test_home.c_str(), &buf) == 0) {
    1.55 -        cout << test_home << " exists. We'll recursively delete. We hope we're not horking your whole system..." << endl;
    1.56 -        success = nftw(test_home.c_str(), util_delete_filepath, 100, FTW_DEPTH);
    1.57 -        if (success != 0)
    1.58 -            throw "SETUP: can't delete the whole directory.";
    1.59 -    }
    1.60 -    
    1.61 -    success = setenv("GNUPGHOME", (test_home + "/.gnupg").c_str(), 1);
    1.62 -    if (success != 0)
    1.63 -        throw "SETUP: Error when setting GNUPGHOME.";
    1.64 -
    1.65 -    success = setenv("HOME", test_home.c_str(), 1);
    1.66 -    if (success != 0)
    1.67 -        throw "SETUP: Cannot set test_home for init.";
    1.68 -    
    1.69 -    cout << "calling init()\n";
    1.70 -    PEP_STATUS status = init(&session);
    1.71 -    // assert(status == PEP_STATUS_OK);
    1.72 -    // assert(session);
    1.73 -    cout << "init() completed.\n";
    1.74 -
    1.75 -    success = setenv("HOME", home.c_str(), 1);
    1.76 -    if (success != 0)
    1.77 -        throw "SETUP: Cannot reset home directory! Either set environment variable manually back to your home, or quit this session!";    
    1.78 -}
    1.79 -
    1.80 -void EngineTest::restore_full_env() {
    1.81 -    int success = system("gpgconf --kill all");
    1.82 -    if (success != 0)
    1.83 -        throw "RESTORE: Error when executing 'gpgconf --kill all'.";
    1.84 -
    1.85 -    success = setenv("GNUPGHOME", prev_gpg_home.c_str(), 1);
    1.86 -    if (success != 0)
    1.87 -        throw "RESTORE: Warning - cannot restore GNUPGHOME. Either set environment variable manually back to your home, or quit this session!";
    1.88 -}
    1.89 -
    1.90 -int EngineTest::util_delete_filepath(const char *filepath, 
    1.91 -                                     const struct stat *file_stat, 
    1.92 -                                     int ftw_info, 
    1.93 -                                     struct FTW * ftw_struct) {
    1.94 -    int retval = 0;
    1.95 -    switch (ftw_info) {
    1.96 -        case FTW_DP:
    1.97 -            retval = rmdir(filepath);
    1.98 -            break;
    1.99 -        case FTW_F:
   1.100 -        case FTW_SLN:
   1.101 -            retval = unlink(filepath);
   1.102 -            break;    
   1.103 -        default:
   1.104 -            retval = -1;
   1.105 -    }
   1.106 -    
   1.107 -    return retval;
   1.108 -}
     2.1 --- a/test/EngineTest.h	Wed Apr 25 16:40:27 2018 +0200
     2.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.3 @@ -1,32 +0,0 @@
     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 -        
    2.17 -    protected:
    2.18 -        PEP_SESSION session;
    2.19 -        string test_home;
    2.20 -        string prev_gpg_home;
    2.21 -        string name;
    2.22 -        
    2.23 -        virtual void setup();
    2.24 -        virtual void tear_down();
    2.25 -        void set_full_env();
    2.26 -        void restore_full_env();
    2.27 -        void initialise_test_home();
    2.28 -        
    2.29 -    private:
    2.30 -        static int util_delete_filepath(const char *filepath, 
    2.31 -                                        const struct stat *file_stat, 
    2.32 -                                        int ftw_info, 
    2.33 -                                        struct FTW * ftw_struct);
    2.34 -};
    2.35 -#endif
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/test/EngineTestIndividualSuite.cc	Thu Apr 26 12:01:33 2018 +0200
     3.3 @@ -0,0 +1,16 @@
     3.4 +#include <stdlib.h>
     3.5 +#include <unistd.h>
     3.6 +#include <ftw.h>
     3.7 +
     3.8 +#include "EngineTest.h"
     3.9 +using namespace std;
    3.10 +
    3.11 +void EngineTestIndividualSuite::setup() {
    3.12 +    set_full_env(); // This will be called by default before every test
    3.13 +}
    3.14 +
    3.15 +void EngineTestIndividualSuite::tear_down() {
    3.16 +    cout << "calling release()\n";
    3.17 +    release(session);
    3.18 +    restore_full_env();
    3.19 +}
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/test/EngineTestIndividualSuite.h	Thu Apr 26 12:01:33 2018 +0200
     4.3 @@ -0,0 +1,15 @@
     4.4 +#ifndef ENGINE_INDIVIDUAL_SESSION_SUITE_H
     4.5 +#define ENGINE_INDIVIDUAL_SESSION_SUITE_H
     4.6 +
     4.7 +#include <cpptest-suite.h>
     4.8 +#include <string>
     4.9 +#include "pEpEngine.h"
    4.10 +
    4.11 +using namespace std;
    4.12 +
    4.13 +class EngineTestIndividualSuite : public EngineTestSuite {
    4.14 +    public:
    4.15 +        EngineIndividualTestSuite(string suitename, string test_home_dir);
    4.16 +        ~EngineIndividualTestSuite();
    4.17 +};
    4.18 +#endif
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/test/EngineTestSessionSuite.cc	Thu Apr 26 12:01:33 2018 +0200
     5.3 @@ -0,0 +1,16 @@
     5.4 +#include <stdlib.h>
     5.5 +#include <unistd.h>
     5.6 +#include <ftw.h>
     5.7 +
     5.8 +#include "EngineTestSuite.h"
     5.9 +using namespace std;
    5.10 +
    5.11 +// Constructor
    5.12 +EngineTestSessionSuite::EngineTestSessionSuite(string suitename, string test_home_dir) :
    5.13 +    EngineTestSuite::EngineTestSuite(suitename, test_home_dir) {
    5.14 +    set_full_env();
    5.15 +}
    5.16 +
    5.17 +EngineTestSessionSuite::~EngineTestSessionSuite() {
    5.18 +    restore_full_env();
    5.19 +}
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/test/EngineTestSessionSuite.h	Thu Apr 26 12:01:33 2018 +0200
     6.3 @@ -0,0 +1,15 @@
     6.4 +#ifndef ENGINE_TEST_SESSION_SUITE_H
     6.5 +#define ENGINE_TEST_SESSION_SUITE_H
     6.6 +
     6.7 +#include <cpptest-suite.h>
     6.8 +#include <string>
     6.9 +#include "pEpEngine.h"
    6.10 +
    6.11 +using namespace std;
    6.12 +
    6.13 +class EngineTestSessionSuite : public EngineTestSuite {
    6.14 +    public:
    6.15 +        EngineTestSessionSuite(string suitename, string test_home_dir);
    6.16 +        ~EngineTestSessionSuite();
    6.17 +};
    6.18 +#endif
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/test/EngineTestSuite.cc	Thu Apr 26 12:01:33 2018 +0200
     7.3 @@ -0,0 +1,91 @@
     7.4 +#include <stdlib.h>
     7.5 +#include <unistd.h>
     7.6 +#include <ftw.h>
     7.7 +
     7.8 +#include "EngineTestSuite.h"
     7.9 +using namespace std;
    7.10 +
    7.11 +// Constructor
    7.12 +EngineTestSuite::EngineTestSuite(string suitename, string test_home_dir) {
    7.13 +    // FIXME: deal with base
    7.14 +    test_home = test_home_dir;
    7.15 +    
    7.16 +    // TODO: This is *nix specific, which the current testing env is anyway,
    7.17 +    // but it needn't remain so forever and always.
    7.18 +    prev_gpg_home = getenv("GNUPGHOME");
    7.19 +}
    7.20 +
    7.21 +void EngineTestSuite::set_full_env() {
    7.22 +
    7.23 +    if (test_home.empty())
    7.24 +        throw "SETUP: BAD INITIALISATION. No test home.";
    7.25 +    
    7.26 +    int success = system("gpgconf --kill all");
    7.27 +    if (success != 0)
    7.28 +        throw "SETUP: Error when executing 'gpgconf --kill all'.";
    7.29 +    
    7.30 +    string home = getenv("HOME");
    7.31 +    if (test_home.compare(home) == 0 || test_home.compare(home + "/") == 0 ||
    7.32 +        test_home.compare(home + "/.gnupg") == 0 || test_home.compare(home + ".gnupg") == 0 ||
    7.33 +        test_home.compare(prev_gpg_home) == 0 || test_home.compare(prev_gpg_home + "/.gnupg") == 0 ||
    7.34 +        test_home.compare(prev_gpg_home + ".gnupg") == 0)
    7.35 +        throw "SETUP: new GNUPGHOME threatens to mess up user GNUPGHOME (and delete all their keys). NO DICE.";
    7.36 +    
    7.37 +    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;
    7.38 +    
    7.39 +    struct stat buf;
    7.40 +    if (stat(test_home.c_str(), &buf) == 0) {
    7.41 +        cout << test_home << " exists. We'll recursively delete. We hope we're not horking your whole system..." << endl;
    7.42 +        success = nftw(test_home.c_str(), util_delete_filepath, 100, FTW_DEPTH);
    7.43 +        if (success != 0)
    7.44 +            throw "SETUP: can't delete the whole directory.";
    7.45 +    }
    7.46 +    
    7.47 +    success = setenv("GNUPGHOME", (test_home + "/.gnupg").c_str(), 1);
    7.48 +    if (success != 0)
    7.49 +        throw "SETUP: Error when setting GNUPGHOME.";
    7.50 +
    7.51 +    success = setenv("HOME", test_home.c_str(), 1);
    7.52 +    if (success != 0)
    7.53 +        throw "SETUP: Cannot set test_home for init.";
    7.54 +    
    7.55 +    cout << "calling init()\n";
    7.56 +    PEP_STATUS status = init(&session);
    7.57 +    // assert(status == PEP_STATUS_OK);
    7.58 +    // assert(session);
    7.59 +    cout << "init() completed.\n";
    7.60 +
    7.61 +    success = setenv("HOME", home.c_str(), 1);
    7.62 +    if (success != 0)
    7.63 +        throw "SETUP: Cannot reset home directory! Either set environment variable manually back to your home, or quit this session!";    
    7.64 +}
    7.65 +
    7.66 +void EngineTestSuite::restore_full_env() {
    7.67 +    int success = system("gpgconf --kill all");
    7.68 +    if (success != 0)
    7.69 +        throw "RESTORE: Error when executing 'gpgconf --kill all'.";
    7.70 +
    7.71 +    success = setenv("GNUPGHOME", prev_gpg_home.c_str(), 1);
    7.72 +    if (success != 0)
    7.73 +        throw "RESTORE: Warning - cannot restore GNUPGHOME. Either set environment variable manually back to your home, or quit this session!";
    7.74 +}
    7.75 +
    7.76 +int EngineTestSuite::util_delete_filepath(const char *filepath, 
    7.77 +                                     const struct stat *file_stat, 
    7.78 +                                     int ftw_info, 
    7.79 +                                     struct FTW * ftw_struct) {
    7.80 +    int retval = 0;
    7.81 +    switch (ftw_info) {
    7.82 +        case FTW_DP:
    7.83 +            retval = rmdir(filepath);
    7.84 +            break;
    7.85 +        case FTW_F:
    7.86 +        case FTW_SLN:
    7.87 +            retval = unlink(filepath);
    7.88 +            break;    
    7.89 +        default:
    7.90 +            retval = -1;
    7.91 +    }
    7.92 +    
    7.93 +    return retval;
    7.94 +}
     8.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.2 +++ b/test/EngineTestSuite.h	Thu Apr 26 12:01:33 2018 +0200
     8.3 @@ -0,0 +1,32 @@
     8.4 +#ifndef ENGINE_TEST_SUITE_H
     8.5 +#define ENGINE_TEST_SUITE_H
     8.6 +
     8.7 +#include <cpptest-suite.h>
     8.8 +#include <string>
     8.9 +#include "pEpEngine.h"
    8.10 +
    8.11 +using namespace std;
    8.12 +
    8.13 +class EngineTestSuite : public Test::Suite {
    8.14 +    public:
    8.15 +        EngineTestSuite(string suitename, string test_home_dir);
    8.16 +        
    8.17 +    protected:
    8.18 +        PEP_SESSION session;
    8.19 +        string test_home;
    8.20 +        string prev_gpg_home;
    8.21 +        string name;
    8.22 +        
    8.23 +        virtual void setup();
    8.24 +        virtual void tear_down();
    8.25 +        void set_full_env();
    8.26 +        void restore_full_env();
    8.27 +        void initialise_test_home();
    8.28 +        
    8.29 +    private:
    8.30 +        static int util_delete_filepath(const char *filepath, 
    8.31 +                                        const struct stat *file_stat, 
    8.32 +                                        int ftw_info, 
    8.33 +                                        struct FTW * ftw_struct);
    8.34 +};
    8.35 +#endif