|
| 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