Rudimentary Engine Test wrapper compiles, finally :) local_cpptest
authorKrista Bennett <krista@pep-project.org>
Wed, 25 Apr 2018 16:40:27 +0200
branchlocal_cpptest
changeset 2632250b86a3a25c
parent 2631 c99b50206039
child 2633 d7f52765febf
Rudimentary Engine Test wrapper compiles, finally :)
test/EngineTest.cc
test/EngineTest.h
     1.1 --- a/test/EngineTest.cc	Wed Apr 25 10:47:56 2018 +0200
     1.2 +++ b/test/EngineTest.cc	Wed Apr 25 16:40:27 2018 +0200
     1.3 @@ -8,8 +8,6 @@
     1.4  // Constructors
     1.5  EngineTest::EngineTest() {
     1.6      session = nullptr;
     1.7 -    test_home = nullptr;
     1.8 -    prev_gpg_home = nullptr;
     1.9  }
    1.10  
    1.11  EngineTest::EngineTest(string suitename, string test_home_dir) {
    1.12 @@ -21,24 +19,24 @@
    1.13      prev_gpg_home = getenv("GNUPGHOME");
    1.14  }
    1.15  
    1.16 -virtual void EngineTest::setup() {
    1.17 +void EngineTest::setup() {
    1.18      set_full_env(); // This will be called by default before every test
    1.19  }
    1.20  
    1.21 -virtual void EngineTest::teardown() {
    1.22 +void EngineTest::tear_down() {
    1.23      cout << "calling release()\n";
    1.24      release(session);
    1.25 -    return 0;
    1.26 +    restore_full_env();
    1.27  }
    1.28  
    1.29  void EngineTest::set_full_env() {
    1.30  
    1.31 -    if (!test_home)
    1.32 +    if (test_home.empty())
    1.33          throw "SETUP: BAD INITIALISATION. No test home.";
    1.34      
    1.35      int success = system("gpgconf --kill all");
    1.36      if (success != 0)
    1.37 -        throw "SETUP: Error when executing 'gpgconf --kill all'."
    1.38 +        throw "SETUP: Error when executing 'gpgconf --kill all'.";
    1.39      
    1.40      string home = getenv("HOME");
    1.41      if (test_home.compare(home) == 0 || test_home.compare(home + "/") == 0 ||
    1.42 @@ -50,7 +48,7 @@
    1.43      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.44      
    1.45      struct stat buf;
    1.46 -    if (stat(test_home.c_str(), buf) == 0) {
    1.47 +    if (stat(test_home.c_str(), &buf) == 0) {
    1.48          cout << test_home << " exists. We'll recursively delete. We hope we're not horking your whole system..." << endl;
    1.49          success = nftw(test_home.c_str(), util_delete_filepath, 100, FTW_DEPTH);
    1.50          if (success != 0)
    1.51 @@ -61,21 +59,31 @@
    1.52      if (success != 0)
    1.53          throw "SETUP: Error when setting GNUPGHOME.";
    1.54  
    1.55 -    success = setenv("HOME", test_home)
    1.56 +    success = setenv("HOME", test_home.c_str(), 1);
    1.57      if (success != 0)
    1.58          throw "SETUP: Cannot set test_home for init.";
    1.59      
    1.60      cout << "calling init()\n";
    1.61      PEP_STATUS status = init(&session);
    1.62 -    assert(status == PEP_STATUS_OK);
    1.63 -    assert(session);
    1.64 +    // assert(status == PEP_STATUS_OK);
    1.65 +    // assert(session);
    1.66      cout << "init() completed.\n";
    1.67  
    1.68 -    success = setenv("HOME", home);
    1.69 +    success = setenv("HOME", home.c_str(), 1);
    1.70      if (success != 0)
    1.71          throw "SETUP: Cannot reset home directory! Either set environment variable manually back to your home, or quit this session!";    
    1.72  }
    1.73  
    1.74 +void EngineTest::restore_full_env() {
    1.75 +    int success = system("gpgconf --kill all");
    1.76 +    if (success != 0)
    1.77 +        throw "RESTORE: Error when executing 'gpgconf --kill all'.";
    1.78 +
    1.79 +    success = setenv("GNUPGHOME", prev_gpg_home.c_str(), 1);
    1.80 +    if (success != 0)
    1.81 +        throw "RESTORE: Warning - cannot restore GNUPGHOME. Either set environment variable manually back to your home, or quit this session!";
    1.82 +}
    1.83 +
    1.84  int EngineTest::util_delete_filepath(const char *filepath, 
    1.85                                       const struct stat *file_stat, 
    1.86                                       int ftw_info, 
     2.1 --- a/test/EngineTest.h	Wed Apr 25 10:47:56 2018 +0200
     2.2 +++ b/test/EngineTest.h	Wed Apr 25 16:40:27 2018 +0200
     2.3 @@ -10,15 +10,23 @@
     2.4      public:
     2.5          EngineTest();
     2.6          EngineTest(string suitename, string test_home_dir);
     2.7 +        
     2.8      protected:
     2.9          PEP_SESSION session;
    2.10          string test_home;
    2.11          string prev_gpg_home;
    2.12          string name;
    2.13 +        
    2.14          virtual void setup();
    2.15          virtual void tear_down();
    2.16          void set_full_env();
    2.17 -        void release_full_env();
    2.18 -        void initialise_test_home();    
    2.19 +        void restore_full_env();
    2.20 +        void initialise_test_home();
    2.21 +        
    2.22 +    private:
    2.23 +        static int util_delete_filepath(const char *filepath, 
    2.24 +                                        const struct stat *file_stat, 
    2.25 +                                        int ftw_info, 
    2.26 +                                        struct FTW * ftw_struct);
    2.27  };
    2.28  #endif