authorization_policy_provider_test.cc 3.1 KB

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