Skip to content

Commit

Permalink
feat: keep compatible with unexpected policy, revert PR: #337 (#350)
Browse files Browse the repository at this point in the history
Signed-off-by: sagilio <sagilio0728@gmail.com>
  • Loading branch information
sagilio0728 committed Apr 22, 2024
1 parent 9cecadd commit c340c97
Show file tree
Hide file tree
Showing 21 changed files with 1,207 additions and 709 deletions.
8 changes: 4 additions & 4 deletions Casbin.Benchmark/DefaultPolicyManagerBenchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,17 @@ public void GlobalSetup()
if (num == 0)
{
NowTestExistedPolicyList.Add(
new PolicyValues($"group{i}", $"obj{i / 10}", "read"));
new PolicyValues<string, string, string>($"group{i}", $"obj{i / 10}", "read"));
NowTestNullPolicyList.Add(
new PolicyValues($"name{i}", $"data{i / 10}", "read"));
new PolicyValues<string, string, string>($"name{i}", $"data{i / 10}", "read"));
}
}

Console.WriteLine($"// Already set {NowPolicyCount} policies.");

NowTestUserName = $"name{NowPolicyCount / 2 + 1}";
NowTestDataName = $"data{NowPolicyCount / 2 + 1}";
NowTestPolicy = new PolicyValues(NowTestUserName, NowTestDataName, "read");
NowTestPolicy = new PolicyValues<string, string, string>(NowTestUserName, NowTestDataName, "read");
Console.WriteLine($"// Already set user name to {NowTestUserName}.");
Console.WriteLine($"// Already set data name to {NowTestDataName}.");
}
Expand All @@ -83,7 +83,7 @@ public async Task RemovePolicyAsync()
public async Task UpdatePolicyAsync()
{
await _policyManager.UpdatePolicyAsync(NowTestPolicy,
new PolicyValues(NowTestUserName + "up", NowTestDataName + "up", "read"));
new PolicyValues<string, string, string>(NowTestUserName + "up", NowTestDataName + "up", "read"));
}

[Benchmark]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Collections.Generic;
using Casbin.Model;
using Xunit;

namespace Casbin.UnitTests.Extensions;

internal static class ManagementEnforcerExtension
{
internal static void TestGetPolicy(this IEnforcer e, IReadOnlyList<IPolicyValues> exceptedValues)
{
IEnumerable<IEnumerable<string>> actualValues = e.GetPolicy();
Assert.True(exceptedValues.DeepEquals(actualValues));
}
}
18 changes: 18 additions & 0 deletions Casbin.UnitTests/Extensions/Model/RequestValuesExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Collections.Generic;
using Casbin.Model;

namespace Casbin.UnitTests.Extensions;

internal static class RequestValuesExtension
{
internal static IEnumerable<string> ToEnumerable(this IRequestValues values)
{
string[] res = new string[values.Count];
for (int i = 0; i < values.Count; i++)
{
res[i] = values[i];
}

return res;
}
}
18 changes: 0 additions & 18 deletions Casbin.UnitTests/Extensions/RequestValuesExtension.cs

This file was deleted.

33 changes: 16 additions & 17 deletions Casbin.UnitTests/GenericTests/GenericMatcherTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,25 @@ public class GenericMatcherTest
[Fact]
public void TestGenericMatcher()
{
//RequestValues<string, int> r = Request.CreateValues("A", 1);
//PolicyValues p = Policy.CreateValues("A", 1);
//Func<RequestValues<string, int>, PolicyValues, bool> func1 = Compile(
// "r.Value1 == p[0].GetType()(p[0]) && r.Value2 == p[1].GetType()(p[1])",
// nameof(r), in r, nameof(p), in p);
RequestValues<string, int> r = Request.CreateValues("A", 1);
PolicyValues<string, int> p = Policy.CreateValues("A", 1);
Func<RequestValues<string, int>, PolicyValues<string, int>, bool> func1 = Compile(
"r.Value1 == p.Value1 && r.Value2 == p.Value2",
nameof(r), in r, nameof(p), in p);

//Assert.True(func1(Request.CreateValues("A", 1), Policy.CreateValues("A", 1)));
//Assert.False(func1(Request.CreateValues("A", 1), Policy.CreateValues("A", 2)));
//Assert.False(func1(Request.CreateValues("B", 1), Policy.CreateValues("B", 2)));
Assert.True(func1(Request.CreateValues("A", 1), Policy.CreateValues("A", 1)));
Assert.False(func1(Request.CreateValues("A", 1), Policy.CreateValues("A", 2)));
Assert.False(func1(Request.CreateValues("B", 1), Policy.CreateValues("B", 2)));

//RequestValues<string, int, string> r2 = Request.CreateValues("A", 1, "read");
//PolicyValues p2 = Policy.CreateValues("A", 1, "read");
//Func<RequestValues<string, int, string>, PolicyValues, bool> func2 = Compile(
// "r2.Value1 == p2[0] && r2.Value2 == p2[1] && r2.Value3 == p2[2]",
// nameof(r2), in r2, nameof(p2), in p2);
RequestValues<string, int, string> r2 = Request.CreateValues("A", 1, "read");
PolicyValues<string, int, string> p2 = Policy.CreateValues("A", 1, "read");
Func<RequestValues<string, int, string>, PolicyValues<string, int, string>, bool> func2 = Compile(
"r2.Value1 == p2.Value1 && r2.Value2 == p2.Value2 && r2.Value3 == p2.Value3",
nameof(r2), in r2, nameof(p2), in p2);

//Assert.True(func2(Request.CreateValues("A", 1, "read"), Policy.CreateValues("A", 1, "read")));
//Assert.False(func2(Request.CreateValues("A", 1, "read"), Policy.CreateValues("A", 2, "read")));
//Assert.False(func2(Request.CreateValues("B", 1, "read"), Policy.CreateValues("B", 2, "read")));
Assert.True(1==1);
Assert.True(func2(Request.CreateValues("A", 1, "read"), Policy.CreateValues("A", 1, "read")));
Assert.False(func2(Request.CreateValues("A", 1, "read"), Policy.CreateValues("A", 2, "read")));
Assert.False(func2(Request.CreateValues("B", 1, "read"), Policy.CreateValues("B", 2, "read")));
}

private static Func<TRequest, TPolicy, bool> Compile<TRequest, TPolicy>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
namespace Casbin.UnitTests.GenericTests;

[Collection("Model collection")]
public class SupportCountTests
public class SupportCountTest
{
private readonly TestModelFixture _testModelFixture;

public SupportCountTests(TestModelFixture testModelFixture) => _testModelFixture = testModelFixture;
public SupportCountTest(TestModelFixture testModelFixture) => _testModelFixture = testModelFixture;

[Fact]
public void TestSupportCount()
Expand Down Expand Up @@ -59,60 +59,74 @@ private static void TestEnforce(IEnforcer enforcer, EnforceContext context, int
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, new[] { "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" }));
Assert.True(enforcer.Enforce(context,
new[] { "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" }));
Assert.True(enforcer.Enforce(context,
new[] { "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" }));
Assert.True(enforcer.Enforce(context,
new[] { "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" }));
Assert.True(enforcer.Enforce(context,
new[]
{
"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", "value10" }));
Assert.True(enforcer.Enforce(context,
new[]
{
"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", "value10", "value11" }));
Assert.True(enforcer.Enforce(context,
new[]
{
"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", "value10", "value11", "value12" }));
Assert.True(enforcer.Enforce(context,
new[]
{
"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",
"value4", "value5", "value6", "value7", "value8", "value9",
"value10", "value11", "value12", "value13")));
Assert.True(enforcer.Enforce(context, new[] { "value1", "value2", "value3",
"value4", "value5", "value6", "value7", "value8", "value9", "value10", "value11", "value12", "value13" }));
Assert.True(enforcer.Enforce(context, "value1", "value2", "value3", "value4", "value5", "value6",
"value7", "value8", "value9", "value10", "value11", "value12", "value13"));
break;
default:
throw new ArgumentOutOfRangeException(nameof(requestCount));
Expand Down
35 changes: 35 additions & 0 deletions Casbin.UnitTests/ModelTests/ManagementApiTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Casbin.Model;
using Casbin.UnitTests.Extensions;
using Casbin.UnitTests.Fixtures;
using Xunit;
using static Casbin.UnitTests.Util.TestUtil;
Expand Down Expand Up @@ -551,4 +553,37 @@ public async Task TestModifyGroupingPolicyAsync()
TestGetRoles(e, "admin", AsList("data5_admin"));
TestGetRoles(e, "eve", AsList("admin_groups"));
}

[Fact]
public void TestModifySpecialPolicy()
{
Enforcer e = new(TestModelFixture.GetNewTestModel(_testModelFixture._rbacModelText));

e.AddPolicy("alice", "data1");
e.AddPolicy("alice", "data1", "read");
e.AddPolicy("alice", "data1", "read", "dump1");

e.TestGetPolicy(Policy.ValuesListFrom(new[]
{
Policy.CreateValues("alice", "data1", ""), Policy.CreateValues("alice", "data1", "read")
}
));
}

[Fact]
public async Task TestModifySpecialPolicyAsync()
{
Enforcer e = new(_testModelFixture.GetNewRbacTestModel());
e.ClearPolicy();

await e.AddPolicyAsync("alice", "data1");
await e.AddPolicyAsync("alice", "data1", "read");
await e.AddPolicyAsync("alice", "data1", "read", "dump1");

e.TestGetPolicy(Policy.ValuesListFrom(new[]
{
Policy.CreateValues("alice", "data1", ""), Policy.CreateValues("alice", "data1", "read")
}
));
}
}
25 changes: 16 additions & 9 deletions Casbin.UnitTests/Util/TestUtil.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Casbin.Rbac;
using Casbin.Model;
using Casbin.Rbac;
using Casbin.Util;
using Xunit;

namespace Casbin.UnitTests.Util;

public static class TestUtil
internal static class TestUtil
{
internal static List<T> AsList<T>(params T[] values) => values.ToList();

Expand All @@ -29,10 +29,13 @@ public static class TestUtil
internal static void TestBatchEnforce<T>(IEnforcer e, IEnumerable<(T, bool)> values) where T : IRequestValues =>
Assert.True(values.Select(x => x.Item2).SequenceEqual(e.BatchEnforce(values.Select(x => x.Item1))));

internal static void TestParallelBatchEnforce<T>(Enforcer e, IEnumerable<(T, bool)> values) where T : IRequestValues =>
Assert.True(values.Select(x => x.Item2).SequenceEqual(e.ParallelBatchEnforce(values.Select(x => x.Item1).ToList())));
internal static void TestParallelBatchEnforce<T>(Enforcer e, IEnumerable<(T, bool)> values)
where T : IRequestValues =>
Assert.True(values.Select(x => x.Item2)
.SequenceEqual(e.ParallelBatchEnforce(values.Select(x => x.Item1).ToList())));

internal static async void TestBatchEnforceAsync<T>(IEnforcer e, IEnumerable<(T, bool)> values) where T : IRequestValues
internal static async void TestBatchEnforceAsync<T>(IEnforcer e, IEnumerable<(T, bool)> values)
where T : IRequestValues
{
#if !NET452
var res = e.BatchEnforceAsync(values.Select(x => x.Item1));
Expand Down Expand Up @@ -63,13 +66,17 @@ await foreach (var item in res)

internal static void TestBatchEnforceWithMatcher<T>(this IEnforcer e, string matcher, IEnumerable<(T, bool)> values)
where T : IRequestValues =>
Assert.True(values.Select(x => x.Item2).SequenceEqual(e.BatchEnforceWithMatcher(matcher, values.Select(x => x.Item1))));
Assert.True(values.Select(x => x.Item2)
.SequenceEqual(e.BatchEnforceWithMatcher(matcher, values.Select(x => x.Item1))));

internal static void TestBatchEnforceWithMatcherParallel<T>(this Enforcer e, string matcher, IEnumerable<(T, bool)> values)
internal static void TestBatchEnforceWithMatcherParallel<T>(this Enforcer e, string matcher,
IEnumerable<(T, bool)> values)
where T : IRequestValues =>
Assert.True(values.Select(x => x.Item2).SequenceEqual(e.BatchEnforceWithMatcherParallel<T>(matcher, values.Select(x => x.Item1).ToList())));
Assert.True(values.Select(x => x.Item2)
.SequenceEqual(e.BatchEnforceWithMatcherParallel<T>(matcher, values.Select(x => x.Item1).ToList())));

internal static async void TestBatchEnforceWithMatcherAsync<T>(IEnforcer e, string matcher, IEnumerable<(T, bool)> values)
internal static async void TestBatchEnforceWithMatcherAsync<T>(IEnforcer e, string matcher,
IEnumerable<(T, bool)> values)
where T : IRequestValues
{
#if !NET452
Expand Down
7 changes: 2 additions & 5 deletions Casbin/Abstractions/Model/IPolicyValues.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;

namespace Casbin.Model;

public interface IPolicyValues : IList<string>
public interface IPolicyValues : IReadOnlyList<string>
{
public new string this[int index] { get; }

public string ToText();

public bool Equals(IPolicyValues other);

int GetRealCount();
}
6 changes: 6 additions & 0 deletions Casbin/Abstractions/Model/IReadOnlyPolicyStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ public interface IReadOnlyPolicyStore

public bool ContainsNode(string section, string policyType);

public int GetRequiredValuesCount(string section, string policyType);

public bool ValidatePolicy(string section, string policyType, IPolicyValues values);

public bool ValidatePolicies(string section, string policyType, IReadOnlyList<IPolicyValues> valuesList);

public PolicyScanner Scan(string section, string policyType);

public IEnumerable<IPolicyValues> GetPolicy(string section, string policyType);
Expand Down
3 changes: 1 addition & 2 deletions Casbin/EnforceView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,7 @@ public static string TransformMatcher(in EnforceView view, string matcher)
foreach (KeyValuePair<string, int> tokenPair in view.PolicyAssertion.Tokens)
{
Regex reg = new Regex(perfix + $@"{view.PolicyType}\.{tokenPair.Key}" + suffix);
//matcher = reg.Replace(matcher, $"{view.PolicyType}.Value{tokenPair.Value + 1}");
matcher = reg.Replace(matcher, $"{view.PolicyType}[{tokenPair.Value}]");
matcher = reg.Replace(matcher, $"{view.PolicyType}.Value{tokenPair.Value + 1}");
}

return matcher;
Expand Down
Loading

0 comments on commit c340c97

Please sign in to comment.