11using System . IO ;
22using System . Threading . Tasks ;
3+ using Casbin . Model ;
4+ using Casbin . Persist ;
35using Casbin . Persist . Adapter . File ;
46using Casbin . Persist . Adapter . Stream ;
57using Casbin . Persist . Adapter . Text ;
@@ -49,13 +51,54 @@ public async Task TestFileAdapterAsync()
4951 Assert . True ( await e . EnforceAsync ( "bob" , "data2" , "write" ) ) ;
5052 }
5153
54+ [ Fact ]
55+ public void TestFilteredFileAdapter ( )
56+ {
57+ Enforcer e = new ( "Examples/basic_model.conf" ) ;
58+ Assert . False ( e . Enforce ( "alice" , "data1" , "read" ) ) ;
59+
60+ FileAdapter a = new ( "Examples/basic_policy.csv" ) ;
61+ e . SetAdapter ( a ) ;
62+ e . LoadFilteredPolicy ( new PolicyFilter ( PermConstants . DefaultPolicyType , 0 , Policy . ValuesFrom ( [ "bob" ] ) ) ) ;
63+
64+ Assert . False ( e . Enforce ( "alice" , "data1" , "read" ) ) ; // because "alice" is not in the filtered policy
65+ Assert . False ( e . Enforce ( "alice" , "data1" , "write" ) ) ;
66+ Assert . False ( e . Enforce ( "alice" , "data2" , "read" ) ) ;
67+ Assert . False ( e . Enforce ( "alice" , "data2" , "write" ) ) ;
68+ Assert . False ( e . Enforce ( "bob" , "data1" , "read" ) ) ;
69+ Assert . False ( e . Enforce ( "bob" , "data1" , "write" ) ) ;
70+ Assert . False ( e . Enforce ( "bob" , "data2" , "read" ) ) ;
71+ Assert . True ( e . Enforce ( "bob" , "data2" , "write" ) ) ;
72+ }
73+
74+ [ Fact ]
75+ public async Task TestFilteredFileAdapterAsync ( )
76+ {
77+ Enforcer e = new ( "Examples/basic_model.conf" ) ;
78+ Assert . False ( await e . EnforceAsync ( "alice" , "data1" , "read" ) ) ;
79+
80+ FileAdapter a = new ( "Examples/basic_policy.csv" ) ;
81+ e . SetAdapter ( a ) ;
82+ await e . LoadFilteredPolicyAsync ( new PolicyFilter ( PermConstants . DefaultPolicyType , 0 , Policy . ValuesFrom ( [ "bob" ] ) ) ) ;
83+
84+ Assert . False ( await e . EnforceAsync ( "alice" , "data1" , "read" ) ) ; // because "alice" is not in the filtered policy
85+ Assert . False ( await e . EnforceAsync ( "alice" , "data1" , "write" ) ) ;
86+ Assert . False ( await e . EnforceAsync ( "alice" , "data2" , "read" ) ) ;
87+ Assert . False ( await e . EnforceAsync ( "alice" , "data2" , "write" ) ) ;
88+ Assert . False ( await e . EnforceAsync ( "bob" , "data1" , "read" ) ) ;
89+ Assert . False ( await e . EnforceAsync ( "bob" , "data1" , "write" ) ) ;
90+ Assert . False ( await e . EnforceAsync ( "bob" , "data2" , "read" ) ) ;
91+ Assert . True ( await e . EnforceAsync ( "bob" , "data2" , "write" ) ) ;
92+ }
93+
5294 [ Fact ]
5395 public void TestStreamAdapter ( )
5496 {
5597 Enforcer e = new ( "Examples/basic_model.conf" ) ;
5698 Assert . False ( e . Enforce ( "alice" , "data1" , "read" ) ) ;
5799
58- StreamAdapter a = new ( File . OpenRead ( "Examples/basic_policy.csv" ) ) ;
100+ using FileStream stream = File . OpenRead ( "Examples/basic_policy.csv" ) ;
101+ StreamAdapter a = new ( stream ) ;
59102 e . SetAdapter ( a ) ;
60103 e . LoadPolicy ( ) ;
61104
@@ -75,7 +118,8 @@ public async Task TestStreamAdapterAsync()
75118 Enforcer e = new ( "Examples/basic_model.conf" ) ;
76119 Assert . False ( await e . EnforceAsync ( "alice" , "data1" , "read" ) ) ;
77120
78- StreamAdapter a = new ( File . OpenRead ( "Examples/basic_policy.csv" ) ) ;
121+ using FileStream stream = File . OpenRead ( "Examples/basic_policy.csv" ) ;
122+ StreamAdapter a = new ( stream ) ;
79123 e . SetAdapter ( a ) ;
80124 await e . LoadPolicyAsync ( ) ;
81125
@@ -89,6 +133,48 @@ public async Task TestStreamAdapterAsync()
89133 Assert . True ( await e . EnforceAsync ( "bob" , "data2" , "write" ) ) ;
90134 }
91135
136+ [ Fact ]
137+ public void TestFilteredStreamAdapter ( )
138+ {
139+ Enforcer e = new ( "Examples/basic_model.conf" ) ;
140+ Assert . False ( e . Enforce ( "alice" , "data1" , "read" ) ) ;
141+
142+ using FileStream stream = File . OpenRead ( "Examples/basic_policy.csv" ) ;
143+ StreamAdapter a = new ( stream ) ;
144+ e . SetAdapter ( a ) ;
145+ e . LoadFilteredPolicy ( new PolicyFilter ( PermConstants . DefaultPolicyType , 0 , Policy . ValuesFrom ( [ "bob" ] ) ) ) ;
146+
147+ Assert . False ( e . Enforce ( "alice" , "data1" , "read" ) ) ; // because "alice" is not in the filtered policy
148+ Assert . False ( e . Enforce ( "alice" , "data1" , "write" ) ) ;
149+ Assert . False ( e . Enforce ( "alice" , "data2" , "read" ) ) ;
150+ Assert . False ( e . Enforce ( "alice" , "data2" , "write" ) ) ;
151+ Assert . False ( e . Enforce ( "bob" , "data1" , "read" ) ) ;
152+ Assert . False ( e . Enforce ( "bob" , "data1" , "write" ) ) ;
153+ Assert . False ( e . Enforce ( "bob" , "data2" , "read" ) ) ;
154+ Assert . True ( e . Enforce ( "bob" , "data2" , "write" ) ) ;
155+ }
156+
157+ [ Fact ]
158+ public async Task TestFilteredStreamAdapterAsync ( )
159+ {
160+ Enforcer e = new ( "Examples/basic_model.conf" ) ;
161+ Assert . False ( await e . EnforceAsync ( "alice" , "data1" , "read" ) ) ;
162+
163+ using FileStream stream = File . OpenRead ( "Examples/basic_policy.csv" ) ;
164+ StreamAdapter a = new ( stream ) ;
165+ e . SetAdapter ( a ) ;
166+ await e . LoadFilteredPolicyAsync ( new PolicyFilter ( PermConstants . DefaultPolicyType , 0 , Policy . ValuesFrom ( [ "bob" ] ) ) ) ;
167+
168+ Assert . False ( await e . EnforceAsync ( "alice" , "data1" , "read" ) ) ; // because "alice" is not in the filtered policy
169+ Assert . False ( await e . EnforceAsync ( "alice" , "data1" , "write" ) ) ;
170+ Assert . False ( await e . EnforceAsync ( "alice" , "data2" , "read" ) ) ;
171+ Assert . False ( await e . EnforceAsync ( "alice" , "data2" , "write" ) ) ;
172+ Assert . False ( await e . EnforceAsync ( "bob" , "data1" , "read" ) ) ;
173+ Assert . False ( await e . EnforceAsync ( "bob" , "data1" , "write" ) ) ;
174+ Assert . False ( await e . EnforceAsync ( "bob" , "data2" , "read" ) ) ;
175+ Assert . True ( await e . EnforceAsync ( "bob" , "data2" , "write" ) ) ;
176+ }
177+
92178 [ Fact ]
93179 public void TestTextAdapter ( )
94180 {
@@ -132,4 +218,48 @@ public async Task TestTextAdapterAsync()
132218 Assert . False ( await e . EnforceAsync ( "bob" , "data2" , "read" ) ) ;
133219 Assert . True ( await e . EnforceAsync ( "bob" , "data2" , "write" ) ) ;
134220 }
221+
222+ [ Fact ]
223+ public void TestFilteredTextAdapter ( )
224+ {
225+ Enforcer e = new ( "Examples/basic_model.conf" ) ;
226+ Assert . False ( e . Enforce ( "alice" , "data1" , "read" ) ) ;
227+
228+ TextAdapter a = new ( File . ReadAllText ( "Examples/basic_policy.csv" ) ) ;
229+ e . SetAdapter ( a ) ;
230+ e . LoadFilteredPolicy ( new PolicyFilter ( PermConstants . DefaultPolicyType , 0 , Policy . ValuesFrom ( [ "bob" ] ) ) ) ;
231+
232+ Assert . False ( e . Enforce ( "alice" , "data1" , "read" ) ) ; // because "alice" is not in the filtered policy
233+ Assert . False ( e . Enforce ( "alice" , "data1" , "write" ) ) ;
234+ Assert . False ( e . Enforce ( "alice" , "data2" , "read" ) ) ;
235+ Assert . False ( e . Enforce ( "alice" , "data2" , "write" ) ) ;
236+ Assert . False ( e . Enforce ( "bob" , "data1" , "read" ) ) ;
237+ Assert . False ( e . Enforce ( "bob" , "data1" , "write" ) ) ;
238+ Assert . False ( e . Enforce ( "bob" , "data2" , "read" ) ) ;
239+ Assert . True ( e . Enforce ( "bob" , "data2" , "write" ) ) ;
240+ }
241+
242+ [ Fact ]
243+ public async Task TestFilteredTextAdapterAsync ( )
244+ {
245+ Enforcer e = new ( "Examples/basic_model.conf" ) ;
246+ Assert . False ( await e . EnforceAsync ( "alice" , "data1" , "read" ) ) ;
247+
248+ #if NET452 || NET461 || NET462
249+ TextAdapter a = new ( File . ReadAllText ( "Examples/basic_policy.csv" ) ) ;
250+ #else
251+ TextAdapter a = new ( await File . ReadAllTextAsync ( "Examples/basic_policy.csv" ) ) ;
252+ #endif
253+ e . SetAdapter ( a ) ;
254+ await e . LoadFilteredPolicyAsync ( new PolicyFilter ( PermConstants . DefaultPolicyType , 0 , Policy . ValuesFrom ( [ "bob" ] ) ) ) ;
255+
256+ Assert . False ( await e . EnforceAsync ( "alice" , "data1" , "read" ) ) ; // because "alice" is not in the filtered policy
257+ Assert . False ( await e . EnforceAsync ( "alice" , "data1" , "write" ) ) ;
258+ Assert . False ( await e . EnforceAsync ( "alice" , "data2" , "read" ) ) ;
259+ Assert . False ( await e . EnforceAsync ( "alice" , "data2" , "write" ) ) ;
260+ Assert . False ( await e . EnforceAsync ( "bob" , "data1" , "read" ) ) ;
261+ Assert . False ( await e . EnforceAsync ( "bob" , "data1" , "write" ) ) ;
262+ Assert . False ( await e . EnforceAsync ( "bob" , "data2" , "read" ) ) ;
263+ Assert . True ( await e . EnforceAsync ( "bob" , "data2" , "write" ) ) ;
264+ }
135265}
0 commit comments