security-manager: test security_manager_set_process_groups_from_appid
[platform/core/test/security-tests.git] / tests / security-manager-tests / security_manager_tests.cpp
index bdbef5a..5698d6d 100644 (file)
@@ -3,98 +3,57 @@
 #include <stdio.h>
 #include <memory.h>
 #include <summary_collector.h>
+#include <string>
+#include <unordered_set>
 
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <sys/xattr.h>
-#include <linux/xattr.h>
+#include <grp.h>
 
 #include <libprivilege-control_test_common.h>
 #include <tests_common.h>
 
 #include <security-manager.h>
+#include <sm_db.h>
+#include <cynara_test_client.h>
 
 DEFINE_SMARTPTR(security_manager_app_inst_req_free, app_inst_req, AppInstReqUniquePtr);
 
+static const char *const LABELLED_BINARY_PATH = "/usr/bin/test-app-efl";
+
 static const char *const SM_APP_ID1 = "sm_test_app_id_double";
 static const char *const SM_PKG_ID1 = "sm_test_pkg_id_double";
 
 static const char *const SM_APP_ID2 = "sm_test_app_id_full";
 static const char *const SM_PKG_ID2 = "sm_test_pkg_id_full";
 
-static const uid_t SM_ALLOWED_UID1 = 6666;
-static const uid_t SM_ALLOWED_UID2 = 7777;
-
-static const char *const SM_ALLOWED_PERMISSION1 = "security_manager_test_rules2_r";
-static const char *const SM_ALLOWED_PERMISSION2 = "security_manager_test_rules2_no_r";
-static const char *const XATTR_NAME_TIZENEXEC =  XATTR_SECURITY_PREFIX "TIZEN_EXEC_LABEL";
-
-static const rules_t SM_ALLOWED_RULES = {
-    { USER_APP_ID, "test_sm_book_8", "r" },
-    { USER_APP_ID, "test_sm_book_9", "w" },
-    { USER_APP_ID, "test_sm_book_10", "x" },
-    { USER_APP_ID, "test_sm_book_11", "rw" },
-    { USER_APP_ID, "test_sm_book_12", "rx" },
-    { USER_APP_ID, "test_sm_book_13", "wx" },
-    { USER_APP_ID, "test_sm_book_14", "rwx" },
-    { USER_APP_ID, "test_sm_book_15", "rwxat" },
-    { "test_sm_subject_8", USER_APP_ID, "r" },
-    { "test_sm_subject_9", USER_APP_ID, "w" },
-    { "test_sm_subject_10", USER_APP_ID, "x" },
-    { "test_sm_subject_11", USER_APP_ID, "rw" },
-    { "test_sm_subject_12", USER_APP_ID, "rx" },
-    { "test_sm_subject_13", USER_APP_ID, "wx" },
-    { "test_sm_subject_14", USER_APP_ID, "rwx" },
-    { "test_sm_subject_15", USER_APP_ID, "rwxat" }
+static const char *const SM_APP_ID3 = "sm_test_app_id_uid";
+static const char *const SM_PKG_ID3 = "sm_test_pkg_id_uid";
+
+static const privileges_t SM_ALLOWED_PRIVILEGES = {
+    "security_manager_test_rules2_r",
+    "security_manager_test_rules2_no_r"
 };
-static const rules_t SM_DENIED_RULES = {
-    { USER_APP_ID, "test_sm_book_1", "r" },
-    { USER_APP_ID, "test_sm_book_2", "w" },
-    { USER_APP_ID, "test_sm_book_3", "x" },
-    { USER_APP_ID, "test_sm_book_4", "rw" },
-    { USER_APP_ID, "test_sm_book_5", "rx" },
-    { USER_APP_ID, "test_sm_book_6", "wx" },
-    { USER_APP_ID, "test_sm_book_7", "rwx" },
-    { "test_sm_subject_1", USER_APP_ID, "r" },
-    { "test_sm_subject_2", USER_APP_ID, "w" },
-    { "test_sm_subject_3", USER_APP_ID, "x" },
-    { "test_sm_subject_4", USER_APP_ID, "rw" },
-    { "test_sm_subject_5", USER_APP_ID, "rx" },
-    { "test_sm_subject_6", USER_APP_ID, "wx" },
-    { "test_sm_subject_7", USER_APP_ID, "rwx" }
+
+static const privileges_t SM_DENIED_PRIVILEGES  = {
+    "security_manager_test_rules1",
+    "security_manager_test_rules2"
 };
 
-static const char *const SM_DENIED_PERMISSION1 = "security_manager_test_rules1";
-static const char *const SM_DENIED_PERMISSION2 = "security_manager_test_rules2";
+static const privileges_t SM_NO_PRIVILEGES  = {
+};
+
+static const std::vector<gid_t> SM_ALLOWED_GIDS = {6001, 6002};
 
 static const char *const SM_PRIVATE_PATH = "/etc/smack/test_DIR/app_dir";
 static const char *const SM_PUBLIC_PATH = "/etc/smack/test_DIR/app_dir_public";
 static const char *const SM_PUBLIC_RO_PATH = "/etc/smack/test_DIR/app_dir_public_ro";
 static const char *const SM_DENIED_PATH = "/etc/smack/test_DIR/non_app_dir";
+static const char *const SM_PRIVATE_PATH_FOR_USER_5000 = "/home/app/securitytests/test_DIR";
+static const char *const ANY_USER_REPRESENTATION = "anyuser";/*this may be actually any string*/
 
-
-static bool isLinkToExec(const char *fpath, const struct stat *sb)
+static void generateAppLabel(const std::string &pkgId, std::string &label)
 {
-
-    struct stat buf;
-    char *target;
-    int ret;
-
-    // check if it's a link
-    if ( !S_ISLNK(sb->st_mode))
-        return false;
-
-    target = realpath(fpath, NULL);
-    RUNNER_ASSERT_MSG_BT(target != 0, "Could not obtain real path from link.");
-
-    ret = stat(target, &buf);
-    RUNNER_ASSERT_MSG_BT(ret == 0, "Could not obtain real path's stat from link.");
-
-    if (buf.st_mode != (buf.st_mode | S_IXUSR | S_IFREG))
-        return false;
-
-
-    return true;
+    (void) pkgId;
+    label = "User";
 }
 
 static int nftw_check_sm_labels_app_dir(const char *fpath, const struct stat *sb,
@@ -102,56 +61,42 @@ static int nftw_check_sm_labels_app_dir(const char *fpath, const struct stat *sb
 {
     int result;
     CStringPtr labelPtr;
-    char* label = NULL;
+    char* label = nullptr;
 
     /* ACCESS */
     result = smack_lgetlabel(fpath, &label, SMACK_LABEL_ACCESS);
-    RUNNER_ASSERT_MSG_BT(result == 0, "Could not get label for the path");
+    RUNNER_ASSERT_MSG(result == 0, "Could not get label for the path");
     labelPtr.reset(label);
-    RUNNER_ASSERT_MSG_BT(label != NULL, "ACCESS label on " << fpath << " is not set");
+    RUNNER_ASSERT_MSG(label != nullptr, "ACCESS label on " << fpath << " is not set");
     result = strcmp(correctLabel, label);
-    RUNNER_ASSERT_MSG_BT(result == 0, "ACCESS label on " << fpath << " is incorrect"
+    RUNNER_ASSERT_MSG(result == 0, "ACCESS label on " << fpath << " is incorrect"
             " (should be '" << correctLabel << "' and is '" << label << "')");
 
 
     /* EXEC */
     result = smack_lgetlabel(fpath, &label, SMACK_LABEL_EXEC);
-    RUNNER_ASSERT_MSG_BT(result == 0, "Could not get label for the path");
+    RUNNER_ASSERT_MSG(result == 0, "Could not get label for the path");
     labelPtr.reset(label);
 
     if (S_ISREG(sb->st_mode) && (sb->st_mode & S_IXUSR) && exec_test) {
-        RUNNER_ASSERT_MSG_BT(label != NULL, "EXEC label on " << fpath << " is not set");
+        RUNNER_ASSERT_MSG(label != nullptr, "EXEC label on " << fpath << " is not set");
         result = strcmp(correctLabel, label);
-        RUNNER_ASSERT_MSG_BT(result == 0, "Incorrect EXEC label on executable file " << fpath);
+        RUNNER_ASSERT_MSG(result == 0, "Incorrect EXEC label on executable file " << fpath);
     } else
-        RUNNER_ASSERT_MSG_BT(label == NULL, "EXEC label on " << fpath << " is set");
-
-
-    /* LINK TO EXEC */
-    if (isLinkToExec(fpath, sb) && exec_test) {
-        char buf[SMACK_LABEL_LEN+1];
-        result = lgetxattr(fpath, XATTR_NAME_TIZENEXEC, buf, sizeof(buf));
-        RUNNER_ASSERT_MSG_BT(result != -1, "Could not get label for the path "
-                << fpath << "("<<strerror(errno)<<")");
-        buf[result]='\0';
-        result = strcmp(correctLabel, buf);
-        RUNNER_ASSERT_MSG_BT(result == 0, "Incorrect TIZEN_EXEC_LABEL attribute"
-                " on link to executable " << fpath);
-    }
-
+        RUNNER_ASSERT_MSG(label == nullptr, "EXEC label on " << fpath << " is set");
 
 
     /* TRANSMUTE */
     result = smack_lgetlabel(fpath, &label, SMACK_LABEL_TRANSMUTE);
-    RUNNER_ASSERT_MSG_BT(result == 0, "Could not get label for the path");
+    RUNNER_ASSERT_MSG(result == 0, "Could not get label for the path");
     labelPtr.reset(label);
 
     if (S_ISDIR(sb->st_mode) && transmute_test == true) {
-        RUNNER_ASSERT_MSG_BT(label != NULL, "TRANSMUTE label on " << fpath << " is not set at all");
-        RUNNER_ASSERT_MSG_BT(strcmp(label,"TRUE") == 0,
+        RUNNER_ASSERT_MSG(label != nullptr, "TRANSMUTE label on " << fpath << " is not set at all");
+        RUNNER_ASSERT_MSG(strcmp(label,"TRUE") == 0,
                 "TRANSMUTE label on " << fpath << " is not set properly: '"<<label<<"'");
     } else {
-        RUNNER_ASSERT_MSG_BT(label == NULL, "TRANSMUTE label on " << fpath << " is set");
+        RUNNER_ASSERT_MSG(label == nullptr, "TRANSMUTE label on " << fpath << " is set");
     }
 
     return 0;
@@ -178,21 +123,197 @@ static int nftw_check_sm_labels_app_floor_dir(const char *fpath, const struct st
     return nftw_check_sm_labels_app_dir(fpath, sb, "_", false, false);
 }
 
-
-RUNNER_TEST_GROUP_INIT(SECURITY_MANAGER)
-
 static app_inst_req* do_app_inst_req_new()
 {
     int result;
-    app_inst_req *req = NULL;
+    app_inst_req *req = nullptr;
 
     result = security_manager_app_inst_req_new(&req);
-    RUNNER_ASSERT_MSG_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
+    RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
             "creation of new request failed. Result: " << result);
-    RUNNER_ASSERT_MSG_BT(req != NULL, "creation of new request did not allocate memory");
+    RUNNER_ASSERT_MSG(req != nullptr, "creation of new request did not allocate memory");
     return req;
 }
 
+static void prepare_app_path()
+{
+    int result;
+
+    result = nftw(SM_PRIVATE_PATH, &nftw_remove_labels, FTW_MAX_FDS, FTW_PHYS);
+    RUNNER_ASSERT_MSG(result == 0, "Unable to clean Smack labels in " << SM_PRIVATE_PATH);
+
+    result = nftw(SM_PUBLIC_PATH, &nftw_remove_labels, FTW_MAX_FDS, FTW_PHYS);
+    RUNNER_ASSERT_MSG(result == 0, "Unable to clean Smack labels in " << SM_PUBLIC_PATH);
+
+    result = nftw(SM_PUBLIC_RO_PATH, &nftw_remove_labels, FTW_MAX_FDS, FTW_PHYS);
+    RUNNER_ASSERT_MSG(result == 0, "Unable to clean Smack labels in " << SM_PUBLIC_RO_PATH);
+
+    result = nftw(SM_DENIED_PATH, &nftw_set_labels_non_app_dir, FTW_MAX_FDS, FTW_PHYS);
+    RUNNER_ASSERT_MSG(result == 0, "Unable to set Smack labels in " << SM_DENIED_PATH);
+}
+
+static void prepare_app_env()
+{
+    prepare_app_path();
+}
+
+/* TODO: add parameters to this function */
+static void check_app_path_after_install()
+{
+    int result;
+
+    result = nftw(SM_PRIVATE_PATH, &nftw_check_sm_labels_app_private_dir, FTW_MAX_FDS, FTW_PHYS);
+    RUNNER_ASSERT_MSG(result == 0, "Unable to check Smack labels for " << SM_PRIVATE_PATH);
+
+    result = nftw(SM_PUBLIC_PATH, &nftw_check_sm_labels_app_public_dir, FTW_MAX_FDS, FTW_PHYS);
+    RUNNER_ASSERT_MSG(result == 0, "Unable to check Smack labels for " << SM_PUBLIC_PATH);
+
+    result = nftw(SM_PUBLIC_RO_PATH, &nftw_check_sm_labels_app_floor_dir, FTW_MAX_FDS, FTW_PHYS);
+    RUNNER_ASSERT_MSG(result == 0, "Unable to check Smack labels for " << SM_PUBLIC_RO_PATH);
+
+    result = nftw(SM_DENIED_PATH, &nftw_check_labels_non_app_dir, FTW_MAX_FDS, FTW_PHYS);
+    RUNNER_ASSERT_MSG(result == 0, "Unable to check Smack labels for " << SM_DENIED_PATH);
+}
+
+
+static void check_app_permissions(const char *const app_id, const char *const pkg_id, const char *const user,
+                                  const privileges_t &allowed_privs, const privileges_t &denied_privs)
+{
+    (void) app_id;
+    std::string smackLabel;
+    generateAppLabel(pkg_id, smackLabel);
+
+    CynaraTestClient ctc;
+
+    for (auto &priv : allowed_privs) {
+        ctc.check(smackLabel.c_str(), "", user, priv.c_str(), CYNARA_API_SUCCESS);
+    }
+
+    for (auto &priv : denied_privs) {
+        ctc.check(smackLabel.c_str(), "", user, priv.c_str(), CYNARA_API_ACCESS_DENIED);
+    }
+}
+
+static void check_app_gids(const char *const app_id, const std::vector<gid_t> &allowed_gids)
+{
+    int ret;
+    gid_t main_gid = getgid();
+    std::unordered_set<gid_t> reference_gids(allowed_gids.begin(), allowed_gids.end());
+
+    // Reset supplementary groups
+    ret = setgroups(0, NULL);
+    RUNNER_ASSERT_MSG(ret != -1, "Unable to set supplementary groups");
+
+    ret = security_manager_set_process_groups_from_appid(app_id);
+    RUNNER_ASSERT_MSG(ret == SECURITY_MANAGER_SUCCESS,
+            "security_manager_set_process_groups_from_appid(" <<
+            app_id << ") failed. Result: " << ret);
+
+    ret = getgroups(0, nullptr);
+    RUNNER_ASSERT_MSG(ret != -1, "Unable to get supplementary groups");
+
+    std::vector<gid_t> actual_gids(ret);
+    ret = getgroups(ret, actual_gids.data());
+    RUNNER_ASSERT_MSG(ret != -1, "Unable to get supplementary groups");
+
+    for (const auto &gid : actual_gids) {
+        RUNNER_ASSERT_MSG(gid == main_gid || reference_gids.count(gid) > 0,
+            "Application shouldn't get access to group " << gid);
+        reference_gids.erase(gid);
+    }
+
+    RUNNER_ASSERT_MSG(reference_gids.empty(), "Application didn't get access to some groups");
+}
+
+static void check_app_after_install(const char *const app_id, const char *const pkg_id,
+                                    const privileges_t &allowed_privs,
+                                    const privileges_t &denied_privs,
+                                    const std::vector<gid_t> &allowed_gids)
+{
+    TestSecurityManagerDatabase dbtest;
+    dbtest.test_db_after__app_install(app_id, pkg_id, allowed_privs);
+    dbtest.check_privileges_removed(app_id, pkg_id, denied_privs);
+
+    /*Privileges should be granted to all users if root installs app*/
+    check_app_permissions(app_id, pkg_id, ANY_USER_REPRESENTATION, allowed_privs, denied_privs);
+
+    /* Setup mapping of gids to privileges */
+    /* Do this for each privilege for extra check */
+    for (const auto &privilege : allowed_privs) {
+        dbtest.setup_privilege_gids(privilege, allowed_gids);
+    }
+
+    check_app_gids(app_id, allowed_gids);
+}
+
+static void check_app_after_install(const char *const app_id, const char *const pkg_id)
+{
+    TestSecurityManagerDatabase dbtest;
+    dbtest.test_db_after__app_install(app_id, pkg_id);
+}
+
+static void check_app_after_uninstall(const char *const app_id, const char *const pkg_id,
+                                      const privileges_t &privileges, const bool is_pkg_removed)
+{
+    TestSecurityManagerDatabase dbtest;
+    dbtest.test_db_after__app_uninstall(app_id, pkg_id, privileges, is_pkg_removed);
+
+
+    /*Privileges should not be granted anymore to any user*/
+    check_app_permissions(app_id, pkg_id, ANY_USER_REPRESENTATION, SM_NO_PRIVILEGES, privileges);
+}
+
+static void check_app_after_uninstall(const char *const app_id, const char *const pkg_id,
+                                      const bool is_pkg_removed)
+{
+    TestSecurityManagerDatabase dbtest;
+    dbtest.test_db_after__app_uninstall(app_id, pkg_id, is_pkg_removed);
+}
+
+static void install_app(const char *app_id, const char *pkg_id)
+{
+    int result;
+    AppInstReqUniquePtr request;
+    request.reset(do_app_inst_req_new());
+
+    result = security_manager_app_inst_req_set_app_id(request.get(), app_id);
+    RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
+            "setting app id failed. Result: " << result);
+
+    result = security_manager_app_inst_req_set_pkg_id(request.get(), pkg_id);
+    RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
+            "setting pkg id failed. Result: " << result);
+
+    result = security_manager_app_install(request.get());
+    RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
+            "installing app failed. Result: " << result);
+
+    check_app_after_install(app_id, pkg_id);
+
+}
+
+static void uninstall_app(const char *app_id, const char *pkg_id,
+        bool expect_installed, bool expect_pkg_removed)
+{
+    int result;
+    AppInstReqUniquePtr request;
+    request.reset(do_app_inst_req_new());
+
+    result = security_manager_app_inst_req_set_app_id(request.get(), app_id);
+    RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
+          "setting app id failed. Result: " << result);
+
+    result = security_manager_app_uninstall(request.get());
+    RUNNER_ASSERT_MSG(!expect_installed || (lib_retcode)result == SECURITY_MANAGER_SUCCESS,
+          "uninstalling app failed. Result: " << result);
+
+    check_app_after_uninstall(app_id, pkg_id, expect_pkg_removed);
+}
+
+
+RUNNER_TEST_GROUP_INIT(SECURITY_MANAGER)
+
+
 RUNNER_TEST(security_manager_01_app_double_install_double_uninstall)
 {
     int result;
@@ -201,160 +322,277 @@ RUNNER_TEST(security_manager_01_app_double_install_double_uninstall)
     request.reset(do_app_inst_req_new());
 
     result = security_manager_app_inst_req_set_app_id(request.get(), SM_APP_ID1);
-    RUNNER_ASSERT_MSG_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
+    RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
             "setting app id failed. Result: " << result);
 
     result = security_manager_app_inst_req_set_pkg_id(request.get(), SM_PKG_ID1);
-    RUNNER_ASSERT_MSG_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
+    RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
             "setting pkg id failed. Result: " << result);
 
     result = security_manager_app_install(request.get());
-    RUNNER_ASSERT_MSG_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
+    RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
             "installing app failed. Result: " << result);
 
     result = security_manager_app_install(request.get());
-    RUNNER_ASSERT_MSG_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
+    RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
             "installing already installed app failed. Result: " << result);
 
+    /* Check records in the security-manager database */
+    check_app_after_install(SM_APP_ID1, SM_PKG_ID1);
+
     request.reset(do_app_inst_req_new());
 
     result = security_manager_app_inst_req_set_app_id(request.get(), SM_APP_ID1);
-    RUNNER_ASSERT_MSG_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
+    RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
             "setting app id failed. Result: " << result);
 
     result = security_manager_app_uninstall(request.get());
-    RUNNER_ASSERT_MSG_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
+    RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
             "uninstalling app failed. Result: " << result);
 
     result = security_manager_app_uninstall(request.get());
-    RUNNER_ASSERT_MSG_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
+    RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
             "uninstalling already uninstalled app failed. Result: " << result);
+
+    /* Check records in the security-manager database */
+    check_app_after_uninstall(SM_APP_ID1, SM_PKG_ID1, TestSecurityManagerDatabase::REMOVED);
 }
 
-static void prepare_app_path()
+RUNNER_TEST(security_manager_02_app_install_uninstall_full)
 {
     int result;
+    AppInstReqUniquePtr request;
 
-    result = nftw(SM_PRIVATE_PATH, &nftw_remove_labels, FTW_MAX_FDS, FTW_PHYS);
-    RUNNER_ASSERT_MSG_BT(result == 0, "Unable to clean Smack labels in " << SM_PRIVATE_PATH);
+    prepare_app_env();
 
-    result = nftw(SM_PUBLIC_PATH, &nftw_remove_labels, FTW_MAX_FDS, FTW_PHYS);
-    RUNNER_ASSERT_MSG_BT(result == 0, "Unable to clean Smack labels in " << SM_PUBLIC_PATH);
+    request.reset(do_app_inst_req_new());
 
-    result = nftw(SM_PUBLIC_RO_PATH, &nftw_remove_labels, FTW_MAX_FDS, FTW_PHYS);
-    RUNNER_ASSERT_MSG_BT(result == 0, "Unable to clean Smack labels in " << SM_PUBLIC_RO_PATH);
+    result = security_manager_app_inst_req_set_app_id(request.get(), SM_APP_ID2);
+    RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
+            "setting app id failed. Result: " << result);
 
-    result = nftw(SM_DENIED_PATH, &nftw_set_labels_non_app_dir, FTW_MAX_FDS, FTW_PHYS);
-    RUNNER_ASSERT_MSG_BT(result == 0, "Unable to set Smack labels in " << SM_DENIED_PATH);
-}
+    result = security_manager_app_inst_req_set_pkg_id(request.get(), SM_PKG_ID2);
+    RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
+            "setting pkg id failed. Result: " << result);
 
-static void check_app_path_after_install()
-{
-    int result;
+    result = security_manager_app_inst_req_add_privilege(request.get(), SM_ALLOWED_PRIVILEGES[0].c_str());
+    RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
+            "setting allowed permission failed. Result: " << result);
+    result = security_manager_app_inst_req_add_privilege(request.get(), SM_ALLOWED_PRIVILEGES[1].c_str());
+    RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
+            "setting allowed permission failed. Result: " << result);
 
-    result = nftw(SM_PRIVATE_PATH, &nftw_check_sm_labels_app_private_dir, FTW_MAX_FDS, FTW_PHYS);
-    RUNNER_ASSERT_MSG_BT(result == 0, "Unable to check Smack labels for " << SM_PRIVATE_PATH);
+    result = security_manager_app_inst_req_add_path(request.get(), SM_PRIVATE_PATH,
+                                                    SECURITY_MANAGER_PATH_PRIVATE);
+    RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
+            "setting allowed path failed. Result: " << result);
 
-    result = nftw(SM_PUBLIC_PATH, &nftw_check_sm_labels_app_public_dir, FTW_MAX_FDS, FTW_PHYS);
-    RUNNER_ASSERT_MSG_BT(result == 0, "Unable to check Smack labels for " << SM_PUBLIC_PATH);
+    result = security_manager_app_inst_req_add_path(request.get(), SM_PUBLIC_PATH,
+                                                    SECURITY_MANAGER_PATH_PUBLIC);
+    RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
+            "setting allowed path failed. Result: " << result);
 
-    result = nftw(SM_PUBLIC_RO_PATH, &nftw_check_sm_labels_app_floor_dir, FTW_MAX_FDS, FTW_PHYS);
-    RUNNER_ASSERT_MSG_BT(result == 0, "Unable to check Smack labels for " << SM_PUBLIC_RO_PATH);
+    result = security_manager_app_inst_req_add_path(request.get(), SM_PUBLIC_RO_PATH,
+                                                    SECURITY_MANAGER_PATH_PUBLIC_RO);
+    RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
+            "setting allowed path failed. Result: " << result);
 
-    result = nftw(SM_DENIED_PATH, &nftw_check_labels_non_app_dir, FTW_MAX_FDS, FTW_PHYS);
-    RUNNER_ASSERT_MSG_BT(result == 0, "Unable to check Smack labels for " << SM_DENIED_PATH);
+    result = security_manager_app_install(request.get());
+    RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
+            "installing app failed. Result: " << result);
+
+    /* Check records in the security-manager database */
+    check_app_after_install(SM_APP_ID2, SM_PKG_ID2,
+                            SM_ALLOWED_PRIVILEGES, SM_DENIED_PRIVILEGES, SM_ALLOWED_GIDS);
+
+    /* TODO: add parameters to this function */
+    check_app_path_after_install();
+
+    request.reset(do_app_inst_req_new());
+
+    result = security_manager_app_inst_req_set_app_id(request.get(), SM_APP_ID2);
+    RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
+            "setting app id failed. Result: " << result);
+
+    result = security_manager_app_uninstall(request.get());
+    RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
+            "uninstalling app failed. Result: " << result);
+
+    /* Check records in the security-manager database,
+     * all previously allowed privileges should be removed */
+    check_app_after_uninstall(SM_APP_ID2, SM_PKG_ID2,
+                              SM_ALLOWED_PRIVILEGES, TestSecurityManagerDatabase::REMOVED);
 }
 
-static void check_app_permission_after_install()
+RUNNER_CHILD_TEST_SMACK(security_manager_03_set_label_from_binary)
 {
-    bool result;
+    const char *const testBinaryPath    = LABELLED_BINARY_PATH;
+    const char *const expectedLabel     = USER_APP_ID;
+    int result;
+    char *label = nullptr;
+    CStringPtr labelPtr;
 
-    result = check_all_accesses(smack_check(), SM_ALLOWED_RULES);
-    RUNNER_ASSERT_MSG_BT(result, "Permissions not added.");
-    result = check_no_accesses(smack_check(), SM_DENIED_RULES);
-    RUNNER_ASSERT_MSG_BT(result, "Permissions added.");
+    result = security_manager_set_process_label_from_binary(testBinaryPath);
+    RUNNER_ASSERT_MSG(result == SECURITY_MANAGER_SUCCESS,
+            "security_manager_set_process_label_from_binary(" <<
+            testBinaryPath << ") failed. Result: " << result);
 
-    check_perm_app_has_permission(USER_APP_ID, SM_ALLOWED_PERMISSION1, true);
-    check_perm_app_has_permission(USER_APP_ID, SM_ALLOWED_PERMISSION2, true);
-    check_perm_app_has_permission(USER_APP_ID, SM_DENIED_PERMISSION1, false);
-    check_perm_app_has_permission(USER_APP_ID, SM_DENIED_PERMISSION2, false);
+    result = smack_new_label_from_self(&label);
+    RUNNER_ASSERT_MSG(result >= 0,
+            " Error getting current process label");
+    RUNNER_ASSERT_MSG(label != nullptr,
+            " Process label is not set");
+    labelPtr.reset(label);
+
+    result = strcmp(expectedLabel, label);
+    RUNNER_ASSERT_MSG(result == 0,
+            " Process label is incorrect. Expected: \"" << expectedLabel << "\" Actual: \""
+            << label << "\"");
 }
 
-static void prepare_app_env()
+RUNNER_CHILD_TEST_NOSMACK(security_manager_03_set_label_from_binary_nosmack)
 {
-    prepare_app_path();
+    const char *const testBinaryPath = LABELLED_BINARY_PATH;
+    int result;
+
+    result = security_manager_set_process_label_from_binary(testBinaryPath);
+    RUNNER_ASSERT_MSG(result == SECURITY_MANAGER_SUCCESS,
+            "security_manager_set_process_label_from_binary(" <<
+            testBinaryPath << ") failed. Result: " << result);
 }
 
-static void check_app_env_after_install()
+RUNNER_CHILD_TEST_SMACK(security_manager_04_set_label_from_appid)
 {
-    check_app_path_after_install();
-    check_app_permission_after_install();
+    const char *const app_id = "sm_test_app_id_set_label_from_appid";
+    const char *const pkg_id = "sm_test_pkg_id_set_label_from_appid";
+    const char *const expected_label = USER_APP_ID;
+    char *label = nullptr;
+    CStringPtr labelPtr;
+    int result;
+
+    uninstall_app(app_id, pkg_id, false, true);
+    install_app(app_id, pkg_id);
+
+    result = security_manager_set_process_label_from_appid(app_id);
+    RUNNER_ASSERT_MSG(result == SECURITY_MANAGER_SUCCESS,
+            "security_manager_set_process_label_from_appid(" <<
+            app_id << ") failed. Result: " << result);
+
+    result = smack_new_label_from_self(&label);
+    RUNNER_ASSERT_MSG(result >= 0,
+            " Error getting current process label");
+    RUNNER_ASSERT_MSG(label != nullptr,
+            " Process label is not set");
+    labelPtr.reset(label);
+
+    result = strcmp(expected_label, label);
+    RUNNER_ASSERT_MSG(result == 0,
+            " Process label is incorrect. Expected: \"" << expected_label <<
+            "\" Actual: \"" << label << "\"");
+
+    uninstall_app(app_id, pkg_id, true, true);
 }
 
-RUNNER_TEST(security_manager_02_app_install_uninstall_full)
+RUNNER_CHILD_TEST_NOSMACK(security_manager_04_set_label_from_appid_nosmack)
 {
+    const char *const app_id = "sm_test_app_id_set_label_from_appid";
+    const char *const pkg_id = "sm_test_pkg_id_set_label_from_appid";
     int result;
-    AppInstReqUniquePtr request;
 
-    prepare_app_env();
+    uninstall_app(app_id, pkg_id, false, true);
+    install_app(app_id, pkg_id);
+
+    result = security_manager_set_process_label_from_appid(app_id);
+    RUNNER_ASSERT_MSG(result == SECURITY_MANAGER_SUCCESS,
+            "security_manager_set_process_label_from_appid(" <<
+            app_id << ") failed. Result: " << result);
+
+    uninstall_app(app_id, pkg_id, true, true);
+}
+
 
+
+static void prepare_request(AppInstReqUniquePtr &request,
+              const char *const app_id,
+              const char *const pkg_id,
+              app_install_path_type pathType,
+              const char *const path)
+{
+    int result;
     request.reset(do_app_inst_req_new());
 
-    result = security_manager_app_inst_req_set_app_id(request.get(), SM_APP_ID2);
-    RUNNER_ASSERT_MSG_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
+    result = security_manager_app_inst_req_set_app_id(request.get(), app_id);
+    RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
             "setting app id failed. Result: " << result);
 
-    result = security_manager_app_inst_req_set_pkg_id(request.get(), SM_PKG_ID2);
-    RUNNER_ASSERT_MSG_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
+    result = security_manager_app_inst_req_set_pkg_id(request.get(), pkg_id);
+    RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
             "setting pkg id failed. Result: " << result);
 
-    result = security_manager_app_inst_req_add_allowed_user(request.get(), SM_ALLOWED_UID1);
-    RUNNER_ASSERT_MSG_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
-            "setting allowed user failed. Result: " << result);
-    result = security_manager_app_inst_req_add_allowed_user(request.get(), SM_ALLOWED_UID2);
-    RUNNER_ASSERT_MSG_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
-            "setting allowed user failed. Result: " << result);
+    result = security_manager_app_inst_req_add_path(request.get(), path, pathType);
+    RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
+            "setting allowed path failed. Result: " << result);
+}
 
-    result = security_manager_app_inst_req_add_privilege(request.get(), SM_ALLOWED_PERMISSION1);
-    RUNNER_ASSERT_MSG_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
-            "setting allowed permission failed. Result: " << result);
-    result = security_manager_app_inst_req_add_privilege(request.get(), SM_ALLOWED_PERMISSION2);
-    RUNNER_ASSERT_MSG_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
-            "setting allowed permission failed. Result: " << result);
 
-    result = security_manager_app_inst_req_add_path(request.get(), SM_PRIVATE_PATH,
-                                                    SECURITY_MANAGER_PATH_PRIVATE);
-    RUNNER_ASSERT_MSG_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
-            "setting allowed path failed. Result: " << result);
 
-    result = security_manager_app_inst_req_add_path(request.get(), SM_PUBLIC_PATH,
-                                                    SECURITY_MANAGER_PATH_PUBLIC);
-    RUNNER_ASSERT_MSG_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
-            "setting allowed path failed. Result: " << result);
+RUNNER_CHILD_TEST(security_manager_05_app_install_uninstall_by_uid_5000)
+{
+    int result;
+    AppInstReqUniquePtr request;
+    const std::string user =  std::to_string(static_cast<unsigned int>(APP_UID));
 
-    result = security_manager_app_inst_req_add_path(request.get(), SM_PUBLIC_RO_PATH,
-                                                    SECURITY_MANAGER_PATH_PUBLIC_RO);
-    RUNNER_ASSERT_MSG_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
-            "setting allowed path failed. Result: " << result);
+
+    //switch user to non-root
+    result = drop_root_privileges();
+    RUNNER_ASSERT_MSG(result == 0, "drop_root_privileges failed");
+
+    //install app as non-root user and try to register public path (should fail)
+    prepare_request(request, SM_APP_ID3, SM_PKG_ID3, SECURITY_MANAGER_PATH_PUBLIC, SM_PRIVATE_PATH_FOR_USER_5000);
 
     result = security_manager_app_install(request.get());
-    RUNNER_ASSERT_MSG_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
+    RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_ERROR_AUTHENTICATION_FAILED,
+            "installing app not failed. Result: " << result);
+
+    //install app as non-root user
+    //should fail (non-root users may only register folders inside their home)
+    prepare_request(request, SM_APP_ID3, SM_PKG_ID3, SECURITY_MANAGER_PATH_PRIVATE, SM_PRIVATE_PATH);
+
+    result = security_manager_app_install(request.get());
+    RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_ERROR_AUTHENTICATION_FAILED,
+            "installing app not failed. Result: " << result);
+
+    //install app as non-root user
+    //should succeed - this time i register folder inside user's home dir
+    prepare_request(request, SM_APP_ID3, SM_PKG_ID3, SECURITY_MANAGER_PATH_PRIVATE, SM_PRIVATE_PATH_FOR_USER_5000);
+
+    for (auto &privilege : SM_ALLOWED_PRIVILEGES) {
+        result = security_manager_app_inst_req_add_privilege(request.get(), privilege.c_str());
+        RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
+            "setting allowed permission failed. Result: " << result);
+    }
+
+    result = security_manager_app_install(request.get());
+    RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
             "installing app failed. Result: " << result);
 
-    check_app_env_after_install();
+    check_app_permissions(SM_APP_ID3, SM_PKG_ID3, user.c_str(), SM_ALLOWED_PRIVILEGES, SM_DENIED_PRIVILEGES);
 
+    //uninstall app as non-root user
     request.reset(do_app_inst_req_new());
 
-    result = security_manager_app_inst_req_set_app_id(request.get(), SM_APP_ID2);
-    RUNNER_ASSERT_MSG_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
+    result = security_manager_app_inst_req_set_app_id(request.get(), SM_APP_ID3);
+    RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
             "setting app id failed. Result: " << result);
 
     result = security_manager_app_uninstall(request.get());
-    RUNNER_ASSERT_MSG_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
+    RUNNER_ASSERT_MSG((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
             "uninstalling app failed. Result: " << result);
 
+    check_app_permissions(SM_APP_ID3, SM_PKG_ID3, user.c_str(), SM_NO_PRIVILEGES, SM_ALLOWED_PRIVILEGES);
 }
 
+
 int main(int argc, char *argv[])
 {
     SummaryCollector::Register();