Move label generation to global commons
[platform/core/test/security-tests.git] / src / security-manager-tests / common / sm_api.cpp
index c4a054c..d7ab650 100644 (file)
@@ -42,10 +42,10 @@ void uninstall(const InstallRequest &request, lib_retcode expectedResult)
                           << " Expected result: " << expectedResult);
 }
 
-std::string getPkgId(const char *appId, lib_retcode expectedResult)
+std::string getPkgId(const std::string &appId, lib_retcode expectedResult)
 {
     char *pkgId = nullptr;
-    int result = security_manager_get_app_pkgid(&pkgId, appId);
+    int result = security_manager_get_app_pkgid(&pkgId, appId.c_str());
     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
                       "getting pkg id from app id returned wrong value."
                           << " App id: " << appId << ";"
@@ -60,9 +60,9 @@ std::string getPkgId(const char *appId, lib_retcode expectedResult)
     return str;
 }
 
-void setProcessLabel(const char *appId, lib_retcode expectedResult)
+void setProcessLabel(const std::string &appId, lib_retcode expectedResult)
 {
-    int result = security_manager_set_process_label_from_appid(appId);
+    int result = security_manager_set_process_label_from_appid(appId.c_str());
     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
                       "setting process label from app id returned wrong value."
                           << " App id: " << appId << ";"
@@ -70,9 +70,9 @@ void setProcessLabel(const char *appId, lib_retcode expectedResult)
                           << " Expected result: " << expectedResult);
 }
 
-void setProcessGroups(const char *appId, lib_retcode expectedResult)
+void setProcessGroups(const std::string &appId, lib_retcode expectedResult)
 {
-    int result = security_manager_set_process_groups_from_appid(appId);
+    int result = security_manager_set_process_groups_from_appid(appId.c_str());
     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
                       "setting process groups from app id returned wrong value."
                           << " App id: " << appId << ";"
@@ -89,9 +89,9 @@ void dropProcessPrivileges(lib_retcode expectedResult)
                           << " Expected result: " << expectedResult);
 }
 
-void prepareApp(const char *appId, lib_retcode expectedResult)
+void prepareApp(const std::string &appId, lib_retcode expectedResult)
 {
-    int result = security_manager_prepare_app(appId);
+    int result = security_manager_prepare_app(appId.c_str());
     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
                       "preparing app returned wrong value."
                           << " App id: " << appId << ";"
@@ -206,14 +206,6 @@ void getPkgIdBySocket(int socketFd, std::string *pkgId, std::string *appId, lib_
                           << " Result: " << result << ";"
                           << " Expected result: " << expectedResult);
 
-    if (pkgId && result == SECURITY_MANAGER_SUCCESS) {
-        RUNNER_ASSERT_MSG(pkg_Id != nullptr, "getting pkg and app id did not allocate memory");
-    }
-
-    if (appId && result == SECURITY_MANAGER_SUCCESS) {
-        RUNNER_ASSERT_MSG(app_Id != nullptr, "getting pkg and app id did not allocate memory");
-    }
-
     if (pkg_Id) {
         *pkgId = pkg_Id;
         free(pkg_Id);
@@ -240,13 +232,32 @@ void getPkgIdByPid(pid_t pid, std::string *pkgId, std::string *appId, lib_retcod
                           << " Result: " << result << ";"
                           << " Expected result: " << expectedResult);
 
-    if (pkgId && result == SECURITY_MANAGER_SUCCESS) {
-        RUNNER_ASSERT_MSG(pkg_Id != nullptr, "getting pkg and app id did not allocate memory");
+    if (pkg_Id) {
+        *pkgId = pkg_Id;
+        free(pkg_Id);
     }
 
-    if (appId && result == SECURITY_MANAGER_SUCCESS) {
-        RUNNER_ASSERT_MSG(app_Id != nullptr, "getting pkg and app id did not allocate memory");
+    if (app_Id) {
+        *appId = app_Id;
+        free(app_Id);
     }
+}
+
+void getPkgIdByCynaraClient(const std::string &client, std::string *pkgId, std::string *appId,
+                            lib_retcode expectedResult)
+{
+    char *pkg_Id = nullptr;
+    char *app_Id = nullptr;
+
+    int result = security_manager_identify_app_from_cynara_client(client.c_str(),
+                    pkgId == nullptr ? nullptr : &pkg_Id,
+                    appId == nullptr ? nullptr : &app_Id);
+
+    RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
+                      "getting pkg id from pid returned wrong value."
+                          << " client: " << client << ";"
+                          << " Result: " << result << ";"
+                          << " Expected result: " << expectedResult);
 
     if (pkg_Id) {
         *pkgId = pkg_Id;
@@ -259,9 +270,10 @@ void getPkgIdByPid(pid_t pid, std::string *pkgId, std::string *appId, lib_retcod
     }
 }
 
-void appHasPrivilege(const char *appId, const char *privilege, uid_t user, int &value, lib_retcode expectedResult)
+void appHasPrivilege(const std::string &appId, const std::string &privilege, uid_t user,
+                     int &value, lib_retcode expectedResult)
 {
-    int result = security_manager_app_has_privilege(appId, privilege, user, &value);
+    int result = security_manager_app_has_privilege(appId.c_str(), privilege.c_str(), user, &value);
 
     RUNNER_ASSERT_MSG((lib_retcode)result == expectedResult,
                       "checking application privilege returned wrong result."
@@ -269,7 +281,7 @@ void appHasPrivilege(const char *appId, const char *privilege, uid_t user, int &
                           << " Expected result: " << expectedResult);
 }
 
-void getSecurityManagerGroups(char ***groups, size_t *groups_count, lib_retcode expectedResult)
+void getSecurityManagerGroups(gid_t **groups, size_t *groups_count, lib_retcode expectedResult)
 {
   int result = security_manager_groups_get(groups, groups_count);
   RUNNER_ASSERT_MSG(static_cast<lib_retcode>(result) == expectedResult,
@@ -277,6 +289,32 @@ void getSecurityManagerGroups(char ***groups, size_t *groups_count, lib_retcode
                     << " Result: " << result << " Expected: " << expectedResult);
 }
 
+void registerPaths(const PathsRequest& req, lib_retcode expectedResult)
+{
+    int result = security_manager_paths_register(req.get());
+    RUNNER_ASSERT_MSG(static_cast<lib_retcode>(result) == expectedResult,
+                      "Unexpected result in security_manager_paths_register()" << std::endl
+                      << " Result: " << result << " Expected: " << expectedResult);
+}
+
+void labelsMonitorGetFd(const LabelMonitor &monitor, int *fd, lib_retcode expectedResult)
+{
+    int result = security_manager_app_labels_monitor_get_fd(monitor.get(), fd);
+    RUNNER_ASSERT_MSG(static_cast<lib_retcode>(result) == expectedResult,
+                      "Unexpected result in security_manager_app_labels_monitor_get_fd()"
+                          << std::endl << " Result: " << result << " Expected: "
+                          << expectedResult);
+};
+
+void labelsProcess(const LabelMonitor &monitor, lib_retcode expectedResult)
+{
+    int result = security_manager_app_labels_monitor_process(monitor.get());
+    RUNNER_ASSERT_MSG(static_cast<lib_retcode>(result) == expectedResult,
+                      "Unexpected result in security_manager_app_labels_monitor_process()"
+                          << std::endl << " Result: " << result << " Expected: "
+                          << expectedResult);
+}
+
 } // namespace Api
 
 } // namespace SecurityManagerTest