commit for merge googletest
authorKrista 'DarthMama' Bennett <krista@pep.foundation>
Fri, 23 Aug 2019 09:02:09 +0200
branchgoogletest
changeset 401028bdbd0b27d0
parent 4004 ef7ff8fa3ead
child 4011 e8b3f8663f65
commit for merge
gtest/gtest_firstpass.py
gtest/include/Engine.h
gtest/src/Engine.cc
gtest/src/engine_tests/ExportKeyTest.cc
     1.1 --- a/gtest/gtest_firstpass.py	Tue Aug 20 16:29:30 2019 +0200
     1.2 +++ b/gtest/gtest_firstpass.py	Fri Aug 23 09:02:09 2019 +0200
     1.3 @@ -1,10 +1,17 @@
     1.4  import re 
     1.5  import sys
     1.6  
     1.7 +def tb(n):
     1.8 +    retval = ""
     1.9 +    for i in range(n):
    1.10 +        retval = retval + "    "
    1.11 +    return retval
    1.12 +        
    1.13  fixture_in = False 
    1.14  removing_old_constructor = False
    1.15  constructor_done = False
    1.16  modline = None
    1.17 +eat_next_line = False;
    1.18  filename = sys.argv[1]
    1.19  outfile = sys.argv[2]
    1.20  
    1.21 @@ -12,6 +19,9 @@
    1.22  
    1.23  with open(filename) as fp: 
    1.24      for line in fp:
    1.25 +        if (eat_next_line):
    1.26 +            eat_next_line = False;
    1.27 +            continue;
    1.28          line = line.rstrip();
    1.29          
    1.30          if not fixture_in:
    1.31 @@ -34,59 +44,76 @@
    1.32                      modline = re.sub(r'(.*)Tests::(.*)Tests\(string suitename, string test_home_dir\) :', r'\1Test', line)                
    1.33                      
    1.34                  if(modline == line):
    1.35 -                    newfile.write(line)
    1.36 +                    newfile.write(line + "\n")
    1.37                      modline = None
    1.38                      continue
    1.39                  else:
    1.40                      if not (constructor_done):
    1.41                          removing_old_constructor = True
    1.42                          continue    
    1.43 +                        
    1.44                      #*Tests::*Tests(string suitename, string test_home_dir)
    1.45                      # Put in fixture blob
    1.46                      # - delete through first }
    1.47                      #print(modline) 
    1.48 +                    newfile.write("#include \"Engine.h\"\n\n")                    
    1.49                      newfile.write("#include <gtest/gtest.h>\n\n\n")
    1.50                      newfile.write("namespace {\n\n\t//The fixture for " + modline + "\n")
    1.51 -                    newfile.write("\tclass " + modline + " public ::testing::Test {\n")
    1.52 -                    newfile.write("\t\tpublic:\n")
    1.53 -                    newfile.write("\t\t\tEngine engine;\n")
    1.54 -                    newfile.write("\t\t\tPEP_SESSION session;\n\n")
    1.55 -                    newfile.write("\t\tprotected:\n")
    1.56 -                    newfile.write("\t\t\t// You can remove any or all of the following functions if its body\n")
    1.57 -                    newfile.write("\t\t\t// is empty.\n")
    1.58 -                    newfile.write("\t\t\t" + modline + "() {\n")
    1.59 -                    newfile.write("\t\t\t\t// You can do set-up work for each test here.\n")
    1.60 -                    newfile.write("\t\t\t\ttest_suite_name = ::testing::UnitTest::GetInstance()->current_test_info()->test_suite_name();\n")
    1.61 -                    newfile.write("\t\t\t\ttest_name = ::testing::UnitTest::GetInstance()->current_test_info()->name();\n")
    1.62 -                    newfile.write("\t\t\t\t")
    1.63 -                    newfile.write("\t\t\t\t")
    1.64 -                    newfile.write("\t\t\t\t")
    1.65 -                    newfile.write("\t\t\t\t")                    
    1.66 -                    newfile.write("\t\t\t}\n\n")
    1.67 -                    newfile.write("\t\t\t~" + modline + "() override {\n")
    1.68 -                    newfile.write("\t\t\t\t// You can do clean-up work that doesn't throw exceptions here.\n")
    1.69 -                    newfile.write("\t\t\t}\n\n")
    1.70 -                    newfile.write("\t\t\t// If the constructor and destructor are not enough for setting up\n")
    1.71 -                    newfile.write("\t\t\t// and cleaning up each test, you can define the following methods:\n\n")
    1.72 -                    newfile.write("\t\t\tvoid SetUp() override {\n")
    1.73 -                    newfile.write("\t\t\t\t// Code here will be called immediately after the constructor (right\n")
    1.74 -                    newfile.write("\t\t\t\t// before each test).\n")
    1.75 -                    newfile.write("\t\t\t}\n\n")
    1.76 -                    newfile.write("\t\t\tvoid TearDown() override {\n")
    1.77 -                    newfile.write("\t\t\t\t// Code here will be called immediately after each test (right\n")
    1.78 -                    newfile.write("\t\t\t}\n\n")
    1.79 -                    newfile.write("\t\tprivate:\n");
    1.80 -                    newfile.write("\t\t\tconst char* test_suite_name;\n")
    1.81 -                    newfile.write("\t\t\tconst char* test_name;\n")                                        
    1.82 -                    newfile.write("\t\t\t// Objects declared here can be used by all tests in the " + modline + " suite.\n\n")
    1.83 -                    newfile.write("\t};\n\n")
    1.84 +                    newfile.write(tb(1) + "class " + modline + " : public ::testing::Test {\n")
    1.85 +                    newfile.write(tb(2) + "public:\n")
    1.86 +                    newfile.write(tb(3) + "Engine* engine;\n")
    1.87 +                    newfile.write(tb(3) + "PEP_SESSION session;\n\n")
    1.88 +                    newfile.write(tb(2) + "protected:\n")
    1.89 +                    newfile.write(tb(3) + "// You can remove any or all of the following functions if its body\n")
    1.90 +                    newfile.write(tb(3) + "// is empty.\n")
    1.91 +                    newfile.write(tb(3) + "" + modline + "() {\n")
    1.92 +                    newfile.write(tb(4) + "// You can do set-up work for each test here.\n")
    1.93 +                    newfile.write(tb(4) + "test_suite_name = ::testing::UnitTest::GetInstance()->current_test_info()->test_suite_name();\n")
    1.94 +                    newfile.write(tb(4) + "test_name = ::testing::UnitTest::GetInstance()->current_test_info()->name();\n")
    1.95 +                    newfile.write(tb(4) + "string test_path = get_main_test_home_dir() + \"/\" + test_suite_name + \"/\" + test_name;\n")
    1.96 +                    newfile.write(tb(3) + "}\n\n")
    1.97 +                    newfile.write(tb(3) + "~" + modline + "() override {\n")
    1.98 +                    newfile.write(tb(4) + "// You can do clean-up work that doesn't throw exceptions here.\n")
    1.99 +                    newfile.write(tb(3) + "}\n\n")
   1.100 +                    newfile.write(tb(3) + "// If the constructor and destructor are not enough for setting up\n")
   1.101 +                    newfile.write(tb(3) + "// and cleaning up each test, you can define the following methods:\n\n")
   1.102 +                    newfile.write(tb(3) + "void SetUp() override {\n")
   1.103 +                    newfile.write(tb(4) + "// Code here will be called immediately after the constructor (right\n")
   1.104 +                    newfile.write(tb(4) + "// before each test).\n")
   1.105 +                    newfile.write("\n" + tb(4) + "// Leave this empty if there are no files to copy to the home directory path\n")
   1.106 +                    newfile.write(tb(4) + "std::vector<std::pair<std::string, std::string>> init_files = std::vector<std::pair<std::string, std::string>>();\n")                                        
   1.107 +                    newfile.write("\n" + tb(4) + "// Get a new test Engine.\n")                    
   1.108 +                    newfile.write(tb(4) + "engine = new Engine(test_path);\n");
   1.109 +                    newfile.write(tb(4) + "ASSERT_NE(engine, nullptr);\n")
   1.110 +                    newfile.write("\n" + tb(4) + "// Ok, let's initialize test directories etc.\n")                                        
   1.111 +                    newfile.write(tb(4) + "engine->prep(NULL, NULL, init_files);\n")
   1.112 +                    newfile.write("\n" + tb(4) + "// Ok, try to start this bugger.\n")                    
   1.113 +                    newfile.write(tb(4) + "engine->start();\n")                    
   1.114 +                    newfile.write(tb(4) + "ASSERT_NE(engine->session, nullptr);\n")                    
   1.115 +                    newfile.write(tb(4) + "session = engine->session;\n") 
   1.116 +                    newfile.write("\n" + tb(4) + "// Engine is up. Keep on truckin\'\n");                                                            
   1.117 +                    newfile.write(tb(3) + "}\n\n")
   1.118 +                    newfile.write(tb(3) + "void TearDown() override {\n")
   1.119 +                    newfile.write(tb(4) + "// Code here will be called immediately after each test (right\n")
   1.120 +                    newfile.write(tb(4) + "// before the destructor).\n")   
   1.121 +                    newfile.write("\n" + tb(4) + "// While it would be nice to have this in the destructor, it can throw exceptions, so it's here.\n")                 
   1.122 +                    newfile.write(tb(4) + "engine->shut_down();\n")
   1.123 +                    newfile.write(tb(4) + "delete engine;\n")                    
   1.124 +                    newfile.write(tb(4) + "engine = NULL;\n")                    
   1.125 +                    newfile.write(tb(4) + "session = NULL;\n")                    
   1.126 +                    newfile.write(tb(3) + "}\n\n")
   1.127 +                    newfile.write(tb(2) + "private:\n");
   1.128 +                    newfile.write(tb(3) + "const char* test_suite_name;\n")
   1.129 +                    newfile.write(tb(3) + "const char* test_name;\n")                                        
   1.130 +                    newfile.write(tb(3) + "// Objects declared here can be used by all tests in the " + modline + " suite.\n\n")
   1.131 +                    newfile.write(tb(1) + "};\n\n")
   1.132                      newfile.write("}  // namespace\n\n\n")
   1.133  
   1.134                      fixture_in = True
   1.135          else:
   1.136              #void *Tests::check*() {
   1.137              # -> TEST_F(*Test, check*) {
   1.138 -            modline = re.sub(r'void\s*(.*)Tests::check(.*)\(\);', r'TEST_F(\1Test, check\2);', line)
   1.139 +            modline = re.sub(r'void\s*(.*)Tests::check(.*)\(\)\s*{', r'TEST_F(\1Test, check\2) {', line)
   1.140              if (line != modline):
   1.141                  newfile.write(modline + "\n")
   1.142                  continue
   1.143 @@ -215,8 +242,22 @@
   1.144              if (line != modline):
   1.145                  newfile.write(modline + "\n")
   1.146                  continue
   1.147 +
   1.148 +            #TEST_ASSERT_MSG(slurp_and_import_key(
   1.149 +            #TEST_ASSERT(slurp_and_import_key(
   1.150 +            # -> ASSERT_TRUE(slurp_and_import_key(
   1.151 +            modline = re.sub(r'TEST_ASSERT_MSG\(slurp_and_import_key', r'ASSERT_TRUE(slurp_and_import_key',line)
   1.152 +            if (line != modline):
   1.153 +                if not line.endswith(";"):
   1.154 +                    eat_next_line = True
   1.155 +                    modline = re.sub(r'\),', r'\)\);', modline);
   1.156 +                newfile.write(modline + "\n")
   1.157 +                continue
   1.158 +            modline = re.sub(r'TEST_ASSERT\(slurp_and_import_key', r'ASSERT_TRUE(slurp_and_import_key',line)
   1.159 +            if (line != modline):
   1.160 +                newfile.write(modline + "\n")
   1.161 +                continue
   1.162              
   1.163 -            # FIXME: either assume ptr or ask about it?
   1.164              #TEST_ASSERT_MSG(!<x>, *);
   1.165              #TEST_ASSERT(!<x>);
   1.166              # -> ASSERT_FALSE(<x>);
     2.1 --- a/gtest/include/Engine.h	Tue Aug 20 16:29:30 2019 +0200
     2.2 +++ b/gtest/include/Engine.h	Fri Aug 23 09:02:09 2019 +0200
     2.3 @@ -14,7 +14,7 @@
     2.4          Engine(string engine_home_dir);
     2.5          virtual ~Engine();
     2.6          
     2.7 -        void prep(string homedir, messageToSend_t mts, inject_sync_event_t ise, std::vector<std::pair<std::string, std::string>> init_files);
     2.8 +        void prep(messageToSend_t mts, inject_sync_event_t ise, std::vector<std::pair<std::string, std::string>> init_files);
     2.9          void start();
    2.10          void shut_down();
    2.11          
     3.1 --- a/gtest/src/Engine.cc	Tue Aug 20 16:29:30 2019 +0200
     3.2 +++ b/gtest/src/Engine.cc	Fri Aug 23 09:02:09 2019 +0200
     3.3 @@ -36,12 +36,11 @@
     3.4  
     3.5  Engine::~Engine() {}
     3.6  
     3.7 -void Engine::prep(string homedir, messageToSend_t mts, inject_sync_event_t ise, 
     3.8 +void Engine::prep(messageToSend_t mts, inject_sync_event_t ise, 
     3.9                    std::vector<std::pair<std::string, std::string>> init_files) {
    3.10 -    if (homedir.empty())
    3.11 +    if (engine_home.empty())
    3.12          throw std::runtime_error("Engine setup: BAD INITIALISATION. No test home.");
    3.13      
    3.14 -    engine_home = homedir;
    3.15      cached_messageToSend = mts;
    3.16      cached_inject_sync_event = ise;
    3.17  
     4.1 --- a/gtest/src/engine_tests/ExportKeyTest.cc	Tue Aug 20 16:29:30 2019 +0200
     4.2 +++ b/gtest/src/engine_tests/ExportKeyTest.cc	Fri Aug 23 09:02:09 2019 +0200
     4.3 @@ -1,12 +1,27 @@
     4.4 -// This file is under GNU General Public License 3.0// see LICENSE.txt#include <stdlib.h>#include <cstring>#include <string>#include "test_util.h"#include "pEpEngine.h"#include <gtest/gtest.h>
     4.5 +// This file is under GNU General Public License 3.0
     4.6 +// see LICENSE.txt
     4.7 +
     4.8 +#include <stdlib.h>
     4.9 +#include <cstring>
    4.10 +#include <string>
    4.11 +
    4.12 +#include "test_util.h"
    4.13 +
    4.14 +#include "pEpEngine.h"
    4.15 +
    4.16 +
    4.17 +
    4.18 +#include "Engine.h"
    4.19 +
    4.20 +#include <gtest/gtest.h>
    4.21  
    4.22  
    4.23  namespace {
    4.24  
    4.25 -    //The fixture for ExportKeyTest
    4.26 -    class ExportKeyTest public ::testing::Test {
    4.27 +	//The fixture for ExportKeyTest
    4.28 +    class ExportKeyTest : public ::testing::Test {
    4.29          public:
    4.30 -            Engine engine;
    4.31 +            Engine* engine;
    4.32              PEP_SESSION session;
    4.33  
    4.34          protected:
    4.35 @@ -14,6 +29,9 @@
    4.36              // is empty.
    4.37              ExportKeyTest() {
    4.38                  // You can do set-up work for each test here.
    4.39 +                test_suite_name = ::testing::UnitTest::GetInstance()->current_test_info()->test_suite_name();
    4.40 +                test_name = ::testing::UnitTest::GetInstance()->current_test_info()->name();
    4.41 +                string test_path = get_main_test_home_dir() + "/" + test_suite_name + "/" + test_name;
    4.42              }
    4.43  
    4.44              ~ExportKeyTest() override {
    4.45 @@ -26,19 +44,47 @@
    4.46              void SetUp() override {
    4.47                  // Code here will be called immediately after the constructor (right
    4.48                  // before each test).
    4.49 +
    4.50 +                // Leave this empty if there are no files to copy to the home directory path
    4.51 +                std::vector<std::pair<std::string, std::string>> init_files = std::vector<std::pair<std::string, std::string>>();
    4.52 +
    4.53 +                // Get a new test Engine.
    4.54 +                engine = new Engine(test_path);
    4.55 +                ASSERT_NE(engine, nullptr);
    4.56 +
    4.57 +                // Ok, let's initialize test directories etc.
    4.58 +                engine->prep(NULL, NULL, init_files);
    4.59 +
    4.60 +                // Ok, try to start this bugger.
    4.61 +                engine->start();
    4.62 +                ASSERT_NE(engine->session, nullptr);
    4.63 +                session = engine->session;
    4.64 +
    4.65 +                // Engine is up. Keep on truckin'
    4.66              }
    4.67  
    4.68              void TearDown() override {
    4.69                  // Code here will be called immediately after each test (right
    4.70 +                // before the destructor).
    4.71 +
    4.72 +                // While it would be nice to have this in the destructor, it can throw exceptions, so it's here.
    4.73 +                engine->shut_down();
    4.74 +                delete engine;
    4.75 +                engine = NULL;
    4.76 +                session = NULL;
    4.77              }
    4.78  
    4.79 +        private:
    4.80 +            const char* test_suite_name;
    4.81 +            const char* test_name;
    4.82              // Objects declared here can be used by all tests in the ExportKeyTest suite.
    4.83 +
    4.84      };
    4.85  
    4.86  }  // namespace
    4.87  
    4.88  
    4.89 -void ExportKeyTests::check_export_key_no_key() {
    4.90 +TEST_F(ExportKeyTest, check_export_key_no_key) {
    4.91      char* keydata = NULL;
    4.92      size_t keysize = 0;
    4.93      PEP_STATUS status = export_key(session, "BFCDB7F301DEEEBBF947F29659BFF488C9C2EE39",
    4.94 @@ -54,10 +100,9 @@
    4.95  
    4.96  }
    4.97  
    4.98 -void ExportKeyTests::check_export_key_pubkey() {
    4.99 +TEST_F(ExportKeyTest, check_export_key_pubkey) {
   4.100      // Own pub key
   4.101 -    TEST_ASSERT_MSG(slurp_and_import_key(session, "test_keys/pub/pep-test-bob-0xC9C2EE39_pub.asc"),
   4.102 -                    "Unable to import test_keys/pub/pep-test-bob-0xC9C2EE39_pub.asc");
   4.103 +    ASSERT_TRUE(slurp_and_import_key(session, "test_keys/pub/pep-test-bob-0xC9C2EE39_pub.asc"\)\);
   4.104  
   4.105      char* keydata = NULL;
   4.106      size_t keysize = 0;
   4.107 @@ -76,11 +121,9 @@
   4.108      free(keydata);
   4.109  }
   4.110  
   4.111 -void ExportKeyTests::check_export_key_secret_key() {
   4.112 -    TEST_ASSERT_MSG(slurp_and_import_key(session, "test_keys/pub/pep-test-bob-0xC9C2EE39_pub.asc"),
   4.113 -                    "Unable to import test_keys/pub/pep-test-bob-0xC9C2EE39_pub.asc");
   4.114 -    TEST_ASSERT_MSG(slurp_and_import_key(session, "test_keys/priv/pep-test-bob-0xC9C2EE39_priv.asc"),
   4.115 -                    "Unable to import test_keys/priv/pep-test-bob-0xC9C2EE39_priv.asc");
   4.116 +TEST_F(ExportKeyTest, check_export_key_secret_key) {
   4.117 +    ASSERT_TRUE(slurp_and_import_key(session, "test_keys/pub/pep-test-bob-0xC9C2EE39_pub.asc"\)\);
   4.118 +    ASSERT_TRUE(slurp_and_import_key(session, "test_keys/priv/pep-test-bob-0xC9C2EE39_priv.asc"\)\);
   4.119      char* keydata = NULL;
   4.120      size_t keysize = 0;
   4.121      stringlist_t* keylist = NULL;
   4.122 @@ -111,10 +154,9 @@
   4.123  }
   4.124  
   4.125  
   4.126 -void ExportKeyTests::check_export_key_no_secret_key() {
   4.127 +TEST_F(ExportKeyTest, check_export_key_no_secret_key) {
   4.128      // Own pub key
   4.129 -    TEST_ASSERT_MSG(slurp_and_import_key(session, "test_keys/pub/pep-test-bob-0xC9C2EE39_pub.asc"),
   4.130 -                    "Unable to import test_keys/pub/pep-test-bob-0xC9C2EE39_pub.asc");
   4.131 +    ASSERT_TRUE(slurp_and_import_key(session, "test_keys/pub/pep-test-bob-0xC9C2EE39_pub.asc"\)\);
   4.132  
   4.133      char* keydata = NULL;
   4.134      size_t keysize = 0;