Fix thread credential synchronization tests 63/108063/1
authorRafal Krypa <r.krypa@samsung.com>
Mon, 2 Jan 2017 13:03:37 +0000 (14:03 +0100)
committerRafal Krypa <r.krypa@samsung.com>
Mon, 2 Jan 2017 14:34:23 +0000 (15:34 +0100)
Tests in SECURITY_MANAGER_PREPARE_APP group consisted of 3 dependant
test cases: prepare, actual test and cleanup.
Now they are merged into single test case that doesn't depend on any
preparations done by other test cases.

Change-Id: I734a7c992b4edb32e7db2f7581c501daed3c4929
Signed-off-by: Rafal Krypa <r.krypa@samsung.com>
src/security-manager-tests/test_cases_prepare_app.cpp

index 7acfa82..bebb90a 100644 (file)
 #include <dpl/test/test_runner.h>
 
 #include <app_install_helper.h>
+#include <scoped_installer.h>
 #include <sm_api.h>
 #include <sm_commons.h>
 #include <memory.h>
+#include <tests_common.h>
 
 using namespace SecurityManagerTest;
 
@@ -56,7 +58,7 @@ std::mutex error_mutex;
         }                                                                      \
     } while (0)
 
-void threadFn(int i)
+void threadFn(int i, const std::string &expectedLabel)
 {
     if (i % 2 == 0) {
         // block all signals
@@ -68,9 +70,6 @@ void threadFn(int i)
     while (!finish)
         usleep(1000);
 
-    AppInstallHelper helper(SYNC_TEST_APP.c_str());
-    std::string expectedLabel = helper.generateAppLabel();
-
     char* label;
     THREAD_ASSERT_MSG(smack_new_label_from_self(&label) > 0, "smack_new_label_from_self failed");
     CStringPtr labelPtr(label);
@@ -100,10 +99,10 @@ struct ThreadWrapper
         thread.join();
     }
 
-    void run(int i)
+    void run(int i, const std::string &expectedLabel)
     {
         THREAD_ASSERT_MSG(!thread.joinable(), "Thread already started");
-        thread = std::thread(threadFn, i);
+        thread = std::thread(threadFn, i, expectedLabel);
     }
 
     std::thread thread;
@@ -113,36 +112,26 @@ struct ThreadWrapper
 
 RUNNER_TEST_GROUP_INIT(SECURITY_MANAGER_PREPARE_APP)
 
-// TODO merge it into one test
-RUNNER_TEST(security_manager_100_synchronize_credentials_1_prepare)
-{
-    AppInstallHelper helper(SYNC_TEST_APP.c_str());
-
-    InstallRequest install;
-    install.setAppId(helper.getAppId());
-    install.setPkgId(helper.getPkgId());
-    Api::install(install);
-}
-
-RUNNER_CHILD_TEST(security_manager_100_synchronize_credentials_2_test)
+RUNNER_CHILD_TEST(security_manager_100_synchronize_credentials_test)
 {
-    AppInstallHelper helper(SYNC_TEST_APP.c_str());
-    {
-        ThreadWrapper threads[THREADS];
-
-        for (size_t i = 0; i < THREADS; i++)
-            threads[i].run(i);
-
-        Api::prepareApp(helper.getAppId().c_str());
+    AppInstallHelper app(SYNC_TEST_APP.c_str());
+    ScopedInstaller appInstall(app);
+    const std::string expectedLabel = app.generateAppLabel();
+
+    pid_t pid = fork();
+    RUNNER_ASSERT_ERRNO_MSG(pid >= 0, "Fork failed");
+    if (pid == 0) {
+        {
+            ThreadWrapper threads[THREADS];
+
+            for (size_t i = 0; i < THREADS; i++)
+                threads[i].run(i, expectedLabel);
+
+            Api::prepareApp(app.getAppId().c_str());
+        }
+        RUNNER_ASSERT_MSG(thread_errors.empty(), std::endl << thread_errors);
+        exit(0);
+    } else {
+        waitPid(pid);
     }
-    RUNNER_ASSERT_MSG(thread_errors.empty(), std::endl << thread_errors);
-}
-
-RUNNER_TEST(security_manager_100_synchronize_credentials_3_cleanup)
-{
-    AppInstallHelper helper(SYNC_TEST_APP.c_str());
-
-    InstallRequest uninstall;
-    uninstall.setAppId(helper.getAppId());
-    Api::uninstall(uninstall);
 }