Add tests for database cleanup mechanism 16/30716/11
authorPawel Wieczorek <p.wieczorek2@samsung.com>
Mon, 24 Nov 2014 11:50:49 +0000 (12:50 +0100)
committerPawel Wieczorek <p.wieczorek2@samsung.com>
Mon, 15 Dec 2014 10:22:08 +0000 (11:22 +0100)
This patch adds remaining tests for database integrity mechanism, e.g.
removal of non-indexed files, removal of invalid backup files.

Change-Id: Ib6e373dba3ef24c5e69b058df2b8dd9403616eec

tests/cynara-tests/test_cases_db.cpp

index e97d88c..76abcb2 100644 (file)
@@ -38,6 +38,7 @@
 #include <set>
 #include <string>
 #include <unistd.h>
+#include <vector>
 
 using namespace CynaraTestAdmin;
 using namespace CynaraTestClient;
@@ -201,7 +202,96 @@ void tcdb02_write_to_backup_failure_func()
     compareDbs(defDbAllow);
 }
 
+/**
+ * @brief   Check whether both invalid and valid backup databases are removed
+ * @test    Expected result: no unnecessary backup files in policy database directory
+ * 1. Fail writing to backup database
+ * 2. Reload Cynara - policies should be loaded from primary (valid) database
+ * 3. Check if all backup files were removed
+ * 4. Successfully write changes to database
+ * 5. Reload Cynara - policies should be loaded from primary (revalidated) database
+ * 6. Check if all backup files were removed
+ */
+void tcdb03_invalid_and_valid_backup_removal_func()
+{
+    Admin admin;
+    Client cynara;
+    DBusAccess dbusAccess(CynaraTestConsts::SERVICE.c_str());
+
+    const char *bucket = CYNARA_ADMIN_DEFAULT_BUCKET;
+    const char *extra = nullptr;
+
+    const auto defaultBucketDumpFile = CynaraTestConsts::DB_DIR + "/_~";
+
+    createDbFile(defaultBucketDumpFile);
+    admin.setBucket(bucket, CYNARA_ADMIN_ALLOW, extra, CYNARA_API_OPERATION_FAILED);
+
+    dbusAccess.restartService();
+    compareDbs(defDb);
+
+    admin.setBucket(bucket, CYNARA_ADMIN_ALLOW, extra);
+
+    dbusAccess.restartService();
+    compareDbs(defDbAllow);
+}
+
+/**
+ * @brief   Comparison between database modified by Cynara with expected one
+ * @test    Expected result: no differences between those files
+ * 1. Write sample policy to database (and let it save to storage)
+ * 2. Compare freshly saved files with samples from test patterns directory
+ */
+void tcdb04_dumped_file_binary_comparison_func()
+{
+    Admin admin;
+    Client cynara;
+    DBusAccess dbusAccess(CynaraTestConsts::SERVICE.c_str());
+
+    const char *bucket = CYNARA_ADMIN_DEFAULT_BUCKET;
+    const char *client = "client";
+    const char *user = "user";
+    const char *privilege = "privilege";
+    const char *extra = nullptr;
+
+    {
+        CynaraPoliciesContainer cp;
+        cp.add(bucket, client, user, privilege, CYNARA_ADMIN_DENY, extra);
+        admin.setPolicies(cp, CYNARA_API_SUCCESS);
+    }
+
+    compareDbs(nonEmptyDb);
+}
+
+/**
+ * @brief   Invalid database files removal
+ * @test    Expected result: no unnecessary files in policy database directory
+ * 1. Fill Cynara's policy database directory with garbage:
+ *         - Sample backup file which should be removed earlier
+ *         - Sample bucket file which is not mentioned in index (shouldn't exist at all)
+ *         - Sample files which don't belong to database
+ * 2. Reload Cynara
+ * 3. Check if any of mentioned above files still remained
+ */
+void tcdb05_non_indexed_files_removal_func()
+{
+    DBusAccess dbusAccess(CynaraTestConsts::SERVICE.c_str());
+
+    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;
+        createDbFile(garbageFilename);
+    }
+
+    dbusAccess.restartService();
+    compareDbs(defDb);
+}
+
 RUNNER_TEST_GROUP_INIT(cynara_db_tests)
 
 RUN_CYNARA_TEST(tcdb01_lockdown_init_failure)
 RUN_CYNARA_TEST(tcdb02_write_to_backup_failure)
+RUN_CYNARA_TEST(tcdb03_invalid_and_valid_backup_removal)
+RUN_CYNARA_TEST(tcdb04_dumped_file_binary_comparison)
+RUN_CYNARA_TEST(tcdb05_non_indexed_files_removal)