ENGINE-434 - Tests work now individually (yay!) and down to the expected bug, I think. Added a debug option to some internal functions with static variables inside of them so we can test. This only applies to unix and when NDEBUG is not defined. ENGINE-434
authorKrista Bennett <krista@pep-project.org>
Tue, 22 May 2018 12:56:27 +0200
branchENGINE-434
changeset 2684a5175dd76542
parent 2680 b6f5a8fb2219
child 2685 01edd04b3c38
ENGINE-434 - Tests work now individually (yay!) and down to the expected bug, I think. Added a debug option to some internal functions with static variables inside of them so we can test. This only applies to unix and when NDEBUG is not defined.
src/pEp_internal.h
src/pgp_gpg.c
src/pgp_netpgp.c
src/platform_unix.c
src/platform_unix.h
test/src/EngineTestIndividualSuite.cc
test/src/EngineTestSuite.cc
test/src/engine_tests/IntegrityTests.cc
     1.1 --- a/src/pEp_internal.h	Tue May 22 09:35:15 2018 +0200
     1.2 +++ b/src/pEp_internal.h	Tue May 22 12:56:27 2018 +0200
     1.3 @@ -61,7 +61,11 @@
     1.4  #else // UNIX
     1.5  #define _POSIX_C_SOURCE 200809L
     1.6  #include <dlfcn.h>
     1.7 +#ifdef NDEBUG
     1.8  #define LOCAL_DB unix_local_db()
     1.9 +#else
    1.10 +#define LOCAL_DB unix_local_db(false)
    1.11 +#endif
    1.12  #ifndef SYSTEM_DB
    1.13  #define SYSTEM_DB "/usr/share/pEp/system.db"
    1.14  #endif
     2.1 --- a/src/pgp_gpg.c	Tue May 22 09:35:15 2018 +0200
     2.2 +++ b/src/pgp_gpg.c	Tue May 22 12:56:27 2018 +0200
     2.3 @@ -254,8 +254,11 @@
     2.4          stringlist_add(conf_keys, "allow-freeform-uid");
     2.5          stringlist_add(conf_values, "");
     2.6  
     2.7 +#if defined(WIN32) || defined(NDEBUG)
     2.8          bResult = ensure_config_values(conf_keys, conf_values, gpg_conf());
     2.9 -
    2.10 +#else
    2.11 +        bResult = ensure_config_values(conf_keys, conf_values, gpg_conf(false));
    2.12 +#endif
    2.13          free_stringlist(conf_keys);
    2.14          free_stringlist(conf_values);
    2.15  
    2.16 @@ -271,8 +274,11 @@
    2.17          stringlist_add(conf_keys, "max-cache-ttl");
    2.18          stringlist_add(conf_values, "1200");
    2.19  
    2.20 +#if defined(WIN32) || defined(NDEBUG)
    2.21          bResult = ensure_config_values(conf_keys, conf_values, gpg_agent_conf());
    2.22 -
    2.23 +#else        
    2.24 +        bResult = ensure_config_values(conf_keys, conf_values, gpg_agent_conf(false));
    2.25 +#endif
    2.26          free_stringlist(conf_keys);
    2.27          free_stringlist(conf_values);
    2.28  
     3.1 --- a/src/pgp_netpgp.c	Tue May 22 09:35:15 2018 +0200
     3.2 +++ b/src/pgp_netpgp.c	Tue May 22 12:56:27 2018 +0200
     3.3 @@ -46,7 +46,11 @@
     3.4      // netpgp_setvar(&netpgp, "need userid", "1");
     3.5  
     3.6      // NetPGP shares home with GPG
     3.7 +#if defined(WIN32) || defined(NDEBUG)
     3.8      home = gpg_home();
     3.9 +#else
    3.10 +    home = gpg_home(false);
    3.11 +#endif
    3.12      if(home){
    3.13          netpgp_set_homedir(&netpgp,(char*)home, NULL, 0);
    3.14      }else{
     4.1 --- a/src/platform_unix.c	Tue May 22 09:35:15 2018 +0200
     4.2 +++ b/src/platform_unix.c	Tue May 22 12:56:27 2018 +0200
     4.3 @@ -208,12 +208,21 @@
     4.4  
     4.5  #endif
     4.6  
     4.7 +#ifdef NDEBUG
     4.8  const char *unix_local_db(void)
     4.9 +#else
    4.10 +const char *unix_local_db(int reset)
    4.11 +#endif
    4.12  {
    4.13      static char buffer[MAX_PATH];
    4.14      static bool done = false;
    4.15  
    4.16 -    if (!done) {
    4.17 +    #ifdef NDEBUG
    4.18 +    if (!done)
    4.19 +    #else
    4.20 +    if ((!done) || reset)
    4.21 +    #endif
    4.22 +    {
    4.23          char *home_env;
    4.24          if((home_env = getenv("HOME"))){
    4.25              char *p = stpncpy(buffer, home_env, MAX_PATH);
    4.26 @@ -240,12 +249,20 @@
    4.27  static const char *gpg_agent_conf_name = "gpg-agent.conf";
    4.28  static const char *gpg_conf_empty = "# Created by pEpEngine\n";
    4.29  
    4.30 +#ifdef NDEBUG
    4.31  static bool ensure_gpg_home(const char **conf, const char **home){
    4.32 +#else
    4.33 +static bool ensure_gpg_home(const char **conf, const char **home, int reset){
    4.34 +#endif    
    4.35      static char path[MAX_PATH];
    4.36      static char dirname[MAX_PATH];
    4.37      static bool done = false;
    4.38  
    4.39 +#ifdef NDEBUG
    4.40      if (!done) {
    4.41 +#else
    4.42 +    if (reset || !done) {
    4.43 +#endif        
    4.44          char *p;
    4.45          ssize_t len;
    4.46          char *gpg_home_env = getenv("GNUPGHOME");
    4.47 @@ -317,15 +334,27 @@
    4.48      return true;
    4.49  }
    4.50  
    4.51 +#ifdef NDEBUG
    4.52  static bool ensure_gpg_agent_conf(const char **agent_conf){
    4.53 +#else
    4.54 +static bool ensure_gpg_agent_conf(const char **agent_conf, int reset){    
    4.55 +#endif    
    4.56      static char agent_path[MAX_PATH];
    4.57      static bool done = false;
    4.58  
    4.59 +#ifdef NDEBUG
    4.60      if (!done) {
    4.61          const char *dirname;
    4.62  
    4.63          if (!ensure_gpg_home(NULL, &dirname)) /* Then dirname won't be set. */
    4.64              return false;
    4.65 +#else
    4.66 +    if (reset || !done) {
    4.67 +        const char *dirname;
    4.68 +
    4.69 +        if (!ensure_gpg_home(NULL, &dirname, reset)) /* Then dirname won't be set. */
    4.70 +            return false;
    4.71 +#endif
    4.72  
    4.73          char *p = stpncpy(agent_path, dirname, MAX_PATH);
    4.74          
    4.75 @@ -367,6 +396,7 @@
    4.76      return true;
    4.77  }
    4.78  
    4.79 +#ifdef NDEBUG
    4.80  const char *gpg_conf(void)
    4.81  {
    4.82      const char *conf;
    4.83 @@ -374,7 +404,17 @@
    4.84          return conf;
    4.85      return NULL;
    4.86  }
    4.87 +#else
    4.88 +const char *gpg_conf(int reset)
    4.89 +{
    4.90 +    const char *conf;
    4.91 +    if(ensure_gpg_home(&conf, NULL, reset))
    4.92 +        return conf;
    4.93 +    return NULL;
    4.94 +}
    4.95 +#endif
    4.96  
    4.97 +#ifdef NDEBUG
    4.98  const char *gpg_home(void)
    4.99  {
   4.100      const char *home;
   4.101 @@ -382,7 +422,17 @@
   4.102          return home;
   4.103      return NULL;
   4.104  }
   4.105 +#else
   4.106 +const char *gpg_home(int reset)
   4.107 +{
   4.108 +    const char *home;
   4.109 +    if(ensure_gpg_home(NULL, &home, reset))
   4.110 +        return home;
   4.111 +    return NULL;
   4.112 +}
   4.113 +#endif
   4.114  
   4.115 +#ifdef NDEBUG
   4.116  const char *gpg_agent_conf(void)
   4.117  {
   4.118      const char *agent_conf;
   4.119 @@ -390,3 +440,12 @@
   4.120          return agent_conf;
   4.121      return NULL;
   4.122  }
   4.123 +#else
   4.124 +const char *gpg_agent_conf(int reset)
   4.125 +{
   4.126 +    const char *agent_conf;
   4.127 +    if(ensure_gpg_agent_conf(&agent_conf, reset))
   4.128 +        return agent_conf;
   4.129 +    return NULL;
   4.130 +}
   4.131 +#endif
   4.132 \ No newline at end of file
     5.1 --- a/src/platform_unix.h	Tue May 22 09:35:15 2018 +0200
     5.2 +++ b/src/platform_unix.h	Tue May 22 12:56:27 2018 +0200
     5.3 @@ -20,11 +20,18 @@
     5.4  extern "C" {
     5.5  #endif
     5.6  
     5.7 +#ifdef NDEBUG
     5.8  const char *unix_local_db(void);
     5.9 -
    5.10  const char *gpg_conf(void);
    5.11  const char *gpg_agent_conf(void);
    5.12  const char *gpg_home(void);
    5.13 +#else
    5.14 +const char *unix_local_db(int reset);
    5.15 +const char *gpg_conf(int reset);
    5.16 +const char *gpg_agent_conf(int reset);
    5.17 +const char *gpg_home(int reset);
    5.18 +#endif
    5.19 +
    5.20  
    5.21  #ifdef ANDROID
    5.22  
     6.1 --- a/test/src/EngineTestIndividualSuite.cc	Tue May 22 09:35:15 2018 +0200
     6.2 +++ b/test/src/EngineTestIndividualSuite.cc	Tue May 22 12:56:27 2018 +0200
     6.3 @@ -25,7 +25,6 @@
     6.4  }
     6.5  
     6.6  void EngineTestIndividualSuite::tear_down() {
     6.7 -    release(session);
     6.8      restore_full_env();
     6.9      EngineTestSuite::tear_down();
    6.10  }
     7.1 --- a/test/src/EngineTestSuite.cc	Tue May 22 09:35:15 2018 +0200
     7.2 +++ b/test/src/EngineTestSuite.cc	Tue May 22 12:56:27 2018 +0200
     7.3 @@ -6,6 +6,8 @@
     7.4  #include <ftw.h>
     7.5  #include <assert.h>
     7.6  
     7.7 +#include "platform_unix.h"
     7.8 +
     7.9  #include "test_util.h"
    7.10  #include "EngineTestSuite.h"
    7.11  using namespace std;
    7.12 @@ -30,6 +32,12 @@
    7.13  void EngineTestSuite::set_full_env() {
    7.14      int success = 0;
    7.15      struct stat dirchk;
    7.16 +    
    7.17 +    success = system("gpgconf --kill all");
    7.18 +    if (success != 0)
    7.19 +        throw std::runtime_error("SETUP: Error when executing 'gpgconf --kill all'.");
    7.20 +    sleep(1); // hopefully enough time for the system to recognise that it is dead. *sigh*    
    7.21 +
    7.22      if (stat(test_home.c_str(), &dirchk) == 0) {
    7.23          if (!S_ISDIR(dirchk.st_mode))
    7.24              throw std::runtime_error(("The test directory, " + test_home + "exists, but is not a directory.").c_str()); 
    7.25 @@ -63,7 +71,7 @@
    7.26          
    7.27      if (temp_test_home.empty())
    7.28          throw std::runtime_error("SETUP: BAD INITIALISATION. No test home.");
    7.29 -    
    7.30 +
    7.31      
    7.32      string home = getenv("HOME");
    7.33      if (temp_test_home.compare(home) == 0 || temp_test_home.compare(home + "/") == 0 ||
    7.34 @@ -82,26 +90,26 @@
    7.35  
    7.36      cout << "New GNUPGHOME is " << getenv("GNUPGHOME") << endl;
    7.37      
    7.38 -    success = system("gpgconf --kill all");
    7.39 -    if (success != 0)
    7.40 -        throw std::runtime_error("SETUP: Error when executing 'gpgconf --kill all'.");
    7.41 -    sleep(1); // hopefully enough time for the system to recognise that it is dead. *sigh*
    7.42 -
    7.43 -    
    7.44      success = setenv("HOME", temp_test_home.c_str(), 1);
    7.45      if (success != 0)
    7.46          throw std::runtime_error("SETUP: Cannot set test_home for init.");
    7.47      
    7.48 +    const char* new_home_db = unix_local_db(true);
    7.49 +    gpg_conf(true);
    7.50 +    gpg_agent_conf(true);
    7.51 +    
    7.52  //    cout << "calling init()\n";
    7.53      PEP_STATUS status = init(&session);
    7.54 -    // assert(status == PEP_STATUS_OK);
    7.55 +    assert(status == PEP_STATUS_OK);
    7.56      assert(session);
    7.57  //    cout << "init() completed.\n";
    7.58  
    7.59  }
    7.60  
    7.61  void EngineTestSuite::restore_full_env() {
    7.62 -            
    7.63 +    release(session);
    7.64 +    session = NULL;
    7.65 +        
    7.66      int success = system("gpgconf --kill all");
    7.67      if (success != 0)
    7.68          throw std::runtime_error("RESTORE: Error when executing 'gpgconf --kill all'.");
     8.1 --- a/test/src/engine_tests/IntegrityTests.cc	Tue May 22 09:35:15 2018 +0200
     8.2 +++ b/test/src/engine_tests/IntegrityTests.cc	Tue May 22 12:56:27 2018 +0200
     8.3 @@ -56,13 +56,13 @@
     8.4      assert(status == PEP_STATUS_OK);
     8.5      recip_key = "";
     8.6      string priv_key = slurp("test_keys/priv/integrity_test_recip_0-0xF390A40D_priv.asc");
     8.7 -    cout << priv_key << endl;
     8.8 -    cout << "GNUPGHOME is " << getenv("GNUPGHOME") << endl;
     8.9 +    // cout << priv_key << endl;
    8.10 +    // cout << "GNUPGHOME is " << getenv("GNUPGHOME") << endl;
    8.11      status = import_key(session, priv_key.c_str(), priv_key.size(), NULL);
    8.12      assert(status == PEP_STATUS_OK);
    8.13 -    stringlist_t* debug_keylist = NULL;
    8.14 -    status = find_private_keys(session, recip_fpr, &debug_keylist);
    8.15 -    assert(debug_keylist);
    8.16 +    // stringlist_t* debug_keylist = NULL;
    8.17 +    // status = find_private_keys(session, recip_fpr, &debug_keylist);
    8.18 +    // assert(debug_keylist);
    8.19      
    8.20      pEp_identity* me = new_identity("integrity_test_recip@darthmama.org", recip_fpr, PEP_OWN_USERID, "Integrity Test Recipient");
    8.21      assert(me != NULL);