| 1 |
// Copyright 2021 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 <gtest/gtest.h> |
| 16 |
|
| 17 |
#include <grpcpp/security/authorization_policy_provider.h> |
| 18 |
|
| 19 |
#include "test/core/util/test_config.h" |
| 20 |
#include "test/core/util/tls_utils.h" |
| 21 |
|
| 22 |
#define VALID_POLICY_PATH_1 \ |
| 23 |
"test/core/security/authorization/test_policies/valid_policy_1.json" |
| 24 |
#define VALID_POLICY_PATH_2 \ |
| 25 |
"test/core/security/authorization/test_policies/valid_policy_2.json" |
| 26 |
#define INVALID_POLICY_PATH \ |
| 27 |
"test/core/security/authorization/test_policies/invalid_policy.json" |
| 28 |
|
| 29 |
namespace grpc { |
| 30 |
|
| 31 |
TEST(AuthorizationPolicyProviderTest, StaticDataCreateReturnsProvider) { |
| 32 |
grpc::Status status; |
| 33 |
auto provider = experimental::StaticDataAuthorizationPolicyProvider::Create( |
| 34 |
grpc_core::testing::GetFileContents(VALID_POLICY_PATH_1), &status); |
| 35 |
ASSERT_NE(provider, nullptr); |
| 36 |
EXPECT_NE(provider->c_provider(), nullptr); |
| 37 |
EXPECT_TRUE(status.ok()); |
| 38 |
EXPECT_TRUE(status.error_message().empty()); |
| 39 |
} |
| 40 |
|
| 41 |
TEST(AuthorizationPolicyProviderTest, StaticDataCreateReturnsErrorStatus) { |
| 42 |
grpc::Status status; |
| 43 |
auto provider = experimental::StaticDataAuthorizationPolicyProvider::Create( |
| 44 |
grpc_core::testing::GetFileContents(INVALID_POLICY_PATH), &status); |
| 45 |
ASSERT_EQ(provider, nullptr); |
| 46 |
EXPECT_EQ(status.error_code(), grpc::StatusCode::INVALID_ARGUMENT); |
| 47 |
EXPECT_EQ(status.error_message(), "\"name\" field is not present."); |
| 48 |
} |
| 49 |
|
| 50 |
TEST(AuthorizationPolicyProviderTest, FileWatcherCreateReturnsProvider) { |
| 51 |
auto tmp_authz_policy = std::make_unique<grpc_core::testing::TmpFile>( |
| 52 |
grpc_core::testing::GetFileContents(VALID_POLICY_PATH_1)); |
| 53 |
grpc::Status status; |
| 54 |
auto provider = experimental::FileWatcherAuthorizationPolicyProvider::Create( |
| 55 |
tmp_authz_policy->name(), /*refresh_interval_sec=*/1, &status); |
| 56 |
ASSERT_NE(provider, nullptr); |
| 57 |
EXPECT_NE(provider->c_provider(), nullptr); |
| 58 |
EXPECT_TRUE(status.ok()); |
| 59 |
EXPECT_TRUE(status.error_message().empty()); |
| 60 |
} |
| 61 |
|
| 62 |
TEST(AuthorizationPolicyProviderTest, FileWatcherCreateReturnsErrorStatus) { |
| 63 |
auto tmp_authz_policy = std::make_unique<grpc_core::testing::TmpFile>( |
| 64 |
grpc_core::testing::GetFileContents(INVALID_POLICY_PATH)); |
| 65 |
grpc::Status status; |
| 66 |
auto provider = experimental::FileWatcherAuthorizationPolicyProvider::Create( |
| 67 |
tmp_authz_policy->name(), /*refresh_interval_sec=*/1, &status); |
| 68 |
ASSERT_EQ(provider, nullptr); |
| 69 |
EXPECT_EQ(status.error_code(), grpc::StatusCode::INVALID_ARGUMENT); |
| 70 |
EXPECT_EQ(status.error_message(), "\"name\" field is not present."); |
| 71 |
} |
| 72 |
|
| 73 |
} // namespace grpc |
| 74 |
|
| 75 |
int main(int argc, char** argv) { |
| 76 |
::testing::InitGoogleTest(&argc, argv); |
| 77 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 78 |
return RUN_ALL_TESTS(); |
| 79 |
} |