Merge branch 'security-manager' into tizen
[platform/core/test/security-tests.git] / src / security-manager-tests / common / scoped_installer.h
index 88d7915..10e1ceb 100644 (file)
 
 #include <app_install_helper.h>
 #include <memory.h>
-#include <sm_db.h>
 #include <sm_request.h>
 #include <sm_api.h>
 #include <temp_test_user.h>
 #include <synchronization_pipe.h>
+#include <dpl/test/safe_cleanup.h>
 
 class ScopedInstaller {
 public:
-    ScopedInstaller(const AppInstallHelper &appInstallHelper)
-        : m_appInstallHelper(appInstallHelper)
+    ScopedInstaller(const AppInstallHelper &app, bool requestUid = true)
+        : m_appId(app.getAppId()),
+          m_uid(app.getUID()),
+          m_installType(app.getInstallType()),
+          m_shouldUninstall(true),
+          m_requestUid(requestUid),
+          m_creatorPid(getpid())
     {
         SecurityManagerTest::InstallRequest instReq;
-        instReq.setAppId(m_appInstallHelper.getAppId());
-        instReq.setPkgId(m_appInstallHelper.getPkgId());
-        instReq.setUid(m_appInstallHelper.getUID());
-        instReq.setAppTizenVersion(m_appInstallHelper.getVersion());
-
-        for (const auto& typePaths : m_appInstallHelper.getDirsMap())
+        instReq.setAppId(app.getAppId());
+        instReq.setPkgId(app.getPkgId());
+        if (app.getIsHybrid())
+            instReq.setHybrid();
+        if (app.getInstallType() != SM_APP_INSTALL_NONE)
+            instReq.setInstallType(app.getInstallType());
+        if (requestUid)
+            instReq.setUid(app.getUID());
+        if (!app.getVersion().empty())
+            instReq.setAppTizenVersion(app.getVersion());
+        if (!app.getAuthor().empty())
+            instReq.setAuthorId(app.getAuthor());
+        for (const auto& typePaths : app.getDirsMap())
             for (const auto& path : typePaths.second)
                 instReq.addPath(path, typePaths.first);
-        for (const auto& typePaths : m_appInstallHelper.getFilesMap())
+        for (const auto& typePaths : app.getFilesMap())
             for (const auto& path : typePaths.second)
                 instReq.addPath(path, typePaths.first);
-
+        for (const auto &priv : app.getPrivileges()) {
+            instReq.addPrivilege(priv.c_str());
+        }
         SecurityManagerTest::Api::install(instReq);
     }
 
+    ScopedInstaller(const ScopedInstaller &) = delete;
+    ScopedInstaller(ScopedInstaller &&other)
+        : m_appId(std::move(other.m_appId)),
+          m_uid(other.m_uid),
+          m_installType(other.m_installType),
+          m_shouldUninstall(other.m_shouldUninstall),
+          m_requestUid(other.m_requestUid),
+          m_creatorPid(other.m_creatorPid)
+    {
+        other.m_uid = 0;
+        other.m_shouldUninstall = false;
+        other.m_creatorPid = -1;
+    }
+
+    ScopedInstaller& operator=(const ScopedInstaller &) = delete;
+
     virtual ~ScopedInstaller() {
-        uninstallApp();
+        if (m_creatorPid == getpid())
+        {
+            SafeCleanup::run([this]{ uninstallApp(); });
+        }
     }
 
     void uninstallApp() {
+        if (!m_shouldUninstall)
+            return;
         SecurityManagerTest::InstallRequest uninstReq;
-        uninstReq.setAppId(m_appInstallHelper.getAppId());
-        uninstReq.setUid(m_appInstallHelper.getUID());
-
+        uninstReq.setAppId(m_appId);
+        if (m_requestUid)
+            uninstReq.setUid(m_uid);
+        if (m_installType != SM_APP_INSTALL_NONE)
+            uninstReq.setInstallType(m_installType);
         SecurityManagerTest::Api::uninstall(uninstReq);
-    }
-
-    AppInstallHelper& getAIH() {
-        return m_appInstallHelper;
+        m_shouldUninstall = false;
     }
 
 protected:
-    AppInstallHelper m_appInstallHelper;
+    std::string m_appId;
+    uid_t m_uid;
+    app_install_type m_installType;
+    bool m_shouldUninstall;
+    bool m_requestUid;
+    pid_t m_creatorPid;
 };