#include <set>
#include <string>
#include <unistd.h>
+#include <vector>
using namespace CynaraTestAdmin;
using namespace CynaraTestClient;
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)