Skip to content

Commit

Permalink
Merge pull request #131 from Sagilio/master
Browse files Browse the repository at this point in the history
test: Add custom function model test
  • Loading branch information
hsluoyz authored Feb 17, 2021
2 parents 3932724 + 4204a10 commit c7ec287
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions NetCasbin.UnitTest/Fixtures/TestModelFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
19 changes: 19 additions & 0 deletions NetCasbin.UnitTest/ModelTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions NetCasbin.UnitTest/NetCasbin.UnitTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@
<None Update="examples\keymatch2_policy.csv">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="examples\keymatch_custom_model.conf">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="examples\keymatch_model.conf">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down
11 changes: 11 additions & 0 deletions NetCasbin.UnitTest/examples/keymatch_custom_model.conf
Original file line number Diff line number Diff line change
@@ -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)
10 changes: 10 additions & 0 deletions NetCasbin/ManagementEnforcer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -988,5 +988,15 @@ public void AddFunction(string name, Delegate function)
{
ExpressionHandler.SetFunction(name, function);
}

/// <summary>
/// Adds a customized function.
/// </summary>
/// <param name="name">The name of the new function.</param>
/// <param name="function">The function.</param>
public void AddFunction(string name, Func<string, string, bool> function)
{
AddFunction(name, (Delegate) function);
}
}
}

0 comments on commit c7ec287

Please sign in to comment.