Enhance compareDbs() with additional information 04/33904/4
authorPawel Wieczorek <p.wieczorek2@samsung.com>
Mon, 12 Jan 2015 10:05:35 +0000 (11:05 +0100)
committerLukasz Wojciechowski <l.wojciechow@partner.samsung.com>
Tue, 17 Mar 2015 14:44:09 +0000 (15:44 +0100)
This patch adds printing more debugging information if an error occurs.
In case of database contents mismatch all the files in pattern and
result database are listed. In case of file contents mismatch filename
of corrupted file is printed.

Change-Id: I8e2bc2a6fdc976fac65e521accd6140ddfebef7e

src/cynara-tests/test_cases_db.cpp

index 6fe3202..16b3d91 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * 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.
@@ -51,6 +51,7 @@ const std::string defDbAllow("defaultAllowed");
 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)
 {
@@ -108,19 +109,63 @@ size_t db_files_count(const std::string &source) {
     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()),
@@ -131,12 +176,12 @@ void compareDbs(const std::string &source)
         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);
     }
 }
 
@@ -160,7 +205,7 @@ void tcdb01_lockdown_init_failure_func()
     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);
@@ -190,7 +235,7 @@ void tcdb02_write_to_backup_failure_func()
     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);
@@ -221,7 +266,7 @@ void tcdb03_invalid_and_valid_backup_removal_func()
     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);
@@ -280,7 +325,7 @@ void tcdb05_non_indexed_files_removal_func()
                                            "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);
     }