security-manager-tests: don't use semaphores in privacy_manager tests 02/60702/1
authorRafal Krypa <r.krypa@samsung.com>
Tue, 1 Mar 2016 17:01:36 +0000 (18:01 +0100)
committerRafal Krypa <r.krypa@samsung.com>
Tue, 1 Mar 2016 17:01:36 +0000 (18:01 +0100)
Drop usage of POSIX semaphores in
security_manager_*_privacy_manager_* test cases. It doesn't handle
failing scenario properly - tests are hanging, waiting inifinitely
on a semaphore.

Use internal SynchronizationPipe class instead, with a proper RAII
semantics.

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

index f25e818bbf10477dc7579b4e4ce298d0e726cc4b..de656d2d1aaf05c42a2bc8ecc5e61dcc6420bce3 100644 (file)
@@ -2,7 +2,6 @@
 #include <fcntl.h>
 #include <stdio.h>
 #include <memory.h>
-#include <semaphore.h>
 #include <unistd.h>
 
 #include <attr/xattr.h>
@@ -1081,14 +1080,11 @@ RUNNER_MULTIPROCESS_TEST(security_manager_10_privacy_manager_fetch_whole_policy_
     users2AppsMap.insert(std::pair<std::string, std::map<std::string, std::set<std::string>>>(username, apps2PrivsMap));
     //TEST DATA END
 
-    sem_t *mutex;
-    errno = 0;
-    RUNNER_ASSERT_MSG(((mutex = sem_open("mutex", O_CREAT, 0644, 1)) != SEM_FAILED), "Failure creating mutex, errno: " << errno);
-    errno = 0;
-    RUNNER_ASSERT_MSG(sem_init(mutex, 1, 0) == 0, "failed to setup mutex, errno: " << errno);
+    SynchronizationPipe pipe;
     pid_t pid = fork();
 
     if (pid != 0) { //parent process
+        pipe.claimParentEp();
         TemporaryTestUser tmpUser(username, GUM_USERTYPE_NORMAL, false);
         tmpUser.create();
 
@@ -1113,19 +1109,17 @@ RUNNER_MULTIPROCESS_TEST(security_manager_10_privacy_manager_fetch_whole_policy_
 
             //check_app_after_install(MANY_APPS[i].c_str(), MANY_APPS_PKGS[i].c_str());
         };
+
         //Start child process
-        errno = 0;
-        RUNNER_ASSERT_MSG(sem_post(mutex) ==  0, "Error while opening mutex, errno: " << errno);
+        pipe.post();
 
         int status;
         wait(&status);
 
         tmpUser.remove();
-    };
-
-    if (pid == 0) { //child process
-        errno = 0;
-        RUNNER_ASSERT_MSG(sem_wait(mutex) == 0, "sem_wait in child process failed, errno: " << errno);
+    } else { //child process
+        pipe.claimChildEp();
+        pipe.wait();
         //the above call, registers 1 new privilege for the given user, hence the incrementation of below variable
 
         struct passwd *pw = getUserStruct(username);
@@ -1190,14 +1184,11 @@ RUNNER_MULTIPROCESS_TEST(security_manager_11_privacy_manager_fetch_whole_policy_
     ++privileges_count;
     //TEST DATA END
 
-    sem_t *mutex;
-    errno = 0;
-    RUNNER_ASSERT_MSG(((mutex = sem_open("mutex", O_CREAT, 0644, 1)) != SEM_FAILED), "Failure creating mutex, errno: " << errno);
-    errno = 0;
-    RUNNER_ASSERT_MSG(sem_init(mutex, 1, 0) == 0, "failed to setup mutex, errno: " << errno);
+    SynchronizationPipe pipe;
     pid_t pid = fork();
 
     if (pid != 0) { //parent process
+        pipe.claimParentEp();
         std::vector<TemporaryTestUser> users = {
             TemporaryTestUser(usernames.at(0), GUM_USERTYPE_NORMAL, false),
             TemporaryTestUser(usernames.at(1), GUM_USERTYPE_ADMIN, false)
@@ -1227,21 +1218,20 @@ RUNNER_MULTIPROCESS_TEST(security_manager_11_privacy_manager_fetch_whole_policy_
 
             //check_app_after_install(MANY_APPS[i].c_str(), MANY_APPS_PKGS[i].c_str());
         };
+
         //Start child
-        errno = 0;
-        RUNNER_ASSERT_MSG(sem_post(mutex) ==  0, "Error while opening mutex, errno: " << errno);
+        pipe.post();
 
         int status;
         wait(&status);
 
-        for(auto &user : users) {
+        for (auto &user : users)
             user.remove();
-        };
-    };
 
-    if (pid == 0) {
-        errno = 0;
-        RUNNER_ASSERT_MSG(sem_wait(mutex) == 0, "sem_wait in child failed, errno: " << errno);
+    } else { //child process
+        pipe.claimChildEp();
+        pipe.wait();
+
         struct passwd *pw = getUserStruct(usernames.at(0));
         register_current_process_as_privilege_manager(pw->pw_uid);
 
@@ -1307,14 +1297,11 @@ RUNNER_MULTIPROCESS_TEST(security_manager_12_privacy_manager_fetch_whole_policy_
     privileges_count += 2;
     //TEST DATA END
 
-    sem_t *mutex;
-    errno = 0;
-    RUNNER_ASSERT_MSG(((mutex = sem_open("mutex", O_CREAT, 0644, 1)) != SEM_FAILED), "Failure creating mutex, errno: " << errno);
-    errno = 0;
-    RUNNER_ASSERT_MSG(sem_init(mutex, 1, 0) == 0, "failed to setup mutex, errno: " << errno);
+    SynchronizationPipe pipe;
     pid_t pid = fork();
 
     if (pid != 0) { //parent process
+        pipe.claimParentEp();
         std::vector<TemporaryTestUser> users = {
             TemporaryTestUser(usernames.at(0), GUM_USERTYPE_NORMAL, false),
             TemporaryTestUser(usernames.at(1), GUM_USERTYPE_ADMIN, false)
@@ -1344,22 +1331,19 @@ RUNNER_MULTIPROCESS_TEST(security_manager_12_privacy_manager_fetch_whole_policy_
             //check_app_after_install(MANY_APPS[i].c_str(), MANY_APPS_PKGS[i].c_str());
         };
 
-        //Start child
-        errno = 0;
-        RUNNER_ASSERT_MSG(sem_post(mutex) ==  0, "Error while opening mutex, errno: " << errno);
+        //Start child process
+        pipe.post();
 
         //Wait for child to finish
         int status;
         wait(&status);
 
-        for(auto &user : users) {
+        for (auto &user : users)
             user.remove();
-        };
-    };
 
-    if (pid == 0) { //child process
-        errno = 0;
-        RUNNER_ASSERT_MSG(sem_wait(mutex) == 0, "sem_wait in child failed, errno: " << errno);
+    } else { //child process
+        pipe.claimChildEp();
+        pipe.wait();
 
         struct passwd *pw = getUserStruct(usernames.at(1));
         register_current_process_as_privilege_manager(pw->pw_uid, true);
@@ -1423,21 +1407,15 @@ RUNNER_MULTIPROCESS_TEST(security_manager_13_privacy_manager_fetch_policy_after_
     //TEST DATA END
 
     pid_t pid[2];
-    sem_t *mutex[2];
-    errno = 0;
-    RUNNER_ASSERT_MSG(((mutex[0] = sem_open("mutex_1", O_CREAT, 0644, 1)) != SEM_FAILED), "Failure creating mutex #1, errno: " << errno);
-    errno = 0;
-    RUNNER_ASSERT_MSG(((mutex[1] = sem_open("mutex_2", O_CREAT, 0644, 1)) != SEM_FAILED), "Failure creating mutex #2, errno: " << errno);
-    errno = 0;
-    RUNNER_ASSERT_MSG(sem_init(mutex[0], 1, 0) == 0, "failed to setup mutex #1, errno: " << errno);
-    errno = 0;
-    RUNNER_ASSERT_MSG(sem_init(mutex[1], 1, 0) == 0, "failed to setup mutex #2, errno: " << errno);
+    SynchronizationPipe sync[2];
     std::vector<PolicyEntry> policyEntries;
 
     pid[0] = fork();
 
-    if(pid[0] == 0) { //child #1 process
-        RUNNER_ASSERT_MSG(sem_wait(mutex[0]) == 0, "sem_wait in child #1 failed, errno: " << errno);
+    if (pid[0] == 0) { //child #1 process
+        sync[0].claimChildEp();
+        sync[0].wait();
+
         struct passwd *pw = getUserStruct(usernames.at(0));
         register_current_process_as_privilege_manager(pw->pw_uid);
 
@@ -1473,14 +1451,13 @@ RUNNER_MULTIPROCESS_TEST(security_manager_13_privacy_manager_fetch_policy_after_
 
         RUNNER_ASSERT_MSG(policyEntries.size() == 2, "Number of policies doesn't match - should be: 2 and is " << policyEntries.size());
         exit(0);
-    };
-
-    if (pid[0] != 0) {//parent process
+    } else { //parent process
+        sync[0].claimParentEp();
         pid[1] = fork();
 
         if (pid[1] == 0) { //child #2 process
-            errno = 0;
-            RUNNER_ASSERT_MSG(sem_wait(mutex[1]) == 0, "sem_wait in child #2 failed, errno: " << errno);
+            sync[1].claimChildEp();
+            sync[1].wait();
             struct passwd *pw_target = getUserStruct(usernames.at(0));
             struct passwd *pw = getUserStruct(usernames.at(1));
             register_current_process_as_privilege_manager(pw->pw_uid);
@@ -1511,10 +1488,8 @@ RUNNER_MULTIPROCESS_TEST(security_manager_13_privacy_manager_fetch_policy_after_
             Api::getPolicyForAdmin(filter, policyEntries, SECURITY_MANAGER_ERROR_ACCESS_DENIED);
             RUNNER_ASSERT_MSG(policyEntries.size() == 0, "Policy is not empty");
             exit(0);
-        };
-
-        if (pid[1] != 0) { //parent
-
+        } else { //parent
+            sync[1].claimParentEp();
             std::vector<TemporaryTestUser> users = {
                 TemporaryTestUser(usernames.at(0), GUM_USERTYPE_NORMAL, false),
                 TemporaryTestUser(usernames.at(1), GUM_USERTYPE_ADMIN, false)
@@ -1548,26 +1523,21 @@ RUNNER_MULTIPROCESS_TEST(security_manager_13_privacy_manager_fetch_policy_after_
 
             int status;
             //Start child #1
-            errno = 0;
-            RUNNER_ASSERT_MSG(sem_post(mutex[0]) ==  0, "Error while opening mutex #1, errno: " << errno);
+            sync[0].post();
 
             //Wait until child #1 finishes
             pid_t ret = wait(&status);
             RUNNER_ASSERT_MSG((ret != -1) && WIFEXITED(status), "Updating privileges failed");
 
             //Start child #2
-            errno = 0;
-            RUNNER_ASSERT_MSG(sem_post(mutex[1]) ==  0, "Error while opening mutex #2, errno: " << errno);
+            sync[1].post();
+
             //Wait until child #2 finishes
             ret = wait(&status);
             RUNNER_ASSERT_MSG((ret =-1) && WIFEXITED(status), "Listing privileges failed");
 
-            for(auto &user : users) {
+            for (auto &user : users)
                 user.remove();
-            };
-
-            sem_close(mutex[0]);
-            sem_close(mutex[1]);
         };
     };
 }
@@ -1599,14 +1569,11 @@ RUNNER_MULTIPROCESS_TEST(security_manager_14_privacy_manager_fetch_and_update_po
 
     privileges_count += 2;
     //TEST DATA END
-    sem_t *mutex;
-    errno = 0;
-    RUNNER_ASSERT_MSG(((mutex = sem_open("mutex", O_CREAT, 0644, 1)) != SEM_FAILED), "Failure creating mutex, errno: " << errno);
-    errno = 0;
-    RUNNER_ASSERT_MSG(sem_init(mutex, 1, 0) == 0, "failed to setup mutex, errno: " << errno);
+    SynchronizationPipe pipe;
 
     pid_t pid = fork();
     if (pid != 0) {
+        pipe.claimParentEp();
         std::vector<TemporaryTestUser> users = {
             TemporaryTestUser(usernames.at(0), GUM_USERTYPE_NORMAL, false),
             TemporaryTestUser(usernames.at(1), GUM_USERTYPE_ADMIN, false)
@@ -1635,24 +1602,21 @@ RUNNER_MULTIPROCESS_TEST(security_manager_14_privacy_manager_fetch_and_update_po
                 Api::install(requestInst);
             };
         };
+
         //Start child process
-        errno = 0;
-        RUNNER_ASSERT_MSG(sem_post(mutex) ==  0, "Error while opening mutex, errno: " << errno);
+        pipe.post();
+
         int status;
         //Wait for child process to finish
         wait(&status);
 
         //switch back to root
-        for(auto &user : users) {
+        for (auto &user : users)
             user.remove();
-        };
 
-        sem_close(mutex);
-    }
-
-    if (pid == 0) { //child process
-        errno = 0;
-        RUNNER_ASSERT_MSG(sem_wait(mutex) == 0, "sem_wait in child process failed, errno: " << errno);
+    } else { //child process
+        pipe.claimChildEp();
+        pipe.wait();
 
         struct passwd *pw = getUserStruct(usernames.at(0));
         register_current_process_as_privilege_manager(pw->pw_uid, true);