Sqlite3 support for tests. Libprivilege tests: check database
[platform/core/test/security-tests.git] / tests / libprivilege-control-tests / test_cases.cpp
index f784166..5c341c5 100644 (file)
@@ -18,6 +18,7 @@
  * @file        test_cases.cpp
  * @author      Jan Olszak (j.olszak@samsung.com)
  * @author      Rafal Krypa (r.krypa@samsung.com)
+ * @author      Lukasz Wojciechowski (l.wojciechow@partner.samsung.com)
  * @version     1.0
  * @brief       libprivilege-control test runner
  */
@@ -46,6 +47,8 @@
 #include <dpl/log/log.h>
 #include <tests_common.h>
 #include <libprivilege-control_test_common.h>
+#include <tracker.h>
+#include "common/db.h"
 
 #include <iostream>
 
 
 #define SMACK_ACC_LEN            6
 
+// Error codes for test_libprivilege_strerror
+const std::vector<int> error_codes {
+    PC_OPERATION_SUCCESS, PC_ERR_FILE_OPERATION, PC_ERR_MEM_OPERATION, PC_ERR_NOT_PERMITTED,
+    PC_ERR_INVALID_PARAM, PC_ERR_INVALID_OPERATION, PC_ERR_DB_OPERATION, PC_ERR_DB_LABEL_TAKEN,
+    PC_ERR_DB_QUERY_PREP, PC_ERR_DB_QUERY_BIND, PC_ERR_DB_QUERY_STEP, PC_ERR_DB_CONNECTION,
+    PC_ERR_DB_NO_SUCH_APP, PC_ERR_DB_PERM_FORBIDDEN
+};
+
 namespace {
 
 std::vector<std::string> gen_names(std::string prefix, std::string suffix, size_t size)
@@ -668,7 +679,6 @@ RUNNER_TEST(privilege_control01_app_install)
 RUNNER_TEST(privilege_control07_app_uninstall)
 {
     int result;
-    int fd = -1;
 
     DB_BEGIN
 
@@ -677,10 +687,8 @@ RUNNER_TEST(privilege_control07_app_uninstall)
 
     DB_END
 
-    // checking if file really exists
-    fd = open(SMACK_RULES_DIR APP_ID, O_RDONLY);
-    RUNNER_ASSERT_MSG(fd == -1, "SMACK file NOT deleted after perm_app_uninstall");
-    close(fd);
+    TestLibPrivilegeControlDatabase db_test;
+    db_test.test_db_after__perm_app_uninstall(TRACE_FROM_HERE, APP_ID);
 }
 
 /*
@@ -697,8 +705,6 @@ RUNNER_TEST_SMACK(privilege_control10_app_register_av)
     smack_revoke_subject(APP_TEST_AV_1);
     smack_revoke_subject(APP_TEST_AV_2);
 
-    cleaning_smack_app_files();
-
     DB_BEGIN
 
     // Adding two apps before antivir
@@ -746,8 +752,6 @@ RUNNER_TEST_SMACK(privilege_control10_app_register_av)
     // cleaning
     smack_revoke_subject(APP_TEST_AV_1);
     smack_revoke_subject(APP_TEST_AV_2);
-
-    cleaning_smack_app_files();
 }
 #pragma GCC diagnostic warning "-Wdeprecated-declarations"
 
@@ -1486,7 +1490,6 @@ RUNNER_TEST(privilege_control21_early_rules)
     RUNNER_IGNORED_MSG("early rules are not implemented");
 
     int result;
-    int fd = -1;
     int pass_1 = 0;
     int pass_2 = 0;
     char *single_line_format = NULL;
@@ -1498,8 +1501,6 @@ RUNNER_TEST(privilege_control21_early_rules)
     char rule_add[SMACK_ACC_LEN + 1] = {0};
     char rule_remove[SMACK_ACC_LEN + 1] = {0};
 
-    unlink(SMACK_RULES_DIR APP_ID);
-
     DB_BEGIN
 
     perm_app_uninstall(APP_ID);
@@ -1511,11 +1512,9 @@ RUNNER_TEST(privilege_control21_early_rules)
 
     DB_END
 
-    // checking if file really exists
-    fd = open(SMACK_RULES_DIR APP_ID, O_RDONLY);
-    close(fd);
-    RUNNER_ASSERT_MSG(fd >= 0, "File open failed: " << SMACK_RULES_DIR << APP_ID << " : " << fd << ". Errno: " << strerror(errno));
-    fd = -1;
+    TestLibPrivilegeControlDatabase db_test;
+    db_test.test_db_after__perm_app_install(TRACE_FROM_HERE, APP_ID);
+    db_test.test_db_after__perm_app_install(TRACE_FROM_HERE, APP_TEST_APP_1);
 
     DB_BEGIN
 
@@ -1847,3 +1846,22 @@ RUNNER_TEST(privilege_control24c_av_privilege_public_ro)
     CheckAVPrivilege(APP_TYPE_OSP, APP_PATH_PUBLIC_RO);
     CheckAVPrivilege(APP_TYPE_EFL, APP_PATH_PUBLIC_RO);
 }
+
+RUNNER_TEST(privilege_control25_test_libprivilege_strerror) {
+    int POSITIVE_ERROR_CODE = 1;
+    int NONEXISTING_ERROR_CODE = -239042;
+    const char *result;
+
+    for (auto itr = error_codes.begin(); itr != error_codes.end(); ++itr) {
+        RUNNER_ASSERT_MSG(strcmp(perm_strerror(*itr), "Unknown error") != 0,
+                "Returned invalid error code description.");
+    }
+
+    result = perm_strerror(POSITIVE_ERROR_CODE);
+    RUNNER_ASSERT_MSG(strcmp(result, "Unknown error") == 0,
+            "Bad message returned for invalid error code: \"" << result << "\"");
+
+    result = perm_strerror(NONEXISTING_ERROR_CODE);
+    RUNNER_ASSERT_MSG(strcmp(result, "Unknown error") == 0,
+            "Bad message returned for invalid error code: \"" << result << "\"");
+}