Enable policy-provider to set compare method
authorSangwan Kwon <sangwan.kwon@samsung.com>
Tue, 26 Nov 2019 05:06:35 +0000 (14:06 +0900)
committer권상완/Security 2Lab(SR)/Engineer/삼성전자 <sangwan.kwon@samsung.com>
Thu, 28 Nov 2019 04:36:35 +0000 (13:36 +0900)
Signed-off-by: Sangwan Kwon <sangwan.kwon@samsung.com>
src/vist/policy/policy-manager.cpp
src/vist/policy/policy-manager.hpp
src/vist/policy/policy-storage.cpp
src/vist/policy/policy-storage.hpp
src/vist/policy/tests/core.cpp
src/vist/policy/tests/storage.cpp
src/vist/sdk/policy-model.hpp

index 8743388537b6fa6e9a2b0a71dd7081efbd5bc6b0..a7431b6efadb942d2f5618622fed588b05243c30 100644 (file)
@@ -22,6 +22,8 @@
 
 #include <klay/filesystem.h>
 
+#include <algorithm>
+
 namespace vist {
 namespace policy {
 
@@ -106,34 +108,26 @@ void PolicyManager::set(const std::string& policy,
                                                const PolicyValue& value,
                                                const std::string& admin)
 {
-       if (this->policies.find(policy) == this->policies.end())
-               THROW(ErrCode::RuntimeError) << "Not exist policy: " << policy;
-
        this->storage.update(admin, policy, value);
-
-       for (auto& p : providers) {
-               if (p->getName() != this->policies[policy])
-                       continue;
-
-               /// dispatch callback written by provider
-               if (p->policies.find(policy) != p->policies.end()) {
-                       p->policies[policy]->set(value);
-                       return;
-               }
-       }
+       this->getPolicy(policy)->set(value);
 }
 
 PolicyValue PolicyManager::get(const std::string& policy)
 {
-       if (this->policies.find(policy) == this->policies.end())
-               THROW(ErrCode::RuntimeError) << "Not exist policy: " << policy;
-
-       return storage.strictest(policy);
+       return storage.strictest(this->getPolicy(policy));
 }
 
 std::unordered_map<std::string, PolicyValue> PolicyManager::getAll()
 {
-       return storage.strictest();
+       std::unordered_map<std::string, PolicyValue> policies;
+       for (const auto& pair : this->policies) {
+               std::string policyName = pair.first;
+               auto value = this->get(policyName);
+
+               policies.emplace(std::move(policyName), std::move(value));
+       }
+
+       return policies;
 }
 
 std::vector<std::string> PolicyManager::getAdmins()
@@ -141,5 +135,22 @@ std::vector<std::string> PolicyManager::getAdmins()
        return storage.getAdmins();
 }
 
+const std::shared_ptr<PolicyModel>& PolicyManager::getPolicy(const std::string& name)
+{
+       if (this->policies.find(name) == this->policies.end())
+               THROW(ErrCode::RuntimeError) << "Not exist policy: " << name;
+
+       auto provider = this->policies[name];
+       auto iter = std::find_if(this->providers.begin(), this->providers.end(),
+                                                        [&provider](const std::unique_ptr<PolicyProvider>& p) {
+                                                                return p->getName() == provider;
+                                                        });
+       if (iter == this->providers.end())
+               THROW(ErrCode::RuntimeError) << "Not exist provider[" << provider
+                                                                        << "] about policy: " << name;
+
+       return (*iter)->policies[name];
+}
+
 } // namespace policy
 } // namespace vist
index 103d616ad6ca0b858362900860545cb9c8f63bd7..b0bacc2ef29dbfb25b09e10974f6fd440ec4b4a8 100644 (file)
@@ -66,10 +66,13 @@ private:
        PolicyStorage storage;
        std::vector<std::unique_ptr<PolicyProvider>> providers;
 
+       const std::shared_ptr<PolicyModel>& getPolicy(const std::string& name);
+
        /// Policy-Provider
        std::unordered_map<std::string, std::string> policies;
 
        FRIEND_TEST(PolicyCoreTests, policy_loader);
+       FRIEND_TEST(PolicyCoreTests, policy_get_policy);
 };
 
 } // namespace policy
index ad32e5a12c7d86630cd82f9f5c8e6c32043da2e9..f7d777f353010fde185764d6ad77cb7d54d4f4be 100644 (file)
@@ -227,18 +227,18 @@ void PolicyStorage::update(const std::string& admin,
 
 /// TODO(sangwan.kwon) Re-design strictest logic
 /// PolicyValue PolicyStorage::strictest(const PolicyValue& policy)
-PolicyValue PolicyStorage::strictest(const std::string& policy)
+PolicyValue PolicyStorage::strictest(const std::shared_ptr<PolicyModel>& policy)
 {
-       if (this->definitions.find(policy) == this->definitions.end())
-               THROW(ErrCode::LogicError) << "Not exist policy: " << policy;
+       if (this->definitions.find(policy->getName()) == this->definitions.end())
+               THROW(ErrCode::LogicError) << "Not exist policy: " << policy->getName();
 
-       /// There is no enrolled admins.
-       /// Make PolicyValue by dumped string.
-       if (this->activatedPolicies.size() == 0)
-               return PolicyValue(definitions[policy].ivalue, true);
+       if (this->activatedPolicies.size() == 0) {
+               INFO(VIST) << "There is no enrolled admin. Return policy initial value.";
+               return policy->getInitial();
+       }
 
        std::shared_ptr<PolicyValue> strictestPtr = nullptr;
-       auto range = activatedPolicies.equal_range(policy);
+       auto range = activatedPolicies.equal_range(policy->getName());
        for (auto iter = range.first; iter != range.second; iter++) {
                DEBUG(VIST) << "Admin: " << iter->second.admin << ", "
                                        << "Policy: " << iter->second.policy  << ", "
@@ -247,9 +247,7 @@ PolicyValue PolicyStorage::strictest(const std::string& policy)
                if (strictestPtr == nullptr) {
                        strictestPtr = std::make_shared<PolicyValue>(iter->second.value, true);
                } else {
-                       /// TODO: Support String type
-                       int strictestValue = *strictestPtr;
-                       if (strictestValue < PolicyValue(iter->second.value, true))
+                       if (policy->compare(*strictestPtr, PolicyValue(iter->second.value, true)) > 0)
                                strictestPtr.reset(new PolicyValue(iter->second.value, true));
                }
        }
@@ -257,26 +255,12 @@ PolicyValue PolicyStorage::strictest(const std::string& policy)
        if (strictestPtr == nullptr)
                THROW(ErrCode::RuntimeError) << "Not exist managed policy: " << policy;
 
-       int strictestValue = *strictestPtr;
-       DEBUG(VIST) << "The strictest value of [" << policy
-                               << "] is " << strictestValue; 
+       DEBUG(VIST) << "The strictest value of [" << policy->getName()
+                               << "] is " << strictestPtr->dump();
 
        return std::move(*strictestPtr);
 }
 
-std::unordered_map<std::string, PolicyValue> PolicyStorage::strictest()
-{
-       std::unordered_map<std::string, PolicyValue> policies;
-       for (const auto& pair : definitions) {
-               std::string name = pair.first;
-               auto value = this->strictest(name);
-
-               policies.emplace(std::move(name), std::move(value));
-       }
-
-       return policies;
-}
-
 const std::vector<std::string>& PolicyStorage::getAdmins() const noexcept
 {
        return admins;
index 9def0b1bee7b8e2cfab092963b17aab5784a9021..27bc27e27f5d6d97ce520016904b1db98a30da3d 100644 (file)
@@ -16,6 +16,7 @@
 
 #pragma once
 
+#include <vist/sdk/policy-model.hpp>
 #include <vist/sdk/policy-value.hpp>
 
 #include "db-schema.hpp"
@@ -60,9 +61,7 @@ public:
                                const std::string& policy,
                                const PolicyValue& value);
 
-       PolicyValue strictest(const std::string& policy);
-       /// Return all strictest policy values
-       std::unordered_map<std::string, PolicyValue> strictest();
+       PolicyValue strictest(const std::shared_ptr<PolicyModel>& policy);
 
        const std::vector<std::string>& getAdmins() const noexcept;
 
index 0c0a416c4d1e423cdfbcec878090de05be62433d..8c1ca832425d45c9fc12a52dcc36703b24d61e79 100644 (file)
 namespace vist {
 namespace policy {
 
-class PolicyCoreTests : public testing::Test {};
-
-TEST_F(PolicyCoreTests, policy_loader) {
+TEST(PolicyCoreTests, policy_loader) {
        auto& manager = PolicyManager::Instance();
 
        EXPECT_TRUE(manager.providers.size() > 0);
        EXPECT_TRUE(manager.policies.size() > 0);
 }
 
-TEST_F(PolicyCoreTests, policy_set_get) {
+TEST(PolicyCoreTests, policy_set_get) {
        auto& manager = PolicyManager::Instance();
        manager.enroll("testAdmin");
        manager.set("bluetooth", PolicyValue(5), "testAdmin");
 
        auto policy = manager.get("bluetooth");
-       EXPECT_EQ((int)policy, 5);
+       EXPECT_EQ(static_cast<int>(policy), 5);
 
        manager.enroll("testAdmin1");
        manager.set("bluetooth", PolicyValue(10), "testAdmin1");
 
        /// Manager should return the strongest policy.
        policy = manager.get("bluetooth");
-       EXPECT_EQ((int)policy, 10);
+       EXPECT_EQ(static_cast<int>(policy), 10);
 
        manager.disenroll("testAdmin");
        manager.disenroll("testAdmin1");
 }
 
+TEST(PolicyCoreTests, policy_get_all) {
+       auto& manager = PolicyManager::Instance();
+       auto policies = manager.getAll();
+       EXPECT_TRUE(policies.size() > 0);
+}
+
+TEST(PolicyCoreTests, policy_get_policy) {
+       auto& manager = PolicyManager::Instance();
+       const auto& policy = manager.getPolicy("bluetooth");
+       EXPECT_EQ(policy->getName(), "bluetooth");
+
+       bool raised = false;
+       try {
+               manager.getPolicy("fakePolicy");
+       } catch (const vist::Exception<ErrCode>&) {
+               raised = true;
+       }
+       EXPECT_TRUE(raised);
+}
+
 } // namespace policy
 } // namespace vist
index 58d9a1f961f2a110d576dbb5ebe0325df4069cdd..71e57aae0d3602e26c0b572723f45ad8afb17486 100644 (file)
@@ -100,41 +100,6 @@ TEST_F(PolicyStorageTests, update)
        storage->disenroll("testAdmin");
 }
 
-TEST_F(PolicyStorageTests, strictest)
-{
-       auto storage = getStorage();
-       storage->enroll("testAdmin0");
-       storage->enroll("testAdmin1");
-
-       storage->update("testAdmin0", "bluetooth", PolicyValue(3));
-       storage->update("testAdmin1", "bluetooth", PolicyValue(6));
-
-       bool isRaised = false;
-       try {
-               auto value = storage->strictest("FakePolicy");
-       } catch (const std::exception&) {
-               isRaised = true;
-       }
-       EXPECT_TRUE(isRaised);
-
-       auto policy = storage->strictest("bluetooth");
-       EXPECT_EQ((int)policy, 6);
-
-       storage->disenroll("testAdmin0");
-       storage->disenroll("testAdmin1");
-}
-
-TEST_F(PolicyStorageTests, strictest_all)
-{
-       auto storage = getStorage();
-       storage->enroll("testAdmin");
-
-       auto policies = storage->strictest();
-       EXPECT_TRUE(policies.size() > 0);
-
-       storage->disenroll("testAdmin");
-}
-
 TEST_F(PolicyStorageTests, admin_list)
 {
        auto storage = getStorage();
index 4cfbb538ad7c62732afdec3fa0692d8f04c255d6..20a52292f1ff626530434b37e2f1b2ed659ff7da 100644 (file)
@@ -56,6 +56,20 @@ public:
                return current;
        }
 
+       /// Default compare function for int type
+       virtual int compare(const PolicyValue& lhs, const PolicyValue& rhs) const
+       {
+               int lvalue = lhs;
+               int rvalue = rhs;
+
+               if (lvalue < rvalue)
+                       return 1;
+               else if (lvalue == rvalue)
+                       return 0;
+               else
+                       return -1;
+       }
+
        virtual void onChanged(const PolicyValue& value) = 0;
 
        const std::string& getName() const noexcept { return name; }