Existing tests now work with 'device' model - needs some refactoring, but moving on sync_test_refactor
authorKrista 'DarthMama' Bennett <krista@pep.foundation>
Tue, 12 Feb 2019 14:55:17 +0100
branchsync_test_refactor
changeset 3296fe7cbfc8e84f
parent 3295 1a12a24ff0e6
child 3299 f15bf275c50f
Existing tests now work with 'device' model - needs some refactoring, but moving on
test/include/EngineTestSuite.h
test/include/KeyResetMessageTests.h
test/include/pEpTestDevice.h
test/src/EngineTestSuite.cc
test/src/engine_tests/KeyResetMessageTests.cc
test/src/pEpTestDevice.cc
     1.1 --- a/test/include/EngineTestSuite.h	Tue Feb 12 12:14:45 2019 +0100
     1.2 +++ b/test/include/EngineTestSuite.h	Tue Feb 12 14:55:17 2019 +0100
     1.3 @@ -35,9 +35,6 @@
     1.4          
     1.5          string current_test_name;
     1.6  
     1.7 -        messageToSend_t cached_messageToSend;
     1.8 -        inject_sync_event_t cached_inject_sync_event;
     1.9 -
    1.10  #define TEST_FAILED_MESSAGE_BUFSIZE 1000        
    1.11          char failed_msg_buf[TEST_FAILED_MESSAGE_BUFSIZE];
    1.12          
     2.1 --- a/test/include/KeyResetMessageTests.h	Tue Feb 12 12:14:45 2019 +0100
     2.2 +++ b/test/include/KeyResetMessageTests.h	Tue Feb 12 14:55:17 2019 +0100
     2.3 @@ -35,6 +35,7 @@
     2.4  
     2.5      protected:
     2.6          void setup();
     2.7 +        void tear_down();
     2.8                  
     2.9      private:
    2.10          void check_key_reset_message();        
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/test/include/pEpTestDevice.h	Tue Feb 12 14:55:17 2019 +0100
     3.3 @@ -0,0 +1,49 @@
     3.4 +#ifndef PEP_TEST_DEVICE_H
     3.5 +#define PEP_TEST_DEVICE_H
     3.6 +
     3.7 +#include <cpptest.h>
     3.8 +#include <string>
     3.9 +#include <map>
    3.10 +#include <vector>
    3.11 +#include <utility>
    3.12 +#include "pEpEngine.h"
    3.13 +
    3.14 +using namespace std;
    3.15 +
    3.16 +class pEpTestDevice {
    3.17 +    public:
    3.18 +        pEpTestDevice(string test_dir, string my_name,
    3.19 +                      messageToSend_t mess_send_func = NULL,
    3.20 +                      inject_sync_event_t inject_sync_ev_func = NULL);        
    3.21 +                      
    3.22 +        virtual ~pEpTestDevice();
    3.23 +        string device_name;
    3.24 +        PEP_SESSION session;
    3.25 +
    3.26 +        messageToSend_t device_messageToSend;
    3.27 +        inject_sync_event_t device_inject_sync_event;
    3.28 +        
    3.29 +    protected:
    3.30 +        string device_dir;        
    3.31 +        string root_test_dir;
    3.32 +        
    3.33 +//        string current_test_name;
    3.34 +
    3.35 +        void set_device_environment();
    3.36 +        void teardown_device_environment();
    3.37 +        void grab_context();
    3.38 +        
    3.39 +//        void set_full_env();
    3.40 +//        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);
    3.41 +//        void restore_full_env();
    3.42 +//        void initialise_test_home();
    3.43 +	
    3.44 +        // std::vector<std::pair<std::string, std::string>> gpgdir_fileadd_queue;
    3.45 +        // std::vector<std::pair<std::string, std::string>> homedir_fileadd_queue;
    3.46 +        // void add_file_to_gpg_dir_queue(std::string copy_from, std::string dst_fname);    
    3.47 +        // void add_file_to_home_dir_queue(std::string copy_from, std::string dst_fname);
    3.48 +        // void process_file_queue(std::string dirname, std::vector<std::pair<std::string, std::string>> file_queue);
    3.49 +
    3.50 +};
    3.51 +
    3.52 +#endif
     4.1 --- a/test/src/EngineTestSuite.cc	Tue Feb 12 12:14:45 2019 +0100
     4.2 +++ b/test/src/EngineTestSuite.cc	Tue Feb 12 14:55:17 2019 +0100
     4.3 @@ -34,8 +34,6 @@
     4.4      number_of_tests = 0;
     4.5      on_test_number = 0;
     4.6      real_home = getenv("HOME");
     4.7 -    cached_messageToSend = NULL;
     4.8 -    cached_inject_sync_event = NULL;
     4.9      session = NULL;
    4.10      device = NULL;
    4.11      make_device = make_default_device;
     5.1 --- a/test/src/engine_tests/KeyResetMessageTests.cc	Tue Feb 12 12:14:45 2019 +0100
     5.2 +++ b/test/src/engine_tests/KeyResetMessageTests.cc	Tue Feb 12 14:55:17 2019 +0100
     5.3 @@ -46,8 +46,6 @@
     5.4                                                                        static_cast<Func>(&KeyResetMessageTests::check_multiple_resets_single_key)));                                                                      
     5.5                                                                        
     5.6      fake_this = this;                                                                  
     5.7 -    
     5.8 -    cached_messageToSend = &KeyResetMessageTests::message_send_callback;
     5.9  }
    5.10  
    5.11  PEP_STATUS KeyResetMessageTests::message_send_callback(message* msg) {
    5.12 @@ -58,6 +56,14 @@
    5.13  void KeyResetMessageTests::setup() {
    5.14      EngineTestIndividualSuite::setup();
    5.15      m_queue.clear();
    5.16 +    device->device_messageToSend = &KeyResetMessageTests::message_send_callback;
    5.17 +    session->messageToSend = device->device_messageToSend;
    5.18 +}
    5.19 +
    5.20 +void KeyResetMessageTests::tear_down() {
    5.21 +    device->device_messageToSend = NULL;
    5.22 +    session->messageToSend = NULL;
    5.23 +    EngineTestIndividualSuite::tear_down();
    5.24  }
    5.25  
    5.26  void KeyResetMessageTests::send_setup() {
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/test/src/pEpTestDevice.cc	Tue Feb 12 14:55:17 2019 +0100
     6.3 @@ -0,0 +1,153 @@
     6.4 +#include <stdlib.h>
     6.5 +#include <sys/stat.h>
     6.6 +#include <errno.h>
     6.7 +#include <stdlib.h>
     6.8 +#include <unistd.h>
     6.9 +#include <ftw.h>
    6.10 +#include <assert.h>
    6.11 +#include <fstream>
    6.12 +#include <iostream>
    6.13 +#include <sys/types.h>
    6.14 +#include <sys/stat.h>
    6.15 +
    6.16 +#include <string>
    6.17 +#include <vector>
    6.18 +#include <utility>
    6.19 +
    6.20 +#include "platform_unix.h"
    6.21 +
    6.22 +#include "TestUtils.h"
    6.23 +#include "pEpTestDevice.h"
    6.24 +#include "pEpTestStatic.h"
    6.25 +#include <algorithm>
    6.26 +#include "TestConstants.h"
    6.27 +
    6.28 +pEpTestDevice::pEpTestDevice(string test_dir, 
    6.29 +                             string my_name,
    6.30 +                             messageToSend_t mess_send_func,
    6.31 +                             inject_sync_event_t inject_sync_ev_func
    6.32 +                             )                
    6.33 +{
    6.34 +    root_test_dir = test_dir;
    6.35 +    // FIXME: do we have to worry about dirlen now?
    6.36 +    device_dir = test_dir + "/" + my_name;
    6.37 +    device_name = my_name;
    6.38 +    device_messageToSend = mess_send_func;
    6.39 +    device_inject_sync_event = inject_sync_ev_func;
    6.40 +    
    6.41 +    set_device_environment();    
    6.42 +}
    6.43 +
    6.44 +pEpTestDevice::~pEpTestDevice() {
    6.45 +    teardown_device_environment();
    6.46 +}
    6.47 +
    6.48 +void pEpTestDevice::set_device_environment() {
    6.49 +    int success = 0;
    6.50 +    struct stat dirchk;
    6.51 +    
    6.52 +//    set_my_name();
    6.53 +
    6.54 +// FIXME
    6.55 +#ifndef USE_NETPGP
    6.56 +    success = system("gpgconf --kill all");
    6.57 +    if (success != 0)
    6.58 +        throw std::runtime_error("SETUP: Error when executing 'gpgconf --kill all'.");    
    6.59 +#endif
    6.60 +
    6.61 +    if (stat(device_dir.c_str(), &dirchk) == 0) {
    6.62 +        if (!S_ISDIR(dirchk.st_mode))
    6.63 +            throw std::runtime_error(("The test directory, " + device_dir + "exists, but is not a directory.").c_str()); 
    6.64 +                    
    6.65 +        struct stat buf;
    6.66 +
    6.67 +        if (stat(device_dir.c_str(), &buf) == 0) {
    6.68 +            int success = nftw((device_dir + "/.").c_str(), util_delete_filepath, 100, FTW_DEPTH);
    6.69 +        }
    6.70 +    }
    6.71 +    else {
    6.72 +        int errchk = mkdir(device_dir.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
    6.73 +        cout << errchk << endl;
    6.74 +        if (errchk != 0)
    6.75 +            throw std::runtime_error("Error creating a test directory.");
    6.76 +    }
    6.77 +        
    6.78 +    struct stat buf;
    6.79 +        
    6.80 +    success = setenv("GNUPGHOME", (device_dir + "/gnupg").c_str(), 1);
    6.81 +
    6.82 +    if (success != 0)
    6.83 +        throw std::runtime_error("SETUP: Error when setting GNUPGHOME.");
    6.84 +
    6.85 +    cout << "New GNUPGHOME is " << getenv("GNUPGHOME") << endl << endl;
    6.86 +        
    6.87 +    success = setenv("HOME", device_dir.c_str(), 1);
    6.88 +    if (success != 0)
    6.89 +        throw std::runtime_error("SETUP: Cannot set copy_fil for init.");
    6.90 +            
    6.91 +    unix_local_db(true);
    6.92 +    gpg_conf(true);
    6.93 +    gpg_agent_conf(true);
    6.94 +        
    6.95 +    PEP_STATUS status = init(&session, device_messageToSend, device_inject_sync_event);
    6.96 +
    6.97 +#ifndef USE_NETPGP            
    6.98 +    success = system("gpgconf --create-socketdir");
    6.99 +    if (success != 0)
   6.100 +        throw std::runtime_error("RESTORE: Error when executing 'gpgconf --create-socketdir'.");        
   6.101 +    system("gpg-connect-agent /bye");   // Just in case - otherwise, we die on MacOS sometimes. Is this enough??
   6.102 +#endif
   6.103 +
   6.104 +    assert(status == PEP_STATUS_OK);
   6.105 +    assert(session);
   6.106 +}
   6.107 +
   6.108 +void pEpTestDevice::grab_context() {
   6.109 +    int success = system("gpgconf --kill all");
   6.110 +    if (success != 0)
   6.111 +        throw std::runtime_error("SETUP: Error when executing 'gpgconf --kill all'.");    
   6.112 +    struct stat buf;
   6.113 +
   6.114 +    success = system("gpgconf --remove-socketdir");            
   6.115 +    if (success != 0)
   6.116 +        throw std::runtime_error("RESTORE: Error when executing 'gpgconf --remove-socketdir'.");    
   6.117 +        
   6.118 +    success = setenv("GNUPGHOME", (device_dir + "/gnupg").c_str(), 1);
   6.119 +
   6.120 +    if (success != 0)
   6.121 +        throw std::runtime_error("SETUP: Error when setting GNUPGHOME.");
   6.122 +
   6.123 +    cout << "New GNUPGHOME is " << getenv("GNUPGHOME") << endl << endl;
   6.124 +
   6.125 +    success = setenv("HOME", device_dir.c_str(), 1);
   6.126 +    if (success != 0)
   6.127 +        throw std::runtime_error("SETUP: Cannot set device_dir for init.");
   6.128 +            
   6.129 +#ifndef USE_NETPGP            
   6.130 +    success = system("gpgconf --create-socketdir");
   6.131 +    if (success != 0)
   6.132 +        throw std::runtime_error("RESTORE: Error when executing 'gpgconf --create-socketdir'.");        
   6.133 +    system("gpg-connect-agent /bye");   // Just in case - otherwise, we die on MacOS sometimes. Is this enough??
   6.134 +#endif
   6.135 +}
   6.136 +
   6.137 +void pEpTestDevice::teardown_device_environment() {
   6.138 +    release(session);
   6.139 +    session = NULL;
   6.140 +        
   6.141 +    int success = 0;    
   6.142 +
   6.143 +#ifndef USE_NETPGP        
   6.144 +    success = system("gpgconf --kill all");
   6.145 +    if (success != 0)
   6.146 +        throw std::runtime_error("RESTORE: Error when executing 'gpgconf --kill all'.");
   6.147 +    success = system("gpgconf --remove-socketdir");            
   6.148 +    if (success != 0)
   6.149 +        throw std::runtime_error("RESTORE: Error when executing 'gpgconf --remove-socketdir'.");    
   6.150 +#endif
   6.151 +
   6.152 +    // FIXME: Remove homedir
   6.153 +    unix_local_db(true);
   6.154 +    gpg_conf(true);
   6.155 +    gpg_agent_conf(true);
   6.156 +}