Skip to content

Commit 5f73b84

Browse files
committed
test: Added Enforcer tests
Signed-off-by: Yash Pandey (YP) <[email protected]>
1 parent bbfdd66 commit 5f73b84

File tree

8 files changed

+140
-5
lines changed

8 files changed

+140
-5
lines changed

casbin/CMakeLists.txt

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
1-
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
1+
# Copyright 2020 The casbin Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
214

315
FILE(GLOB_RECURSE SRC_FILES "*.cpp" "*.h")
416

@@ -20,3 +32,9 @@ install(
2032
TARGETS casbin
2133
DESTINATION lib
2234
)
35+
36+
install(
37+
DIRECTORY ${CMAKE_SOURCE_DIR}/casbin
38+
DESTINATION ${CMAKE_SOURCE_DIR}/lib
39+
FILES_MATCHING PATTERN "*.h"
40+
)

casbin/enforcer_synced.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ void SyncedEnforcer ::StartAutoLoadPolicy(std::chrono::duration<int64_t, std::na
110110
SyncedEnforcer::LoadPolicy();
111111
++n;
112112
};
113-
ticker = std::unique_ptr<Ticker>(new Ticker(onTick, t));
113+
ticker = std::make_unique<Ticker>(onTick, t);
114114
n = 1;
115115
ticker->start();
116116
}

casbin/util/ticker.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class Ticker {
4444
void timer_loop();
4545
on_tick_t _onTick;
4646
tick_interval_t _tickInterval;
47-
std::atomic_bool _running;
47+
std::atomic_bool _running;
4848
std::mutex _tickIntervalMutex;
4949
future_vec _futures1;
5050
future_vec _futures2;

tests/CMakeLists.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
set(CMAKE_CXX_STANDARD 17)
16+
1517
add_executable(
1618
casbintest
17-
test_enforcer.cpp
18-
main.cpp
19+
enforcer_test.cpp
20+
enforcer_cached_test.cpp
21+
enforcer_synced_test.cpp
1922
)
2023

2124
target_include_directories(casbintest PUBLIC ${CMAKE_SOURCE_DIR})

tests/enforcer_cached_test.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2020 The casbin Authors. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* This is a test file showcasing the workflow of casbin::CachedEnforcer
17+
*/
18+
19+
#include <gtest/gtest.h>
20+
#include <casbin/casbin.h>
21+
22+
TEST(TestEnforcerCached, TestCache) {
23+
std::string model = "../../examples/basic_model.conf";
24+
std::string policy = "../../examples/basic_policy.csv";
25+
casbin::CachedEnforcer e(model, policy);
26+
ASSERT_EQ(e.Enforce({ "alice", "data1", "read" }), true);
27+
ASSERT_EQ(e.Enforce({ "alice", "data1", "write" }), false);
28+
ASSERT_EQ(e.Enforce({ "alice", "data2", "read" }), false);
29+
ASSERT_EQ(e.Enforce({ "alice", "data2", "write" }), false);
30+
31+
// The cache is enabled, so even if we remove a policy rule, the decision
32+
// for ("alice", "data1", "read") will still be true, as it uses the cached result.
33+
e.RemovePolicy({ "alice", "data1", "read" });
34+
ASSERT_EQ(e.Enforce({ "alice", "data1", "read" }), true);
35+
ASSERT_EQ(e.Enforce({ "alice", "data1", "write" }), false);
36+
ASSERT_EQ(e.Enforce({ "alice", "data2", "read" }), false);
37+
ASSERT_EQ(e.Enforce({ "alice", "data2", "write" }), false);
38+
39+
// Now we invalidate the cache, then all first-coming Enforce() has to be evaluated in real-time.
40+
// The decision for ("alice", "data1", "read") will be false now.
41+
e.InvalidateCache();
42+
ASSERT_EQ(e.Enforce({ "alice", "data1", "read" }), false);
43+
ASSERT_EQ(e.Enforce({ "alice", "data1", "write" }), false);
44+
ASSERT_EQ(e.Enforce({ "alice", "data2", "read" }), false);
45+
ASSERT_EQ(e.Enforce({ "alice", "data2", "write" }), false);
46+
}

tests/enforcer_synced_test.cpp

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright 2020 The casbin Authors. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* This is a test file showcasing the workflow of casbin::CachedEnforcer
17+
*/
18+
19+
#include <gtest/gtest.h>
20+
#include <casbin/casbin.h>
21+
22+
// TEST(TestEnforcerSynced, TestSync) {
23+
// std::string model = "../../examples/basic_model.conf";
24+
// std::string policy = "../../examples/basic_policy.csv";
25+
// casbin::SyncedEnforcer e(model, policy);
26+
27+
// using namespace std::literals::chrono_literals;
28+
// auto time1 = 200ms;
29+
// e.StartAutoLoadPolicy(time1);
30+
31+
// EXPECT_TRUE(e.Enforce({ "alice", "data1", "read" }));
32+
// EXPECT_FALSE(e.Enforce({ "alice", "data1", "write" }));
33+
// EXPECT_FALSE(e.Enforce({ "alice", "data2", "read" }));
34+
// EXPECT_FALSE(e.Enforce({ "alice", "data2", "write" }));
35+
// EXPECT_FALSE(e.Enforce({ "bob", "data1", "read" }));
36+
// EXPECT_FALSE(e.Enforce({ "bob", "data1", "write" }));
37+
// EXPECT_FALSE(e.Enforce({ "bob", "data2", "read" }));
38+
// EXPECT_TRUE(e.Enforce({ "bob", "data2", "write" }));
39+
40+
// e.StopAutoLoadPolicy();
41+
// }
42+
43+
// TEST(TestEnforcerSynced, TestStopLoadPolicy) {
44+
// std::string model = "../../examples/basic_model.conf";
45+
// std::string policy = "../../examples/basic_policy.csv";
46+
// casbin::SyncedEnforcer e(model, policy);
47+
48+
// using namespace std::literals::chrono_literals;
49+
// std::chrono::duration<int64_t, std::nano> t = 5ms;
50+
51+
// e.StartAutoLoadPolicy(t);
52+
53+
// EXPECT_EQ(e.IsAutoLoadingRunning(), true);
54+
55+
// ASSERT_EQ(e.Enforce({ "alice", "data1", "read" }), true);
56+
// ASSERT_EQ(e.Enforce({ "alice", "data1", "write" }), false);
57+
// ASSERT_EQ(e.Enforce({ "alice", "data2", "read" }), false);
58+
// ASSERT_EQ(e.Enforce({ "alice", "data2", "write" }), false);
59+
// ASSERT_EQ(e.Enforce({ "bob", "data1", "read" }), false);
60+
// ASSERT_EQ(e.Enforce({ "bob", "data1", "write" }), false);
61+
// ASSERT_EQ(e.Enforce({ "bob", "data2", "read" }), false);
62+
// ASSERT_EQ(e.Enforce({ "bob", "data2", "write" }), true);
63+
64+
// e.StopAutoLoadPolicy();
65+
// std::this_thread::sleep_for(10ms);
66+
67+
// EXPECT_EQ(e.IsAutoLoadingRunning(), false);
68+
// }
File renamed without changes.

tests/main.cpp

Whitespace-only changes.

0 commit comments

Comments
 (0)