-
-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #94 from EmperorYP7/synced-enforcer
feat: Added Tests for SyncedEnforcer
- Loading branch information
Showing
3 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |