Add tests to libprivilege-control perm_app_has_permission function.
[platform/core/test/security-tests.git] / tests / libprivilege-control-tests / test_cases.cpp
index c7603af..197aa83 100644 (file)
@@ -1018,6 +1018,98 @@ RUNNER_TEST_SMACK(privilege_control19_app_setup_path_settings)
     test_app_setup_path(__LINE__, APP_PATH_SETTINGS_RW);
 }
 
+void check_perm_app_has_permission(const char* app_label, const char* permission, bool is_enabled_expected)
+{
+    int result;
+    bool is_enabled;
+
+    result = perm_app_has_permission(app_label, APP_TYPE_WGT, permission, &is_enabled);
+    RUNNER_ASSERT_MSG_BT(result == PC_OPERATION_SUCCESS,
+            "Error calling perm_app_has_permission. Result: " << result);
+
+    RUNNER_ASSERT_MSG_BT(is_enabled == is_enabled_expected,
+            "Result of perm_app_has_permission should be: " << is_enabled_expected);
+}
+
+RUNNER_TEST(privilege_control20_perm_app_has_permission)
+{
+    int result;
+    const char *other_app_label = "test_other_app_label";
+
+    DB_BEGIN
+
+    result = perm_app_uninstall(WGT_APP_ID);
+    RUNNER_ASSERT_MSG_BT(result == PC_OPERATION_SUCCESS,
+            "Error uninstalling app. Result" << result);
+
+    result = perm_app_install(WGT_APP_ID);
+    RUNNER_ASSERT_MSG_BT(result == PC_OPERATION_SUCCESS,
+            "Error installing app. Result" << result);
+
+    result = perm_app_disable_permissions(WGT_APP_ID, APP_TYPE_WGT, PRIVS2_R_AND_NO_R);
+    RUNNER_ASSERT_MSG_BT(result == PC_OPERATION_SUCCESS,
+            "Error disabling app r and no r permissions. Result: " << result);
+
+    DB_END
+
+    check_perm_app_has_permission(USER_APP_ID, PRIVS2_R[0], false);
+    check_perm_app_has_permission(USER_APP_ID, PRIVS2_NO_R[0], false);
+    check_perm_app_has_permission(other_app_label, PRIVS2_R[0], false);
+    check_perm_app_has_permission(other_app_label, PRIVS2_NO_R[0], false);
+
+    DB_BEGIN
+
+    result = perm_app_enable_permissions(WGT_APP_ID, APP_TYPE_WGT, PRIVS2_R, false);
+    RUNNER_ASSERT_MSG_BT(result == PC_OPERATION_SUCCESS,
+            "Error registering app r permissions. Result: " << result);
+
+    DB_END
+
+    check_perm_app_has_permission(USER_APP_ID, PRIVS2_R[0], true);
+    check_perm_app_has_permission(USER_APP_ID, PRIVS2_NO_R[0], false);
+    check_perm_app_has_permission(other_app_label, PRIVS2_R[0], false);
+    check_perm_app_has_permission(other_app_label, PRIVS2_NO_R[0], false);
+
+    DB_BEGIN
+
+    result = perm_app_enable_permissions(WGT_APP_ID, APP_TYPE_WGT, PRIVS2_NO_R, false);
+    RUNNER_ASSERT_MSG_BT(result == PC_OPERATION_SUCCESS,
+            "Error registering app r permissions. Result: " << result);
+
+    DB_END
+
+    check_perm_app_has_permission(USER_APP_ID, PRIVS2_R[0], true);
+    check_perm_app_has_permission(USER_APP_ID, PRIVS2_NO_R[0], true);
+    check_perm_app_has_permission(other_app_label, PRIVS2_R[0], false);
+    check_perm_app_has_permission(other_app_label, PRIVS2_NO_R[0], false);
+
+    DB_BEGIN
+
+    result = perm_app_disable_permissions(WGT_APP_ID, APP_TYPE_WGT, PRIVS2_R);
+    RUNNER_ASSERT_MSG_BT(result == PC_OPERATION_SUCCESS,
+            "Error disabling app r and no r permissions. Result: " << result);
+
+    DB_END
+
+    check_perm_app_has_permission(USER_APP_ID, PRIVS2_R[0], false);
+    check_perm_app_has_permission(USER_APP_ID, PRIVS2_NO_R[0], true);
+    check_perm_app_has_permission(other_app_label, PRIVS2_R[0], false);
+    check_perm_app_has_permission(other_app_label, PRIVS2_NO_R[0], false);
+
+    DB_BEGIN
+
+    result = perm_app_disable_permissions(WGT_APP_ID, APP_TYPE_WGT, PRIVS2_NO_R);
+    RUNNER_ASSERT_MSG_BT(result == PC_OPERATION_SUCCESS,
+            "Error disabling app r and no r permissions. Result: " << result);
+
+    DB_END
+
+    check_perm_app_has_permission(USER_APP_ID, PRIVS2_R[0], false);
+    check_perm_app_has_permission(USER_APP_ID, PRIVS2_NO_R[0], false);
+    check_perm_app_has_permission(other_app_label, PRIVS2_R[0], false);
+    check_perm_app_has_permission(other_app_label, PRIVS2_NO_R[0], false);
+}
+
 RUNNER_TEST(privilege_control25_test_libprivilege_strerror) {
     int POSITIVE_ERROR_CODE = 1;
     int NONEXISTING_ERROR_CODE = -239042;