ENGINE-451: old files now backed up appropriately, new file now created in a separate file before rename clobbers confs. ENGINE-451
authorKrista Bennett <krista@pep-project.org>
Wed, 01 Aug 2018 16:34:38 +0200
branchENGINE-451
changeset 2798e0abb44b8098
parent 2797 0ff0fc8d9683
child 2809 45ebfd77285d
ENGINE-451: old files now backed up appropriately, new file now created in a separate file before rename clobbers confs.
test/include/EngineTestSuite.h
test/include/GPGConfFixTests.h
test/include/test_util.h
test/src/EngineTestSuite.cc
test/src/engine_tests/GPGConfFixTests.cc
test/src/util/test_util.cc
     1.1 --- a/test/include/EngineTestSuite.h	Wed Aug 01 11:54:36 2018 +0200
     1.2 +++ b/test/include/EngineTestSuite.h	Wed Aug 01 16:34:38 2018 +0200
     1.3 @@ -4,6 +4,8 @@
     1.4  #include <cpptest.h>
     1.5  #include <string>
     1.6  #include <map>
     1.7 +#include <vector>
     1.8 +#include <utility>
     1.9  #include "pEpEngine.h"
    1.10  
    1.11  using namespace std;
    1.12 @@ -40,11 +42,18 @@
    1.13          virtual void tear_down();
    1.14          
    1.15          void set_full_env();
    1.16 -	    void set_full_env(const char* gpg_conf_copy_path, const char* gpg_agent_conf_file_copy_path, const char* db_conf_file_copy_path);
    1.17 +        void set_full_env(const char* gpg_conf_copy_path, const char* gpg_agent_conf_file_copy_path, const char* db_conf_file_copy_path);
    1.18          void restore_full_env();
    1.19          void initialise_test_home();
    1.20          void set_my_name();    
    1.21  	
    1.22 -	    void copy_conf_file_to_test_dir(const char* dest_path, const char* conf_orig_path, const char* conf_dest_name);
    1.23 +        void copy_conf_file_to_test_dir(const char* dest_path, const char* conf_orig_path, const char* conf_dest_name);
    1.24 +        
    1.25 +        std::vector<std::pair<std::string, std::string>> gpgdir_fileadd_queue;
    1.26 +        std::vector<std::pair<std::string, std::string>> homedir_fileadd_queue;
    1.27 +        void add_file_to_gpg_dir_queue(std::string copy_from, std::string dst_fname);    
    1.28 +        void add_file_to_home_dir_queue(std::string copy_from, std::string dst_fname);
    1.29 +        void process_file_queue(std::string dirname, std::vector<std::pair<std::string, std::string>> file_queue);
    1.30 +
    1.31  };
    1.32  #endif
     2.1 --- a/test/include/GPGConfFixTests.h	Wed Aug 01 11:54:36 2018 +0200
     2.2 +++ b/test/include/GPGConfFixTests.h	Wed Aug 01 16:34:38 2018 +0200
     2.3 @@ -30,7 +30,8 @@
     2.4          void check_conf_fix_broken_agent_conf_old_db_3();
     2.5          void check_conf_fix_broken_agent_conf_old_db_4();
     2.6          void check_conf_fix_broken_agent_conf_old_db_5();
     2.7 -        void check_conf_fix_broken_agent_conf_old_db_6();                
     2.8 +        void check_conf_fix_broken_agent_conf_old_db_6();  
     2.9 +        void check_gpgconf_overwrite_0();              
    2.10  };
    2.11  
    2.12  #endif
     3.1 --- a/test/include/test_util.h	Wed Aug 01 11:54:36 2018 +0200
     3.2 +++ b/test/include/test_util.h	Wed Aug 01 16:34:38 2018 +0200
     3.3 @@ -11,6 +11,8 @@
     3.4  
     3.5  void test_init();
     3.6  
     3.7 +bool file_exists(std::string filename);
     3.8 +
     3.9  // string equality (case and non-case sensitive)
    3.10  bool _streq(const char* str1, const char* str2);
    3.11  bool _strceq(const char* str1, const char* str2);
     4.1 --- a/test/src/EngineTestSuite.cc	Wed Aug 01 11:54:36 2018 +0200
     4.2 +++ b/test/src/EngineTestSuite.cc	Wed Aug 01 16:34:38 2018 +0200
     4.3 @@ -10,6 +10,10 @@
     4.4  #include <sys/types.h>
     4.5  #include <sys/stat.h>
     4.6  
     4.7 +#import <string>
     4.8 +#import <vector>
     4.9 +#include <utility>
    4.10 +
    4.11  #include "platform_unix.h"
    4.12  
    4.13  #include "test_util.h"
    4.14 @@ -60,6 +64,27 @@
    4.15      dst.close();
    4.16  }
    4.17  
    4.18 +void EngineTestSuite::add_file_to_gpg_dir_queue(string copy_from, string dst_fname) {    
    4.19 +    gpgdir_fileadd_queue.push_back(make_pair(copy_from, dst_fname));
    4.20 +}
    4.21 +
    4.22 +void EngineTestSuite::add_file_to_home_dir_queue(string copy_from, string dst_fname) {
    4.23 +    homedir_fileadd_queue.push_back(make_pair(copy_from, dst_fname));
    4.24 +}
    4.25 +
    4.26 +void EngineTestSuite::process_file_queue(string dirname, vector<pair<string, string>> file_queue) {
    4.27 +    if (file_queue.empty())
    4.28 +        return;
    4.29 +        
    4.30 +    vector<pair<string, string>>::iterator it;
    4.31 +    
    4.32 +    for (it = file_queue.begin(); it != file_queue.end(); it++) {
    4.33 +        copy_conf_file_to_test_dir(dirname.c_str(), it->first.c_str(), it->second.c_str());
    4.34 +    }
    4.35 +    
    4.36 +    file_queue.clear();
    4.37 +}
    4.38 +
    4.39  void EngineTestSuite::set_full_env() {
    4.40      set_full_env(NULL, NULL, NULL);
    4.41  }
    4.42 @@ -145,10 +170,15 @@
    4.43      if (success != 0)
    4.44          throw std::runtime_error("SETUP: Cannot set test_home for init.");
    4.45  
    4.46 +    string tmp_gpg_dir = temp_test_home + "/.gnupg";
    4.47 +
    4.48 +    process_file_queue(tmp_gpg_dir, gpgdir_fileadd_queue);
    4.49 +    process_file_queue(temp_test_home, homedir_fileadd_queue);
    4.50 +
    4.51      if (gpg_conf_copy_path)
    4.52 -        copy_conf_file_to_test_dir((temp_test_home + "/.gnupg").c_str(), gpg_conf_copy_path, "gpg.conf");
    4.53 +        copy_conf_file_to_test_dir(tmp_gpg_dir.c_str(), gpg_conf_copy_path, "gpg.conf");
    4.54      if (gpg_agent_conf_file_copy_path)        
    4.55 -        copy_conf_file_to_test_dir((temp_test_home + "/.gnupg").c_str(), gpg_agent_conf_file_copy_path, "gpg-agent.conf");
    4.56 +        copy_conf_file_to_test_dir(tmp_gpg_dir.c_str(), gpg_agent_conf_file_copy_path, "gpg-agent.conf");
    4.57      if (db_conf_file_copy_path)
    4.58          copy_conf_file_to_test_dir(temp_test_home.c_str(), db_conf_file_copy_path, ".pEp_management.db");
    4.59          
    4.60 @@ -186,14 +216,15 @@
    4.61      unix_local_db(true);
    4.62      gpg_conf(true);
    4.63      gpg_agent_conf(true);
    4.64 -
    4.65 +    
    4.66  }
    4.67  
    4.68  void EngineTestSuite::setup() {
    4.69      on_test_number++;
    4.70  }
    4.71  
    4.72 -void EngineTestSuite::tear_down() {}
    4.73 +void EngineTestSuite::tear_down() {
    4.74 +}
    4.75  
    4.76  void EngineTestSuite::set_my_name() {
    4.77      my_name = typeid(*this).name();
     5.1 --- a/test/src/engine_tests/GPGConfFixTests.cc	Wed Aug 01 11:54:36 2018 +0200
     5.2 +++ b/test/src/engine_tests/GPGConfFixTests.cc	Wed Aug 01 16:34:38 2018 +0200
     5.3 @@ -8,6 +8,7 @@
     5.4  
     5.5  #include "pEpEngine.h"
     5.6  
     5.7 +#include "test_util.h"
     5.8  #include "EngineTestIndividualSuite.h"
     5.9  #include "GPGConfFixTests.h"
    5.10  
    5.11 @@ -43,7 +44,8 @@
    5.12                                                                        static_cast<Func>(&GPGConfFixTests::check_conf_fix_broken_agent_conf_old_db_5)));
    5.13      add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("GPGConfFixTests::check_conf_fix_broken_agent_conf_old_db_6"),
    5.14                                                                        static_cast<Func>(&GPGConfFixTests::check_conf_fix_broken_agent_conf_old_db_6)));                                                                      
    5.15 -                                                                      
    5.16 +    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("GPGConfFixTests::check_gpgconf_overwrite_0"),
    5.17 +                                                                      static_cast<Func>(&GPGConfFixTests::check_gpgconf_overwrite_0)));
    5.18  }
    5.19  
    5.20  void GPGConfFixTests::setup() {
    5.21 @@ -165,3 +167,11 @@
    5.22      set_full_env(NULL, "test_files/450_bad_gpgagent_conf_6", "test_files/427_old_db");        
    5.23      TEST_ASSERT(file_bytes_equal("test_files/450_good_gpgagent_conf_6", (temp_test_home + "/.gnupg/gpg-agent.conf").c_str()));    
    5.24  }
    5.25 +
    5.26 +void GPGConfFixTests::check_gpgconf_overwrite_0() {
    5.27 +    add_file_to_gpg_dir_queue("test_files/450_bad_gpgagent_conf_6", "gpg-agent.conf.0.pep.old");
    5.28 +    set_full_env(NULL, "test_files/450_bad_gpgagent_conf_6", "test_files/427_old_db");        
    5.29 +    TEST_ASSERT(file_bytes_equal("test_files/450_good_gpgagent_conf_6", (temp_test_home + "/.gnupg/gpg-agent.conf").c_str()));    
    5.30 +    TEST_ASSERT(file_exists(temp_test_home + "/.gnupg/gpg-agent.conf.1.pep.old"));
    5.31 +    TEST_ASSERT(!file_exists(temp_test_home + "/.gnupg/gpg-agent.conf.pep.new"));    
    5.32 +}
     6.1 --- a/test/src/util/test_util.cc	Wed Aug 01 11:54:36 2018 +0200
     6.2 +++ b/test/src/util/test_util.cc	Wed Aug 01 16:34:38 2018 +0200
     6.3 @@ -13,6 +13,11 @@
     6.4  #include <unistd.h>
     6.5  #include <ftw.h>
     6.6  
     6.7 +bool file_exists(std::string filename) {
     6.8 +    struct stat buffer;
     6.9 +    return (stat(filename.c_str(), &buffer) == 0);
    6.10 +}
    6.11 +
    6.12  char* str_to_lower(const char* str) {
    6.13      if (!str)
    6.14          return NULL;