security-manager-tests: Fix security_manager_12_(...) 42/66742/1
authorRafal Krypa <r.krypa@samsung.com>
Wed, 20 Apr 2016 15:00:46 +0000 (17:00 +0200)
committerRafal Krypa <r.krypa@samsung.com>
Wed, 20 Apr 2016 15:01:03 +0000 (17:01 +0200)
Fix the test fetching entire policy with privacy-manager API.
This test incorrectly assumes that the only policy fetched from
security-manager is policy for test users and applications created
in this test case. But security-manager, when queried for entire
policy by admin user fetches policy for all apps, including preloaded.

This fix first queries security-manager for existing policy, then
adds test users and apps, fetches the policy again and finally
analyzes the difference.

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

index 043b8d16b80001cc781b96748323e6fdc2c31029..debda17bdb00b037f02818fbd4802096b16f65bd 100644 (file)
@@ -121,6 +121,39 @@ void PolicyEntry::free(void)
     security_manager_policy_entry_free(m_entry);
 }
 
+bool PolicyEntry::operator==(const PolicyEntry &other) const
+{
+    auto cmp = [](const std::pair<bool, std::string> &a, const std::pair<bool, std::string> &b)->bool
+    {
+        return (a.first) ? (b.first && a.second == b.second) : !b.first;
+    };
+
+    return (
+        cmp(m_appId, other.m_appId) &&
+        cmp(m_user, other.m_user) &&
+        cmp(m_privilege, other.m_privilege) &&
+        cmp(m_currentLevel, other.m_currentLevel) &&
+        cmp(m_maxLevel, other.m_maxLevel));
+}
+
+std::string PolicyEntry::toString() const
+{
+    std::stringstream ss;
+    auto append = [&](const std::pair<bool, std::string> &x)
+    {
+        if (x.first)
+            ss << x.second;
+        ss << '\0';
+    };
+
+    append(m_appId);
+    append(m_user);
+    append(m_privilege);
+    append(m_currentLevel);
+    append(m_maxLevel);
+
+    return ss.str();
+}
 
 PolicyRequest::PolicyRequest()
     : m_req(nullptr),
index bd3132965510ba44a8addc9420c689ead2134b53..4c121027737624324fa5800de735049d3f339604 100644 (file)
@@ -50,6 +50,8 @@ public:
     void free(void);
 
     friend std::ostream& operator<<(std::ostream &, const PolicyEntry&);
+    bool operator==(const PolicyEntry &) const;
+    std::string toString() const;
 
 private:
     policy_entry *m_entry;
@@ -84,4 +86,13 @@ std::ostream& operator<<(std::ostream &os, const SecurityManagerTest::PolicyRequ
 
 } // namespace SecurityManagerTest
 
+namespace std {
+
+template<>
+struct hash<SecurityManagerTest::PolicyEntry> {
+    size_t operator()(const SecurityManagerTest::PolicyEntry &x) const { return hash<string>()(x.toString()); }
+};
+
+} // namespace std
+
 #endif // SECURITY_MANAGER_TEST_USERREQUEST
index 4868809d6a35630f98d7a1568de4d4aaba14a1bc..14016503cbfbdad9a23eb612d754a721d2a1be04 100644 (file)
@@ -1340,15 +1340,18 @@ RUNNER_MULTIPROCESS_TEST(security_manager_11_privacy_manager_fetch_whole_policy_
 
 RUNNER_MULTIPROCESS_TEST(security_manager_12_privacy_manager_fetch_whole_policy_for_admin_privileged)
 {
+    std::vector<PolicyEntry> oldPolicyVec;
+    Api::getPolicy(PolicyEntry(), oldPolicyVec);
+    std::unordered_set<PolicyEntry> oldPolicySet(oldPolicyVec.begin(), oldPolicyVec.end());
+
     //TEST DATA
     const std::vector<std::string> usernames = {"sm_test_12_user_name_1", "sm_test_12_user_name_2"};
-    unsigned int privileges_count = 0;
+    unsigned int privileges_count = oldPolicyVec.size();
 
     std::map<std::string, std::map<std::string, std::set<std::string>>> users2AppsMap;
     std::map<std::string, std::set<std::string>> apps2PrivsMap;
 
     for (const auto &username : usernames) {
-
         for(unsigned int i = 0; i < MANY_APPS.size(); ++i) {
             apps2PrivsMap.insert(std::pair<std::string, std::set<std::string>>(
                 MANY_APPS.at(i), std::set<std::string>(
@@ -1357,11 +1360,11 @@ RUNNER_MULTIPROCESS_TEST(security_manager_12_privacy_manager_fetch_whole_policy_
             privileges_count+=MANY_APPS_PRIVILEGES.at(i).size();
         };
 
-        users2AppsMap.insert(std::pair<std::string, std::map<std::string, std::set<std::string>>>(username, apps2PrivsMap));
+        users2AppsMap.insert(std::make_pair(username, apps2PrivsMap));
     };
 
-    users2AppsMap.at(usernames.at(1)).insert(std::pair<std::string, std::set<std::string>>(
-        PRIVILEGE_MANAGER_APP, std::set<std::string>{PRIVILEGE_MANAGER_SELF_PRIVILEGE, PRIVILEGE_MANAGER_ADMIN_PRIVILEGE}));
+    users2AppsMap.at(usernames.at(1)).insert(std::make_pair(PRIVILEGE_MANAGER_APP,
+        std::set<std::string>{PRIVILEGE_MANAGER_SELF_PRIVILEGE, PRIVILEGE_MANAGER_ADMIN_PRIVILEGE}));
 
     privileges_count += 2;
     //TEST DATA END
@@ -1419,14 +1422,16 @@ RUNNER_MULTIPROCESS_TEST(security_manager_12_privacy_manager_fetch_whole_policy_
         RUNNER_ASSERT_MSG(result == 0, "drop_root_privileges failed");
 
         std::vector<PolicyEntry> policyEntries;
-        PolicyEntry filter;
         //this call should succeed as the calling user is privileged
-        Api::getPolicy(filter, policyEntries);
+        Api::getPolicy(PolicyEntry(), policyEntries);
 
         RUNNER_ASSERT_MSG(policyEntries.size() != 0, "Policy is empty");
         RUNNER_ASSERT_MSG(policyEntries.size() == privileges_count, "Number of policies doesn't match - should be: " << privileges_count << " and is " << policyEntries.size());
 
         for (const auto &policyEntry : policyEntries) {
+            if (oldPolicySet.count(policyEntry))
+                continue;
+
             std::string user = policyEntry.getUser();
             std::string app = policyEntry.getAppId();
             std::string privilege = policyEntry.getPrivilege();