Skip to content

Commit 6a4d1f1

Browse files
committed
feat: Added config path for test entities
Signed-off-by: Yash Pandey (YP) <[email protected]>
1 parent aeadf83 commit 6a4d1f1

13 files changed

+146
-117
lines changed

tests/CMakeLists.txt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515
if(CASBIN_BUILD_TEST)
1616
set(CMAKE_CXX_STANDARD 17)
1717

18-
add_executable(
19-
casbintest
18+
add_definitions(-DCASBIN_PROJECT_DIR=${CMAKE_SOURCE_DIR})
19+
20+
set(CASBIN_TEST_SOURCE
2021
built_in_functions_test.cpp
2122
config_test.cpp
2223
enforcer_test.cpp
@@ -31,6 +32,12 @@ if(CASBIN_BUILD_TEST)
3132
util_test.cpp
3233
)
3334

35+
set(CASBIN_TEST_HEADER
36+
config_path.h
37+
)
38+
39+
add_executable(casbintest ${CASBIN_TEST_SOURCE} ${CASBIN_TEST_HEADER})
40+
3441
if(UNIX)
3542
set_target_properties(casbintest PROPERTIES
3643
POSITION_INDEPENDENT_CODE ON

tests/benchmarks/CMakeLists.txt

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

15-
add_executable(casbin_benchmark
16-
config_path.h
15+
add_definitions(-DCASBIN_PROJECT_DIR=${CMAKE_SOURCE_DIR})
16+
17+
set(CASBIN_BENCHMARK_SOURCE
1718
main.cpp
1819
model_b.cpp
1920
enforcer_cached_b.cpp
2021
management_api_b.cpp
2122
role_manager_b.cpp
2223
)
2324

25+
set(CASBIN_HEADER
26+
config_path.h
27+
)
28+
29+
add_executable(casbin_benchmark ${CASBIN_BENCHMARK_SOURCE} ${CASBIN_HEADER})
30+
2431
target_include_directories(casbin_benchmark PUBLIC ${CMAKE_SOURCE_DIR})
2532

2633
if(UNIX)

tests/benchmarks/config_path.h

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,25 @@
1818

1919
#include <string>
2020

21-
static const std::string relative_path = "../../../";
22-
static const std::string basic_model_path = relative_path + "examples/basic_model.conf";
23-
static const std::string basic_policy_path = relative_path + "examples/basic_policy.csv";
24-
static const std::string rbac_model_path = relative_path + "examples/rbac_model.conf";
25-
static const std::string rbac_policy_path = relative_path + "examples/rbac_policy.csv";
26-
static const std::string rbac_with_resource_roles_model_path = relative_path + "examples/rbac_with_resource_roles_model.conf";
27-
static const std::string rbac_with_resource_roles_policy_path = relative_path + "examples/rbac_with_resource_roles_policy.csv";
28-
static const std::string rbac_with_domains_model_path = relative_path + "examples/rbac_with_domains_model.conf";
29-
static const std::string rbac_with_domains_policy_path = relative_path + "examples/rbac_with_domains_policy.csv";
30-
static const std::string keymatch_model_path = relative_path + "examples/keymatch_model.conf";
31-
static const std::string keymatch_policy_path = relative_path + "examples/keymatch_policy.csv";
32-
static const std::string rbac_with_deny_model_path = relative_path + "examples/rbac_with_deny_model.conf";
33-
static const std::string rbac_with_deny_policy_path = relative_path + "examples/rbac_with_deny_policy.csv";
34-
static const std::string priority_model_path = relative_path + "examples/priority_model.conf";
35-
static const std::string priority_policy_path = relative_path + "examples/priority_policy.csv";
21+
#define STRINGIFY_IMPL(x) #x
22+
#define STRINGIFY(x) STRINGIFY_IMPL(x)
23+
24+
static const std::string relative_path = STRINGIFY(CASBIN_PROJECT_DIR);
25+
26+
static const std::string basic_model_path = relative_path + "/examples/basic_model.conf";
27+
static const std::string basic_policy_path = relative_path + "/examples/basic_policy.csv";
28+
29+
static const std::string rbac_model_path = relative_path + "/examples/rbac_model.conf";
30+
static const std::string rbac_policy_path = relative_path + "/examples/rbac_policy.csv";
31+
static const std::string rbac_with_resource_roles_model_path = relative_path + "/examples/rbac_with_resource_roles_model.conf";
32+
static const std::string rbac_with_resource_roles_policy_path = relative_path + "/examples/rbac_with_resource_roles_policy.csv";
33+
static const std::string rbac_with_domains_model_path = relative_path + "/examples/rbac_with_domains_model.conf";
34+
static const std::string rbac_with_domains_policy_path = relative_path + "/examples/rbac_with_domains_policy.csv";
35+
static const std::string rbac_with_deny_model_path = relative_path + "/examples/rbac_with_deny_model.conf";
36+
static const std::string rbac_with_deny_policy_path = relative_path + "/examples/rbac_with_deny_policy.csv";
37+
38+
static const std::string keymatch_model_path = relative_path + "/examples/keymatch_model.conf";
39+
static const std::string keymatch_policy_path = relative_path + "/examples/keymatch_policy.csv";
40+
41+
static const std::string priority_model_path = relative_path + "/examples/priority_model.conf";
42+
static const std::string priority_policy_path = relative_path + "/examples/priority_policy.csv";

tests/benchmarks/enforcer_cached_b.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ BENCHMARK(BenchmarkCachedRBACModelWithDomains);
135135

136136
// ---- TODO ----
137137
// static void BenchmarkCachedABACModel(benchmark::State& state) {
138-
// casbin::CachedEnforcer e("examples/abac_model.conf", false);
138+
// casbin::CachedEnforcer e(abac_model_path, false);
139139
// auto data1 = casbin::GetData({
140140
// {"Name", "data1"},
141141
// {"Owner", "alice"}

tests/config_path.h

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2021 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 contains the path to various config resources within casbin
17+
*/
18+
19+
#include <string>
20+
21+
#define STRINGIFY_IMPL(x) #x
22+
#define STRINGIFY(x) STRINGIFY_IMPL(x)
23+
24+
static const std::string relative_path = STRINGIFY(CASBIN_PROJECT_DIR);
25+
26+
static const std::string basic_model_path = relative_path + "/examples/basic_model.conf";
27+
static const std::string basic_policy_path = relative_path + "/examples/basic_policy.csv";
28+
static const std::string basic_model_without_spaces_path = relative_path + "/examples/basic_model_without_spaces.conf";
29+
static const std::string basic_without_users_model_path = relative_path + "/examples/basic_without_users_model.conf";
30+
static const std::string basic_without_users_policy_path = relative_path + "/examples/basic_without_users_policy.csv";
31+
static const std::string basic_without_resources_model_path = relative_path + "/examples/basic_without_resources_model.conf";
32+
static const std::string basic_without_resources_policy_path = relative_path + "/examples/basic_without_resources_policy.csv";
33+
static const std::string basic_with_root_model_path = relative_path + "/examples/basic_with_root_model.conf";
34+
35+
static const std::string rbac_with_not_deny_model_path = relative_path + "/examples/rbac_with_not_deny_model.conf";
36+
static const std::string rbac_model_path = relative_path + "/examples/rbac_model.conf";
37+
static const std::string rbac_policy_path = relative_path + "/examples/rbac_policy.csv";
38+
static const std::string rbac_with_resource_roles_model_path = relative_path + "/examples/rbac_with_resource_roles_model.conf";
39+
static const std::string rbac_with_resource_roles_policy_path = relative_path + "/examples/rbac_with_resource_roles_policy.csv";
40+
static const std::string rbac_with_domains_model_path = relative_path + "/examples/rbac_with_domains_model.conf";
41+
static const std::string rbac_with_domains_policy_path = relative_path + "/examples/rbac_with_domains_policy.csv";
42+
static const std::string rbac_with_pattern_model_path = relative_path + "/examples/rbac_with_pattern_model.conf";
43+
static const std::string rbac_with_pattern_policy_path = relative_path + "/examples/rbac_with_pattern_policy.csv";
44+
static const std::string rbac_with_hierarchy_policy_path = relative_path + "/examples/rbac_with_hierarchy_policy.csv";
45+
static const std::string rbac_with_hierarchy_with_domains_policy_path = relative_path + "/examples/rbac_with_hierarchy_with_domains_policy.csv";
46+
static const std::string rbac_with_deny_model_path = relative_path + "/examples/rbac_with_deny_model.conf";
47+
static const std::string rbac_with_deny_policy_path = relative_path + "/examples/rbac_with_deny_policy.csv";
48+
49+
static const std::string keymatch_model_path = relative_path + "/examples/keymatch_model.conf";
50+
static const std::string keymatch_policy_path = relative_path + "/examples/keymatch_policy.csv";
51+
52+
static const std::string priority_model_path = relative_path + "/examples/priority_model.conf";
53+
static const std::string priority_policy_path = relative_path + "/examples/priority_policy.csv";

tests/enforcer_cached_test.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,12 @@
1818

1919
#include <gtest/gtest.h>
2020
#include <casbin/casbin.h>
21+
#include "config_path.h"
2122

2223
namespace {
2324

2425
TEST(TestEnforcerCached, TestCache) {
25-
std::string model = "../../examples/basic_model.conf";
26-
std::string policy = "../../examples/basic_policy.csv";
27-
casbin::CachedEnforcer e(model, policy);
26+
casbin::CachedEnforcer e(basic_model_path, basic_policy_path);
2827
ASSERT_EQ(e.Enforce({ "alice", "data1", "read" }), true);
2928
ASSERT_EQ(e.Enforce({ "alice", "data1", "write" }), false);
3029
ASSERT_EQ(e.Enforce({ "alice", "data2", "read" }), false);

tests/enforcer_synced_test.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ namespace {
2727
// }
2828

2929
// TEST(TestEnforcerSynced, TestSync) {
30-
// std::string model = "../../examples/basic_model.conf";
31-
// std::string policy = "../../examples/basic_policy.csv";
32-
// casbin::SyncedEnforcer e(model, policy);
30+
// casbin::SyncedEnforcer e(basic_model_path, basic_policy_path);
3331

3432
// using namespace std::literals::chrono_literals;
3533
// auto time1 = 200ms;
@@ -49,9 +47,7 @@ namespace {
4947
// }
5048

5149
// TEST(TestEnforcerSynced, TestStopLoadPolicy) {
52-
// std::string model = "../../examples/basic_model.conf";
53-
// std::string policy = "../../examples/basic_policy.csv";
54-
// casbin::SyncedEnforcer e(model, policy);
50+
// casbin::SyncedEnforcer e(basic_model_path, basic_policy_path);
5551

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

tests/enforcer_test.cpp

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,12 @@
1818

1919
#include <gtest/gtest.h>
2020
#include <casbin/casbin.h>
21+
#include "config_path.h"
2122

2223
namespace {
2324

2425
TEST(TestEnforcer, TestFourParams) {
25-
std::string model = "../../examples/rbac_with_domains_model.conf";
26-
std::string policy = "../../examples/rbac_with_domains_policy.csv";
27-
casbin::Enforcer e = casbin::Enforcer(model, policy);
26+
casbin::Enforcer e(rbac_with_domains_model_path, rbac_with_domains_policy_path);
2827

2928
ASSERT_EQ(e.Enforce({ "alice", "domain1", "data1", "read" }), true);
3029
ASSERT_EQ(e.Enforce({ "alice", "domain1", "data1", "write" }), true);
@@ -37,9 +36,7 @@ TEST(TestEnforcer, TestFourParams) {
3736
}
3837

3938
TEST(TestEnforcer, TestThreeParams) {
40-
std::string model = "../../examples/basic_model_without_spaces.conf";
41-
std::string policy = "../../examples/basic_policy.csv";
42-
casbin::Enforcer e(model, policy);
39+
casbin::Enforcer e(basic_model_without_spaces_path, basic_policy_path);
4340

4441
ASSERT_EQ(e.Enforce({ "alice", "data1", "read" }), true);
4542
ASSERT_EQ(e.Enforce({ "alice", "data1", "write" }), false);
@@ -52,9 +49,7 @@ TEST(TestEnforcer, TestThreeParams) {
5249
}
5350

5451
TEST(TestEnforcer, TestVectorParams) {
55-
std::string model = "../../examples/basic_model_without_spaces.conf";
56-
std::string policy = "../../examples/basic_policy.csv";
57-
casbin::Enforcer e(model, policy);
52+
casbin::Enforcer e(basic_model_without_spaces_path, basic_policy_path);
5853

5954
ASSERT_EQ(e.Enforce({ "alice", "data1", "read" }), true);
6055
ASSERT_EQ(e.Enforce({ "alice", "data1", "write" }), false);
@@ -67,9 +62,7 @@ TEST(TestEnforcer, TestVectorParams) {
6762
}
6863

6964
TEST(TestEnforcer, TestMapParams) {
70-
std::string model = "../../examples/basic_model_without_spaces.conf";
71-
std::string policy = "../../examples/basic_policy.csv";
72-
casbin::Enforcer e(model, policy);
65+
casbin::Enforcer e(basic_model_without_spaces_path, basic_policy_path);
7366

7467
casbin::DataMap params = {{"sub", "alice"}, {"obj", "data1"}, {"act", "read"}};
7568
ASSERT_EQ(e.Enforce(params), true);

tests/management_api_test.cpp

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,12 @@
1818

1919
#include <gtest/gtest.h>
2020
#include <casbin/casbin.h>
21+
#include "config_path.h"
2122

2223
namespace {
2324

2425
TEST(TestManagementAPI, TestGetList) {
25-
std::string model = "../../examples/rbac_model.conf";
26-
std::string policy = "../../examples/rbac_policy.csv";
27-
casbin::Enforcer e(model, policy);
26+
casbin::Enforcer e(rbac_model_path, rbac_policy_path);
2827

2928
ASSERT_TRUE(casbin::ArrayEquals({ "alice", "bob", "data2_admin" }, e.GetAllSubjects()));
3029
ASSERT_TRUE(casbin::ArrayEquals({ "data1", "data2" }, e.GetAllObjects()));
@@ -79,9 +78,7 @@ void TestHasGroupingPolicy(casbin::Enforcer& e, const std::vector<std::string>&
7978
}
8079

8180
TEST(TestManagementAPI, TestGetPolicyAPI) {
82-
std::string model = "../../examples/rbac_model.conf";
83-
std::string policy = "../../examples/rbac_policy.csv";
84-
casbin::Enforcer e(model, policy);
81+
casbin::Enforcer e(rbac_model_path, rbac_policy_path);
8582

8683
TestGetPolicy(e, {
8784
{"alice", "data1", "read"},
@@ -123,10 +120,8 @@ TEST(TestManagementAPI, TestGetPolicyAPI) {
123120

124121

125122
TEST(TestManagementAPI, TestModifyPolicyAPI) {
126-
std::string model = "../../examples/rbac_model.conf";
127-
std::string policy = "../../examples/rbac_policy.csv";
128-
std::shared_ptr<casbin::Adapter> adapter = std::make_shared<casbin::BatchFileAdapter>(policy);
129-
casbin::Enforcer e(model, adapter);
123+
std::shared_ptr<casbin::Adapter> adapter = std::make_shared<casbin::BatchFileAdapter>(rbac_policy_path);
124+
casbin::Enforcer e(rbac_model_path, adapter);
130125

131126
TestGetPolicy(e, {
132127
{"alice", "data1", "read"},
@@ -200,10 +195,8 @@ TEST(TestManagementAPI, TestModifyPolicyAPI) {
200195
}
201196

202197
TEST(TestManagementAPI, TestModifyGroupingPolicyAPI) {
203-
std::string model = "../../examples/rbac_model.conf";
204-
std::string policy = "../../examples/rbac_policy.csv";
205-
std::shared_ptr<casbin::Adapter> adapter = std::make_shared<casbin::BatchFileAdapter>(policy);
206-
casbin::Enforcer e(model, adapter);
198+
std::shared_ptr<casbin::Adapter> adapter = std::make_shared<casbin::BatchFileAdapter>(rbac_policy_path);
199+
casbin::Enforcer e(rbac_model_path, adapter);
207200

208201
ASSERT_TRUE(casbin::ArrayEquals({"data2_admin"}, e.GetRolesForUser("alice")));
209202
ASSERT_TRUE(casbin::ArrayEquals({}, e.GetRolesForUser("bob")));

0 commit comments

Comments
 (0)