Skip to content

Commit 8540825

Browse files
authored
Merge pull request #107 from EmperorYP7/ctest-setup
feat: Initiated CTest setup
2 parents f5c0ef9 + a5019e1 commit 8540825

File tree

12 files changed

+363
-7
lines changed

12 files changed

+363
-7
lines changed

.github/workflows/ci.yml

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,15 @@ jobs:
3333
id: building-lib
3434
run: |
3535
cd build && cmake --build . --config Debug --target all -j 10 --
36+
- name: Tests
37+
id: test-lib
38+
run: |
39+
cd build
40+
ctest -j10 -C Debug -T test --output-on-failure -T test --output-on-failure
3641
- name: Cleanup
3742
id: clean-up
3843
run: |
39-
rm -r build lib
44+
rm -r build
4045
4146
windows:
4247
name: "Windows 10 (MSVC 19.29)"
@@ -55,12 +60,16 @@ jobs:
5560
id: building-lib
5661
run: |
5762
cd build
58-
cmake --build . --config Release --target casbin -j 10 --
63+
cmake --build . --config Debug --target casbin gtest gtest_main gmock gmock_main casbintest -j 10 --
64+
- name: Tests
65+
id: test-lib
66+
run: |
67+
cd build
68+
ctest -j10 -C Debug -T test --output-on-failure -T test --output-on-failure
5969
- name: Cleanup
6070
id: clean-up
6171
run: |
6272
rm -r build
63-
rm -r lib
6473
6574
macos:
6675
name: "macOS Catalina 10.15 (AppleClang 12.0)"
@@ -76,7 +85,12 @@ jobs:
7685
- name: Building library
7786
id: building-lib
7887
run: |
79-
cd build && cmake --build . --config Debug --target all -j 10 --
88+
cd build && cmake --build . --config Debug --target all install -j 10 --
89+
- name: Tests
90+
id: test-lib
91+
run: |
92+
cd build
93+
ctest -j10 -C Debug -T test --output-on-failure -T test --output-on-failure
8094
- name: Cleanup
8195
id: clean-up
8296
run: |

CMakeLists.txt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
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.
14+
115
cmake_minimum_required(VERSION 3.19)
216

17+
set(CMAKE_MODULE_PATH
18+
${CMAKE_MODULE_PATH}
19+
${CMAKE_SOURCE_DIR}/cmake/modules
20+
)
21+
322
set(CMAKE_WARN_DEPRECATED ON)
423

524
if(APPLE AND NOT DEFINED CMAKE_OSX_DEPLOYMENT_TARGET)
@@ -34,6 +53,9 @@ if(NOT DEFINED CMAKE_INSTALL_MESSAGE)
3453
set(CMAKE_INSTALL_MESSAGE "NEVER")
3554
endif()
3655

56+
enable_testing()
57+
add_subdirectory(tests)
58+
3759
# Change the path max size to avoid problem on Windows.
3860
if(NOT DEFINED CMAKE_OBJECT_PATH_MAX)
3961
set(CMAKE_OBJECT_PATH_MAX 300)
@@ -42,4 +64,12 @@ endif()
4264
# Setting to C++ standard to C++17
4365
set(CMAKE_CXX_STANDARD 17)
4466

67+
###############################################################################
68+
# Find or install external dependencies
69+
# Some required targets may be created by third-party CMake configs, which
70+
# don't generally produce global targets. To guarantee all imported targets are
71+
# global, this module is included at the project root level.
72+
73+
include(FindExtPackages)
74+
4575
add_subdirectory(casbin)

casbin/CMakeLists.txt

Lines changed: 26 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

@@ -7,9 +19,22 @@ include_directories(${CMAKE_SOURCE_DIR}/casbin)
719

820
target_precompile_headers(casbin PUBLIC "pch.h")
921

22+
set(CMAKE_INSTALL_PREFIX ${CMAKE_SOURCE_DIR})
23+
1024
set_target_properties(casbin PROPERTIES PREFIX "")
1125
if(WIN32 OR MSVC)
1226
set_target_properties(casbin PROPERTIES SUFFIX ".lib")
1327
elseif(UNIX)
1428
set_target_properties(casbin PROPERTIES SUFFIX ".a")
1529
endif()
30+
31+
install(
32+
TARGETS casbin
33+
DESTINATION lib
34+
)
35+
36+
install(
37+
DIRECTORY ${CMAKE_SOURCE_DIR}/casbin
38+
DESTINATION ${CMAKE_SOURCE_DIR}/lib
39+
FILES_MATCHING PATTERN "*.h"
40+
)

casbin/casbin.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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 file is the root header which is to be included by the client to use
17+
* casbin in C++ environment
18+
*/
19+
20+
#include "enforcer.h"
21+
#include "enforcer_cached.h"
22+
#include "enforcer_synced.h"

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;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
###############################################################################
2+
### Global package options ###
3+
4+
set(CMAKE_FIND_PACKAGE_NO_PACKAGE_REGISTRY ON CACHE BOOL
5+
"Disable CMake User Package Registry when finding packages")
6+
7+
set(CMAKE_FIND_PACKAGE_NO_SYSTEM_PACKAGE_REGISTRY ON CACHE BOOL
8+
"Disable CMake System Package Registry when finding packages")
9+
10+
###############################################################################
11+
### Packages and versions ###
12+
13+
# googletest
14+
# https://github.com/google/googletest
15+
find_package(googletest 1.11.0 REQUIRED)

cmake/modules/Findgoogletest.cmake

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
include(FetchContent)
2+
FetchContent_Declare(
3+
googletest
4+
URL https://github.com/google/googletest/archive/refs/tags/release-1.11.0.zip
5+
)
6+
# For Windows: Prevent overriding the parent project's compiler/linker settings
7+
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
8+
FetchContent_MakeAvailable(googletest)

tests/CMakeLists.txt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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.
14+
15+
set(CMAKE_CXX_STANDARD 17)
16+
17+
add_executable(
18+
casbintest
19+
enforcer_test.cpp
20+
enforcer_cached_test.cpp
21+
enforcer_synced_test.cpp
22+
)
23+
24+
target_include_directories(casbintest PUBLIC ${CMAKE_SOURCE_DIR})
25+
26+
target_link_libraries(
27+
casbintest
28+
gtest_main
29+
casbin
30+
)
31+
32+
include(GoogleTest)
33+
gtest_discover_tests(casbintest)

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

0 commit comments

Comments
 (0)