Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Casbin.UnitTests/Casbin.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
14 changes: 14 additions & 0 deletions Casbin.UnitTests/Examples/batch_model.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[request_definition]
r = sub, obj, act

[policy_definition]
p = sub, act, condition, eft

[role_definition]
g = _, _

[policy_effect]
e = some(where (p.eft == allow))

[matchers]
m = g(r.sub, p.sub) && eval(p.condition) && (r.act == p.act)
2 changes: 2 additions & 0 deletions Casbin.UnitTests/Examples/batch_policy.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
p, 77, Write, r.obj.type == "File" || r.obj.color== "Red", allow
p, 55, Read, r.obj.type == "View", allow
130 changes: 129 additions & 1 deletion Casbin.UnitTests/ModelTests/EnforcerTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Casbin.Model;
using Casbin.Persist;
Expand Down Expand Up @@ -1235,6 +1237,132 @@
await e.TestEnforceExWithMatcherAsync(matcher, "bob", "data2", "read", []);
await e.TestEnforceExWithMatcherAsync(matcher, "bob", "data2", "write", []);
}
public class ObjectRequest : IRequestValues
{
public string Sub { get; set; }
public Attributes Obj { get; set; }
public string Action { get; set; }

public bool TrySetValue<T>(int index, T value)
{
try
{
switch (index)
{
case 0:
Sub = value as string ?? throw new InvalidCastException();
break;
case 1:
Obj = value as Attributes ?? throw new InvalidCastException();
break;
case 2:
Action = value as string ?? throw new InvalidCastException();
break;
default:
return false;
}
return true;
}
catch
{
return false;
}
}
public string this[int index] => index switch
{
0 => Sub,
1 => Obj.ToString(),
2 => Action,
_ => throw new IndexOutOfRangeException()
};
public object? GetValue(int index) => index switch

Check warning on line 1278 in Casbin.UnitTests/ModelTests/EnforcerTest.cs

View workflow job for this annotation

GitHub Actions / InferSharp

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 1278 in Casbin.UnitTests/ModelTests/EnforcerTest.cs

View workflow job for this annotation

GitHub Actions / InferSharp

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 1278 in Casbin.UnitTests/ModelTests/EnforcerTest.cs

View workflow job for this annotation

GitHub Actions / InferSharp

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 1278 in Casbin.UnitTests/ModelTests/EnforcerTest.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 1278 in Casbin.UnitTests/ModelTests/EnforcerTest.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 1278 in Casbin.UnitTests/ModelTests/EnforcerTest.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
{
0 => Sub,
1 => Obj,
2 => Action,
_ => throw new IndexOutOfRangeException()
};

public int Count => 3;
}


public class Attributes : IRequestValues
{
public string type { get; set; }
public string color { get; set; }

public bool TrySetValue<T>(int index, T value)
{
try
{
switch (index)
{
case 0:
type = value as string ?? throw new InvalidCastException();
break;
case 1:
color = value as string ?? throw new InvalidCastException();
break;
default:
return false;
}
return true;
}
catch
{
return false;
}
}

public string this[int index] => index switch
{
0 => type,
1 => color,
_ => throw new IndexOutOfRangeException()
};

public object? GetValue(int index) => index switch

Check warning on line 1325 in Casbin.UnitTests/ModelTests/EnforcerTest.cs

View workflow job for this annotation

GitHub Actions / InferSharp

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 1325 in Casbin.UnitTests/ModelTests/EnforcerTest.cs

View workflow job for this annotation

GitHub Actions / InferSharp

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 1325 in Casbin.UnitTests/ModelTests/EnforcerTest.cs

View workflow job for this annotation

GitHub Actions / InferSharp

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 1325 in Casbin.UnitTests/ModelTests/EnforcerTest.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 1325 in Casbin.UnitTests/ModelTests/EnforcerTest.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 1325 in Casbin.UnitTests/ModelTests/EnforcerTest.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
{
0 => type,
1 => color,
_ => throw new IndexOutOfRangeException()
};

public int Count => 2;
}

[Fact]
public void TestBatchEnforceWithEvalShouldFail()
{
var e = new Enforcer("Examples/batch_model.conf", "Examples/batch_policy.csv");

var reqs = new List<ObjectRequest>
{
new ObjectRequest
{
Sub = "77",
Obj = new Attributes { type = "File", color = "Blue" },
Action = "Write"
},
new ObjectRequest
{
Sub = "77",
Obj = new Attributes { type = "Folder", color = "Red" },
Action = "Write"
}
};
var request1 = Request.CreateValues(reqs[0].Sub, reqs[0].Obj, reqs[0].Action);
var request2 = Request.CreateValues(reqs[1].Sub, reqs[1].Obj, reqs[1].Action);
RequestValues<string, Attributes, string>[] requests = new[] { request1, request2 };
var batchResult = e.BatchEnforce(requests).ToList();
Assert.All(batchResult, result => Assert.True(result));
foreach (var req in reqs)
{
bool result = e.Enforce(req.Sub, req.Obj, req.Action);
Assert.True(result);
}
}

#endregion
}
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
"rollForward": "latestMajor",
"allowPrerelease": true
}
}
}
Loading