/*
- * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2014-2015 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
const std::string nonEmptyDb("nonEmptyDatabase");
const std::string cynaraTestPatternsPath("/etc/security-tests/db_patterns/");
const std::string directoryWildcard("/*");
+const char directorySeparator('/');
void createDbFile(const std::string &filename)
{
return dbFilesCount;
}
+const std::set<std::string> dump_glob_filenames(const glob_t &globbuf) {
+ std::set<std::string> set;
+
+ for (unsigned i = 0; i < globbuf.gl_pathc; ++i) {
+ std::string filename(globbuf.gl_pathv[i]);
+ set.insert(filename.substr(filename.find_last_of(directorySeparator)+1));
+ }
+
+ return set;
+}
+
+const std::set<std::string> glob_filenames(const std::string &source, const std::string &wildcard) {
+ //for finding files matching pattern in directory
+ glob_t globbuf;
+ std::string pattern = source + wildcard;
+
+ //for freeing allocated memory
+ GlobPtr globbufPtr(&globbuf);
+
+ //actually find files matching pattern 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 dump_glob_filenames(globbuf);
+}
+
+const std::set<std::string> db_files_pathnames(const std::string &source) {
+ return glob_filenames(source, directoryWildcard);
+}
+
+std::ostream& operator<<(std::ostream& os, const std::set<std::string> &set)
+{
+ os << "{";
+ for (const auto &item : set) {
+ os << " " << item;
+ }
+ os << " }";
+ return os;
+}
+
void compareDbs(const std::string &source)
{
//for accessing files in directory
std::string patternDir = cynaraTestPatternsPath + source;
+ std::string resultDir = CynaraTestConsts::DB_DIR;
DIR *patternDirPtr = nullptr;
struct dirent *direntPtr;
size_t patternFileCount = db_files_count(patternDir);
- size_t resultFileCount = db_files_count(CynaraTestConsts::DB_DIR);
+ size_t resultFileCount = db_files_count(resultDir);
//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");
+ "No match in database and pattern directory file count: "
+ << resultFileCount << " != " << patternFileCount << "." << std::endl
+ << "Expected: " << db_files_pathnames(patternDir) << std::endl
+ << "Actual: " << db_files_pathnames(resultDir));
//compare files in database directory with pattern directory
RUNNER_ASSERT_ERRNO_MSG(patternDirPtr = opendir(patternDir.c_str()),
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;
+ std::string patternName = patternDir + directorySeparator + direntPtr->d_name;
+ std::string resultName = CynaraTestConsts::DB_DIR + directorySeparator + 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");
+ "No match in stored file and pattern file: " << resultName);
}
}
const char *bucket = CYNARA_ADMIN_DEFAULT_BUCKET;
const char *extra = nullptr;
- const auto fakeBackupGuard = CynaraTestConsts::DB_DIR + "/guard";
+ const auto fakeBackupGuard = CynaraTestConsts::DB_DIR + directorySeparator + "guard";
createDbFile(fakeBackupGuard);
admin.setBucket(bucket, CYNARA_ADMIN_ALLOW, extra, CYNARA_API_OPERATION_FAILED);
const char *bucket = CYNARA_ADMIN_DEFAULT_BUCKET;
const char *extra = nullptr;
- const auto fakeBucketDumpFile = CynaraTestConsts::DB_DIR + "/_~";
+ const auto fakeBucketDumpFile = CynaraTestConsts::DB_DIR + directorySeparator + "_~";
admin.setBucket(bucket, CYNARA_ADMIN_ALLOW, extra);
compareDbs(defDbAllow);
const char *bucket = CYNARA_ADMIN_DEFAULT_BUCKET;
const char *extra = nullptr;
- const auto defaultBucketDumpFile = CynaraTestConsts::DB_DIR + "/_~";
+ const auto defaultBucketDumpFile = CynaraTestConsts::DB_DIR + directorySeparator + "_~";
createDbFile(defaultBucketDumpFile);
admin.setBucket(bucket, CYNARA_ADMIN_ALLOW, extra, CYNARA_API_OPERATION_FAILED);
"some-file-that-doesnt-belong-here" };
for (const auto &filename : filenames) {
- auto garbageFilename = CynaraTestConsts::DB_DIR + "/" + filename;
+ auto garbageFilename = CynaraTestConsts::DB_DIR + directorySeparator + filename;
createDbFile(garbageFilename);
}