Skip to content

Commit

Permalink
Merge pull request #107 from EmperorYP7/ctest-setup
Browse files Browse the repository at this point in the history
feat: Initiated CTest setup
  • Loading branch information
hsluoyz authored Jun 24, 2021
2 parents f5c0ef9 + a5019e1 commit 8540825
Show file tree
Hide file tree
Showing 12 changed files with 363 additions and 7 deletions.
22 changes: 18 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,15 @@ jobs:
id: building-lib
run: |
cd build && cmake --build . --config Debug --target all -j 10 --
- name: Tests
id: test-lib
run: |
cd build
ctest -j10 -C Debug -T test --output-on-failure -T test --output-on-failure
- name: Cleanup
id: clean-up
run: |
rm -r build lib
rm -r build
windows:
name: "Windows 10 (MSVC 19.29)"
Expand All @@ -55,12 +60,16 @@ jobs:
id: building-lib
run: |
cd build
cmake --build . --config Release --target casbin -j 10 --
cmake --build . --config Debug --target casbin gtest gtest_main gmock gmock_main casbintest -j 10 --
- name: Tests
id: test-lib
run: |
cd build
ctest -j10 -C Debug -T test --output-on-failure -T test --output-on-failure
- name: Cleanup
id: clean-up
run: |
rm -r build
rm -r lib
macos:
name: "macOS Catalina 10.15 (AppleClang 12.0)"
Expand All @@ -76,7 +85,12 @@ jobs:
- name: Building library
id: building-lib
run: |
cd build && cmake --build . --config Debug --target all -j 10 --
cd build && cmake --build . --config Debug --target all install -j 10 --
- name: Tests
id: test-lib
run: |
cd build
ctest -j10 -C Debug -T test --output-on-failure -T test --output-on-failure
- name: Cleanup
id: clean-up
run: |
Expand Down
30 changes: 30 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
# Copyright 2020 The casbin Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

cmake_minimum_required(VERSION 3.19)

set(CMAKE_MODULE_PATH
${CMAKE_MODULE_PATH}
${CMAKE_SOURCE_DIR}/cmake/modules
)

set(CMAKE_WARN_DEPRECATED ON)

if(APPLE AND NOT DEFINED CMAKE_OSX_DEPLOYMENT_TARGET)
Expand Down Expand Up @@ -34,6 +53,9 @@ if(NOT DEFINED CMAKE_INSTALL_MESSAGE)
set(CMAKE_INSTALL_MESSAGE "NEVER")
endif()

enable_testing()
add_subdirectory(tests)

# Change the path max size to avoid problem on Windows.
if(NOT DEFINED CMAKE_OBJECT_PATH_MAX)
set(CMAKE_OBJECT_PATH_MAX 300)
Expand All @@ -42,4 +64,12 @@ endif()
# Setting to C++ standard to C++17
set(CMAKE_CXX_STANDARD 17)

###############################################################################
# Find or install external dependencies
# Some required targets may be created by third-party CMake configs, which
# don't generally produce global targets. To guarantee all imported targets are
# global, this module is included at the project root level.

include(FindExtPackages)

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

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

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

target_precompile_headers(casbin PUBLIC "pch.h")

set(CMAKE_INSTALL_PREFIX ${CMAKE_SOURCE_DIR})

set_target_properties(casbin PROPERTIES PREFIX "")
if(WIN32 OR MSVC)
set_target_properties(casbin PROPERTIES SUFFIX ".lib")
elseif(UNIX)
set_target_properties(casbin PROPERTIES SUFFIX ".a")
endif()

install(
TARGETS casbin
DESTINATION lib
)

install(
DIRECTORY ${CMAKE_SOURCE_DIR}/casbin
DESTINATION ${CMAKE_SOURCE_DIR}/lib
FILES_MATCHING PATTERN "*.h"
)
22 changes: 22 additions & 0 deletions casbin/casbin.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright 2020 The casbin Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* This file is the root header which is to be included by the client to use
* casbin in C++ environment
*/

#include "enforcer.h"
#include "enforcer_cached.h"
#include "enforcer_synced.h"
2 changes: 1 addition & 1 deletion casbin/enforcer_synced.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ void SyncedEnforcer ::StartAutoLoadPolicy(std::chrono::duration<int64_t, std::na
SyncedEnforcer::LoadPolicy();
++n;
};
ticker = std::unique_ptr<Ticker>(new Ticker(onTick, t));
ticker = std::make_unique<Ticker>(onTick, t);
n = 1;
ticker->start();
}
Expand Down
2 changes: 1 addition & 1 deletion casbin/util/ticker.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class Ticker {
void timer_loop();
on_tick_t _onTick;
tick_interval_t _tickInterval;
std::atomic_bool _running;
std::atomic_bool _running;
std::mutex _tickIntervalMutex;
future_vec _futures1;
future_vec _futures2;
Expand Down
15 changes: 15 additions & 0 deletions cmake/modules/FindExtPackages.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
###############################################################################
### Global package options ###

set(CMAKE_FIND_PACKAGE_NO_PACKAGE_REGISTRY ON CACHE BOOL
"Disable CMake User Package Registry when finding packages")

set(CMAKE_FIND_PACKAGE_NO_SYSTEM_PACKAGE_REGISTRY ON CACHE BOOL
"Disable CMake System Package Registry when finding packages")

###############################################################################
### Packages and versions ###

# googletest
# https://github.com/google/googletest
find_package(googletest 1.11.0 REQUIRED)
8 changes: 8 additions & 0 deletions cmake/modules/Findgoogletest.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/refs/tags/release-1.11.0.zip
)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)
33 changes: 33 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Copyright 2020 The casbin Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set(CMAKE_CXX_STANDARD 17)

add_executable(
casbintest
enforcer_test.cpp
enforcer_cached_test.cpp
enforcer_synced_test.cpp
)

target_include_directories(casbintest PUBLIC ${CMAKE_SOURCE_DIR})

target_link_libraries(
casbintest
gtest_main
casbin
)

include(GoogleTest)
gtest_discover_tests(casbintest)
46 changes: 46 additions & 0 deletions tests/enforcer_cached_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2020 The casbin Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* This is a test file showcasing the workflow of casbin::CachedEnforcer
*/

#include <gtest/gtest.h>
#include <casbin/casbin.h>

TEST(TestEnforcerCached, TestCache) {
std::string model = "../../examples/basic_model.conf";
std::string policy = "../../examples/basic_policy.csv";
casbin::CachedEnforcer e(model, policy);
ASSERT_EQ(e.Enforce({ "alice", "data1", "read" }), true);
ASSERT_EQ(e.Enforce({ "alice", "data1", "write" }), false);
ASSERT_EQ(e.Enforce({ "alice", "data2", "read" }), false);
ASSERT_EQ(e.Enforce({ "alice", "data2", "write" }), false);

// The cache is enabled, so even if we remove a policy rule, the decision
// for ("alice", "data1", "read") will still be true, as it uses the cached result.
e.RemovePolicy({ "alice", "data1", "read" });
ASSERT_EQ(e.Enforce({ "alice", "data1", "read" }), true);
ASSERT_EQ(e.Enforce({ "alice", "data1", "write" }), false);
ASSERT_EQ(e.Enforce({ "alice", "data2", "read" }), false);
ASSERT_EQ(e.Enforce({ "alice", "data2", "write" }), false);

// Now we invalidate the cache, then all first-coming Enforce() has to be evaluated in real-time.
// The decision for ("alice", "data1", "read") will be false now.
e.InvalidateCache();
ASSERT_EQ(e.Enforce({ "alice", "data1", "read" }), false);
ASSERT_EQ(e.Enforce({ "alice", "data1", "write" }), false);
ASSERT_EQ(e.Enforce({ "alice", "data2", "read" }), false);
ASSERT_EQ(e.Enforce({ "alice", "data2", "write" }), false);
}
68 changes: 68 additions & 0 deletions tests/enforcer_synced_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright 2020 The casbin Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* This is a test file showcasing the workflow of casbin::CachedEnforcer
*/

#include <gtest/gtest.h>
#include <casbin/casbin.h>

// TEST(TestEnforcerSynced, TestSync) {
// std::string model = "../../examples/basic_model.conf";
// std::string policy = "../../examples/basic_policy.csv";
// casbin::SyncedEnforcer e(model, policy);

// using namespace std::literals::chrono_literals;
// auto time1 = 200ms;
// e.StartAutoLoadPolicy(time1);

// EXPECT_TRUE(e.Enforce({ "alice", "data1", "read" }));
// EXPECT_FALSE(e.Enforce({ "alice", "data1", "write" }));
// EXPECT_FALSE(e.Enforce({ "alice", "data2", "read" }));
// EXPECT_FALSE(e.Enforce({ "alice", "data2", "write" }));
// EXPECT_FALSE(e.Enforce({ "bob", "data1", "read" }));
// EXPECT_FALSE(e.Enforce({ "bob", "data1", "write" }));
// EXPECT_FALSE(e.Enforce({ "bob", "data2", "read" }));
// EXPECT_TRUE(e.Enforce({ "bob", "data2", "write" }));

// e.StopAutoLoadPolicy();
// }

// TEST(TestEnforcerSynced, TestStopLoadPolicy) {
// std::string model = "../../examples/basic_model.conf";
// std::string policy = "../../examples/basic_policy.csv";
// casbin::SyncedEnforcer e(model, policy);

// using namespace std::literals::chrono_literals;
// std::chrono::duration<int64_t, std::nano> t = 5ms;

// e.StartAutoLoadPolicy(t);

// EXPECT_EQ(e.IsAutoLoadingRunning(), true);

// ASSERT_EQ(e.Enforce({ "alice", "data1", "read" }), true);
// ASSERT_EQ(e.Enforce({ "alice", "data1", "write" }), false);
// ASSERT_EQ(e.Enforce({ "alice", "data2", "read" }), false);
// ASSERT_EQ(e.Enforce({ "alice", "data2", "write" }), false);
// ASSERT_EQ(e.Enforce({ "bob", "data1", "read" }), false);
// ASSERT_EQ(e.Enforce({ "bob", "data1", "write" }), false);
// ASSERT_EQ(e.Enforce({ "bob", "data2", "read" }), false);
// ASSERT_EQ(e.Enforce({ "bob", "data2", "write" }), true);

// e.StopAutoLoadPolicy();
// std::this_thread::sleep_for(10ms);

// EXPECT_EQ(e.IsAutoLoadingRunning(), false);
// }
Loading

0 comments on commit 8540825

Please sign in to comment.