Add admin api to check for activation
authorSangwan Kwon <sangwan.kwon@samsung.com>
Thu, 9 Jan 2020 06:19:46 +0000 (15:19 +0900)
committer권상완/Security 2Lab(SR)/Engineer/삼성전자 <sangwan.kwon@samsung.com>
Tue, 14 Jan 2020 01:57:30 +0000 (10:57 +0900)
API::Admin::IsActivated() in vist-policy

Signed-off-by: Sangwan Kwon <sangwan.kwon@samsung.com>
src/vist/policy/api.cpp
src/vist/policy/api.hpp
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

index 6c0386fd58762569d4967c7dbcd967523e494c8a..947aafb613edd152234e900f5fdf34a761c81ea3 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
+ *  Copyright (c) 2019-present Samsung Electronics Co., Ltd All Rights Reserved
  *
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
@@ -52,6 +52,11 @@ void API::Admin::Activate(const std::string& admin, bool state)
        PolicyManager::Instance().activate(admin, state);
 }
 
+bool API::Admin::IsActivated()
+{
+       return PolicyManager::Instance().isActivated();
+}
+
 std::unordered_map<std::string, int> API::Admin::GetAll()
 {
        return PolicyManager::Instance().getAdmins();
index dabbcd7a995ee98ff721e5df643335e2f4351f61..2998c1f9544b2c414dcf4c48030da14eba7b4bb2 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
+ *  Copyright (c) 2019-present Samsung Electronics Co., Ltd All Rights Reserved
  *
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
@@ -35,6 +35,7 @@ struct API {
                static void Disenroll(const std::string& admin);
 
                static void Activate(const std::string& admin, bool state = true);
+               static bool IsActivated();
 
                static std::unordered_map<std::string, int> GetAll();
        };
index f03b8277fd1815f3955ac642e2d27c1102622e1b..2ab3ca354b724950b3df31b9b782f32efda973f0 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
+ *  Copyright (c) 2019-present Samsung Electronics Co., Ltd All Rights Reserved
  *
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
@@ -108,6 +108,11 @@ void PolicyManager::activate(const std::string& admin, bool state)
        this->storage.activate(admin, state);
 }
 
+bool PolicyManager::isActivated()
+{
+       return this->storage.isActivated();
+}
+
 void PolicyManager::set(const std::string& policy,
                                                const PolicyValue& value,
                                                const std::string& admin)
index 67d499409552fb2e092f6165d7de5283e34e301c..3eda4d7c228f9c7889764110737949363c5514cd 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
+ *  Copyright (c) 2019-present Samsung Electronics Co., Ltd All Rights Reserved
  *
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
@@ -49,6 +49,7 @@ public:
        void disenroll(const std::string& admin);
 
        void activate(const std::string& admin, bool state);
+       bool isActivated();
 
        void set(const std::string& policy,
                         const PolicyValue& value,
index d570f513d0fc9fc8f508a5c568fbcf0d415f643e..ba1ba20326de48f90c14f32b2f0a9d0e4fddaa53 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
+ *  Copyright (c) 2019-present Samsung Electronics Co., Ltd All Rights Reserved
  *
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
@@ -228,6 +228,15 @@ bool PolicyStorage::isActivated(const std::string& admin)
        return this->admins[admin].activated;
 }
 
+bool PolicyStorage::isActivated()
+{
+       for (const auto& admin : this->admins)
+               if (admin.second.activated)
+                       return true;
+
+       return false;
+}
+
 void PolicyStorage::update(const std::string& admin,
                                                   const std::string& policy,
                                                   const PolicyValue& value)
index e86ecaec77ce20ab7b38df542f58666dce9cdcb5..8cff28902654817d33f730d5f0885a799bb2074e 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
+ *  Copyright (c) 2019-present Samsung Electronics Co., Ltd All Rights Reserved
  *
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
@@ -50,6 +50,8 @@ public:
        void disenroll(const std::string& admin);
        void activate(const std::string& admin, bool state = true);
        bool isActivated(const std::string& admin);
+       /// Check that none of admins are activated.
+       bool isActivated();
 
        void define(const std::string& policy, const PolicyValue& ivalue);
        void update(const std::string& admin,
index e8a2c373164b3a709d35f15444f2b4dabde3456a..57e9fee550d60bd6469695df1817af3b8c8f2880 100644 (file)
@@ -109,5 +109,28 @@ TEST(PolicyCoreTests, admin) {
        manager.disenroll("testAdmin");
 }
 
+TEST(PolicyCoreTests, is_activated) {
+       auto& manager = PolicyManager::Instance();
+       manager.enroll("testAdmin1");
+       manager.enroll("testAdmin2");
+
+       EXPECT_FALSE(manager.isActivated());
+
+       manager.activate("testAdmin1", true);
+       EXPECT_TRUE(manager.isActivated());
+
+       manager.activate("testAdmin2", true);
+       EXPECT_TRUE(manager.isActivated());
+
+       manager.activate("testAdmin1", false);
+       EXPECT_TRUE(manager.isActivated());
+
+       manager.activate("testAdmin2", false);
+       EXPECT_FALSE(manager.isActivated());
+
+       manager.disenroll("testAdmin1");
+       manager.disenroll("testAdmin2");
+}
+
 } // namespace policy
 } // namespace vist