*/
#include <dpl/test/test_runner.h>
+#include <cynara_test_commons.h>
+
+#include <dirent.h>
+#include <glob.h>
+#include <fstream>
+#include <iterator>
+#include <memory.h>
+#include <set>
+#include <string>
+
+namespace
+{
+
+const std::string cynaraTestPatternsPath("/etc/security-tests/db_patterns/");
+const std::string directoryWildcard("/*");
+
+bool unordered_files_match(const std::string &patternFilePath, const std::string &resultFilePath) {
+ std::ifstream patternFile(patternFilePath, std::ifstream::in | std::ifstream::binary);
+ std::ifstream resultFile(resultFilePath, std::ifstream::in | std::ifstream::binary);
+
+ RUNNER_ASSERT_MSG(patternFile.is_open(), "Failed to open " << patternFile << ".");
+ RUNNER_ASSERT_MSG(resultFile.is_open(), "Failed to open " << resultFile << ".");
+
+ auto patternRecords = std::multiset<std::string>(std::istream_iterator<std::string>(patternFile),
+ std::istream_iterator<std::string>());
+
+ auto resultRecords = std::multiset<std::string>(std::istream_iterator<std::string>(resultFile),
+ std::istream_iterator<std::string>());
+
+ return patternRecords == resultRecords;
+}
+
+size_t glob_count(const std::string &source, const std::string &wildcard) {
+ //for counting files in directory
+ glob_t globbuf;
+ std::string pattern = source + wildcard;
+
+ //for freeing allocated memory
+ GlobPtr globbufPtr(&globbuf);
+
+ //actually count files in directory - including dotfiles
+ RUNNER_ASSERT_MSG(0 == glob(pattern.c_str(), GLOB_NOSORT | GLOB_PERIOD, NULL, &globbuf),
+ "Failed to search for requested pathnames in " << source << ".");
+
+ return globbuf.gl_pathc;
+}
+
+size_t db_files_count(const std::string &source) {
+ size_t dbFilesCount = 0;
+
+ //database directory must not be empty
+ RUNNER_ASSERT_MSG(0 != (dbFilesCount = glob_count(source, directoryWildcard)),
+ "Unexpected condition: " << source << " was empty.");
+
+ return dbFilesCount;
+}
+
+void compareDbs(const std::string &source)
+{
+ //for accessing files in directory
+ std::string patternDir = cynaraTestPatternsPath + source;
+ DIR *patternDirPtr = nullptr;
+ struct dirent *direntPtr;
+
+ size_t patternFileCount = db_files_count(patternDir);
+ size_t resultFileCount = db_files_count(CynaraTestConsts::DB_DIR);
+
+ //directories do not match if there is different number of files
+ RUNNER_ASSERT_MSG(patternFileCount == resultFileCount,
+ "No match in database and pattern directory file count");
+
+ //compare files in database directory with pattern directory
+ RUNNER_ASSERT_ERRNO_MSG(patternDirPtr = opendir(patternDir.c_str()),
+ "Opening " << patternDir << " directory failed");
+ DirPtr patternDirScopedPtr(patternDirPtr);
+
+ while ((direntPtr = readdir(patternDirPtr)) != nullptr) {
+ if (!strcmp(direntPtr->d_name, ".")
+ || !strcmp(direntPtr->d_name, ".."))
+ continue;
+ std::string patternName = patternDir + "/" + direntPtr->d_name;
+ std::string resultName = CynaraTestConsts::DB_DIR + "/" + direntPtr->d_name;
+
+ //comparing file saved db dir with reference file from patterns dir
+ RUNNER_ASSERT_MSG(true == unordered_files_match(patternName, resultName),
+ "No match in stored file and pattern file");
+ }
+}
+
+}
RUNNER_TEST_GROUP_INIT(cynara_db_tests)