From 4204a10b40aa6a409fe689bfa0bba697bef2c64e Mon Sep 17 00:00:00 2001 From: Sagilio Date: Wed, 17 Feb 2021 05:16:40 +0800 Subject: [PATCH] test: Add custom function model test Signed-off-by: Sagilio --- .../Fixtures/TestModelFixture.cs | 1 + NetCasbin.UnitTest/ModelTest.cs | 19 +++++++++++++++++++ NetCasbin.UnitTest/NetCasbin.UnitTest.csproj | 3 +++ .../examples/keymatch_custom_model.conf | 11 +++++++++++ NetCasbin/ManagementEnforcer.cs | 10 ++++++++++ 5 files changed, 44 insertions(+) create mode 100644 NetCasbin.UnitTest/examples/keymatch_custom_model.conf diff --git a/NetCasbin.UnitTest/Fixtures/TestModelFixture.cs b/NetCasbin.UnitTest/Fixtures/TestModelFixture.cs index 360a0de0..ae710289 100644 --- a/NetCasbin.UnitTest/Fixtures/TestModelFixture.cs +++ b/NetCasbin.UnitTest/Fixtures/TestModelFixture.cs @@ -16,6 +16,7 @@ public class TestModelFixture internal readonly string _ipMatchModelText = ReadTestFile("ipmatch_model.conf"); internal readonly string _keyMatchModelText = ReadTestFile("keymatch_model.conf"); internal readonly string _keyMatch2ModelText = ReadTestFile("keymatch2_model.conf"); + internal readonly string _keyMatchCustomModelText = ReadTestFile("keymatch_custom_model.conf"); internal readonly string _priorityModelText = ReadTestFile("priority_model.conf"); internal readonly string _rbacModelText = ReadTestFile("rbac_model.conf"); internal readonly string _rbacWithDenyModelText = ReadTestFile("rbac_with_deny_model.conf"); diff --git a/NetCasbin.UnitTest/ModelTest.cs b/NetCasbin.UnitTest/ModelTest.cs index 3274bffb..e3943fc4 100644 --- a/NetCasbin.UnitTest/ModelTest.cs +++ b/NetCasbin.UnitTest/ModelTest.cs @@ -477,6 +477,25 @@ public void TestKeyMatch2Model() TestEnforce(e, "alice", "/alice_data2/myid/using/res_id", "GET", true); } + [Fact] + public void TestKeyMatchCustomModel() + { + static bool CustomFunction(string key1, string key2) + { + return key1 is "/alice_data2/myid/using/res_id" && key2 is "/alice_data/:resource" + || key1 is "/alice_data2/myid/using/res_id" && key2 is "/alice_data2/:id/using/:resId"; + } + + var e = new Enforcer(TestModelFixture.GetNewTestModel( + _testModelFixture._keyMatchCustomModelText, + _testModelFixture._keyMatch2PolicyText)); + + e.AddFunction("keyMatchCustom", CustomFunction); + + TestEnforce(e, "alice", "/alice_data2/myid", "GET", false); + TestEnforce(e, "alice", "/alice_data2/myid/using/res_id", "GET", true); + } + public class TestResource { public TestResource(string name, string owner) diff --git a/NetCasbin.UnitTest/NetCasbin.UnitTest.csproj b/NetCasbin.UnitTest/NetCasbin.UnitTest.csproj index cd8e335f..187818d8 100644 --- a/NetCasbin.UnitTest/NetCasbin.UnitTest.csproj +++ b/NetCasbin.UnitTest/NetCasbin.UnitTest.csproj @@ -79,6 +79,9 @@ PreserveNewest + + PreserveNewest + PreserveNewest diff --git a/NetCasbin.UnitTest/examples/keymatch_custom_model.conf b/NetCasbin.UnitTest/examples/keymatch_custom_model.conf new file mode 100644 index 00000000..1cad8bfd --- /dev/null +++ b/NetCasbin.UnitTest/examples/keymatch_custom_model.conf @@ -0,0 +1,11 @@ +[request_definition] +r = sub, obj, act + +[policy_definition] +p = sub, obj, act + +[policy_effect] +e = some(where (p.eft == allow)) + +[matchers] +m = r.sub == p.sub && keyMatchCustom(r.obj, p.obj) && regexMatch(r.act, p.act) \ No newline at end of file diff --git a/NetCasbin/ManagementEnforcer.cs b/NetCasbin/ManagementEnforcer.cs index 748d3e90..cc95e0a6 100644 --- a/NetCasbin/ManagementEnforcer.cs +++ b/NetCasbin/ManagementEnforcer.cs @@ -988,5 +988,15 @@ public void AddFunction(string name, Delegate function) { ExpressionHandler.SetFunction(name, function); } + + /// + /// Adds a customized function. + /// + /// The name of the new function. + /// The function. + public void AddFunction(string name, Func function) + { + AddFunction(name, (Delegate) function); + } } }