Skip to content

Commit

Permalink
Merge pull request #94 from EmperorYP7/synced-enforcer
Browse files Browse the repository at this point in the history
feat: Added Tests for SyncedEnforcer
  • Loading branch information
hsluoyz authored Apr 5, 2021
2 parents b84ed34 + 0a0a48a commit 61143d9
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
1 change: 1 addition & 0 deletions test/test.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@
<ClCompile Include="test_config.cpp" />
<ClCompile Include="test_enforcer.cpp" />
<ClCompile Include="test_enforcer_cached.cpp" />
<ClCompile Include="test_enforcer_synced.cpp" />
<ClCompile Include="test_management_api.cpp" />
<ClCompile Include="test_model.cpp" />
<ClCompile Include="test_model_enforcer.cpp" />
Expand Down
3 changes: 3 additions & 0 deletions test/test.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@
<ClCompile Include="test_enforcer_cached.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="test_enforcer_synced.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="pch.h">
Expand Down
72 changes: 72 additions & 0 deletions test/test_enforcer_synced.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#include "pch.h"

#ifndef TEST_ENFORCER_SYNCED_CPP
#define TEST_ENFORCER_SYNCED_CPP

#include <enforcer_synced.h>

using namespace std;

namespace test_enforcer_synced {
TEST_CLASS(TestEnforcerSynced){
public:

void testEnforceSync(SyncedEnforcer & e, string sub, string obj, string act, bool res){
Assert::AreEqual(res, e.Enforce({sub, obj, act}));
}

void testAutoLoadRunning(bool test, bool control) {
Assert::AreEqual(test, control);
}

TEST_METHOD(TestSync) {
string model = "../../examples/basic_model.conf";
string policy = "../../examples/basic_policy.csv";
SyncedEnforcer e(model, policy);

chrono::duration<int64_t, nano> t = 200ms;

e.StartAutoLoadPolicy(t);

testEnforceSync(e, "alice", "data1", "read", true);
testEnforceSync(e, "alice", "data1", "write", false);
testEnforceSync(e, "alice", "data2", "read", false);
testEnforceSync(e, "alice", "data2", "write", false);
testEnforceSync(e, "bob", "data1", "read", false);
testEnforceSync(e, "bob", "data1", "write", false);
testEnforceSync(e, "bob", "data2", "read", false);
testEnforceSync(e, "bob", "data2", "write", true);

e.StopAutoLoadPolicy();
}

TEST_METHOD(TestStopLoadPolicy) {
string model = "../../examples/basic_model.conf";
string policy = "../../examples/basic_policy.csv";
SyncedEnforcer e(model, policy);

chrono::duration<int64_t, nano> t = 5ms;

e.StartAutoLoadPolicy(t);

testAutoLoadRunning(e.IsAutoLoadingRunning(), true);

testEnforceSync(e, "alice", "data1", "read", true);
testEnforceSync(e, "alice", "data1", "write", false);
testEnforceSync(e, "alice", "data2", "read", false);
testEnforceSync(e, "alice", "data2", "write", false);
testEnforceSync(e, "bob", "data1", "read", false);
testEnforceSync(e, "bob", "data1", "write", false);
testEnforceSync(e, "bob", "data2", "read", false);
testEnforceSync(e, "bob", "data2", "write", true);

e.StopAutoLoadPolicy();
this_thread::sleep_for(10ms);

testAutoLoadRunning(e.IsAutoLoadingRunning(), false);

}
};
}

#endif // TEST_ENFORCER_SYNCED_CPP

0 comments on commit 61143d9

Please sign in to comment.