|
| 1 | +using System.IO; |
| 2 | +using System.Threading.Tasks; |
| 3 | +using Casbin.Model; |
| 4 | +using Casbin.Persist; |
| 5 | +using Casbin.Persist.Adapter.File; |
| 6 | +using Xunit; |
| 7 | + |
| 8 | +namespace Casbin.UnitTests.PersistTests; |
| 9 | + |
| 10 | +public class IncrementalFilteredAdapterTest |
| 11 | +{ |
| 12 | + [Fact] |
| 13 | + public void TestLoadIncrementalFilteredPolicy() |
| 14 | + { |
| 15 | + Enforcer e = new("Examples/rbac_model.conf"); |
| 16 | + Assert.False(e.Enforce("alice", "data1", "read")); |
| 17 | + |
| 18 | + FileAdapter a = new("Examples/rbac_policy.csv"); |
| 19 | + e.SetAdapter(a); |
| 20 | + |
| 21 | + // Load only p policies for alice |
| 22 | + e.LoadFilteredPolicy(new PolicyFilter(PermConstants.DefaultPolicyType, 0, Policy.ValuesFrom(["alice"]))); |
| 23 | + |
| 24 | + // alice can read data1 (from p policy) |
| 25 | + Assert.True(e.Enforce("alice", "data1", "read")); |
| 26 | + // bob cannot write data2 (not loaded yet) |
| 27 | + Assert.False(e.Enforce("bob", "data2", "write")); |
| 28 | + |
| 29 | + // Incrementally load p policies for data2_admin role |
| 30 | + e.LoadIncrementalFilteredPolicy(new PolicyFilter(PermConstants.DefaultPolicyType, 0, Policy.ValuesFrom(["data2_admin"]))); |
| 31 | + |
| 32 | + // alice still can only read data1 (no role link yet) |
| 33 | + Assert.True(e.Enforce("alice", "data1", "read")); |
| 34 | + Assert.False(e.Enforce("alice", "data2", "read")); |
| 35 | + |
| 36 | + // Incrementally load g policies for alice |
| 37 | + e.LoadIncrementalFilteredPolicy(new PolicyFilter(PermConstants.DefaultRoleType, 0, Policy.ValuesFrom(["alice"]))); |
| 38 | + |
| 39 | + // Now alice can read data2 through role inheritance |
| 40 | + Assert.True(e.Enforce("alice", "data2", "read")); |
| 41 | + Assert.True(e.Enforce("alice", "data2", "write")); |
| 42 | + // bob still cannot write data2 (not loaded) |
| 43 | + Assert.False(e.Enforce("bob", "data2", "write")); |
| 44 | + } |
| 45 | + |
| 46 | + [Fact] |
| 47 | + public async Task TestLoadIncrementalFilteredPolicyAsync() |
| 48 | + { |
| 49 | + Enforcer e = new("Examples/rbac_model.conf"); |
| 50 | + Assert.False(await e.EnforceAsync("alice", "data1", "read")); |
| 51 | + |
| 52 | + FileAdapter a = new("Examples/rbac_policy.csv"); |
| 53 | + e.SetAdapter(a); |
| 54 | + |
| 55 | + // Load only p policies for alice |
| 56 | + await e.LoadFilteredPolicyAsync(new PolicyFilter(PermConstants.DefaultPolicyType, 0, Policy.ValuesFrom(["alice"]))); |
| 57 | + |
| 58 | + // alice can read data1 (from p policy) |
| 59 | + Assert.True(await e.EnforceAsync("alice", "data1", "read")); |
| 60 | + // bob cannot write data2 (not loaded yet) |
| 61 | + Assert.False(await e.EnforceAsync("bob", "data2", "write")); |
| 62 | + |
| 63 | + // Incrementally load p policies for data2_admin role |
| 64 | + await e.LoadIncrementalFilteredPolicyAsync(new PolicyFilter(PermConstants.DefaultPolicyType, 0, Policy.ValuesFrom(["data2_admin"]))); |
| 65 | + |
| 66 | + // alice still can only read data1 (no role link yet) |
| 67 | + Assert.True(await e.EnforceAsync("alice", "data1", "read")); |
| 68 | + Assert.False(await e.EnforceAsync("alice", "data2", "read")); |
| 69 | + |
| 70 | + // Incrementally load g policies for alice |
| 71 | + await e.LoadIncrementalFilteredPolicyAsync(new PolicyFilter(PermConstants.DefaultRoleType, 0, Policy.ValuesFrom(["alice"]))); |
| 72 | + |
| 73 | + // Now alice can read data2 through role inheritance |
| 74 | + Assert.True(await e.EnforceAsync("alice", "data2", "read")); |
| 75 | + Assert.True(await e.EnforceAsync("alice", "data2", "write")); |
| 76 | + // bob still cannot write data2 (not loaded) |
| 77 | + Assert.False(await e.EnforceAsync("bob", "data2", "write")); |
| 78 | + } |
| 79 | + |
| 80 | + [Fact] |
| 81 | + public void TestLoadIncrementalFilteredPolicyMultiplePTypes() |
| 82 | + { |
| 83 | + Enforcer e = new("Examples/rbac_model.conf"); |
| 84 | + Assert.False(e.Enforce("alice", "data1", "read")); |
| 85 | + |
| 86 | + FileAdapter a = new("Examples/rbac_policy.csv"); |
| 87 | + e.SetAdapter(a); |
| 88 | + |
| 89 | + // Load only p policies for alice |
| 90 | + e.LoadFilteredPolicy(new PolicyFilter(PermConstants.DefaultPolicyType, 0, Policy.ValuesFrom(["alice"]))); |
| 91 | + |
| 92 | + // alice can read data1 |
| 93 | + Assert.True(e.Enforce("alice", "data1", "read")); |
| 94 | + // bob cannot write data2 |
| 95 | + Assert.False(e.Enforce("bob", "data2", "write")); |
| 96 | + |
| 97 | + // Incrementally load p policies for bob |
| 98 | + e.LoadIncrementalFilteredPolicy(new PolicyFilter(PermConstants.DefaultPolicyType, 0, Policy.ValuesFrom(["bob"]))); |
| 99 | + |
| 100 | + // alice still can read data1 |
| 101 | + Assert.True(e.Enforce("alice", "data1", "read")); |
| 102 | + // Now bob can write data2 |
| 103 | + Assert.True(e.Enforce("bob", "data2", "write")); |
| 104 | + } |
| 105 | + |
| 106 | + [Fact] |
| 107 | + public async Task TestLoadIncrementalFilteredPolicyMultiplePTypesAsync() |
| 108 | + { |
| 109 | + Enforcer e = new("Examples/rbac_model.conf"); |
| 110 | + Assert.False(await e.EnforceAsync("alice", "data1", "read")); |
| 111 | + |
| 112 | + FileAdapter a = new("Examples/rbac_policy.csv"); |
| 113 | + e.SetAdapter(a); |
| 114 | + |
| 115 | + // Load only p policies for alice |
| 116 | + await e.LoadFilteredPolicyAsync(new PolicyFilter(PermConstants.DefaultPolicyType, 0, Policy.ValuesFrom(["alice"]))); |
| 117 | + |
| 118 | + // alice can read data1 |
| 119 | + Assert.True(await e.EnforceAsync("alice", "data1", "read")); |
| 120 | + // bob cannot write data2 |
| 121 | + Assert.False(await e.EnforceAsync("bob", "data2", "write")); |
| 122 | + |
| 123 | + // Incrementally load p policies for bob |
| 124 | + await e.LoadIncrementalFilteredPolicyAsync(new PolicyFilter(PermConstants.DefaultPolicyType, 0, Policy.ValuesFrom(["bob"]))); |
| 125 | + |
| 126 | + // alice still can read data1 |
| 127 | + Assert.True(await e.EnforceAsync("alice", "data1", "read")); |
| 128 | + // Now bob can write data2 |
| 129 | + Assert.True(await e.EnforceAsync("bob", "data2", "write")); |
| 130 | + } |
| 131 | + |
| 132 | + [Fact] |
| 133 | + public void TestLoadFilteredPolicyClearsExistingPolicies() |
| 134 | + { |
| 135 | + Enforcer e = new("Examples/rbac_model.conf"); |
| 136 | + FileAdapter a = new("Examples/rbac_policy.csv"); |
| 137 | + e.SetAdapter(a); |
| 138 | + |
| 139 | + // Load only p policies for alice |
| 140 | + e.LoadFilteredPolicy(new PolicyFilter(PermConstants.DefaultPolicyType, 0, Policy.ValuesFrom(["alice"]))); |
| 141 | + Assert.True(e.Enforce("alice", "data1", "read")); |
| 142 | + |
| 143 | + // Load filtered policy again (should clear previous policies) |
| 144 | + e.LoadFilteredPolicy(new PolicyFilter(PermConstants.DefaultPolicyType, 0, Policy.ValuesFrom(["bob"]))); |
| 145 | + |
| 146 | + // alice can no longer read data1 (previous policies cleared) |
| 147 | + Assert.False(e.Enforce("alice", "data1", "read")); |
| 148 | + // bob can write data2 (new filtered policies loaded) |
| 149 | + Assert.True(e.Enforce("bob", "data2", "write")); |
| 150 | + } |
| 151 | + |
| 152 | + [Fact] |
| 153 | + public async Task TestLoadFilteredPolicyClearsExistingPoliciesAsync() |
| 154 | + { |
| 155 | + Enforcer e = new("Examples/rbac_model.conf"); |
| 156 | + FileAdapter a = new("Examples/rbac_policy.csv"); |
| 157 | + e.SetAdapter(a); |
| 158 | + |
| 159 | + // Load only p policies for alice |
| 160 | + await e.LoadFilteredPolicyAsync(new PolicyFilter(PermConstants.DefaultPolicyType, 0, Policy.ValuesFrom(["alice"]))); |
| 161 | + Assert.True(await e.EnforceAsync("alice", "data1", "read")); |
| 162 | + |
| 163 | + // Load filtered policy again (should clear previous policies) |
| 164 | + await e.LoadFilteredPolicyAsync(new PolicyFilter(PermConstants.DefaultPolicyType, 0, Policy.ValuesFrom(["bob"]))); |
| 165 | + |
| 166 | + // alice can no longer read data1 (previous policies cleared) |
| 167 | + Assert.False(await e.EnforceAsync("alice", "data1", "read")); |
| 168 | + // bob can write data2 (new filtered policies loaded) |
| 169 | + Assert.True(await e.EnforceAsync("bob", "data2", "write")); |
| 170 | + } |
| 171 | +} |
0 commit comments