Skip to content

Commit 5fc6caa

Browse files
authored
Merge pull request #108 from EmperorYP7/ctest-full
test: Management API, Utility methods and more
2 parents 1a1ea59 + bf0af68 commit 5fc6caa

File tree

7 files changed

+599
-17
lines changed

7 files changed

+599
-17
lines changed

casbin/casbin.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,7 @@
2020
#include "enforcer.h"
2121
#include "enforcer_cached.h"
2222
#include "enforcer_synced.h"
23+
#include "config/config.h"
24+
#include "persist.h"
25+
#include "util.h"
2326
#include "exception.h"

tests/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,13 @@ set(CMAKE_CXX_STANDARD 17)
1616

1717
add_executable(
1818
casbintest
19+
built_in_functions_test.cpp
20+
config_test.cpp
1921
enforcer_test.cpp
2022
enforcer_cached_test.cpp
2123
enforcer_synced_test.cpp
24+
management_api_test.cpp
25+
util_test.cpp
2226
)
2327

2428
target_include_directories(casbintest PUBLIC ${CMAKE_SOURCE_DIR})

tests/built_in_functions_test.cpp

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
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 for testing built in functions in casbin
17+
*/
18+
19+
#include <gtest/gtest.h>
20+
#include <casbin/casbin.h>
21+
22+
namespace {
23+
24+
void TestKeyMatchFn(std::string key1, std::string key2, bool res){
25+
casbin::Scope scope = casbin::InitializeScope();
26+
casbin::PushStringValue(scope, key1);
27+
casbin::PushStringValue(scope, key2);
28+
casbin::KeyMatch(scope);
29+
bool my_res = casbin::GetBoolean(scope);
30+
EXPECT_EQ(res, my_res);
31+
}
32+
33+
TEST(TestBuiltInFunctions, TestKeyMatch) {
34+
TestKeyMatchFn("/foo", "/foo", true);
35+
TestKeyMatchFn("/foo", "/foo*", true);
36+
TestKeyMatchFn("/foo", "/foo/*", false);
37+
TestKeyMatchFn("/foo/bar", "/foo", false);
38+
TestKeyMatchFn("/foo/bar", "/foo*", true);
39+
TestKeyMatchFn("/foo/bar", "/foo/*", true);
40+
TestKeyMatchFn("/foobar", "/foo", false);
41+
TestKeyMatchFn("/foobar", "/foo*", true);
42+
TestKeyMatchFn("/foobar", "/foo/*", false);
43+
}
44+
45+
void TestKeyMatch2Fn(std::string key1, std::string key2, bool res) {
46+
casbin::Scope scope = casbin::InitializeScope();
47+
casbin::PushStringValue(scope, key1);
48+
casbin::PushStringValue(scope, key2);
49+
50+
casbin::KeyMatch2(scope);
51+
bool my_res = casbin::GetBoolean(scope);
52+
53+
EXPECT_EQ(res, my_res);
54+
}
55+
56+
TEST(TestBuiltInFunctions, TestKeyMatch2){
57+
TestKeyMatch2Fn("/foo", "/foo", true);
58+
TestKeyMatch2Fn("/foo", "/foo*", true);
59+
TestKeyMatch2Fn("/foo", "/foo/*", false);
60+
TestKeyMatch2Fn("/foo/bar", "/foo", false);
61+
TestKeyMatch2Fn("/foo/bar", "/foo*", false); // different with KeyMatch.
62+
TestKeyMatch2Fn("/foo/bar", "/foo/*", true);
63+
TestKeyMatch2Fn("/foobar", "/foo", false);
64+
TestKeyMatch2Fn("/foobar", "/foo*", false); // different with KeyMatch.
65+
TestKeyMatch2Fn("/foobar", "/foo/*", false);
66+
67+
TestKeyMatch2Fn("/", "/:resource", false);
68+
TestKeyMatch2Fn("/resource1", "/:resource", true);
69+
TestKeyMatch2Fn("/myid", "/:id/using/:resId", false);
70+
TestKeyMatch2Fn("/myid/using/myresid", "/:id/using/:resId", true);
71+
72+
TestKeyMatch2Fn("/proxy/myid", "/proxy/:id/*", false);
73+
TestKeyMatch2Fn("/proxy/myid/", "/proxy/:id/*", true);
74+
TestKeyMatch2Fn("/proxy/myid/res", "/proxy/:id/*", true);
75+
TestKeyMatch2Fn("/proxy/myid/res/res2", "/proxy/:id/*", true);
76+
TestKeyMatch2Fn("/proxy/myid/res/res2/res3", "/proxy/:id/*", true);
77+
TestKeyMatch2Fn("/proxy/", "/proxy/:id/*", false);
78+
79+
TestKeyMatch2Fn("/alice", "/:id", true);
80+
TestKeyMatch2Fn("/alice/all", "/:id/all", true);
81+
TestKeyMatch2Fn("/alice", "/:id/all", false);
82+
TestKeyMatch2Fn("/alice/all", "/:id", false);
83+
84+
TestKeyMatch2Fn("/alice/all", "/:/all", false);
85+
}
86+
87+
void TestKeyMatch3Fn(std::string key1, std::string key2, bool res) {
88+
casbin::Scope scope = casbin::InitializeScope();
89+
casbin::PushStringValue(scope, key1);
90+
casbin::PushStringValue(scope, key2);
91+
casbin::KeyMatch3(scope);
92+
bool my_res = casbin::GetBoolean(scope);
93+
94+
EXPECT_EQ(res, my_res);
95+
}
96+
97+
TEST(TestBuiltInFunctions, TestKeyMatch3) {
98+
// keyMatch3() is similar with KeyMatch2(), except using "/proxy/{id}" instead of "/proxy/:id".
99+
TestKeyMatch3Fn("/foo", "/foo", true);
100+
TestKeyMatch3Fn("/foo", "/foo*", true);
101+
TestKeyMatch3Fn("/foo", "/foo/*", false);
102+
TestKeyMatch3Fn("/foo/bar", "/foo", false);
103+
TestKeyMatch3Fn("/foo/bar", "/foo*", false);
104+
TestKeyMatch3Fn("/foo/bar", "/foo/*", true);
105+
TestKeyMatch3Fn("/foobar", "/foo", false);
106+
TestKeyMatch3Fn("/foobar", "/foo*", false);
107+
TestKeyMatch3Fn("/foobar", "/foo/*", false);
108+
109+
TestKeyMatch3Fn("/", "/{resource}", false);
110+
TestKeyMatch3Fn("/resource1", "/{resource}", true);
111+
TestKeyMatch3Fn("/myid", "/{id}/using/{resId}", false);
112+
TestKeyMatch3Fn("/myid/using/myresid", "/{id}/using/{resId}", true);
113+
114+
TestKeyMatch3Fn("/proxy/myid", "/proxy/{id}/*", false);
115+
TestKeyMatch3Fn("/proxy/myid/", "/proxy/{id}/*", true);
116+
TestKeyMatch3Fn("/proxy/myid/res", "/proxy/{id}/*", true);
117+
TestKeyMatch3Fn("/proxy/myid/res/res2", "/proxy/{id}/*", true);
118+
TestKeyMatch3Fn("/proxy/myid/res/res2/res3", "/proxy/{id}/*", true);
119+
TestKeyMatch3Fn("/proxy/", "/proxy/{id}/*", false);
120+
121+
TestKeyMatch3Fn("/myid/using/myresid", "/{id/using/{resId}", false);
122+
}
123+
124+
void TestRegexMatchFn(std::string key1, std::string key2, bool res) {
125+
casbin::Scope scope = casbin::InitializeScope();
126+
casbin::PushStringValue(scope, key1);
127+
casbin::PushStringValue(scope, key2);
128+
129+
casbin::RegexMatch(scope);
130+
bool my_res = casbin::GetBoolean(scope);
131+
132+
EXPECT_EQ(res, my_res);
133+
}
134+
135+
TEST(TestBuiltInFunctions, TestRegexMatch) {
136+
TestRegexMatchFn("/topic/create", "/topic/create", true);
137+
TestRegexMatchFn("/topic/create/123", "/topic/create", false);
138+
TestRegexMatchFn("/topic/delete", "/topic/create", false);
139+
TestRegexMatchFn("/topic/edit", "/topic/edit/[0-9]+", false);
140+
TestRegexMatchFn("/topic/edit/123", "/topic/edit/[0-9]+", true);
141+
TestRegexMatchFn("/topic/edit/abc", "/topic/edit/[0-9]+", false);
142+
TestRegexMatchFn("/foo/delete/123", "/topic/delete/[0-9]+", false);
143+
TestRegexMatchFn("/topic/delete/0", "/topic/delete/[0-9]+", true);
144+
TestRegexMatchFn("/topic/edit/123s", "/topic/delete/[0-9]+", false);
145+
}
146+
147+
void TestIPMatchFn(std::string ip1, std::string ip2, bool res) {
148+
casbin::Scope scope = casbin::InitializeScope();
149+
casbin::PushStringValue(scope, ip1);
150+
casbin::PushStringValue(scope, ip2);
151+
152+
casbin::IPMatch(scope);
153+
bool my_res = casbin::GetBoolean(scope);
154+
155+
EXPECT_EQ(res, my_res);
156+
}
157+
158+
TEST(TestBuiltInFunctions, TestIPMatch) {
159+
TestIPMatchFn("192.168.2.123", "192.168.2.0/24", true);
160+
TestIPMatchFn("192.168.2.123", "192.168.3.0/24", false);
161+
TestIPMatchFn("192.168.2.123", "192.168.2.0/16", true);
162+
TestIPMatchFn("192.168.2.123", "192.168.2.123/32", true);
163+
TestIPMatchFn("10.0.0.11", "10.0.0.0/8", true);
164+
TestIPMatchFn("11.0.0.123", "10.0.0.0/8", false);
165+
}
166+
167+
}

tests/config_test.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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::Config
17+
*/
18+
19+
#include <gtest/gtest.h>
20+
#include <casbin/casbin.h>
21+
22+
namespace {
23+
24+
std::shared_ptr<casbin::Config> GetTestConfig() {
25+
return casbin::Config::NewConfig("../../casbin/config/testdata/testini.ini");
26+
}
27+
28+
TEST(TestConfig, TestDebug) {
29+
auto config = GetTestConfig();
30+
EXPECT_EQ(config->GetBool("debug"), true);
31+
}
32+
33+
TEST(TestConfig, TestURL) {
34+
auto config = GetTestConfig();
35+
ASSERT_EQ(config->GetString("url"), "act.wiki");
36+
}
37+
38+
TEST(TestConfig, TestRedis) {
39+
auto config = GetTestConfig();
40+
std::vector<std::string> values = config->GetStrings("redis::redis.key");
41+
ASSERT_EQ(std::string("push1"), values[0]);
42+
ASSERT_EQ(std::string("push2"), values[1]);
43+
}
44+
45+
}

tests/enforcer_synced_test.cpp

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,17 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*
16-
* This is a test file showcasing the workflow of casbin::CachedEnforcer
16+
* This is a test file showcasing the workflow of casbin::SyncedEnforcer
1717
*/
1818

1919
#include <gtest/gtest.h>
2020
#include <casbin/casbin.h>
2121

22+
// void TestSyncFn(casbin::SyncedEnforcer& e, const std::string& sub, const std::string& obj, const std::string& act, bool control) {
23+
// bool response = e.Enforce({ sub, obj, act });
24+
// ASSERT_EQ(response, control);
25+
// }
26+
2227
// TEST(TestEnforcerSynced, TestSync) {
2328
// std::string model = "../../examples/basic_model.conf";
2429
// std::string policy = "../../examples/basic_policy.csv";
@@ -28,15 +33,16 @@
2833
// auto time1 = 200ms;
2934
// e.StartAutoLoadPolicy(time1);
3035

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" }));
36+
// TestSyncFn(e, "alice", "data1", "read", true);
37+
// TestSyncFn(e, "alice", "data1", "write", false);
38+
// TestSyncFn(e, "alice", "data2", "read", false);
39+
// TestSyncFn(e, "alice", "data2", "write", false);
40+
// TestSyncFn(e, "bob", "data1", "read", false);
41+
// TestSyncFn(e, "bob", "data1", "write", false);
42+
// TestSyncFn(e, "bob", "data2", "read", false);
43+
// TestSyncFn(e, "bob", "data2", "write", true);
3944

45+
// std::this_thread::sleep_for(200ms);
4046
// e.StopAutoLoadPolicy();
4147
// }
4248

@@ -52,14 +58,14 @@
5258

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

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);
61+
// TestSyncFn(e , "alice", "data1", "read", true);
62+
// TestSyncFn(e , "alice", "data1", "write", false);
63+
// TestSyncFn(e , "alice", "data2", "read", false);
64+
// TestSyncFn(e , "alice", "data2", "write", false);
65+
// TestSyncFn(e , "bob", "data1", "read", false);
66+
// TestSyncFn(e , "bob", "data1", "write", false);
67+
// TestSyncFn(e , "bob", "data2", "read", false);
68+
// TestSyncFn(e , "bob", "data2", "write", true);
6369

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

0 commit comments

Comments
 (0)