Skip to content

Commit 61143d9

Browse files
authored
Merge pull request #94 from EmperorYP7/synced-enforcer
feat: Added Tests for SyncedEnforcer
2 parents b84ed34 + 0a0a48a commit 61143d9

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

test/test.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@
169169
<ClCompile Include="test_config.cpp" />
170170
<ClCompile Include="test_enforcer.cpp" />
171171
<ClCompile Include="test_enforcer_cached.cpp" />
172+
<ClCompile Include="test_enforcer_synced.cpp" />
172173
<ClCompile Include="test_management_api.cpp" />
173174
<ClCompile Include="test_model.cpp" />
174175
<ClCompile Include="test_model_enforcer.cpp" />

test/test.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@
5151
<ClCompile Include="test_enforcer_cached.cpp">
5252
<Filter>Source Files</Filter>
5353
</ClCompile>
54+
<ClCompile Include="test_enforcer_synced.cpp">
55+
<Filter>Source Files</Filter>
56+
</ClCompile>
5457
</ItemGroup>
5558
<ItemGroup>
5659
<ClInclude Include="pch.h">

test/test_enforcer_synced.cpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#include "pch.h"
2+
3+
#ifndef TEST_ENFORCER_SYNCED_CPP
4+
#define TEST_ENFORCER_SYNCED_CPP
5+
6+
#include <enforcer_synced.h>
7+
8+
using namespace std;
9+
10+
namespace test_enforcer_synced {
11+
TEST_CLASS(TestEnforcerSynced){
12+
public:
13+
14+
void testEnforceSync(SyncedEnforcer & e, string sub, string obj, string act, bool res){
15+
Assert::AreEqual(res, e.Enforce({sub, obj, act}));
16+
}
17+
18+
void testAutoLoadRunning(bool test, bool control) {
19+
Assert::AreEqual(test, control);
20+
}
21+
22+
TEST_METHOD(TestSync) {
23+
string model = "../../examples/basic_model.conf";
24+
string policy = "../../examples/basic_policy.csv";
25+
SyncedEnforcer e(model, policy);
26+
27+
chrono::duration<int64_t, nano> t = 200ms;
28+
29+
e.StartAutoLoadPolicy(t);
30+
31+
testEnforceSync(e, "alice", "data1", "read", true);
32+
testEnforceSync(e, "alice", "data1", "write", false);
33+
testEnforceSync(e, "alice", "data2", "read", false);
34+
testEnforceSync(e, "alice", "data2", "write", false);
35+
testEnforceSync(e, "bob", "data1", "read", false);
36+
testEnforceSync(e, "bob", "data1", "write", false);
37+
testEnforceSync(e, "bob", "data2", "read", false);
38+
testEnforceSync(e, "bob", "data2", "write", true);
39+
40+
e.StopAutoLoadPolicy();
41+
}
42+
43+
TEST_METHOD(TestStopLoadPolicy) {
44+
string model = "../../examples/basic_model.conf";
45+
string policy = "../../examples/basic_policy.csv";
46+
SyncedEnforcer e(model, policy);
47+
48+
chrono::duration<int64_t, nano> t = 5ms;
49+
50+
e.StartAutoLoadPolicy(t);
51+
52+
testAutoLoadRunning(e.IsAutoLoadingRunning(), true);
53+
54+
testEnforceSync(e, "alice", "data1", "read", true);
55+
testEnforceSync(e, "alice", "data1", "write", false);
56+
testEnforceSync(e, "alice", "data2", "read", false);
57+
testEnforceSync(e, "alice", "data2", "write", false);
58+
testEnforceSync(e, "bob", "data1", "read", false);
59+
testEnforceSync(e, "bob", "data1", "write", false);
60+
testEnforceSync(e, "bob", "data2", "read", false);
61+
testEnforceSync(e, "bob", "data2", "write", true);
62+
63+
e.StopAutoLoadPolicy();
64+
this_thread::sleep_for(10ms);
65+
66+
testAutoLoadRunning(e.IsAutoLoadingRunning(), false);
67+
68+
}
69+
};
70+
}
71+
72+
#endif // TEST_ENFORCER_SYNCED_CPP

0 commit comments

Comments
 (0)