Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Ensure tab characters in policy files are parsed as white-space #324

Merged
merged 3 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
tests: Add unit tests
  • Loading branch information
janpieterz committed Aug 3, 2023
commit a0e4475f0e8edaf1c9aab1db3129d34b3597f6b0
6 changes: 6 additions & 0 deletions Casbin.UnitTests/Casbin.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,12 @@
<None Update="Examples\rbac_comment.conf">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="examples\tabs_policy.csv">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="examples\tabs_model.conf">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="examples\tokens_with_substring_relation_rbac.csv">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down
5 changes: 5 additions & 0 deletions Casbin.UnitTests/Fixtures/TestModelFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public class TestModelFixture
//https://github.com/casbin/Casbin.NET/issues/310
internal readonly string _commaAndQuotationsModelText = ReadTestFile("comma_quotations_model.conf");
internal readonly string _commaAndQuotationsPolicyText = ReadTestFile("comma_quotations_policy.csv");
internal readonly string _tabsModelText = ReadTestFile("tabs_model.conf");
internal readonly string _tabsPolicyText = ReadTestFile("tabs_policy.csv");
internal readonly string _ipMatchModelText = ReadTestFile("ipmatch_model.conf");
internal readonly string _ipMatchPolicyText = ReadTestFile("ipmatch_policy.csv");
internal readonly string _keyMatch2ModelText = ReadTestFile("keymatch2_model.conf");
Expand Down Expand Up @@ -152,6 +154,9 @@ public IModel GetNewRbacWithResourceRoleTestModel() =>
public IModel GetNewCommaAndQuotationsModel() =>
GetNewTestModel(_commaAndQuotationsModelText, _commaAndQuotationsPolicyText);

public IModel GetNewTabsModel() =>
GetNewTestModel(_tabsModelText, _tabsPolicyText);

public static IModel GetNewTestModel(string modelText) => DefaultModel.CreateFromText(modelText);

public static IModel GetNewTestModel(string modelText, string policyText) =>
Expand Down
9 changes: 9 additions & 0 deletions Casbin.UnitTests/ModelTests/ModelTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,15 @@ public void TestModelWithCommaAndQuotations()
TestEnforce(e, "cindy", "\"\"Muti Quotations Test\"", "Get", false);
}

[Fact]
public void TestModelWithTabs()
{
Enforcer e = new Enforcer(_testModelFixture.GetNewTabsModel());
e.AddRoleForUserInDomain("/user/john", "admin", "/tenant/1");

Assert.True(e.Enforce("/user/john", "/tenant/1", "/tenant/1/resource", "Write"));
}

[Fact]
public void TestRbacTokensWithSubstringRelation()
{
Expand Down
15 changes: 15 additions & 0 deletions Casbin.UnitTests/examples/tabs_model.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[request_definition]
r = a, b, c, d

[policy_definition]
p = a, b, c, d
#p = wallet, ecosystem, resource, action

[role_definition]
g = _, _, _

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

[matchers]
m = (keyMatch2(r.a, p.a) || g(r.a, p.a, r.b)) && keyMatch2(r.c, p.c) && keyMatch(r.d, p.d) && keyMatch2(r.b, p.b) && keyGet2(r.b, p.b, "id") == keyGet2(r.c, p.c, "id")
1 change: 1 addition & 0 deletions Casbin.UnitTests/examples/tabs_policy.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
p, admin, /tenant/:id, /tenant/:id/*, *
Loading