Add tests for installing apps by different users
[platform/core/test/security-tests.git] / tests / security-manager-tests / security_manager_tests.cpp
index b33e468..0e505af 100644 (file)
 
 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 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"
@@ -74,6 +79,7 @@ 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 bool isLinkToExec(const char *fpath, const struct stat *sb)
@@ -289,6 +295,46 @@ static void check_app_after_uninstall(const char *const app_id, const char *cons
     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_BT((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_BT((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,
+            "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_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
+          "setting app id failed. Result: " << result);
+
+    result = security_manager_app_uninstall(request.get());
+    RUNNER_ASSERT_MSG_BT(!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)
 
@@ -404,6 +450,164 @@ RUNNER_TEST(security_manager_02_app_install_uninstall_full)
                               SM_ALLOWED_PRIVILEGES, TestSecurityManagerDatabase::REMOVED);
 }
 
+RUNNER_CHILD_TEST_SMACK(security_manager_03_set_label_from_binary)
+{
+    const char *const testBinaryPath    = LABELLED_BINARY_PATH;
+    const char *const expectedLabel     = USER_APP_ID;
+    int result;
+    char *label = NULL;
+    CStringPtr labelPtr;
+
+    result = security_manager_set_process_label_from_binary(testBinaryPath);
+    RUNNER_ASSERT_MSG_BT(result == SECURITY_MANAGER_SUCCESS,
+            "security_manager_set_process_label_from_binary(" <<
+            testBinaryPath << ") failed. Result: " << result);
+
+    result = smack_new_label_from_self(&label);
+    RUNNER_ASSERT_MSG_BT(result >= 0,
+            " Error getting current process label");
+    RUNNER_ASSERT_MSG_BT(label != NULL,
+            " Process label is not set");
+    labelPtr.reset(label);
+
+    result = strcmp(expectedLabel, label);
+    RUNNER_ASSERT_MSG_BT(result == 0,
+            " Process label is incorrect. Expected: \"" << expectedLabel << "\" Actual: \""
+            << label << "\"");
+}
+
+RUNNER_CHILD_TEST_NOSMACK(security_manager_03_set_label_from_binary_nosmack)
+{
+    const char *const testBinaryPath = LABELLED_BINARY_PATH;
+    int result;
+
+    result = security_manager_set_process_label_from_binary(testBinaryPath);
+    RUNNER_ASSERT_MSG_BT(result == SECURITY_MANAGER_SUCCESS,
+            "security_manager_set_process_label_from_binary(" <<
+            testBinaryPath << ") failed. Result: " << result);
+}
+
+RUNNER_CHILD_TEST_SMACK(security_manager_04_set_label_from_appid)
+{
+    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 = NULL;
+    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_BT(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_BT(result >= 0,
+            " Error getting current process label");
+    RUNNER_ASSERT_MSG_BT(label != NULL,
+            " Process label is not set");
+    labelPtr.reset(label);
+
+    result = strcmp(expected_label, label);
+    RUNNER_ASSERT_MSG_BT(result == 0,
+            " Process label is incorrect. Expected: \"" << expected_label <<
+            "\" Actual: \"" << label << "\"");
+
+    uninstall_app(app_id, pkg_id, true, true);
+}
+
+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;
+
+    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_BT(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(), app_id);
+    RUNNER_ASSERT_MSG_BT((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_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
+            "setting pkg id failed. Result: " << result);
+
+    result = security_manager_app_inst_req_add_path(request.get(), path, pathType);
+    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;
+
+
+    //switch user to non-root
+    result = drop_root_privileges();
+    RUNNER_ASSERT_MSG_BT(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_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_BT((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);
+
+    result = security_manager_app_install(request.get());
+    RUNNER_ASSERT_MSG_BT((lib_retcode)result == SECURITY_MANAGER_SUCCESS,
+            "installing app failed. Result: " << result);
+
+    //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_ID3);
+    RUNNER_ASSERT_MSG_BT((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,
+            "uninstalling app failed. Result: " << result);
+}
+
+
 int main(int argc, char *argv[])
 {
     SummaryCollector::Register();