test_base_class.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * test_base_class.cpp
  3. *
  4. * Copyright (C) 2023 charlie-foxtrot
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <dirent.h>
  20. #include "logging.h"
  21. #include "test_base_class.h"
  22. using namespace std;
  23. void delete_directory(const string& root) {
  24. DIR* dp = NULL;
  25. dp = opendir(root.c_str());
  26. if (dp == NULL) {
  27. cerr << "Error opening directory " << root << endl;
  28. return;
  29. }
  30. string current_dir = ".";
  31. string parent_dir = "..";
  32. struct dirent* entry = NULL;
  33. while ((entry = readdir(dp))) {
  34. if (current_dir.compare(entry->d_name) == 0 || parent_dir.compare(entry->d_name) == 0) {
  35. continue;
  36. }
  37. struct stat info;
  38. string filepath = root + "/" + string(entry->d_name);
  39. if (stat(filepath.c_str(), &info) != 0) {
  40. cerr << "Error getting info on " << filepath.c_str() << ": " << strerror(errno) << endl;
  41. continue;
  42. }
  43. if (S_ISDIR(info.st_mode)) {
  44. delete_directory(filepath);
  45. } else {
  46. unlink(filepath.c_str());
  47. }
  48. }
  49. closedir(dp);
  50. rmdir(root.c_str());
  51. }
  52. string make_temp_dir(void) {
  53. char temp_path_template[] = "/tmp/temp_unittest_dir_XXXXXX";
  54. if (mkdtemp(temp_path_template) == NULL) {
  55. cerr << "Error making temp dir for test files: " << strerror(errno) << endl;
  56. return "";
  57. }
  58. return string(temp_path_template);
  59. }
  60. void TestBaseClass::SetUp(void) {
  61. ::testing::Test::SetUp();
  62. // setup debug log file for each test
  63. temp_dir = make_temp_dir();
  64. ASSERT_FALSE(temp_dir.empty());
  65. string debug_filepath = temp_dir + "/debug_file.log";
  66. init_debug(debug_filepath.c_str());
  67. // point logging to stderr
  68. log_destination = STDERR;
  69. }
  70. void TestBaseClass::TearDown(void) {
  71. ::testing::Test::TearDown();
  72. close_debug();
  73. delete_directory(temp_dir);
  74. }
  75. TEST(TestHelpers, make_temp_dir) {
  76. // make a temp dir
  77. string temp_dir = make_temp_dir();
  78. // path should not be empty string
  79. ASSERT_FALSE(temp_dir.empty());
  80. // a directory should exist at the path
  81. struct stat info;
  82. ASSERT_EQ(stat(temp_dir.c_str(), &info), 0);
  83. EXPECT_TRUE(S_ISDIR(info.st_mode));
  84. delete_directory(temp_dir);
  85. }
  86. TEST(TestHelpers, delete_directory) {
  87. // make a temp dir
  88. string temp_dir = make_temp_dir();
  89. ASSERT_FALSE(temp_dir.empty());
  90. // build a bunch of nested sub-dirs and files
  91. string path = temp_dir;
  92. for (int i = 0; i < 5; ++i) {
  93. path = path + "/sub_dir";
  94. mkdir(path.c_str(), 0777);
  95. string filename = path + "/some_file";
  96. fclose(fopen(filename.c_str(), "w"));
  97. }
  98. // last sub-dir should exist and be a directory
  99. struct stat info;
  100. ASSERT_EQ(stat(path.c_str(), &info), 0);
  101. EXPECT_TRUE(S_ISDIR(info.st_mode));
  102. // last sub-dir should have a file in it
  103. string filename = path + "/some_file";
  104. ASSERT_EQ(stat(filename.c_str(), &info), 0);
  105. EXPECT_TRUE(S_ISREG(info.st_mode));
  106. // delete the root temp dir
  107. delete_directory(temp_dir);
  108. // root temp dir should no longer exist
  109. ASSERT_NE(stat(temp_dir.c_str(), &info), 0);
  110. }