evaluate_args_test_util.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. #ifndef GRPC_TEST_CORE_UTIL_EVALUATE_ARGS_TEST_UTIL_H
  15. #define GRPC_TEST_CORE_UTIL_EVALUATE_ARGS_TEST_UTIL_H
  16. #include <grpc/support/port_platform.h>
  17. #include <list>
  18. #include <gtest/gtest.h>
  19. #include "src/core/lib/resource_quota/resource_quota.h"
  20. #include "src/core/lib/security/authorization/evaluate_args.h"
  21. #include "test/core/util/mock_authorization_endpoint.h"
  22. namespace grpc_core {
  23. class EvaluateArgsTestUtil {
  24. public:
  25. EvaluateArgsTestUtil() = default;
  26. ~EvaluateArgsTestUtil() { delete channel_args_; }
  27. void AddPairToMetadata(const char* key, const char* value) {
  28. metadata_.Append(key, Slice::FromStaticString(value),
  29. [](absl::string_view, const Slice&) {
  30. // We should never ever see an error here.
  31. abort();
  32. });
  33. }
  34. void SetLocalEndpoint(absl::string_view local_uri) {
  35. endpoint_.SetLocalAddress(local_uri);
  36. }
  37. void SetPeerEndpoint(absl::string_view peer_uri) {
  38. endpoint_.SetPeer(peer_uri);
  39. }
  40. void AddPropertyToAuthContext(const char* name, const char* value) {
  41. auth_context_.add_cstring_property(name, value);
  42. }
  43. EvaluateArgs MakeEvaluateArgs() {
  44. channel_args_ =
  45. new EvaluateArgs::PerChannelArgs(&auth_context_, &endpoint_);
  46. return EvaluateArgs(&metadata_, channel_args_);
  47. }
  48. private:
  49. MemoryAllocator allocator_ =
  50. ResourceQuota::Default()->memory_quota()->CreateMemoryAllocator(
  51. "EvaluateArgsTestUtil");
  52. ScopedArenaPtr arena_ = MakeScopedArena(1024, &allocator_);
  53. grpc_metadata_batch metadata_{arena_.get()};
  54. MockAuthorizationEndpoint endpoint_{/*local_uri=*/"", /*peer_uri=*/""};
  55. grpc_auth_context auth_context_{nullptr};
  56. EvaluateArgs::PerChannelArgs* channel_args_ = nullptr;
  57. };
  58. } // namespace grpc_core
  59. #endif // GRPC_TEST_CORE_UTIL_EVALUATE_ARGS_TEST_UTIL_H