|
| 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 | + |
1 | 19 | #include <gtest/gtest.h> |
2 | 20 | #include <casbin/casbin.h> |
3 | 21 |
|
@@ -30,3 +48,48 @@ TEST(TestEnforcer, TestThreeParams) { |
30 | 48 | ASSERT_EQ(e.Enforce({ "bob", "data2", "read" }), false); |
31 | 49 | ASSERT_EQ(e.Enforce({ "bob", "data2", "write" }), true); |
32 | 50 | } |
| 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