From 753880b162736e2b8e73f763c4d1f31679fa0916 Mon Sep 17 00:00:00 2001 From: "Yash Pandey (YP)" Date: Sat, 19 Jun 2021 04:21:46 +0530 Subject: [PATCH 1/4] test: Added RBAC tests and more Signed-off-by: Yash Pandey (YP) --- azure-pipelines.yml | 35 -- casbin/casbin.h | 1 + test/.clang-format | 27 - test/pch.cpp | 5 - test/pch.h | 15 - test/test.vcxproj | 190 ------- test/test.vcxproj.filters | 66 --- test/test_built_in_functions.cpp | 165 ------ test/test_config.cpp | 66 --- test/test_enforcer.cpp | 115 ---- test/test_enforcer_cached.cpp | 52 -- test/test_enforcer_synced.cpp | 75 --- test/test_management_api.cpp | 288 ---------- test/test_model.cpp | 100 ---- test/test_model_enforcer.cpp | 795 --------------------------- test/test_rbac_api.cpp | 231 -------- test/test_rbac_api_with_domains.cpp | 189 ------- test/test_role_manager.cpp | 188 ------- test/test_util.cpp | 65 --- tests/CMakeLists.txt | 5 + tests/model_enforcer_test.cpp | 759 +++++++++++++++++++++++++ tests/model_test.cpp | 100 ++++ tests/rbac_api_test.cpp | 228 ++++++++ tests/rbac_api_with_domains_test.cpp | 192 +++++++ tests/role_manager_test.cpp | 195 +++++++ 25 files changed, 1480 insertions(+), 2667 deletions(-) delete mode 100644 azure-pipelines.yml delete mode 100644 test/.clang-format delete mode 100644 test/pch.cpp delete mode 100644 test/pch.h delete mode 100644 test/test.vcxproj delete mode 100644 test/test.vcxproj.filters delete mode 100644 test/test_built_in_functions.cpp delete mode 100644 test/test_config.cpp delete mode 100644 test/test_enforcer.cpp delete mode 100644 test/test_enforcer_cached.cpp delete mode 100644 test/test_enforcer_synced.cpp delete mode 100644 test/test_management_api.cpp delete mode 100644 test/test_model.cpp delete mode 100644 test/test_model_enforcer.cpp delete mode 100644 test/test_rbac_api.cpp delete mode 100644 test/test_rbac_api_with_domains.cpp delete mode 100644 test/test_role_manager.cpp delete mode 100644 test/test_util.cpp create mode 100644 tests/model_enforcer_test.cpp create mode 100644 tests/model_test.cpp create mode 100644 tests/rbac_api_test.cpp create mode 100644 tests/rbac_api_with_domains_test.cpp create mode 100644 tests/role_manager_test.cpp diff --git a/azure-pipelines.yml b/azure-pipelines.yml deleted file mode 100644 index 5cab2d1d..00000000 --- a/azure-pipelines.yml +++ /dev/null @@ -1,35 +0,0 @@ -# .NET Desktop -# Build and run tests for .NET Desktop or Windows classic desktop solutions. -# Add steps that publish symbols, save build artifacts, and more: -# https://docs.microsoft.com/azure/devops/pipelines/apps/windows/dot-net - -trigger: -- master - -pool: - vmImage: 'windows-latest' - -variables: - solution: '**/*.sln' - buildPlatform: 'x64' - buildConfiguration: 'Debug' - -steps: -- task: NuGetToolInstaller@1 - -- task: NuGetCommand@2 - inputs: - restoreSolution: '$(solution)' - -- task: VSBuild@1 - inputs: - solution: '$(solution)' - platform: '$(buildPlatform)' - configuration: '$(buildConfiguration)' - -- task: VSTest@2 - inputs: - platform: '$(buildPlatform)' - configuration: '$(buildConfiguration)' - testAssemblyVer2: '**/test*.dll' - codeCoverageEnabled: True diff --git a/casbin/casbin.h b/casbin/casbin.h index d4172eb9..0525d69c 100644 --- a/casbin/casbin.h +++ b/casbin/casbin.h @@ -24,3 +24,4 @@ #include "persist.h" #include "util.h" #include "exception.h" +#include "rbac.h" diff --git a/test/.clang-format b/test/.clang-format deleted file mode 100644 index a37ae61b..00000000 --- a/test/.clang-format +++ /dev/null @@ -1,27 +0,0 @@ -# Visual Studio 生成的 .clang-format 文件 - -BasedOnStyle: Google -PointerAlignment: Right -AccessModifierOffset: -4 -IndentWidth: 4 -MaxEmptyLinesToKeep: 1 -BreakBeforeBraces: Attach -AllowShortFunctionsOnASingleLine: true -AllowShortIfStatementsOnASingleLine: true -AlignAfterOpenBracket: true -IndentCaseLabels: true -ObjCBlockIndentWidth: 4 -ObjCSpaceAfterProperty: true -ColumnLimit: 0 -AlignTrailingComments: true -SpaceAfterCStyleCast: false -AlignOperands: true -SpacesInSquareBrackets: false -AlignConsecutiveDeclarations: false -SpacesInContainerLiterals: false -BreakConstructorInitializersBeforeComma: true -AllowAllParametersOfDeclarationOnNextLine: true -ContinuationIndentWidth: 4 -TabWidth: 4 -SpaceBeforeAssignmentOperators: true -SpacesBeforeTrailingComments: 1 \ No newline at end of file diff --git a/test/pch.cpp b/test/pch.cpp deleted file mode 100644 index 64b7eef6..00000000 --- a/test/pch.cpp +++ /dev/null @@ -1,5 +0,0 @@ -// pch.cpp: source file corresponding to the pre-compiled header - -#include "pch.h" - -// When you are using pre-compiled headers, this source file is necessary for compilation to succeed. diff --git a/test/pch.h b/test/pch.h deleted file mode 100644 index 4d242716..00000000 --- a/test/pch.h +++ /dev/null @@ -1,15 +0,0 @@ -// pch.h: This is a precompiled header file. -// Files listed below are compiled only once, improving build performance for future builds. -// This also affects IntelliSense performance, including code completion and many code browsing features. -// However, files listed here are ALL re-compiled if any one of them is updated between builds. -// Do not add files here that you will be updating frequently as this negates the performance advantage. - -#ifndef PCH_H -#define PCH_H - -// add headers that you want to pre-compile here -#include "CppUnitTest.h" - -using namespace Microsoft::VisualStudio::CppUnitTestFramework; - -#endif //PCH_H diff --git a/test/test.vcxproj b/test/test.vcxproj deleted file mode 100644 index 5eb7cf90..00000000 --- a/test/test.vcxproj +++ /dev/null @@ -1,190 +0,0 @@ - - - - - Debug - Win32 - - - Release - Win32 - - - Debug - x64 - - - Release - x64 - - - - 16.0 - {4EA930EE-C5C4-4CF3-BE47-AAF37A158C40} - Win32Proj - test - 10.0 - NativeUnitTestProject - test - - - - DynamicLibrary - true - v142 - Unicode - false - - - DynamicLibrary - false - v142 - true - Unicode - false - - - DynamicLibrary - true - v142 - Unicode - false - - - DynamicLibrary - false - v142 - true - Unicode - false - - - - - - - - - - - - - - - - - - - - - true - - - true - - - false - - - false - - - - Use - Level3 - true - ../casbin;$(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories) - _DEBUG;%(PreprocessorDefinitions) - true - pch.h - - - Windows - ../$(IntDir);$(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories) - casbin.lib;%(AdditionalDependencies) - - - - - Use - Level3 - true - $(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories) - WIN32;_DEBUG;%(PreprocessorDefinitions) - true - pch.h - - - Windows - $(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories) - - - - - Use - Level3 - true - true - true - $(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories) - WIN32;NDEBUG;%(PreprocessorDefinitions) - true - pch.h - - - Windows - true - true - $(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories) - - - - - Use - Level3 - true - true - true - ../casbin;../casbin;$(VCInstallDir)UnitTest\include;%(AdditionalIncludeDirectories) - NDEBUG;%(PreprocessorDefinitions) - true - pch.h - - - Windows - true - true - ../$(IntDir);../$(IntDir);$(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories) - casbin.lib;%(AdditionalDependencies) - - - - - Create - Create - Create - Create - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/test/test.vcxproj.filters b/test/test.vcxproj.filters deleted file mode 100644 index 3300aca4..00000000 --- a/test/test.vcxproj.filters +++ /dev/null @@ -1,66 +0,0 @@ - - - - - {4FC737F1-C7A5-4376-A066-2A32D752A2FF} - cpp;c;cc;cxx;c++;def;odl;idl;hpj;bat;asm;asmx - - - {93995380-89BD-4b04-88EB-625FBE52EBFB} - h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd - - - {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} - rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms - - - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - - - Header Files - - - - - - \ No newline at end of file diff --git a/test/test_built_in_functions.cpp b/test/test_built_in_functions.cpp deleted file mode 100644 index 156d5d2c..00000000 --- a/test/test_built_in_functions.cpp +++ /dev/null @@ -1,165 +0,0 @@ -#include "pch.h" - -#ifndef TEST_BUILT_IN_FUNCTIONS_CPP -#define TEST_BUILT_IN_FUNCTIONS_CPP - - -#include - -namespace test_built_in_functions -{ - using namespace casbin; - - TEST_CLASS(TestBuiltInFunctions) - { - public: - - void TestKeyMatch(std::string key1, std::string key2, bool res){ - Scope scope = InitializeScope(); - PushStringValue(scope, key1); - PushStringValue(scope, key2); - - KeyMatch(scope); - bool my_res = GetBoolean(scope); - - Assert::AreEqual(res, my_res); - } - - TEST_METHOD(TestKeyMatch) { - TestKeyMatch("/foo", "/foo", true); - TestKeyMatch("/foo", "/foo*", true); - TestKeyMatch("/foo", "/foo/*", false); - TestKeyMatch("/foo/bar", "/foo", false); - TestKeyMatch("/foo/bar", "/foo*", true); - TestKeyMatch("/foo/bar", "/foo/*", true); - TestKeyMatch("/foobar", "/foo", false); - TestKeyMatch("/foobar", "/foo*", true); - TestKeyMatch("/foobar", "/foo/*", false); - } - - void TestKeyMatch2(std::string key1, std::string key2, bool res) { - Scope scope = InitializeScope(); - PushStringValue(scope, key1); - PushStringValue(scope, key2); - - KeyMatch2(scope); - bool my_res = GetBoolean(scope); - - Assert::AreEqual(res, my_res); - } - - TEST_METHOD(TestKeyMatch2){ - TestKeyMatch2("/foo", "/foo", true); - TestKeyMatch2("/foo", "/foo*", true); - TestKeyMatch2("/foo", "/foo/*", false); - TestKeyMatch2("/foo/bar", "/foo", false); - TestKeyMatch2("/foo/bar", "/foo*", false); // different with KeyMatch. - TestKeyMatch2("/foo/bar", "/foo/*", true); - TestKeyMatch2("/foobar", "/foo", false); - TestKeyMatch2("/foobar", "/foo*", false); // different with KeyMatch. - TestKeyMatch2("/foobar", "/foo/*", false); - - TestKeyMatch2("/", "/:resource", false); - TestKeyMatch2("/resource1", "/:resource", true); - TestKeyMatch2("/myid", "/:id/using/:resId", false); - TestKeyMatch2("/myid/using/myresid", "/:id/using/:resId", true); - - TestKeyMatch2("/proxy/myid", "/proxy/:id/*", false); - TestKeyMatch2("/proxy/myid/", "/proxy/:id/*", true); - TestKeyMatch2("/proxy/myid/res", "/proxy/:id/*", true); - TestKeyMatch2("/proxy/myid/res/res2", "/proxy/:id/*", true); - TestKeyMatch2("/proxy/myid/res/res2/res3", "/proxy/:id/*", true); - TestKeyMatch2("/proxy/", "/proxy/:id/*", false); - - TestKeyMatch2("/alice", "/:id", true); - TestKeyMatch2("/alice/all", "/:id/all", true); - TestKeyMatch2("/alice", "/:id/all", false); - TestKeyMatch2("/alice/all", "/:id", false); - - TestKeyMatch2("/alice/all", "/:/all", false); - } - - void TestKeyMatch3(std::string key1, std::string key2, bool res) { - Scope scope = InitializeScope(); - PushStringValue(scope, key1); - PushStringValue(scope, key2); - - KeyMatch3(scope); - bool my_res = GetBoolean(scope); - - Assert::AreEqual(res, my_res); - } - - TEST_METHOD(TestKeyMatch3){ - // keyMatch3() is similar with KeyMatch2(), except using "/proxy/{id}" instead of "/proxy/:id". - TestKeyMatch3("/foo", "/foo", true); - TestKeyMatch3("/foo", "/foo*", true); - TestKeyMatch3("/foo", "/foo/*", false); - TestKeyMatch3("/foo/bar", "/foo", false); - TestKeyMatch3("/foo/bar", "/foo*", false); - TestKeyMatch3("/foo/bar", "/foo/*", true); - TestKeyMatch3("/foobar", "/foo", false); - TestKeyMatch3("/foobar", "/foo*", false); - TestKeyMatch3("/foobar", "/foo/*", false); - - TestKeyMatch3("/", "/{resource}", false); - TestKeyMatch3("/resource1", "/{resource}", true); - TestKeyMatch3("/myid", "/{id}/using/{resId}", false); - TestKeyMatch3("/myid/using/myresid", "/{id}/using/{resId}", true); - - TestKeyMatch3("/proxy/myid", "/proxy/{id}/*", false); - TestKeyMatch3("/proxy/myid/", "/proxy/{id}/*", true); - TestKeyMatch3("/proxy/myid/res", "/proxy/{id}/*", true); - TestKeyMatch3("/proxy/myid/res/res2", "/proxy/{id}/*", true); - TestKeyMatch3("/proxy/myid/res/res2/res3", "/proxy/{id}/*", true); - TestKeyMatch3("/proxy/", "/proxy/{id}/*", false); - - TestKeyMatch3("/myid/using/myresid", "/{id/using/{resId}", false); - } - - void TestRegexMatch(std::string key1, std::string key2, bool res) { - Scope scope = InitializeScope(); - PushStringValue(scope, key1); - PushStringValue(scope, key2); - - RegexMatch(scope); - bool my_res = GetBoolean(scope); - - Assert::AreEqual(res, my_res); - } - - TEST_METHOD(TestRegexMatch) { - TestRegexMatch("/topic/create", "/topic/create", true); - TestRegexMatch("/topic/create/123", "/topic/create", false); - TestRegexMatch("/topic/delete", "/topic/create", false); - TestRegexMatch("/topic/edit", "/topic/edit/[0-9]+", false); - TestRegexMatch("/topic/edit/123", "/topic/edit/[0-9]+", true); - TestRegexMatch("/topic/edit/abc", "/topic/edit/[0-9]+", false); - TestRegexMatch("/foo/delete/123", "/topic/delete/[0-9]+", false); - TestRegexMatch("/topic/delete/0", "/topic/delete/[0-9]+", true); - TestRegexMatch("/topic/edit/123s", "/topic/delete/[0-9]+", false); - } - - void TestIPMatch(std::string ip1, std::string ip2, bool res) { - Scope scope = InitializeScope(); - PushStringValue(scope, ip1); - PushStringValue(scope, ip2); - - IPMatch(scope); - bool my_res = GetBoolean(scope); - - Assert::AreEqual(res, my_res); - } - - TEST_METHOD(TestIPMatch) { - TestIPMatch("192.168.2.123", "192.168.2.0/24", true); - TestIPMatch("192.168.2.123", "192.168.3.0/24", false); - TestIPMatch("192.168.2.123", "192.168.2.0/16", true); - TestIPMatch("192.168.2.123", "192.168.2.123/32", true); - TestIPMatch("10.0.0.11", "10.0.0.0/8", true); - TestIPMatch("11.0.0.123", "10.0.0.0/8", false); - } - }; -} - -#endif // TEST_BUILT_IN_FUNCTIONS_CPP diff --git a/test/test_config.cpp b/test/test_config.cpp deleted file mode 100644 index e58c719e..00000000 --- a/test/test_config.cpp +++ /dev/null @@ -1,66 +0,0 @@ -#include "pch.h" - -#ifndef TEST_CONFIG_CPP -#define TEST_CONFIG_CPP - - -#include -#include - -namespace test_config -{ - using namespace casbin; - - TEST_CLASS(TestConfig) - { - public: - - std::shared_ptr config; - - TEST_METHOD_INITIALIZE(InitializeConfig) { - std::string filepath = "../../casbin/config/testdata/testini.ini"; - config = Config::NewConfig(filepath); - } - - TEST_METHOD(TestDebug) { - Assert::IsTrue(config->GetBool("debug")); - } - - TEST_METHOD(TestURL) { - Assert::AreEqual(std::string("act.wiki"), config->GetString("url")); - } - - TEST_METHOD(TestRedis) { - std::vector values = config->GetStrings("redis::redis.key"); - Assert::AreEqual(std::string("push1"), values[0]); - Assert::AreEqual(std::string("push2"), values[1]); - } - - TEST_METHOD(TestMYSQLDEV) { - Assert::AreEqual(std::string("127.0.0.1"), config->GetString("mysql::mysql.dev.host")); - } - - TEST_METHOD(TestMYSQLMASTER) { - Assert::AreEqual(std::string("10.0.0.1"), config->GetString("mysql::mysql.master.host")); - } - - TEST_METHOD(TestMathInt) { - Assert::AreEqual(64, config->GetInt("math::math.i64")); - } - - TEST_METHOD(TestMathFloat) { - Assert::AreEqual(float(64.1), config->GetFloat("math::math.f64")); - } - - TEST_METHOD(TestSetConfig) { - config->Set("other::key1", "new test key"); - Assert::AreEqual(std::string("new test key"), config->GetString("other::key1")); - } - - TEST_METHOD(TestMulti) { - Assert::AreEqual(std::string("r.sub==p.sub && r.obj==p.obj"), config->GetString("multi1::name")); - } - }; -} - -#endif // TEST_CONFIG_CPP diff --git a/test/test_enforcer.cpp b/test/test_enforcer.cpp deleted file mode 100644 index 0dd6728f..00000000 --- a/test/test_enforcer.cpp +++ /dev/null @@ -1,115 +0,0 @@ -#include "pch.h" - -#ifndef TEST_ENFORCER_CPP -#define TEST_ENFORCER_CPP - - -#include -#include -#include -#include - -namespace test_enforcer -{ - using namespace casbin; - - TEST_CLASS(TestEnforcer) - { - public: - - void TestEnforce(Enforcer e, std::string sub, std::string dom, std::string obj, std::string act, bool res){ - Assert::AreEqual(res, e.Enforce({sub, dom, obj, act})); - } - - void TestEnforce(Enforcer e, std::string sub, std::string obj, std::string act, bool res) { - Assert::AreEqual(res, e.Enforce({sub, obj, act})); - } - - void TestEnforce(Enforcer e, std::vector params, bool res) { - Assert::AreEqual(res, e.Enforce(params)); - } - - void TestEnforce(Enforcer e, std::unordered_map params, bool res) { - Assert::AreEqual(res, e.Enforce(params)); - } - - - TEST_METHOD(TestFourParams) { - - std::string model = "../../examples/rbac_with_domains_model.conf"; - std::string policy = "../../examples/rbac_with_domains_policy.csv"; - Enforcer e = Enforcer(model, policy); - - TestEnforce(e, "alice", "domain1", "data1", "read", true); - TestEnforce(e, "alice", "domain1", "data1", "write", true); - TestEnforce(e, "alice", "domain1", "data2", "read", false); - TestEnforce(e, "alice", "domain1", "data2", "write", false); - TestEnforce(e, "bob", "domain2", "data1", "read", false); - TestEnforce(e, "bob", "domain2", "data1", "write", false); - TestEnforce(e, "bob", "domain2", "data2", "read", true); - TestEnforce(e, "bob", "domain2", "data2", "write", true); - } - - TEST_METHOD(TestThreeParams) { - std::string model = "../../examples/basic_model_without_spaces.conf"; - std::string policy = "../../examples/basic_policy.csv"; - Enforcer e = Enforcer(model, policy); - - TestEnforce(e, { "alice", "data1", "read" }, true); - TestEnforce(e, { "alice", "data1", "write" }, false); - TestEnforce(e, { "alice", "data2", "read" }, false); - TestEnforce(e, { "alice", "data2", "write" }, false); - TestEnforce(e, { "bob", "data1", "read" }, false); - TestEnforce(e, { "bob", "data1", "write" }, false); - TestEnforce(e, { "bob", "data2", "read" }, false); - TestEnforce(e, { "bob", "data2", "write" }, true); - } - - TEST_METHOD(TestVectorParams) { - std::string model = "../../examples/basic_model_without_spaces.conf"; - std::string policy = "../../examples/basic_policy.csv"; - Enforcer e = Enforcer(model, policy); - - TestEnforce(e, { "alice", "data1", "read" }, true); - TestEnforce(e, { "alice", "data1", "write" }, false); - TestEnforce(e, {"alice", "data2", "read" }, false); - TestEnforce(e, {"alice", "data2", "write" }, false); - TestEnforce(e, {"bob", "data1", "read" }, false); - TestEnforce(e, {"bob", "data1", "write" }, false); - TestEnforce(e, {"bob", "data2", "read" }, false); - TestEnforce(e, {"bob", "data2", "write" }, true); - } - - TEST_METHOD(TestMapParams) { - std::string model = "../../examples/basic_model_without_spaces.conf"; - std::string policy = "../../examples/basic_policy.csv"; - Enforcer e = Enforcer(model, policy); - - std::unordered_map params = {{"sub", "alice"}, {"obj", "data1"}, {"act", "read"}}; - TestEnforce(e, params, true); - - params = { {"sub","alice"},{"obj","data1"},{"act","write"} }; - TestEnforce(e, params, false); - - params = { {"sub","alice"},{"obj","data2"},{"act","read"} }; - TestEnforce(e, params, false); - - params = { {"sub","alice"},{"obj","data2"},{"act","write"} }; - TestEnforce(e, params, false); - - params = { {"sub","bob"},{"obj","data1"},{"act","read"} }; - TestEnforce(e, params, false); - - params = { {"sub","bob"},{"obj","data1"},{"act","write"} }; - TestEnforce(e, params, false); - - params = { {"sub","bob"},{"obj","data2"},{"act","read"} }; - TestEnforce(e, params, false); - - params = { {"sub","bob"},{"obj","data2"},{"act","write"} }; - TestEnforce(e, params, true); - } - }; -} - -#endif // TEST_ENFORCER_CPP diff --git a/test/test_enforcer_cached.cpp b/test/test_enforcer_cached.cpp deleted file mode 100644 index e5ce13d3..00000000 --- a/test/test_enforcer_cached.cpp +++ /dev/null @@ -1,52 +0,0 @@ -#include "pch.h" - -#ifndef TEST_ENFORCER_CACHED_CPP -#define TEST_ENFORCER_CACHED_CPP - - -#include - -namespace test_enforcer_cached -{ - using namespace casbin; - - TEST_CLASS(TestEnforcerCached) - { - public: - - void testEnforceCache(CachedEnforcer & e, std::string sub, std::string obj, std::string act, bool res){ - Assert::AreEqual(res, e.Enforce({sub,obj,act})); - } - - - - TEST_METHOD(TestCache) { - std::string model = "../../examples/basic_model.conf"; - std::string policy = "../../examples/basic_policy.csv"; - CachedEnforcer e = CachedEnforcer(model, policy); - testEnforceCache(e, "alice", "data1", "read", true); - testEnforceCache(e, "alice", "data1", "write", false); - testEnforceCache(e, "alice", "data2", "read", false); - testEnforceCache(e, "alice", "data2", "write", false); - - // The cache is enabled, so even if we remove a policy rule, the decision - // for ("alice", "data1", "read") will still be true, as it uses the cached result. - e.RemovePolicy({"alice", "data1", "read"}); - testEnforceCache(e, "alice", "data1", "read", true); - testEnforceCache(e, "alice", "data1", "write", false); - testEnforceCache(e, "alice", "data2", "read", false); - testEnforceCache(e, "alice", "data2", "write", false); - - // Now we invalidate the cache, then all first-coming Enforce() has to be evaluated in real-time. - // The decision for ("alice", "data1", "read") will be false now. - e.InvalidateCache(); - testEnforceCache(e, "alice", "data1", "read", false); - testEnforceCache(e, "alice", "data1", "write", false); - testEnforceCache(e, "alice", "data2", "read", false); - testEnforceCache(e, "alice", "data2", "write", false); - } - - }; -} - -#endif // TEST_ENFORCER_CACHED_CPP diff --git a/test/test_enforcer_synced.cpp b/test/test_enforcer_synced.cpp deleted file mode 100644 index ca15c006..00000000 --- a/test/test_enforcer_synced.cpp +++ /dev/null @@ -1,75 +0,0 @@ -#include "pch.h" - -#ifndef TEST_ENFORCER_SYNCED_CPP -#define TEST_ENFORCER_SYNCED_CPP - -#include - -using namespace std; - -namespace test_enforcer_synced { - - using namespace casbin; - - TEST_CLASS(TestEnforcerSynced){ - public: - - void testEnforceSync(SyncedEnforcer & e, string sub, string obj, string act, bool res){ - Assert::AreEqual(res, e.Enforce({sub, obj, act})); - } - - void testAutoLoadRunning(bool test, bool control) { - Assert::AreEqual(test, control); - } - - TEST_METHOD(TestSync) { - string model = "../../examples/basic_model.conf"; - string policy = "../../examples/basic_policy.csv"; - SyncedEnforcer e(model, policy); - - chrono::duration t = 200ms; - - e.StartAutoLoadPolicy(t); - - testEnforceSync(e, "alice", "data1", "read", true); - testEnforceSync(e, "alice", "data1", "write", false); - testEnforceSync(e, "alice", "data2", "read", false); - testEnforceSync(e, "alice", "data2", "write", false); - testEnforceSync(e, "bob", "data1", "read", false); - testEnforceSync(e, "bob", "data1", "write", false); - testEnforceSync(e, "bob", "data2", "read", false); - testEnforceSync(e, "bob", "data2", "write", true); - - e.StopAutoLoadPolicy(); - } - - TEST_METHOD(TestStopLoadPolicy) { - string model = "../../examples/basic_model.conf"; - string policy = "../../examples/basic_policy.csv"; - SyncedEnforcer e(model, policy); - - chrono::duration t = 5ms; - - e.StartAutoLoadPolicy(t); - - testAutoLoadRunning(e.IsAutoLoadingRunning(), true); - - testEnforceSync(e, "alice", "data1", "read", true); - testEnforceSync(e, "alice", "data1", "write", false); - testEnforceSync(e, "alice", "data2", "read", false); - testEnforceSync(e, "alice", "data2", "write", false); - testEnforceSync(e, "bob", "data1", "read", false); - testEnforceSync(e, "bob", "data1", "write", false); - testEnforceSync(e, "bob", "data2", "read", false); - testEnforceSync(e, "bob", "data2", "write", true); - - e.StopAutoLoadPolicy(); - this_thread::sleep_for(10ms); - - testAutoLoadRunning(e.IsAutoLoadingRunning(), false); - - } - }; -} - -#endif // TEST_ENFORCER_SYNCED_CPP diff --git a/test/test_management_api.cpp b/test/test_management_api.cpp deleted file mode 100644 index d49da842..00000000 --- a/test/test_management_api.cpp +++ /dev/null @@ -1,288 +0,0 @@ -#include "pch.h" - -#ifndef TEST_MANAGEMENT_API_CPP -#define TEST_MANAGEMENT_API_CPP - - -#include -#include -#include -#include -#include - -namespace test_management_api -{ - using namespace casbin; - - TEST_CLASS(TestManagementAPI) - { - public: - - TEST_METHOD(TestGetList) { - std::string model = "../../examples/rbac_model.conf"; - std::string policy = "../../examples/rbac_policy.csv"; - Enforcer e = Enforcer(model, policy); - - Assert::IsTrue(ArrayEquals({ "alice", "bob", "data2_admin" }, e.GetAllSubjects())); - Assert::IsTrue(ArrayEquals({ "data1", "data2" }, e.GetAllObjects())); - Assert::IsTrue(ArrayEquals({ "read", "write" }, e.GetAllActions())); - Assert::IsTrue(ArrayEquals({ "data2_admin" }, e.GetAllRoles())); - } - - void TestGetPolicy(Enforcer e, std::vector> res) { - std::vector> my_res; - my_res = e.GetPolicy(); - - int count = 0; - for (int i = 0; i < my_res.size(); i++) { - for (int j = 0; j < res.size(); j++) { - if (ArrayEquals(my_res[i], res[j])) - count++; - } - } - - if (count == res.size()) - Assert::IsTrue(true); - } - - void TestGetFilteredPolicy(Enforcer e, int field_index, std::vector> res, std::vector field_values) { - std::vector> my_res = e.GetFilteredPolicy(field_index, field_values); - for (int i = 0; i < res.size(); i++) { - Assert::IsTrue(ArrayEquals(my_res[i], res[i])); - } - } - - void TestGetGroupingPolicy(Enforcer e, std::vector> res) { - std::vector> my_res = e.GetGroupingPolicy(); - - for (int i = 0; i < my_res.size(); i++) { - Assert::IsTrue(ArrayEquals(my_res[i], res[i])); - } - } - - void TestGetFilteredGroupingPolicy(Enforcer e, int field_index, std::vector> res, std::vector field_values) { - std::vector> my_res = e.GetFilteredGroupingPolicy(field_index, field_values); - - for (int i = 0; i < my_res.size(); i++) { - Assert::IsTrue(ArrayEquals(my_res[i], res[i])); - } - } - - void TestHasPolicy(Enforcer e, std::vector policy, bool res) { - bool my_res = e.HasPolicy(policy); - Assert::AreEqual(res, my_res); - } - - void TestHasGroupingPolicy(Enforcer e, std::vector policy, bool res) { - bool my_res = e.HasGroupingPolicy(policy); - Assert::AreEqual(res, my_res); - } - - TEST_METHOD(TestGetPolicyAPI) { - std::string model = "../../examples/rbac_model.conf"; - std::string policy = "../../examples/rbac_policy.csv"; - Enforcer e = Enforcer(model, policy); - - TestGetPolicy(e, std::vector>{ - {"alice", "data1", "read"}, - { "bob", "data2", "write" }, - { "data2_admin", "data2", "read" }, - { "data2_admin", "data2", "write" }}); - - TestGetFilteredPolicy(e, 0, { {"alice", "data1", "read"} }, {"alice"}); - TestGetFilteredPolicy(e, 0, { {"bob", "data2", "write"}}, {"bob"}); - TestGetFilteredPolicy(e, 0, { {"data2_admin", "data2", "read"}, { "data2_admin", "data2", "write" }}, {"data2_admin"}); - TestGetFilteredPolicy(e, 1, { {"alice", "data1", "read"}}, {"data1"}); - TestGetFilteredPolicy(e, 1, { {"bob", "data2", "write"}, { "data2_admin", "data2", "read" }, { "data2_admin", "data2", "write" }}, {"data2"}); - TestGetFilteredPolicy(e, 2, { {"alice", "data1", "read"}, { "data2_admin", "data2", "read" }}, {"read"}); - TestGetFilteredPolicy(e, 2, { {"bob", "data2", "write"}, { "data2_admin", "data2", "write" }}, {"write"}); - - TestGetFilteredPolicy(e, 0, { {"data2_admin", "data2", "read"}, { "data2_admin", "data2", "write" }}, {"data2_admin", "data2"}); - // Note: "" (empty string) in fieldValues means matching all values. - TestGetFilteredPolicy(e, 0, { {"data2_admin", "data2", "read"}}, {"data2_admin", "", "read"}); - TestGetFilteredPolicy(e, 1, { {"bob", "data2", "write"}, { "data2_admin", "data2", "write" }}, {"data2", "write"}); - - TestHasPolicy(e, {"alice", "data1", "read"}, true); - TestHasPolicy(e, {"bob", "data2", "write"}, true); - TestHasPolicy(e, {"alice", "data2", "read"}, false); - TestHasPolicy(e, {"bob", "data3", "write"}, false); - - TestGetGroupingPolicy(e, std::vector>{ {"alice", "data2_admin"}}); - - TestGetFilteredGroupingPolicy(e, 0, {{"alice", "data2_admin"}}, {"alice"}); - TestGetFilteredGroupingPolicy(e, 0, {}, {"bob"}); - TestGetFilteredGroupingPolicy(e, 1, {}, {"data1_admin"}); - TestGetFilteredGroupingPolicy(e, 1, { {"alice", "data2_admin"}}, {"data2_admin"}); - // Note: "" (empty string) in fieldValues means matching all values. - TestGetFilteredGroupingPolicy(e, 0, { {"alice", "data2_admin"}}, {"", "data2_admin"}); - - TestHasGroupingPolicy(e, {"alice", "data2_admin"}, true); - TestHasGroupingPolicy(e, {"bob", "data2_admin"}, false); - } - - - TEST_METHOD(TestModifyPolicyAPI) { - std::string model = "../../examples/rbac_model.conf"; - std::string policy = "../../examples/rbac_policy.csv"; - std::shared_ptr adapter = std::shared_ptr(new BatchFileAdapter(policy)); - Enforcer e = Enforcer(model, adapter); - - TestGetPolicy(e, { - {"alice", "data1", "read"}, - {"bob", "data2", "write"}, - {"data2_admin", "data2", "read"}, - {"data2_admin", "data2", "write"} - }); - - e.RemovePolicy({"alice", "data1", "read"}); - e.RemovePolicy({"bob", "data2", "write"}); - e.RemovePolicy({"alice", "data1", "read"}); - e.AddPolicy({"eve", "data3", "read"}); - e.AddPolicy({"eve", "data3", "read"}); - - std::vector> rules { - {"jack", "data4", "read"}, - {"katy", "data4", "write"}, - {"leyo", "data4", "read"}, - {"ham", "data4", "write"}, - }; - - e.AddPolicies(rules); - e.AddPolicies(rules); - - TestGetPolicy(e, { - {"data2_admin", "data2", "read"}, - { "data2_admin", "data2", "write" }, - { "eve", "data3", "read" }, - { "jack", "data4", "read" }, - { "katy", "data4", "write" }, - { "leyo", "data4", "read" }, - { "ham", "data4", "write" } - }); - - e.RemovePolicies(rules); - e.RemovePolicies(rules); - - std::vector named_policy{ "eve", "data3", "read" }; - e.RemoveNamedPolicy("p", named_policy); - e.AddNamedPolicy("p", named_policy); - - TestGetPolicy(e, { - {"data2_admin", "data2", "read"}, - { "data2_admin", "data2", "write" }, - { "eve", "data3", "read" } - }); - - e.RemoveFilteredPolicy(1, {"data2"}); - - TestGetPolicy(e, { {"eve", "data3", "read"}}); - - e.UpdatePolicy({"eve", "data3", "read"}, {"eve", "data3", "write"}); - TestGetPolicy(e, {{"eve", "data3", "write"}}); - - e.AddPolicies(rules); - e.UpdatePolicies({ - {"eve", "data3", "write"}, - {"leyo", "data4", "read"}, - {"katy", "data4", "write"} - }, { - {"eve", "data3", "read"}, - {"leyo", "data4", "write"}, - {"katy", "data1", "write"} - }); - - TestGetPolicy(e, { - {"eve", "data3", "read"}, - {"leyo", "data4", "write"}, - {"katy", "data1", "write"} - }); - } - - TEST_METHOD(TestModifyGroupingPolicyAPI) { - std::string model = "../../examples/rbac_model.conf"; - std::string policy = "../../examples/rbac_policy.csv"; - std::shared_ptr adapter = std::make_shared(policy); - Enforcer e = Enforcer(model, adapter); - - Assert::IsTrue(ArrayEquals({"data2_admin"}, e.GetRolesForUser("alice"))); - Assert::IsTrue(ArrayEquals({}, e.GetRolesForUser("bob"))); - Assert::IsTrue(ArrayEquals({}, e.GetRolesForUser("eve"))); - Assert::IsTrue(ArrayEquals({}, e.GetRolesForUser("non_exist"))); - - e.RemoveGroupingPolicy({"alice", "data2_admin"}); - e.AddGroupingPolicy({"bob", "data1_admin"}); - e.AddGroupingPolicy({"eve", "data3_admin"}); - - std::vector> grouping_rules { - {"ham", "data4_admin"}, - {"jack", "data5_admin"}, - }; - - e.AddGroupingPolicies(grouping_rules); - Assert::IsTrue(ArrayEquals({"data4_admin"}, e.GetRolesForUser("ham"))); - Assert::IsTrue(ArrayEquals({"data5_admin"}, e.GetRolesForUser("jack"))); - e.RemoveGroupingPolicies(grouping_rules); - - Assert::IsTrue(ArrayEquals({}, e.GetRolesForUser("alice"))); - std::vector named_grouping_policy{ "alice", "data2_admin" }; - Assert::IsTrue(ArrayEquals({}, e.GetRolesForUser("alice"))); - e.AddNamedGroupingPolicy("g", named_grouping_policy); - Assert::IsTrue(ArrayEquals({"data2_admin"}, e.GetRolesForUser("alice"))); - e.RemoveNamedGroupingPolicy("g", named_grouping_policy); - - e.AddNamedGroupingPolicies("g", grouping_rules); - e.AddNamedGroupingPolicies("g", grouping_rules); - Assert::IsTrue(ArrayEquals({"data4_admin"}, e.GetRolesForUser("ham"))); - Assert::IsTrue(ArrayEquals({"data5_admin"}, e.GetRolesForUser("jack"))); - e.RemoveNamedGroupingPolicies("g", grouping_rules); - e.RemoveNamedGroupingPolicies("g", grouping_rules); - - Assert::IsTrue(ArrayEquals({}, e.GetRolesForUser("alice"))); - Assert::IsTrue(ArrayEquals({"data1_admin"}, e.GetRolesForUser("bob"))); - Assert::IsTrue(ArrayEquals({"data3_admin"}, e.GetRolesForUser("eve"))); - Assert::IsTrue(ArrayEquals({}, e.GetRolesForUser("non_exist"))); - - Assert::IsTrue(ArrayEquals({"bob"}, e.GetUsersForRole("data1_admin"))); - try { - e.GetUsersForRole("data2_admin", {}); - } - catch (CasbinRBACException e) { - Assert::IsTrue(true); - } - Assert::IsTrue(ArrayEquals({"eve"}, e.GetUsersForRole("data3_admin"))); - - e.RemoveFilteredGroupingPolicy(0, {"bob"}); - - Assert::IsTrue(ArrayEquals({}, e.GetRolesForUser("alice"))); - Assert::IsTrue(ArrayEquals({}, e.GetRolesForUser("bob"))); - Assert::IsTrue(ArrayEquals({"data3_admin"}, e.GetRolesForUser("eve"))); - Assert::IsTrue(ArrayEquals({}, e.GetRolesForUser("non_exist"))); - - try { - e.GetUsersForRole("data1_admin"); - } - catch (CasbinRBACException e) { - Assert::IsTrue(true); - } - try { - e.GetUsersForRole("data2_admin"); - } - catch (CasbinRBACException e) { - Assert::IsTrue(true); - } - Assert::IsTrue(ArrayEquals({"eve"}, e.GetUsersForRole("data3_admin"))); - - Assert::IsTrue(e.AddGroupingPolicy({"data3_admin", "data4_admin"})); - e.UpdateGroupingPolicy({"eve", "data3_admin"}, {"eve", "admin"}); - e.UpdateGroupingPolicy({"data3_admin", "data4_admin"}, {"admin", "data4_admin"}); - - // Assert::IsTrue(ArrayEquals({"admin"}, e.GetUsersForRole("data4_admin"))); - Assert::IsTrue(ArrayEquals({"eve"}, e.GetUsersForRole("admin"))); - - Assert::IsTrue(ArrayEquals({"admin"}, e.GetRolesForUser("eve"))); - Assert::IsTrue(ArrayEquals({"data4_admin"}, e.GetRolesForUser("admin"))); - } - }; -} - -#endif // TEST_MANAGEMENT_API_CPP diff --git a/test/test_model.cpp b/test/test_model.cpp deleted file mode 100644 index 86d275ad..00000000 --- a/test/test_model.cpp +++ /dev/null @@ -1,100 +0,0 @@ -#include "pch.h" - -#ifndef TEST_MODEL_CPP -#define TEST_MODEL_CPP - - -#include - -#include -#include -#include -#include - -namespace test_model -{ - using namespace casbin; - - TEST_CLASS(TestModel) - { - public: - - std::string basic_example; - std::shared_ptr basic_config; - - TEST_METHOD_INITIALIZE(InitializeBasicConfig) { - basic_example = "../../examples/basic_model.conf"; - basic_config = Config::NewConfig(basic_example); - } - - TEST_METHOD(TestNewModel) { - Model* model = Model::NewModel(); - Assert::IsNotNull(model); - } - - TEST_METHOD(TestNewModelFromFile) { - Model* model = Model::NewModelFromFile(basic_example); - Assert::IsNotNull(model); - } - - TEST_METHOD(TestNewModelFromString) { - std::ifstream infile; - infile.open(basic_example); - std::string content; - std::getline(infile, content, '\0'); - Model* model = Model::NewModelFromString(content); - - Assert::IsNotNull(model); - } - - TEST_METHOD(TestLoadModelFromConfig) { - Model* model = Model::NewModel(); - model->LoadModelFromConfig(basic_config); - - model = Model::NewModel(); - std::shared_ptr config = Config::NewConfigFromText(""); - try { - model->LoadModelFromConfig(config); - Assert::Fail(); - } - catch (MissingRequiredSections e) { - } - } - - TEST_METHOD(TestHasSection) { - Model* model = Model::NewModel(); - model->LoadModelFromConfig(basic_config); - - for (int i = 0; i < (Model::required_sections).size(); i++) { - Assert::IsTrue(model->HasSection((Model::required_sections)[i])); - } - - model = Model::NewModel(); - std::shared_ptr config = Config::NewConfigFromText(""); - try { - model->LoadModelFromConfig(config); - Assert::Fail(); - } - catch (MissingRequiredSections e) { - } - - for (int i = 0; i < (Model::required_sections).size(); i++) { - Assert::IsFalse(model->HasSection((Model::required_sections)[i])); - } - } - - TEST_METHOD(TestModel_AddDef) { - Model* model = Model::NewModel(); - std::string s = "r"; - std::string v = "sub, obj, act"; - - bool ok = model->AddDef(s, s, v); - Assert::IsTrue(ok); - - ok = model->AddDef(s, s, ""); - Assert::IsFalse(ok); - } - }; -} - -#endif // TEST_MODEL_CPP diff --git a/test/test_model_enforcer.cpp b/test/test_model_enforcer.cpp deleted file mode 100644 index 4c32fb51..00000000 --- a/test/test_model_enforcer.cpp +++ /dev/null @@ -1,795 +0,0 @@ -#include "pch.h" - -#ifndef TEST_MODEL_ENFORCER_CPP -#define TEST_MODEL_ENFORCER_CPP - - -#include -#include -#include -#include - -namespace test_model_enforcer -{ - using namespace casbin; - - TEST_CLASS(TestModelEnforcer) - { - public: - - Scope InitializeParams(std::string sub, std::string obj, std::string act){ - Scope scope = InitializeScope(); - PushObject(scope, "r"); - PushStringPropToObject(scope, "r", sub, "sub"); - PushStringPropToObject(scope, "r", obj, "obj"); - PushStringPropToObject(scope, "r", act, "act"); - - return scope; - } - - Scope InitializeParamsWithoutUsers(std::string obj, std::string act) { - Scope scope = InitializeScope(); - PushObject(scope, "r"); - PushStringPropToObject(scope, "r", obj, "obj"); - PushStringPropToObject(scope, "r", act, "act"); - return scope; - } - - Scope InitializeParamsWithoutResources(std::string sub, std::string act) { - Scope scope = InitializeScope(); - PushObject(scope, "r"); - PushStringPropToObject(scope, "r", sub, "sub"); - PushStringPropToObject(scope, "r", act, "act"); - - return scope; - } - - Scope InitializeParamsWithDomains(std::string sub, std::string domain, std::string obj, std::string act) { - Scope scope = InitializeScope(); - PushObject(scope, "r"); - PushStringPropToObject(scope, "r", sub, "sub"); - PushStringPropToObject(scope, "r", domain, "dom"); - PushStringPropToObject(scope, "r", obj, "obj"); - PushStringPropToObject(scope, "r", act, "act"); - - return scope; - } - - void TestEnforce(Enforcer e, Scope scope, bool res) { - Assert::AreEqual(res, e.Enforce(scope)); - } - - TEST_METHOD(TestBasicModel) { - std::string model = "../../examples/basic_model.conf"; - std::string policy = "../../examples/basic_policy.csv"; - Enforcer e = Enforcer(model, policy); - - Scope scope; - - scope = InitializeParams("alice", "data1", "read"); - TestEnforce(e, scope, true); - scope = InitializeParams("alice", "data1", "write"); - TestEnforce(e, scope, false); - scope = InitializeParams("alice", "data2", "read"); - TestEnforce(e, scope, false); - scope = InitializeParams("alice", "data2", "write"); - TestEnforce(e, scope, false); - scope = InitializeParams("bob", "data1", "read"); - TestEnforce(e, scope, false); - scope = InitializeParams("bob", "data1", "write"); - TestEnforce(e, scope, false); - scope = InitializeParams("bob", "data2", "read"); - TestEnforce(e, scope, false); - scope = InitializeParams("bob", "data2", "write"); - TestEnforce(e, scope, true); - } - - TEST_METHOD(TestBasicModelWithoutSpaces) { - std::string model = "../../examples/basic_model_without_spaces.conf"; - std::string policy = "../../examples/basic_policy.csv"; - Enforcer e = Enforcer(model, policy); - - Scope scope = InitializeParams("alice", "data1", "read"); - TestEnforce(e, scope, true); - scope = InitializeParams("alice", "data1", "write"); - TestEnforce(e, scope, false); - scope = InitializeParams("alice", "data2", "read"); - TestEnforce(e, scope, false); - scope = InitializeParams("alice", "data2", "write"); - TestEnforce(e, scope, false); - scope = InitializeParams("bob", "data1", "read"); - TestEnforce(e, scope, false); - scope = InitializeParams("bob", "data1", "write"); - TestEnforce(e, scope, false); - scope = InitializeParams("bob", "data2", "read"); - TestEnforce(e, scope, false); - scope = InitializeParams("bob", "data2", "write"); - TestEnforce(e, scope, true); - } - - TEST_METHOD(TestBasicModelNoPolicy) { - std::string model = "../../examples/basic_model.conf"; - Enforcer e = Enforcer(model); - - Scope scope = InitializeParams("alice", "data1", "read"); - TestEnforce(e, scope, false); - scope = InitializeParams("alice", "data1", "write"); - TestEnforce(e, scope, false); - scope = InitializeParams("alice", "data2", "read"); - TestEnforce(e, scope, false); - scope = InitializeParams("alice", "data2", "write"); - TestEnforce(e, scope, false); - scope = InitializeParams("bob", "data1", "read"); - TestEnforce(e, scope, false); - scope = InitializeParams("bob", "data1", "write"); - TestEnforce(e, scope, false); - scope = InitializeParams("bob", "data2", "read"); - TestEnforce(e, scope, false); - scope = InitializeParams("bob", "data2", "write"); - TestEnforce(e, scope, false); - } - - TEST_METHOD(TestBasicModelWithRoot) { - std::string model = "../../examples/basic_with_root_model.conf"; - std::string policy = "../../examples/basic_policy.csv"; - Enforcer e = Enforcer(model, policy); - - Scope scope = InitializeParams("alice", "data1", "read"); - TestEnforce(e, scope, true); - scope = InitializeParams("alice", "data1", "write"); - TestEnforce(e, scope, false); - scope = InitializeParams("alice", "data2", "read"); - TestEnforce(e, scope, false); - scope = InitializeParams("alice", "data2", "write"); - TestEnforce(e, scope, false); - scope = InitializeParams("bob", "data1", "read"); - TestEnforce(e, scope, false); - scope = InitializeParams("bob", "data1", "write"); - TestEnforce(e, scope, false); - scope = InitializeParams("bob", "data2", "read"); - TestEnforce(e, scope, false); - scope = InitializeParams("bob", "data2", "write"); - TestEnforce(e, scope, true); - scope = InitializeParams("root", "data1", "read"); - TestEnforce(e, scope, true); - scope = InitializeParams("root", "data1", "write"); - TestEnforce(e, scope, true); - scope = InitializeParams("root", "data2", "read"); - TestEnforce(e, scope, true); - scope = InitializeParams("root", "data2", "write"); - TestEnforce(e, scope, true); - } - - TEST_METHOD(TestBasicModelWithRootNoPolicy) { - std::string model = "../../examples/basic_with_root_model.conf"; - Enforcer e = Enforcer(model); - - Scope scope = InitializeParams("alice", "data1", "read"); - TestEnforce(e, scope, false); - scope = InitializeParams("alice", "data1", "write"); - TestEnforce(e, scope, false); - scope = InitializeParams("alice", "data2", "read"); - TestEnforce(e, scope, false); - scope = InitializeParams("alice", "data2", "write"); - TestEnforce(e, scope, false); - scope = InitializeParams("bob", "data1", "read"); - TestEnforce(e, scope, false); - scope = InitializeParams("bob", "data1", "write"); - TestEnforce(e, scope, false); - scope = InitializeParams("bob", "data2", "read"); - TestEnforce(e, scope, false); - scope = InitializeParams("bob", "data2", "write"); - TestEnforce(e, scope, false); - scope = InitializeParams("root", "data1", "read"); - TestEnforce(e, scope, true); - scope = InitializeParams("root", "data1", "write"); - TestEnforce(e, scope, true); - scope = InitializeParams("root", "data2", "read"); - TestEnforce(e, scope, true); - scope = InitializeParams("root", "data2", "write"); - TestEnforce(e, scope, true); - } - - TEST_METHOD(TestBasicModelWithoutUsers) { - std::string model = "../../examples/basic_without_users_model.conf"; - std::string policy = "../../examples/basic_without_users_policy.csv"; - Enforcer e = Enforcer(model, policy); - - Scope scope = InitializeParamsWithoutUsers("data1", "read"); - TestEnforce(e, scope, true); - scope = InitializeParamsWithoutUsers("data1", "write"); - TestEnforce(e, scope, false); - scope = InitializeParamsWithoutUsers("data2", "read"); - TestEnforce(e, scope, false); - scope = InitializeParamsWithoutUsers("data2", "write"); - TestEnforce(e, scope, true); - } - - TEST_METHOD(TestBasicModelWithoutResources) { - std::string model = "../../examples/basic_without_resources_model.conf"; - std::string policy = "../../examples/basic_without_resources_policy.csv"; - Enforcer e = Enforcer(model, policy); - - Scope scope = InitializeParamsWithoutResources("alice", "read"); - TestEnforce(e, scope, true); - scope = InitializeParamsWithoutResources("alice", "write"); - TestEnforce(e, scope, false); - scope = InitializeParamsWithoutResources("bob", "read"); - TestEnforce(e, scope, false); - scope = InitializeParamsWithoutResources("bob", "write"); - TestEnforce(e, scope, true); - } - - TEST_METHOD(TestRBACModel) { - std::string model = "../../examples/rbac_model.conf"; - std::string policy = "../../examples/rbac_policy.csv"; - Enforcer e = Enforcer(model, policy); - - Scope scope = InitializeParams("alice", "data1", "read"); - TestEnforce(e, scope, true); - scope = InitializeParams("alice", "data1", "write"); - TestEnforce(e, scope, false); - scope = InitializeParams("alice", "data2", "read"); - TestEnforce(e, scope, true); - scope = InitializeParams("alice", "data2", "write"); - TestEnforce(e, scope, true); - scope = InitializeParams("bob", "data1", "read"); - TestEnforce(e, scope, false); - scope = InitializeParams("bob", "data1", "write"); - TestEnforce(e, scope, false); - scope = InitializeParams("bob", "data2", "read"); - TestEnforce(e, scope, false); - scope = InitializeParams("bob", "data2", "write"); - TestEnforce(e, scope, true); - } - - TEST_METHOD(TestRBACModelWithResourceRoles) { - std::string model = "../../examples/rbac_with_resource_roles_model.conf"; - std::string policy = "../../examples/rbac_with_resource_roles_policy.csv"; - Enforcer e = Enforcer(model, policy); - - Scope scope = InitializeParams("alice", "data1", "read"); - TestEnforce(e, scope, true); - scope = InitializeParams("alice", "data1", "write"); - TestEnforce(e, scope, true); - scope = InitializeParams("alice", "data2", "read"); - TestEnforce(e, scope, false); - scope = InitializeParams("alice", "data2", "write"); - TestEnforce(e, scope, true); - scope = InitializeParams("bob", "data1", "read"); - TestEnforce(e, scope, false); - scope = InitializeParams("bob", "data1", "write"); - TestEnforce(e, scope, false); - scope = InitializeParams("bob", "data2", "read"); - TestEnforce(e, scope, false); - scope = InitializeParams("bob", "data2", "write"); - TestEnforce(e, scope, true); - } - - TEST_METHOD(TestRBACModelWithDomains) { - std::string model = "../../examples/rbac_with_domains_model.conf"; - std::string policy = "../../examples/rbac_with_domains_policy.csv"; - Enforcer e = Enforcer(model, policy); - - Scope scope = InitializeParamsWithDomains("alice", "domain1", "data1", "read"); - TestEnforce(e, scope, true); - scope = InitializeParamsWithDomains("alice", "domain1", "data1", "write"); - TestEnforce(e, scope, true); - scope = InitializeParamsWithDomains("alice", "domain1", "data2", "read"); - TestEnforce(e, scope, false); - scope = InitializeParamsWithDomains("alice", "domain1", "data2", "write"); - TestEnforce(e, scope, false); - scope = InitializeParamsWithDomains("bob", "domain2", "data1", "read"); - TestEnforce(e, scope, false); - scope = InitializeParamsWithDomains("bob", "domain2", "data1", "write"); - TestEnforce(e, scope, false); - scope = InitializeParamsWithDomains("bob", "domain2", "data2", "read"); - TestEnforce(e, scope, true); - scope = InitializeParamsWithDomains("bob", "domain2", "data2", "write"); - TestEnforce(e, scope, true); - } - - TEST_METHOD(TestRBACModelWithDomainsAtRuntime) { - std::string model = "../../examples/rbac_with_domains_model.conf"; - Enforcer e = Enforcer(model); - - std::vector params{ "admin", "domain1", "data1", "read" }; - e.AddPolicy(params); - params = std::vector{ "admin", "domain1", "data1", "write" }; - e.AddPolicy(params); - params = std::vector{ "admin", "domain2", "data2", "read" }; - e.AddPolicy(params); - params = std::vector{ "admin", "domain2", "data2", "write" }; - e.AddPolicy(params); - - params = std::vector{ "alice", "admin", "domain1" }; - e.AddGroupingPolicy(params); - params = std::vector{ "bob", "admin", "domain2" }; - e.AddGroupingPolicy(params); - - Scope scope = InitializeParamsWithDomains("alice", "domain1", "data1", "read"); - TestEnforce(e, scope, true); - scope = InitializeParamsWithDomains("alice", "domain1", "data1", "write"); - TestEnforce(e, scope, true); - scope = InitializeParamsWithDomains("alice", "domain1", "data2", "read"); - TestEnforce(e, scope, false); - scope = InitializeParamsWithDomains("alice", "domain1", "data2", "write"); - TestEnforce(e, scope, false); - scope = InitializeParamsWithDomains("bob", "domain2", "data1", "read"); - TestEnforce(e, scope, false); - scope = InitializeParamsWithDomains("bob", "domain2", "data1", "write"); - TestEnforce(e, scope, false); - scope = InitializeParamsWithDomains("bob", "domain2", "data2", "read"); - TestEnforce(e, scope, true); - scope = InitializeParamsWithDomains("bob", "domain2", "data2", "write"); - TestEnforce(e, scope, true); - - // Remove all policy rules related to domain1 and data1. - params = std::vector{ "domain1", "data1" }; - e.RemoveFilteredPolicy(1, params); - - scope = InitializeParamsWithDomains("alice", "domain1", "data1", "read"); - TestEnforce(e, scope, false); - scope = InitializeParamsWithDomains("alice", "domain1", "data1", "write"); - TestEnforce(e, scope, false); - scope = InitializeParamsWithDomains("alice", "domain1", "data2", "read"); - TestEnforce(e, scope, false); - scope = InitializeParamsWithDomains("alice", "domain1", "data2", "read"); - TestEnforce(e, scope, false); - scope = InitializeParamsWithDomains("bob", "domain2", "data1", "read"); - TestEnforce(e, scope, false); - scope = InitializeParamsWithDomains("bob", "domain2", "data1", "write"); - TestEnforce(e, scope, false); - scope = InitializeParamsWithDomains("bob", "domain2", "data2", "read"); - TestEnforce(e, scope, true); - scope = InitializeParamsWithDomains("bob", "domain2", "data2", "write"); - TestEnforce(e, scope, true); - - // Remove the specified policy rule. - params = std::vector{ "admin", "domain2", "data2", "read" }; - e.RemovePolicy(params); - - scope = InitializeParamsWithDomains("alice", "domain1", "data1", "read"); - TestEnforce(e, scope, false); - scope = InitializeParamsWithDomains("alice", "domain1", "data1", "write"); - TestEnforce(e, scope, false); - scope = InitializeParamsWithDomains("alice", "domain1", "data2", "read"); - TestEnforce(e, scope, false); - scope = InitializeParamsWithDomains("alice", "domain1", "data2", "write"); - TestEnforce(e, scope, false); - scope = InitializeParamsWithDomains("bob", "domain2", "data1", "read"); - TestEnforce(e, scope, false); - scope = InitializeParamsWithDomains("bob", "domain2", "data1", "write"); - TestEnforce(e, scope, false); - scope = InitializeParamsWithDomains("bob", "domain2", "data2", "read"); - TestEnforce(e, scope, false); - scope = InitializeParamsWithDomains("bob", "domain2", "data2", "write"); - TestEnforce(e, scope, true); - } - - TEST_METHOD(TestRBACModelWithDomainsAtRuntimeMockAdapter) { - std::string model = "../../examples/rbac_with_domains_model.conf"; - std::string policy = "../../examples/rbac_with_domains_policy.csv"; - std::shared_ptr adapter = std::shared_ptr(new FileAdapter(policy)); - Enforcer e = Enforcer(model, adapter); - - std::vector params{ "admin", "domain3", "data1", "read" }; - e.AddPolicy(params); - params = std::vector{ "alice", "admin", "domain3" }; - e.AddGroupingPolicy(params); - - Scope scope = InitializeParamsWithDomains("alice", "domain3", "data1", "read"); - TestEnforce(e, scope, true); - - scope = InitializeParamsWithDomains("alice", "domain1", "data1", "read"); - TestEnforce(e, scope, true); - params = std::vector{ "domain1", "data1" }; - e.RemoveFilteredPolicy(1, params); - scope = InitializeParamsWithDomains("alice", "domain1", "data1", "read"); - TestEnforce(e, scope, false); - - scope = InitializeParamsWithDomains("bob", "domain2", "data2", "read"); - TestEnforce(e, scope, true); - params = std::vector{ "admin", "domain2", "data2", "read" }; - e.RemovePolicy(params); - scope = InitializeParamsWithDomains("bob", "domain2", "data2", "read"); - TestEnforce(e, scope, false); - } - - TEST_METHOD(TestRBACModelWithDeny) { - std::string model = "../../examples/rbac_with_deny_model.conf"; - std::string policy = "../../examples/rbac_with_deny_policy.csv"; - Enforcer e = Enforcer(model, policy); - - Scope scope = InitializeParams("alice", "data1", "read"); - TestEnforce(e, scope, true); - scope = InitializeParams("alice", "data1", "write"); - TestEnforce(e, scope, false); - scope = InitializeParams("alice", "data2", "read"); - TestEnforce(e, scope, true); - scope = InitializeParams("alice", "data2", "write"); - TestEnforce(e, scope, false); - scope = InitializeParams("bob", "data1", "read"); - TestEnforce(e, scope, false); - scope = InitializeParams("bob", "data1", "write"); - TestEnforce(e, scope, false); - scope = InitializeParams("bob", "data2", "read"); - TestEnforce(e, scope, false); - scope = InitializeParams("bob", "data2", "write"); - TestEnforce(e, scope, true); - } - - TEST_METHOD(TestRBACModelWithOnlyDeny) { - std::string model = "../../examples/rbac_with_not_deny_model.conf"; - std::string policy = "../../examples/rbac_with_deny_policy.csv"; - Enforcer e = Enforcer(model, policy); - - Scope scope = InitializeParams("alice", "data2", "write"); - TestEnforce(e, scope, false); - } - - TEST_METHOD(TestRBACModelWithCustomData) { - std::string model = "../../examples/rbac_model.conf"; - std::string policy = "../../examples/rbac_policy.csv"; - Enforcer e = Enforcer(model, policy); - - // You can add custom data to a grouping policy, Casbin will ignore it. It is only meaningful to the caller. - // This feature can be used to store information like whether "bob" is an end user (so no subject will inherit "bob") - // For Casbin, it is equivalent to: e.AddGroupingPolicy("bob", "data2_admin") - std::vector params{ "bob", "data2_admin", "custom_data" }; - e.AddGroupingPolicy(params); - - Scope scope = InitializeParams("alice", "data1", "read"); - TestEnforce(e, scope, true); - scope = InitializeParams("alice", "data1", "write"); - TestEnforce(e, scope, false); - scope = InitializeParams("alice", "data2", "read"); - TestEnforce(e, scope, true); - scope = InitializeParams("alice", "data2", "write"); - TestEnforce(e, scope, true); - scope = InitializeParams("bob", "data1", "read"); - TestEnforce(e, scope, false); - scope = InitializeParams("bob", "data1", "write"); - TestEnforce(e, scope, false); - scope = InitializeParams("bob", "data2", "read"); - TestEnforce(e, scope, true); - scope = InitializeParams("bob", "data2", "write"); - TestEnforce(e, scope, true); - - // You should also take the custom data as a parameter when deleting a grouping policy. - // e.RemoveGroupingPolicy("bob", "data2_admin") won't work. - // Or you can remove it by using RemoveFilteredGroupingPolicy(). - params = std::vector{ "bob", "data2_admin", "custom_data" }; - e.RemoveGroupingPolicy(params); - - scope = InitializeParams("alice", "data1", "read"); - TestEnforce(e, scope, true); - scope = InitializeParams("alice", "data1", "write"); - TestEnforce(e, scope, false); - scope = InitializeParams("alice", "data2", "read"); - TestEnforce(e, scope, true); - scope = InitializeParams("alice", "data2", "write"); - TestEnforce(e, scope, true); - scope = InitializeParams("bob", "data1", "read"); - TestEnforce(e, scope, false); - scope = InitializeParams("bob", "data1", "write"); - TestEnforce(e, scope, false); - scope = InitializeParams("bob", "data2", "read"); - TestEnforce(e, scope, false); - scope = InitializeParams("bob", "data2", "write"); - TestEnforce(e, scope, true); - } - - TEST_METHOD(TestRBACModelWithPattern) { - std::string model = "../../examples/rbac_with_pattern_model.conf"; - std::string policy = "../../examples/rbac_with_pattern_policy.csv"; - Enforcer e = Enforcer(model, policy); - - // Here's a little confusing: the matching function here is not the custom function used in matcher. - // It is the matching function used by "g" (and "g2", "g3" if any..) - // You can see in policy that: "g2, /book/:id, book_group", so in "g2()" function in the matcher, instead - // of checking whether "/book/:id" equals the obj: "/book/1", it checks whether the pattern matches. - // You can see it as normal RBAC: "/book/:id" == "/book/1" becomes KeyMatch2("/book/:id", "/book/1") - DefaultRoleManager* rm_tmp = (DefaultRoleManager*)e.rm.get(); - rm_tmp->AddMatchingFunc(KeyMatch2); - Scope scope = InitializeParams("alice", "/book/1", "GET"); - TestEnforce(e, scope, true); - scope = InitializeParams("alice", "/book/2", "GET"); - TestEnforce(e, scope, true); - scope = InitializeParams("alice", "/pen/1", "GET"); - TestEnforce(e, scope, true); - scope = InitializeParams("alice", "/pen/2", "GET"); - TestEnforce(e, scope, false); - scope = InitializeParams("bob", "/book/1", "GET"); - TestEnforce(e, scope, false); - scope = InitializeParams("bob", "/book/2", "GET"); - TestEnforce(e, scope, false); - scope = InitializeParams("bob", "/pen/1", "GET"); - TestEnforce(e, scope, true); - scope = InitializeParams("bob", "/pen/2", "GET"); - TestEnforce(e, scope, true); - - // AddMatchingFunc() is actually setting a function because only one function is allowed, - // so when we set "KeyMatch3", we are actually replacing "KeyMatch2" with "KeyMatch3". - rm_tmp->AddMatchingFunc(KeyMatch3); - scope = InitializeParams("alice", "/book2/1", "GET"); - TestEnforce(e, scope, true); - scope = InitializeParams("alice", "/book2/2", "GET"); - TestEnforce(e, scope, true); - scope = InitializeParams("alice", "/pen2/1", "GET"); - TestEnforce(e, scope, true); - scope = InitializeParams("alice", "/pen2/2", "GET"); - TestEnforce(e, scope, false); - scope = InitializeParams("bob", "/book2/1", "GET"); - TestEnforce(e, scope, false); - scope = InitializeParams("bob", "/book2/2", "GET"); - TestEnforce(e, scope, false); - scope = InitializeParams("bob", "/pen2/1", "GET"); - TestEnforce(e, scope, true); - scope = InitializeParams("bob", "/pen2/2", "GET"); - TestEnforce(e, scope, true); - } - /* - type testCustomRoleManager struct {} - - func NewRoleManager() rbac.RoleManager{ - return &testCustomRoleManager{} - } - func(rm* testCustomRoleManager) Clear() error { return nil } - func(rm* testCustomRoleManager) AddLink(name1 string, name2 string, domain ...string) error { - return nil - } - func(rm* testCustomRoleManager) DeleteLink(name1 string, name2 string, domain ...string) error { - return nil - } - func(rm* testCustomRoleManager) HasLink(name1 string, name2 string, domain ...string) (bool, error) { - if name1 == "alice" && name2 == "alice" { - return true, nil - } - else if name1 == "alice" && name2 == "data2_admin" { - return true, nil - } - else if name1 == "bob" && name2 == "bob" { - return true, nil - } - return false, nil - } - func(rm* testCustomRoleManager) GetRoles(name string, domain ...string) ([]string, error) { - return[]string{}, nil - } - func(rm* testCustomRoleManager) GetUsers(name string, domain ...string) ([]string, error) { - return[]string{}, nil - } - func(rm* testCustomRoleManager) PrintRoles() error { return nil } - - func TestRBACModelWithCustomRoleManager(t* testing.T) { - e, _ : = NewEnforcer("examples/rbac_model.conf", "examples/rbac_policy.csv") - e.SetRoleManager(NewRoleManager()) - e.LoadModel() - _ = e.LoadPolicy() - - TestEnforce(e, "alice", "data1", "read", true) - TestEnforce(e, "alice", "data1", "write", false) - TestEnforce(e, "alice", "data2", "read", true) - TestEnforce(e, "alice", "data2", "write", true) - TestEnforce(e, "bob", "data1", "read", false) - TestEnforce(e, "bob", "data1", "write", false) - TestEnforce(e, "bob", "data2", "read", false) - TestEnforce(e, "bob", "data2", "write", true) - } - - type testResource struct { - Name string - Owner string - } - - func newTestResource(name string, owner string) testResource { - r: = testResource{} - r.Name = name - r.Owner = owner - return r - } - - func TestABACModel(t* testing.T) { - e, _ : = NewEnforcer("examples/abac_model.conf") - - data1 : = newTestResource("data1", "alice") - data2 : = newTestResource("data2", "bob") - - TestEnforce(e, "alice", data1, "read", true) - TestEnforce(e, "alice", data1, "write", true) - TestEnforce(e, "alice", data2, "read", false) - TestEnforce(e, "alice", data2, "write", false) - TestEnforce(e, "bob", data1, "read", false) - TestEnforce(e, "bob", data1, "write", false) - TestEnforce(e, "bob", data2, "read", true) - TestEnforce(e, "bob", data2, "write", true) - } - - func TestKeyMatchModel(t* testing.T) { - e, _ : = NewEnforcer("examples/keymatch_model.conf", "examples/keymatch_policy.csv") - - TestEnforce(e, "alice", "/alice_data/resource1", "GET", true) - TestEnforce(e, "alice", "/alice_data/resource1", "POST", true) - TestEnforce(e, "alice", "/alice_data/resource2", "GET", true) - TestEnforce(e, "alice", "/alice_data/resource2", "POST", false) - TestEnforce(e, "alice", "/bob_data/resource1", "GET", false) - TestEnforce(e, "alice", "/bob_data/resource1", "POST", false) - TestEnforce(e, "alice", "/bob_data/resource2", "GET", false) - TestEnforce(e, "alice", "/bob_data/resource2", "POST", false) - - TestEnforce(e, "bob", "/alice_data/resource1", "GET", false) - TestEnforce(e, "bob", "/alice_data/resource1", "POST", false) - TestEnforce(e, "bob", "/alice_data/resource2", "GET", true) - TestEnforce(e, "bob", "/alice_data/resource2", "POST", false) - TestEnforce(e, "bob", "/bob_data/resource1", "GET", false) - TestEnforce(e, "bob", "/bob_data/resource1", "POST", true) - TestEnforce(e, "bob", "/bob_data/resource2", "GET", false) - TestEnforce(e, "bob", "/bob_data/resource2", "POST", true) - - TestEnforce(e, "cathy", "/cathy_data", "GET", true) - TestEnforce(e, "cathy", "/cathy_data", "POST", true) - TestEnforce(e, "cathy", "/cathy_data", "DELETE", false) - } - - func TestKeyMatch2Model(t* testing.T) { - e, _ : = NewEnforcer("examples/keymatch2_model.conf", "examples/keymatch2_policy.csv") - - TestEnforce(e, "alice", "/alice_data", "GET", false) - TestEnforce(e, "alice", "/alice_data/resource1", "GET", true) - TestEnforce(e, "alice", "/alice_data2/myid", "GET", false) - TestEnforce(e, "alice", "/alice_data2/myid/using/res_id", "GET", true) - } - - func CustomFunction(key1 string, key2 string) bool{ - if key1 == "/alice_data2/myid/using/res_id" && key2 == "/alice_data/:resource" { - return true - } - else if key1 == "/alice_data2/myid/using/res_id" && key2 == "/alice_data2/:id/using/:resId" { - return true - } - else { - return false - } - } - - func CustomFunctionWrapper(args ...interface {}) (interface {}, error) { - key1: = args[0].(std::string) - key2 : = args[1].(std::string) - - return bool(CustomFunction(key1, key2)), nil - } - - func TestKeyMatchCustomModel(t* testing.T) { - e, _ : = NewEnforcer("examples/keymatch_custom_model.conf", "examples/keymatch2_policy.csv") - - e.AddFunction("keyMatchCustom", CustomFunctionWrapper) - - TestEnforce(e, "alice", "/alice_data2/myid", "GET", false) - TestEnforce(e, "alice", "/alice_data2/myid/using/res_id", "GET", true) - } - - func TestIPMatchModel(t* testing.T) { - e, _ : = NewEnforcer("examples/ipmatch_model.conf", "examples/ipmatch_policy.csv") - - TestEnforce(e, "192.168.2.123", "data1", "read", true) - TestEnforce(e, "192.168.2.123", "data1", "write", false) - TestEnforce(e, "192.168.2.123", "data2", "read", false) - TestEnforce(e, "192.168.2.123", "data2", "write", false) - - TestEnforce(e, "192.168.0.123", "data1", "read", false) - TestEnforce(e, "192.168.0.123", "data1", "write", false) - TestEnforce(e, "192.168.0.123", "data2", "read", false) - TestEnforce(e, "192.168.0.123", "data2", "write", false) - - TestEnforce(e, "10.0.0.5", "data1", "read", false) - TestEnforce(e, "10.0.0.5", "data1", "write", false) - TestEnforce(e, "10.0.0.5", "data2", "read", false) - TestEnforce(e, "10.0.0.5", "data2", "write", true) - - TestEnforce(e, "192.168.0.1", "data1", "read", false) - TestEnforce(e, "192.168.0.1", "data1", "write", false) - TestEnforce(e, "192.168.0.1", "data2", "read", false) - TestEnforce(e, "192.168.0.1", "data2", "write", false) - } - - func TestGlobMatchModel(t* testing.T) { - e, _ : = NewEnforcer("examples/glob_model.conf", "examples/glob_policy.csv") - TestEnforce(e, "u1", "/foo/", "read", true) - TestEnforce(e, "u1", "/foo", "read", false) - TestEnforce(e, "u1", "/foo/subprefix", "read", true) - TestEnforce(e, "u1", "foo", "read", false) - - TestEnforce(e, "u2", "/foosubprefix", "read", true) - TestEnforce(e, "u2", "/foo/subprefix", "read", false) - TestEnforce(e, "u2", "foo", "read", false) - - TestEnforce(e, "u3", "/prefix/foo/subprefix", "read", true) - TestEnforce(e, "u3", "/prefix/foo/", "read", true) - TestEnforce(e, "u3", "/prefix/foo", "read", false) - - TestEnforce(e, "u4", "/foo", "read", false) - TestEnforce(e, "u4", "foo", "read", true) - } - - func TestPriorityModel(t* testing.T) { - e, _ : = NewEnforcer("examples/priority_model.conf", "examples/priority_policy.csv") - - TestEnforce(e, "alice", "data1", "read", true) - TestEnforce(e, "alice", "data1", "write", false) - TestEnforce(e, "alice", "data2", "read", false) - TestEnforce(e, "alice", "data2", "write", false) - TestEnforce(e, "bob", "data1", "read", false) - TestEnforce(e, "bob", "data1", "write", false) - TestEnforce(e, "bob", "data2", "read", true) - TestEnforce(e, "bob", "data2", "write", false) - } - - func TestPriorityModelIndeterminate(t* testing.T) { - e, _ : = NewEnforcer("examples/priority_model.conf", "examples/priority_indeterminate_policy.csv") - - TestEnforce(e, "alice", "data1", "read", false) - } - - func TestRBACModelInMultiLines(t* testing.T) { - e, _ : = NewEnforcer("examples/rbac_model_in_multi_line.conf", "examples/rbac_policy.csv") - - TestEnforce(e, "alice", "data1", "read", true) - TestEnforce(e, "alice", "data1", "write", false) - TestEnforce(e, "alice", "data2", "read", true) - TestEnforce(e, "alice", "data2", "write", true) - TestEnforce(e, "bob", "data1", "read", false) - TestEnforce(e, "bob", "data1", "write", false) - TestEnforce(e, "bob", "data2", "read", false) - TestEnforce(e, "bob", "data2", "write", true) - } - - type testSub struct { - Name string - Age int - } - - func newTestSubject(name string, age int) testSub { - s: = testSub{} - s.Name = name - s.Age = age - return s - } - - func TestABACPolicy(t* testing.T) { - e, _ : = NewEnforcer("examples/abac_rule_model.conf", "examples/abac_rule_policy.csv") - sub1 : = newTestSubject("alice", 16) - sub2 : = newTestSubject("alice", 20) - sub3 : = newTestSubject("alice", 65) - - TestEnforce(e, sub1, "/data1", "read", false) - TestEnforce(e, sub1, "/data2", "read", false) - TestEnforce(e, sub1, "/data1", "write", false) - TestEnforce(e, sub1, "/data2", "write", true) - TestEnforce(e, sub2, "/data1", "read", true) - TestEnforce(e, sub2, "/data2", "read", false) - TestEnforce(e, sub2, "/data1", "write", false) - TestEnforce(e, sub2, "/data2", "write", true) - TestEnforce(e, sub3, "/data1", "read", true) - TestEnforce(e, sub3, "/data2", "read", false) - TestEnforce(e, sub3, "/data1", "write", false) - TestEnforce(e, sub3, "/data2", "write", false) - } - - func TestCommentModel(t* testing.T) { - e, _ : = NewEnforcer("examples/comment_model.conf", "examples/basic_policy.csv") - - TestEnforce(e, "alice", "data1", "read", true) - TestEnforce(e, "alice", "data1", "write", false) - TestEnforce(e, "alice", "data2", "read", false) - TestEnforce(e, "alice", "data2", "write", false) - TestEnforce(e, "bob", "data1", "read", false) - TestEnforce(e, "bob", "data1", "write", false) - TestEnforce(e, "bob", "data2", "read", false) - TestEnforce(e, "bob", "data2", "write", true) - } - */ - }; -} - -#endif // TEST_MODEL_ENFORCER_CPP diff --git a/test/test_rbac_api.cpp b/test/test_rbac_api.cpp deleted file mode 100644 index 1c1a9ce2..00000000 --- a/test/test_rbac_api.cpp +++ /dev/null @@ -1,231 +0,0 @@ -#include "pch.h" - -#ifndef TEST_RBAC_API_CPP -#define TEST_RBAC_API_CPP - - -#include -#include -#include - -namespace test_rbac_api -{ - using namespace casbin; - - TEST_CLASS(TestRBACAPI) - { - public: - - TEST_METHOD(TestRoleAPI) { - Enforcer e = Enforcer("../../examples/rbac_model.conf", "../../examples/rbac_policy.csv"); - - Assert::IsTrue(ArrayEquals(std::vector{ "data2_admin" }, e.GetRolesForUser("alice"))); - Assert::IsTrue(ArrayEquals(std::vector{ }, e.GetRolesForUser("bob"))); - Assert::IsTrue(ArrayEquals(std::vector{ }, e.GetRolesForUser("data2_admin"))); - Assert::IsTrue(ArrayEquals(std::vector{ }, e.GetRolesForUser("non_exist"))); - - Assert::IsFalse(e.HasRoleForUser("alice", "data1_admin")); - Assert::IsTrue(e.HasRoleForUser("alice", "data2_admin")); - - e.AddRoleForUser("alice", "data1_admin"); - - Assert::IsTrue(ArrayEquals(std::vector{ "data1_admin", "data2_admin" }, e.GetRolesForUser("alice"))); - Assert::IsTrue(ArrayEquals(std::vector{ }, e.GetRolesForUser("bob"))); - Assert::IsTrue(ArrayEquals(std::vector{ }, e.GetRolesForUser("data2_admin"))); - - e.DeleteRoleForUser("alice", "data1_admin"); - - Assert::IsTrue(ArrayEquals(std::vector{ "data2_admin" }, e.GetRolesForUser("alice"))); - Assert::IsTrue(ArrayEquals(std::vector{ }, e.GetRolesForUser("bob"))); - Assert::IsTrue(ArrayEquals(std::vector{ }, e.GetRolesForUser("data2_admin"))); - - e.DeleteRolesForUser("alice"); - - Assert::IsTrue(ArrayEquals(std::vector{ }, e.GetRolesForUser("alice"))); - Assert::IsTrue(ArrayEquals(std::vector{ }, e.GetRolesForUser("bob"))); - Assert::IsTrue(ArrayEquals(std::vector{ }, e.GetRolesForUser("data2_admin"))); - - e.AddRoleForUser("alice", "data1_admin"); - e.DeleteUser("alice"); - - Assert::IsTrue(ArrayEquals(std::vector{ }, e.GetRolesForUser("alice"))); - Assert::IsTrue(ArrayEquals(std::vector{ }, e.GetRolesForUser("bob"))); - Assert::IsTrue(ArrayEquals(std::vector{ }, e.GetRolesForUser("data2_admin"))); - - e.AddRoleForUser("alice", "data2_admin"); - - Assert::IsFalse(e.Enforce({ "alice", "data1", "read" })); - Assert::IsFalse(e.Enforce({ "alice", "data1", "write" })); - Assert::IsTrue(e.Enforce({ "alice", "data2", "read" })); - Assert::IsTrue(e.Enforce({ "alice", "data2", "write" })); - Assert::IsFalse(e.Enforce({ "bob", "data1", "read" })); - Assert::IsFalse(e.Enforce({ "bob", "data1", "write" })); - Assert::IsFalse(e.Enforce({ "bob", "data2", "read" })); - Assert::IsTrue(e.Enforce({ "bob", "data2", "write" })); - - e.DeleteRole("data2_admin"); - - Assert::IsFalse(e.Enforce({ "alice", "data1", "read" })); - Assert::IsFalse(e.Enforce({ "alice", "data1", "write" })); - Assert::IsFalse(e.Enforce({ "alice", "data2", "read" })); - Assert::IsFalse(e.Enforce({ "alice", "data2", "write" })); - Assert::IsFalse(e.Enforce({ "bob", "data1", "read" })); - Assert::IsFalse(e.Enforce({ "bob", "data1", "write" })); - Assert::IsFalse(e.Enforce({ "bob", "data2", "read" })); - Assert::IsTrue(e.Enforce({ "bob", "data2", "write" })); - } - - TEST_METHOD(TestEnforcer_AddRolesForUser) { - Enforcer e = Enforcer("../../examples/rbac_model.conf", "../../examples/rbac_policy.csv"); - - e.AddRolesForUser("alice", std::vector{ "data1_admin", "data2_admin", "data3_admin" }); - Assert::IsTrue(ArrayEquals(std::vector{ "data1_admin", "data2_admin", "data3_admin" }, e.GetRolesForUser("alice"))); - - Assert::IsTrue(e.Enforce({ "alice", "data1", "read" })); - Assert::IsTrue(e.Enforce({ "alice", "data2", "read" })); - Assert::IsTrue(e.Enforce({ "alice", "data2", "write" })); - } - - void TestGetPermissions(Enforcer e, std::string name, std::vector> res) { - std::vector> my_res = e.GetPermissionsForUser(name); - - int count = 0; - for (int i = 0; i < my_res.size(); i++) { - for (int j = 0; j < res.size(); j++) { - if (ArrayEquals(res[j], my_res[i])) { - count += 1; - break; - } - } - } - - Assert::AreEqual(int(res.size()), count); - } - - TEST_METHOD(TestPermissionAPI) { - Enforcer e = Enforcer("../../examples/basic_without_resources_model.conf", "../../examples/basic_without_resources_policy.csv"); - - Assert::IsTrue(e.Enforce(std::vector{ "alice", "read" })); - Assert::IsFalse(e.Enforce(std::vector{ "alice", "write" })); - Assert::IsFalse(e.Enforce(std::vector{ "bob", "read" })); - Assert::IsTrue(e.Enforce(std::vector{ "bob", "write" })); - - TestGetPermissions(e, "alice", std::vector>{ {"alice", "read"} }); - TestGetPermissions(e, "bob", std::vector>{ {"bob", "write"} }); - - Assert::IsTrue(e.HasPermissionForUser("alice", { "read" })); - Assert::IsFalse(e.HasPermissionForUser("alice", { "write" })); - Assert::IsFalse(e.HasPermissionForUser("bob", { "read" })); - Assert::IsTrue(e.HasPermissionForUser("bob", { "write" })); - - e.DeletePermission({ "read" }); - - Assert::IsFalse(e.Enforce(std::vector{ "alice", "read" })); - Assert::IsFalse(e.Enforce(std::vector{ "alice", "write" })); - Assert::IsFalse(e.Enforce(std::vector{ "bob", "read" })); - Assert::IsTrue(e.Enforce(std::vector{ "bob", "write" })); - - e.AddPermissionForUser("bob", { "read" }); - - Assert::IsFalse(e.Enforce(std::vector{ "alice", "read" })); - Assert::IsFalse(e.Enforce(std::vector{ "alice", "write" })); - Assert::IsTrue(e.Enforce(std::vector{ "bob", "read" })); - Assert::IsTrue(e.Enforce(std::vector{ "bob", "write" })); - - e.DeletePermissionForUser("bob", { "read" }); - - Assert::IsFalse(e.Enforce(std::vector{ "alice", "read" })); - Assert::IsFalse(e.Enforce(std::vector{ "alice", "write" })); - Assert::IsFalse(e.Enforce(std::vector{ "bob", "read" })); - Assert::IsTrue(e.Enforce(std::vector{ "bob", "write" })); - - e.DeletePermissionsForUser("bob"); - - Assert::IsFalse(e.Enforce(std::vector{ "alice", "read" })); - Assert::IsFalse(e.Enforce(std::vector{ "alice", "write" })); - Assert::IsFalse(e.Enforce(std::vector{ "bob", "read" })); - Assert::IsFalse(e.Enforce(std::vector{ "bob", "write" })); - } - - TEST_METHOD(TestImplicitRoleAPI) { - Enforcer e = Enforcer("../../examples/rbac_model.conf", "../../examples/rbac_with_hierarchy_policy.csv"); - - TestGetPermissions(e, "alice", std::vector>{ {"alice", "data1", "read"} }); - TestGetPermissions(e, "bob", std::vector>{ {"bob", "data2", "write"} }); - - Assert::IsTrue(ArrayEquals(std::vector{ "admin", "data1_admin", "data2_admin" }, e.GetImplicitRolesForUser("alice"))); - Assert::IsTrue(ArrayEquals(std::vector{ }, e.GetImplicitRolesForUser("bob"))); - - e = Enforcer("../../examples/rbac_with_pattern_model.conf", "../../examples/rbac_with_pattern_policy.csv"); - - dynamic_cast(e.GetRoleManager().get())->AddMatchingFunc(KeyMatch); - - Assert::IsTrue(ArrayEquals(std::vector{ "/book/1/2/3/4/5", "pen_admin", "/book/*", "book_group" }, e.GetImplicitRolesForUser("cathy"))); - Assert::IsTrue(ArrayEquals(std::vector{ "/book/1/2/3/4/5", "pen_admin" }, e.GetRolesForUser("cathy"))); - } - - void TestGetImplicitPermissions(Enforcer e, std::string name, std::vector> res) { - std::vector> my_res = e.GetImplicitPermissionsForUser(name); - - int count = 0; - for (int i = 0; i < my_res.size(); i++) { - for (int j = 0; j < res.size(); j++) { - if (ArrayEquals(res[j], my_res[i])) { - count += 1; - break; - } - } - } - - Assert::AreEqual(int(res.size()), count); - } - - void TestGetImplicitPermissionsWithDomain(Enforcer e, std::string name, std::string domain, std::vector> res) { - std::vector> my_res = e.GetImplicitPermissionsForUser(name, { domain }); - - int count = 0; - for (int i = 0; i < my_res.size(); i++) { - for (int j = 0; j < res.size(); j++) { - if (ArrayEquals(res[j], my_res[i])) { - count += 1; - break; - } - } - } - - Assert::AreEqual(int(res.size()), count); - } - - TEST_METHOD(TestImplicitPermissionAPI) { - Enforcer e = Enforcer("../../examples/rbac_model.conf", "../../examples/rbac_with_hierarchy_policy.csv"); - - TestGetPermissions(e, "alice", std::vector>{ {"alice", "data1", "read"} }); - TestGetPermissions(e, "bob", std::vector>{ {"bob", "data2", "write"} }); - - TestGetImplicitPermissions(e, "alice", std::vector>{ {"alice", "data1", "read"}, { "data1_admin", "data1", "read" }, { "data1_admin", "data1", "write" }, { "data2_admin", "data2", "read" }, { "data2_admin", "data2", "write" } }); - TestGetImplicitPermissions(e, "bob", std::vector>{ {"bob", "data2", "write"} }); - } - - TEST_METHOD(TestImplicitPermissionAPIWithDomain) { - Enforcer e = Enforcer("../../examples/rbac_with_domains_model.conf", "../../examples/rbac_with_hierarchy_with_domains_policy.csv"); - TestGetImplicitPermissionsWithDomain(e, "alice", "domain1", std::vector>{ {"alice", "domain1", "data2", "read"}, { "role:reader", "domain1", "data1", "read" }, { "role:writer", "domain1", "data1", "write" } }); - } - - TEST_METHOD(TestImplicitUserAPI) { - Enforcer e = Enforcer("../../examples/rbac_model.conf", "../../examples/rbac_with_hierarchy_policy.csv"); - - Assert::IsTrue(ArrayEquals(std::vector{ "alice" }, e.GetImplicitUsersForPermission({ "data1", "read" }))); - Assert::IsTrue(ArrayEquals(std::vector{ "alice" }, e.GetImplicitUsersForPermission({ "data1", "write" }))); - Assert::IsTrue(ArrayEquals(std::vector{ "alice" }, e.GetImplicitUsersForPermission({ "data2", "read" }))); - Assert::IsTrue(ArrayEquals(std::vector{ "alice", "bob" }, e.GetImplicitUsersForPermission({ "data2", "write" }))); - - e.ClearPolicy(); - e.AddPolicy({ "admin", "data1", "read" }); - e.AddPolicy({ "bob", "data1", "read" }); - e.AddGroupingPolicy({ "alice", "admin" }); - Assert::IsTrue(ArrayEquals(std::vector{ "alice", "bob" }, e.GetImplicitUsersForPermission({ "data1", "read" }))); - } - }; -} - -#endif // TEST_RBAC_API_CPP diff --git a/test/test_rbac_api_with_domains.cpp b/test/test_rbac_api_with_domains.cpp deleted file mode 100644 index 83dc148c..00000000 --- a/test/test_rbac_api_with_domains.cpp +++ /dev/null @@ -1,189 +0,0 @@ -#include "pch.h" - -#ifndef TEST_RBAC_API_WITH_DOMAINS_CPP -#define TEST_RBAC_API_WITH_DOMAINS_CPP - - -#include -#include -#include -#include - -namespace test_rbac_api_with_domains -{ - using namespace casbin; - - TEST_CLASS(TestRBACAPIWithDomains) - { - public: - - TEST_METHOD(TestGetImplicitRolesForDomainUser) { - Enforcer e = Enforcer("../../examples/rbac_with_domains_model.conf", "../../examples/rbac_with_hierarchy_with_domains_policy.csv"); - - // This is only able to retrieve the first level of roles. - Assert::IsTrue(ArrayEquals({ "role:global_admin" }, e.GetRolesForUserInDomain("alice", { "domain1" }))); - - // Retrieve all inherit roles. It supports domains as well. - Assert::IsTrue(ArrayEquals(std::vector{"role:global_admin", "role:reader", "role:writer"}, e.GetImplicitRolesForUser("alice", {"domain1"}))); - } - - // TestUserAPIWithDomains: Add by Gordon - TEST_METHOD(TestUserAPIWithDomains) { - Enforcer e = Enforcer("../../examples/rbac_with_domains_model.conf", "../../examples/rbac_with_domains_policy.csv"); - - Assert::IsTrue(ArrayEquals({ "alice" }, e.GetUsersForRole("admin", { "domain1" }))); - Assert::IsTrue(ArrayEquals({ "alice" }, e.GetUsersForRoleInDomain("admin", { "domain1" }))); - - try { - e.GetUsersForRole("non_exist", { "domain1" }); - } - catch (CasbinRBACException e) { - Assert::IsTrue(true); - } - try { - e.GetUsersForRoleInDomain("non_exist", { "domain1" }); - } - catch (CasbinRBACException e) { - Assert::IsTrue(true); - } - - Assert::IsTrue(ArrayEquals({ "bob" }, e.GetUsersForRole("admin", { "domain2" }))); - Assert::IsTrue(ArrayEquals({ "bob" }, e.GetUsersForRoleInDomain("admin", { "domain2" }))); - - try { - e.GetUsersForRole("non_exist", { "domain2" }); - } - catch (CasbinRBACException e) { - Assert::IsTrue(true); - } - try { - e.GetUsersForRoleInDomain("non_exist", { "domain2" }); - } - catch (CasbinRBACException e) { - Assert::IsTrue(true); - } - - e.DeleteRoleForUserInDomain("alice", "admin", "domain1"); - e.AddRoleForUserInDomain("bob", "admin", "domain1"); - - Assert::IsTrue(ArrayEquals({ "bob" }, e.GetUsersForRole("admin", { "domain1" }))); - Assert::IsTrue(ArrayEquals({ "bob" }, e.GetUsersForRoleInDomain("admin", { "domain1" }))); - - try { - e.GetUsersForRole("non_exist", { "domain1" }); - } - catch (CasbinRBACException e) { - Assert::IsTrue(true); - } - try { - e.GetUsersForRoleInDomain("non_exist", { "domain1" }); - } - catch (CasbinRBACException e) { - Assert::IsTrue(true); - } - - Assert::IsTrue(ArrayEquals({ "bob" }, e.GetUsersForRole("admin", { "domain2" }))); - Assert::IsTrue(ArrayEquals({ "bob" }, e.GetUsersForRoleInDomain("admin", { "domain2" }))); - - try { - e.GetUsersForRole("non_exist", { "domain2" }); - } - catch (CasbinRBACException e) { - Assert::IsTrue(true); - } - try { - e.GetUsersForRoleInDomain("non_exist", { "domain2" }); - } - catch (CasbinRBACException e) { - Assert::IsTrue(true); - } - } - - TEST_METHOD(TestRoleAPIWithDomains) { - Enforcer e = Enforcer("../../examples/rbac_with_domains_model.conf", "../../examples/rbac_with_domains_policy.csv"); - - Assert::IsTrue(ArrayEquals({ "admin" }, e.GetRolesForUser("alice", { "domain1" }))); - Assert::IsTrue(ArrayEquals({ "admin" }, e.GetRolesForUserInDomain("alice", { "domain1" }))); - - Assert::IsTrue(ArrayEquals({ }, e.GetRolesForUser("bob", { "domain1" }))); - Assert::IsTrue(ArrayEquals({ }, e.GetRolesForUserInDomain("bob", { "domain1" }))); - - Assert::IsTrue(ArrayEquals({ }, e.GetRolesForUser("admin", { "domain1" }))); - Assert::IsTrue(ArrayEquals({ }, e.GetRolesForUserInDomain("admin", { "domain1" }))); - - Assert::IsTrue(ArrayEquals({ }, e.GetRolesForUser("non_exist", { "domain1" }))); - Assert::IsTrue(ArrayEquals({ }, e.GetRolesForUserInDomain("non_exist", { "domain1" }))); - - Assert::IsTrue(ArrayEquals({ }, e.GetRolesForUser("alice", { "domain2" }))); - Assert::IsTrue(ArrayEquals({ }, e.GetRolesForUserInDomain("alice", { "domain2" }))); - - Assert::IsTrue(ArrayEquals({ "admin" }, e.GetRolesForUser("bob", { "domain2" }))); - Assert::IsTrue(ArrayEquals({ "admin" }, e.GetRolesForUserInDomain("bob", { "domain2" }))); - - Assert::IsTrue(ArrayEquals({ }, e.GetRolesForUser("admin", { "domain2" }))); - Assert::IsTrue(ArrayEquals({ }, e.GetRolesForUserInDomain("admin", { "domain2" }))); - - Assert::IsTrue(ArrayEquals({ }, e.GetRolesForUser("non_exist", { "domain2" }))); - Assert::IsTrue(ArrayEquals({ }, e.GetRolesForUserInDomain("non_exist", { "domain2" }))); - - e.DeleteRoleForUserInDomain("alice", "admin", "domain1"); - e.AddRoleForUserInDomain("bob", "admin", "domain1"); - - Assert::IsTrue(ArrayEquals({ }, e.GetRolesForUser("alice", { "domain1" }))); - Assert::IsTrue(ArrayEquals({ }, e.GetRolesForUserInDomain("alice", { "domain1" }))); - - Assert::IsTrue(ArrayEquals({ "admin" }, e.GetRolesForUser("bob", { "domain1" }))); - Assert::IsTrue(ArrayEquals({ "admin" }, e.GetRolesForUserInDomain("bob", { "domain1" }))); - - Assert::IsTrue(ArrayEquals({ }, e.GetRolesForUser("admin", { "domain1" }))); - Assert::IsTrue(ArrayEquals({ }, e.GetRolesForUserInDomain("admin", { "domain1" }))); - - Assert::IsTrue(ArrayEquals({ }, e.GetRolesForUser("non_exist", { "domain1" }))); - Assert::IsTrue(ArrayEquals({ }, e.GetRolesForUserInDomain("non_exist", { "domain1" }))); - - Assert::IsTrue(ArrayEquals({ }, e.GetRolesForUser("alice", { "domain2" }))); - Assert::IsTrue(ArrayEquals({ }, e.GetRolesForUserInDomain("alice", { "domain2" }))); - - Assert::IsTrue(ArrayEquals({ "admin" }, e.GetRolesForUser("bob", { "domain2" }))); - Assert::IsTrue(ArrayEquals({ "admin" }, e.GetRolesForUserInDomain("bob", { "domain2" }))); - - Assert::IsTrue(ArrayEquals({ }, e.GetRolesForUser("admin", { "domain2" }))); - Assert::IsTrue(ArrayEquals({ }, e.GetRolesForUserInDomain("admin", { "domain2" }))); - - Assert::IsTrue(ArrayEquals({ }, e.GetRolesForUser("non_exist", { "domain2" }))); - Assert::IsTrue(ArrayEquals({ }, e.GetRolesForUserInDomain("non_exist", { "domain2" }))); - } - - void TestGetPermissionsInDomain(Enforcer e, std::string name, std::string domain, std::vector> res) { - std::vector> my_res = e.GetPermissionsForUserInDomain(name, { domain }); - - int count = 0; - for (int i = 0; i < my_res.size(); i++) { - for (int j = 0; j < res.size(); j++) { - if (ArrayEquals(res[j], my_res[i])) { - count += 1; - break; - } - } - } - - Assert::AreEqual(int(res.size()), count); - } - - TEST_METHOD(TestPermissionAPIInDomain) { - Enforcer e = Enforcer("../../examples/rbac_with_domains_model.conf", "../../examples/rbac_with_domains_policy.csv"); - - TestGetPermissionsInDomain(e, "alice", "domain1", {}); - TestGetPermissionsInDomain(e, "bob", "domain1", {}); - TestGetPermissionsInDomain(e, "admin", "domain1", { {"admin", "domain1", "data1", "read"}, {"admin", "domain1", "data1", "write"} }); - TestGetPermissionsInDomain(e, "non_exist", "domain1", {}); - - TestGetPermissionsInDomain(e, "alice", "domain2", {}); - TestGetPermissionsInDomain(e, "bob", "domain2", {}); - TestGetPermissionsInDomain(e, "admin", "domain2", { {"admin", "domain2", "data2", "read"}, {"admin", "domain2", "data2", "write"} }); - TestGetPermissionsInDomain(e, "non_exist", "domain2", {}); - } - }; -} - -#endif // TEST_RBAC_API_WITH_DOMAINS_CPP diff --git a/test/test_role_manager.cpp b/test/test_role_manager.cpp deleted file mode 100644 index 520f3579..00000000 --- a/test/test_role_manager.cpp +++ /dev/null @@ -1,188 +0,0 @@ -#include "pch.h" - -#ifndef TEST_ROLE_MANAGER_CPP -#define TEST_ROLE_MANAGER_CPP - - -#include - -namespace test_role_manager -{ - using namespace casbin; - - TEST_CLASS(TestRoleManager) - { - public: - - void TestRole(DefaultRoleManager rm, std::string name1, std::string name2, bool res) { - bool my_res = rm.HasLink(name1, name2); - - Assert::AreEqual(res, my_res); - } - - void TestDomainRole(DefaultRoleManager rm, std::string name1, std::string name2, std::vector domain, bool res) { - bool my_res = rm.HasLink(name1, name2, domain); - - Assert::AreEqual(res, my_res); - } - - TEST_METHOD(TestRole) { - DefaultRoleManager rm = DefaultRoleManager(3); - rm.AddLink("u1", "g1"); - rm.AddLink("u2", "g1"); - rm.AddLink("u3", "g2"); - rm.AddLink("u4", "g2"); - rm.AddLink("u4", "g3"); - rm.AddLink("g1", "g3"); - - // Current role inheritance tree: - // g3 g2 - // / \ / \ - // g1 u4 u3 - // / \ - // u1 u2 - - TestRole(rm, "u1", "g1", true); - TestRole(rm, "u1", "g2", false); - TestRole(rm, "u1", "g3", true); - TestRole(rm, "u2", "g1", true); - TestRole(rm, "u2", "g2", false); - TestRole(rm, "u2", "g3", true); - TestRole(rm, "u3", "g1", false); - TestRole(rm, "u3", "g2", true); - TestRole(rm, "u3", "g3", false); - TestRole(rm, "u4", "g1", false); - TestRole(rm, "u4", "g2", true); - TestRole(rm, "u4", "g3", true); - - rm.DeleteLink("g1", "g3"); - rm.DeleteLink("u4", "g2"); - - // Current role inheritance tree after deleting the links: - // g3 g2 - // \ \ - // g1 u4 u3 - // / \ - // u1 u2 - - TestRole(rm, "u1", "g1", true); - TestRole(rm, "u1", "g2", false); - TestRole(rm, "u1", "g3", false); - TestRole(rm, "u2", "g1", true); - TestRole(rm, "u2", "g2", false); - TestRole(rm, "u2", "g3", false); - TestRole(rm, "u3", "g1", false); - TestRole(rm, "u3", "g2", true); - TestRole(rm, "u3", "g3", false); - TestRole(rm, "u4", "g1", false); - TestRole(rm, "u4", "g2", false); - TestRole(rm, "u4", "g3", true); - } - - TEST_METHOD(TestDomainRole) { - DefaultRoleManager rm = DefaultRoleManager(3); - std::vector domain1{ "domain1" }; - std::vector domain2{ "domain2" }; - rm.AddLink("u1", "g1", domain1); - rm.AddLink("u2", "g1", domain1); - rm.AddLink("u3", "admin", domain2); - rm.AddLink("u4", "admin", domain2); - rm.AddLink("u4", "admin", domain1); - rm.AddLink("g1", "admin", domain1); - - // Current role inheritance tree: - // domain1:admin domain2:admin - // / \ / \ - // domain1:g1 u4 u3 - // / \ - // u1 u2 - - TestDomainRole(rm, "u1", "g1", domain1, true); - TestDomainRole(rm, "u1", "g1", domain2, false); - TestDomainRole(rm, "u1", "admin", domain1, true); - TestDomainRole(rm, "u1", "admin", domain2, false); - - TestDomainRole(rm, "u2", "g1", domain1, true); - TestDomainRole(rm, "u2", "g1", domain2, false); - TestDomainRole(rm, "u2", "admin", domain1, true); - TestDomainRole(rm, "u2", "admin", domain2, false); - - TestDomainRole(rm, "u3", "g1", domain1, false); - TestDomainRole(rm, "u3", "g1", domain2, false); - TestDomainRole(rm, "u3", "admin", domain1, false); - TestDomainRole(rm, "u3", "admin", domain2, true); - - TestDomainRole(rm, "u4", "g1", domain1, false); - TestDomainRole(rm, "u4", "g1", domain2, false); - TestDomainRole(rm, "u4", "admin", domain1, true); - TestDomainRole(rm, "u4", "admin", domain2, true); - - rm.DeleteLink("g1", "admin", domain1); - rm.DeleteLink("u4", "admin", domain2); - - // Current role inheritance tree after deleting the links: - // domain1:admin domain2:admin - // \ \ - // domain1:g1 u4 u3 - // / \ - // u1 u2 - - TestDomainRole(rm, "u1", "g1", domain1, true); - TestDomainRole(rm, "u1", "g1", domain2, false); - TestDomainRole(rm, "u1", "admin", domain1, false); - TestDomainRole(rm, "u1", "admin", domain2, false); - - TestDomainRole(rm, "u2", "g1", domain1, true); - TestDomainRole(rm, "u2", "g1", domain2, false); - TestDomainRole(rm, "u2", "admin", domain1, false); - TestDomainRole(rm, "u2", "admin", domain2, false); - - TestDomainRole(rm, "u3", "g1", domain1, false); - TestDomainRole(rm, "u3", "g1", domain2, false); - TestDomainRole(rm, "u3", "admin", domain1, false); - TestDomainRole(rm, "u3", "admin", domain2, true); - - TestDomainRole(rm, "u4", "g1", domain1, false); - TestDomainRole(rm, "u4", "g1", domain2, false); - TestDomainRole(rm, "u4", "admin", domain1, true); - TestDomainRole(rm, "u4", "admin", domain2, false); - } - - TEST_METHOD(TestClear) { - DefaultRoleManager rm = DefaultRoleManager(3); - rm.AddLink("u1", "g1"); - rm.AddLink("u2", "g1"); - rm.AddLink("u3", "g2"); - rm.AddLink("u4", "g2"); - rm.AddLink("u4", "g3"); - rm.AddLink("g1", "g3"); - - // Current role inheritance tree: - // g3 g2 - // / \ / \ - // g1 u4 u3 - // / \ - // u1 u2 - - rm.Clear(); - - // All data is cleared. - // No role inheritance now. - - TestRole(rm, "u1", "g1", false); - TestRole(rm, "u1", "g2", false); - TestRole(rm, "u1", "g3", false); - TestRole(rm, "u2", "g1", false); - TestRole(rm, "u2", "g2", false); - TestRole(rm, "u2", "g3", false); - TestRole(rm, "u3", "g1", false); - TestRole(rm, "u3", "g2", false); - TestRole(rm, "u3", "g3", false); - TestRole(rm, "u4", "g1", false); - TestRole(rm, "u4", "g2", false); - TestRole(rm, "u4", "g3", false); - } - }; -} - -#endif // TEST_ROLE_MANAGER_CPP diff --git a/test/test_util.cpp b/test/test_util.cpp deleted file mode 100644 index 2329f379..00000000 --- a/test/test_util.cpp +++ /dev/null @@ -1,65 +0,0 @@ -#include "pch.h" - -#ifndef TEST_UTIL_CPP -#define TEST_UTIL_CPP - - -#include - -namespace test_util -{ - using namespace casbin; - - TEST_CLASS(TestModel) - { - public: - - void TestEscapeAssertion(std::string s, std::string res){ - std::string my_res = EscapeAssertion(s); - Assert::AreEqual(my_res, res); - } - - TEST_METHOD(TestEscapeAssertion) { - TestEscapeAssertion("r.attr.value == p.attr", "r_attr.value == p_attr"); - TestEscapeAssertion("r.attp.value || p.attr", "r_attp.value || p_attr"); - TestEscapeAssertion("r.attp.value &&p.attr", "r_attp.value &&p_attr"); - TestEscapeAssertion("r.attp.value >p.attr", "r_attp.value >p_attr"); - TestEscapeAssertion("r.attp.value a, std::vector b, bool res) { - bool my_res = ArrayEquals(a, b); - Assert::AreEqual(my_res, res); - } - - TEST_METHOD(TestArrayEquals) { - TestArrayEquals(std::vector{"a", "b", "c"}, std::vector{"a", "b", "c"}, true); - TestArrayEquals(std::vector{"a", "b", "c"}, std::vector{"a", "b"}, false); - TestArrayEquals(std::vector{"a", "b", "c"}, std::vector{"a", "c", "b"}, true); - TestArrayEquals(std::vector{"a", "b", "c"}, std::vector{}, false); - } - }; -} - -#endif // TEST_UTIL_CPP diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index d5aed1d6..4152e30b 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -22,6 +22,11 @@ add_executable( enforcer_cached_test.cpp enforcer_synced_test.cpp management_api_test.cpp + model_enforcer_test.cpp + model_test.cpp + rbac_api_with_domains_test.cpp + rbac_api_test.cpp + role_manager_test.cpp util_test.cpp ) diff --git a/tests/model_enforcer_test.cpp b/tests/model_enforcer_test.cpp new file mode 100644 index 00000000..04f4787e --- /dev/null +++ b/tests/model_enforcer_test.cpp @@ -0,0 +1,759 @@ +/* +* Copyright 2020 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. +* +* This is a test file for testing built in functions in casbin +*/ + +#include +#include + +namespace { + +casbin::Scope InitializeParams(const std::string& sub, const std::string& obj, const std::string& act){ + casbin::Scope scope = casbin::InitializeScope(); + casbin::PushObject(scope, "r"); + casbin::PushStringPropToObject(scope, "r", sub, "sub"); + casbin::PushStringPropToObject(scope, "r", obj, "obj"); + casbin::PushStringPropToObject(scope, "r", act, "act"); + + return scope; +} + +casbin::Scope InitializeParamsWithoutUsers(const std::string& obj, const std::string& act) { + casbin::Scope scope = casbin::InitializeScope(); + casbin::PushObject(scope, "r"); + casbin::PushStringPropToObject(scope, "r", obj, "obj"); + casbin::PushStringPropToObject(scope, "r", act, "act"); + return scope; +} + +casbin::Scope InitializeParamsWithoutResources(const std::string& sub, const std::string& act) { + casbin::Scope scope = casbin::InitializeScope(); + casbin::PushObject(scope, "r"); + casbin::PushStringPropToObject(scope, "r", sub, "sub"); + casbin::PushStringPropToObject(scope, "r", act, "act"); + + return scope; +} + +casbin::Scope InitializeParamsWithDomains(const std::string& sub, const std::string& domain, const std::string& obj, const std::string& act) { + casbin::Scope scope = casbin::InitializeScope(); + casbin::PushObject(scope, "r"); + casbin::PushStringPropToObject(scope, "r", sub, "sub"); + casbin::PushStringPropToObject(scope, "r", domain, "dom"); + casbin::PushStringPropToObject(scope, "r", obj, "obj"); + casbin::PushStringPropToObject(scope, "r", act, "act"); + return scope; +} + +void TestEnforce(casbin::Enforcer& e, casbin::Scope& scope, bool res) { + ASSERT_EQ(res, e.Enforce(scope)); +} + +TEST(TestModelEnforcer, TestBasicModel) { + std::string model = "../../examples/basic_model.conf"; + std::string policy = "../../examples/basic_policy.csv"; + casbin::Enforcer e(model, policy); + + casbin::Scope scope; + + scope = InitializeParams("alice", "data1", "read"); + TestEnforce(e, scope, true); + scope = InitializeParams("alice", "data1", "write"); + TestEnforce(e, scope, false); + scope = InitializeParams("alice", "data2", "read"); + TestEnforce(e, scope, false); + scope = InitializeParams("alice", "data2", "write"); + TestEnforce(e, scope, false); + scope = InitializeParams("bob", "data1", "read"); + TestEnforce(e, scope, false); + scope = InitializeParams("bob", "data1", "write"); + TestEnforce(e, scope, false); + scope = InitializeParams("bob", "data2", "read"); + TestEnforce(e, scope, false); + scope = InitializeParams("bob", "data2", "write"); + TestEnforce(e, scope, true); +} + +TEST(TestModelEnforcer, TestBasicModelWithoutSpaces) { + std::string model = "../../examples/basic_model_without_spaces.conf"; + std::string policy = "../../examples/basic_policy.csv"; + casbin::Enforcer e(model, policy); + + casbin::Scope scope = InitializeParams("alice", "data1", "read"); + TestEnforce(e, scope, true); + scope = InitializeParams("alice", "data1", "write"); + TestEnforce(e, scope, false); + scope = InitializeParams("alice", "data2", "read"); + TestEnforce(e, scope, false); + scope = InitializeParams("alice", "data2", "write"); + TestEnforce(e, scope, false); + scope = InitializeParams("bob", "data1", "read"); + TestEnforce(e, scope, false); + scope = InitializeParams("bob", "data1", "write"); + TestEnforce(e, scope, false); + scope = InitializeParams("bob", "data2", "read"); + TestEnforce(e, scope, false); + scope = InitializeParams("bob", "data2", "write"); + TestEnforce(e, scope, true); +} + +TEST(TestModelEnforcer, TestBasicModelNoPolicy) { + std::string model = "../../examples/basic_model.conf"; + casbin::Enforcer e(model); + + casbin::Scope scope = InitializeParams("alice", "data1", "read"); + TestEnforce(e, scope, false); + scope = InitializeParams("alice", "data1", "write"); + TestEnforce(e, scope, false); + scope = InitializeParams("alice", "data2", "read"); + TestEnforce(e, scope, false); + scope = InitializeParams("alice", "data2", "write"); + TestEnforce(e, scope, false); + scope = InitializeParams("bob", "data1", "read"); + TestEnforce(e, scope, false); + scope = InitializeParams("bob", "data1", "write"); + TestEnforce(e, scope, false); + scope = InitializeParams("bob", "data2", "read"); + TestEnforce(e, scope, false); + scope = InitializeParams("bob", "data2", "write"); + TestEnforce(e, scope, false); +} + +TEST(TestModelEnforcer, TestBasicModelWithRoot) { + std::string model = "../../examples/basic_with_root_model.conf"; + std::string policy = "../../examples/basic_policy.csv"; + casbin::Enforcer e(model, policy); + + casbin::Scope scope = InitializeParams("alice", "data1", "read"); + TestEnforce(e, scope, true); + scope = InitializeParams("alice", "data1", "write"); + TestEnforce(e, scope, false); + scope = InitializeParams("alice", "data2", "read"); + TestEnforce(e, scope, false); + scope = InitializeParams("alice", "data2", "write"); + TestEnforce(e, scope, false); + scope = InitializeParams("bob", "data1", "read"); + TestEnforce(e, scope, false); + scope = InitializeParams("bob", "data1", "write"); + TestEnforce(e, scope, false); + scope = InitializeParams("bob", "data2", "read"); + TestEnforce(e, scope, false); + scope = InitializeParams("bob", "data2", "write"); + TestEnforce(e, scope, true); + scope = InitializeParams("root", "data1", "read"); + TestEnforce(e, scope, true); + scope = InitializeParams("root", "data1", "write"); + TestEnforce(e, scope, true); + scope = InitializeParams("root", "data2", "read"); + TestEnforce(e, scope, true); + scope = InitializeParams("root", "data2", "write"); + TestEnforce(e, scope, true); +} + +TEST(TestModelEnforcer, TestBasicModelWithRootNoPolicy) { + std::string model = "../../examples/basic_with_root_model.conf"; + casbin::Enforcer e(model); + + casbin::Scope scope = InitializeParams("alice", "data1", "read"); + TestEnforce(e, scope, false); + scope = InitializeParams("alice", "data1", "write"); + TestEnforce(e, scope, false); + scope = InitializeParams("alice", "data2", "read"); + TestEnforce(e, scope, false); + scope = InitializeParams("alice", "data2", "write"); + TestEnforce(e, scope, false); + scope = InitializeParams("bob", "data1", "read"); + TestEnforce(e, scope, false); + scope = InitializeParams("bob", "data1", "write"); + TestEnforce(e, scope, false); + scope = InitializeParams("bob", "data2", "read"); + TestEnforce(e, scope, false); + scope = InitializeParams("bob", "data2", "write"); + TestEnforce(e, scope, false); + scope = InitializeParams("root", "data1", "read"); + TestEnforce(e, scope, true); + scope = InitializeParams("root", "data1", "write"); + TestEnforce(e, scope, true); + scope = InitializeParams("root", "data2", "read"); + TestEnforce(e, scope, true); + scope = InitializeParams("root", "data2", "write"); + TestEnforce(e, scope, true); +} + +TEST(TestModelEnforcer, TestBasicModelWithoutUsers) { + std::string model = "../../examples/basic_without_users_model.conf"; + std::string policy = "../../examples/basic_without_users_policy.csv"; + casbin::Enforcer e(model, policy); + + casbin::Scope scope = InitializeParamsWithoutUsers("data1", "read"); + TestEnforce(e, scope, true); + scope = InitializeParamsWithoutUsers("data1", "write"); + TestEnforce(e, scope, false); + scope = InitializeParamsWithoutUsers("data2", "read"); + TestEnforce(e, scope, false); + scope = InitializeParamsWithoutUsers("data2", "write"); + TestEnforce(e, scope, true); +} + +TEST(TestModelEnforcer, TestBasicModelWithoutResources) { + std::string model = "../../examples/basic_without_resources_model.conf"; + std::string policy = "../../examples/basic_without_resources_policy.csv"; + casbin::Enforcer e(model, policy); + + casbin::Scope scope = InitializeParamsWithoutResources("alice", "read"); + TestEnforce(e, scope, true); + scope = InitializeParamsWithoutResources("alice", "write"); + TestEnforce(e, scope, false); + scope = InitializeParamsWithoutResources("bob", "read"); + TestEnforce(e, scope, false); + scope = InitializeParamsWithoutResources("bob", "write"); + TestEnforce(e, scope, true); +} + +TEST(TestModelEnforcer, TestRBACModel) { + std::string model = "../../examples/rbac_model.conf"; + std::string policy = "../../examples/rbac_policy.csv"; + casbin::Enforcer e(model, policy); + + casbin::Scope scope = InitializeParams("alice", "data1", "read"); + TestEnforce(e, scope, true); + scope = InitializeParams("alice", "data1", "write"); + TestEnforce(e, scope, false); + scope = InitializeParams("alice", "data2", "read"); + TestEnforce(e, scope, true); + scope = InitializeParams("alice", "data2", "write"); + TestEnforce(e, scope, true); + scope = InitializeParams("bob", "data1", "read"); + TestEnforce(e, scope, false); + scope = InitializeParams("bob", "data1", "write"); + TestEnforce(e, scope, false); + scope = InitializeParams("bob", "data2", "read"); + TestEnforce(e, scope, false); + scope = InitializeParams("bob", "data2", "write"); + TestEnforce(e, scope, true); +} + +TEST(TestModelEnforcer, TestRBACModelWithResourceRoles) { + std::string model = "../../examples/rbac_with_resource_roles_model.conf"; + std::string policy = "../../examples/rbac_with_resource_roles_policy.csv"; + casbin::Enforcer e(model, policy); + + casbin::Scope scope = InitializeParams("alice", "data1", "read"); + TestEnforce(e, scope, true); + scope = InitializeParams("alice", "data1", "write"); + TestEnforce(e, scope, true); + scope = InitializeParams("alice", "data2", "read"); + TestEnforce(e, scope, false); + scope = InitializeParams("alice", "data2", "write"); + TestEnforce(e, scope, true); + scope = InitializeParams("bob", "data1", "read"); + TestEnforce(e, scope, false); + scope = InitializeParams("bob", "data1", "write"); + TestEnforce(e, scope, false); + scope = InitializeParams("bob", "data2", "read"); + TestEnforce(e, scope, false); + scope = InitializeParams("bob", "data2", "write"); + TestEnforce(e, scope, true); +} + +TEST(TestModelEnforcer, TestRBACModelWithDomains) { + std::string model = "../../examples/rbac_with_domains_model.conf"; + std::string policy = "../../examples/rbac_with_domains_policy.csv"; + casbin::Enforcer e(model, policy); + + casbin::Scope scope = InitializeParamsWithDomains("alice", "domain1", "data1", "read"); + TestEnforce(e, scope, true); + scope = InitializeParamsWithDomains("alice", "domain1", "data1", "write"); + TestEnforce(e, scope, true); + scope = InitializeParamsWithDomains("alice", "domain1", "data2", "read"); + TestEnforce(e, scope, false); + scope = InitializeParamsWithDomains("alice", "domain1", "data2", "write"); + TestEnforce(e, scope, false); + scope = InitializeParamsWithDomains("bob", "domain2", "data1", "read"); + TestEnforce(e, scope, false); + scope = InitializeParamsWithDomains("bob", "domain2", "data1", "write"); + TestEnforce(e, scope, false); + scope = InitializeParamsWithDomains("bob", "domain2", "data2", "read"); + TestEnforce(e, scope, true); + scope = InitializeParamsWithDomains("bob", "domain2", "data2", "write"); + TestEnforce(e, scope, true); +} + +TEST(TestModelEnforcer, TestRBACModelWithDomainsAtRuntime) { + std::string model = "../../examples/rbac_with_domains_model.conf"; + casbin::Enforcer e(model); + + std::vector params{ "admin", "domain1", "data1", "read" }; + e.AddPolicy(params); + params = std::vector{ "admin", "domain1", "data1", "write" }; + e.AddPolicy(params); + params = std::vector{ "admin", "domain2", "data2", "read" }; + e.AddPolicy(params); + params = std::vector{ "admin", "domain2", "data2", "write" }; + e.AddPolicy(params); + + params = std::vector{ "alice", "admin", "domain1" }; + e.AddGroupingPolicy(params); + params = std::vector{ "bob", "admin", "domain2" }; + e.AddGroupingPolicy(params); + + casbin::Scope scope = InitializeParamsWithDomains("alice", "domain1", "data1", "read"); + TestEnforce(e, scope, true); + scope = InitializeParamsWithDomains("alice", "domain1", "data1", "write"); + TestEnforce(e, scope, true); + scope = InitializeParamsWithDomains("alice", "domain1", "data2", "read"); + TestEnforce(e, scope, false); + scope = InitializeParamsWithDomains("alice", "domain1", "data2", "write"); + TestEnforce(e, scope, false); + scope = InitializeParamsWithDomains("bob", "domain2", "data1", "read"); + TestEnforce(e, scope, false); + scope = InitializeParamsWithDomains("bob", "domain2", "data1", "write"); + TestEnforce(e, scope, false); + scope = InitializeParamsWithDomains("bob", "domain2", "data2", "read"); + TestEnforce(e, scope, true); + scope = InitializeParamsWithDomains("bob", "domain2", "data2", "write"); + TestEnforce(e, scope, true); + + // Remove all policy rules related to domain1 and data1. + params = std::vector{ "domain1", "data1" }; + e.RemoveFilteredPolicy(1, params); + + scope = InitializeParamsWithDomains("alice", "domain1", "data1", "read"); + TestEnforce(e, scope, false); + scope = InitializeParamsWithDomains("alice", "domain1", "data1", "write"); + TestEnforce(e, scope, false); + scope = InitializeParamsWithDomains("alice", "domain1", "data2", "read"); + TestEnforce(e, scope, false); + scope = InitializeParamsWithDomains("alice", "domain1", "data2", "read"); + TestEnforce(e, scope, false); + scope = InitializeParamsWithDomains("bob", "domain2", "data1", "read"); + TestEnforce(e, scope, false); + scope = InitializeParamsWithDomains("bob", "domain2", "data1", "write"); + TestEnforce(e, scope, false); + scope = InitializeParamsWithDomains("bob", "domain2", "data2", "read"); + TestEnforce(e, scope, true); + scope = InitializeParamsWithDomains("bob", "domain2", "data2", "write"); + TestEnforce(e, scope, true); + + // Remove the specified policy rule. + params = std::vector{ "admin", "domain2", "data2", "read" }; + e.RemovePolicy(params); + + scope = InitializeParamsWithDomains("alice", "domain1", "data1", "read"); + TestEnforce(e, scope, false); + scope = InitializeParamsWithDomains("alice", "domain1", "data1", "write"); + TestEnforce(e, scope, false); + scope = InitializeParamsWithDomains("alice", "domain1", "data2", "read"); + TestEnforce(e, scope, false); + scope = InitializeParamsWithDomains("alice", "domain1", "data2", "write"); + TestEnforce(e, scope, false); + scope = InitializeParamsWithDomains("bob", "domain2", "data1", "read"); + TestEnforce(e, scope, false); + scope = InitializeParamsWithDomains("bob", "domain2", "data1", "write"); + TestEnforce(e, scope, false); + scope = InitializeParamsWithDomains("bob", "domain2", "data2", "read"); + TestEnforce(e, scope, false); + scope = InitializeParamsWithDomains("bob", "domain2", "data2", "write"); + TestEnforce(e, scope, true); +} + +TEST(TestModelEnforcer, TestRBACModelWithDomainsAtRuntimeMockAdapter) { + std::string model = "../../examples/rbac_with_domains_model.conf"; + std::string policy = "../../examples/rbac_with_domains_policy.csv"; + std::shared_ptr adapter = std::make_shared(policy); + casbin::Enforcer e(model, adapter); + + std::vector params{ "admin", "domain3", "data1", "read" }; + e.AddPolicy(params); + params = std::vector{ "alice", "admin", "domain3" }; + e.AddGroupingPolicy(params); + + casbin::Scope scope = InitializeParamsWithDomains("alice", "domain3", "data1", "read"); + TestEnforce(e, scope, true); + + scope = InitializeParamsWithDomains("alice", "domain1", "data1", "read"); + TestEnforce(e, scope, true); + params = std::vector{ "domain1", "data1" }; + e.RemoveFilteredPolicy(1, params); + scope = InitializeParamsWithDomains("alice", "domain1", "data1", "read"); + TestEnforce(e, scope, false); + + scope = InitializeParamsWithDomains("bob", "domain2", "data2", "read"); + TestEnforce(e, scope, true); + params = std::vector{ "admin", "domain2", "data2", "read" }; + e.RemovePolicy(params); + scope = InitializeParamsWithDomains("bob", "domain2", "data2", "read"); + TestEnforce(e, scope, false); +} + +TEST(TestModelEnforcer, TestRBACModelWithDeny) { + std::string model = "../../examples/rbac_with_deny_model.conf"; + std::string policy = "../../examples/rbac_with_deny_policy.csv"; + casbin::Enforcer e(model, policy); + + casbin::Scope scope = InitializeParams("alice", "data1", "read"); + TestEnforce(e, scope, true); + scope = InitializeParams("alice", "data1", "write"); + TestEnforce(e, scope, false); + scope = InitializeParams("alice", "data2", "read"); + TestEnforce(e, scope, true); + scope = InitializeParams("alice", "data2", "write"); + TestEnforce(e, scope, false); + scope = InitializeParams("bob", "data1", "read"); + TestEnforce(e, scope, false); + scope = InitializeParams("bob", "data1", "write"); + TestEnforce(e, scope, false); + scope = InitializeParams("bob", "data2", "read"); + TestEnforce(e, scope, false); + scope = InitializeParams("bob", "data2", "write"); + TestEnforce(e, scope, true); +} + +TEST(TestModelEnforcer, TestRBACModelWithOnlyDeny) { + std::string model = "../../examples/rbac_with_not_deny_model.conf"; + std::string policy = "../../examples/rbac_with_deny_policy.csv"; + casbin::Enforcer e(model, policy); + + casbin::Scope scope = InitializeParams("alice", "data2", "write"); + TestEnforce(e, scope, false); +} + +TEST(TestModelEnforcer, TestRBACModelWithCustomData) { + std::string model = "../../examples/rbac_model.conf"; + std::string policy = "../../examples/rbac_policy.csv"; + casbin::Enforcer e(model, policy); + + // You can add custom data to a grouping policy, Casbin will ignore it. It is only meaningful to the caller. + // This feature can be used to store information like whether "bob" is an end user (so no subject will inherit "bob") + // For Casbin, it is equivalent to: e.AddGroupingPolicy("bob", "data2_admin") + std::vector params{ "bob", "data2_admin", "custom_data" }; + e.AddGroupingPolicy(params); + + casbin::Scope scope = InitializeParams("alice", "data1", "read"); + TestEnforce(e, scope, true); + scope = InitializeParams("alice", "data1", "write"); + TestEnforce(e, scope, false); + scope = InitializeParams("alice", "data2", "read"); + TestEnforce(e, scope, true); + scope = InitializeParams("alice", "data2", "write"); + TestEnforce(e, scope, true); + scope = InitializeParams("bob", "data1", "read"); + TestEnforce(e, scope, false); + scope = InitializeParams("bob", "data1", "write"); + TestEnforce(e, scope, false); + scope = InitializeParams("bob", "data2", "read"); + TestEnforce(e, scope, true); + scope = InitializeParams("bob", "data2", "write"); + TestEnforce(e, scope, true); + + // You should also take the custom data as a parameter when deleting a grouping policy. + // e.RemoveGroupingPolicy("bob", "data2_admin") won't work. + // Or you can remove it by using RemoveFilteredGroupingPolicy(). + params = std::vector{ "bob", "data2_admin", "custom_data" }; + e.RemoveGroupingPolicy(params); + + scope = InitializeParams("alice", "data1", "read"); + TestEnforce(e, scope, true); + scope = InitializeParams("alice", "data1", "write"); + TestEnforce(e, scope, false); + scope = InitializeParams("alice", "data2", "read"); + TestEnforce(e, scope, true); + scope = InitializeParams("alice", "data2", "write"); + TestEnforce(e, scope, true); + scope = InitializeParams("bob", "data1", "read"); + TestEnforce(e, scope, false); + scope = InitializeParams("bob", "data1", "write"); + TestEnforce(e, scope, false); + scope = InitializeParams("bob", "data2", "read"); + TestEnforce(e, scope, false); + scope = InitializeParams("bob", "data2", "write"); + TestEnforce(e, scope, true); +} + +TEST(TestModelEnforcer, TestRBACModelWithPattern) { + std::string model = "../../examples/rbac_with_pattern_model.conf"; + std::string policy = "../../examples/rbac_with_pattern_policy.csv"; + casbin::Enforcer e(model, policy); + + // Here's a little confusing: the matching function here is not the custom function used in matcher. + // It is the matching function used by "g" (and "g2", "g3" if any..) + // You can see in policy that: "g2, /book/:id, book_group", so in "g2()" function in the matcher, instead + // of checking whether "/book/:id" equals the obj: "/book/1", it checks whether the pattern matches. + // You can see it as normal RBAC: "/book/:id" == "/book/1" becomes KeyMatch2("/book/:id", "/book/1") + casbin::DefaultRoleManager* rm_tmp = dynamic_cast(e.rm.get()); + rm_tmp->AddMatchingFunc(casbin::KeyMatch2); + casbin::Scope scope = InitializeParams("alice", "/book/1", "GET"); + TestEnforce(e, scope, true); + scope = InitializeParams("alice", "/book/2", "GET"); + TestEnforce(e, scope, true); + scope = InitializeParams("alice", "/pen/1", "GET"); + TestEnforce(e, scope, true); + scope = InitializeParams("alice", "/pen/2", "GET"); + TestEnforce(e, scope, false); + scope = InitializeParams("bob", "/book/1", "GET"); + TestEnforce(e, scope, false); + scope = InitializeParams("bob", "/book/2", "GET"); + TestEnforce(e, scope, false); + scope = InitializeParams("bob", "/pen/1", "GET"); + TestEnforce(e, scope, true); + scope = InitializeParams("bob", "/pen/2", "GET"); + TestEnforce(e, scope, true); + + // AddMatchingFunc() is actually setting a function because only one function is allowed, + // so when we set "KeyMatch3", we are actually replacing "KeyMatch2" with "KeyMatch3". + rm_tmp->AddMatchingFunc(casbin::KeyMatch3); + scope = InitializeParams("alice", "/book2/1", "GET"); + TestEnforce(e, scope, true); + scope = InitializeParams("alice", "/book2/2", "GET"); + TestEnforce(e, scope, true); + scope = InitializeParams("alice", "/pen2/1", "GET"); + TestEnforce(e, scope, true); + scope = InitializeParams("alice", "/pen2/2", "GET"); + TestEnforce(e, scope, false); + scope = InitializeParams("bob", "/book2/1", "GET"); + TestEnforce(e, scope, false); + scope = InitializeParams("bob", "/book2/2", "GET"); + TestEnforce(e, scope, false); + scope = InitializeParams("bob", "/pen2/1", "GET"); + TestEnforce(e, scope, true); + scope = InitializeParams("bob", "/pen2/2", "GET"); + TestEnforce(e, scope, true); +} +/* +type testCustomRoleManager struct {} + +func NewRoleManager() rbac.RoleManager{ + return &testCustomRoleManager{} +} +func(rm* testCustomRoleManager) Clear() error { return nil } +func(rm* testCustomRoleManager) AddLink(name1 string, name2 string, domain ...string) error { + return nil +} +func(rm* testCustomRoleManager) DeleteLink(name1 string, name2 string, domain ...string) error { + return nil +} +func(rm* testCustomRoleManager) HasLink(name1 string, name2 string, domain ...string) (bool, error) { + if name1 == "alice" && name2 == "alice" { + return true, nil + } + else if name1 == "alice" && name2 == "data2_admin" { + return true, nil + } + else if name1 == "bob" && name2 == "bob" { + return true, nil + } + return false, nil +} +func(rm* testCustomRoleManager) GetRoles(name string, domain ...string) ([]string, error) { + return[]string{}, nil +} +func(rm* testCustomRoleManager) GetUsers(name string, domain ...string) ([]string, error) { + return[]string{}, nil +} +func(rm* testCustomRoleManager) PrintRoles() error { return nil } +func TestRBACModelWithCustomRoleManager(t* testing.T) { + e, _ : = NewEnforcer("examples/rbac_model.conf", "examples/rbac_policy.csv") + e.SetRoleManager(NewRoleManager()) + e.LoadModel() + _ = e.LoadPolicy() + + TestEnforce(e, "alice", "data1", "read", true) + TestEnforce(e, "alice", "data1", "write", false) + TestEnforce(e, "alice", "data2", "read", true) + TestEnforce(e, "alice", "data2", "write", true) + TestEnforce(e, "bob", "data1", "read", false) + TestEnforce(e, "bob", "data1", "write", false) + TestEnforce(e, "bob", "data2", "read", false) + TestEnforce(e, "bob", "data2", "write", true) +} + +type testResource struct { + Name string + Owner string +} + +func newTestResource(name string, owner string) testResource { +r: = testResource{} + r.Name = name + r.Owner = owner + return r +} +func TestABACModel(t* testing.T) { + e, _ : = NewEnforcer("examples/abac_model.conf") + data1 : = newTestResource("data1", "alice") + data2 : = newTestResource("data2", "bob") + TestEnforce(e, "alice", data1, "read", true) + TestEnforce(e, "alice", data1, "write", true) + TestEnforce(e, "alice", data2, "read", false) + TestEnforce(e, "alice", data2, "write", false) + TestEnforce(e, "bob", data1, "read", false) + TestEnforce(e, "bob", data1, "write", false) + TestEnforce(e, "bob", data2, "read", true) + TestEnforce(e, "bob", data2, "write", true) +} +func TestKeyMatchModel(t* testing.T) { + e, _ : = NewEnforcer("examples/keymatch_model.conf", "examples/keymatch_policy.csv") + TestEnforce(e, "alice", "/alice_data/resource1", "GET", true) + TestEnforce(e, "alice", "/alice_data/resource1", "POST", true) + TestEnforce(e, "alice", "/alice_data/resource2", "GET", true) + TestEnforce(e, "alice", "/alice_data/resource2", "POST", false) + TestEnforce(e, "alice", "/bob_data/resource1", "GET", false) + TestEnforce(e, "alice", "/bob_data/resource1", "POST", false) + TestEnforce(e, "alice", "/bob_data/resource2", "GET", false) + TestEnforce(e, "alice", "/bob_data/resource2", "POST", false) + TestEnforce(e, "bob", "/alice_data/resource1", "GET", false) + TestEnforce(e, "bob", "/alice_data/resource1", "POST", false) + TestEnforce(e, "bob", "/alice_data/resource2", "GET", true) + TestEnforce(e, "bob", "/alice_data/resource2", "POST", false) + TestEnforce(e, "bob", "/bob_data/resource1", "GET", false) + TestEnforce(e, "bob", "/bob_data/resource1", "POST", true) + TestEnforce(e, "bob", "/bob_data/resource2", "GET", false) + TestEnforce(e, "bob", "/bob_data/resource2", "POST", true) + TestEnforce(e, "cathy", "/cathy_data", "GET", true) + TestEnforce(e, "cathy", "/cathy_data", "POST", true) + TestEnforce(e, "cathy", "/cathy_data", "DELETE", false) +} +func TestKeyMatch2Model(t* testing.T) { + e, _ : = NewEnforcer("examples/keymatch2_model.conf", "examples/keymatch2_policy.csv") + TestEnforce(e, "alice", "/alice_data", "GET", false) + TestEnforce(e, "alice", "/alice_data/resource1", "GET", true) + TestEnforce(e, "alice", "/alice_data2/myid", "GET", false) + TestEnforce(e, "alice", "/alice_data2/myid/using/res_id", "GET", true) +} +func CustomFunction(key1 string, key2 string) bool{ + if key1 == "/alice_data2/myid/using/res_id" && key2 == "/alice_data/:resource" { + return true + } + else if key1 == "/alice_data2/myid/using/res_id" && key2 == "/alice_data2/:id/using/:resId" { + return true +} +else { +return false +} +} + func CustomFunctionWrapper(args ...interface {}) (interface {}, error) { +key1: = args[0].(std::string) + key2 : = args[1].(std::string) + return bool(CustomFunction(key1, key2)), nil +} +func TestKeyMatchCustomModel(t* testing.T) { + e, _ : = NewEnforcer("examples/keymatch_custom_model.conf", "examples/keymatch2_policy.csv") + e.AddFunction("keyMatchCustom", CustomFunctionWrapper) + TestEnforce(e, "alice", "/alice_data2/myid", "GET", false) + TestEnforce(e, "alice", "/alice_data2/myid/using/res_id", "GET", true) +} +func TestIPMatchModel(t* testing.T) { + e, _ : = NewEnforcer("examples/ipmatch_model.conf", "examples/ipmatch_policy.csv") + TestEnforce(e, "192.168.2.123", "data1", "read", true) + TestEnforce(e, "192.168.2.123", "data1", "write", false) + TestEnforce(e, "192.168.2.123", "data2", "read", false) + TestEnforce(e, "192.168.2.123", "data2", "write", false) + TestEnforce(e, "192.168.0.123", "data1", "read", false) + TestEnforce(e, "192.168.0.123", "data1", "write", false) + TestEnforce(e, "192.168.0.123", "data2", "read", false) + TestEnforce(e, "192.168.0.123", "data2", "write", false) + TestEnforce(e, "10.0.0.5", "data1", "read", false) + TestEnforce(e, "10.0.0.5", "data1", "write", false) + TestEnforce(e, "10.0.0.5", "data2", "read", false) + TestEnforce(e, "10.0.0.5", "data2", "write", true) + TestEnforce(e, "192.168.0.1", "data1", "read", false) + TestEnforce(e, "192.168.0.1", "data1", "write", false) + TestEnforce(e, "192.168.0.1", "data2", "read", false) + TestEnforce(e, "192.168.0.1", "data2", "write", false) +} +func TestGlobMatchModel(t* testing.T) { + e, _ : = NewEnforcer("examples/glob_model.conf", "examples/glob_policy.csv") + TestEnforce(e, "u1", "/foo/", "read", true) + TestEnforce(e, "u1", "/foo", "read", false) + TestEnforce(e, "u1", "/foo/subprefix", "read", true) + TestEnforce(e, "u1", "foo", "read", false) + TestEnforce(e, "u2", "/foosubprefix", "read", true) + TestEnforce(e, "u2", "/foo/subprefix", "read", false) + TestEnforce(e, "u2", "foo", "read", false) + TestEnforce(e, "u3", "/prefix/foo/subprefix", "read", true) + TestEnforce(e, "u3", "/prefix/foo/", "read", true) + TestEnforce(e, "u3", "/prefix/foo", "read", false) + TestEnforce(e, "u4", "/foo", "read", false) + TestEnforce(e, "u4", "foo", "read", true) +} +func TestPriorityModel(t* testing.T) { + e, _ : = NewEnforcer("examples/priority_model.conf", "examples/priority_policy.csv") + TestEnforce(e, "alice", "data1", "read", true) + TestEnforce(e, "alice", "data1", "write", false) + TestEnforce(e, "alice", "data2", "read", false) + TestEnforce(e, "alice", "data2", "write", false) + TestEnforce(e, "bob", "data1", "read", false) + TestEnforce(e, "bob", "data1", "write", false) + TestEnforce(e, "bob", "data2", "read", true) + TestEnforce(e, "bob", "data2", "write", false) +} +func TestPriorityModelIndeterminate(t* testing.T) { + e, _ : = NewEnforcer("examples/priority_model.conf", "examples/priority_indeterminate_policy.csv") + TestEnforce(e, "alice", "data1", "read", false) +} +func TestRBACModelInMultiLines(t* testing.T) { + e, _ : = NewEnforcer("examples/rbac_model_in_multi_line.conf", "examples/rbac_policy.csv") + TestEnforce(e, "alice", "data1", "read", true) + TestEnforce(e, "alice", "data1", "write", false) + TestEnforce(e, "alice", "data2", "read", true) + TestEnforce(e, "alice", "data2", "write", true) + TestEnforce(e, "bob", "data1", "read", false) + TestEnforce(e, "bob", "data1", "write", false) + TestEnforce(e, "bob", "data2", "read", false) + TestEnforce(e, "bob", "data2", "write", true) +} +type testSub struct { + Name string + Age int +} +func newTestSubject(name string, age int) testSub { +s: = testSub{} + s.Name = name + s.Age = age + return s +} +func TestABACPolicy(t* testing.T) { + e, _ : = NewEnforcer("examples/abac_rule_model.conf", "examples/abac_rule_policy.csv") + sub1 : = newTestSubject("alice", 16) + sub2 : = newTestSubject("alice", 20) + sub3 : = newTestSubject("alice", 65) + TestEnforce(e, sub1, "/data1", "read", false) + TestEnforce(e, sub1, "/data2", "read", false) + TestEnforce(e, sub1, "/data1", "write", false) + TestEnforce(e, sub1, "/data2", "write", true) + TestEnforce(e, sub2, "/data1", "read", true) + TestEnforce(e, sub2, "/data2", "read", false) + TestEnforce(e, sub2, "/data1", "write", false) + TestEnforce(e, sub2, "/data2", "write", true) + TestEnforce(e, sub3, "/data1", "read", true) + TestEnforce(e, sub3, "/data2", "read", false) + TestEnforce(e, sub3, "/data1", "write", false) + TestEnforce(e, sub3, "/data2", "write", false) +} +func TestCommentModel(t* testing.T) { + e, _ : = NewEnforcer("examples/comment_model.conf", "examples/basic_policy.csv") + TestEnforce(e, "alice", "data1", "read", true) + TestEnforce(e, "alice", "data1", "write", false) + TestEnforce(e, "alice", "data2", "read", false) + TestEnforce(e, "alice", "data2", "write", false) + TestEnforce(e, "bob", "data1", "read", false) + TestEnforce(e, "bob", "data1", "write", false) + TestEnforce(e, "bob", "data2", "read", false) + TestEnforce(e, "bob", "data2", "write", true) +} +*/ + +} diff --git a/tests/model_test.cpp b/tests/model_test.cpp new file mode 100644 index 00000000..bdb6255c --- /dev/null +++ b/tests/model_test.cpp @@ -0,0 +1,100 @@ +/* +* Copyright 2020 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. +* +* This is a test file for testing built in functions in casbin +*/ + +#include +#include +#include + +namespace { + +std::string basic_example = "../../examples/basic_model.conf"; +std::shared_ptr basic_config; +void InitTest() { + basic_config = casbin::Config::NewConfig(basic_example); +} + +TEST(TestModel, TestNewModel) { + casbin::Model* model = casbin::Model::NewModel(); + ASSERT_NE(model, nullptr); +} + +TEST(TestModel, TestNewModelFromFile) { + casbin::Model *model = casbin::Model::NewModelFromFile(basic_example); + ASSERT_NE(model, nullptr); +} + +TEST(TestModel, TestNewModelFromString) { + std::ifstream infile; + infile.open(basic_example); + std::string content; + std::getline(infile, content, '\0'); + casbin::Model* model = casbin::Model::NewModelFromString(content); + + ASSERT_NE(model, nullptr); +} + +TEST(TestModel, TestLoadModelFromConfig) { + InitTest(); + casbin::Model* model = casbin::Model::NewModel(); + model->LoadModelFromConfig(basic_config); + + model = casbin::Model::NewModel(); + std::shared_ptr config = casbin::Config::NewConfigFromText(""); + try { + model->LoadModelFromConfig(config); + FAIL(); + } + catch (casbin::MissingRequiredSections e) { + } +} + +TEST(TestModel, TestHasSection) { + InitTest(); + casbin::Model* model = casbin::Model::NewModel(); + model->LoadModelFromConfig(basic_config); + + for (int i = 0; i < (casbin::Model::required_sections).size(); i++) { + ASSERT_TRUE(model->HasSection((casbin::Model::required_sections)[i])); + } + + model = casbin::Model::NewModel(); + std::shared_ptr config = casbin::Config::NewConfigFromText(""); + try { + model->LoadModelFromConfig(config); + FAIL(); + } + catch (casbin::MissingRequiredSections e) { + } + + for (const auto& section : casbin::Model::required_sections) + ASSERT_FALSE(model->HasSection(section)); +} + +TEST(TestModel, TestModel_AddDef) { + casbin::Model *model = casbin::Model::NewModel(); + std::string s = "r"; + std::string v = "sub, obj, act"; + + bool ok = model->AddDef(s, s, v); + ASSERT_TRUE(ok); + + ok = model->AddDef(s, s, ""); + ASSERT_FALSE(ok); +} + +} \ No newline at end of file diff --git a/tests/rbac_api_test.cpp b/tests/rbac_api_test.cpp new file mode 100644 index 00000000..4271fc0c --- /dev/null +++ b/tests/rbac_api_test.cpp @@ -0,0 +1,228 @@ +/* +* Copyright 2020 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. +* +* This is a test file for testing built in functions in casbin +*/ + +#include +#include + +namespace { + +TEST(TestRBACAPI, TestRoleAPI) { + casbin::Enforcer e("../../examples/rbac_model.conf", "../../examples/rbac_policy.csv"); + + ASSERT_TRUE(casbin::ArrayEquals({ "data2_admin" }, e.GetRolesForUser("alice"))); + ASSERT_TRUE(casbin::ArrayEquals({ }, e.GetRolesForUser("bob"))); + ASSERT_TRUE(casbin::ArrayEquals({ }, e.GetRolesForUser("data2_admin"))); + ASSERT_TRUE(casbin::ArrayEquals({ }, e.GetRolesForUser("non_exist"))); + + ASSERT_FALSE(e.HasRoleForUser("alice", "data1_admin")); + ASSERT_TRUE(e.HasRoleForUser("alice", "data2_admin")); + + e.AddRoleForUser("alice", "data1_admin"); + + ASSERT_TRUE(casbin::ArrayEquals({ "data1_admin", "data2_admin" }, e.GetRolesForUser("alice"))); + ASSERT_TRUE(casbin::ArrayEquals({ }, e.GetRolesForUser("bob"))); + ASSERT_TRUE(casbin::ArrayEquals({ }, e.GetRolesForUser("data2_admin"))); + + e.DeleteRoleForUser("alice", "data1_admin"); + + ASSERT_TRUE(casbin::ArrayEquals({ "data2_admin" }, e.GetRolesForUser("alice"))); + ASSERT_TRUE(casbin::ArrayEquals({ }, e.GetRolesForUser("bob"))); + ASSERT_TRUE(casbin::ArrayEquals({ }, e.GetRolesForUser("data2_admin"))); + + e.DeleteRolesForUser("alice"); + + ASSERT_TRUE(casbin::ArrayEquals({ }, e.GetRolesForUser("alice"))); + ASSERT_TRUE(casbin::ArrayEquals({ }, e.GetRolesForUser("bob"))); + ASSERT_TRUE(casbin::ArrayEquals({ }, e.GetRolesForUser("data2_admin"))); + + e.AddRoleForUser("alice", "data1_admin"); + e.DeleteUser("alice"); + + ASSERT_TRUE(casbin::ArrayEquals({ }, e.GetRolesForUser("alice"))); + ASSERT_TRUE(casbin::ArrayEquals({ }, e.GetRolesForUser("bob"))); + ASSERT_TRUE(casbin::ArrayEquals({ }, e.GetRolesForUser("data2_admin"))); + + e.AddRoleForUser("alice", "data2_admin"); + + ASSERT_FALSE(e.Enforce({ "alice", "data1", "read" })); + ASSERT_FALSE(e.Enforce({ "alice", "data1", "write" })); + ASSERT_TRUE(e.Enforce({ "alice", "data2", "read" })); + ASSERT_TRUE(e.Enforce({ "alice", "data2", "write" })); + ASSERT_FALSE(e.Enforce({ "bob", "data1", "read" })); + ASSERT_FALSE(e.Enforce({ "bob", "data1", "write" })); + ASSERT_FALSE(e.Enforce({ "bob", "data2", "read" })); + ASSERT_TRUE(e.Enforce({ "bob", "data2", "write" })); + + e.DeleteRole("data2_admin"); + + ASSERT_FALSE(e.Enforce({ "alice", "data1", "read" })); + ASSERT_FALSE(e.Enforce({ "alice", "data1", "write" })); + ASSERT_FALSE(e.Enforce({ "alice", "data2", "read" })); + ASSERT_FALSE(e.Enforce({ "alice", "data2", "write" })); + ASSERT_FALSE(e.Enforce({ "bob", "data1", "read" })); + ASSERT_FALSE(e.Enforce({ "bob", "data1", "write" })); + ASSERT_FALSE(e.Enforce({ "bob", "data2", "read" })); + ASSERT_TRUE(e.Enforce({ "bob", "data2", "write" })); +} + +TEST(TestRBACAPI, TestEnforcer_AddRolesForUser) { + casbin::Enforcer e("../../examples/rbac_model.conf", "../../examples/rbac_policy.csv"); + + e.AddRolesForUser("alice", { "data1_admin", "data2_admin", "data3_admin" }); + ASSERT_TRUE(casbin::ArrayEquals({ "data1_admin", "data2_admin", "data3_admin" }, e.GetRolesForUser("alice"))); + + ASSERT_TRUE(e.Enforce({ "alice", "data1", "read" })); + ASSERT_TRUE(e.Enforce({ "alice", "data2", "read" })); + ASSERT_TRUE(e.Enforce({ "alice", "data2", "write" })); +} + +void TestGetPermissions(casbin::Enforcer& e, const std::string& name, const std::vector>& res) { + std::vector> my_res = e.GetPermissionsForUser(name); + int count = 0; + for (auto& my_response : my_res) { + for (auto& response : res) { + if (casbin::ArrayEquals(response, my_response)) { + ++count; + break; + } + } + } + ASSERT_EQ(static_cast(res.size()), count); +} + +TEST(TestRBACAPI, TestPermissionAPI) { + casbin::Enforcer e("../../examples/basic_without_resources_model.conf", "../../examples/basic_without_resources_policy.csv"); + + ASSERT_TRUE(e.Enforce(std::vector{ "alice", "read" })); + ASSERT_FALSE(e.Enforce(std::vector{ "alice", "write" })); + ASSERT_FALSE(e.Enforce(std::vector{ "bob", "read" })); + ASSERT_TRUE(e.Enforce(std::vector{ "bob", "write" })); + + TestGetPermissions(e, "alice", { {"alice", "read"} }); + TestGetPermissions(e, "bob", { {"bob", "write"} }); + + ASSERT_TRUE(e.HasPermissionForUser("alice", { "read" })); + ASSERT_FALSE(e.HasPermissionForUser("alice", { "write" })); + ASSERT_FALSE(e.HasPermissionForUser("bob", { "read" })); + ASSERT_TRUE(e.HasPermissionForUser("bob", { "write" })); + + e.DeletePermission({ "read" }); + + ASSERT_FALSE(e.Enforce(std::vector{ "alice", "read" })); + ASSERT_FALSE(e.Enforce(std::vector{ "alice", "write" })); + ASSERT_FALSE(e.Enforce(std::vector{ "bob", "read" })); + ASSERT_TRUE(e.Enforce(std::vector{ "bob", "write" })); + + e.AddPermissionForUser("bob", { "read" }); + + ASSERT_FALSE(e.Enforce(std::vector{ "alice", "read" })); + ASSERT_FALSE(e.Enforce(std::vector{ "alice", "write" })); + ASSERT_TRUE(e.Enforce(std::vector{ "bob", "read" })); + ASSERT_TRUE(e.Enforce(std::vector{ "bob", "write" })); + + e.DeletePermissionForUser("bob", { "read" }); + + ASSERT_FALSE(e.Enforce(std::vector{ "alice", "read" })); + ASSERT_FALSE(e.Enforce(std::vector{ "alice", "write" })); + ASSERT_FALSE(e.Enforce(std::vector{ "bob", "read" })); + ASSERT_TRUE(e.Enforce(std::vector{ "bob", "write" })); + + e.DeletePermissionsForUser("bob"); + + ASSERT_FALSE(e.Enforce(std::vector{ "alice", "read" })); + ASSERT_FALSE(e.Enforce(std::vector{ "alice", "write" })); + ASSERT_FALSE(e.Enforce(std::vector{ "bob", "read" })); + ASSERT_FALSE(e.Enforce(std::vector{ "bob", "write" })); +} + +TEST(TestRBACAPI, TestImplicitRoleAPI) { + casbin::Enforcer e("../../examples/rbac_model.conf", "../../examples/rbac_with_hierarchy_policy.csv"); + + TestGetPermissions(e, "alice", std::vector>{ {"alice", "data1", "read"} }); + TestGetPermissions(e, "bob", std::vector>{ {"bob", "data2", "write"} }); + + ASSERT_TRUE(casbin::ArrayEquals(std::vector{ "admin", "data1_admin", "data2_admin" }, e.GetImplicitRolesForUser("alice"))); + ASSERT_TRUE(casbin::ArrayEquals(std::vector{ }, e.GetImplicitRolesForUser("bob"))); + + e = casbin::Enforcer("../../examples/rbac_with_pattern_model.conf", "../../examples/rbac_with_pattern_policy.csv"); + + dynamic_cast(e.GetRoleManager().get())->AddMatchingFunc(casbin::KeyMatch); + + ASSERT_TRUE(casbin::ArrayEquals(std::vector{ "/book/1/2/3/4/5", "pen_admin", "/book/*", "book_group" }, e.GetImplicitRolesForUser("cathy"))); + ASSERT_TRUE(casbin::ArrayEquals(std::vector{ "/book/1/2/3/4/5", "pen_admin" }, e.GetRolesForUser("cathy"))); +} + +void TestGetImplicitPermissions(casbin::Enforcer& e, const std::string& name, const std::vector>& res) { + std::vector> my_res = e.GetImplicitPermissionsForUser(name); + int count = 0; + for (auto& my_response : my_res) { + for (auto& response : res) { + if (casbin::ArrayEquals(response, my_response)) { + ++count; + break; + } + } + } + ASSERT_EQ(static_cast(res.size()), count); +} + +void TestGetImplicitPermissionsWithDomain(casbin::Enforcer& e, const std::string& name, const std::string& domain, const std::vector>& res) { + std::vector> my_res = e.GetImplicitPermissionsForUser(name, { domain }); + int count = 0; + for (auto& my_response : my_res) { + for (auto& response : res) { + if (casbin::ArrayEquals(response, my_response)) { + ++count; + break; + } + } + } + ASSERT_EQ(static_cast(res.size()), count); +} + +TEST(TestRBACAPI, TestImplicitPermissionAPI) { + casbin::Enforcer e("../../examples/rbac_model.conf", "../../examples/rbac_with_hierarchy_policy.csv"); + + TestGetPermissions(e, "alice", { {"alice", "data1", "read"} }); + TestGetPermissions(e, "bob", { {"bob", "data2", "write"} }); + + TestGetImplicitPermissions(e, "alice", { {"alice", "data1", "read"}, { "data1_admin", "data1", "read" }, { "data1_admin", "data1", "write" }, { "data2_admin", "data2", "read" }, { "data2_admin", "data2", "write" } }); + TestGetImplicitPermissions(e, "bob", { {"bob", "data2", "write"} }); +} + +TEST(TestRBACAPI, TestImplicitPermissionAPIWithDomain) { + casbin::Enforcer e("../../examples/rbac_with_domains_model.conf", "../../examples/rbac_with_hierarchy_with_domains_policy.csv"); + TestGetImplicitPermissionsWithDomain(e, "alice", "domain1", { {"alice", "domain1", "data2", "read"}, { "role:reader", "domain1", "data1", "read" }, { "role:writer", "domain1", "data1", "write" } }); +} + +TEST(TestRBACAPI, TestImplicitUserAPI) { + casbin::Enforcer e("../../examples/rbac_model.conf", "../../examples/rbac_with_hierarchy_policy.csv"); + + ASSERT_TRUE(casbin::ArrayEquals({ "alice" }, e.GetImplicitUsersForPermission({ "data1", "read" }))); + ASSERT_TRUE(casbin::ArrayEquals({ "alice" }, e.GetImplicitUsersForPermission({ "data1", "write" }))); + ASSERT_TRUE(casbin::ArrayEquals({ "alice" }, e.GetImplicitUsersForPermission({ "data2", "read" }))); + ASSERT_TRUE(casbin::ArrayEquals({ "alice", "bob" }, e.GetImplicitUsersForPermission({ "data2", "write" }))); + + e.ClearPolicy(); + e.AddPolicy({ "admin", "data1", "read" }); + e.AddPolicy({ "bob", "data1", "read" }); + e.AddGroupingPolicy({ "alice", "admin" }); + ASSERT_TRUE(casbin::ArrayEquals({ "alice", "bob" }, e.GetImplicitUsersForPermission({ "data1", "read" }))); +} + +} diff --git a/tests/rbac_api_with_domains_test.cpp b/tests/rbac_api_with_domains_test.cpp new file mode 100644 index 00000000..92f55d58 --- /dev/null +++ b/tests/rbac_api_with_domains_test.cpp @@ -0,0 +1,192 @@ +/* +* Copyright 2020 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. +* +* This is a test file for testing built in functions in casbin +*/ + +#include +#include + +namespace { + +TEST(TestRBACAPIWithDomains, TestGetImplicitRolesForDomainUser) { + casbin::Enforcer e("../../examples/rbac_with_domains_model.conf", "../../examples/rbac_with_hierarchy_with_domains_policy.csv"); + + // This is only able to retrieve the first level of roles. + ASSERT_TRUE(casbin::ArrayEquals({ "role:global_admin" }, e.GetRolesForUserInDomain("alice", { "domain1" }))); + + // Retrieve all inherit roles. It supports domains as well. + ASSERT_TRUE(casbin::ArrayEquals(std::vector{"role:global_admin", "role:reader", "role:writer"}, e.GetImplicitRolesForUser("alice", {"domain1"}))); +} + +// TestUserAPIWithDomains: Add by Gordon +TEST(TestRBACAPIWithDomains, TestUserAPIWithDomains) { + casbin::Enforcer e("../../examples/rbac_with_domains_model.conf", "../../examples/rbac_with_domains_policy.csv"); + + ASSERT_TRUE(casbin::ArrayEquals({ "alice" }, e.GetUsersForRole("admin", { "domain1" }))); + ASSERT_TRUE(casbin::ArrayEquals({ "alice" }, e.GetUsersForRoleInDomain("admin", { "domain1" }))); + + try { + e.GetUsersForRole("non_exist", { "domain1" }); + } + catch (casbin::CasbinRBACException e) { + ASSERT_TRUE(true); + } + + try { + e.GetUsersForRoleInDomain("non_exist", { "domain1" }); + } + catch (casbin::CasbinRBACException e) { + ASSERT_TRUE(true); + } + + ASSERT_TRUE(casbin::ArrayEquals({ "bob" }, e.GetUsersForRole("admin", { "domain2" }))); + ASSERT_TRUE(casbin::ArrayEquals({ "bob" }, e.GetUsersForRoleInDomain("admin", { "domain2" }))); + + try { + e.GetUsersForRole("non_exist", { "domain2" }); + } + catch (casbin::CasbinRBACException e) { + ASSERT_TRUE(true); + } + + try { + e.GetUsersForRoleInDomain("non_exist", { "domain2" }); + } + catch (casbin::CasbinRBACException e) { + ASSERT_TRUE(true); + } + + e.DeleteRoleForUserInDomain("alice", "admin", "domain1"); + e.AddRoleForUserInDomain("bob", "admin", "domain1"); + + ASSERT_TRUE(casbin::ArrayEquals({ "bob" }, e.GetUsersForRole("admin", { "domain1" }))); + ASSERT_TRUE(casbin::ArrayEquals({ "bob" }, e.GetUsersForRoleInDomain("admin", { "domain1" }))); + + try { + e.GetUsersForRole("non_exist", { "domain1" }); + } + catch (casbin::CasbinRBACException e) { + ASSERT_TRUE(true); + } + + try { + e.GetUsersForRoleInDomain("non_exist", { "domain1" }); + } + catch (casbin::CasbinRBACException e) { + ASSERT_TRUE(true); + } + + ASSERT_TRUE(casbin::ArrayEquals({ "bob" }, e.GetUsersForRole("admin", { "domain2" }))); + ASSERT_TRUE(casbin::ArrayEquals({ "bob" }, e.GetUsersForRoleInDomain("admin", { "domain2" }))); + + try { + e.GetUsersForRole("non_exist", { "domain2" }); + } + catch (casbin::CasbinRBACException e) { + ASSERT_TRUE(true); + } + try { + e.GetUsersForRoleInDomain("non_exist", { "domain2" }); + } + catch (casbin::CasbinRBACException e) { + ASSERT_TRUE(true); + } +} + +TEST(TestRBACAPIWithDomains, TestRoleAPIWithDomains) { + casbin::Enforcer e("../../examples/rbac_with_domains_model.conf", "../../examples/rbac_with_domains_policy.csv"); + + ASSERT_TRUE(casbin::ArrayEquals({ "admin" }, e.GetRolesForUser("alice", { "domain1" }))); + ASSERT_TRUE(casbin::ArrayEquals({ "admin" }, e.GetRolesForUserInDomain("alice", { "domain1" }))); + + ASSERT_TRUE(casbin::ArrayEquals({ }, e.GetRolesForUser("bob", { "domain1" }))); + ASSERT_TRUE(casbin::ArrayEquals({ }, e.GetRolesForUserInDomain("bob", { "domain1" }))); + + ASSERT_TRUE(casbin::ArrayEquals({ }, e.GetRolesForUser("admin", { "domain1" }))); + ASSERT_TRUE(casbin::ArrayEquals({ }, e.GetRolesForUserInDomain("admin", { "domain1" }))); + + ASSERT_TRUE(casbin::ArrayEquals({ }, e.GetRolesForUser("non_exist", { "domain1" }))); + ASSERT_TRUE(casbin::ArrayEquals({ }, e.GetRolesForUserInDomain("non_exist", { "domain1" }))); + + ASSERT_TRUE(casbin::ArrayEquals({ }, e.GetRolesForUser("alice", { "domain2" }))); + ASSERT_TRUE(casbin::ArrayEquals({ }, e.GetRolesForUserInDomain("alice", { "domain2" }))); + + ASSERT_TRUE(casbin::ArrayEquals({ "admin" }, e.GetRolesForUser("bob", { "domain2" }))); + ASSERT_TRUE(casbin::ArrayEquals({ "admin" }, e.GetRolesForUserInDomain("bob", { "domain2" }))); + + ASSERT_TRUE(casbin::ArrayEquals({ }, e.GetRolesForUser("admin", { "domain2" }))); + ASSERT_TRUE(casbin::ArrayEquals({ }, e.GetRolesForUserInDomain("admin", { "domain2" }))); + + ASSERT_TRUE(casbin::ArrayEquals({ }, e.GetRolesForUser("non_exist", { "domain2" }))); + ASSERT_TRUE(casbin::ArrayEquals({ }, e.GetRolesForUserInDomain("non_exist", { "domain2" }))); + + e.DeleteRoleForUserInDomain("alice", "admin", "domain1"); + e.AddRoleForUserInDomain("bob", "admin", "domain1"); + + ASSERT_TRUE(casbin::ArrayEquals({ }, e.GetRolesForUser("alice", { "domain1" }))); + ASSERT_TRUE(casbin::ArrayEquals({ }, e.GetRolesForUserInDomain("alice", { "domain1" }))); + + ASSERT_TRUE(casbin::ArrayEquals({ "admin" }, e.GetRolesForUser("bob", { "domain1" }))); + ASSERT_TRUE(casbin::ArrayEquals({ "admin" }, e.GetRolesForUserInDomain("bob", { "domain1" }))); + + ASSERT_TRUE(casbin::ArrayEquals({ }, e.GetRolesForUser("admin", { "domain1" }))); + ASSERT_TRUE(casbin::ArrayEquals({ }, e.GetRolesForUserInDomain("admin", { "domain1" }))); + + ASSERT_TRUE(casbin::ArrayEquals({ }, e.GetRolesForUser("non_exist", { "domain1" }))); + ASSERT_TRUE(casbin::ArrayEquals({ }, e.GetRolesForUserInDomain("non_exist", { "domain1" }))); + + ASSERT_TRUE(casbin::ArrayEquals({ }, e.GetRolesForUser("alice", { "domain2" }))); + ASSERT_TRUE(casbin::ArrayEquals({ }, e.GetRolesForUserInDomain("alice", { "domain2" }))); + + ASSERT_TRUE(casbin::ArrayEquals({ "admin" }, e.GetRolesForUser("bob", { "domain2" }))); + ASSERT_TRUE(casbin::ArrayEquals({ "admin" }, e.GetRolesForUserInDomain("bob", { "domain2" }))); + + ASSERT_TRUE(casbin::ArrayEquals({ }, e.GetRolesForUser("admin", { "domain2" }))); + ASSERT_TRUE(casbin::ArrayEquals({ }, e.GetRolesForUserInDomain("admin", { "domain2" }))); + + ASSERT_TRUE(casbin::ArrayEquals({ }, e.GetRolesForUser("non_exist", { "domain2" }))); + ASSERT_TRUE(casbin::ArrayEquals({ }, e.GetRolesForUserInDomain("non_exist", { "domain2" }))); +} + +void TestGetPermissionsInDomain(casbin::Enforcer& e, const std::string& name, const std::string& domain, const std::vector>& res) { + std::vector> my_res = e.GetPermissionsForUserInDomain(name, { domain }); + int count = 0; + for (auto& my_response : my_res) { + for (auto& response : res) { + if (casbin::ArrayEquals(response, my_response)) { + ++count; + break; + } + } + } + ASSERT_EQ(static_cast(res.size()), count); +} + +TEST(TestRBACAPIWithDomains, TestPermissionAPIInDomain) { + casbin::Enforcer e("../../examples/rbac_with_domains_model.conf", "../../examples/rbac_with_domains_policy.csv"); + + TestGetPermissionsInDomain(e, "alice", "domain1", {}); + TestGetPermissionsInDomain(e, "bob", "domain1", {}); + TestGetPermissionsInDomain(e, "admin", "domain1", { {"admin", "domain1", "data1", "read"}, {"admin", "domain1", "data1", "write"} }); + TestGetPermissionsInDomain(e, "non_exist", "domain1", {}); + + TestGetPermissionsInDomain(e, "alice", "domain2", {}); + TestGetPermissionsInDomain(e, "bob", "domain2", {}); + TestGetPermissionsInDomain(e, "admin", "domain2", { {"admin", "domain2", "data2", "read"}, {"admin", "domain2", "data2", "write"} }); + TestGetPermissionsInDomain(e, "non_exist", "domain2", {}); +} + +} diff --git a/tests/role_manager_test.cpp b/tests/role_manager_test.cpp new file mode 100644 index 00000000..95dbfdc6 --- /dev/null +++ b/tests/role_manager_test.cpp @@ -0,0 +1,195 @@ +/* +* Copyright 2020 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. +* +* This is a test file for testing built in functions in casbin +*/ + +#include +#include + +namespace { + +void TestRole(casbin::DefaultRoleManager& rm, const std::string& name1, const std::string& name2, bool res) { + bool my_res = rm.HasLink(name1, name2); + ASSERT_EQ(res, my_res); +} + +void TestDomainRole(casbin::DefaultRoleManager& rm, const std::string& name1, const std::string& name2, const std::vector& domain, bool res) { + bool my_res = rm.HasLink(name1, name2, domain); + + ASSERT_EQ(res, my_res); +} + +TEST(TestRoleManager, TestRole) { + casbin::DefaultRoleManager rm(3); + rm.AddLink("u1", "g1"); + rm.AddLink("u2", "g1"); + rm.AddLink("u3", "g2"); + rm.AddLink("u4", "g2"); + rm.AddLink("u4", "g3"); + rm.AddLink("g1", "g3"); + + // Current role inheritance tree: + // g3 g2 + // / \ / \ + // g1 u4 u3 + // / \ + // u1 u2 + + TestRole(rm, "u1", "g1", true); + TestRole(rm, "u1", "g2", false); + TestRole(rm, "u1", "g3", true); + TestRole(rm, "u2", "g1", true); + TestRole(rm, "u2", "g2", false); + TestRole(rm, "u2", "g3", true); + TestRole(rm, "u3", "g1", false); + TestRole(rm, "u3", "g2", true); + TestRole(rm, "u3", "g3", false); + TestRole(rm, "u4", "g1", false); + TestRole(rm, "u4", "g2", true); + TestRole(rm, "u4", "g3", true); + + rm.DeleteLink("g1", "g3"); + rm.DeleteLink("u4", "g2"); + + // Current role inheritance tree after deleting the links: + // g3 g2 + // \ \ + // g1 u4 u3 + // / \ + // u1 u2 + + TestRole(rm, "u1", "g1", true); + TestRole(rm, "u1", "g2", false); + TestRole(rm, "u1", "g3", false); + TestRole(rm, "u2", "g1", true); + TestRole(rm, "u2", "g2", false); + TestRole(rm, "u2", "g3", false); + TestRole(rm, "u3", "g1", false); + TestRole(rm, "u3", "g2", true); + TestRole(rm, "u3", "g3", false); + TestRole(rm, "u4", "g1", false); + TestRole(rm, "u4", "g2", false); + TestRole(rm, "u4", "g3", true); +} + +TEST(TestRoleManager, TestDomainRole) { + casbin::DefaultRoleManager rm(3); + std::vector domain1 = { "domain1" }; + std::vector domain2 = { "domain2" }; + + rm.AddLink("u1", "g1", domain1); + rm.AddLink("u2", "g1", domain1); + + rm.AddLink("u3", "admin", domain2); + rm.AddLink("u4", "admin", domain2); + + rm.AddLink("u4", "admin", domain1); + rm.AddLink("g1", "admin", domain1); + + // Current role inheritance tree: + // domain1:admin domain2:admin + // / \ / \ + // domain1:g1 u4 u3 + // / \ + // u1 u2 + + TestDomainRole(rm, "u1", "g1", domain1, true); + TestDomainRole(rm, "u1", "g1", domain2, false); + TestDomainRole(rm, "u1", "admin", domain1, true); + TestDomainRole(rm, "u1", "admin", domain2, false); + + TestDomainRole(rm, "u2", "g1", domain1, true); + TestDomainRole(rm, "u2", "g1", domain2, false); + TestDomainRole(rm, "u2", "admin", domain1, true); + TestDomainRole(rm, "u2", "admin", domain2, false); + + TestDomainRole(rm, "u3", "g1", domain1, false); + TestDomainRole(rm, "u3", "g1", domain2, false); + TestDomainRole(rm, "u3", "admin", domain1, false); + TestDomainRole(rm, "u3", "admin", domain2, true); + + TestDomainRole(rm, "u4", "g1", domain1, false); + TestDomainRole(rm, "u4", "g1", domain2, false); + TestDomainRole(rm, "u4", "admin", domain1, true); + TestDomainRole(rm, "u4", "admin", domain2, true); + + rm.DeleteLink("g1", "admin", domain1); + rm.DeleteLink("u4", "admin", domain2); + + // Current role inheritance tree after deleting the links: + // domain1:admin domain2:admin + // \ \ + // domain1:g1 u4 u3 + // / \ + // u1 u2 + + TestDomainRole(rm, "u1", "g1", domain1, true); + TestDomainRole(rm, "u1", "g1", domain2, false); + TestDomainRole(rm, "u1", "admin", domain1, false); + TestDomainRole(rm, "u1", "admin", domain2, false); + + TestDomainRole(rm, "u2", "g1", domain1, true); + TestDomainRole(rm, "u2", "g1", domain2, false); + TestDomainRole(rm, "u2", "admin", domain1, false); + TestDomainRole(rm, "u2", "admin", domain2, false); + + TestDomainRole(rm, "u3", "g1", domain1, false); + TestDomainRole(rm, "u3", "g1", domain2, false); + TestDomainRole(rm, "u3", "admin", domain1, false); + TestDomainRole(rm, "u3", "admin", domain2, true); + + TestDomainRole(rm, "u4", "g1", domain1, false); + TestDomainRole(rm, "u4", "g1", domain2, false); + TestDomainRole(rm, "u4", "admin", domain1, true); + TestDomainRole(rm, "u4", "admin", domain2, false); +} + +TEST(TestRoleManager, TestClear) { + casbin::DefaultRoleManager rm(3); + rm.AddLink("u1", "g1"); + rm.AddLink("u2", "g1"); + rm.AddLink("u3", "g2"); + rm.AddLink("u4", "g2"); + rm.AddLink("u4", "g3"); + rm.AddLink("g1", "g3"); + + // Current role inheritance tree: + // g3 g2 + // / \ / \ + // g1 u4 u3 + // / \ + // u1 u2 + + rm.Clear(); + + // All data is cleared. + // No role inheritance now. + + TestRole(rm, "u1", "g1", false); + TestRole(rm, "u1", "g2", false); + TestRole(rm, "u1", "g3", false); + TestRole(rm, "u2", "g1", false); + TestRole(rm, "u2", "g2", false); + TestRole(rm, "u2", "g3", false); + TestRole(rm, "u3", "g1", false); + TestRole(rm, "u3", "g2", false); + TestRole(rm, "u3", "g3", false); + TestRole(rm, "u4", "g1", false); + TestRole(rm, "u4", "g2", false); + TestRole(rm, "u4", "g3", false); +} + +} From 6dfa478eb5b7540cc5fae1c26eabff8c4b62af19 Mon Sep 17 00:00:00 2001 From: "Yash Pandey (YP)" Date: Sun, 20 Jun 2021 15:38:08 +0530 Subject: [PATCH 2/4] chore: Removed MSV files Signed-off-by: Yash Pandey (YP) --- casbin.sln | 44 --- casbin/casbin.vcxproj | 311 --------------------- casbin/casbin.vcxproj.filters | 513 ---------------------------------- 3 files changed, 868 deletions(-) delete mode 100644 casbin.sln delete mode 100644 casbin/casbin.vcxproj delete mode 100644 casbin/casbin.vcxproj.filters diff --git a/casbin.sln b/casbin.sln deleted file mode 100644 index 10971a1f..00000000 --- a/casbin.sln +++ /dev/null @@ -1,44 +0,0 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 16 -VisualStudioVersion = 16.0.29806.167 -MinimumVisualStudioVersion = 10.0.40219.1 -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "casbin", "casbin\casbin.vcxproj", "{BEAD4515-CC5D-4E73-BC71-334859913D06}" -EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "test", "test\test.vcxproj", "{4EA930EE-C5C4-4CF3-BE47-AAF37A158C40}" - ProjectSection(ProjectDependencies) = postProject - {BEAD4515-CC5D-4E73-BC71-334859913D06} = {BEAD4515-CC5D-4E73-BC71-334859913D06} - EndProjectSection -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|x64 = Debug|x64 - Debug|x86 = Debug|x86 - Release|x64 = Release|x64 - Release|x86 = Release|x86 - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {BEAD4515-CC5D-4E73-BC71-334859913D06}.Debug|x64.ActiveCfg = Debug|x64 - {BEAD4515-CC5D-4E73-BC71-334859913D06}.Debug|x64.Build.0 = Debug|x64 - {BEAD4515-CC5D-4E73-BC71-334859913D06}.Debug|x86.ActiveCfg = Debug|Win32 - {BEAD4515-CC5D-4E73-BC71-334859913D06}.Debug|x86.Build.0 = Debug|Win32 - {BEAD4515-CC5D-4E73-BC71-334859913D06}.Release|x64.ActiveCfg = Debug|x64 - {BEAD4515-CC5D-4E73-BC71-334859913D06}.Release|x64.Build.0 = Debug|x64 - {BEAD4515-CC5D-4E73-BC71-334859913D06}.Release|x86.ActiveCfg = Release|Win32 - {BEAD4515-CC5D-4E73-BC71-334859913D06}.Release|x86.Build.0 = Release|Win32 - {4EA930EE-C5C4-4CF3-BE47-AAF37A158C40}.Debug|x64.ActiveCfg = Debug|x64 - {4EA930EE-C5C4-4CF3-BE47-AAF37A158C40}.Debug|x64.Build.0 = Debug|x64 - {4EA930EE-C5C4-4CF3-BE47-AAF37A158C40}.Debug|x86.ActiveCfg = Debug|Win32 - {4EA930EE-C5C4-4CF3-BE47-AAF37A158C40}.Debug|x86.Build.0 = Debug|Win32 - {4EA930EE-C5C4-4CF3-BE47-AAF37A158C40}.Release|x64.ActiveCfg = Release|x64 - {4EA930EE-C5C4-4CF3-BE47-AAF37A158C40}.Release|x64.Build.0 = Release|x64 - {4EA930EE-C5C4-4CF3-BE47-AAF37A158C40}.Release|x86.ActiveCfg = Release|Win32 - {4EA930EE-C5C4-4CF3-BE47-AAF37A158C40}.Release|x86.Build.0 = Release|Win32 - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection - GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {4E08836F-47E7-49C2-B86A-1DE826AD7662} - EndGlobalSection -EndGlobal diff --git a/casbin/casbin.vcxproj b/casbin/casbin.vcxproj deleted file mode 100644 index 62532767..00000000 --- a/casbin/casbin.vcxproj +++ /dev/null @@ -1,311 +0,0 @@ - - - - - Debug - Win32 - - - Release - Win32 - - - Debug - x64 - - - Release - x64 - - - - 16.0 - {BEAD4515-CC5D-4E73-BC71-334859913D06} - Win32Proj - casbin - 10.0 - - - - DynamicLibrary - true - v142 - Unicode - - - DynamicLibrary - false - v142 - true - Unicode - - - StaticLibrary - true - v142 - Unicode - - - StaticLibrary - false - v142 - true - Unicode - - - - - - - - - - - - - - - - - - - - - true - - - true - - - false - - - false - - - - Use - Level3 - true - WIN32;_DEBUG;CASBIN_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) - true - pch.h - - - Windows - true - false - - - - - Use - Level3 - true - _DEBUG;CASBIN_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) - true - pch.h - 4996 - stdcpp17 - - - Windows - true - false - - - - - Use - Level3 - true - true - true - WIN32;NDEBUG;CASBIN_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) - true - pch.h - - - Windows - true - true - true - false - - - - - Use - Level3 - true - true - true - NDEBUG;CASBIN_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions) - true - pch.h - 4996 - stdcpp17 - - - Windows - true - true - true - false - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Create - Create - Create - Create - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/casbin/casbin.vcxproj.filters b/casbin/casbin.vcxproj.filters deleted file mode 100644 index 85d39704..00000000 --- a/casbin/casbin.vcxproj.filters +++ /dev/null @@ -1,513 +0,0 @@ - - - - - {4FC737F1-C7A5-4376-A066-2A32D752A2FF} - cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx - - - {93995380-89BD-4b04-88EB-625FBE52EBFB} - h;hh;hpp;hxx;hm;inl;inc;ipp;xsd - - - {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} - rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms - - - {32489c5d-6ea0-4d7e-9a3d-72ad59cd1ce0} - - - {fc255813-1f15-42bb-bc05-26a1111d9c3d} - - - {5c372a6d-1a50-464b-a457-2c7bc084d256} - - - {2e3edbd9-b87d-42e7-aa4a-3dcfdb0af1a9} - - - {1edc3d86-e4a6-41bd-8c13-c6a849d3fd5b} - - - {998783e5-4cdf-4808-9c75-e51ab0952cc9} - - - {6f74038e-9348-49cf-86e3-0288330df3aa} - - - {fe9a31f8-f199-41cc-a143-97d857160133} - - - {608fe2a4-0ca4-4b1b-8585-b33692c7efe4} - - - {19fadd05-2499-4380-9033-b75f79f90483} - - - {02df1eec-cf14-4ca6-a900-09c7feada787} - - - {d1ae171d-ef7f-4125-8c0b-c21c38a96a3b} - - - {64f7731e-f8d6-4a9f-b49c-aeeb8e833cb7} - - - {6ce08247-6b46-4921-867b-2a3d7b56248d} - - - {943dca81-dd7c-43b9-ba98-ee6efd09dd65} - - - {8c4709f3-d214-42f5-b506-2f416b7e2819} - - - {1521add1-3af8-4660-adb9-ec55175d4621} - - - {deb02d53-9580-4fb9-b666-6b3a2713d286} - - - {c1a83ec0-b3e2-4b46-9755-6e20d8f26356} - - - {c9d1230b-64a7-4553-aba5-fe331709ed07} - - - {ba2b7711-b8b5-4932-a296-e1ad76599728} - - - {4d2de745-b3c0-4019-a95b-a1b641e698da} - - - {523502e9-4d2b-4d36-8556-2f8e84d461aa} - - - {efc1c0ae-be20-4cac-a147-934d0743420a} - - - - - Source Files - - - Source Files\duktape - - - Source Files\config - - - Source Files\effect - - - Source Files\exception - - - Source Files\exception - - - Source Files\exception - - - Source Files\exception - - - Source Files\exception - - - Source Files\exception - - - Source Files\exception - - - Source Files\ip_parser\parser - - - Source Files\ip_parser\parser - - - Source Files\ip_parser\parser - - - Source Files\ip_parser\parser - - - Source Files\ip_parser\parser - - - Source Files\ip_parser\parser - - - Source Files\ip_parser\parser - - - Source Files\ip_parser\parser - - - Source Files\ip_parser\parser - - - Source Files\ip_parser\parser - - - Source Files\ip_parser\parser - - - Source Files\ip_parser\parser - - - Source Files\model - - - Source Files\model - - - Source Files\model - - - Source Files\model - - - Source Files\util - - - Source Files\util - - - Source Files\util - - - Source Files\util - - - Source Files\util - - - Source Files\util - - - Source Files\util - - - Source Files\util - - - Source Files\util - - - Source Files\util - - - Source Files\util - - - Source Files\util - - - Source Files\util - - - Source Files\util - - - Source Files\util - - - Source Files\rbac - - - Source Files\persist\file_adapter - - - Source Files\persist\file_adapter - - - Source Files\persist - - - Source Files\persist - - - Source Files\persist - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files - - - Source Files\ip_parser\parser - - - Source Files\persist\file_adapter - - - Source Files - - - Source Files\ip_parser\exception - - - Source Files - - - Source Files - - - Source Files - - - - - Header Files\config - - - Header Files\config - - - Header Files\effect - - - Header Files\effect - - - Header Files\persist - - - Header Files\persist - - - Header Files\persist\file_adapter - - - Header Files\rbac - - - Header Files\rbac - - - Header Files - - - Header Files - - - Header Files\persist\file_adapter - - - Header Files\persist - - - Header Files\persist - - - Header Files\persist - - - Header Files\util - - - Header Files\util - - - Header Files\util - - - Header Files\config - - - Header Files\persist - - - Header Files\persist\file_adapter - - - Header Files\rbac - - - Header Files\exception - - - Header Files\exception - - - Header Files\exception - - - Header Files\exception - - - Header Files\exception - - - Header Files\exception - - - Header Files\exception - - - Header Files\exception - - - Header Files\ip_parser\exception - - - Header Files\ip_parser\parser - - - Header Files\ip_parser\parser - - - Header Files\ip_parser\parser - - - Header Files\ip_parser\parser - - - Header Files\ip_parser\parser - - - Header Files\ip_parser\parser - - - Header Files\ip_parser\parser - - - Header Files\ip_parser\parser - - - Header Files\ip_parser\parser - - - Header Files\ip_parser\parser - - - Header Files\ip_parser\parser - - - Header Files\ip_parser\parser - - - Header Files\ip_parser\parser - - - Header Files\ip_parser\parser - - - Header Files\ip_parser\parser - - - Header Files\ip_parser\parser - - - Header Files\ip_parser\parser - - - Header Files\duktape - - - Header Files\duktape - - - Header Files\duktape - - - Header Files\effect - - - Header Files\effect - - - Header Files\persist - - - Header Files - - - Header Files - - - Header Files - - - Header Files - - - Header Files - - - Header Files - - - Header Files - - - Header Files - - - Header Files\model - - - Header Files\model - - - Header Files\model - - - Header Files\model - - - Header Files\model - - - Header Files\persist - - - Header Files\persist\file_adapter - - - Header Files - - - Header Files\ip_parser\exception - - - Header Files - - - Header Files - - - Header Files - - - Header Files - - - Header Files - - - Header Files - - - Header Files - - - Header Files - - - - - - \ No newline at end of file From 51e01fb67cd85f9cee8f8c7667020f7430fee682 Mon Sep 17 00:00:00 2001 From: "Yash Pandey (YP)" Date: Sun, 20 Jun 2021 15:51:31 +0530 Subject: [PATCH 3/4] test: Added ABACData Signed-off-by: Yash Pandey (YP) --- casbin/abac_data.cpp | 2 +- casbin/abac_data.h | 1 + casbin/casbin.h | 1 + tests/built_in_functions_test.cpp | 2 +- tests/config_test.cpp | 2 +- tests/enforcer_cached_test.cpp | 6 +++++- tests/enforcer_synced_test.cpp | 4 ++++ tests/enforcer_test.cpp | 30 ++++++++++++++++++++++++++++ tests/management_api_test.cpp | 2 +- tests/model_enforcer_test.cpp | 2 +- tests/model_test.cpp | 2 +- tests/rbac_api_test.cpp | 2 +- tests/rbac_api_with_domains_test.cpp | 2 +- tests/role_manager_test.cpp | 2 +- tests/util_test.cpp | 2 +- 15 files changed, 51 insertions(+), 11 deletions(-) diff --git a/casbin/abac_data.cpp b/casbin/abac_data.cpp index 6eaed23a..dc82aad6 100644 --- a/casbin/abac_data.cpp +++ b/casbin/abac_data.cpp @@ -36,7 +36,7 @@ namespace casbin { * Key's type is std::string and value's type can be one of std::string, int32_t, and float only * @return Pointer to casbin::ABACData entity */ -static const std::shared_ptr GetData(const ABACData::VariantMap& attribs) { +const std::shared_ptr GetData(const ABACData::VariantMap& attribs) { return std::make_shared(attribs); } diff --git a/casbin/abac_data.h b/casbin/abac_data.h index 1285aa1c..c896dce3 100644 --- a/casbin/abac_data.h +++ b/casbin/abac_data.h @@ -104,6 +104,7 @@ class ABACData { // Casbin ABAC entity type typedef ABACData ABACData; +const std::shared_ptr GetData(const ABACData::VariantMap& attribs); } #endif diff --git a/casbin/casbin.h b/casbin/casbin.h index 0525d69c..73a985d6 100644 --- a/casbin/casbin.h +++ b/casbin/casbin.h @@ -25,3 +25,4 @@ #include "util.h" #include "exception.h" #include "rbac.h" +#include "abac_data.h" diff --git a/tests/built_in_functions_test.cpp b/tests/built_in_functions_test.cpp index e588c157..1992803d 100644 --- a/tests/built_in_functions_test.cpp +++ b/tests/built_in_functions_test.cpp @@ -164,4 +164,4 @@ TEST(TestBuiltInFunctions, TestIPMatch) { TestIPMatchFn("11.0.0.123", "10.0.0.0/8", false); } -} \ No newline at end of file +} // namespace diff --git a/tests/config_test.cpp b/tests/config_test.cpp index 1af7c68d..4de35d5f 100644 --- a/tests/config_test.cpp +++ b/tests/config_test.cpp @@ -42,4 +42,4 @@ TEST(TestConfig, TestRedis) { ASSERT_EQ(std::string("push2"), values[1]); } -} \ No newline at end of file +} // namespace diff --git a/tests/enforcer_cached_test.cpp b/tests/enforcer_cached_test.cpp index 6242c0b9..9a0cf80f 100644 --- a/tests/enforcer_cached_test.cpp +++ b/tests/enforcer_cached_test.cpp @@ -19,6 +19,8 @@ #include #include +namespace { + TEST(TestEnforcerCached, TestCache) { std::string model = "../../examples/basic_model.conf"; std::string policy = "../../examples/basic_policy.csv"; @@ -43,4 +45,6 @@ TEST(TestEnforcerCached, TestCache) { ASSERT_EQ(e.Enforce({ "alice", "data1", "write" }), false); ASSERT_EQ(e.Enforce({ "alice", "data2", "read" }), false); ASSERT_EQ(e.Enforce({ "alice", "data2", "write" }), false); -} \ No newline at end of file +} + +} // namespace diff --git a/tests/enforcer_synced_test.cpp b/tests/enforcer_synced_test.cpp index 844b9e97..14b8176f 100644 --- a/tests/enforcer_synced_test.cpp +++ b/tests/enforcer_synced_test.cpp @@ -19,6 +19,8 @@ #include #include +namespace { + // void TestSyncFn(casbin::SyncedEnforcer& e, const std::string& sub, const std::string& obj, const std::string& act, bool control) { // bool response = e.Enforce({ sub, obj, act }); // ASSERT_EQ(response, control); @@ -72,3 +74,5 @@ // EXPECT_EQ(e.IsAutoLoadingRunning(), false); // } + +} // namespace diff --git a/tests/enforcer_test.cpp b/tests/enforcer_test.cpp index 562feca2..6df00786 100644 --- a/tests/enforcer_test.cpp +++ b/tests/enforcer_test.cpp @@ -19,6 +19,8 @@ #include #include +namespace { + TEST(TestEnforcer, TestFourParams) { std::string model = "../../examples/rbac_with_domains_model.conf"; std::string policy = "../../examples/rbac_with_domains_policy.csv"; @@ -93,3 +95,31 @@ TEST(TestEnforcer, TestMapParams) { params = { {"sub","bob"},{"obj","data2"},{"act","write"} }; ASSERT_EQ(e.Enforce(params), true); } + +TEST(TestEnforcer, ABACData) { + casbin::ABACData::VariantMap params = { + { "Name", "Yash" }, + { "Grade", 8.6f }, + { "Age", 18 }, + }; + + auto data = casbin::GetData(params); + EXPECT_EQ(params, data->GetAttributes()); + + data->DeleteAttribute("Name"); + params = { + { "Grade", 8.6f }, + { "Age", 18 }, + }; + EXPECT_EQ(params, data->GetAttributes()); + + data->AddAttribute("ID", 156); + params["ID"] = 156; + EXPECT_EQ(params, data->GetAttributes()); + + data->UpdateAttribute("ID", 152); + params["ID"] = 152; + EXPECT_EQ(params, data->GetAttributes()); +} + +} // namespace diff --git a/tests/management_api_test.cpp b/tests/management_api_test.cpp index c609c5c9..0af16e77 100644 --- a/tests/management_api_test.cpp +++ b/tests/management_api_test.cpp @@ -284,4 +284,4 @@ TEST(TestManagementAPI, TestModifyGroupingPolicyAPI) { ASSERT_TRUE(casbin::ArrayEquals({"data4_admin"}, e.GetRolesForUser("admin"))); } -} \ No newline at end of file +} // namespace diff --git a/tests/model_enforcer_test.cpp b/tests/model_enforcer_test.cpp index 04f4787e..49751107 100644 --- a/tests/model_enforcer_test.cpp +++ b/tests/model_enforcer_test.cpp @@ -756,4 +756,4 @@ func TestCommentModel(t* testing.T) { } */ -} +} // namespace diff --git a/tests/model_test.cpp b/tests/model_test.cpp index bdb6255c..4b37aac5 100644 --- a/tests/model_test.cpp +++ b/tests/model_test.cpp @@ -97,4 +97,4 @@ TEST(TestModel, TestModel_AddDef) { ASSERT_FALSE(ok); } -} \ No newline at end of file +} // namespace diff --git a/tests/rbac_api_test.cpp b/tests/rbac_api_test.cpp index 4271fc0c..1feb9e2f 100644 --- a/tests/rbac_api_test.cpp +++ b/tests/rbac_api_test.cpp @@ -225,4 +225,4 @@ TEST(TestRBACAPI, TestImplicitUserAPI) { ASSERT_TRUE(casbin::ArrayEquals({ "alice", "bob" }, e.GetImplicitUsersForPermission({ "data1", "read" }))); } -} +} // namespace diff --git a/tests/rbac_api_with_domains_test.cpp b/tests/rbac_api_with_domains_test.cpp index 92f55d58..7b6f2f4a 100644 --- a/tests/rbac_api_with_domains_test.cpp +++ b/tests/rbac_api_with_domains_test.cpp @@ -189,4 +189,4 @@ TEST(TestRBACAPIWithDomains, TestPermissionAPIInDomain) { TestGetPermissionsInDomain(e, "non_exist", "domain2", {}); } -} +} // namespace diff --git a/tests/role_manager_test.cpp b/tests/role_manager_test.cpp index 95dbfdc6..986b6da3 100644 --- a/tests/role_manager_test.cpp +++ b/tests/role_manager_test.cpp @@ -192,4 +192,4 @@ TEST(TestRoleManager, TestClear) { TestRole(rm, "u4", "g3", false); } -} +} // namespace diff --git a/tests/util_test.cpp b/tests/util_test.cpp index 3b5e3624..9c6bbec2 100644 --- a/tests/util_test.cpp +++ b/tests/util_test.cpp @@ -67,4 +67,4 @@ TEST(TestModel, TestArrayEquals) { TestArrayEqualsFn({"a", "b", "c"}, {}, false); } -} \ No newline at end of file +} // namespace From 70efa6faedca8fd7785505447f3f4a60e0439e63 Mon Sep 17 00:00:00 2001 From: "Yash Pandey (YP)" Date: Sun, 20 Jun 2021 16:01:54 +0530 Subject: [PATCH 4/4] fix: Build on macOS Signed-off-by: Yash Pandey (YP) --- tests/enforcer_test.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/enforcer_test.cpp b/tests/enforcer_test.cpp index 6df00786..d578ffce 100644 --- a/tests/enforcer_test.cpp +++ b/tests/enforcer_test.cpp @@ -104,22 +104,22 @@ TEST(TestEnforcer, ABACData) { }; auto data = casbin::GetData(params); - EXPECT_EQ(params, data->GetAttributes()); + ASSERT_TRUE(params == data->GetAttributes()); data->DeleteAttribute("Name"); params = { { "Grade", 8.6f }, { "Age", 18 }, }; - EXPECT_EQ(params, data->GetAttributes()); + ASSERT_TRUE(params == data->GetAttributes()); data->AddAttribute("ID", 156); params["ID"] = 156; - EXPECT_EQ(params, data->GetAttributes()); + ASSERT_TRUE(params == data->GetAttributes()); data->UpdateAttribute("ID", 152); params["ID"] = 152; - EXPECT_EQ(params, data->GetAttributes()); + ASSERT_TRUE(params == data->GetAttributes()); } } // namespace