Adjust restart of cynara in cynara-tests
[platform/core/test/security-tests.git] / src / cynara-tests / test_cases_db.cpp
index 6fe3202..a0f4d78 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);
     }
 }
 
@@ -155,12 +200,11 @@ void tcdb01_lockdown_init_failure_func()
 {
     Admin admin;
     Client cynara;
-    ServiceManager serviceManager(CynaraTestConsts::SERVICE);
 
     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);
@@ -168,7 +212,7 @@ void tcdb01_lockdown_init_failure_func()
     deleteDbFile(fakeBackupGuard);
     admin.setBucket(bucket, CYNARA_ADMIN_ALLOW, extra);
 
-    serviceManager.restartService();
+    restartCynaraServiceAndSockets();
     compareDbs(defDbAllow);
 }
 
@@ -185,12 +229,11 @@ void tcdb02_write_to_backup_failure_func()
 {
     Admin admin;
     Client cynara;
-    ServiceManager serviceManager(CynaraTestConsts::SERVICE);
 
     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);
@@ -198,7 +241,7 @@ void tcdb02_write_to_backup_failure_func()
     createDbFile(fakeBucketDumpFile);
     admin.setBucket(bucket, CYNARA_ADMIN_DENY, extra, CYNARA_API_OPERATION_FAILED);
 
-    serviceManager.restartService();
+    restartCynaraServiceAndSockets();
     compareDbs(defDbAllow);
 }
 
@@ -216,22 +259,21 @@ void tcdb03_invalid_and_valid_backup_removal_func()
 {
     Admin admin;
     Client cynara;
-    ServiceManager serviceManager(CynaraTestConsts::SERVICE);
 
     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);
 
-    serviceManager.restartService();
+    restartCynaraServiceAndSockets();
     compareDbs(defDb);
 
     admin.setBucket(bucket, CYNARA_ADMIN_ALLOW, extra);
 
-    serviceManager.restartService();
+    restartCynaraServiceAndSockets();
     compareDbs(defDbAllow);
 }
 
@@ -274,17 +316,15 @@ void tcdb04_dumped_file_binary_comparison_func()
  */
 void tcdb05_non_indexed_files_removal_func()
 {
-    ServiceManager serviceManager(CynaraTestConsts::SERVICE);
-
     std::vector<std::string> filenames = { "_broken-backup~", "_non-indexed-bucket",
                                            "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);
     }
 
-    serviceManager.restartService();
+    restartCynaraServiceAndSockets();
     compareDbs(defDb);
 }