Skip to content

Commit

Permalink
feat: support load policy from text (#372)
Browse files Browse the repository at this point in the history
* feat: support load policy from text

Signed-off-by: Taoyuesong <[email protected]>

* feat: add text adapter

Signed-off-by: Taoyuesong <[email protected]>

* feat: clean up test project

Signed-off-by: Taoyuesong <[email protected]>

* fix: remove not used set equal

Signed-off-by: Taoyuesong <[email protected]>

---------

Signed-off-by: Taoyuesong <[email protected]>
  • Loading branch information
Taoyuesong authored Oct 29, 2024
1 parent 0b477c0 commit 85b9330
Show file tree
Hide file tree
Showing 35 changed files with 1,871 additions and 2,163 deletions.
46 changes: 21 additions & 25 deletions Casbin.UnitTests/GenericTests/SupportCountTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,85 +37,81 @@ private static void TestEnforce(IEnforcer enforcer, EnforceContext context, int
{
case 1:
Assert.True(enforcer.Enforce(context, "value1"));
Assert.True(enforcer.Enforce(context, new[] { "value1" }));
Assert.True(enforcer.Enforce(context, ["value1"]));
break;
case 2:
Assert.True(enforcer.Enforce(context, "value1", "value2"));
Assert.True(enforcer.Enforce(context, new[] { "value1", "value2" }));
Assert.True(enforcer.Enforce(context, ["value1", "value2"]));
break;
case 3:
Assert.True(enforcer.Enforce(context, "value1", "value2", "value3"));
Assert.True(enforcer.Enforce(context, new[] { "value1", "value2", "value3" }));
Assert.True(enforcer.Enforce(context, ["value1", "value2", "value3"]));
break;
case 4:
Assert.True(enforcer.Enforce(context, "value1", "value2", "value3",
"value4"));
Assert.True(enforcer.Enforce(context, new[] { "value1", "value2", "value3", "value4" }));
Assert.True(enforcer.Enforce(context, ["value1", "value2", "value3", "value4"]));
break;
case 5:
Assert.True(enforcer.Enforce(context, "value1", "value2", "value3",
"value4", "value5"));
Assert.True(enforcer.Enforce(context, new[] { "value1", "value2", "value3", "value4", "value5" }));
Assert.True(enforcer.Enforce(context, ["value1", "value2", "value3", "value4", "value5"]));
break;
case 6:
Assert.True(enforcer.Enforce(context, "value1", "value2", "value3",
"value4", "value5", "value6"));
Assert.True(enforcer.Enforce(context,
new[] { "value1", "value2", "value3", "value4", "value5", "value6" }));
["value1", "value2", "value3", "value4", "value5", "value6"]));
break;
case 7:
Assert.True(enforcer.Enforce(context, "value1", "value2", "value3",
"value4", "value5", "value6", "value7"));
Assert.True(enforcer.Enforce(context,
new[] { "value1", "value2", "value3", "value4", "value5", "value6", "value7" }));
["value1", "value2", "value3", "value4", "value5", "value6", "value7"]));
break;
case 8:
Assert.True(enforcer.Enforce(context, "value1", "value2", "value3",
"value4", "value5", "value6", "value7", "value8"));
Assert.True(enforcer.Enforce(context,
new[] { "value1", "value2", "value3", "value4", "value5", "value6", "value7", "value8" }));
["value1", "value2", "value3", "value4", "value5", "value6", "value7", "value8"]));
break;
case 9:
Assert.True(enforcer.Enforce(context, "value1", "value2", "value3",
"value4", "value5", "value6", "value7", "value8", "value9"));
Assert.True(enforcer.Enforce(context,
new[]
{
"value1", "value2", "value3", "value4", "value5", "value6", "value7", "value8", "value9"
}));
[
"value1", "value2", "value3", "value4", "value5", "value6", "value7", "value8", "value9"
]));
break;
case 10:
Assert.True(enforcer.Enforce(context, "value1", "value2", "value3",
"value4", "value5", "value6", "value7", "value8", "value9",
"value10"));
Assert.True(enforcer.Enforce(context,
new[]
{
"value1", "value2", "value3", "value4", "value5", "value6", "value7", "value8", "value9",
[
"value1", "value2", "value3", "value4", "value5", "value6", "value7", "value8", "value9",
"value10"
}));
]));
break;
case 11:
Assert.True(enforcer.Enforce(context, "value1", "value2", "value3",
"value4", "value5", "value6", "value7", "value8", "value9",
"value10", "value11"));
Assert.True(enforcer.Enforce(context,
new[]
{
"value1", "value2", "value3", "value4", "value5", "value6", "value7", "value8", "value9",
[
"value1", "value2", "value3", "value4", "value5", "value6", "value7", "value8", "value9",
"value10", "value11"
}));
]));
break;
case 12:
Assert.True(enforcer.Enforce(context, "value1", "value2", "value3",
"value4", "value5", "value6", "value7", "value8", "value9",
"value10", "value11", "value12"));
Assert.True(enforcer.Enforce(context,
new[]
{
"value1", "value2", "value3", "value4", "value5", "value6", "value7", "value8", "value9",
[
"value1", "value2", "value3", "value4", "value5", "value6", "value7", "value8", "value9",
"value10", "value11", "value12"
}));
]));
break;
case 13:
Assert.True(enforcer.Enforce(context, Request.CreateValues("value1", "value2", "value3",
Expand All @@ -131,7 +127,7 @@ private static void TestEnforce(IEnforcer enforcer, EnforceContext context, int

private static IPolicyValues CreateTestPolicy(int count)
{
List<string> policy = new();
List<string> policy = [];
for (int i = 0; i < count; i++)
{
policy.Add($"value{i + 1}");
Expand Down
2 changes: 1 addition & 1 deletion Casbin.UnitTests/Mock/MockRoleManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class MockCustomRoleManager : IRoleManager

public IEnumerable<string> GetRoles(string name, string domain = null) => null;
public IEnumerable<string> GetUsers(string name, string domain = null) => null;
public IEnumerable<string> GetDomains(string name) => Enumerable.Empty<string>();
public IEnumerable<string> GetDomains(string name) => [];

public bool HasLink(string name1, string name2, string domain = null)
{
Expand Down
14 changes: 14 additions & 0 deletions Casbin.UnitTests/Mock/TestRescource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace Casbin.UnitTests.Mock;

public class TestResource
{
public TestResource(string name, string owner)
{
Name = name;
Owner = owner;
}

public string Name { get; }

public string Owner { get; }
}
14 changes: 14 additions & 0 deletions Casbin.UnitTests/Mock/TestSubject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace Casbin.UnitTests.Mock;

public class TestSubject
{
public TestSubject(string name, int age)
{
Name = name;
Age = age;
}

public string Name { get; }

public int Age { get; }
}
45 changes: 22 additions & 23 deletions Casbin.UnitTests/ModelTests/CachedEnforcerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,19 @@
using Casbin.UnitTests.Mock;
using Xunit;
using Xunit.Abstractions;
using static Casbin.UnitTests.Util.TestUtil;

namespace Casbin.UnitTests.ModelTests;

[Collection("Model collection")]
public class CachedEnforcerTest
{
private readonly TestModelFixture TestModelFixture;
private readonly TestModelFixture _testModelFixture;
private readonly ITestOutputHelper _testOutputHelper;

public CachedEnforcerTest(ITestOutputHelper testOutputHelper, TestModelFixture testModelFixture)
{
_testOutputHelper = testOutputHelper;
TestModelFixture = testModelFixture;
_testModelFixture = testModelFixture;
}

[Fact]
Expand All @@ -32,28 +31,28 @@ public void TestEnforceWithCache()
e.EnableCache(true);
e.EnableAutoCleanEnforceCache(false);

TestEnforce(e, "alice", "data1", "read", true);
TestEnforce(e, "alice", "data1", "write", false);
TestEnforce(e, "alice", "data2", "read", false);
TestEnforce(e, "alice", "data2", "write", false);
Assert.True(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"));

// 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");

TestEnforce(e, "alice", "data1", "read", true);
TestEnforce(e, "alice", "data1", "write", false);
TestEnforce(e, "alice", "data2", "read", false);
TestEnforce(e, "alice", "data2", "write", false);
Assert.True(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"));

// 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.EnforceCache.Clear();

TestEnforce(e, "alice", "data1", "read", false);
TestEnforce(e, "alice", "data1", "write", false);
TestEnforce(e, "alice", "data2", "read", false);
TestEnforce(e, "alice", "data2", "write", false);
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"));
}

[Fact]
Expand All @@ -69,18 +68,18 @@ public void TestAutoCleanCache()
#endif
e.EnableCache(true);

TestEnforce(e, "alice", "data1", "read", true);
TestEnforce(e, "alice", "data1", "write", false);
TestEnforce(e, "alice", "data2", "read", false);
TestEnforce(e, "alice", "data2", "write", false);
Assert.True(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"));

// 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");

TestEnforce(e, "alice", "data1", "read", false);
TestEnforce(e, "alice", "data1", "write", false);
TestEnforce(e, "alice", "data2", "read", false);
TestEnforce(e, "alice", "data2", "write", false);
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"));
}
}
Loading

0 comments on commit 85b9330

Please sign in to comment.