Spring cleaning
[platform/core/test/security-tests.git] / src / security-manager-tests / test_cases_prepare_app.cpp
index 9ff9179..ff00627 100644 (file)
 #include <sm_commons.h>
 #include <memory.h>
 #include <tests_common.h>
+#include <privilege_names.h>
 
 using namespace SecurityManagerTest;
+using namespace PrivilegeNames;
 
 namespace {
 bool finish = false;
@@ -45,9 +47,6 @@ const size_t THREADS = 10;
 
 const std::string APP_TEST_USER = "app_test_user";
 
-const std::string EXTERNAL_STORAGE_PRIVILEGE = "http://tizen.org/privilege/externalstorage";
-const std::string MEDIA_STORAGE_PRIVILEGE = "http://tizen.org/privilege/mediastorage";
-
 const std::string ACCESS_DENIED_DIR_PATH = "/usr/share/security-manager/dummy";
 const std::string EXTERNAL_STORAGE_DIR_PATH = "/opt/media";
 const std::string MEDIA_STORAGE_RW_DIR_PATH = "/opt/usr/media";
@@ -132,7 +131,7 @@ ino_t getFileInode(const std::string &path)
 
 std::string getTextFileContents(const std::string &path)
 {
-    std::ifstream in(path.c_str());
+    std::ifstream in(path);
     if (in.fail())
         return std::string();
     std::stringstream ss;
@@ -173,13 +172,13 @@ RUNNER_CHILD_TEST(security_manager_100_synchronize_credentials_test)
             for (size_t i = 0; i < THREADS; i++)
                 threads[i].run(i, expectedLabel);
 
-            Api::prepareApp(app.getAppId().c_str());
+            Api::prepareApp(app.getAppId());
         }
         RUNNER_ASSERT_MSG(thread_errors.empty(), std::endl << thread_errors);
         exit(0);
     } else {
         waitPid(pid);
-        Api::cleanupApp(app.getAppId().c_str(), tmpUser.getUid(), pid);
+        Api::cleanupApp(app.getAppId(), tmpUser.getUid(), pid);
     }
 }
 
@@ -226,7 +225,7 @@ RUNNER_CHILD_TEST(security_manager_101_create_namespace_test)
         synchPipe.claimParentEp();
         RUNNER_ASSERT_ERRNO_MSG(setLauncherSecurityAttributes(tmpUser) == 0, "launcher failed");
         Api::prepareAppCandidate();
-        Api::prepareApp(app.getAppId().c_str());
+        Api::prepareApp(app.getAppId());
         synchPipe.post();
         synchPipe.wait();
 
@@ -235,7 +234,7 @@ RUNNER_CHILD_TEST(security_manager_101_create_namespace_test)
         synchPipe.claimChildEp();
         synchPipe.wait();
 
-        std::string appBindPath = std::string("/var/run/user/") + std::to_string(tmpUser.getUid())
+        std::string appBindPath = std::string("/var/run/user/") + tmpUser.getUidString()
                                   + "/apps/" + app.generateAppLabel() + "/" + std::to_string(pid);
         std::string appProcPath = std::string("/proc/") + std::to_string(pid) + "/ns/mnt";
         std::string launcherProcPath = std::string("/proc/") + std::to_string(getpid()) + "/ns/mnt";
@@ -253,7 +252,7 @@ RUNNER_CHILD_TEST(security_manager_101_create_namespace_test)
 
         synchPipe.post();
         waitPid(pid);
-        Api::cleanupApp(app.getAppId().c_str(), tmpUser.getUid(), pid);
+        Api::cleanupApp(app.getAppId(), tmpUser.getUid(), pid);
     }
 }
 
@@ -272,7 +271,7 @@ RUNNER_CHILD_TEST(security_manager_102_check_propagation_test)
         synchPipe.claimParentEp();
         RUNNER_ASSERT_ERRNO_MSG(setLauncherSecurityAttributes(tmpUser) == 0, "launcher failed");
         Api::prepareAppCandidate();
-        Api::prepareApp(app.getAppId().c_str());
+        Api::prepareApp(app.getAppId());
         synchPipe.post();
         synchPipe.wait();
 
@@ -297,7 +296,7 @@ RUNNER_CHILD_TEST(security_manager_102_check_propagation_test)
 
         synchPipe.post();
         waitPid(pid);
-        Api::cleanupApp(app.getAppId().c_str(), tmpUser.getUid(), pid);
+        Api::cleanupApp(app.getAppId(), tmpUser.getUid(), pid);
     }
 }
 
@@ -307,8 +306,7 @@ RUNNER_CHILD_TEST(security_manager_103_policy_change_test)
     tmpUser.create();
 
     AppInstallHelper app("app103", tmpUser.getUid());
-    app.addPrivilege(EXTERNAL_STORAGE_PRIVILEGE);
-    app.addPrivilege(MEDIA_STORAGE_PRIVILEGE);
+    app.addPrivileges({PRIV_EXTERNALSTORAGE, PRIV_MEDIASTORAGE});
     ScopedInstaller appInstall(app);
 
     SynchronizationPipe synchPipe;
@@ -318,7 +316,7 @@ RUNNER_CHILD_TEST(security_manager_103_policy_change_test)
         synchPipe.claimParentEp();
         RUNNER_ASSERT_ERRNO_MSG(setLauncherSecurityAttributes(tmpUser) == 0, "launcher failed");
         Api::prepareAppCandidate();
-        Api::prepareApp(app.getAppId().c_str());
+        Api::prepareApp(app.getAppId());
         synchPipe.post();
         synchPipe.wait();
 
@@ -335,12 +333,12 @@ RUNNER_CHILD_TEST(security_manager_103_policy_change_test)
         RUNNER_ASSERT_ERRNO_MSG(result == false, "path is bound");
 
         PolicyRequest policyRequest;
-        PolicyEntry policyEntry(app.getAppId(), std::to_string(tmpUser.getUid()), EXTERNAL_STORAGE_PRIVILEGE);
-        policyEntry.setLevel("Deny");
+        PolicyEntry policyEntry(app.getAppId(), tmpUser.getUidString(), PRIV_EXTERNALSTORAGE);
+        policyEntry.setLevel(PolicyEntry::LEVEL_DENY);
         policyRequest.addEntry(policyEntry);
 
-        policyEntry = PolicyEntry(app.getAppId(), std::to_string(tmpUser.getUid()), MEDIA_STORAGE_PRIVILEGE);
-        policyEntry.setLevel("Deny");
+        policyEntry = PolicyEntry(app.getAppId(), tmpUser.getUidString(), PRIV_MEDIASTORAGE);
+        policyEntry.setLevel(PolicyEntry::LEVEL_DENY);
         policyRequest.addEntry(policyEntry);
         Api::sendPolicy(policyRequest);
 
@@ -351,12 +349,12 @@ RUNNER_CHILD_TEST(security_manager_103_policy_change_test)
         result = isPathBound(ACCESS_DENIED_DIR_PATH, MEDIA_STORAGE_RO_DIR_PATH, pid);
         RUNNER_ASSERT_ERRNO_MSG(result == true, "path is not bound");
 
-        policyEntry = PolicyEntry(app.getAppId(),  std::to_string(tmpUser.getUid()), EXTERNAL_STORAGE_PRIVILEGE);
-        policyEntry.setLevel("Allow");
+        policyEntry = PolicyEntry(app.getAppId(), tmpUser.getUidString(), PRIV_EXTERNALSTORAGE);
+        policyEntry.setLevel(PolicyEntry::LEVEL_ALLOW);
         policyRequest.addEntry(policyEntry);
 
-        policyEntry = PolicyEntry(app.getAppId(),  std::to_string(tmpUser.getUid()), MEDIA_STORAGE_PRIVILEGE);
-        policyEntry.setLevel("Allow");
+        policyEntry = PolicyEntry(app.getAppId(), tmpUser.getUidString(), PRIV_MEDIASTORAGE);
+        policyEntry.setLevel(PolicyEntry::LEVEL_ALLOW);
         policyRequest.addEntry(policyEntry);
         Api::sendPolicy(policyRequest);
 
@@ -369,7 +367,46 @@ RUNNER_CHILD_TEST(security_manager_103_policy_change_test)
 
         synchPipe.post();
         waitPid(pid);
-        Api::cleanupApp(app.getAppId().c_str(), tmpUser.getUid(), pid);
+        Api::cleanupApp(app.getAppId(), tmpUser.getUid(), pid);
+    }
+}
+
+RUNNER_CHILD_TEST(security_manager_104_policy_change_kill_app_test)
+{
+    TemporaryTestUser tmpUser(APP_TEST_USER, GUM_USERTYPE_NORMAL, false);
+    tmpUser.create();
+
+    AppInstallHelper app("app104", tmpUser.getUid());
+    app.addPrivileges({PRIV_EXTERNALSTORAGE, PRIV_MEDIASTORAGE});
+    ScopedInstaller appInstall(app);
+
+    SynchronizationPipe synchPipe;
+    pid_t pid = fork();
+    RUNNER_ASSERT_ERRNO_MSG(pid >= 0, "Fork failed");
+    if (pid == 0) {
+        synchPipe.claimParentEp();
+        try {
+            RUNNER_ASSERT_ERRNO_MSG(setLauncherSecurityAttributes(tmpUser) == 0, "launcher failed");
+            Api::prepareAppCandidate();
+            Api::prepareApp(app.getAppId());
+        } catch (...) {
+            synchPipe.post();
+            throw;
+        }
+        synchPipe.post();
+        exit(0);
+    } else {
+        synchPipe.claimChildEp();
+        synchPipe.wait();
+
+        PolicyRequest policyRequest;
+        PolicyEntry policyEntry(app.getAppId(), tmpUser.getUidString(), PRIV_EXTERNALSTORAGE);
+        policyEntry.setLevel(PolicyEntry::LEVEL_DENY);
+        policyRequest.addEntry(policyEntry);
+        Api::sendPolicy(policyRequest);
+
+        waitPid(pid);
+        Api::cleanupApp(app.getAppId(), tmpUser.getUid(), pid);
     }
 }
 
@@ -469,10 +506,7 @@ RUNNER_TEST(security_manager_200_prepare_app_perf)
     for (int i = 0; i < nAppsMax; i++) {
         apps.emplace_back(App{AppInstallHelper("app200_" + std::to_string(i), uid), 0});
         auto &hlp = apps.back().hlp;
-        for (const auto &p : { EXTERNAL_STORAGE_PRIVILEGE, MEDIA_STORAGE_PRIVILEGE,
-                std::string("http://tizen.org/privilege/camera"),
-                std::string("http://tizen.org/privilege/internet") })
-            hlp.addPrivilege(p);
+        hlp.addPrivileges({PRIV_EXTERNALSTORAGE, PRIV_MEDIASTORAGE, PRIV_CAMERA, PRIV_INTERNET});
         hlp.createSharedRODir();
         appInstalls.emplace_back(ScopedInstaller(hlp));
     }