From 0a0a48a38c752fa0737a0c11e9911a00475a7926 Mon Sep 17 00:00:00 2001 From: "Yash Pandey (YP)" Date: Fri, 2 Apr 2021 19:09:32 +0530 Subject: [PATCH] feat: added tests for synced enforcer Signed-off-by: Yash Pandey (YP) --- test/test.vcxproj | 1 + test/test.vcxproj.filters | 3 ++ test/test_enforcer_synced.cpp | 72 +++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+) create mode 100644 test/test_enforcer_synced.cpp diff --git a/test/test.vcxproj b/test/test.vcxproj index 99235b14..5eb7cf90 100644 --- a/test/test.vcxproj +++ b/test/test.vcxproj @@ -169,6 +169,7 @@ + diff --git a/test/test.vcxproj.filters b/test/test.vcxproj.filters index 4289d648..3300aca4 100644 --- a/test/test.vcxproj.filters +++ b/test/test.vcxproj.filters @@ -51,6 +51,9 @@ Source Files + + Source Files + diff --git a/test/test_enforcer_synced.cpp b/test/test_enforcer_synced.cpp new file mode 100644 index 00000000..f1f11e39 --- /dev/null +++ b/test/test_enforcer_synced.cpp @@ -0,0 +1,72 @@ +#include "pch.h" + +#ifndef TEST_ENFORCER_SYNCED_CPP +#define TEST_ENFORCER_SYNCED_CPP + +#include + +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 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 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