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+ }
0 commit comments