Skip to content

Commit

Permalink
Merge pull request #108 from EmperorYP7/ctest-full
Browse files Browse the repository at this point in the history
test: Management API, Utility methods and more
  • Loading branch information
hsluoyz authored Jun 29, 2021
2 parents 1a1ea59 + bf0af68 commit 5fc6caa
Show file tree
Hide file tree
Showing 7 changed files with 599 additions and 17 deletions.
3 changes: 3 additions & 0 deletions casbin/casbin.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@
#include "enforcer.h"
#include "enforcer_cached.h"
#include "enforcer_synced.h"
#include "config/config.h"
#include "persist.h"
#include "util.h"
#include "exception.h"
4 changes: 4 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@ set(CMAKE_CXX_STANDARD 17)

add_executable(
casbintest
built_in_functions_test.cpp
config_test.cpp
enforcer_test.cpp
enforcer_cached_test.cpp
enforcer_synced_test.cpp
management_api_test.cpp
util_test.cpp
)

target_include_directories(casbintest PUBLIC ${CMAKE_SOURCE_DIR})
Expand Down
167 changes: 167 additions & 0 deletions tests/built_in_functions_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
/*
* 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 for testing built in functions in casbin
*/

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

namespace {

void TestKeyMatchFn(std::string key1, std::string key2, bool res){
casbin::Scope scope = casbin::InitializeScope();
casbin::PushStringValue(scope, key1);
casbin::PushStringValue(scope, key2);
casbin::KeyMatch(scope);
bool my_res = casbin::GetBoolean(scope);
EXPECT_EQ(res, my_res);
}

TEST(TestBuiltInFunctions, TestKeyMatch) {
TestKeyMatchFn("/foo", "/foo", true);
TestKeyMatchFn("/foo", "/foo*", true);
TestKeyMatchFn("/foo", "/foo/*", false);
TestKeyMatchFn("/foo/bar", "/foo", false);
TestKeyMatchFn("/foo/bar", "/foo*", true);
TestKeyMatchFn("/foo/bar", "/foo/*", true);
TestKeyMatchFn("/foobar", "/foo", false);
TestKeyMatchFn("/foobar", "/foo*", true);
TestKeyMatchFn("/foobar", "/foo/*", false);
}

void TestKeyMatch2Fn(std::string key1, std::string key2, bool res) {
casbin::Scope scope = casbin::InitializeScope();
casbin::PushStringValue(scope, key1);
casbin::PushStringValue(scope, key2);

casbin::KeyMatch2(scope);
bool my_res = casbin::GetBoolean(scope);

EXPECT_EQ(res, my_res);
}

TEST(TestBuiltInFunctions, TestKeyMatch2){
TestKeyMatch2Fn("/foo", "/foo", true);
TestKeyMatch2Fn("/foo", "/foo*", true);
TestKeyMatch2Fn("/foo", "/foo/*", false);
TestKeyMatch2Fn("/foo/bar", "/foo", false);
TestKeyMatch2Fn("/foo/bar", "/foo*", false); // different with KeyMatch.
TestKeyMatch2Fn("/foo/bar", "/foo/*", true);
TestKeyMatch2Fn("/foobar", "/foo", false);
TestKeyMatch2Fn("/foobar", "/foo*", false); // different with KeyMatch.
TestKeyMatch2Fn("/foobar", "/foo/*", false);

TestKeyMatch2Fn("/", "/:resource", false);
TestKeyMatch2Fn("/resource1", "/:resource", true);
TestKeyMatch2Fn("/myid", "/:id/using/:resId", false);
TestKeyMatch2Fn("/myid/using/myresid", "/:id/using/:resId", true);

TestKeyMatch2Fn("/proxy/myid", "/proxy/:id/*", false);
TestKeyMatch2Fn("/proxy/myid/", "/proxy/:id/*", true);
TestKeyMatch2Fn("/proxy/myid/res", "/proxy/:id/*", true);
TestKeyMatch2Fn("/proxy/myid/res/res2", "/proxy/:id/*", true);
TestKeyMatch2Fn("/proxy/myid/res/res2/res3", "/proxy/:id/*", true);
TestKeyMatch2Fn("/proxy/", "/proxy/:id/*", false);

TestKeyMatch2Fn("/alice", "/:id", true);
TestKeyMatch2Fn("/alice/all", "/:id/all", true);
TestKeyMatch2Fn("/alice", "/:id/all", false);
TestKeyMatch2Fn("/alice/all", "/:id", false);

TestKeyMatch2Fn("/alice/all", "/:/all", false);
}

void TestKeyMatch3Fn(std::string key1, std::string key2, bool res) {
casbin::Scope scope = casbin::InitializeScope();
casbin::PushStringValue(scope, key1);
casbin::PushStringValue(scope, key2);
casbin::KeyMatch3(scope);
bool my_res = casbin::GetBoolean(scope);

EXPECT_EQ(res, my_res);
}

TEST(TestBuiltInFunctions, TestKeyMatch3) {
// keyMatch3() is similar with KeyMatch2(), except using "/proxy/{id}" instead of "/proxy/:id".
TestKeyMatch3Fn("/foo", "/foo", true);
TestKeyMatch3Fn("/foo", "/foo*", true);
TestKeyMatch3Fn("/foo", "/foo/*", false);
TestKeyMatch3Fn("/foo/bar", "/foo", false);
TestKeyMatch3Fn("/foo/bar", "/foo*", false);
TestKeyMatch3Fn("/foo/bar", "/foo/*", true);
TestKeyMatch3Fn("/foobar", "/foo", false);
TestKeyMatch3Fn("/foobar", "/foo*", false);
TestKeyMatch3Fn("/foobar", "/foo/*", false);

TestKeyMatch3Fn("/", "/{resource}", false);
TestKeyMatch3Fn("/resource1", "/{resource}", true);
TestKeyMatch3Fn("/myid", "/{id}/using/{resId}", false);
TestKeyMatch3Fn("/myid/using/myresid", "/{id}/using/{resId}", true);

TestKeyMatch3Fn("/proxy/myid", "/proxy/{id}/*", false);
TestKeyMatch3Fn("/proxy/myid/", "/proxy/{id}/*", true);
TestKeyMatch3Fn("/proxy/myid/res", "/proxy/{id}/*", true);
TestKeyMatch3Fn("/proxy/myid/res/res2", "/proxy/{id}/*", true);
TestKeyMatch3Fn("/proxy/myid/res/res2/res3", "/proxy/{id}/*", true);
TestKeyMatch3Fn("/proxy/", "/proxy/{id}/*", false);

TestKeyMatch3Fn("/myid/using/myresid", "/{id/using/{resId}", false);
}

void TestRegexMatchFn(std::string key1, std::string key2, bool res) {
casbin::Scope scope = casbin::InitializeScope();
casbin::PushStringValue(scope, key1);
casbin::PushStringValue(scope, key2);

casbin::RegexMatch(scope);
bool my_res = casbin::GetBoolean(scope);

EXPECT_EQ(res, my_res);
}

TEST(TestBuiltInFunctions, TestRegexMatch) {
TestRegexMatchFn("/topic/create", "/topic/create", true);
TestRegexMatchFn("/topic/create/123", "/topic/create", false);
TestRegexMatchFn("/topic/delete", "/topic/create", false);
TestRegexMatchFn("/topic/edit", "/topic/edit/[0-9]+", false);
TestRegexMatchFn("/topic/edit/123", "/topic/edit/[0-9]+", true);
TestRegexMatchFn("/topic/edit/abc", "/topic/edit/[0-9]+", false);
TestRegexMatchFn("/foo/delete/123", "/topic/delete/[0-9]+", false);
TestRegexMatchFn("/topic/delete/0", "/topic/delete/[0-9]+", true);
TestRegexMatchFn("/topic/edit/123s", "/topic/delete/[0-9]+", false);
}

void TestIPMatchFn(std::string ip1, std::string ip2, bool res) {
casbin::Scope scope = casbin::InitializeScope();
casbin::PushStringValue(scope, ip1);
casbin::PushStringValue(scope, ip2);

casbin::IPMatch(scope);
bool my_res = casbin::GetBoolean(scope);

EXPECT_EQ(res, my_res);
}

TEST(TestBuiltInFunctions, TestIPMatch) {
TestIPMatchFn("192.168.2.123", "192.168.2.0/24", true);
TestIPMatchFn("192.168.2.123", "192.168.3.0/24", false);
TestIPMatchFn("192.168.2.123", "192.168.2.0/16", true);
TestIPMatchFn("192.168.2.123", "192.168.2.123/32", true);
TestIPMatchFn("10.0.0.11", "10.0.0.0/8", true);
TestIPMatchFn("11.0.0.123", "10.0.0.0/8", false);
}

}
45 changes: 45 additions & 0 deletions tests/config_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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::Config
*/

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

namespace {

std::shared_ptr<casbin::Config> GetTestConfig() {
return casbin::Config::NewConfig("../../casbin/config/testdata/testini.ini");
}

TEST(TestConfig, TestDebug) {
auto config = GetTestConfig();
EXPECT_EQ(config->GetBool("debug"), true);
}

TEST(TestConfig, TestURL) {
auto config = GetTestConfig();
ASSERT_EQ(config->GetString("url"), "act.wiki");
}

TEST(TestConfig, TestRedis) {
auto config = GetTestConfig();
std::vector<std::string> values = config->GetStrings("redis::redis.key");
ASSERT_EQ(std::string("push1"), values[0]);
ASSERT_EQ(std::string("push2"), values[1]);
}

}
40 changes: 23 additions & 17 deletions tests/enforcer_synced_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@
* 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
* This is a test file showcasing the workflow of casbin::SyncedEnforcer
*/

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

// void TestSyncFn(casbin::SyncedEnforcer& e, const std::string& sub, const std::string& obj, const std::string& act, bool control) {
// bool response = e.Enforce({ sub, obj, act });
// ASSERT_EQ(response, control);
// }

// TEST(TestEnforcerSynced, TestSync) {
// std::string model = "../../examples/basic_model.conf";
// std::string policy = "../../examples/basic_policy.csv";
Expand All @@ -28,15 +33,16 @@
// 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" }));
// TestSyncFn(e, "alice", "data1", "read", true);
// TestSyncFn(e, "alice", "data1", "write", false);
// TestSyncFn(e, "alice", "data2", "read", false);
// TestSyncFn(e, "alice", "data2", "write", false);
// TestSyncFn(e, "bob", "data1", "read", false);
// TestSyncFn(e, "bob", "data1", "write", false);
// TestSyncFn(e, "bob", "data2", "read", false);
// TestSyncFn(e, "bob", "data2", "write", true);

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

Expand All @@ -52,14 +58,14 @@

// 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);
// TestSyncFn(e , "alice", "data1", "read", true);
// TestSyncFn(e , "alice", "data1", "write", false);
// TestSyncFn(e , "alice", "data2", "read", false);
// TestSyncFn(e , "alice", "data2", "write", false);
// TestSyncFn(e , "bob", "data1", "read", false);
// TestSyncFn(e , "bob", "data1", "write", false);
// TestSyncFn(e , "bob", "data2", "read", false);
// TestSyncFn(e , "bob", "data2", "write", true);

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

0 comments on commit 5fc6caa

Please sign in to comment.