| 1 |
// Copyright 2020 gRPC authors. |
| 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 |
|
| 15 |
#include "src/core/lib/security/authorization/cel_authorization_engine.h" |
| 16 |
|
| 17 |
#include <gtest/gtest.h> |
| 18 |
|
| 19 |
namespace grpc_core { |
| 20 |
|
| 21 |
class CelAuthorizationEngineTest : public ::testing::Test { |
| 22 |
protected: |
| 23 |
void SetUp() override { |
| 24 |
deny_policy_ = envoy_config_rbac_v3_RBAC_new(arena_.ptr()); |
| 25 |
envoy_config_rbac_v3_RBAC_set_action(deny_policy_, 1); |
| 26 |
allow_policy_ = envoy_config_rbac_v3_RBAC_new(arena_.ptr()); |
| 27 |
envoy_config_rbac_v3_RBAC_set_action(allow_policy_, 0); |
| 28 |
} |
| 29 |
upb::Arena arena_; |
| 30 |
envoy_config_rbac_v3_RBAC* deny_policy_; |
| 31 |
envoy_config_rbac_v3_RBAC* allow_policy_; |
| 32 |
}; |
| 33 |
|
| 34 |
TEST_F(CelAuthorizationEngineTest, CreateEngineSuccessOnePolicy) { |
| 35 |
std::vector<envoy_config_rbac_v3_RBAC*> policies{allow_policy_}; |
| 36 |
std::unique_ptr<CelAuthorizationEngine> engine = |
| 37 |
CelAuthorizationEngine::CreateCelAuthorizationEngine(policies); |
| 38 |
EXPECT_NE(engine, nullptr) |
| 39 |
<< "Error: Failed to create CelAuthorizationEngine with one policy."; |
| 40 |
} |
| 41 |
|
| 42 |
TEST_F(CelAuthorizationEngineTest, CreateEngineSuccessTwoPolicies) { |
| 43 |
std::vector<envoy_config_rbac_v3_RBAC*> policies{deny_policy_, allow_policy_}; |
| 44 |
std::unique_ptr<CelAuthorizationEngine> engine = |
| 45 |
CelAuthorizationEngine::CreateCelAuthorizationEngine(policies); |
| 46 |
EXPECT_NE(engine, nullptr) |
| 47 |
<< "Error: Failed to create CelAuthorizationEngine with two policies."; |
| 48 |
} |
| 49 |
|
| 50 |
TEST_F(CelAuthorizationEngineTest, CreateEngineFailNoPolicies) { |
| 51 |
std::vector<envoy_config_rbac_v3_RBAC*> policies{}; |
| 52 |
std::unique_ptr<CelAuthorizationEngine> engine = |
| 53 |
CelAuthorizationEngine::CreateCelAuthorizationEngine(policies); |
| 54 |
EXPECT_EQ(engine, nullptr) |
| 55 |
<< "Error: Created CelAuthorizationEngine without policies."; |
| 56 |
} |
| 57 |
|
| 58 |
TEST_F(CelAuthorizationEngineTest, CreateEngineFailTooManyPolicies) { |
| 59 |
std::vector<envoy_config_rbac_v3_RBAC*> policies{deny_policy_, allow_policy_, |
| 60 |
deny_policy_}; |
| 61 |
std::unique_ptr<CelAuthorizationEngine> engine = |
| 62 |
CelAuthorizationEngine::CreateCelAuthorizationEngine(policies); |
| 63 |
EXPECT_EQ(engine, nullptr) |
| 64 |
<< "Error: Created CelAuthorizationEngine with more than two policies."; |
| 65 |
} |
| 66 |
|
| 67 |
TEST_F(CelAuthorizationEngineTest, CreateEngineFailWrongPolicyOrder) { |
| 68 |
std::vector<envoy_config_rbac_v3_RBAC*> policies{allow_policy_, deny_policy_}; |
| 69 |
std::unique_ptr<CelAuthorizationEngine> engine = |
| 70 |
CelAuthorizationEngine::CreateCelAuthorizationEngine(policies); |
| 71 |
EXPECT_EQ(engine, nullptr) << "Error: Created CelAuthorizationEngine with " |
| 72 |
"policies in the wrong order."; |
| 73 |
} |
| 74 |
|
| 75 |
} // namespace grpc_core |
| 76 |
|
| 77 |
int main(int argc, char** argv) { |
| 78 |
::testing::InitGoogleTest(&argc, argv); |
| 79 |
return RUN_ALL_TESTS(); |
| 80 |
} |