Make ScopedAppLauncher child always notify the parent
[platform/core/test/security-tests.git] / src / security-manager-tests / common / scoped_app_launcher.cpp
index 8647535..50a9587 100644 (file)
 #include <sm_api.h>
 #include <sm_commons.h>
 #include <tests_common.h>
+#include <dpl/test/safe_cleanup.h>
 
 using namespace SecurityManagerTest;
 
-ScopedAppLauncher::ScopedAppLauncher(const AppInstallHelper& app)
+ScopedAppLauncher::ScopedAppLauncher(const AppInstallHelper& app) :
+    m_uid(app.getUID()),
+    m_appId(app.getAppId())
 {
-    SynchronizationPipe syncPipe;
-
     m_pid = fork();
     RUNNER_ASSERT_ERRNO_MSG(m_pid >= 0, "Fork failed");
     if (m_pid != 0) {
@@ -33,11 +34,15 @@ ScopedAppLauncher::ScopedAppLauncher(const AppInstallHelper& app)
     } else {
         m_syncPipe.claimChildEp();
 
-        RUNNER_ASSERT_ERRNO_MSG(setLauncherSecurityAttributes(app.getUID(), app.getGID()) == 0,
-                                "launcher failed");
-        Api::prepareAppCandidate();
-        Api::prepareApp(app.getAppId().c_str());
-
+        try {
+            RUNNER_ASSERT_ERRNO_MSG(setLauncherSecurityAttributes(app.getUID(), app.getGID()) == 0,
+                                    "launcher failed");
+            Api::prepareAppCandidate();
+            Api::prepareApp(app.getAppId().c_str());
+        } catch (...) {
+            m_syncPipe.post();
+            throw;
+        }
         m_syncPipe.post();
         m_syncPipe.wait();
         exit(0);
@@ -46,6 +51,9 @@ ScopedAppLauncher::ScopedAppLauncher(const AppInstallHelper& app)
 
 ScopedAppLauncher::~ScopedAppLauncher()
 {
-    m_syncPipe.post();
-    waitPid(m_pid);
+    SafeCleanup::run([this]{
+        m_syncPipe.post();
+        waitPid(m_pid);
+        Api::cleanupApp(m_appId, m_uid, m_pid);
+    });
 }