Skip to content

Commit bbfdd66

Browse files
committed
feat: Added more tests
Signed-off-by: Yash Pandey (YP) <[email protected]>
1 parent 7daac09 commit bbfdd66

File tree

4 files changed

+110
-0
lines changed

4 files changed

+110
-0
lines changed

CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
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

317
set(CMAKE_MODULE_PATH

casbin/casbin.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +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+
120
#include "enforcer.h"
221
#include "enforcer_cached.h"
322
#include "enforcer_synced.h"

tests/CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
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
add_executable(
216
casbintest
317
test_enforcer.cpp

tests/test_enforcer.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
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::Enforcer
17+
*/
18+
119
#include <gtest/gtest.h>
220
#include <casbin/casbin.h>
321

@@ -30,3 +48,48 @@ TEST(TestEnforcer, TestThreeParams) {
3048
ASSERT_EQ(e.Enforce({ "bob", "data2", "read" }), false);
3149
ASSERT_EQ(e.Enforce({ "bob", "data2", "write" }), true);
3250
}
51+
52+
TEST(TestEnforcer, TestVectorParams) {
53+
std::string model = "../../examples/basic_model_without_spaces.conf";
54+
std::string policy = "../../examples/basic_policy.csv";
55+
casbin::Enforcer e(model, policy);
56+
57+
ASSERT_EQ(e.Enforce({ "alice", "data1", "read" }), true);
58+
ASSERT_EQ(e.Enforce({ "alice", "data1", "write" }), false);
59+
ASSERT_EQ(e.Enforce({ "alice", "data2", "read" }), false);
60+
ASSERT_EQ(e.Enforce({ "alice", "data2", "write" }), false);
61+
ASSERT_EQ(e.Enforce({ "bob", "data1", "read" }), false);
62+
ASSERT_EQ(e.Enforce({ "bob", "data1", "write" }), false);
63+
ASSERT_EQ(e.Enforce({ "bob", "data2", "read" }), false);
64+
ASSERT_EQ(e.Enforce({ "bob", "data2", "write" }), true);
65+
}
66+
67+
TEST(TestEnforcer, TestMapParams) {
68+
std::string model = "../../examples/basic_model_without_spaces.conf";
69+
std::string policy = "../../examples/basic_policy.csv";
70+
casbin::Enforcer e(model, policy);
71+
72+
std::unordered_map<std::string, std::string> params = {{"sub", "alice"}, {"obj", "data1"}, {"act", "read"}};
73+
ASSERT_EQ(e.Enforce(params), true);
74+
75+
params = { {"sub","alice"},{"obj","data1"},{"act","write"} };
76+
ASSERT_EQ(e.Enforce(params), false);
77+
78+
params = { {"sub","alice"},{"obj","data2"},{"act","read"} };
79+
ASSERT_EQ(e.Enforce(params), false);
80+
81+
params = { {"sub","alice"},{"obj","data2"},{"act","write"} };
82+
ASSERT_EQ(e.Enforce(params), false);
83+
84+
params = { {"sub","bob"},{"obj","data1"},{"act","read"} };
85+
ASSERT_EQ(e.Enforce(params), false);
86+
87+
params = { {"sub","bob"},{"obj","data1"},{"act","write"} };
88+
ASSERT_EQ(e.Enforce(params), false);
89+
90+
params = { {"sub","bob"},{"obj","data2"},{"act","read"} };
91+
ASSERT_EQ(e.Enforce(params), false);
92+
93+
params = { {"sub","bob"},{"obj","data2"},{"act","write"} };
94+
ASSERT_EQ(e.Enforce(params), true);
95+
}

0 commit comments

Comments
 (0)