Add init and finish functionality
[platform/core/test/security-tests.git] / src / framework / src / test_runner_child.cpp
index b4157ba..fcb745e 100644 (file)
  *    See the License for the specific language governing permissions and
  *    limitations under the License.
  */
-/*
+/**
  * @file        test_runner_child.cpp
  * @author      Bartlomiej Grzelewski (b.grzelewski@samsung.com)
  * @version     1.0
  * @brief       This file is the implementation file of test runner
  */
 #include <stddef.h>
+#include <dpl/assert.h>
+#include <dpl/test/test_failed.h>
+#include <dpl/test/test_ignored.h>
 #include <dpl/test/test_runner.h>
 #include <dpl/test/test_runner_child.h>
 #include <dpl/test/test_results_collector.h>
@@ -285,17 +288,17 @@ PipeWrapper::Status PipeWrapper::readHelp(void *buf, int size, time_t deadline)
     return SUCCESS;
 }
 
-void RunChildProc(TestRunner::TestCase procChild)
+void RunChildProc(const std::function<void(void)> &testFunc)
 {
     PipeWrapper pipe;
     if (!pipe.isReady()) {
-        throw TestRunner::TestFailed("Pipe creation failed");
+        throw TestFailed("Pipe creation failed");
     }
 
     pid_t pid = fork();
 
     if (pid == -1) {
-        throw TestRunner::TestFailed("Child creation failed");
+        throw TestFailed("Child creation failed");
     }
 
     if (pid != 0) {
@@ -317,19 +320,19 @@ void RunChildProc(TestRunner::TestCase procChild)
         waitpid(pid, &status, 0);
 
         if (pipeReturn == PipeWrapper::TIMEOUT) {
-            throw TestRunner::TestFailed("Timeout");
+            throw TestFailed("Timeout");
         }
 
         if (pipeReturn == PipeWrapper::ERROR) {
-            throw TestRunner::TestFailed("Reading pipe error");
+            throw TestFailed("Reading pipe error");
         }
 
         TestRunnerSingleton::Instance().setCurrentTestCasePerformanceResult(performance);
 
         if (code == CHILD_TEST_FAIL) {
-            throw TestRunner::TestFailed(message);
+            throw TestFailed(message);
         } else if (code == CHILD_TEST_IGNORED) {
-            throw TestRunner::Ignored(message);
+            throw TestIgnored(message);
         }
     } else {
         // child code
@@ -337,9 +340,6 @@ void RunChildProc(TestRunner::TestCase procChild)
         // End Runner after current test
         TestRunnerSingleton::Instance().Terminate();
 
-        int code = CHILD_TEST_PASS;
-        std::string msg;
-
         bool allowLogs = TestRunnerSingleton::Instance().GetAllowChildLogs();
 
         close(STDIN_FILENO);
@@ -349,17 +349,20 @@ void RunChildProc(TestRunner::TestCase procChild)
 
         pipe.setUsage(PipeWrapper::WRITEONLY);
 
-        try {
-            procChild();
-        } catch (const DPL::Test::TestRunner::TestFailed &e) {
-            msg = e.GetMessage();
-            code = CHILD_TEST_FAIL;
-        } catch (const DPL::Test::TestRunner::Ignored &e) {
-            msg = e.GetMessage();
-            code = CHILD_TEST_IGNORED;
-        } catch (...) { // catch all exception generated by "user" code
-            msg = "unhandled exeception";
-            code = CHILD_TEST_FAIL;
+        int code;
+        std::string msg;
+        switch (TryCatch(testFunc, msg)) {
+            case TestResult::FailStatus::FAILED:
+                code = CHILD_TEST_FAIL;
+                break;
+            case TestResult::FailStatus::IGNORED:
+                code = CHILD_TEST_IGNORED;
+                break;
+            case TestResult::FailStatus::NONE:
+                code = CHILD_TEST_PASS;
+                break;
+            default:
+                Assert(false && "Unhandled fail status");
         }
 
         if (allowLogs) {