From 0e5de116f9e9be61c53836aa3339a5d8f34a3876 Mon Sep 17 00:00:00 2001 From: stonexx <1479765922@qq.com> Date: Wed, 24 Nov 2021 23:05:23 +0800 Subject: [PATCH] chore: update cmakelists version, add GetEvalValue test and delete ABACData Signed-off-by: stonexx <1479765922@qq.com> --- CMakeLists.txt | 2 +- bindings/python/CMakeLists.txt | 1 - bindings/python/main.cpp | 1 - bindings/python/py_abac_data.cpp | 83 ----------------------- bindings/python/py_casbin.h | 1 - casbin/CMakeLists.txt | 1 - casbin/abac_data.cpp | 69 ------------------- casbin/abac_data.h | 112 ------------------------------- casbin/attribute_types.h | 31 --------- casbin/data_types.h | 3 +- casbin/enforcer.cpp | 72 +------------------- casbin/enforcer_cached.cpp | 39 ----------- casbin/pch.h | 1 - include/casbin/casbin_types.h | 96 +------------------------- tests/enforcer_test.cpp | 26 ------- tests/util_test.cpp | 17 +++++ 16 files changed, 21 insertions(+), 534 deletions(-) delete mode 100644 bindings/python/py_abac_data.cpp delete mode 100644 casbin/abac_data.cpp delete mode 100644 casbin/abac_data.h delete mode 100644 casbin/attribute_types.h diff --git a/CMakeLists.txt b/CMakeLists.txt index f3e35c24..284fd01f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -33,7 +33,7 @@ endif() project( casbin - VERSION 1.38.0 + VERSION 1.43.0 DESCRIPTION "An authorization library that supports access control models like ACL, RBAC, ABAC in C/C++" HOMEPAGE_URL https://github.com/casbin/casbin-cpp LANGUAGES CXX C diff --git a/bindings/python/CMakeLists.txt b/bindings/python/CMakeLists.txt index 26e05774..f4058028 100644 --- a/bindings/python/CMakeLists.txt +++ b/bindings/python/CMakeLists.txt @@ -18,7 +18,6 @@ set(SOURCES main.cpp py_cached_enforcer.cpp py_enforcer.cpp - py_abac_data.cpp py_model.cpp py_config.cpp py_synced_enforcer.cpp diff --git a/bindings/python/main.cpp b/bindings/python/main.cpp index 95a2b2ba..4cdc08ec 100644 --- a/bindings/python/main.cpp +++ b/bindings/python/main.cpp @@ -36,7 +36,6 @@ PYBIND11_MODULE(pycasbin, m) { bindPyEnforcer(m); bindPyCachedEnforcer(m); - bindABACData(m); bindPyModel(m); bindPyConfig(m); bindPySyncedEnforcer(m); diff --git a/bindings/python/py_abac_data.cpp b/bindings/python/py_abac_data.cpp deleted file mode 100644 index ff7f7ef6..00000000 --- a/bindings/python/py_abac_data.cpp +++ /dev/null @@ -1,83 +0,0 @@ -/* -* Copyright 2021 The casbin Authors. 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. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -#include -#include -#include - -namespace py = pybind11; - -void bindABACData(py::module& m) { - py::class_(m, "ABACData") - .def(py::init(), R"doc( - @brief Construct a new casbin::ABACData object - - @param attribs Should be of the format: { - { "attrib_name1", value1 }, - { "attring_name2", value2 }, - ... - } - - Key's type is std::string and value's type can be one of std::string, int32_t, double, and float only - )doc") - .def("AddAttribute", &casbin::ABACData::AddAttribute, R"doc( - @brief Add attribute to the corresponding ABAC entity - @param key Name of the attribute - @param value Value of the attribute - @return true when attribute is added successfully, false otherwise - )doc") - .def("AddAttributes", &casbin::ABACData::AddAttributes, R"doc( - @brief Add attributes to the corresponding ABAC entity - - @param attribs Should be of the format: { - { "attrib_name1", value1 }, - { "attring_name2", value2 }, - ... - } - - Key's type is std::string and value's type can be one of std::string, int32_t, and float only - @return true if attributes are added successfully, false otherwise - - )doc") - .def("DeleteAttribute", &casbin::ABACData::DeleteAttribute, R"doc( - @brief Delete attribute of the corresponding ABAC entity - @param key Name of the attribute to be deleted - @return true when attribute is deleted successfully, false otherwise - )doc") - .def("UpdateAttribute", &casbin::ABACData::UpdateAttribute, R"doc( - @brief Update attribute of the corresponding ABAC entity - @param key Name of the attribute to be updated - @param value Value which would replace the current value of the attribute corresponding - to the given key - @return true - @return false - )doc"); - // .def("GetAttributes", &casbin::ABACData::GetAttributes, R"doc( - // @brief Get the Attributes of the corresponding ABAC entity - // @return const reference to the hashmap containing attributes in key-value pairs - // )doc"); - - m.def("GetDataObject", &casbin::GetDataObject, R"doc( - @brief Get casbin::ABACData object - @param attribs Should be of the format: { - { "attrib_name1", value1 }, - { "attrib_name2", value2 }, - ... - } - Key's type is std::string and value's type can be one of std::string, int32_t, double, and float only - @return Pointer to casbin::ABACData entity - )doc"); -} diff --git a/bindings/python/py_casbin.h b/bindings/python/py_casbin.h index 826107d8..320ed7e2 100644 --- a/bindings/python/py_casbin.h +++ b/bindings/python/py_casbin.h @@ -20,7 +20,6 @@ namespace py = pybind11; void bindPyEnforcer(py::module &m); void bindPyCachedEnforcer(py::module &m); -void bindABACData(py::module &m); void bindPyModel(py::module &m); void bindPyConfig(py::module &m); void bindPySyncedEnforcer(py::module& m); diff --git a/casbin/CMakeLists.txt b/casbin/CMakeLists.txt index 95523f4f..00b137b2 100644 --- a/casbin/CMakeLists.txt +++ b/casbin/CMakeLists.txt @@ -13,7 +13,6 @@ # limitations under the License. set(CASBIN_SOURCE_FILES - abac_data.cpp enforcer.cpp enforcer_cached.cpp enforcer_synced.cpp diff --git a/casbin/abac_data.cpp b/casbin/abac_data.cpp deleted file mode 100644 index 84db1c78..00000000 --- a/casbin/abac_data.cpp +++ /dev/null @@ -1,69 +0,0 @@ -/* -* Copyright 2021 The casbin Authors. 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. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -#include "pch.h" - -#ifndef ABAC_CPP -#define ABAC_CPP - -#include "abac_data.h" -#include "./model/scope_config.h" - -namespace casbin { - -const std::shared_ptr GetDataObject(const AttributeMap& attribs) { - return std::make_shared(attribs); -} - -ABACData::ABACData(const AttributeMap& attribs) - : m_attributes(std::move(attribs)) -{} - -bool ABACData::AddAttribute(const std::string& key, const AttributeValue& value) { - m_attributes[key] = value; - return true; -} - -bool ABACData::AddAttributes(const AttributeList& attribs) { - for(auto [name, value] : attribs) - m_attributes[name] = value; - return true; -} - -bool ABACData::DeleteAttribute(const std::string& key) { - auto it = m_attributes.find(key); - - // If key is not present in the map, indicate deletion failiure - if(it == m_attributes.end()) { - return false; - } - - m_attributes.erase(it); - return true; -} - -bool ABACData::UpdateAttribute(const std::string& key, const AttributeValue& value) { - m_attributes[key] = value; - return true; -} - -const AttributeMap& ABACData::GetAttributes() { - return m_attributes; -} - -} - -#endif // ABAC_CPP diff --git a/casbin/abac_data.h b/casbin/abac_data.h deleted file mode 100644 index a0572384..00000000 --- a/casbin/abac_data.h +++ /dev/null @@ -1,112 +0,0 @@ -/* -* Copyright 2021 The casbin Authors. 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. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -#ifndef CASBIN_ABAC_H -#define CASBIN_ABAC_H - -#include "attribute_types.h" - -namespace casbin { - -/** - * @brief A wrapper to contain ABAC entity with a list of attributes stored in a hashmap - * - */ -class ABACData { - -private: - - // HashMap containing attributes as key-value pairs - AttributeMap m_attributes; - -public: - /** - * @brief Construct a new casbin::ABACData object - * - * @param attribs Should be of the format: { - * { "attrib_name1", value1 }, - * { "attring_name2", value2 }, - * ... - * } - * - * Key's type is std::string and value's type can be one of std::string, int32_t, and float only - */ - ABACData(const AttributeMap& attribs); - /** - * @brief Add attribute to the corresponding ABAC entity - * - * @param key Name of the attribute - * @param value Value of the attribute - * @return true when attribute is added successfully, false otherwise - */ - bool AddAttribute(const std::string& key, const AttributeValue& value); - /** - * @brief Add attributes to the corresponding ABAC entity - * - * @param attribs Should be of the format: { - * { "attrib_name1", value1 }, - * { "attring_name2", value2 }, - * ... - * } - * - * Key's type is std::string and value's type can be one of std::string, int32_t, and float only - * @return true if attributes are added successfully, false otherwise - */ - bool AddAttributes(const AttributeList& attribs); - /** - * @brief Delete attribute of the corresponding ABAC entity - * - * @param key Name of the attribute to be deleted - * @return true when attribute is deleted successfully, false otherwise - */ - bool DeleteAttribute(const std::string& key); - /** - * @brief Update attribute of the corresponding ABAC entity - * - * @param key Name of the attribute to be updated - * @param value Value which would replace the current value of the attribute corresponding - * to the given key - * @return true - * @return false - */ - bool UpdateAttribute(const std::string& key, const AttributeValue& value); - /** - * @brief Get the Attributes of the corresponding ABAC entity - * - * @return const reference to the hashmap containing attributes in key-value pairs - */ - const AttributeMap& GetAttributes(); -}; - -// Casbin ABAC entity type -typedef ABACData ABACData; - -/** - * @brief Get casbin::ABACData object - * - * @param attribs Should be of the format: { - * { "attrib_name1", value1 }, - * { "attrib_name2", value2 }, - * ... - * } - * - * Key's type is std::string and value's type can be one of std::string, int32_t, double, and float only - * @return Pointer to casbin::ABACData entity - */ -const std::shared_ptr GetDataObject(const AttributeMap& attribs); -} - -#endif // CASBIN_ABAC_H diff --git a/casbin/attribute_types.h b/casbin/attribute_types.h deleted file mode 100644 index 954ec63b..00000000 --- a/casbin/attribute_types.h +++ /dev/null @@ -1,31 +0,0 @@ -/* -* Copyright 2021 The casbin Authors. 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. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -#include -#include -#include -#include -#include - -namespace casbin { - -typedef std::variant AttributeValue; -typedef std::pair Attribute; -typedef std::vector AttributeVector; -typedef std::initializer_list AttributeList; -typedef std::unordered_map AttributeMap; - -} // namespace casbin diff --git a/casbin/data_types.h b/casbin/data_types.h index d4339589..1c6e3028 100644 --- a/casbin/data_types.h +++ b/casbin/data_types.h @@ -19,11 +19,10 @@ #include #include #include -#include "abac_data.h" namespace casbin { -typedef std::variant, std::shared_ptr> Data; +typedef std::variant> Data; typedef std::vector DataVector; typedef std::initializer_list DataList; typedef std::unordered_map DataMap; diff --git a/casbin/enforcer.cpp b/casbin/enforcer.cpp index 8e93d1b9..b28a1bc5 100644 --- a/casbin/enforcer.cpp +++ b/casbin/enforcer.cpp @@ -501,30 +501,6 @@ bool Enforcer::EnforceWithMatcher(const std::string& matcher, const DataList& pa for(const Data& param : params) { if(const auto string_param = std::get_if(¶m)) { PushStringPropToObject(scope, "r", *string_param, r_tokens[i].substr(2, r_tokens[i].size() - 2)); - } - else if(const auto abac_param = std::get_if>(¶m)) { - auto data_ptr = *abac_param; - std::string token_name = r_tokens[i].substr(2, r_tokens[i].size() - 2); - - PushObjectPropToObject(scope, "r", token_name); - - for(auto [attrib_name, attrib_value] : data_ptr->GetAttributes()) { - - if(const auto string_value = std::get_if(&attrib_value)) - PushStringPropToObject(scope, token_name, *string_value, attrib_name); - - else if(const auto int_value = std::get_if(&attrib_value)) - PushIntPropToObject(scope, token_name, *int_value, attrib_name); - - else if(const auto float_value = std::get_if(&attrib_value)) - PushFloatPropToObject(scope, token_name, *float_value, attrib_name); - - else if(const auto double_value = std::get_if(&attrib_value)) - PushDoublePropToObject(scope, token_name, *double_value, attrib_name); - - else - throw CasbinEnforcerException("Not a valid type"); - } } else if (const auto json_param = std::get_if>(¶m)) { auto data_ptr = *json_param; @@ -564,30 +540,6 @@ bool Enforcer::EnforceWithMatcher(const std::string& matcher, const DataVector& for(const auto& param : params) { if(const auto string_param = std::get_if(¶m)) { PushStringPropToObject(scope, "r", *string_param, r_tokens[i].substr(2, r_tokens[i].size() - 2)); - } - else if(const auto abac_param = std::get_if>(¶m)) { - auto data_ptr = *abac_param; - std::string token_name = r_tokens[i].substr(2, r_tokens[i].size() - 2); - - PushObjectPropToObject(scope, "r", token_name); - - for(auto [attrib_name, attrib_value] : data_ptr->GetAttributes()) { - - if(const auto string_value = std::get_if(&attrib_value)) - PushStringPropToObject(scope, token_name, *string_value, attrib_name); - - else if(const auto int_value = std::get_if(&attrib_value)) - PushIntPropToObject(scope, token_name, *int_value, attrib_name); - - else if(const auto float_value = std::get_if(&attrib_value)) - PushFloatPropToObject(scope, token_name, *float_value, attrib_name); - - else if(const auto double_value = std::get_if(&attrib_value)) - PushDoublePropToObject(scope, token_name, *double_value, attrib_name); - - else - throw CasbinEnforcerException("Not a valid type"); - } } else if (const auto json_param = std::get_if>(¶m)) { auto data_ptr = *json_param; @@ -618,30 +570,8 @@ bool Enforcer::EnforceWithMatcher(const std::string& matcher, const DataMap& par PushObject(scope, "r"); for (auto [param_name, param_data] : params) { - if(const auto string_param = std::get_if(¶m_data)) + if(const auto string_param = std::get_if(¶m_data)) { PushStringPropToObject(scope, "r", *string_param, param_name); - else if(const auto abac_param = std::get_if>(¶m_data)) { - auto data_ptr = *abac_param; - - PushObjectPropToObject(scope, "r", param_name); - - for(auto [attrib_name, attrib_value] : data_ptr->GetAttributes()) { - - if(const auto string_value = std::get_if(&attrib_value)) - PushStringPropToObject(scope, param_name, *string_value, attrib_name); - - else if(const auto int_value = std::get_if(&attrib_value)) - PushIntPropToObject(scope, param_name, *int_value, attrib_name); - - else if(const auto float_value = std::get_if(&attrib_value)) - PushFloatPropToObject(scope, param_name, *float_value, attrib_name); - - else if(const auto double_value = std::get_if(&attrib_value)) - PushDoublePropToObject(scope, param_name, *double_value, attrib_name); - - else - throw CasbinEnforcerException("Not a valid type"); - } } else if (const auto json_param = std::get_if>(¶m_data)) { auto data_ptr = *json_param; diff --git a/casbin/enforcer_cached.cpp b/casbin/enforcer_cached.cpp index 9c94fd7e..136ba6d6 100644 --- a/casbin/enforcer_cached.cpp +++ b/casbin/enforcer_cached.cpp @@ -183,19 +183,6 @@ bool CachedEnforcer::EnforceWithMatcher(const std::string& matcher, const DataVe for (const auto& r : params) { if(const auto string_param = std::get_if(&r)) key += *string_param; - else if(const auto abac_param = std::get_if>(&r)) { - auto data_ptr = *abac_param; - for(auto [_, attrib_value] : data_ptr->GetAttributes()) { - if(auto string_value = std::get_if(&attrib_value)) - key += *string_value + "$"; - else if(auto int_value = std::get_if(&attrib_value)) - key += std::to_string(*int_value) + "$"; - else if(auto double_value = std::get_if(&attrib_value)) - key += std::to_string(*double_value) + "$"; - else if(auto float_value = std::get_if(&attrib_value)) - key += std::to_string(*float_value) + "$"; - } - } key += "$$"; } key += matcher; @@ -224,19 +211,6 @@ bool CachedEnforcer::EnforceWithMatcher(const std::string& matcher, const DataLi for (const auto& r : params) { if(const auto string_param = std::get_if(&r)) key += *string_param; - else if(const auto abac_param = std::get_if>(&r)) { - auto data_ptr = *abac_param; - for(auto [_, attrib_value] : data_ptr->GetAttributes()) { - if(auto string_value = std::get_if(&attrib_value)) - key += *string_value + "$"; - else if(auto int_value = std::get_if(&attrib_value)) - key += std::to_string(*int_value) + "$"; - else if(auto double_value = std::get_if(&attrib_value)) - key += std::to_string(*double_value) + "$"; - else if(auto float_value = std::get_if(&attrib_value)) - key += std::to_string(*float_value) + "$"; - } - } key += "$$"; } key += matcher; @@ -265,19 +239,6 @@ bool CachedEnforcer::EnforceWithMatcher(const std::string& matcher, const DataMa for (auto [param_name, param_value] : params) { if(const auto string_value = std::get_if(¶m_value)) key += *string_value; - else if(const auto abac_param = std::get_if>(¶m_value)) { - auto data_ptr = *abac_param; - for(auto [_, attrib_value] : data_ptr->GetAttributes()) { - if(auto string_value = std::get_if(&attrib_value)) - key += *string_value + "$"; - else if(auto int_value = std::get_if(&attrib_value)) - key += std::to_string(*int_value) + "$"; - else if(auto double_value = std::get_if(&attrib_value)) - key += std::to_string(*double_value) + "$"; - else if(auto float_value = std::get_if(&attrib_value)) - key += std::to_string(*float_value) + "$"; - } - } key += "$$"; } key += matcher; diff --git a/casbin/pch.h b/casbin/pch.h index 31cf03d3..50721796 100644 --- a/casbin/pch.h +++ b/casbin/pch.h @@ -42,7 +42,6 @@ #include #include -#include "attribute_types.h" #include "data_types.h" diff --git a/include/casbin/casbin_types.h b/include/casbin/casbin_types.h index 14efebf4..be9a4d6b 100644 --- a/include/casbin/casbin_types.h +++ b/include/casbin/casbin_types.h @@ -28,101 +28,7 @@ #include namespace casbin { - - typedef std::variant AttributeValue; - typedef std::pair Attribute; - typedef std::vector AttributeVector; - typedef std::initializer_list AttributeList; - typedef std::unordered_map AttributeMap; - - /** - * @brief A wrapper to contain ABAC entity with a list of attributes stored in a hashmap - * - */ - class ABACData { - - private: - - // HashMap containing attributes as key-value pairs - AttributeMap m_attributes; - - public: - /** - * @brief Construct a new casbin::ABACData object - * - * @param attribs Should be of the format: { - * { "attrib_name1", value1 }, - * { "attring_name2", value2 }, - * ... - * } - * - * Key's type is std::string and value's type can be one of std::string, int32_t, and float only - */ - ABACData(const AttributeMap& attribs); - /** - * @brief Add attribute to the corresponding ABAC entity - * - * @param key Name of the attribute - * @param value Value of the attribute - * @return true when attribute is added successfully, false otherwise - */ - bool AddAttribute(const std::string& key, const AttributeValue& value); - /** - * @brief Add attributes to the corresponding ABAC entity - * - * @param attribs Should be of the format: { - * { "attrib_name1", value1 }, - * { "attring_name2", value2 }, - * ... - * } - * - * Key's type is std::string and value's type can be one of std::string, int32_t, and float only - * @return true if attributes are added successfully, false otherwise - */ - bool AddAttributes(const AttributeList& attribs); - /** - * @brief Delete attribute of the corresponding ABAC entity - * - * @param key Name of the attribute to be deleted - * @return true when attribute is deleted successfully, false otherwise - */ - bool DeleteAttribute(const std::string& key); - /** - * @brief Update attribute of the corresponding ABAC entity - * - * @param key Name of the attribute to be updated - * @param value Value which would replace the current value of the attribute corresponding - * to the given key - * @return true - * @return false - */ - bool UpdateAttribute(const std::string& key, const AttributeValue& value); - /** - * @brief Get the Attributes of the corresponding ABAC entity - * - * @return const reference to the hashmap containing attributes in key-value pairs - */ - const AttributeMap& GetAttributes(); - }; - - // Casbin ABAC entity type - typedef ABACData ABACData; - - /** - * @brief Get casbin::ABACData object - * - * @param attribs Should be of the format: { - * { "attrib_name1", value1 }, - * { "attrib_name2", value2 }, - * ... - * } - * - * Key's type is std::string and value's type can be one of std::string, int32_t, double, and float only - * @return Pointer to casbin::ABACData entity - */ - const std::shared_ptr GetDataObject(const AttributeMap& attribs); - - typedef std::variant, std::shared_ptr> Data; + typedef std::variant> Data; typedef std::vector DataVector; typedef std::initializer_list DataList; typedef std::unordered_map DataMap; diff --git a/tests/enforcer_test.cpp b/tests/enforcer_test.cpp index 78f5ccf9..1b03b729 100644 --- a/tests/enforcer_test.cpp +++ b/tests/enforcer_test.cpp @@ -89,32 +89,6 @@ TEST(TestEnforcer, TestMapParams) { ASSERT_EQ(e.Enforce(params), true); } -TEST(TestEnforcer, ABACData) { - casbin::AttributeMap params = { - { "Name", "Yash" }, - { "Grade", 8.6f }, - { "Age", 18 }, - }; - - auto data = casbin::GetDataObject(params); - ASSERT_TRUE(params == data->GetAttributes()); - - data->DeleteAttribute("Name"); - params = { - { "Grade", 8.6f }, - { "Age", 18 }, - }; - ASSERT_TRUE(params == data->GetAttributes()); - - data->AddAttribute("ID", 156); - params["ID"] = 156; - ASSERT_TRUE(params == data->GetAttributes()); - - data->UpdateAttribute("ID", 152); - params["ID"] = 152; - ASSERT_TRUE(params == data->GetAttributes()); -} - TEST(TestEnforcer, JsonData) { using json = nlohmann::json; casbin::Scope scope = casbin::InitializeScope(); diff --git a/tests/util_test.cpp b/tests/util_test.cpp index eff46443..9fc271e0 100644 --- a/tests/util_test.cpp +++ b/tests/util_test.cpp @@ -98,4 +98,21 @@ TEST(TestUtil, TestReplaceEvalWithMap) { testReplaceEvalWithMap("eval(rule1) || eval(rule2) && c && d", {}, "eval(rule1) || eval(rule2) && c && d"); } +void testGetEvalValue(std::string s, std::vector res) { + auto myRes = casbin::GetEvalValue(s); + + ASSERT_EQ(res.size(), myRes.size()); + + for (size_t i = 0; i < res.size(); i++) { + ASSERT_EQ(res[i], myRes[i]); + } +} + +TEST(TestUtil, TestGetEvalValue) { + testGetEvalValue("eval(a) && a && b && c", {"a"}); + testGetEvalValue("a && eval(a) && b && c", {"a"}); + testGetEvalValue("eval(a) && eval(b) && a && b && c", {"a", "b"}); + testGetEvalValue("a && eval(a) && eval(b) && b && c", {"a", "b"}); +} + } // namespace