Test cases for app privileges state getters
authorPawel Broda <p.broda@partner.samsung.com>
Thu, 16 Jan 2014 12:55:02 +0000 (13:55 +0100)
committerMarcin Niesluchowski <m.niesluchow@samsung.com>
Wed, 12 Mar 2014 11:00:41 +0000 (12:00 +0100)
[Issue#]        SSDWSSP-627
[Feature]       Tests for added API functions.
[Cause]         N/A
[Solution]      N/A
[Verification]  Build and run tests on the target
                The code to be tested:
                http://slp-info.sec.samsung.net/gerrit/#/c/341259/

Change-Id: I10189b0c528f34f5cb74531109e7461cd6d1a4b1

tests/libprivilege-control-tests/common/libprivilege-control_test_common.h
tests/libprivilege-control-tests/test_cases.cpp

index c56b74b..acb7d2b 100644 (file)
@@ -30,6 +30,7 @@
 #include <string>
 #include <set>
 #include <ftw.h>
+#include <privilege-control.h>
 #include <tests_common.h>
 #include <unistd.h>
 
@@ -138,6 +139,12 @@ template<typename T> struct list_deleter {
 };
 typedef std::unique_ptr<char*, list_deleter<char> > CStringListPtr;
 
+typedef struct perm_app_status_list {
+    perm_app_status_t *status;
+    size_t size;
+} perm_app_status_list_t;
+typedef std::unique_ptr<perm_app_status_list_t, void (*)(perm_app_status_list_t*)> ListAppStatusPtr;
+
 class DBBackup {
 private:
     bool backupfile(const std::string& src, const std::string& dst);
index 44df62d..d44c263 100644 (file)
@@ -529,6 +529,243 @@ RUNNER_CHILD_TEST_SMACK(privilege_control05_set_app_privilege_osp_platform)
 }
 
 /**
+ * Check perm_get_permissions()
+ */
+bool is_permission_on_the_list(char **permissions, const char *const test_permission_name)
+{
+    if(permissions != NULL) {
+        for(size_t i = 0; permissions[i] != NULL; ++i) {
+            if(strcmp(permissions[i], test_permission_name) == 0)
+                return true;
+        }
+    }
+    return false;
+}
+
+RUNNER_TEST(privilege_control05_perm_get_permissions)
+{
+    DBBackup dbbackup;
+
+    char **permissions_1 = NULL;
+    char **permissions_2 = NULL;
+    const char *test_permission_name = "perm_get_permissions_test_2";
+    bool permission_found_1;
+    bool permission_found_2;
+    int result;
+
+    result = perm_get_permissions(&permissions_1, APP_TYPE_OSP);
+    RUNNER_ASSERT_MSG_BT(result == 0, "perm_get_permissions returned " << perm_strerror(result));
+    CStringListPtr listPtr_1(permissions_1);
+
+    permission_found_1 = is_permission_on_the_list(permissions_1, test_permission_name);
+    RUNNER_ASSERT_MSG_BT(permission_found_1 == false, "permission_found_1 = " << permission_found_1);
+
+    DB_BEGIN
+
+    result = perm_add_api_feature(APP_TYPE_OSP, test_permission_name, (const char*[]) {
+            "~APP~    object\t rwxatl",
+            " \t \n",
+            "subject2\t~APP~ ltxarw",
+            "",
+            NULL
+            }, NULL, 0);
+    RUNNER_ASSERT_MSG_BT(result == PC_OPERATION_SUCCESS, "perm_add_api_feature returned: " <<
+                         perm_strerror(result));
+
+    DB_END
+
+    result = perm_get_permissions(&permissions_2, APP_TYPE_OSP);
+    RUNNER_ASSERT_MSG_BT(result == 0, "perm_get_permissions returned " << perm_strerror(result));
+    CStringListPtr listPtr_2(permissions_2);
+
+    permission_found_2 = is_permission_on_the_list(permissions_2, test_permission_name);
+    RUNNER_ASSERT_MSG_BT(permission_found_2 == true, "permission_found_2 = " << permission_found_2);
+}
+
+/**
+ * Check perm_get_apps_with_permission()
+ */
+int get_app_index(perm_app_status_t *apps, const char *pkg_id, size_t apps_count)
+{
+    size_t i = 0;
+    if(apps_count > 0) {
+        for(i = 0; i < apps_count; ++i) {
+            if(strcmp(pkg_id, apps[i].app_id) == 0)
+                return i;
+        }
+    }
+    return -1;
+}
+
+void wrapper_perm_free_apps_list(perm_app_status_list_t *list) {
+    perm_free_apps_list(list->status, list->size);
+}
+
+RUNNER_TEST(privilege_control05_perm_get_apps_with_permission_default)
+{
+    perm_app_status_t *apps_1 = NULL;
+    perm_app_status_t *apps_2 = NULL;
+    size_t apps_count_1 = 0;
+    size_t apps_count_2 = 0;
+    int app_index_2 = 0;
+    int result = 0;
+
+    DB_BEGIN
+
+    result = perm_app_uninstall(APP_ID);
+    RUNNER_ASSERT_MSG_BT(result == 0, "perm_app_uninstall (1) returned " << perm_strerror(result));
+
+    DB_END
+
+    result = perm_get_apps_with_permission(&apps_1, &apps_count_1, APP_TYPE_OSP, PRIVS_OSP[0]);
+
+    RUNNER_ASSERT_MSG_BT(result == 0, "perm_get_apps_with_permission (1) returned " <<
+                         perm_strerror(result));
+
+    perm_app_status_list_t app_list_1;
+    app_list_1.status = apps_1;
+    app_list_1.size = apps_count_1;
+    ListAppStatusPtr listAppStatusPtr_1(&app_list_1, wrapper_perm_free_apps_list);
+
+    DB_BEGIN
+
+    result = perm_app_install(APP_ID);
+    RUNNER_ASSERT_MSG_BT(result == 0, "perm_app_install returned " << perm_strerror(result));
+
+    result = perm_app_enable_permissions(APP_ID, APP_TYPE_OSP, PRIVS_OSP, true);
+    RUNNER_ASSERT_MSG_BT(result == PC_OPERATION_SUCCESS,
+            "error enabling app permissions. Result: " << perm_strerror(result));
+    DB_END
+
+    result = perm_get_apps_with_permission(&apps_2, &apps_count_2, APP_TYPE_OSP, PRIVS_OSP[0]);
+
+    RUNNER_ASSERT_MSG_BT(result == 0, "perm_get_apps_with_permission (2) returned " <<
+                         perm_strerror(result));
+
+    perm_app_status_list_t app_list_2;
+    app_list_2.status = apps_2;
+    app_list_2.size = apps_count_2;
+    ListAppStatusPtr listAppStatusPtr_2(&app_list_2, wrapper_perm_free_apps_list);
+
+    RUNNER_ASSERT_MSG_BT(apps_count_2 > apps_count_1, "no new app added " << perm_strerror(result));
+
+    DB_BEGIN
+
+    // check default is_permanent (true), is_enabled (true)
+    app_index_2 = get_app_index(apps_2, APP_ID, apps_count_2);
+
+    RUNNER_ASSERT_MSG_BT(app_index_2 >= 0, "index not found (2): " << app_index_2);
+
+    RUNNER_ASSERT_MSG_BT(apps_2[app_index_2].is_permanent,
+            "incorrect is_permanent value: " << apps_2[app_index_2].is_permanent);
+
+    RUNNER_ASSERT_MSG_BT(apps_2[app_index_2].is_enabled,
+            "incorrect is_enabled value: " << apps_2[app_index_2].is_enabled);
+
+    result = perm_app_uninstall(APP_ID);
+    RUNNER_ASSERT_MSG_BT(result == 0, "perm_app_uninstall (2) returned " << perm_strerror(result));
+
+    DB_END
+}
+
+RUNNER_TEST(privilege_control05_perm_get_apps_with_permission_is_permanent)
+{
+    perm_app_status_t *apps = NULL;
+    size_t apps_count = 0;
+    int app_index = 0;
+    int result = 0;
+
+    DB_BEGIN
+
+    result = perm_app_install(APP_ID);
+    RUNNER_ASSERT_MSG_BT(result == 0, "perm_app_install returned " << perm_strerror(result));
+
+    // change is_permanent to false using perm_app_enable_permissions
+
+    bool is_permanent = false;
+    result = perm_app_enable_permissions(APP_ID, APP_TYPE_OSP, PRIVS_OSP, is_permanent);
+    RUNNER_ASSERT_MSG_BT(result == 0, "perm_app_enable_permissions returned " <<
+                         perm_strerror(result));
+
+    DB_END
+
+    result = perm_get_apps_with_permission(&apps, &apps_count, APP_TYPE_OSP, PRIVS_OSP[0]);
+
+    RUNNER_ASSERT_MSG_BT(result == 0, "perm_get_apps_with_permission returned " <<
+                         perm_strerror(result));
+
+    perm_app_status_list_t app_list;
+    app_list.status = apps;
+    app_list.size = apps_count;
+    ListAppStatusPtr listAppStatusPtr(&app_list, wrapper_perm_free_apps_list);
+
+    DB_BEGIN
+
+    app_index = get_app_index(apps, APP_ID, apps_count);
+
+    RUNNER_ASSERT_MSG_BT(app_index >= 0, "index not found: "  << app_index);
+
+    RUNNER_ASSERT_MSG_BT(apps[app_index].is_permanent == false,
+            "incorrect is_permanent value: " << apps[app_index].is_permanent);
+
+    result = perm_app_uninstall(APP_ID);
+    RUNNER_ASSERT_MSG_BT(result == 0, "perm_app_uninstall (2) returned " << perm_strerror(result));
+
+    DB_END
+}
+
+RUNNER_TEST(privilege_control05_perm_get_apps_with_permission_is_enabled)
+{
+    perm_app_status_t *apps = NULL;
+    size_t apps_count = 0;
+    int app_index = 0;
+    int result = 0;
+
+    DB_BEGIN
+
+    result = perm_app_install(APP_ID);
+    RUNNER_ASSERT_MSG_BT(result == 0, "perm_app_install returned " << perm_strerror(result));
+
+    // enable (i.e. register) permission for installed app
+
+    result = perm_app_enable_permissions(APP_ID, APP_TYPE_OSP, PRIVS_OSP, false);
+    RUNNER_ASSERT_MSG_BT(result == 0, "perm_app_enable_permissions returned " <<
+                         perm_strerror(result));
+
+    // change is_enabled to false
+    result = perm_app_disable_permissions(APP_ID, APP_TYPE_OSP, PRIVS_OSP);
+    RUNNER_ASSERT_MSG_BT(result == 0, "perm_app_disable_permissions returned " <<
+                         perm_strerror(result));
+
+    DB_END
+
+    result = perm_get_apps_with_permission(&apps, &apps_count, APP_TYPE_OSP, PRIVS_OSP[0]);
+
+    RUNNER_ASSERT_MSG_BT(result == 0, "perm_get_apps_with_permission returned " <<
+                         perm_strerror(result));
+
+    perm_app_status_list_t app_list;
+    app_list.status = apps;
+    app_list.size = apps_count;
+    ListAppStatusPtr listAppStatusPtr(&app_list, wrapper_perm_free_apps_list);
+
+    DB_BEGIN
+
+    app_index = get_app_index(apps, APP_ID, apps_count);
+
+    RUNNER_ASSERT_MSG_BT(app_index >= 0, "index not found: " << app_index);
+
+    RUNNER_ASSERT_MSG_BT(apps[app_index].is_enabled == false,
+            "incorrect is_enabled value: " << apps[app_index].is_enabled);
+
+    result = perm_app_uninstall(APP_ID);
+
+    RUNNER_ASSERT_MSG_BT(result == 0, "perm_app_uninstall returned " << perm_strerror(result));
+
+    DB_END
+}
+
+/**
  * Add new API feature
  */
 RUNNER_TEST(privilege_control11_add_api_feature)